do----------基础框架第一部分:纯Lua环境---------- --luajit changes xpcall from lua5.1 (which does not support arguments), but luajit can't run Unity5 on OSX if UNITY_EDITOR == 'OSX' or UNITY_STANDALONE == 'OSX' then local function xpcall_handler(errFunc, ret, msg, ...) if ret then return ret, msg, ... else return ret, errFunc(msg) end end local pcall = pcall xpcall = function(func, errFunc, ...) return xpcall_handler(errFunc, pcall(func, ...)) end --math.random(a,b)函数在a>b时在mac平台下会报错, --在windows里不会报错,测了下有奇怪的结果,当a-b<=1时就返回a,a-b>1时就返回(a,b)中的随机数,两边是开集 --这里简单处理成反过来,即math.random(b,a).只影响mac editor local mr = math.random math.random = function(...) local a, b = ... if a and b and b < a then return mr(b, a) end return mr(...) end end --设置gc参数,setpause 99表示一个gc循环跑完之后立即跑下一个, setstepmul 200表示以内存分配的2倍速跑gc循环 collectgarbage("setpause", 99) collectgarbage("setstepmul", 200) --设置随机数种子,使用反转来改善当种子数值变化不大时,产生的随机序列数值也相近的问题 math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,6))) --显式定义全局变量的辅助方法 function def_G(key, val) rawset(_G, key, val) end end do----------基础框架第二部分:日志和面向对象---------- --日志系统 def_G("logging", require('Main/Logging')) --日志等级 if UNITY_EDITOR then logging.setLevel(logging.LL_LOG) logging.setTraceLevel(logging.LL_LOG) else if TAI_DEVELOPMENT_BUILD then logging.setLevel(logging.LL_LOG) else logging.setLevel(logging.LL_WARN) end logging.setTraceLevel(logging.LL_ERROR) end --日志标记 def_G("LTAG", require("Main/LogTags")) --面向对象 def_G('class', require('Infrast/Class')) end rawset(_G, "GameAppInfo", require("Main/GameAppInfo")) GameAppInfo.InitGameInfo(true) logging.crit(LTAG.CRIT, "Global_PackageType", GameAppInfo.Global_PackageType) --数据持久化 def_G("PersistentData", require('Tools/PersistentData')) --事件系统 local EventSystem = require('Event/EventSystem') --更新函数 local LuaUpdater = require('Main/LuaUpdater') LuaUpdater.Startup() ----------初始化基础框架---------- --这个库依赖module函数和全局表直接赋值 local protobuf = require("protobuf/protobuf") --开启全局表写入限制(理想情况下,此步应该尽可能提前) if not (UNITY_EDITOR == 'OSX' or UNITY_STANDALONE == 'OSX') then --Mac平台的module函数实现似乎与其它平台不同,造成运行时报错,暂时关闭全局检查 setmetatable(_G, { __newindex = function(t, key, val) error(string.format("__newindex on _G for key %s", key), 2) end }) end --事件枚举 def_G("EventID", require("Common/EventID")) --辅助方法 require("Common/Utils") --大数据 def_G("BIManager", require("BI/BIManager")) --BattleCheck不支持 BIManager.Init() --数据,本地化Localization.Init要用 def_G("DataSheetManager", require("Main/DataSheetManager")) --本地化 --BattleCheck不支持 def_G("Localization", require("Localization/Localization")) --Proto生成代码中使用了module函数,需要向全局表插入键值对,需要在开启限制前加载 def_G("MessageEnums", require("Proto/message_enums_pb")) --网络 def_G("NetManager", require("Main/NetManager")) --本地化 def_G("Const_IDS", require("Localization/Const_IDS")) --界面 --BattleCheck不支持 def_G("UIManager", require("Main/UIManager")) --Assets 路径 def_G("ConstAssetPathConfig", require("Common/ConstAssetPathConfig")) --战斗模块 require("Common/define") ----------模块初始化---------- --设置日志等级 logging.setLevel(logging.LL_LOG) logging.setTraceLevel(logging.LL_ERROR) --重定向用不到的lua代码↓↓↓↓ local NeedRedirectLua = { "Battle/Expression/ExpEntity/System/ExpSystemManager", "Battle/Expression/ExpEntity/ExpEntity", "Battle/Expression/ExpLevel/ExpLevelManager", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpSyncLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpGoldHeroBaseLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpGoldHeroCooperateLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpSyncTeamPVPLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpSyncWorldBossLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpSyncGuildWarMapResourcesLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpSyncGuildWarBaseLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpSyncEscortLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpFightThroneBaseLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpDemonGodLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpSyncWarZoneWorldBossLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpGoldHeroTrialLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpSyncDragonArteryLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpSyncCountryEveningBossLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpAttackCityLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpSyncFourCountryChaosLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpSyncCityWarBaseLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpExpelMonsterLevelFunction", "Battle/Expression/ExpLevel/ExpLevelFunction/ExpSlgGvELevelFunction", "Battle/Expression/ExpMissile/ExpMissile", "Battle/Expression/ExpUpdateManager", "Battle/Logic/MainLoop/SyncMoveManager", "BI/BIManager", } local function DummyFunc() end for i,str in ipairs(NeedRedirectLua) do local table = require(str) for key,fuc in pairs(table) do if type(fuc) == "function" then table[key] = DummyFunc end end end rawset(_G, "UIUtils", setmetatable({}, { __index = function() return DummyFunc end })) rawset(_G, "AuxUtil", setmetatable({}, { __index = function() return DummyFunc end })) rawset(_G, "CameraController", { Instance = setmetatable({}, { __index = function() return DummyFunc end } ) } ) --重定向用不到的lua代码↑↑↑↑↑↑ local BattleSimulator = require("Battle/Logic/MainLoop/BattleSimulator") local BattleLogSetter = require("Battle/BattleLogSetter") local timeSinceUpdate = 0 local OpEnum = { InitData = 0, Tick = 1, GetResultBattleCheck = 2, GetHpList = 3, GetCompare = 4, GetPlayerCount = 5, InsertServerFrameData = 6, GetAllPlayerDamageBoss = 7, GetServerCheckFrameLogStr = 8, NewFighter = 9, GetGuildWarWallDamage = 10, GetGuildWarResource = 11, GetGuildWarMapInfo = 12, GetGuildWarPlayerInfos = 13, GetGuildWarBroadcastTips = 14, GetServerBattleActionRequestListByStart = 15, GetBattleResultMsg = 16, CheckNotSyncLevelBattle = 17, AutoRunCheckLevel = 18, CheckNotSyncLevelBattleByFile = 19, GetFrameCmdStatistics = 20, GetKillBossPlayerID = 21, OpGetPlayerToBossDmgRecord = 22, OpGetCountryEveningBossPlayerInfos = 23, OpGetCityWarPlayerInfos = 24, OpGetCityWarResource = 25, --已废弃,服务器不会用 GetTowerMonsterIdByIndex = 26, GetTowerEventList = 27, ClearTowerEventList = 28, GetCityWarWallDamage = 29, GetCityWarResourcesList = 30, GetCityWarStatueHpLossList = 31, GetCityWarVehicleCount = 32, GetCityWarDefendBossIsDead = 33, GetGuildWarMapAttackInfo = 34, CollectGC = 101, } --与校验程序之间的通信接口,接收操控参数并返回战斗数据 local function OperateBattleCheck(opcode, ...) if opcode == OpEnum.InitData then local initBytes = ... --0. 可以设置要用谁的视角播放战斗,TODO:以后需要循环播放所有player local replayPlayerID = 1 local replayDataBytes = initBytes or LuaExt.FileReadBytes("ReplayData/replaydata") BattleSimulator.SetPlayerID(replayPlayerID) --1.先加载服务器的回放数据 ,利用Replay函数 ,重放战斗 BattleSimulator.StartServerData(replayDataBytes) LevelManager._levelLogicData.IsServer = true -- 标记场景加载完毕 return true elseif opcode == OpEnum.Tick then if LogicUpdateManager.IsReplayOrServerCheck then LuaUpdater.CheckBattle_Update(0.05, timeSinceUpdate) timeSinceUpdate = timeSinceUpdate + 0.05 return LogicUpdateManager._clientHandleFrame else return -1 end elseif opcode == OpEnum.GetResultBattleCheck then-- 获取校验字符串 return BattleSimulator.CheckBattleResultStr or "" elseif opcode == OpEnum.GetServerCheckFrameLogStr then-- 获取战斗过程log return BattleLogSetter.GetServerCheckFrameLogStr() elseif opcode == OpEnum.GetHpList then local hpList = {} for i, entity in ipairs(LogicUpdateManager.EntityList_Sync) do local hp = entity:GetHp() table.insert(hpList, FP.tonumberV1(hp)) end return hpList elseif opcode == OpEnum.GetCompare then local comparedResult = BattleSimulator.GetComparedResult() return comparedResult elseif opcode == OpEnum.GetPlayerCount then -- 真实玩家的个数 local playerCount = BattleSimulator.GetRealPlayerCount() return playerCount elseif opcode == OpEnum.InsertServerFrameData then -- 插入 要执行的帧并且执行 BattleSimulator.InsertServerFrameData(...) return not BattleSimulator.LoseFrame elseif opcode == OpEnum.GetAllPlayerDamageBoss then -- 获取 军团boss | 大乱斗 中所有人对boss的伤害值 local damageDic = BattleSimulator.GetCurLevelFuncBossDamage() return damageDic elseif opcode == 8 then -- 服务器暂时不用 return "" elseif opcode == OpEnum.NewFighter then -- 有新玩家进入 模拟TCP的 newFighter return BattleSimulator.NewFighterEnter(...) elseif opcode == OpEnum.GetGuildWarWallDamage then -- 军团战:获取两边城墙的掉血量 local damageDic = BattleSimulator.GetGuildWarWallDamage() return damageDic elseif opcode == OpEnum.GetGuildWarResource then -- 军团战:获取资源量 local resourceList = BattleSimulator.GetGuildWarResourceAmount() return resourceList elseif opcode == OpEnum.GetGuildWarMapInfo then -- 军团战:获取地图信息 local mapInfo = BattleSimulator.GetGuildWarMapInfo() return mapInfo elseif opcode == OpEnum.GetGuildWarPlayerInfos then -- 军团战:获取玩家信息 local playerInfo = BattleSimulator.GetGuildWarPlayerInfos() return playerInfo elseif opcode == OpEnum.GetGuildWarBroadcastTips then -- 军团战:获取地图信息 local hasFirstBlood ,tipsBytes = BattleSimulator.GetGuildWarBroadcastTips(...) return {hasFirstBlood,tipsBytes} elseif opcode == OpEnum.GetServerBattleActionRequestListByStart then return BattleSimulator.GetServerBattleActionRequestListByStart(...) elseif opcode == OpEnum.GetBattleResultMsg then return BattleSimulator.BattleResultMsgBytes elseif opcode == OpEnum.CheckNotSyncLevelBattle then return BattleSimulator.CheckNotSyncLevelBattle(...) elseif opcode == OpEnum.CheckNotSyncLevelBattleByFile then return BattleSimulator.CheckNotSyncLevelBattleByFile(...) elseif opcode == OpEnum.AutoRunCheckLevel then local BattleSimulatorAutoFight = require("Battle/Logic/MainLoop/BattleSimulatorAutoFight") --无界面自动跑很多关 BattleSimulatorAutoFight.RunLevels() return true elseif opcode == OpEnum.GetFrameCmdStatistics then local datas = BattleSimulator.GetFrameCmdStatistics() -- battle_common_pb.Frame2CmdStatistic 的序列化 return datas elseif opcode == OpEnum.CollectGC then collectgarbage("collect") return true elseif opcode == OpEnum.GetKillBossPlayerID then -- 获取击杀boss的玩家ID local playerID = BattleSimulator.GetKillBossPlayerID() return playerID elseif opcode == OpEnum.OpGetPlayerToBossDmgRecord then return BattleSimulator.GetPlayerToBossDmgRecord() elseif opcode == OpEnum.OpGetCountryEveningBossPlayerInfos then return BattleSimulator.GetCountryEveningBossPlayerInfos() elseif opcode == OpEnum.OpGetCityWarPlayerInfos then return BattleSimulator.GetCityWarPlayerInfos() elseif opcode == OpEnum.OpGetCityWarResource then return BattleSimulator.GetCityWarResourceAmount() elseif opcode == OpEnum.GetTowerMonsterIdByIndex then return BattleSimulator.GetTowerMonsterIdByIndex(...) elseif opcode == OpEnum.GetTowerEventList then return BattleSimulator.GetTowerEventList() elseif opcode == OpEnum.ClearTowerEventList then BattleSimulator.ClearTowerEventList(...) elseif opcode == OpEnum.GetCityWarWallDamage then return BattleSimulator.GetCityWarWallDamage() elseif opcode == OpEnum.GetCityWarResourcesList then return BattleSimulator.GetCityWarResourcesList() elseif opcode == OpEnum.GetCityWarStatueHpLossList then return BattleSimulator.GetCityWarStatueHpLossList() elseif opcode == OpEnum.GetCityWarVehicleCount then return BattleSimulator.GetVehicleEntityCount() elseif opcode == OpEnum.GetCityWarDefendBossIsDead then return BattleSimulator.GetCityWarDefendBossIsDead() elseif opcode == OpEnum.GetGuildWarMapAttackInfo then return BattleSimulator.GetGuildWarMapAttackInfo() else return ... end end rawset(_G, "OperateBattleCheck", OperateBattleCheck) return OperateBattleCheck