chore: initialize UE 5.8 + UnLua game framework project

This commit is contained in:
szy73711 committed 2026-11-28 01:45:03 +08:00
commit 943af16954
397 files changed
+95588

No files matched your search

@@ -0,0 +1,316 @@
---@class UnLua
local UnLua = {}
---Similar to "package.path" for lua module searching with UnLua loader. Used by FLuaEnv::LoadFromFileSystem.
UnLua.PackagePath = "Content/Script/?.lua;Plugins/UnLua/Content/Script/?.lua"
---Whether or not enable FText support at lua runtime which will no longer be treated as a string.
UnLua.FTextEnabled = false
---Define a lua class for binding.
---@param SuperClass string @[opt]
function UnLua.Class(SuperClass)
end
---Hot reload lua modules
---@param ModuleNames table @[opt]Specify a list of module names that need hot reloading. If this parameter is not passed, it will be obtained by default according to the file modification timestamp
function UnLua.HotReload(ModuleNames)
end
---Add or find a manual reference to specified UObject. This prevents the object from UE GC.
---@param Object UObject
---@return userdata @Proxy object of the reference. The reference of UObject will be removed when proxy object deleted by lua GC.
function UnLua.Ref(Object)
end
---Force remove all manual reference to specified UObject.
---@param Object UObject
function UnLua.Unref(Object)
end
_G.UnLua = UnLua
---@class TArray<TElement>
local TArray = {}
---Get the number of items in an array
---@return integer
function TArray:Length()
end
---Get the number of items in an array
---@return integer
function TArray:Num()
end
---Add item to array
---@param NewItem TElement @The item to look for
---@return integer @The index of the newly added item
function TArray:Add(NewItem)
end
---Add item to array (unique)
---@param NewItem TElement @The item to add to the array
---@return integer @The index of the newly added item, or INDEX_NONE if the item is already present in the array
function TArray:AddUnique(NewItem)
end
---Finds the index of the first instance of the item within the array
---@param ItemToFind TElement @The item to look for
---@return integer @The index the item was found at, or -1 if not found
function TArray:Find(ItemToFind)
end
---Insert item at the given index into the array.
---@param NewItem TElement @The item to insert into the array
---@param Index integer @The index at which to insert the item into the array
function TArray:Insert(NewItem, Index)
end
---Remove item at the given index from the array.
---@param IndexToRemove integer @The index into the array to remove from
function TArray:Remove(IndexToRemove)
end
---Remove all instances of item from array.
---@param Item TElement @The item to remove from the array
---@return boolean @True if one or more items were removed
function TArray:RemoveItem(Item)
end
---Clear the array, removes all content
function TArray:Clear()
end
---Reserve space for N elements
---@param Size integer @Size
---@return bool @whether the operation succeed
function TArray:Reserve(Size)
end
---Resize Array to specified size.
---@param Size integer @The new size of the array
function TArray:Resize(Size)
end
---Get address of the i'th element
---@param Index integer @the index
---return lightuserdata @the address of the i'th element
function TArray:GetData(Index)
end
---Given an array and an index, returns a copy of the item found at that index
---@param Index integer @The index in the array to get an item from
---@return any @A copy of the item stored at the index
function TArray:Get(Index)
end
---Given an array and an index, returns a reference of the item found at that index
---@param Index integer @The index in the array to get an item from
---@return TElement @A reference of the item stored at the index
function TArray:GetRef(Index)
end
---Given an array and an index, assigns the item to that array element
---@param Index integer @The index to assign the item to
---@param Item TElement @The item to assign to the index of the array
function TArray:Set(Index, Item)
end
---Swaps the elements at the specified positions
---If the specified positions are equal, invoking this method leaves the array unchanged
---@param FirstIndex integer @The index of one element to be swapped
---@param SecondIndex integer @The index of the other element to be swapped
function TArray:Swap(FirstIndex, SecondIndex)
end
---Shuffle (randomize) the elements
function TArray:Shuffle()
end
---Get the last valid index
---@return integer @The last valid index of the array
function TArray:LastIndex()
end
---Tests if IndexToTest is valid, i.e. greater than or equal to zero, and less than the number of elements in array.
---@param IndexToTest integer @The Index, that we want to test for being valid
---@return boolean @True if the Index is Valid, i.e. greater than or equal to zero, and less than the number of elements in array.
function TArray:IsValidIndex(IndexToTest)
end
---Returns true if the array contains the given item
---@param ItemToFind any @The item to look for
---@return boolean @True if the item was found within the array
function TArray:Contains(ItemToFind)
end
---Append an array to another array
---@param OtherArray TArray @The array to add
function TArray:Append(OtherArray)
end
---Get a lua table copy of this array.
---@return table
function TArray:ToTable()
end
---@type fun(ElementType:any):TArray
UE.TArray = TArray
---@class TMap<TKey,TValue>
local TMap = {}
---Determines the number of entries in map
---@return integer
function TMap:Length()
end
---Determines the number of entries in map
---@return integer
function TMap:Num()
end
---Adds a key and value to the map. If something already uses the provided key it will be overwritten with the new value.
---After calling Key is guaranteed to be associated with Value until a subsequent mutation of the Map.
---@param Key TKey @The key that will be used to look the value up
---@param Value TValue @The value to be retrieved later
function TMap:Add(Key, Value)
end
---Removes a key and its associated value from the map.
---@param Key any @The key that will be used to look the value up
---@return boolean @True if an item was removed (False indicates nothing in the map uses the provided key)
function TMap:Remove(Key)
end
---Finds the value associated with the provided Key
---@param Key any @The key that will be used to look the value up
---@return any @The value associated with the key
function TMap:Find(Key)
end
---Finds the value associated with the provided Key
---@param Key any @The key that will be used to look the value up
---@return any @The value associated with the key
function TMap:FindRef(Key)
end
---Clears all entries, resetting it to empty
function TMap:Clear()
end
---Outputs an array of all keys present in the map
---@return TArray @All keys present in the map
function TMap:Keys()
end
---Outputs an array of all values present in the map
---@return TArray @All values present in the map
function TMap:Values()
end
---Get a lua table copy of this map.
---@return table
function TMap:ToTable()
end
---@type fun(KeyType:any,ValueType:any):TMap
UE.TMap = TMap
---@class TSet<TElement>
local TSet = {}
---Get the number of items in set.
---@return integer
function TSet:Length()
end
---Get the number of items in set.
---@return integer
function TSet:Num()
end
---Adds item to set
---@param NewItem TElement @The item to add to the set
function TSet:Add(NewItem)
end
---Remove item from set
---@param Item TElement @The item to remove from the set
---@return boolean @True if an item was removed (False indicates no equivalent item was present)
function TSet:Remove(Item)
end
---Returns true if the set contains the given item.
---@param ItemToFind any @The item to look for
---@return boolean @True if the item was found within the set
function TSet:Contains(ItemToFind)
end
---Clear the set, removes all content.
function TSet:Clear()
end
---Outputs an Array containing copies of the entries of a Set.
---@return TArray @Array
function TSet:ToArray()
end
---Get a lua table copy of this set.
---@return table
function TSet:ToTable()
end
---@type fun(ElementType:any):TSet
UE.TSet = TSet
---@class UClass
local UClass = {}
---Load a class object
---@param Path string @path to the class
---@return UClass
function UClass.Load(Path)
end
---Test whether this class is a child of another class
---@param TargetClass UClass @target class
---@return boolean @true if this object is of the specified type.
function UClass:IsChildOf(TargetClass)
end
---Get default object of a class.
---@return UObject @class default obejct
function UClass:GetDefaultObject()
end
UE.UClass = UClass
---@class MulticastDelegate
local MulticastDelegate = {}
---Add a callback to the delegate
---@param Object UObject @receiver object
---@param Callback function @callback
function MulticastDelegate:Add(Object, Function)
end
---Remove a callback from the delegate
---@param Object UObject @receiver object
---@param Callback function @callback
function MulticastDelegate:Remove(Object, Function)
end
---Clear all callbacks for the delegate
function MulticastDelegate:Clear()
end
---Call all callbacks bound to the delegate
function MulticastDelegate:Broadcast()
end
---Detect if any callback bound to this delegate
---@return boolean
function MulticastDelegate:IsBound()
end
@@ -0,0 +1,17 @@
Date/Time,Word Count,zh-Hans
2022.03.17-11.26.16,101,101
2022.05.28-01.30.11,166,166
2022.06.07-12.00.37,183,183
2022.06.07-12.18.21,187,187
2022.06.07-12.23.19,321,321
2022.06.07-12.38.33,187,187
2022.06.07-12.40.09,515,515
2022.06.07-12.44.45,585,585
2022.06.07-13.04.33,515,515
2022.06.07-13.08.26,585,585
2022.06.07-13.27.43,599,599
2022.06.07-18.25.31,562,562
2022.06.07-19.09.46,606,606
2022.06.07-21.37.50,504,504
2022.06.07-22.44.41,568,568
2022.09.24-18.38.33,665,665
1 Date/Time Word Count zh-Hans
2 2022.03.17-11.26.16 101 101
3 2022.05.28-01.30.11 166 166
4 2022.06.07-12.00.37 183 183
5 2022.06.07-12.18.21 187 187
6 2022.06.07-12.23.19 321 321
7 2022.06.07-12.38.33 187 187
8 2022.06.07-12.40.09 515 515
9 2022.06.07-12.44.45 585 585
10 2022.06.07-13.04.33 515 515
11 2022.06.07-13.08.26 585 585
12 2022.06.07-13.27.43 599 599
13 2022.06.07-18.25.31 562 562
14 2022.06.07-19.09.46 606 606
15 2022.06.07-21.37.50 504 504
16 2022.06.07-22.44.41 568 568
17 2022.09.24-18.38.33 665 665
Whitespace-only changes.
@@ -0,0 +1,64 @@
local Input = UnLua.Input
local M = {}
local function MakeBinding(BindingClass, Args)
local Ret = BindingClass()
return Ret
end
local function MakeLuaFunction(Module, Prefix, Handler, No)
local Name = string.format("%s_%d", Prefix, No)
if not Module[Name] then
Module[Name] = Handler
return Name
end
return MakeLuaFunction(Module, Prefix, Handler, No + 1)
end
local SignatureFunctionNames = {
"EnhancedInputActionDigital",
"EnhancedInputActionAxis1D",
"EnhancedInputActionAxis2D",
"EnhancedInputActionAxis3D"
}
--- 为当前模块绑定操作输入响应
---@param Module table @需要绑定的lua模块
---@param InputAction string @绑定的操作资源路径,是个UInputAction
---@param TriggerEvent string @绑定的事件名称,参考ETriggerEvent下的命名
---@param Handler fun(Key:FKey) @事件响应回调函数
---@param Args table @[opt]扩展参数
function M.BindAction(Module, ActionPath, TriggerEvent, Handler, Args)
Args = Args or {}
Module.__UnLuaInputBindings = Module.__UnLuaInputBindings or {}
local InputAction = UE.UObject.Load(ActionPath)
if not InputAction then
return
end
local SignatureFunctionName = SignatureFunctionNames[InputAction.ValueType + 1]
local FunctionName = MakeLuaFunction(Module, string.format("UnLuaInput_%s_%s", InputAction:GetName(), TriggerEvent), Handler, 0)
local Bindings = Module.__UnLuaInputBindings
table.insert(Bindings, function(Manager, Class)
local BindingObject = Manager:GetOrAddBindingObject(Class, UE.UEnhancedInputActionDelegateBinding)
for _, OldBinding in pairs(BindingObject.InputActionDelegateBindings) do
if OldBinding.FunctionNameToBind == FunctionName then
-- PIE下这个蓝图可能已经绑定过了
Manager:Override(Class, SignatureFunctionName, FunctionName)
return
end
end
local Binding = MakeBinding(UE.FBlueprintEnhancedInputActionBinding, Args)
Binding.InputAction = InputAction
Binding.TriggerEvent = UE.ETriggerEvent[TriggerEvent]
Binding.FunctionNameToBind = FunctionName
BindingObject.InputActionDelegateBindings:Add(Binding)
Manager:Override(Class, SignatureFunctionName, FunctionName)
end)
end
return M
@@ -0,0 +1,629 @@
------------------------------------------
--- HotFix - 运行时HotFix支持 参考云风方案
--- 替换运行环境中的函数,保持upvalue和运行时的table
--- 为开发期设计,尽量替换。最差的结果就是重新启动
--- 原表改变,类型改变 后逻辑由业务保证正确
--- 基于lua 5.3,重定向require,使用env加载实现沙盒
--- 提供一系列回调注册,可以在hotfix前后做处理
------------------------------------------
-- 在Module中如果定义此标记,则对此module进行reload操作
-- local M = { HOT_RELOAD = true }
local HOT_RELOAD_MARK = "HOT_RELOAD"
local loaded_modules = setmetatable({}, { __mode = "v" })
local ignore_modules = {}
local config = {
debug = false,
script_root_path = UE.UUnLuaFunctionLibrary.GetScriptRootPath(),
ignore_modules = ignore_modules
}
local hook = {
module_loaded = nil
}
local M = {
config = config,
hook = hook
}
local dump = function(tbl, max_indent)
local rep = string.rep
local handled = {}
local function traverse(tbl, indent)
if not indent then indent = 0 end
if type(tbl) ~= "table" then return tostring(tbl) end
if(indent > max_indent) then return tostring(tbl) end
if handled[tbl] then
return ""
end
handled[tbl] = true
local ret = rep(" ", indent) .. "{\r\n"
indent = indent + 2
for k, v in pairs(tbl) do
ret = ret .. rep(" ", indent)
if type(k) == "number" then
ret = ret .. "[" .. k .. "] = "
elseif type(k) == "string" then
ret = ret .. k .. " = "
else
ret = ret .. tostring(k) .. " = "
end
if type(v) == "number" then
ret = ret .. v .. ",\r\n"
elseif type(v) == "string" then
ret = ret .. "\"" .. v .. "\",\r\n"
elseif type(v) == "table" then
ret = ret .. tostring(v) .. traverse(v, indent + 2) .. ",\r\n"
else
ret = ret .. "\"" .. tostring(v) .. "\",\r\n"
end
end
ret = ret .. rep(" ", indent - 2) .. "}"
return ret
end
if max_indent == nil then
max_indent = 64
end
return traverse(tbl)
end
local print = function(...)
if config.debug then
UnLua.Log("HotReload ", ...)
end
end
local table = table
local debug = debug
local pairs = pairs
local origin_require = require
local function safe_pairs(t)
local _next, _t, _nil = pairs(t)
local function safe_next(_t, i)
local ok, result, next_i, value = pcall(_next, _t, i)
if ok then
return result, next_i, value
else
return nil
end
end
return safe_next, _t, _nil
end
local function load_error_handler(err)
local msg = err .. "\n" .. debug.traceback()
UnLua.LogError(msg)
end
local function call_hook(name, ...)
local func = hook[name]
if not func then
return
end
local ok, result = pcall(func, ...)
if not ok then
print(string.format("calling hook function '%s' failed : %s", name, result))
end
end
--- 脚本加载的修改时间
---@type table<string, number>
local loaded_module_times = {}
local function get_last_modified_time(module_name)
local filename = config.script_root_path .. module_name:gsub("%.", "/") .. ".lua"
return UE.UUnLuaFunctionLibrary.GetFileLastModifiedTimestamp(filename)
end
local function make_sandbox()
local reloading
local loaded
local proxy = setmetatable({}, { __index = _G, __newindex = _G })
local env_mt = { __index = proxy, __newindex = proxy }
local function load(module_name)
local found, chunk
for i, searcher in ipairs(package.searchers) do
chunk = searcher(module_name)
if type(chunk) == "function" then
found = true
break
end
end
if not found then
return
end
local env = {}
setmetatable(env, env_mt)
debug.setupvalue(chunk, 1, env)
return chunk, env
end
local function require(module_name, ...)
-- https://github.com/lua/lua/blob/v5.4.0/loadlib.c#L680
-- https://github.com/lua/lua/blob/v5.3/loadlib.c#L617
-- lua5.4之后会返回2个值,这里保持一样的行为
if package.loaded[module_name] ~= nil then
return package.loaded[module_name], nil
end
local loaded = loaded_modules[module_name]
if loaded then
if package.loaded[module_name] == nil then
package.loaded[module_name] = loaded
end
return loaded, nil
end
local func, env = load(module_name)
if func then
local _, new_module = xpcall(func, load_error_handler, ...)
if loaded_modules[module_name] == nil then
loaded_modules[module_name] = new_module
package.loaded[module_name] = new_module
loaded_module_times[module_name] = get_last_modified_time(module_name)
call_hook("module_loaded", new_module, module_name, false)
return new_module, nil
end
else
return origin_require(module_name, ...)
end
return env, nil
end
local function enter(modules)
reloading = true
loaded = setmetatable({}, { __mode = "kv" })
for name, module in pairs(modules) do
loaded[name] = module
loaded[module] = name
end
end
local function exit()
reloading = false
loaded = setmetatable({}, { __mode = "kv" })
end
local function is_loaded(obj)
return loaded[obj] ~= nil
end
proxy.require = function(module_name, ...)
if reloading then
if loaded[module_name] ~= nil then
return loaded[module_name]
end
end
local ret = require(module_name, ...)
if reloading then
loaded[module_name] = ret
loaded[ret] = module_name
end
return ret
end
return {
enter = enter,
exit = exit,
load = load,
require = require,
is_loaded = is_loaded,
}
end
local sandbox = make_sandbox()
local function merge_objects(module_res)
for _, m in ipairs(module_res) do
assert(m.old_module)
for index, v in ipairs(m.values) do
for name, value_map in pairs(v) do
for old, new in pairs(value_map) do
if old == nil and not sandbox.is_loaded(new) then
m.old_module[name] = new
elseif type(new) == "table" and not sandbox.is_loaded(new) then
print("COPY", tostring(new), tostring(old))
for k, nv in pairs(new) do
old[k] = nv
end
elseif type(new) == "function" then
local i = 1
while true do
local name,v = debug.getupvalue(new, i)
if name == nil or name == "" then
break
end
local id = debug.upvalueid(new, i)
local uv = m.upvalue_map[id]
if uv then
print("SET UV :", tostring(new), name, tostring(uv.replaced_upvalue))
debug.setupvalue(new, i, uv.replaced_upvalue)
end
i = i + 1
end
end
end
end
end
end
end
--- 枚举出module中所有函数的upvalue,Lua在不同的闭包间访问相同的外部local变量时,使用的是同样的upvalue
---@param moudule table
local function collect_module_upvalues(moudule)
local function collect_function_upvalues(func, upvalues)
assert(type(func) == "function")
local i = 1
while true do
local name, value = debug.getupvalue(func, i)
if name == nil or name == "" then
break
else
if not name:find("^[_%w]") then
error("Invalid upvalue : " .. table.concat(path, "."))
end
if not upvalues[name] then
upvalues[name] = value
if type(value) == "function" then
collect_function_upvalues(value, upvalues)
end
end
end
i = i + 1
end
end
local ret = {}
for _, v in pairs(moudule) do
if type(v) == "function" then
collect_function_upvalues(v, ret)
end
end
return ret
end
local function collect_module_info(module)
local ret = {}
if sandbox.is_loaded(module) then
return ret
end
for k, v in pairs(module) do
if sandbox.is_loaded(v) then
elseif type(v) == "function" then
table.insert(ret, {name = k, value = v})
end
end
return ret
end
---@param new_module_info table
---@param old_module table
---@return table
local function match_module(new_module_info, old_module)
local ret = {}
for _, v in ipairs(new_module_info) do
local oldFun = rawget(old_module, v.name)
if oldFun and oldFun ~= v.value then
table.insert(ret, { [v.name] = { [oldFun] = v.value } } )
end
end
return ret
end
---@param value_info_map table
---@param old_upvalues table
---@return table
local function match_upvalues(value_info_map, old_upvalues)
local ret = {}
for index, v in ipairs(value_info_map) do
for name, value_map in pairs(v) do
for old, new in pairs(value_map) do
if type(new) == "function" then
local i = 1
while true do
local name, new_upvalue = debug.getupvalue(new, i)
if name == nil or name == "" then
break
end
local id = debug.upvalueid(new, i)
if not ret[id] then
local replaced_upvalue = nil
if old_upvalues[name] ~= nil then
replaced_upvalue = old_upvalues[name]
else
-- 新增的upvalue
print("ADD NEW UPVALUE : ", tostring(new), name, tostring(new_upvalue))
-- 解决新增的upvalue是当前module
if type(new_upvalue) == "table" or type(new_upvalue) == "function" and value_info_map[new_upvalue] ~= nil then
replaced_upvalue = value_info_map[new_upvalue]
else
replaced_upvalue = new_upvalue
end
end
if replaced_upvalue then
ret[id] = { replaced_upvalue = replaced_upvalue }
end
end
i = i + 1
end
end
end
end
end
return ret
end
local function update_global(value_map)
local running_state = coroutine.running()
local exclude = { [debug] = true, [coroutine] = true, [io] = true }
exclude[exclude] = true
exclude[M] = true
exclude[sandbox] = true
exclude[value_map] = true
exclude[package] = true
exclude[package.loaded] = true
exclude[loaded_modules] = true
local update_table
local function update_running_stack(co, level)
local info = debug.getinfo(co, level + 1, "f")
if info == nil then
return
end
local f = info.func
info = nil
update_table(f)
local i = 1
while true do
local name, v = debug.getlocal(co, level + 1, i)
if name == nil then
if i > 0 then
i = -1
else
break
end
end
local nv = value_map[v]
if nv then
debug.setlocal(co, level + 1, i, nv)
update_table(nv)
else
update_table(v)
end
if i > 0 then
i = i + 1
else
i = i - 1
end
end
return update_running_stack(co, level + 1)
end
function update_table(root)
if root == nil or exclude[root] then
return
end
-- print("update_table : ", dump(root, 2))
exclude[root] = true
local t = type(root)
if t == "table" then
local mt = getmetatable(root)
if mt then update_table(mt) end
local ReplaceK = {}
for key, value in safe_pairs(root) do
local nv = value_map[value]
if nv then
rawset(root, key, nv)
update_table(nv)
else
update_table(value)
end
nv = value_map[key]
if nv then
ReplaceK[key] = nv
end
end
for key, value in pairs(ReplaceK) do
root[key], root[value] = nil, root[key]
update_table(value)
end
elseif t == "userdata" then
local mt = getmetatable(root)
if mt then update_table(mt) end
local user_value = debug.getuservalue(root)
if user_value then
local nv = value_map[user_value]
if nv then
debug.setuservalue(root, user_value)
update_table(nv)
else
update_table(user_value)
end
end
elseif t == "function" then
local i = 1
while true do
local name, v = debug.getupvalue(root, i)
if name == nil then
break
else
local nv = value_map[v]
if nv then
update_table(nv)
else
update_table(v)
end
end
i=i+1
end
end
end
update_running_stack(running_state, 2)
update_table(_G)
update_table(debug.getregistry())
end
local function update_modules(old_modules, new_modules, new_envs)
print("HOT RELOAD START")
local result = {}
for i, old_module in ipairs(old_modules) do
local new_module = new_modules[i]
local new_env = new_envs[i]
if new_module[HOT_RELOAD_MARK] then
local moduleres = {
values = {},
old_module = old_module
}
table.insert(moduleres.values, { [new_module] = { [old_module] = new_module } } )
print("--------------Print ValueMap--------------")
print(dump(moduleres.values, 6))
result[i] = moduleres
else
local new_module_info = collect_module_info(new_module)
print("--------------Print NewModuleInfo--------------")
print(dump(new_module_info))
local old_module_upvalues = collect_module_upvalues(old_module)
print("--------------Print OldModuleUpValue--------------")
print(dump(old_module_upvalues, 6))
local moduleres = {
values = {},
upvalue_map = {},
old_module = old_module,
}
moduleres.values = match_module(new_module_info, old_module)
for k, v in pairs(new_module) do
if type(v) == "function" then
old_module[k] = v
end
if old_module[k] == nil then
old_module[k] = v
end
end
print("--------------Print ValueMap--------------")
print(dump(moduleres.values))
moduleres.upvalue_map = match_upvalues(moduleres.values, old_module_upvalues)
print("--------------Print UVMap--------------")
print(dump(moduleres.upvalue_map, 10))
result[i] = moduleres
end
end
merge_objects(result)
local all_value_maps = {}
for _, rv in ipairs(result) do
for _, v in ipairs(rv.values) do
for name, value_map in pairs(v) do
for key, value in pairs(value_map) do
all_value_maps[key] = value
end
end
end
end
print("--------------Print AllValueMap--------------")
print(dump(all_value_maps))
update_global(all_value_maps)
print("HOT RELOAD END")
end
---@param module_names table
local function reload_modules(module_names)
if not module_names or #module_names == 0 then
return
end
local tmp_modules = {}
for _, module_name in ipairs(module_names) do
if loaded_modules[module_name] then
tmp_modules[module_name] = loaded_modules[module_name]
end
end
sandbox.enter(tmp_modules)
local old_modules = {}
local new_modules = {}
local module_envs = {}
for _, module_name in ipairs(module_names) do
if loaded_modules[module_name] == nil then
sandbox.require(module_name)
else
local func, env = sandbox.load(module_name)
if func ~= nil then
local ok, new_module = xpcall(func, load_error_handler)
if not ok then
sandbox.exit()
return
end
if new_module then
for k, v in pairs(env) do
new_module[k] = v
end
else
new_module = env
end
old_modules[#old_modules+1] = loaded_modules[module_name]
new_modules[#new_modules+1] = new_module
module_envs[#module_envs+1] = env
call_hook("module_loaded", new_module, module_name, true)
else
sandbox.exit()
return
end
end
end
update_modules(old_modules, new_modules, module_envs)
sandbox.exit()
end
function M.reload(module_names)
if module_names then
reload_modules(module_names)
return
end
local modified_modules = {}
for module_name, time in pairs(loaded_module_times) do
if not ignore_modules[module_name] then
local current_time = get_last_modified_time(module_name)
if current_time ~= time then
modified_modules[#modified_modules + 1] = module_name
loaded_module_times[module_name] = current_time
end
end
end
print("modified modules:", dump(modified_modules))
if #modified_modules > 0 then
reload_modules(modified_modules)
end
end
M.require = sandbox.require
return M
@@ -0,0 +1,152 @@
local M = {}
local function GetBooleanArg(Args, Name, DefaultValue)
if Args[Name] == nil then
return DefaultValue
end
return not not Args[Name]
end
local function MakeBinding(BindingClass, Args)
local Ret = BindingClass()
Ret.bConsumeInput = GetBooleanArg(Args, "ConsumeInput", true)
Ret.bExecuteWhenPaused = GetBooleanArg(Args, "ExecuteWhenPaused", false)
Ret.bOverrideParentBinding = GetBooleanArg(Args, "OverrideParentBinding", true)
return Ret
end
local function MakeLuaFunction(Module, Prefix, Handler, No)
local Name = string.format("%s_%d", Prefix, No)
if not Module[Name] then
Module[Name] = Handler
return Name
end
return MakeLuaFunction(Module, Prefix, Handler, No + 1)
end
local Modifiers = { "Ctrl", "Alt", "Shift", "Cmd" }
local function GetModifierSuffix(Args)
local Array = { "" }
for _, Modifier in ipairs(Modifiers) do
if Args[Modifier] then
table.insert(Array, Modifier)
end
end
return table.concat(Array, "_")
end
--- 为当前模块绑定按键输入响应
---@param Module table @需要绑定的lua模块
---@param KeyName string @绑定的按键名称,参考EKeys下的命名
---@param KeyEvent string @绑定的事件名称,参考EInputEvent下的命名,不需要 “IE_” 前缀
---@param Handler fun(Key:FKey) @事件响应回调函数
---@param Args table @[opt]扩展参数 使用 Ctrl/Shift/Alt/Cmd = true 来控制组合键
function M.BindKey(Module, KeyName, KeyEvent, Handler, Args)
Args = Args or {}
Module.__UnLuaInputBindings = Module.__UnLuaInputBindings or {}
local ModifierSuffix = GetModifierSuffix(Args)
local FunctionName = MakeLuaFunction(Module, string.format("UnLuaInput_%s_%s%s", KeyName, KeyEvent, ModifierSuffix), Handler, 0)
local Bindings = Module.__UnLuaInputBindings
table.insert(Bindings, function(Manager, Class)
local BindingObject = Manager:GetOrAddBindingObject(Class, UE.UInputKeyDelegateBinding)
for _, OldBinding in pairs(BindingObject.InputKeyDelegateBindings) do
if OldBinding.FunctionNameToBind == FunctionName then
-- PIE下这个蓝图可能已经绑定过了
Manager:Override(Class, "InputAction", FunctionName)
return
end
end
local InputChord = UE.FInputChord()
InputChord.Key = UE.EKeys[KeyName]
InputChord.bShift = not not Args.Shift
InputChord.bCtrl = not not Args.Ctrl
InputChord.bAlt = not not Args.Alt
InputChord.bCmd = not not Args.Cmd
local Binding = MakeBinding(UE.FBlueprintInputKeyDelegateBinding, Args)
Binding.InputChord = InputChord
Binding.InputKeyEvent = UE.EInputEvent["IE_" .. KeyEvent]
Binding.FunctionNameToBind = FunctionName
BindingObject.InputKeyDelegateBindings:Add(Binding)
Manager:Override(Class, "InputAction", FunctionName)
end)
end
--- 为当前模块绑定操作输入响应
---@param Module table @需要绑定的lua模块
---@param ActionName string @绑定的操作名称,对应 “项目设置->输入” 中设置的命名
---@param KeyEvent string @绑定的事件名称,参考EInputEvent下的命名,不需要 “IE_” 前缀
---@param Handler fun(Key:FKey) @事件响应回调函数
---@param Args table @[opt]扩展参数
function M.BindAction(Module, ActionName, KeyEvent, Handler, Args)
Args = Args or {}
Module.__UnLuaInputBindings = Module.__UnLuaInputBindings or {}
local FunctionName = MakeLuaFunction(Module, string.format("UnLuaInput_%s_%s", ActionName, KeyEvent), Handler, 0)
local Bindings = Module.__UnLuaInputBindings
table.insert(Bindings, function(Manager, Class)
local BindingObject = Manager:GetOrAddBindingObject(Class, UE.UInputActionDelegateBinding)
for _, OldBinding in pairs(BindingObject.InputActionDelegateBindings) do
if OldBinding.FunctionNameToBind == FunctionName then
-- PIE下这个蓝图可能已经绑定过了
Manager:Override(Class, "InputAction", FunctionName)
return
end
end
local Binding = MakeBinding(UE.FBlueprintInputActionDelegateBinding, Args)
Binding.InputActionName = ActionName
Binding.InputKeyEvent = UE.EInputEvent["IE_" .. KeyEvent]
Binding.FunctionNameToBind = FunctionName
BindingObject.InputActionDelegateBindings:Add(Binding)
Manager:Override(Class, "InputAction", FunctionName)
end)
end
--- 为当前模块绑定轴输入响应
---@param Module table @需要绑定的lua模块
---@param AxisName string @绑定的轴名称,对应 “项目设置->输入” 中设置的命名
---@param Handler fun(AxisValue:number) @事件响应回调函数
---@param Args table @[opt]扩展参数
function M.BindAxis(Module, AxisName, Handler, Args)
Args = Args or {}
Module.__UnLuaInputBindings = Module.__UnLuaInputBindings or {}
local FunctionName = MakeLuaFunction(Module, string.format("UnLuaInput_%s", AxisName), Handler, 0)
local Bindings = Module.__UnLuaInputBindings
table.insert(Bindings, function(Manager, Class)
local BindingObject = Manager:GetOrAddBindingObject(Class, UE.UInputAxisDelegateBinding)
for _, OldBinding in pairs(BindingObject.InputAxisDelegateBindings) do
if OldBinding.FunctionNameToBind == FunctionName then
-- PIE下这个蓝图可能已经绑定过了
Manager:Override(Class, "InputAxis", FunctionName)
return
end
end
local Binding = MakeBinding(UE.FBlueprintInputAxisDelegateBinding, Args)
Binding.InputAxisName = AxisName
Binding.FunctionNameToBind = FunctionName
BindingObject.InputAxisDelegateBindings:Add(Binding)
Manager:Override(Class, "InputAxis", FunctionName)
end)
end
function M.PerformBindings(Module, Manager, Class)
local Bindings = Module.__UnLuaInputBindings
if not Bindings then
return
end
for _, Binding in ipairs(Bindings) do
xpcall(Binding, function(Error)
UnLua.LogError(Error .. "\n" .. debug.traceback())
end, Manager, Class)
end
end
return M