--[[
作者：小开心
请不要擅自修改或删除
请尊重版权
@

说明：
想患 懒癌强迫症
]]

_G = _G or GLOBAL
local TheSim = _G.TheSim

if TheSim.RAILGetPlatform and (TheSim:RAILGetPlatform() == "TGP" or TheSim:RAILGetPlatform() == "WG") then
    for _, mod in ipairs(_G.ModManager.mods) do 
        if mod and mod.modinfo and mod.modinfo.id == modname or mod.modinfo.name == modname then
            _G.KnownModIndex:Disable(modname)
        end
    end
    _G.Shutdown()
end

local msversion = "1.3"
local tonumber = _G.tonumber
if table.containskey(_G, "msversion") and (tonumber(_G.msversion) > tonumber(msversion)) then
    return _G:xkloadfn(env)
end
local loadstring = _G.loadstring
local pathname = char
local require = _G.require
if not table.containskey(_G, "xk") then
    _G.xk = { GetVersion = function( self ) return self.version or 0 end }
end
local xk_util = require(pathname.."/xk_util")
for k,v in pairs(xk_util) do 
    if _G.xk[k] ~= k or tonumber(_G.xk:GetVersion()) < tonumber(xk_util:GetVersion()) then
        _G.xk[k] = v
    end
end

function _G.MakePlayerCharacter(name, customprefabs, customassets, common_postinit, master_postinit, starting_inventory)
    if TheSim:GetGameID() == "DS" then 
        local function fn( ... )
            if common_postinit then
                common_postinit(...)
            end
            if master_postinit then
                master_postinit(...)
            end
        end
        return require("prefabs/player_common")(name, customprefabs, customassets, fn, starting_inventory)
    else
        return require("prefabs/player_common")(name, customprefabs, customassets, common_postinit, master_postinit, starting_inventory)
    end
end

function _G.MakeReload( inst, name )
    if TheSim:GetGameID() == "DS" then 
        local SaveIndex = _G.SaveIndex
        
        local function loadcb()
            inst:DoTaskInTime(0, function()
                inst.sg:GoToState("teleportato_teleport")
            end)
            inst:DoTaskInTime(5, function() 
                _G.StartNextInstance({reset_action = _G.RESET_ACTION.LOAD_SLOT, save_slot = _G.SaveGameIndex:GetCurrentSaveSlot()}, true)
            end)
        end

        SaveIndex._EraseCurrent = SaveIndex.EraseCurrent
        function SaveIndex:EraseCurrent(cb) 
            if inst.prefab == name then 
                self:Save(loadcb) 
            else
                self:_EraseCurrent(cb)
            end
        end

        SaveIndex._OnFailAdventure = SaveIndex.OnFailAdventure
        function SaveIndex:OnFailAdventure(cb) 
            if inst.prefab == name then 
                self:Save(loadcb) 
            else
                self:_OnFailAdventure(cb)
            end
        end
    end
end

function _G:xkloadfn(env)
    local TheSim = _G.TheSim

    if TheSim:GetGameID() == "DS" then 

        AddGamePostInit(modGamePostInit)
        function env.AddRecipeTab( rec_str, rec_sort, rec_atlas, rec_icon, rec_owner_tag, rec_crafting_station )
            _G.RECIPETABS[rec_str] = { str = rec_str, sort = 999 , icon_atlas = rec_atlas, icon = rec_icon, owner_tag = rec_owner_tag, crafting_station = rec_crafting_station }
            _G.STRINGS.TABS[rec_str] = rec_str
            return _G.RECIPETABS[rec_str]
        end

        function env.AddRecipe( recname, ingredients, tab, level, placer, min_spacing, nounlock, numtogive, builder_tag, atlas, image, testfn, game_type, aquatic, distance )
            AddSimPostInit(function(inst)  
                local rec = nil
                if _G.SaveGameIndex:IsModeShipwrecked() then
                    rec = _G.Recipe(recname, ingredients, tab, level, game_type, placer, min_spacing, nounlock, numtogive, aquatic, distance)
                    rec.atlas = atlas or resolvefilepath("images/inventoryimages.xml")
                    rec.image = image or recname .. ".tex"
                else
                    rec = _G.Recipe(recname, ingredients, tab, level, nil, min_spacing, nounlock, numtogive)
                    rec.atlas = atlas or resolvefilepath("images/inventoryimages.xml")
                    rec.image = image or recname .. ".tex"
                    rec.placer = placer
                end 
                return rec 
            end)
        end
    end
end

if TheSim:GetGameID() == "DS" then 
    _G.msversion = msversion
    require(pathname.."/ds_util") 
    require(pathname.."/dscomponentsposinit")(AddComponentPostInit) 
    _G.global("TheWorld")
    TheWorld = nil 
    _G.global("TheNet")
    --TheNet = nil
    _G.TheNet = {}

    function _G.TheNet:IsDedicated(  )
        return false
    end
    
    AddPrefabPostInit("world", function( inst ) 
        inst.ismastersim = true

        _G.TheWorld = inst
        if not inst.components.worldstate then
            inst.components["worldstate"] = require(pathname.."/worldstate")(inst)
            inst.state = inst.components.worldstate.data
        end
    end)
else
    function _G.MakeInventoryFloatable( ... )
        
    end

    AddClassPostConstruct("widgets/controls", function(self)

        self.custwidget = self:AddChild(require(pathname.."/widgets/custwidget")(self.owner))
        self.custwidget:SetHAnchor(1) 
        self.custwidget:SetVAnchor(2)
    end)
end
_G:xkloadfn(env)
