if jit then
    jit.off()
    jit.flush()
    print("jit close")
end

real_os_time = real_os_time or os.time 
local Timer   
function os.time(tb) 
    if tb then      
        return real_os_time(tb)    
    end    
    Timer = Timer or require("core.Timer")    
    local t = Timer.now          
    return 0 < t and (t - t % 1000) / 1000 or real_os_time()         
end

real_os_date = real_os_date or os.date

local tb = {}     
for i = 1, 64 do                
    tb[i] = {}                  
end


local tblen = 0                                 
function os.date(fmt, t)                          
    t = t or os.time()                    
    if fmt == "*t" then                  
        tblen = tblen + 1   
        if #tb < tblen then                  
            tblen = 1                      
        end            
        local d = tb[tblen]       
        d.year, d.month, d.day, d.hour, d.min, d.sec, d.wday,d.yday = global.date(t)        
        return d             
    else
        return real_os_date(fmt, t) 
    end 
end

adj = adj or {}
envs = envs or {}
file_last_modify_time = file_last_modify_time or {}
loaded = loaded or {}
str2filename = str2filename or {}
local sta = {}
tb_loadfile = tb_loadfile or {} 
local function hot_dfs(a)
    if loaded[a] then
        print("hot=====", a)  
        loaded[a] = nil
        for b in pairs(adj[a]) do 
            hot_dfs(b)  
        end   
    end
end 

local function hot()
    for k, v in pairs(str2filename) do
        local a = global.get_file_last_modify_time(v)   
        if a ~= file_last_modify_time[k] then
            file_last_modify_time[k] = a 
            tb_loadfile[k] = nil    
            hot_dfs(k)
        end
    end 
end

collectgarbage("step", 100000)
hot()
collectgarbage("step", 100000)

local path = {
    "script/",
    "script/module/", 
}  

local require_cnt = 0 
_G.old_require =  _G.old_require or require

threadID = threadID or nil
function _G.get_tid() 
    if not threadID then  
        threadID = _G.get_thread_id(_G.logic_id) 
    end  
    return threadID
end

ConfigLoadFile = ConfigLoadFile or nil

function require(str, not_need_exist) 
    
    if not ConfigLoadFile then
        ConfigLoadFile = {}
        ConfigLoadFile = require("Config") 
        print("load excel project:", ConfigLoadFile.PROJECT_NAME)
    end
    require_cnt = require_cnt + 1 
    adj[str] = adj[str] or {} 
    if sta[#sta] then   
        adj[str][sta[#sta]] = true 
    end 
    sta[#sta + 1] = str    
    if not loaded[str] then  
        loaded[str] = true  
        envs[str] = envs[str] or {  
            _G = _G,  
            assert = assert, 
            error = error, 
            getfenv = getfenv,  
            ipairs = ipairs,  
            load = load,   
            next = next,   
            pairs = pairs,
            pcall = pcall,   
            print = print,  
            require = require,    
            setmetatable = setmetatable,
            tonumber = tonumber,  
            tostring = tostring, 
            type = type,  

            debug = debug,  
            io = io,  
            math = math, 
            string = string,   
            table = table,  
            os = os, 
        } 
        envs[str]._ENV = envs[str]   
        if not str2filename[str] then    
            local filename = string.gsub(str, "%.", "/") .. ".lua"  
            local _, excelPos = string.find(filename, "excel/", 1, true)
            if excelPos then
                local prename = string.sub(filename, 1, excelPos)
                local lastname = string.sub(filename, excelPos+1, #filename) 
                filename = prename .. ConfigLoadFile.PROJECT_NAME .. "/" .. lastname
            end              
            local ac  
            for _, v in ipairs(path) do  
                local pre_filename = v .. filename    
                local a = global.get_file_last_modify_time(pre_filename)   
                if a ~= 0 then 
                    str2filename[str] = pre_filename   
                    file_last_modify_time[str] = a     
                    ac = true  
                    break  
                end
            end             
            if not ac then  
                if not_need_exist then
                    return
                else
                    assert(nil, str .. " not exist") 
                end   
            end 
        end 
        local fun = tb_loadfile[str] 
        if not fun then   
            local err  
            fun, err = loadfile(str2filename[str], nil, envs[str])  

            if not fun then 
                assert(nil, err) 
            end  
        end    
        local require_cnt_old = require_cnt   
        fun()    
        if require_cnt_old < require_cnt then   
            tb_loadfile[str] = fun    
        end   
    end  
    sta[#sta] = nil   
    return envs[str]  
end

modtb = modtb or nil

local function cmd2str(cmd)
    if modtb then  
    return
    end  
    local f = io.popen(cmd) 
    local str = f:read("*a")   
    assert(io.close(f)) 
    return str
end

local function get_modules(str)
    if modtb then
        return modtb   
    end  
    modtb = {}
    local i = 1  
    while true do 
        local j = string.find(str, "\n", i)
        if not j then  
            break
        end 
        modtb[#modtb + 1] = string.sub(str, i, j - 1)
        i = j + 1
    end 
    table.sort(modtb)
    return modtb
end

local cmd = os.getenv("OS") == "Windows_NT" and "dir/b script\\module" or "ls script/module"
local m = get_modules(cmd2str(cmd))
local Msg = require("core.Msg") 
local InnerMsg = require("core.InnerMsg")
local Config = require("Config")  

if Config.IS_MIDDLE == true then
    _G['is_middle'] = true
end


local Log = require("common.Log") 
local Timer = require("core.Timer")
local Monitor = require("core.Monitor") 
local AdminLogic = require("AdminLogic") 
local ObjHuman = require("core.ObjHuman")   
local Obj = require("core.Obj")
local msg_ex = _G.msg_ex 


Timer.now = global.get_msec() --当前时间 
function handlerTime(msec)    
    -- Timer.handlerTime(msec) 
end


--合服开始
local MergeServerLogic = require("merge.MergeServerLogic")
MergeServerLogic.Start()





collectgarbage("collect")
print("logic:"..collectgarbage("count"))--, jit.status()) 