You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

379 lines
12 KiB

  1. require("preload.Plugin")
  2. require("preload.preloadLanguage")
  3. require("preload.PreloadTools")
  4. collectgarbage("setpause", 100)
  5. collectgarbage("setstepmul", 5000)
  6. CONST_WIDTH = 1280
  7. CONST_HEIGHT = 720
  8. if isReviewByPortrait() then
  9. print("main, isReviewByPortrait = true")
  10. CONST_WIDTH = 720
  11. CONST_HEIGHT = 1280
  12. setOrientation(0)
  13. else
  14. print("main, isReviewByPortrait = false")
  15. CONST_WIDTH = 1280
  16. CONST_HEIGHT = 720
  17. setOrientation(1)
  18. end
  19. --错误类型
  20. GAME_ERROR_TYPE =
  21. {
  22. TEXTURE = "TextureError", --纹理错误
  23. UPLOAD = "uploadByUser", --用户上传的日记
  24. AUTO = "automaticToUpload", --自动上传日志
  25. HTTP = "HttpFail", --
  26. TABLENOFIND = "tableNoFind", --
  27. UPDATEFAILED = "UpdateFailed", --热更新失败
  28. TABLELOADSTRING = "tableloadstring", --热更新失败
  29. UNKNOW_TYPE = "unknowtype", --未知的错误类型
  30. DING_HAO_ERROR = "DingHaoError", --未知的错误类型
  31. }
  32. local _logUploading = false;
  33. -- 目标平台
  34. local target_platform = cc.Application:getInstance():getTargetPlatform()
  35. -- 调用游戏逻辑里面的 showTooltip 来显示提示
  36. local function showToolTipsInPreload(message)
  37. if showTooltip then
  38. showTooltip(message)
  39. end
  40. end
  41. -- 获取用于上传的日志名
  42. local function getFileNameForUpload(key)
  43. -- 获取角色ID
  44. local actorId = loadUserId();
  45. local resVersion = loadVersion()
  46. local appPackageName = getAppPackageName()
  47. local appVersionName = getAppVersionName()
  48. local appVersionCode = getAppVersionCode()
  49. print("=====================================")
  50. print("===== actorId : ", actorId)
  51. print("===== resVersion : ", resVersion)
  52. print("===== appPackageName : ", appPackageName)
  53. print("===== appVersionName : ", appVersionName)
  54. print("===== appVersionCode : ", appVersionCode)
  55. print("=====================================")
  56. -- 文件名
  57. local logFileName = tostring(target_platform)
  58. .. "_" .. tostring(resVersion)
  59. .. "_" .. tostring(actorId)
  60. .. "_" .. tostring(key)
  61. .. "_" .. os.date("%Y%m%d%H%M%S", os.time())
  62. .. "_" .. tostring(appVersionName)
  63. .. "_" .. tostring(appVersionCode)
  64. .. "_" .. string.format("%08X.7z" , math.random(1 , 2147483647))
  65. return logFileName;
  66. end
  67. -- 上传错误日志
  68. function uploadErrorLogs(msg)
  69. local tracebackInfo = debug.traceback();
  70. -- 打印错误日志
  71. local err = "LUA ERROR: " .. tostring(msg) .. "\n" .. tracebackInfo;
  72. print(err);
  73. -- 苹果审核版本不理会错误信息
  74. if isReviewVersion() and not isDevMode() then
  75. return
  76. end
  77. -- 文件名
  78. local key = md5.sumhexa(tracebackInfo)
  79. local logFileName = getFileNameForUpload(key)
  80. -- 用于选择上传错误日志后重启
  81. local function onOK()
  82. local function upload()
  83. if not _logUploading then
  84. local function onUploadEnd()
  85. _logUploading = false;
  86. -- 上传日志之后重启客户端
  87. cc.Application:getInstance():restart();
  88. end
  89. _logUploading = true;
  90. converUrlToIp(RomSetting.LogFTPIP, function(urlNew, header)
  91. cc.CURLManager:getInstance():uploadLog(
  92. urlNew,
  93. RomSetting.LogFTPUserName,
  94. RomSetting.LogFTPPassword,
  95. logFileName,
  96. onUploadEnd);
  97. end)
  98. end
  99. end
  100. xpcall(upload , function(...)print(...)end)
  101. end
  102. showErrorView(err, onOK)
  103. end
  104. -- 上传错误日志不重启也不展示界面
  105. function uploadLogs(errName)
  106. if not errName then
  107. showToolTipsInPreload("上传但没指定错误类型!")
  108. end
  109. -- 如果没有指定类型,就指定为未知的
  110. errName = errName or GAME_ERROR_TYPE.UNKNOW_TYPE
  111. -- 有些类型的日志不需要上传
  112. if errName == GAME_ERROR_TYPE.HTTP -- 访问WEB失败的
  113. --or errName == GAME_ERROR_TYPE.UPLOAD -- 用户主动上传的
  114. or errName == GAME_ERROR_TYPE.UPDATEFAILED -- 热更新连接服务器失败的
  115. then
  116. return
  117. end
  118. local logFileName = getFileNameForUpload(errName)
  119. -- 用于选择上传错误日志后重启
  120. local function upload()
  121. if not _logUploading then
  122. local function onUploadEnd()
  123. _logUploading = false;
  124. --主动上传日志的需要提示,其他不需要
  125. if errName == GAME_ERROR_TYPE.UPLOAD then
  126. showToolTipsInPreload("上传成功!")
  127. end
  128. end
  129. _logUploading = true;
  130. converUrlToIp(RomSetting.LogFTPIP, function(urlNew, header)
  131. cc.CURLManager:getInstance():uploadLog(
  132. urlNew,
  133. RomSetting.LogFTPUserName,
  134. RomSetting.LogFTPPassword,
  135. logFileName,
  136. onUploadEnd);
  137. end)
  138. end
  139. end
  140. xpcall(upload , function(...)print(...)end)
  141. end
  142. -- 调试信息
  143. function __G__TRACKBACK__(msg)
  144. uploadErrorLogs(msg);
  145. end
  146. -- 上传宕机日志的函数
  147. function uploadDmpToftp()
  148. if target_platform == 0 then
  149. --print("window版本就不需要上传宕机日志了")
  150. return;
  151. end
  152. -- 拼接名字
  153. local name_file_prefix = "crash_" .. tostring(target_platform) .. "_" .. getDeviceCPUType() .. "_" .. getAppVersion()
  154. -- 上传下宕机日志
  155. cc.CURLManager:getInstance():postALLDump(RomSetting.LogFTPIP, RomSetting.LogFTPUserName, RomSetting.LogFTPPassword, name_file_prefix);
  156. end
  157. -- 根据屏幕大小匹配好资源尺寸
  158. local function adaptResolutionSize()
  159. if RomSetting.SuitScreen then
  160. local glview = cc.Director:getInstance():getOpenGLView();
  161. local winSize = glview:getFrameSize();
  162. local resourceSize = {width = CONST_WIDTH ; height = CONST_HEIGHT};
  163. -- 实际比率
  164. local radio = winSize.width / winSize.height;
  165. -- 标准比率
  166. local resourceRadio = resourceSize.width / resourceSize.height;
  167. print("实际分辨率" , winSize.width , winSize.height);
  168. local finalResourceSize;
  169. if radio > resourceRadio then
  170. -- 比16:9扁,就固定高度,让宽度变宽
  171. local realRadio = resourceSize.height / winSize.height;
  172. finalResourceSize = {width = winSize.width * realRadio; height = winSize.height * realRadio};
  173. print("资源分辨率1" , finalResourceSize.width , finalResourceSize.height);
  174. glview:setDesignResolutionSize(finalResourceSize.width , finalResourceSize.height, 0);
  175. else
  176. -- 比16:9高,就固定宽度,让高度变高
  177. local realRadio = resourceSize.width / winSize.width;
  178. finalResourceSize = {width = winSize.width * realRadio; height = winSize.height * realRadio};
  179. print("资源分辨率2" , finalResourceSize.width , finalResourceSize.height);
  180. glview:setDesignResolutionSize(finalResourceSize.width , finalResourceSize.height, 0);
  181. end
  182. g_radio_x = finalResourceSize.width / resourceSize.width
  183. g_radio_y = finalResourceSize.height / resourceSize.height
  184. else
  185. g_radio_x = 1
  186. g_radio_y = 1
  187. local glview = cc.Director:getInstance():getOpenGLView();
  188. local winSize = glview:getFrameSize();
  189. local resourceSize = {width = CONST_WIDTH ; height = CONST_HEIGHT};
  190. -- 实际比率
  191. local radio = winSize.width / winSize.height;
  192. -- 标准比率
  193. local resourceRadio = resourceSize.width / resourceSize.height;
  194. print("实际分辨率" , winSize.width , winSize.height);
  195. local finalResourceSize;
  196. -- 比16:9还扁,就用标准高度,让宽度变宽
  197. if radio > resourceRadio then
  198. finalResourceSize = {width = resourceSize.width ; height = resourceSize.height};
  199. print("资源分辨率(固定宽度)" , finalResourceSize.width , finalResourceSize.height);
  200. glview:setDesignResolutionSize(finalResourceSize.width , finalResourceSize.height, 2);
  201. -- 比16:9高,就固定宽度,让高度变高
  202. else
  203. finalResourceSize = {width = resourceSize.width ; height = resourceSize.width / radio};
  204. print("资源分辨率" , finalResourceSize.width , finalResourceSize.height);
  205. glview:setDesignResolutionSize(finalResourceSize.width , finalResourceSize.height, 0);
  206. end
  207. if table.tostring then
  208. print("getWinSize" , table.tostring(cc.Director:getInstance():getWinSize()));
  209. print("getFrameSize" , table.tostring(glview:getFrameSize()));
  210. print("getDesignResolutionSize" , table.tostring(glview:getDesignResolutionSize()));
  211. print("finalResourceSize" , table.tostring(finalResourceSize));
  212. end
  213. end
  214. end
  215. -- 侦听键盘返回键退出游戏
  216. local function keyExitEvent()
  217. local function onOK(value)
  218. if tonumber(value) == 1 then
  219. cc.Director:getInstance():endToLua();
  220. end
  221. end
  222. local function onKeyPressed(keyCode , event)
  223. end
  224. local function onKeyReleased(keyCode , event)
  225. -- Windows的ESC 或 Android的返回键
  226. if keyCode == 6 then
  227. local params =
  228. {
  229. "是否要退出游戏?";
  230. nil,
  231. 2;
  232. onOK;
  233. }
  234. if PluginDevice then
  235. PluginDevice:callMemberMethod("showDialog" , "(Ljava/lang/String;Ljava/lang/String;II)V" , params);
  236. end
  237. end
  238. end
  239. local listener = cc.EventListenerKeyboard:create(onKeyPressed , onKeyReleased);
  240. cc.Director:getInstance():getEventDispatcher():addEventListenerWithFixedPriority(listener, 1)
  241. end
  242. local function initLogFileManager()
  243. logFileManager = require("preload.AutoUpdate.LogFileManager")
  244. logFileManager:init(RomSetting)
  245. end
  246. local function main()
  247. initLogFileManager()
  248. -- 屏幕常亮
  249. cc.Device:setKeepScreenOn(true);
  250. -- 打印设备信息
  251. devicePrintBaseInfo()
  252. -- initialize director
  253. local windowSize = {width = CONST_WIDTH, height = CONST_HEIGHT};
  254. if target_platform == 0 then
  255. --windowSize = {width = CONST_WIDTH , height = CONST_HEIGHT }; -- stander
  256. --windowSize = {width = 2436 * 0.5, height = 1125 * 0.5}; -- iPhonex
  257. --windowSize = {width = 2732 * 0.4, height = 2048 * 0.4}; -- iPad 12.9
  258. windowSize = {width = 1280 * 0.65, height = 720 * 0.65}; -- iPhonex
  259. end
  260. local director = cc.Director:getInstance();
  261. local glview = director:getOpenGLView();
  262. if not glview then
  263. glview = cc.GLView:createWithRect("w3h", {x=0,y=0,width=windowSize.width,height=windowSize.height});
  264. director:setOpenGLView(glview);
  265. end
  266. -- 匹配屏幕尺寸
  267. adaptResolutionSize();
  268. local function applicationScreenSizeChanged()
  269. print("applicationScreenSizeChanged()");
  270. adaptResolutionSize();
  271. local glview = director:getOpenGLView();
  272. local scene = director:getRunningScene();
  273. if not glview then
  274. print("applicationScreenSizeChanged() glview is nil");
  275. end
  276. if not scene then
  277. print("applicationScreenSizeChanged() scene is nil");
  278. end
  279. if glview and scene then
  280. local oldContentSize = scene:getContentSize();
  281. local newContentSize = glview:getDesignResolutionSize();
  282. -- 全部重新布局
  283. local function visit(node)
  284. if node.requestDoLayout then
  285. --print("重新布局:" , node:getName());
  286. node:requestDoLayout();
  287. end
  288. if node.getContentSize then
  289. local size = node:getContentSize();
  290. if size.width == oldContentSize.width and size.height == oldContentSize.height then
  291. --print("重新设置大小:" , node:getName());
  292. node:setContentSize(newContentSize);
  293. end
  294. end
  295. if node.updateSizeAndPosition then
  296. node:updateSizeAndPosition();
  297. end
  298. end
  299. scene:visitNode(visit);
  300. end
  301. end
  302. cc.Director:getInstance():getEventDispatcher():addEventListenerWithFixedPriority(cc.EventListenerCustom:create("applicationScreenSizeChanged" , applicationScreenSizeChanged) , 1);
  303. -- 侦听退出事件
  304. keyExitEvent();
  305. local isTestUpdate = false
  306. if isTestUpdate or target_platform ~= 0 then
  307. local mainScene = cc.Scene:create();
  308. mainScene:setContentSize(glview:getDesignResolutionSize());
  309. mainScene:setAnchorPoint({x=0,y=0});
  310. director:runWithScene(mainScene);
  311. local function onUpdateFinished()
  312. require("luaScript.startup")
  313. end
  314. local updaterNew=require("preload.AutoUpdate.AutoUpdater"):new(RomSetting , mainScene, onUpdateFinished)
  315. updaterNew:startUpdate()
  316. else
  317. getServerConfigs(function()
  318. require("luaScript.startup")
  319. end, true)
  320. end
  321. if target_platform == 0 then
  322. pcall(require, "luaScript.Win32Ext")
  323. end
  324. end
  325. xpcall(main, __G__TRACKBACK__)