Main.lua 14 KB

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