require("preload.Plugin") require("preload.preloadLanguage") require("preload.PreloadTools") collectgarbage("setpause", 100) collectgarbage("setstepmul", 5000) CONST_WIDTH = 1280 CONST_HEIGHT = 720 if isReviewByPortrait() then print("main, isReviewByPortrait = true") CONST_WIDTH = 720 CONST_HEIGHT = 1280 setOrientation(0) else print("main, isReviewByPortrait = false") CONST_WIDTH = 1280 CONST_HEIGHT = 720 setOrientation(1) end --错误类型 GAME_ERROR_TYPE = { TEXTURE = "TextureError", --纹理错误 UPLOAD = "uploadByUser", --用户上传的日记 AUTO = "automaticToUpload", --自动上传日志 HTTP = "HttpFail", -- TABLENOFIND = "tableNoFind", -- UPDATEFAILED = "UpdateFailed", --热更新失败 TABLELOADSTRING = "tableloadstring", --热更新失败 UNKNOW_TYPE = "unknowtype", --未知的错误类型 DING_HAO_ERROR = "DingHaoError", --未知的错误类型 } local _logUploading = false; -- 目标平台 local target_platform = cc.Application:getInstance():getTargetPlatform() -- 调用游戏逻辑里面的 showTooltip 来显示提示 local function showToolTipsInPreload(message) if showTooltip then showTooltip(message) end end -- 获取用于上传的日志名 local function getFileNameForUpload(key) -- 获取角色ID local actorId = loadUserId(); local resVersion = loadVersion() local appPackageName = getAppPackageName() local appVersionName = getAppVersionName() local appVersionCode = getAppVersionCode() print("=====================================") print("===== actorId : ", actorId) print("===== resVersion : ", resVersion) print("===== appPackageName : ", appPackageName) print("===== appVersionName : ", appVersionName) print("===== appVersionCode : ", appVersionCode) print("=====================================") -- 文件名 local logFileName = tostring(target_platform) .. "_" .. tostring(resVersion) .. "_" .. tostring(actorId) .. "_" .. tostring(key) .. "_" .. os.date("%Y%m%d%H%M%S", os.time()) .. "_" .. tostring(appVersionName) .. "_" .. tostring(appVersionCode) .. "_" .. string.format("%08X.7z" , math.random(1 , 2147483647)) return logFileName; end -- 上传错误日志 function uploadErrorLogs(msg) local tracebackInfo = debug.traceback(); -- 打印错误日志 local err = "LUA ERROR: " .. tostring(msg) .. "\n" .. tracebackInfo; print(err); -- 苹果审核版本不理会错误信息 if isReviewVersion() and not isDevMode() then return end -- 文件名 local key = md5.sumhexa(tracebackInfo) local logFileName = getFileNameForUpload(key) -- 用于选择上传错误日志后重启 local function onOK() local function upload() if not _logUploading then local function onUploadEnd() _logUploading = false; -- 上传日志之后重启客户端 cc.Application:getInstance():restart(); end _logUploading = true; converUrlToIp(RomSetting.LogFTPIP, function(urlNew, header) cc.CURLManager:getInstance():uploadLog( urlNew, RomSetting.LogFTPUserName, RomSetting.LogFTPPassword, logFileName, onUploadEnd); end) end end xpcall(upload , function(...)print(...)end) end showErrorView(err, onOK) end -- 上传错误日志不重启也不展示界面 function uploadLogs(errName) if not errName then showToolTipsInPreload("上传但没指定错误类型!") end -- 如果没有指定类型,就指定为未知的 errName = errName or GAME_ERROR_TYPE.UNKNOW_TYPE -- 有些类型的日志不需要上传 if errName == GAME_ERROR_TYPE.HTTP -- 访问WEB失败的 --or errName == GAME_ERROR_TYPE.UPLOAD -- 用户主动上传的 or errName == GAME_ERROR_TYPE.UPDATEFAILED -- 热更新连接服务器失败的 then return end local logFileName = getFileNameForUpload(errName) -- 用于选择上传错误日志后重启 local function upload() if not _logUploading then local function onUploadEnd() _logUploading = false; --主动上传日志的需要提示,其他不需要 if errName == GAME_ERROR_TYPE.UPLOAD then showToolTipsInPreload("上传成功!") end end _logUploading = true; converUrlToIp(RomSetting.LogFTPIP, function(urlNew, header) cc.CURLManager:getInstance():uploadLog( urlNew, RomSetting.LogFTPUserName, RomSetting.LogFTPPassword, logFileName, onUploadEnd); end) end end xpcall(upload , function(...)print(...)end) end -- 调试信息 function __G__TRACKBACK__(msg) uploadErrorLogs(msg); end -- 上传宕机日志的函数 function uploadDmpToftp() if target_platform == 0 then --print("window版本就不需要上传宕机日志了") return; end -- 拼接名字 local name_file_prefix = "crash_" .. tostring(target_platform) .. "_" .. getDeviceCPUType() .. "_" .. getAppVersion() -- 上传下宕机日志 cc.CURLManager:getInstance():postALLDump(RomSetting.LogFTPIP, RomSetting.LogFTPUserName, RomSetting.LogFTPPassword, name_file_prefix); end -- 根据屏幕大小匹配好资源尺寸 local function adaptResolutionSize() if RomSetting.SuitScreen then local glview = cc.Director:getInstance():getOpenGLView(); local winSize = glview:getFrameSize(); local resourceSize = {width = CONST_WIDTH ; height = CONST_HEIGHT}; -- 实际比率 local radio = winSize.width / winSize.height; -- 标准比率 local resourceRadio = resourceSize.width / resourceSize.height; print("实际分辨率" , winSize.width , winSize.height); local finalResourceSize; if radio > resourceRadio then -- 比16:9扁,就固定高度,让宽度变宽 local realRadio = resourceSize.height / winSize.height; finalResourceSize = {width = winSize.width * realRadio; height = winSize.height * realRadio}; print("资源分辨率1" , finalResourceSize.width , finalResourceSize.height); glview:setDesignResolutionSize(finalResourceSize.width , finalResourceSize.height, 0); else -- 比16:9高,就固定宽度,让高度变高 local realRadio = resourceSize.width / winSize.width; finalResourceSize = {width = winSize.width * realRadio; height = winSize.height * realRadio}; print("资源分辨率2" , finalResourceSize.width , finalResourceSize.height); glview:setDesignResolutionSize(finalResourceSize.width , finalResourceSize.height, 0); end g_radio_x = finalResourceSize.width / resourceSize.width g_radio_y = finalResourceSize.height / resourceSize.height else g_radio_x = 1 g_radio_y = 1 local glview = cc.Director:getInstance():getOpenGLView(); local winSize = glview:getFrameSize(); local resourceSize = {width = CONST_WIDTH ; height = CONST_HEIGHT}; -- 实际比率 local radio = winSize.width / winSize.height; -- 标准比率 local resourceRadio = resourceSize.width / resourceSize.height; print("实际分辨率" , winSize.width , winSize.height); local finalResourceSize; -- 比16:9还扁,就用标准高度,让宽度变宽 if radio > resourceRadio then finalResourceSize = {width = resourceSize.width ; height = resourceSize.height}; print("资源分辨率(固定宽度)" , finalResourceSize.width , finalResourceSize.height); glview:setDesignResolutionSize(finalResourceSize.width , finalResourceSize.height, 2); -- 比16:9高,就固定宽度,让高度变高 else finalResourceSize = {width = resourceSize.width ; height = resourceSize.width / radio}; print("资源分辨率" , finalResourceSize.width , finalResourceSize.height); glview:setDesignResolutionSize(finalResourceSize.width , finalResourceSize.height, 0); end if table.tostring then print("getWinSize" , table.tostring(cc.Director:getInstance():getWinSize())); print("getFrameSize" , table.tostring(glview:getFrameSize())); print("getDesignResolutionSize" , table.tostring(glview:getDesignResolutionSize())); print("finalResourceSize" , table.tostring(finalResourceSize)); end end end -- 侦听键盘返回键退出游戏 local function keyExitEvent() local function onOK(value) if tonumber(value) == 1 then cc.Director:getInstance():endToLua(); end end local function onKeyPressed(keyCode , event) end local function onKeyReleased(keyCode , event) -- Windows的ESC 或 Android的返回键 if keyCode == 6 then local params = { "是否要退出游戏?"; nil, 2; onOK; } if PluginDevice then PluginDevice:callMemberMethod("showDialog" , "(Ljava/lang/String;Ljava/lang/String;II)V" , params); end end end local listener = cc.EventListenerKeyboard:create(onKeyPressed , onKeyReleased); cc.Director:getInstance():getEventDispatcher():addEventListenerWithFixedPriority(listener, 1) end local function initLogFileManager() logFileManager = require("preload.AutoUpdate.LogFileManager") logFileManager:init(RomSetting) end local function main() initLogFileManager() -- 屏幕常亮 cc.Device:setKeepScreenOn(true); -- 打印设备信息 devicePrintBaseInfo() -- initialize director local windowSize = {width = CONST_WIDTH, height = CONST_HEIGHT}; if target_platform == 0 then --windowSize = {width = CONST_WIDTH , height = CONST_HEIGHT }; -- stander --windowSize = {width = 2436 * 0.5, height = 1125 * 0.5}; -- iPhonex --windowSize = {width = 2732 * 0.4, height = 2048 * 0.4}; -- iPad 12.9 windowSize = {width = 1280 * 0.65, height = 720 * 0.65}; -- iPhonex end local director = cc.Director:getInstance(); local glview = director:getOpenGLView(); if not glview then glview = cc.GLView:createWithRect("w3h", {x=0,y=0,width=windowSize.width,height=windowSize.height}); director:setOpenGLView(glview); end -- 匹配屏幕尺寸 adaptResolutionSize(); local function applicationScreenSizeChanged() print("applicationScreenSizeChanged()"); adaptResolutionSize(); local glview = director:getOpenGLView(); local scene = director:getRunningScene(); if not glview then print("applicationScreenSizeChanged() glview is nil"); end if not scene then print("applicationScreenSizeChanged() scene is nil"); end if glview and scene then local oldContentSize = scene:getContentSize(); local newContentSize = glview:getDesignResolutionSize(); -- 全部重新布局 local function visit(node) if node.requestDoLayout then --print("重新布局:" , node:getName()); node:requestDoLayout(); end if node.getContentSize then local size = node:getContentSize(); if size.width == oldContentSize.width and size.height == oldContentSize.height then --print("重新设置大小:" , node:getName()); node:setContentSize(newContentSize); end end if node.updateSizeAndPosition then node:updateSizeAndPosition(); end end scene:visitNode(visit); end end cc.Director:getInstance():getEventDispatcher():addEventListenerWithFixedPriority(cc.EventListenerCustom:create("applicationScreenSizeChanged" , applicationScreenSizeChanged) , 1); -- 侦听退出事件 keyExitEvent(); local isTestUpdate = false if isTestUpdate or target_platform ~= 0 then local mainScene = cc.Scene:create(); mainScene:setContentSize(glview:getDesignResolutionSize()); mainScene:setAnchorPoint({x=0,y=0}); director:runWithScene(mainScene); local function onUpdateFinished() require("luaScript.startup") end local updaterNew=require("preload.AutoUpdate.AutoUpdater"):new(RomSetting , mainScene, onUpdateFinished) updaterNew:startUpdate() else getServerConfigs(function() require("luaScript.startup") end, true) end if target_platform == 0 then pcall(require, "luaScript.Win32Ext") end end xpcall(main, __G__TRACKBACK__)