Skip to content

Commit

Permalink
重构:UnLua.lua和一些全局函数合并到UnLuaLib里
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyanghuang-tencent committed Jul 11, 2022
1 parent 1f2cc7b commit a473363
Show file tree
Hide file tree
Showing 13 changed files with 347 additions and 386 deletions.
63 changes: 0 additions & 63 deletions Content/Script/UnLua.lua

This file was deleted.

13 changes: 6 additions & 7 deletions Content/Script/UnLuaHotReload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ end

local print = function(...)
if config.debug then
UEPrint("HotReload ", ...)
UnLua.Log("HotReload ", ...)
end
end

local table = table
local debug = debug
local pairs = pairs
local origin_require = require
local error_msg = ""

local function safe_pairs(t)
local _next, _t, _nil = pairs(t)
Expand All @@ -94,8 +93,9 @@ local function safe_pairs(t)
return safe_next, _t, _nil
end

local function error_handler(err)
print(error_msg .. " " .. err .. " " .. debug.traceback())
local function load_error_handler(err)
local msg = err .. "\n" .. debug.traceback()
UnLua.LogError(msg)
end

local function call_hook(name, ...)
Expand Down Expand Up @@ -162,7 +162,7 @@ local function make_sandbox()

local func, env = load(module_name)
if func then
local _, new_module = xpcall(func, error_handler, ...)
local _, new_module = xpcall(func, load_error_handler, ...)
if new_module == nil then
new_module = env
end
Expand Down Expand Up @@ -577,8 +577,7 @@ local function reload_modules(module_names)
else
local func, env = sandbox.load(module_name)
if func ~= nil then
error_msg = module_name
local ok, new_module = xpcall(func, error_handler)
local ok, new_module = xpcall(func, load_error_handler)
if not ok then
sandbox.exit()
return
Expand Down
63 changes: 0 additions & 63 deletions Plugins/UnLua/Content/UnLua.lua

This file was deleted.

13 changes: 6 additions & 7 deletions Plugins/UnLua/Content/UnLuaHotReload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ end

local print = function(...)
if config.debug then
UEPrint("HotReload ", ...)
UnLua.Log("HotReload ", ...)
end
end

local table = table
local debug = debug
local pairs = pairs
local origin_require = require
local error_msg = ""

local function safe_pairs(t)
local _next, _t, _nil = pairs(t)
Expand All @@ -94,8 +93,9 @@ local function safe_pairs(t)
return safe_next, _t, _nil
end

local function error_handler(err)
print(error_msg .. " " .. err .. " " .. debug.traceback())
local function load_error_handler(err)
local msg = err .. "\n" .. debug.traceback()
UnLua.LogError(msg)
end

local function call_hook(name, ...)
Expand Down Expand Up @@ -162,7 +162,7 @@ local function make_sandbox()

local func, env = load(module_name)
if func then
local _, new_module = xpcall(func, error_handler, ...)
local _, new_module = xpcall(func, load_error_handler, ...)
if new_module == nil then
new_module = env
end
Expand Down Expand Up @@ -577,8 +577,7 @@ local function reload_modules(module_names)
else
local func, env = sandbox.load(module_name)
if func ~= nil then
error_msg = module_name
local ok, new_module = xpcall(func, error_handler)
local ok, new_module = xpcall(func, load_error_handler)
if not ok then
sandbox.exit()
return
Expand Down
160 changes: 0 additions & 160 deletions Plugins/UnLua/Source/UnLua/Private/LuaCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1257,166 +1257,6 @@ int32 TraverseTable(lua_State *L, int32 Index, void *Userdata, bool(*TraverseWor
return INDEX_NONE;
}

extern int32 UObject_Identical(lua_State *L);
extern int32 UObject_Delete(lua_State *L);

int32 Global_GetUProperty(lua_State *L)
{
if (lua_type(L, 2) != LUA_TUSERDATA)
{
lua_pushnil(L);
return 1;
}

const auto Registry = UnLua::FLuaEnv::FindEnvChecked(L).GetObjectRegistry();
const auto Property = Registry->Get<UnLua::ITypeOps>(L, -1);
if (!Property.IsValid())
{
lua_pushnil(L);
return 1;
}

void* Self = GetCppInstance(L, 1);
if (!Self)
{
lua_pushnil(L);
return 1;
}

if (UnLua::LowLevel::IsReleasedPtr(Self))
{
UE_LOG(LogUnLua, Warning, TEXT("attempt to read property '%s' on released object"), *Property->GetName());
lua_pushnil(L);
return 1;
}

Property->Read(L, Self, false);
return 1;
}

int32 Global_SetUProperty(lua_State *L)
{
if (lua_type(L, 2) == LUA_TUSERDATA)
{
const auto Registry = UnLua::FLuaEnv::FindEnvChecked(L).GetObjectRegistry();
const auto Property = Registry->Get<UnLua::ITypeOps>(L, 2);
if (!Property.IsValid())
return 0;

UObject* Object = UnLua::GetUObject(L, 1, false);
if (UnLua::LowLevel::IsReleasedPtr(Object))
{
UE_LOG(LogUnLua, Warning, TEXT("attempt to write property '%s' on released object"), *Property->GetName());
return 0;
}

Property->Write(L, Object, 3); // set UProperty value
}
return 0;
}

extern int32 UObject_Load(lua_State *L);
extern int32 UClass_Load(lua_State *L);

/**
* Global glue function to load a UObject
*/
int32 Global_LoadObject(lua_State *L)
{
return UObject_Load(L);
}

/**
* Global glue function to load a UClass
*/
int32 Global_LoadClass(lua_State *L)
{
return UClass_Load(L);
}

/**
* Global glue function to create a UObject
*/
int32 Global_NewObject(lua_State *L)
{
int32 NumParams = lua_gettop(L);
if (NumParams < 1)
{
UNLUA_LOGERROR(L, LogUnLua, Log, TEXT("%s: Invalid parameters!"), ANSI_TO_TCHAR(__FUNCTION__));
return 0;
}

UClass *Class = Cast<UClass>(UnLua::GetUObject(L, 1));
if (!Class)
{
UNLUA_LOGERROR(L, LogUnLua, Log, TEXT("%s: Invalid class!"), ANSI_TO_TCHAR(__FUNCTION__));
return 0;
}

UObject *Outer = NumParams > 1 ? UnLua::GetUObject(L, 2) : (UObject*)GetTransientPackage();
if (!Outer)
{
UNLUA_LOGERROR(L, LogUnLua, Log, TEXT("%s: Invalid outer!"), ANSI_TO_TCHAR(__FUNCTION__));
return 0;
}

FName Name = NumParams > 2 ? FName(lua_tostring(L, 3)) : NAME_None;
//EObjectFlags Flags = NumParams > 3 ? EObjectFlags(lua_tointeger(L, 4)) : RF_NoFlags;

{
const char *ModuleName = NumParams > 3 ? lua_tostring(L, 4) : nullptr;
int32 TableRef = LUA_NOREF;
if (NumParams > 4 && lua_type(L, 5) == LUA_TTABLE)
{
lua_pushvalue(L, 5);
TableRef = luaL_ref(L, LUA_REGISTRYINDEX);
}
FScopedLuaDynamicBinding Binding(L, Class, UTF8_TO_TCHAR(ModuleName), TableRef);
#if ENGINE_MAJOR_VERSION <= 4 && ENGINE_MINOR_VERSION < 26
UObject* Object = StaticConstructObject_Internal(Class, Outer, Name);
#else
FStaticConstructObjectParameters ObjParams(Class);
ObjParams.Outer = Outer;
ObjParams.Name = Name;
UObject* Object = StaticConstructObject_Internal(ObjParams);
#endif
if (Object)
{
UnLua::PushUObject(L, Object);
}
else
{
UNLUA_LOGERROR(L, LogUnLua, Log, TEXT("%s: Failed to new object for class %s!"), ANSI_TO_TCHAR(__FUNCTION__), *Class->GetName());
return 0;
}
}

return 1;
}

int32 Global_Print(lua_State *L)
{
FString StrLog;
int32 nargs = lua_gettop(L);
for (int32 i = 1; i <= nargs; ++i)
{
const char* arg = luaL_tolstring(L, i, nullptr);
if (!arg)
{
arg = "";
}
StrLog += UTF8_TO_TCHAR(arg);
StrLog += TEXT(" ");
}

UE_LOG(LogUnLua, Log, TEXT("UNLUA_PRINT[%d] : %s"), GFrameNumber,*StrLog);
if (IsInGameThread())
{
UKismetSystemLibrary::PrintString(GWorld, StrLog, false, false);
}
return 0;
}

/**
* __index meta methods for enum
*/
Expand Down
Loading

1 comment on commit a473363

@xuyanghuang-tencent
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.