local ____lualib = require("lualib_bundle") local __TS__ClassExtends = ____lualib.__TS__ClassExtends local __TS__New = ____lualib.__TS__New local ____exports = __module.env local M__share_base_logger = __MODULE__.Import("share.base.logger", __module) local Logger = M__share_base_logger.Logger local M__share_base_scoped_object = __MODULE__.Import("share.base.scoped_object", __module) local ScopedObject = M__share_base_scoped_object.ScopedObject local M__share_event_event_hub = __MODULE__.Import("share.event.event_hub", __module) local EventHub = M__share_event_event_hub.EventHub local M__framework_core_scheduler = __MODULE__.Import("framework.core.scheduler", __module) local FrameScheduler = M__framework_core_scheduler.FrameScheduler local M__framework_module_mgr_registry = __MODULE__.Import("framework.module.mgr_registry", __module) local MgrRegistry = M__framework_module_mgr_registry.MgrRegistry local logger = Logger("GameContext") ____exports.WorldContext = __TS__DefineClass("WorldContext", __module) local WorldContext = ____exports.WorldContext WorldContext.name = "WorldContext" __TS__ClassExtends(WorldContext, ScopedObject) function WorldContext.prototype.____constructor(self, worldGen, world) ScopedObject.prototype.____constructor(self) self.worldGen = worldGen self.world = world end ____exports.LocalPlayerContext = __TS__DefineClass("LocalPlayerContext", __module) local LocalPlayerContext = ____exports.LocalPlayerContext LocalPlayerContext.name = "LocalPlayerContext" __TS__ClassExtends(LocalPlayerContext, ScopedObject) function LocalPlayerContext.prototype.____constructor(self, playerIndex, playerController) ScopedObject.prototype.____constructor(self) self.services = {} self.playerIndex = playerIndex self.playerController = playerController end function LocalPlayerContext.prototype.RegisterService(self, key, service) local old = self.services[key] if old ~= nil and old ~= service then old:Dispose() end self.services[key] = service self:AddScopedObject(service) return service end function LocalPlayerContext.prototype.GetService(self, key) return self.services[key] end --- -- @module_data local nextContextId = 0 ____exports.GameInstanceContext = __TS__DefineClass("GameInstanceContext", __module) local GameInstanceContext = ____exports.GameInstanceContext GameInstanceContext.name = "GameInstanceContext" __TS__ClassExtends(GameInstanceContext, ScopedObject) function GameInstanceContext.prototype.____constructor(self, gameInstance) ScopedObject.prototype.____constructor(self) self.shuttingDown = false self.localPlayers = {} self.services = {} self.shutdownHooks = {} nextContextId = nextContextId + 1 self.contextId = nextContextId self.gameInstance = gameInstance self.scheduler = __TS__New(FrameScheduler) self.mgrRegistry = __TS__New(MgrRegistry) self.appEvents = __TS__New( EventHub, "AppLifecycle#" .. tostring(self.contextId) ) self.worldContext = __TS__New( ____exports.WorldContext, self.scheduler:GetCurrentWorldGen() ) end function GameInstanceContext.prototype.IsShuttingDown(self) return self.shuttingDown or self:IsDisposed() end function GameInstanceContext.prototype.GetWorldContext(self) return self.worldContext end function GameInstanceContext.prototype.GetWorldGen(self) return self.worldContext.worldGen end function GameInstanceContext.prototype.GetOrCreateLocalPlayer(self, playerIndex, pc) local lp = self.localPlayers[playerIndex] if lp == nil or lp:IsDisposed() then lp = __TS__New(____exports.LocalPlayerContext, playerIndex, pc) self.localPlayers[playerIndex] = lp elseif pc ~= nil then lp.playerController = pc end return lp end function GameInstanceContext.prototype.RemoveLocalPlayer(self, playerIndex) local lp = self.localPlayers[playerIndex] if lp ~= nil then self.localPlayers[playerIndex] = nil lp:Dispose() end end function GameInstanceContext.prototype.RegisterService(self, key, service) local old = self.services[key] if old ~= nil and old ~= service then old:Dispose() end self.services[key] = service return service end function GameInstanceContext.prototype.GetService(self, key) return self.services[key] end function GameInstanceContext.prototype.AddShutdownHook(self, hook) if self.shuttingDown then return end self.shutdownHooks[#self.shutdownHooks + 1] = hook end function GameInstanceContext.prototype.Tick(self, unscaledDeltaSeconds, worldDeltaSeconds, isWorldPaused) if isWorldPaused == nil then isWorldPaused = false end if self:IsShuttingDown() then return end self.scheduler:Tick(unscaledDeltaSeconds, worldDeltaSeconds, isWorldPaused) end function GameInstanceContext.prototype.OnWorldCleanup(self, nextWorld) if self:IsShuttingDown() then return end local oldGen = self.worldContext.worldGen self.appEvents.emit.OnWorldCleanup(oldGen) self.worldContext:Dispose() local newGen = self.scheduler:InvalidateWorld() self.worldContext = __TS__New(____exports.WorldContext, newGen, nextWorld) end function GameInstanceContext.prototype.Dispose(self) if self:IsDisposed() then return end self.shuttingDown = true self.appEvents.emit.OnBeforeShutdown() local playerKeys = {} for idx in pairs(self.localPlayers) do playerKeys[#playerKeys + 1] = idx end do local i = 0 while i < #playerKeys do local idx = playerKeys[i + 1] local lp = self.localPlayers[idx] self.localPlayers[idx] = nil if lp ~= nil then lp:Dispose() end i = i + 1 end end self.worldContext:Dispose() self.scheduler:InvalidateWorld() do local i = #self.shutdownHooks - 1 while i >= 0 do self.shutdownHooks[i + 1]() i = i - 1 end end self.shutdownHooks = {} local serviceList = {} for k, svc in pairs(self.services) do if svc ~= nil then self.services[k] = nil serviceList[#serviceList + 1] = svc end end do local i = 0 while i < #serviceList do serviceList[i + 1]:Dispose() i = i + 1 end end self.mgrRegistry:Dispose() self.scheduler:Dispose() self.appEvents:Dispose() ScopedObject.prototype.Dispose(self) logger:Info("GameInstanceContext disposed", {contextId = self.contextId}) end --- -- @module_data local activeContextsByKey = {} --- -- @noSelf function ____exports.GetGameInstanceKey(gameInstance) if gameInstance.GetName ~= nil then return gameInstance:GetName() end return tostring(gameInstance) end --- -- @noSelf function ____exports.CreateGameInstanceContext(gameInstance) local key = ____exports.GetGameInstanceKey(gameInstance) local old = activeContextsByKey[key] if old ~= nil and not old:IsDisposed() then old:Dispose() end local ctx = __TS__New(____exports.GameInstanceContext, gameInstance) activeContextsByKey[key] = ctx return ctx end --- -- @noSelf function ____exports.GetGameInstanceContext(gameInstance) return activeContextsByKey[____exports.GetGameInstanceKey(gameInstance)] end --- -- @noSelf function ____exports.DestroyGameInstanceContext(gameInstance) local key = ____exports.GetGameInstanceKey(gameInstance) local ctx = activeContextsByKey[key] if ctx ~= nil then activeContextsByKey[key] = nil ctx:Dispose() end end --- -- @noSelf function ____exports.GetActiveGameInstanceContextCount() local count = 0 for _, ctx in pairs(activeContextsByKey) do if ctx ~= nil and not ctx:IsDisposed() then count = count + 1 end end return count end function __module.StashData(env, to) local ____has = to.__has__ if not ____has then ____has = {} to.__has__ = ____has end to.nextContextId = nextContextId ____has.nextContextId = true to.activeContextsByKey = activeContextsByKey ____has.activeContextsByKey = true end function __module.RestoreData(env, from) local ____has = from.__has__ if not ____has then return end if ____has.nextContextId then nextContextId = from.nextContextId end if ____has.activeContextsByKey then activeContextsByKey = from.activeContextsByKey end end function __module.UpdateImports() Logger = M__share_base_logger.Logger ScopedObject = M__share_base_scoped_object.ScopedObject EventHub = M__share_event_event_hub.EventHub FrameScheduler = M__framework_core_scheduler.FrameScheduler MgrRegistry = M__framework_module_mgr_registry.MgrRegistry end