349 lines
12 KiB
Lua
349 lines
12 KiB
Lua
local ____lualib = require("lualib_bundle")
|
|
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
|
|
local __TS__New = ____lualib.__TS__New
|
|
local __TS__InstanceOf = ____lualib.__TS__InstanceOf
|
|
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_base_traceback = __MODULE__.Import("share.base.traceback", __module)
|
|
local tbpcall = M__share_base_traceback.tbpcall
|
|
local M__framework_context_game_context = __MODULE__.Import("framework.context.game_context", __module)
|
|
local GetGameInstanceKey = M__framework_context_game_context.GetGameInstanceKey
|
|
local M__framework_resource_asset_catalog = __MODULE__.Import("framework.resource.asset_catalog", __module)
|
|
local RegisterBaseAssetCatalog = M__framework_resource_asset_catalog.RegisterBaseAssetCatalog
|
|
local logger = Logger("ResourceService")
|
|
____exports.UEResourceBackend = __TS__DefineClass("UEResourceBackend", __module)
|
|
local UEResourceBackend = ____exports.UEResourceBackend
|
|
UEResourceBackend.name = "UEResourceBackend"
|
|
function UEResourceBackend.prototype.____constructor(self, subsystem)
|
|
self.subsystem = subsystem
|
|
end
|
|
function UEResourceBackend.prototype.ResolveAssetPath(self, keyOrPath)
|
|
return self.subsystem:ResolveAssetPath(keyOrPath)
|
|
end
|
|
function UEResourceBackend.prototype.DoesAssetExist(self, keyOrPath)
|
|
return self.subsystem:DoesAssetExist(keyOrPath)
|
|
end
|
|
function UEResourceBackend.prototype.IsPackagedMode(self)
|
|
return UE.ULuaResourceSubsystem.IsPackagedResourceMode()
|
|
end
|
|
function UEResourceBackend.prototype.RequestLoadObjectAsync(self, keyOrPath, worldGen)
|
|
return self.subsystem:RequestLoadObjectAsync(keyOrPath, worldGen)
|
|
end
|
|
function UEResourceBackend.prototype.RequestLoadClassAsync(self, keyOrPath, worldGen)
|
|
return self.subsystem:RequestLoadClassAsync(keyOrPath, worldGen)
|
|
end
|
|
function UEResourceBackend.prototype.CancelRequest(self, requestId)
|
|
return self.subsystem:CancelRequest(requestId)
|
|
end
|
|
function UEResourceBackend.prototype.CancelRequestsForWorld(self, worldGen)
|
|
return self.subsystem:CancelRequestsForWorld(worldGen)
|
|
end
|
|
function UEResourceBackend.prototype.ReleaseAsset(self, resolvedSoftPath)
|
|
self.subsystem:ReleaseAsset(resolvedSoftPath)
|
|
end
|
|
function UEResourceBackend.prototype.GetResourceStats(self)
|
|
return self.subsystem:GetResourceStats()
|
|
end
|
|
____exports.ResourceLease = __TS__DefineClass("ResourceLease", __module)
|
|
local ResourceLease = ____exports.ResourceLease
|
|
ResourceLease.name = "ResourceLease"
|
|
function ResourceLease.prototype.____constructor(self, resolvedPath, asset, backend)
|
|
self.disposed = false
|
|
self.resolvedPath = resolvedPath
|
|
self.asset = asset
|
|
self.backend = backend
|
|
end
|
|
function ResourceLease.prototype.GetAsset(self)
|
|
if self.disposed then
|
|
return nil
|
|
end
|
|
return self.asset
|
|
end
|
|
function ResourceLease.prototype.IsDisposed(self)
|
|
return self.disposed
|
|
end
|
|
function ResourceLease.prototype.Dispose(self)
|
|
if self.disposed then
|
|
return
|
|
end
|
|
self.disposed = true
|
|
self.asset = nil
|
|
if self.backend ~= nil then
|
|
local b = self.backend
|
|
self.backend = nil
|
|
b:ReleaseAsset(self.resolvedPath)
|
|
end
|
|
end
|
|
____exports.ResourceLoadHandle = __TS__DefineClass("ResourceLoadHandle", __module)
|
|
local ResourceLoadHandle = ____exports.ResourceLoadHandle
|
|
ResourceLoadHandle.name = "ResourceLoadHandle"
|
|
function ResourceLoadHandle.prototype.____constructor(self, service, requestId)
|
|
self.disposed = false
|
|
self.service = service
|
|
self.requestId = requestId
|
|
end
|
|
function ResourceLoadHandle.prototype.AttachLeaseInternal(self, lease)
|
|
if self.disposed then
|
|
lease:Dispose()
|
|
return
|
|
end
|
|
self.lease = lease
|
|
self.service = nil
|
|
end
|
|
function ResourceLoadHandle.prototype.MarkSettledWithoutLeaseInternal(self)
|
|
self.service = nil
|
|
end
|
|
function ResourceLoadHandle.prototype.GetLease(self)
|
|
return self.lease
|
|
end
|
|
function ResourceLoadHandle.prototype.IsDisposed(self)
|
|
return self.disposed
|
|
end
|
|
function ResourceLoadHandle.prototype.Cancel(self)
|
|
self:Dispose()
|
|
end
|
|
function ResourceLoadHandle.prototype.Dispose(self)
|
|
if self.disposed then
|
|
return
|
|
end
|
|
self.disposed = true
|
|
if self.service ~= nil then
|
|
local svc = self.service
|
|
self.service = nil
|
|
svc:CancelPendingByIdInternal(self.requestId)
|
|
end
|
|
if self.lease ~= nil then
|
|
local l = self.lease
|
|
self.lease = nil
|
|
l:Dispose()
|
|
end
|
|
end
|
|
---
|
|
-- @module_data
|
|
local serviceByGameInstanceKey = {}
|
|
____exports.ResourceService = __TS__DefineClass("ResourceService", __module)
|
|
local ResourceService = ____exports.ResourceService
|
|
ResourceService.name = "ResourceService"
|
|
__TS__ClassExtends(ResourceService, ScopedObject)
|
|
function ResourceService.prototype.____constructor(self, ctx, backend)
|
|
ScopedObject.prototype.____constructor(self)
|
|
self.pendingById = {}
|
|
self.ctx = ctx
|
|
self.backend = backend
|
|
if ctx.gameInstance ~= nil then
|
|
self.giKey = GetGameInstanceKey(ctx.gameInstance)
|
|
serviceByGameInstanceKey[self.giKey] = self
|
|
end
|
|
ctx.appEvents.on.OnWorldCleanup(
|
|
function(oldWorldGen)
|
|
self:CancelForWorld(oldWorldGen)
|
|
end,
|
|
self
|
|
)
|
|
end
|
|
function ResourceService.prototype.ResolveAssetPath(self, keyOrPath)
|
|
return self.backend:ResolveAssetPath(keyOrPath)
|
|
end
|
|
function ResourceService.prototype.DoesAssetExist(self, keyOrPath)
|
|
return self.backend:DoesAssetExist(keyOrPath)
|
|
end
|
|
function ResourceService.prototype.IsPackagedMode(self)
|
|
return self.backend:IsPackagedMode()
|
|
end
|
|
function ResourceService.prototype.LoadObjectAsync(self, keyOrPath, owner, callback)
|
|
return self:StartAsyncLoad(keyOrPath, owner, callback, false)
|
|
end
|
|
function ResourceService.prototype.LoadTextureAsync(self, keyOrPath, owner, callback)
|
|
return self:LoadObjectAsync(keyOrPath, owner, callback)
|
|
end
|
|
function ResourceService.prototype.LoadClassAsync(self, keyOrPath, owner, callback)
|
|
return self:StartAsyncLoad(keyOrPath, owner, callback, true)
|
|
end
|
|
function ResourceService.prototype.StartAsyncLoad(self, keyOrPath, owner, callback, isClassLoad)
|
|
if self:IsDisposed() or self.ctx:IsShuttingDown() or owner ~= nil and owner:IsDisposed() then
|
|
local dead = __TS__New(____exports.ResourceLoadHandle, self, 0)
|
|
dead:Dispose()
|
|
return dead
|
|
end
|
|
local worldGen = self.ctx:GetWorldGen()
|
|
local requestId = isClassLoad and self.backend:RequestLoadClassAsync(keyOrPath, worldGen) or self.backend:RequestLoadObjectAsync(keyOrPath, worldGen)
|
|
local handle = __TS__New(____exports.ResourceLoadHandle, self, requestId)
|
|
local record = {
|
|
requestId = requestId,
|
|
keyOrPath = keyOrPath,
|
|
worldGen = worldGen,
|
|
owner = owner,
|
|
callback = callback,
|
|
handle = handle,
|
|
cancelled = false
|
|
}
|
|
self.pendingById[requestId] = record
|
|
if owner ~= nil and __TS__InstanceOf(owner, ScopedObject) then
|
|
owner:AddScopedObject(handle)
|
|
end
|
|
return handle
|
|
end
|
|
function ResourceService.prototype.CancelPendingByIdInternal(self, requestId)
|
|
local record = self.pendingById[requestId]
|
|
if record == nil then
|
|
return
|
|
end
|
|
record.cancelled = true
|
|
self.pendingById[requestId] = nil
|
|
self.backend:CancelRequest(requestId)
|
|
end
|
|
function ResourceService.prototype.CancelForWorld(self, worldGen)
|
|
local toCancel = {}
|
|
for id, rec in pairs(self.pendingById) do
|
|
if rec ~= nil and rec.worldGen == worldGen then
|
|
toCancel[#toCancel + 1] = id
|
|
end
|
|
end
|
|
do
|
|
local i = 0
|
|
while i < #toCancel do
|
|
local rec = self.pendingById[toCancel[i + 1]]
|
|
if rec ~= nil then
|
|
rec.cancelled = true
|
|
self.pendingById[toCancel[i + 1]] = nil
|
|
rec.handle:MarkSettledWithoutLeaseInternal()
|
|
end
|
|
i = i + 1
|
|
end
|
|
end
|
|
self.backend:CancelRequestsForWorld(worldGen)
|
|
return #toCancel
|
|
end
|
|
function ResourceService.prototype.HandleNativeLoadComplete(self, requestId, ok, loadedObj, resolvedPath, errorMessage)
|
|
local record = self.pendingById[requestId]
|
|
if record == nil then
|
|
if ok and resolvedPath ~= "" then
|
|
self.backend:ReleaseAsset(resolvedPath)
|
|
end
|
|
return
|
|
end
|
|
self.pendingById[requestId] = nil
|
|
local isStale = record.cancelled or self:IsDisposed() or self.ctx:IsShuttingDown() or record.worldGen ~= self.ctx:GetWorldGen() or record.owner ~= nil and record.owner:IsDisposed() or record.handle:IsDisposed()
|
|
if isStale then
|
|
record.handle:MarkSettledWithoutLeaseInternal()
|
|
if ok and resolvedPath ~= "" then
|
|
self.backend:ReleaseAsset(resolvedPath)
|
|
end
|
|
return
|
|
end
|
|
if ok and loadedObj ~= nil then
|
|
local lease = __TS__New(____exports.ResourceLease, resolvedPath, loadedObj, self.backend)
|
|
record.handle:AttachLeaseInternal(lease)
|
|
tbpcall(
|
|
record.callback,
|
|
true,
|
|
loadedObj,
|
|
lease,
|
|
nil
|
|
)
|
|
else
|
|
record.handle:MarkSettledWithoutLeaseInternal()
|
|
local err = errorMessage ~= "" and errorMessage or "Failed to load resource: " .. record.keyOrPath
|
|
logger:Warn("Resource load failed", {key = record.keyOrPath, path = resolvedPath, err = err})
|
|
tbpcall(
|
|
record.callback,
|
|
false,
|
|
nil,
|
|
nil,
|
|
err
|
|
)
|
|
end
|
|
end
|
|
function ResourceService.prototype.GetDiagnostics(self)
|
|
local pending = 0
|
|
for _, rec in pairs(self.pendingById) do
|
|
if rec ~= nil and not rec.cancelled then
|
|
pending = pending + 1
|
|
end
|
|
end
|
|
local nativeInFlight, nativeRetained, nativeRefs = self.backend:GetResourceStats()
|
|
return {tsPendingRequests = pending, nativeInFlight = nativeInFlight, nativeRetainedAssets = nativeRetained, nativeTotalRefCount = nativeRefs}
|
|
end
|
|
function ResourceService.prototype.Dispose(self)
|
|
if self:IsDisposed() then
|
|
return
|
|
end
|
|
local ids = {}
|
|
for id in pairs(self.pendingById) do
|
|
ids[#ids + 1] = id
|
|
end
|
|
do
|
|
local i = 0
|
|
while i < #ids do
|
|
self:CancelPendingByIdInternal(ids[i + 1])
|
|
i = i + 1
|
|
end
|
|
end
|
|
if self.giKey ~= nil and serviceByGameInstanceKey[self.giKey] == self then
|
|
serviceByGameInstanceKey[self.giKey] = nil
|
|
end
|
|
ScopedObject.prototype.Dispose(self)
|
|
end
|
|
--- 供 C++ FLuaCallbackBridge 回调的纯函数入口(路由到对应 GameInstance 的 ResourceService)。
|
|
--
|
|
-- @noSelf
|
|
function ____exports.OnNativeLoadComplete(gameInstance, requestId, ok, loadedObj, resolvedPath, errorMessage)
|
|
local svc = serviceByGameInstanceKey[GetGameInstanceKey(gameInstance)]
|
|
if svc ~= nil then
|
|
svc:HandleNativeLoadComplete(
|
|
requestId,
|
|
ok,
|
|
loadedObj,
|
|
resolvedPath,
|
|
errorMessage
|
|
)
|
|
end
|
|
end
|
|
--- 为指定 GameInstanceContext 装配生产环境 ResourceService。
|
|
--
|
|
-- @noSelf
|
|
function ____exports.CreateUEResourceService(ctx)
|
|
if ctx.gameInstance == nil then
|
|
return nil
|
|
end
|
|
local subsystem = UE.ULuaResourceSubsystem.GetFromGameInstance(ctx.gameInstance)
|
|
if subsystem == nil then
|
|
logger:Error("ULuaResourceSubsystem unavailable on GameInstance")
|
|
return nil
|
|
end
|
|
RegisterBaseAssetCatalog(subsystem)
|
|
return __TS__New(
|
|
____exports.ResourceService,
|
|
ctx,
|
|
__TS__New(____exports.UEResourceBackend, subsystem)
|
|
)
|
|
end
|
|
function __module.StashData(env, to)
|
|
local ____has = to.__has__
|
|
if not ____has then
|
|
____has = {}
|
|
to.__has__ = ____has
|
|
end
|
|
to.serviceByGameInstanceKey = serviceByGameInstanceKey
|
|
____has.serviceByGameInstanceKey = true
|
|
end
|
|
function __module.RestoreData(env, from)
|
|
local ____has = from.__has__
|
|
if not ____has then
|
|
return
|
|
end
|
|
if ____has.serviceByGameInstanceKey then
|
|
serviceByGameInstanceKey = from.serviceByGameInstanceKey
|
|
end
|
|
end
|
|
function __module.UpdateImports()
|
|
Logger = M__share_base_logger.Logger
|
|
ScopedObject = M__share_base_scoped_object.ScopedObject
|
|
tbpcall = M__share_base_traceback.tbpcall
|
|
GetGameInstanceKey = M__framework_context_game_context.GetGameInstanceKey
|
|
RegisterBaseAssetCatalog = M__framework_resource_asset_catalog.RegisterBaseAssetCatalog
|
|
end
|