Main.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. if jit then
  2. jit.off()
  3. jit.flush()
  4. print("jit close")
  5. end
  6. real_os_time = real_os_time or os.time
  7. local Timer
  8. function os.time(tb)
  9. if tb then
  10. return real_os_time(tb)
  11. end
  12. Timer = Timer or require("core.Timer")
  13. local t = Timer.now
  14. return 0 < t and (t - t % 1000) / 1000 or real_os_time()
  15. end
  16. real_os_date = real_os_date or os.date
  17. local tb = {}
  18. for i = 1, 64 do
  19. tb[i] = {}
  20. end
  21. local tblen = 0
  22. function os.date(fmt, t)
  23. t = t or os.time()
  24. if fmt == "*t" then
  25. tblen = tblen + 1
  26. if #tb < tblen then
  27. tblen = 1
  28. end
  29. local d = tb[tblen]
  30. d.year, d.month, d.day, d.hour, d.min, d.sec, d.wday,d.yday = global.date(t)
  31. return d
  32. else
  33. return real_os_date(fmt, t)
  34. end
  35. end
  36. adj = adj or {}
  37. envs = envs or {}
  38. file_last_modify_time = file_last_modify_time or {}
  39. loaded = loaded or {}
  40. str2filename = str2filename or {}
  41. local sta = {}
  42. tb_loadfile = tb_loadfile or {}
  43. local function hot_dfs(a)
  44. if loaded[a] then
  45. print("hot=====", a)
  46. loaded[a] = nil
  47. for b in pairs(adj[a]) do
  48. hot_dfs(b)
  49. end
  50. end
  51. end
  52. local function hot()
  53. for k, v in pairs(str2filename) do
  54. local a = global.get_file_last_modify_time(v)
  55. if a ~= file_last_modify_time[k] then
  56. file_last_modify_time[k] = a
  57. tb_loadfile[k] = nil
  58. hot_dfs(k)
  59. end
  60. end
  61. end
  62. collectgarbage("step", 100000)
  63. hot()
  64. collectgarbage("step", 100000)
  65. local path = {
  66. "script/",
  67. "script/module/",
  68. }
  69. local require_cnt = 0
  70. _G.old_require = _G.old_require or require
  71. threadID = threadID or nil
  72. function _G.get_tid()
  73. if not threadID then
  74. threadID = _G.get_thread_id(_G.logic_id)
  75. end
  76. return threadID
  77. end
  78. ConfigLoadFile = ConfigLoadFile or nil
  79. function require(str, not_need_exist)
  80. if not ConfigLoadFile then
  81. ConfigLoadFile = {}
  82. ConfigLoadFile = require("Config")
  83. print("load excel project:", ConfigLoadFile.PROJECT_NAME)
  84. end
  85. require_cnt = require_cnt + 1
  86. adj[str] = adj[str] or {}
  87. if sta[#sta] then
  88. adj[str][sta[#sta]] = true
  89. end
  90. sta[#sta + 1] = str
  91. if not loaded[str] then
  92. loaded[str] = true
  93. envs[str] = envs[str] or {
  94. _G = _G,
  95. assert = assert,
  96. error = error,
  97. getfenv = getfenv,
  98. ipairs = ipairs,
  99. load = load,
  100. next = next,
  101. pairs = pairs,
  102. pcall = pcall,
  103. print = print,
  104. require = require,
  105. setmetatable = setmetatable,
  106. tonumber = tonumber,
  107. tostring = tostring,
  108. type = type,
  109. debug = debug,
  110. io = io,
  111. math = math,
  112. string = string,
  113. table = table,
  114. os = os,
  115. }
  116. envs[str]._ENV = envs[str]
  117. if not str2filename[str] then
  118. local filename = string.gsub(str, "%.", "/") .. ".lua"
  119. local _, excelPos = string.find(filename, "excel/", 1, true)
  120. if excelPos then
  121. local prename = string.sub(filename, 1, excelPos)
  122. local lastname = string.sub(filename, excelPos+1, #filename)
  123. filename = prename .. ConfigLoadFile.PROJECT_NAME .. "/" .. lastname
  124. end
  125. local ac
  126. for _, v in ipairs(path) do
  127. local pre_filename = v .. filename
  128. local a = global.get_file_last_modify_time(pre_filename)
  129. if a ~= 0 then
  130. str2filename[str] = pre_filename
  131. file_last_modify_time[str] = a
  132. ac = true
  133. break
  134. end
  135. end
  136. if not ac then
  137. if not_need_exist then
  138. return
  139. else
  140. assert(nil, str .. " not exist")
  141. end
  142. end
  143. end
  144. local fun = tb_loadfile[str]
  145. if not fun then
  146. local err
  147. fun, err = loadfile(str2filename[str], nil, envs[str])
  148. if not fun then
  149. assert(nil, err)
  150. end
  151. end
  152. local require_cnt_old = require_cnt
  153. fun()
  154. if require_cnt_old < require_cnt then
  155. tb_loadfile[str] = fun
  156. end
  157. end
  158. sta[#sta] = nil
  159. return envs[str]
  160. end
  161. modtb = modtb or nil
  162. local function cmd2str(cmd)
  163. if modtb then
  164. return
  165. end
  166. local f = io.popen(cmd)
  167. local str = f:read("*a")
  168. assert(io.close(f))
  169. return str
  170. end
  171. local function get_modules(str)
  172. if modtb then
  173. return modtb
  174. end
  175. modtb = {}
  176. local i = 1
  177. while true do
  178. local j = string.find(str, "\n", i)
  179. if not j then
  180. break
  181. end
  182. modtb[#modtb + 1] = string.sub(str, i, j - 1)
  183. i = j + 1
  184. end
  185. table.sort(modtb)
  186. return modtb
  187. end
  188. local cmd = os.getenv("OS") == "Windows_NT" and "dir/b script\\module" or "ls script/module"
  189. local m = get_modules(cmd2str(cmd))
  190. local Msg = require("core.Msg")
  191. local InnerMsg = require("core.InnerMsg")
  192. local Config = require("Config")
  193. if Config.IS_MIDDLE == true then
  194. _G['is_middle'] = true
  195. end
  196. function init_proto_id()
  197. if has_init_proto_id then
  198. return
  199. end
  200. local protoIDS = require("common.ProtoID")
  201. has_init_proto_id = true
  202. Msg.proto_id = {}
  203. for k,v in pairs(protoIDS) do
  204. if type(k) == "number" then
  205. Msg.proto_id[v] = k
  206. end
  207. end
  208. protoIDS = require("common.InnerProtoID")
  209. InnerMsg.proto_id = {}
  210. for k,v in pairs(protoIDS) do
  211. if type(k) == "number" then
  212. InnerMsg.proto_id[v] = k
  213. end
  214. end
  215. end
  216. init_proto_id()
  217. require("core.PKLimit")
  218. local function requireModule(module_name)
  219. local proto = require(module_name .. ".Proto", true)
  220. if not proto then
  221. return
  222. end
  223. local handler = require(module_name .. ".Handler", true)
  224. for k, v in pairs(proto) do
  225. local sub = string.sub(k, 1, 2)
  226. if sub == "CG" or sub == "GC" then
  227. if sub == "CG" and not handler[k] then
  228. assert(nil, k .. " not exist in HandlerL.lua")
  229. end
  230. local pid = Msg.proto_id[k]
  231. Msg.register(k, pid, v, handler and handler[k])
  232. elseif sub == "WL" or sub == "LW" then
  233. assert(nil, k.." can't define here")
  234. end
  235. end
  236. end
  237. for _, v in pairs(m) do
  238. requireModule(v)
  239. end
  240. local function initInnerProto()
  241. local proto = require("common.InnerProto")
  242. if not proto then
  243. return
  244. end
  245. local handler = require("common.InnerHandler", true)
  246. for k, v in pairs(proto) do
  247. local sub = string.sub(k, 1, 2)
  248. local pid = InnerMsg.proto_id[k]
  249. if sub == "LW" or sub == "WL" then
  250. if sub == "LW" and not handler[k] then
  251. assert(nil, k .. " not exist in handler.lua")
  252. end
  253. InnerMsg.register(k, pid, v, handler and handler[k])
  254. elseif sub == "CG" or sub == "GC" then
  255. assert(nil, k.." can't define here")
  256. end
  257. end
  258. end
  259. initInnerProto()
  260. local Log = require("common.Log")
  261. local Timer = require("core.Timer")
  262. local Monitor = require("core.Monitor")
  263. local AdminLogic = require("AdminLogic")
  264. local ObjHuman = require("core.ObjHuman")
  265. local Obj = require("core.Obj")
  266. local msg_ex = _G.msg_ex
  267. function handlerMsg(fd, protoId)
  268. local container = Msg.cg[protoId]
  269. if msg_ex.read(protoId, container) then
  270. print("err handlerMsg msg_ex.read", protoId, Msg.proto_name[protoId])
  271. local mm = Msg.gc.GC_DISCONNECT
  272. mm.code = 0
  273. mm.msg = "proto err"
  274. Msg.send(mm, fd)
  275. return
  276. end
  277. local id = ObjHuman.fds[fd]
  278. if id then
  279. local human = Obj.objs[id]
  280. if not human then
  281. assert()
  282. end
  283. if protoId == Msg.proto_id.CG_ASK_LOGIN then
  284. assert()
  285. end
  286. Monitor.handlePacket(human, container, protoId)
  287. else
  288. if protoId ~= Msg.proto_id.CG_AA_DISCONNECT and
  289. protoId ~= Msg.proto_id.CG_ASK_LOGIN and
  290. protoId ~= Msg.proto_id.CG_MIDDLE_LOGIN and
  291. protoId ~= Msg.proto_id.CG_TEST_PROTO then
  292. --如果登录了两个相同的帐号,第二个帐号把第一个帐号踢掉后,第一个帐号在收到断开的消息之前,可能继续发包,就会跑到这里来
  293. return
  294. end
  295. Monitor.handlePacket(fd, container, protoId)
  296. end
  297. end
  298. Timer.now = global.get_msec() --当前时间
  299. function handlerTime(msec)
  300. Timer.handlerTime(msec)
  301. end
  302. function handlerAdmin(fd, input)
  303. return AdminLogic.handleAdminRequest(fd, input)
  304. end
  305. function handlerInner(fd) -- 处理跨服和普通服之间的消息
  306. local ret, proto_id = _G.msg_inner_parse.readint()
  307. assert(ret)
  308. local container = nil
  309. if Config.IS_MIDDLE then -- logic传来
  310. container = InnerMsg.lw[proto_id]
  311. else -- world传来
  312. container = InnerMsg.wl[proto_id]
  313. end
  314. InnerMsg.readMsg(proto_id, container)
  315. Monitor.handleInnerPacket(fd, container, proto_id)
  316. end
  317. -- 启动之后需要做的事情,没有直接写在某个模块的init里面的原因是因为这些init需要启动初始化完完整数据之后才能进行
  318. local function initAfterStart()
  319. if has_init then return end
  320. has_init = true
  321. math.randomseed(os.time())
  322. require("core.RoleDel").roleDel()
  323. require("common.Check").check()
  324. require("common.CommonDB").init()
  325. require("role.RoleDBLogic").roleDBInit()
  326. require("drill.DrillDB").initAfterStart()
  327. require("jjc.JjcDB").initAfterStart()
  328. require("billboard.BillboardDB").initAfterStart()
  329. require("middle.MiddleActGroup").initAfterStart()
  330. require("theStars.TheStarsDBLogic").init()
  331. require("copy.CopyManage").init()
  332. require("combat.CombatObj").initAfterStart()
  333. require("bag.DropSpecial").initAfterStart()
  334. require("warReport.WarReportLogic").initAfterStart()
  335. require("battle.BattleLogic").initAfterStart()
  336. require("jjcGodWar.JjcGodWarDB").initAfterStart()
  337. require("union.UnionWarDBLogic").init()
  338. require("middle.MiddleConnect").initAfterStart()
  339. require("role.RoleStrongLogic").initAfterStart()
  340. require("huanjingTower.HuanjingTowerLogic").initSeverTower()
  341. require("yjTreasure.YjTreasureDB").changeOldToNewDB()
  342. require("yjTreasure.YjTreasureDB").initAfterStart()
  343. require("union.UnionWarDBLogic").init()
  344. require("zhanbu.ZhanbuLogic").initAfterStart()
  345. require("absAct.HeroGrowUp").initAfterStart()
  346. require("present.OpenServerRankDB").initAfterStart()
  347. require("absAct.AbsActBillboardMiddleLogic").initAfterStart()
  348. require("mozhu.MoZhuDB").initAfterStart()
  349. require("common.CommonRankDB").initAfterStart()
  350. math.randomseed(os.time())
  351. end
  352. local function initAfterHot()
  353. require("platform.ProjectLogic").initAfterHot()
  354. require("hero.HeroDefine").initAfterHot()
  355. require("common.CommonDB").init()
  356. require("roleSystem.RoleSystemLogic").initAfterHot()
  357. require("drawCard.DrawCardLogic").initAfterHot()
  358. require("absAct.AbsActLogic").initAfterHot()
  359. require("yunying.YunYingLogic").init()
  360. require("fuwen.FuwenGrid").initAfterHot()
  361. require("skin.SkinLogic").initAfterHot()
  362. require("jjc.JjcActLogic").init()
  363. require("combat.CombatLogic").init()
  364. require("combat.Skill").initAfterHot()
  365. require("present.WelfareGiftLogic").initAfterHot()
  366. require("combat.JibanLogic").initAfterHot()
  367. require("guide.GuideLogic").initAfterHot()
  368. require("chengjiu.ChengjiuLogic").initAfterHot()
  369. require("huanjingTower.HuanjingTowerLogic").initSeverTower()
  370. require("core.RoleDel").initAfterHot()
  371. require("platform.FanliLogic").initAfterHot()
  372. require("moshou.MoshouLogic").initAfterHot()
  373. require("chengjiu.ChengjiuLogic").initAfterHotTemp()
  374. require("present.OpenAct").initAfterHot()
  375. require("absAct.PremiumGiftLogic").initAfterHot()
  376. require("absAct.HeroGrowUp").initAfterHot()
  377. require("absAct.WishGiftLogic").initAfterHot()
  378. require("present.CDK").initAfterHot()
  379. require("topup.BuyLogic").initAfterHot()
  380. Log.write(Log.LOGID_TEST, "hot success")
  381. end
  382. initAfterStart()
  383. initAfterHot()
  384. collectgarbage("collect")
  385. print("logic:"..collectgarbage("count"))--, jit.status())