local SubGameConfigManager = class("SubGameConfigManager") function SubGameConfigManager:ctor() self.subGameConfigs = {} self.subGameConfigPath = "dataconfig" self.subGameConfigFileName = "SubGameConfig_%03d.json" self.isInited = false end -- 初始化所有子游戏的配置信息 function SubGameConfigManager:init() logD("SubGameConfigManager:init()") self.subGameConfigs = {} -- 先使用的本地的数据初始化 local subGameIds = app.serverConfigs:getSubGameIds() for _, gameId in pairs(subGameIds) do self:_initSubGameConfig(gameId) end -- 然后获取服务器最新的数据 self:_requestLastestConfigs() end -- 获取单个子游戏的配置信息 -- 如果配置未找到,则返回nil function SubGameConfigManager:getSubGameConfig(gameId) logD("SubGameConfigManager:getSubGameConfig(), gameId = " .. tostring(gameId)) gameId = tonumber(gameId) if not gameId or gameId <= 0 or gameId >= 1000 then return nil end return self.subGameConfigs[gameId] end -- 初始化单个子游戏的配置信息 function SubGameConfigManager:_initSubGameConfig(gameId) logD("SubGameConfigManager:_initSubGameConfig(), gameId = " .. tostring(gameId)) gameId = tonumber(gameId) if not gameId or gameId <= 0 or gameId >= 1000 then return end local writablePath = cc.FileUtils:getInstance():getWritablePath(); local jsonFileName = string.format(self.subGameConfigFileName, gameId) local jsonFilePath = self.subGameConfigPath .. "/" .. jsonFileName if isFileExist(jsonFilePath) then -- 读取文本数据 local fileString = getFileData(jsonFilePath) if fileString == nil or fileString == "" then logD("SubGameConfigManager:_initSubGameConfig(), fileString = " .. tostring(fileString)) return end -- 非WIN32要解密 if not isWin32Platform() then fileString = cc.FilePackage:decrypt(fileString) end -- 转json对象 local jsonData = json.decode(fileString) if not jsonData or type(jsonData) ~= "table" then logD("SubGameConfigManager:_initSubGameConfig(), jsonData = " .. table.tostring(jsonData)) return end self.subGameConfigs[gameId] = jsonData else logD("SubGameConfigManager:_initSubGameConfig(), file not exist, gameId = " .. gameId) end end -- 获取所有子游戏的最新配置信息 function SubGameConfigManager:_requestLastestConfigs() if self.isInited then return end logD("SubGameConfigManager:_requestLastestConfigs()") local subGameIds = app.serverConfigs:getSubGameIds() local versionString = "" for _, gameId in pairs(subGameIds) do local subGameVersion = app.subGameManager:getSubGameVersionNum(gameId) if versionString == "" then versionString = tostring(gameId) .. "," .. tostring(subGameVersion) else versionString = versionString .. "|" .. tostring(gameId) .. "," .. tostring(subGameVersion) end end local tt = { action = "system.gamestruct", apkver = getAppVersionNum(), app = getAppId(), uid = loadUserId(), gameid = versionString, } logD("SubGameConfigManager:_requestLastestConfigs() tt = ", table.tostring(tt)) httpPost(getGlobalPhpUrl(), tt, handler(self, self.onGetLastestConfigsResponse)) end function SubGameConfigManager:onGetLastestConfigsResponse(status, response) logD(string.format("SubGameConfigManager:onGetLastestConfigsResponse(), status = %s response = %s", tostring(status), tostring(response))) if status ~= "successed" or not response then return end local stResponse = json.decode(response) if not stResponse or type(stResponse) ~= "table" or not stResponse.result or type(stResponse.result) ~= "table" then return end -- 标记为初始化完成 self.isInited = true for gameId,gameConfig in pairs(stResponse.result) do if gameConfig and type(gameConfig) == "table" and table.nums(gameConfig) > 0 then self:_saveSubGameConfig(gameId, gameConfig) end end -- 重新初始化,将最新的数据读入内存 self:init() end -- 保存单个子游戏的配置信息到缓存文件 function SubGameConfigManager:_saveSubGameConfig(gameId, jsonData) logD("SubGameConfigManager:_saveSubGameConfig(), gameId = " .. tostring(gameId)) logD("SubGameConfigManager:_saveSubGameConfig(), jsonData = " .. tostring(jsonData)) gameId = tonumber(gameId) if not gameId or gameId <= 0 or gameId >= 1000 then return end if jsonData == nil or jsonData == "" then return end local fileString = json.encode(jsonData) -- 非WIN32要加密保存 if not isWin32Platform() then fileString = cc.FilePackage:encrypt(fileString) end local writablePath = cc.FileUtils:getInstance():getWritablePath(); local subGameConfigPath = writablePath .. self.subGameConfigPath if not cc.FileUtils:getInstance():isDirectoryExist(subGameConfigPath) then cc.FileUtils:getInstance():createDirectory(subGameConfigPath) end local jsonFileName = string.format(self.subGameConfigFileName, gameId) local jsonFilePath = self.subGameConfigPath .. "/" .. jsonFileName saveStringToFile(fileString, jsonFilePath) end return SubGameConfigManager