LuaMain.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. -- print("luaMain")
  2. if jit then
  3. jit.off();jit.flush()
  4. end
  5. RO_XPCALL = function (func, msgh, ...)
  6. if jit then
  7. return xpcall(func, msgh, ...)
  8. else
  9. local args = {...}
  10. local localfunc = function() return func(unpack(args, 1, table.maxn(args))) end
  11. return xpcall(localfunc, msgh)
  12. end
  13. end
  14. local LuaMgr = LuaMgr.Instance
  15. LuaMgr:AddLuaSearchPath("Lua/Core")
  16. LuaMgr:AddLuaSearchPath("Lua/Pb")
  17. LuaMgr:AddLuaSearchPath("Lua/Enum")
  18. LuaMgr:AddLuaSearchPath("Lua/Config")
  19. LuaMgr:AddLuaSearchPath("Lua/UI")
  20. LuaMgr:AddLuaSearchPath("Lua/FSMState")
  21. LuaMgr:AddLuaSearchPath("Lua/Logic")
  22. LuaMgr:AddLuaSearchPath("Lua/Managers")
  23. LuaMgr:AddLuaSearchPath("Lua/Util")
  24. LuaMgr:AddLuaSearchPath("ToLua/Lua")
  25. LuaMgr:AddLuaSearchPath("ToLua/Lua/cjson")
  26. LuaMgr:AddLuaSearchPath("Lua/Entities")
  27. LuaMgr:AddLuaSearchPath("Lua/System")
  28. LuaMgr:AddLuaSearchPath("Lua/RedPoint")
  29. require("class")
  30. require("LuaExtension")
  31. Log = function(obj, ...)
  32. local info = debug.getinfo(2, "Sln")
  33. DebugHelper.Log(obj, info.source, ...)
  34. end
  35. LogError = function(str)
  36. local info = debug.getinfo(2, "Sln")
  37. DebugHelper.LogError(info.source.." "..str)
  38. end
  39. LogWarning = function(str)
  40. local info = debug.getinfo(2, "Sln")
  41. DebugHelper.LogWarning(info.source.." "..str)
  42. end
  43. LogHRWarning = function(str)
  44. local info = debug.getinfo(2, "Sln")
  45. DebugHelper.LogWarning("[hr] "..info.source.." "..info.currentline.." "..str)
  46. end
  47. LogHRWarningStack = function (str)
  48. local s = ""
  49. for i = 2, 8 do
  50. local info = debug.getinfo(i, "nlS")
  51. if info then
  52. s = s .. string.format("%s:%d: in %s\n", info.source, info.currentline, info.name or "")
  53. end
  54. end
  55. DebugHelper.LogWarning(string.format("[hr] %s\n%s", tostring(str), s))
  56. end
  57. local percent = 0
  58. InternalRequire = function(name, endStep)
  59. if percent then
  60. percent = percent + 1
  61. LuaMgr:StartMainPrecent(percent / 190)
  62. -- local temp =(percent / 190 * 80) + 10
  63. -- LogError("pr = "..temp.." Name = " ..name)
  64. end
  65. local data = require(name)
  66. if data and type(data) == 'function' then
  67. data = data()
  68. package.loaded[name] = nil
  69. end
  70. return data
  71. end
  72. local LuaMain = class("LuaMain")
  73. -- 这里是异步初始化, 初始化的耗时操作可放这里面
  74. function LuaMain:Init()
  75. Constant = InternalRequire("Constant")
  76. PlatformPack = InternalRequire("PlatformPack")
  77. Inspect = InternalRequire("inspect")
  78. Enum = InternalRequire("Enum")
  79. Enum.UIPageName = InternalRequire("UIPageName")
  80. UIEventNames = InternalRequire("UIEventNames")
  81. ProtoMsgId = InternalRequire("ProtoMsgId")
  82. ProtoConfirmMsgId = InternalRequire("ProtoConfirmMsgId")
  83. Bit = InternalRequire "bit"
  84. JSON = InternalRequire('json')
  85. InternalRequire("CommonUtil")
  86. ProtocalDataNormal = InternalRequire("ProtocalDataNormal")
  87. ManagerContainer = InternalRequire("ManagerContainer")
  88. ManagerContainer:Init()
  89. LogWarning(tostring(percent))
  90. percent = nil
  91. collectgarbage("collect")
  92. end
  93. local updateHandle = nil
  94. function LuaMain:Start()
  95. collectgarbage("setpause",100)
  96. collectgarbage("setstepmul",5000)
  97. LogHRWarning("lua init "..collectgarbage("count"))
  98. ManagerContainer = require("ManagerContainer")
  99. ManagerContainer:InitMgr()
  100. ManagerContainer.FSMMgr:RegisterStates()
  101. ManagerContainer.DataMgr:RegisterAllDataEvents()
  102. if not updateHandle then
  103. updateHandle = UpdateBeat:CreateListener(self.Update, self)
  104. end
  105. UpdateBeat:AddListener(updateHandle)
  106. LuaMgr:StartMainComplete()
  107. end
  108. function LuaMain:Update()
  109. if ManagerContainer ~= nil then
  110. ManagerContainer:Update()
  111. end
  112. if UnityEngine.Input.GetKeyUp(UnityEngine.KeyCode.Escape) then
  113. if SDKMgr.Instance:CheckHasModul(SDKModulType.EXIT_VIEW) then
  114. SDKMgr.Instance:OpenModul(SDKModulType.EXIT_VIEW)
  115. end
  116. end
  117. end
  118. function LuaMain:EnterLogin(relogin)
  119. if ManagerContainer.FSMMgr then
  120. if not ManagerContainer.FSMMgr.currentState
  121. or ManagerContainer.FSMMgr.currentState.stateId ~= Enum.StateEnum.LoginState.stateId
  122. then
  123. ManagerContainer.FSMMgr:ChangeState(Enum.StateEnum.LoginState.stateId, relogin)
  124. else
  125. ManagerContainer.FSMMgr:GetCurrentState():Enter(relogin)
  126. end
  127. end
  128. end
  129. function LuaMain:Destroy()
  130. if updateHandle ~= nil then
  131. UpdateBeat:RemoveListener(updateHandle)
  132. updateHandle = nil
  133. end
  134. if ManagerContainer then
  135. ManagerContainer:Destroy()
  136. end
  137. ManagerContainer = nil
  138. collectgarbage("collect")
  139. LogHRWarning("lua destroy "..collectgarbage("count"))
  140. end
  141. return LuaMain