chore: initialize UE 5.8 + UnLua game framework project
This commit is contained in:
commit
943af16954
397 files changed
+95588
No files matched your search
@@ -0,0 +1,37 @@
|
||||
---@class Framework.Bootstrap
|
||||
---Central Lua game framework bootstrapper initialized by ULuaGameInstance.
|
||||
local Log = require("Framework.Log")
|
||||
local EventBus = require("Framework.EventBus")
|
||||
local HotReload = require("Framework.HotReload")
|
||||
|
||||
local Bootstrap = {
|
||||
Version = "0.1.0",
|
||||
IsInitialized = false,
|
||||
GameInstance = nil,
|
||||
}
|
||||
|
||||
---@param gameInstance ULuaGameInstance
|
||||
function Bootstrap.Init(gameInstance)
|
||||
Bootstrap.GameInstance = gameInstance
|
||||
Bootstrap.IsInitialized = true
|
||||
|
||||
_G.GameFramework = {
|
||||
Log = Log,
|
||||
EventBus = EventBus,
|
||||
HotReload = HotReload,
|
||||
Bootstrap = Bootstrap,
|
||||
GameInstance = gameInstance,
|
||||
}
|
||||
|
||||
Log.Info("Lua GameFramework initialized (version=%s, Lua=%s)", Bootstrap.Version, _VERSION)
|
||||
end
|
||||
|
||||
function Bootstrap.Shutdown()
|
||||
Log.Info("Lua GameFramework shutting down...")
|
||||
EventBus.Clear()
|
||||
Bootstrap.GameInstance = nil
|
||||
Bootstrap.IsInitialized = false
|
||||
_G.GameFramework = nil
|
||||
end
|
||||
|
||||
return Bootstrap
|
||||
@@ -0,0 +1,94 @@
|
||||
---@class Framework.EventBus
|
||||
---Lightweight publish/subscribe event bus for decoupled Lua game systems.
|
||||
local EventBus = {}
|
||||
|
||||
local Listeners = {}
|
||||
|
||||
---@param eventName string
|
||||
---@param callback function
|
||||
---@param owner any|nil
|
||||
function EventBus.On(eventName, callback, owner)
|
||||
assert(type(eventName) == "string", "EventBus.On: eventName must be a string")
|
||||
assert(type(callback) == "function", "EventBus.On: callback must be a function")
|
||||
|
||||
local list = Listeners[eventName]
|
||||
if not list then
|
||||
list = {}
|
||||
Listeners[eventName] = list
|
||||
end
|
||||
|
||||
for _, entry in ipairs(list) do
|
||||
if entry.callback == callback and entry.owner == owner then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
list[#list + 1] = {
|
||||
callback = callback,
|
||||
owner = owner,
|
||||
}
|
||||
end
|
||||
|
||||
---@param eventName string
|
||||
---@param callback function
|
||||
---@param owner any|nil
|
||||
function EventBus.Off(eventName, callback, owner)
|
||||
local list = Listeners[eventName]
|
||||
if not list then
|
||||
return
|
||||
end
|
||||
|
||||
for i = #list, 1, -1 do
|
||||
local entry = list[i]
|
||||
if entry.callback == callback and (owner == nil or entry.owner == owner) then
|
||||
table.remove(list, i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@param owner any
|
||||
function EventBus.OffAllForOwner(owner)
|
||||
if owner == nil then
|
||||
return
|
||||
end
|
||||
for _, list in pairs(Listeners) do
|
||||
for i = #list, 1, -1 do
|
||||
if list[i].owner == owner then
|
||||
table.remove(list, i)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@param eventName string
|
||||
function EventBus.Emit(eventName, ...)
|
||||
local list = Listeners[eventName]
|
||||
if not list then
|
||||
return
|
||||
end
|
||||
|
||||
-- Snapshot to allow listeners to unsubscribe during dispatch
|
||||
local snapshot = {}
|
||||
for i = 1, #list do
|
||||
snapshot[i] = list[i]
|
||||
end
|
||||
|
||||
for i = 1, #snapshot do
|
||||
local entry = snapshot[i]
|
||||
local ok, err
|
||||
if entry.owner ~= nil then
|
||||
ok, err = pcall(entry.callback, entry.owner, ...)
|
||||
else
|
||||
ok, err = pcall(entry.callback, ...)
|
||||
end
|
||||
if not ok then
|
||||
print(string.format("[UnrealLuaDemo][ERROR] EventBus(%s): %s", eventName, tostring(err)))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function EventBus.Clear()
|
||||
Listeners = {}
|
||||
end
|
||||
|
||||
return EventBus
|
||||
@@ -0,0 +1,57 @@
|
||||
---@class Framework.HotReload
|
||||
---Coordinates Lua module hot-reloading with UnLua and notifies listeners.
|
||||
local Log = require("Framework.Log")
|
||||
local EventBus = require("Framework.EventBus")
|
||||
|
||||
local HotReload = {}
|
||||
|
||||
HotReload.EVENT_BEFORE_RELOAD = "Framework.BeforeHotReload"
|
||||
HotReload.EVENT_AFTER_RELOAD = "Framework.AfterHotReload"
|
||||
|
||||
---Reloads a specific Lua module (clears package.loaded and re-requires it).
|
||||
---@param moduleName string Dot-separated Lua module name, e.g. "Game.LuaGameMode"
|
||||
---@return boolean ok
|
||||
---@return any resultOrErr
|
||||
function HotReload.ReloadModule(moduleName)
|
||||
if type(moduleName) ~= "string" or moduleName == "" then
|
||||
return false, "Invalid module name"
|
||||
end
|
||||
|
||||
Log.Info("Hot-reloading Lua module: %s", moduleName)
|
||||
EventBus.Emit(HotReload.EVENT_BEFORE_RELOAD, moduleName)
|
||||
|
||||
package.loaded[moduleName] = nil
|
||||
local ok, result = pcall(require, moduleName)
|
||||
if not ok then
|
||||
Log.Error("Failed to hot-reload module '%s': %s", moduleName, tostring(result))
|
||||
return false, result
|
||||
end
|
||||
|
||||
EventBus.Emit(HotReload.EVENT_AFTER_RELOAD, moduleName, result)
|
||||
Log.Info("Successfully hot-reloaded Lua module: %s", moduleName)
|
||||
return true, result
|
||||
end
|
||||
|
||||
---Triggers UnLua's built-in hot reload across all loaded modules.
|
||||
---@return boolean ok
|
||||
function HotReload.ReloadAll()
|
||||
Log.Info("Triggering full UnLua HotReload...")
|
||||
EventBus.Emit(HotReload.EVENT_BEFORE_RELOAD, "*")
|
||||
|
||||
if UnLua and type(UnLua.HotReload) == "function" then
|
||||
local ok, err = pcall(UnLua.HotReload)
|
||||
if not ok then
|
||||
Log.Error("UnLua.HotReload() failed: %s", tostring(err))
|
||||
return false
|
||||
end
|
||||
else
|
||||
Log.Warn("UnLua.HotReload is unavailable; skipping global hot reload.")
|
||||
return false
|
||||
end
|
||||
|
||||
EventBus.Emit(HotReload.EVENT_AFTER_RELOAD, "*")
|
||||
Log.Info("UnLua HotReload completed.")
|
||||
return true
|
||||
end
|
||||
|
||||
return HotReload
|
||||
@@ -0,0 +1,26 @@
|
||||
---@class Framework.Log
|
||||
local Log = {}
|
||||
|
||||
local TAG = "[UnrealLuaDemo]"
|
||||
|
||||
local function FormatMessage(level, fmt, ...)
|
||||
local ok, msg = pcall(string.format, tostring(fmt), ...)
|
||||
if not ok then
|
||||
msg = tostring(fmt)
|
||||
end
|
||||
return string.format("%s[%s] %s", TAG, level, msg)
|
||||
end
|
||||
|
||||
function Log.Info(fmt, ...)
|
||||
print(FormatMessage("INFO", fmt, ...))
|
||||
end
|
||||
|
||||
function Log.Warn(fmt, ...)
|
||||
print(FormatMessage("WARN", fmt, ...))
|
||||
end
|
||||
|
||||
function Log.Error(fmt, ...)
|
||||
print(FormatMessage("ERROR", fmt, ...))
|
||||
end
|
||||
|
||||
return Log
|
||||
Reference in new issue
Block a user