diff options
author | chai <chaifix@163.com> | 2021-10-21 16:11:45 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-21 16:11:45 +0800 |
commit | 998a13b08c43b0813d1d4d38692ea7f8bd31c936 (patch) | |
tree | 20ce144f9897c47d5840e34929ca32be8f2cc2c6 | |
parent | f2dcd16fc72e2f10c9504897bacf5c4c2ecff516 (diff) |
*misc
22 files changed, 361 insertions, 206 deletions
diff --git a/Editor/EditorMain.cpp b/Editor/EditorMain.cpp index 182a2c4..95937a7 100644 --- a/Editor/EditorMain.cpp +++ b/Editor/EditorMain.cpp @@ -15,24 +15,40 @@ void ErrorHandle(cc8* msg) log_error(std::string("[Lua] ") + msg);
}
+void OnRegisterFactoryClass(LuaBind::State& state, int cls, std::string clsName, std::string pkgName)
+{
+ // 填充类型的元数据
+ lua_newtable(state);
+ int typeIdx = state.GetTop();
+ state.Push("native");
+ state.SetField(typeIdx, "mode");
+ state.Push(clsName);
+ state.SetField(typeIdx, "name");
+ state.Push(pkgName);
+ state.SetField(typeIdx, "package");
+ lua_setfield(state, cls, "_type");
+}
+
void InitLuaState()
{
LuaBind::VM vm;
vm.Setup();
vm.OpenLibs();
+
+ LuaBind::onRegisterFactoryClass = OnRegisterFactoryClass;
+
if (!SetupGameLabEditorScripting(vm.GetMainThread()))
{
log_error("Can't setup scripting.");
}
- LuaBind::State state = vm.GetMainState();
// https://stackoverflow.com/questions/21495901/loadlibrarya-and-relative-path/21495971
// ll_load装载dll,路径需要设置搜索路径
std::string workingDir = Win::GetCurrentWorkingDirectory();
Win::SetDllSearchDirectory(workingDir);
+ LuaBind::State state = vm.GetMainState();
state.DoFile("./boot.lua", ErrorHandle);
- state.DoFile("./Scripts/EditorApplication.lua", ErrorHandle);
}
#ifdef GAMELAB_DEBUG
diff --git a/Editor/GUI/GUIWindow.cpp b/Editor/GUI/GUIWindow.cpp index 107a298..0502b21 100644 --- a/Editor/GUI/GUIWindow.cpp +++ b/Editor/GUI/GUIWindow.cpp @@ -45,10 +45,7 @@ LRESULT CALLBACK GUIWindow::GUIViewWndProc(HWND hWnd, UINT message, WPARAM wPara { log_info("WndProc", "WM_PAINT"); self->SetAsRenderContext(); - glEnable(GL_BLEND); - glClearColor(1,0,0,1); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glFlush(); + BeginPaint(self->m_Handle, &ps); EndPaint(self->m_Handle, &ps); UpdateWindow(self->m_Handle); diff --git a/Editor/IMGUI/GUIButton.cpp b/Editor/GUI/IMGUI/GUIButton.cpp index e69de29..e69de29 100644 --- a/Editor/IMGUI/GUIButton.cpp +++ b/Editor/GUI/IMGUI/GUIButton.cpp diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj b/Projects/VisualStudio/Editor/Editor.vcxproj index e6ffe4b..5bcaf94 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj +++ b/Projects/VisualStudio/Editor/Editor.vcxproj @@ -149,6 +149,7 @@ <ClCompile Include="..\..\..\Editor\Graphics\Graphics.cpp" />
<ClCompile Include="..\..\..\Editor\GUI\ContainerWindow.cpp" />
<ClCompile Include="..\..\..\Editor\GUI\Dock.cpp" />
+ <ClCompile Include="..\..\..\Editor\GUI\IMGUI\GUIButton.cpp" />
<ClCompile Include="..\..\..\Editor\GUI\MainWindow.cpp" />
<ClCompile Include="..\..\..\Editor\GUI\WindowManager.cpp" />
<ClCompile Include="..\..\..\Editor\GUI\GUIWindow.cpp" />
@@ -156,7 +157,6 @@ <ClCompile Include="..\..\..\Editor\GUI\SplitWindow.cpp" />
<ClCompile Include="..\..\..\Editor\GUI\WindowUtil.cpp" />
<ClCompile Include="..\..\..\Editor\GUI\WinUtils.cpp" />
- <ClCompile Include="..\..\..\Editor\IMGUI\GUIButton.cpp" />
<ClCompile Include="..\..\..\Editor\Resource\ResourceManager.cpp" />
<ClCompile Include="..\..\..\Editor\Scripting\EditorGUI\ContainerWindow.bind.cpp" />
<ClCompile Include="..\..\..\Editor\Scripting\EditorGUI\EditorGUI.bind.cpp" />
@@ -172,6 +172,7 @@ <ClCompile Include="..\..\..\Runtime\Debug\Log.cpp" />
<ClCompile Include="..\..\..\Runtime\FileSystem\Path.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\OpenGL.cpp" />
+ <ClCompile Include="..\..\..\Runtime\Graphics\Shader.cpp" />
<ClCompile Include="..\..\..\Runtime\Lua\LuaBind\LuaBindCFunctions.cpp" />
<ClCompile Include="..\..\..\Runtime\Lua\LuaBind\LuaBindClass.cpp" />
<ClCompile Include="..\..\..\Runtime\Lua\LuaBind\LuaBindEnum.cpp" />
@@ -208,6 +209,7 @@ <ClInclude Include="..\..\..\Runtime\Debug\Log.h" />
<ClInclude Include="..\..\..\Runtime\FileSystem\Path.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\OpenGL.h" />
+ <ClInclude Include="..\..\..\Runtime\Graphics\Shader.h" />
<ClInclude Include="..\..\..\Runtime\Lua\LuaBind\LuaBind.h" />
<ClInclude Include="..\..\..\Runtime\Lua\LuaBind\LuaBindCFunctions.h" />
<ClInclude Include="..\..\..\Runtime\Lua\LuaBind\LuaBindClass.hpp" />
diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj.filters b/Projects/VisualStudio/Editor/Editor.vcxproj.filters index 30d6087..e60fc65 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj.filters +++ b/Projects/VisualStudio/Editor/Editor.vcxproj.filters @@ -22,9 +22,6 @@ <Filter Include="Runtime\Graphics">
<UniqueIdentifier>{6c7e1979-1cd3-40c7-98e1-f063c325d642}</UniqueIdentifier>
</Filter>
- <Filter Include="Editor\IMGUI">
- <UniqueIdentifier>{47032dd6-dca2-478f-b594-d08c0b22e119}</UniqueIdentifier>
- </Filter>
<Filter Include="Editor\Scripting">
<UniqueIdentifier>{bfc8b148-db9d-403d-96b9-32c946e15402}</UniqueIdentifier>
</Filter>
@@ -85,6 +82,9 @@ <Filter Include="Runtime\Scripting\FileSystem">
<UniqueIdentifier>{350338b7-1176-4edc-9cc7-553a02d69895}</UniqueIdentifier>
</Filter>
+ <Filter Include="Editor\GUI\IMGUI">
+ <UniqueIdentifier>{47032dd6-dca2-478f-b594-d08c0b22e119}</UniqueIdentifier>
+ </Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\Editor\GUI\Dock.cpp">
@@ -150,9 +150,6 @@ <ClCompile Include="..\..\..\Editor\Scripting\EditorGUI\SplitWindow.bind.cpp">
<Filter>Editor\Scripting\EditorGUI</Filter>
</ClCompile>
- <ClCompile Include="..\..\..\Editor\IMGUI\GUIButton.cpp">
- <Filter>Editor\IMGUI</Filter>
- </ClCompile>
<ClCompile Include="..\..\..\Editor\Scripting\IMGUI\GUIButton.bind.cpp">
<Filter>Editor\Scripting\IMGUI</Filter>
</ClCompile>
@@ -228,6 +225,12 @@ <ClCompile Include="..\..\..\Editor\GUI\ContainerWindow.cpp">
<Filter>Editor\GUI</Filter>
</ClCompile>
+ <ClCompile Include="..\..\..\Editor\GUI\IMGUI\GUIButton.cpp">
+ <Filter>Editor\GUI\IMGUI</Filter>
+ </ClCompile>
+ <ClCompile Include="..\..\..\Runtime\Graphics\Shader.cpp">
+ <Filter>Runtime\Graphics</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\Editor\GUI\Dock.h">
@@ -365,6 +368,9 @@ <ClInclude Include="..\..\..\Runtime\FileSystem\Path.h">
<Filter>Runtime\FileSystem</Filter>
</ClInclude>
+ <ClInclude Include="..\..\..\Runtime\Graphics\Shader.h">
+ <Filter>Runtime\Graphics</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\Runtime\Lua\LuaBind\LuaBindClass.inc">
diff --git a/Resources/DefaultContent/Libraries/GameLab/Class.lua b/Resources/DefaultContent/Libraries/GameLab/Class.lua index 346d807..8f74d10 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Class.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Class.lua @@ -1,5 +1,32 @@ --- GameLab Class
-local Class = {}
-GameLab.Class = Class
+local _class = function (className, pkg)
+ local class = {}
+ class._type = { -- 绫诲瀷鍏冩暟鎹紝GameLab鐨勭被鍨嬮兘浼氬寘鍚繖浜
+ mode = "lua",
+ name = className,
+ package = pkg,
+ }
+ class.__index = class
+ class.New = function(...)
+ local instance = {}
+ setmetatable(instance, class)
+ instance:Ctor(...)
+ return instance
+ end
+ return class
+end
+local Class = function(className, pkg)
+ local cls = _class(className, pkg)
+ cls.Extend = function(childName, childPkg)
+ local child = _class(childName, childPkg)
+ if cls then
+ setmetatable(child, cls)
+ child._base = cls
+ end
+ return child
+ end
+ return cls
+end
+GameLab.Class = Class
+return Class
\ No newline at end of file diff --git a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector2.lua b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector2.lua index e69de29..dc961b2 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector2.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector2.lua @@ -0,0 +1,5 @@ +local Vector2 = GameLab.Class("Vector2", "GameLab.Engine.Math")
+
+
+
+return Vector2
\ No newline at end of file diff --git a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector3.lua b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector3.lua index e69de29..f355a65 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector3.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector3.lua @@ -0,0 +1,5 @@ +local Vector3 = GameLab.Class("Vector3", "GameLab.Engine.Math")
+
+
+
+return Vector3
\ No newline at end of file diff --git a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector4.lua b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector4.lua index e69de29..84a114f 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector4.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector4.lua @@ -0,0 +1,45 @@ +local Vector4 = GameLab.Class("Vector4", "GameLab.Engine.Math")
+
+Vector4.Ctor = function (self, x, y, z, w)
+ self.x = x or 0
+ self.y = y or 0
+ self.z = z or 0
+ self.w = w or 0
+end
+
+Vector4.Magnitude = function (self)
+
+end
+
+Vector4.Normalized = function (self)
+
+end
+
+Vector4.SqrMagnitude = function (self)
+
+end
+
+Vector4.Distance = function (self, to)
+
+end
+
+Vector4.Dot = function(self, v)
+
+end
+
+Vector4.Lerp = function(self, v, t)
+
+end
+
+Vector4.Project = function (self, v)
+
+end
+
+Vector4.Scale = function(self, scale)
+
+end
+
+Vector4.one = Vector4.New(1,1,1,1)
+Vector4.zero = Vector4.New(0,0,0,0)
+
+return Vector4
\ No newline at end of file diff --git a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/init.lua b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/init.lua index ade8fca..0cf945e 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/init.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/init.lua @@ -1,12 +1,13 @@ -GameLab.Math = GameLab.Math or {}
+GameLab.Engine.Math = GameLab.Engine.Math or {}
local Debug = GameLab.Debug
-local m = GameLab.Math
+local m = GameLab.Engine.Math
local require = GameLab.require(...)
m.Vector2 = require("Vector2")
m.Vector3 = require("Vector3")
+m.Vector4 = require("Vector4")
m.Matrix4x4 = require("Matrix4x4")
m.Matrix3x3 = require("Matrix3x3")
m.Quaternion = require("Quaternion")
diff --git a/Resources/DefaultContent/Libraries/GameLab/init.lua b/Resources/DefaultContent/Libraries/GameLab/init.lua index abc6e9a..9245165 100644 --- a/Resources/DefaultContent/Libraries/GameLab/init.lua +++ b/Resources/DefaultContent/Libraries/GameLab/init.lua @@ -1,16 +1,16 @@ GameLab = GameLab or {}
+-- methods
GameLab.require = function(packageName)
local _require = function(path)
- return packageName .. "." .. path
+ return require(packageName .. "." .. path)
end
return _require
end
-GameLab.Debug.Log("GameLab")
-
local require = GameLab.require(...)
---GameLab.Class = require("Class")
+-- classes
+GameLab.Class = require("Class")
return GameLab
\ No newline at end of file diff --git a/Resources/Libraries/GameLab/Editor/GUI/EditorWindow.lua b/Resources/Libraries/GameLab/Editor/GUI/EditorWindow.lua new file mode 100644 index 0000000..99d242d --- /dev/null +++ b/Resources/Libraries/GameLab/Editor/GUI/EditorWindow.lua @@ -0,0 +1,10 @@ +local EditorWindow = GameLab.Class("EditorWindow", "GameLab.Editor.GUI")
+
+EditorWindow.Ctor = function(self, ...)
+end
+
+EditorWindow.SetGUIWindow = function(self)
+ print("SetGUIwidow")
+end
+
+return EditorWindow
\ No newline at end of file diff --git a/Resources/Libraries/GameLab/Editor/GUI/init.lua b/Resources/Libraries/GameLab/Editor/GUI/init.lua new file mode 100644 index 0000000..a9c6ac9 --- /dev/null +++ b/Resources/Libraries/GameLab/Editor/GUI/init.lua @@ -0,0 +1,10 @@ +GameLab.Editor.GUI = GameLab.Editor.GUI or {}
+local m = GameLab.Editor.GUI
+
+local require = GameLab.require(...)
+
+m.EditorWindow = require("EditorWindow")
+
+
+
+return m
\ No newline at end of file diff --git a/Resources/Libraries/GameLab/Editor/init.lua b/Resources/Libraries/GameLab/Editor/init.lua index e69de29..99a8091 100644 --- a/Resources/Libraries/GameLab/Editor/init.lua +++ b/Resources/Libraries/GameLab/Editor/init.lua @@ -0,0 +1,2 @@ +
+
diff --git a/Resources/Scripts/EditorApplication.lua b/Resources/Scripts/EditorApplication.lua index 89efb0d..cb4bcb0 100644 --- a/Resources/Scripts/EditorApplication.lua +++ b/Resources/Scripts/EditorApplication.lua @@ -25,6 +25,14 @@ Debug.Log(GameLab.Path.GetRootDirectory()) Debug.Log(inspect{foo=1,2,3,4}) +local wnd = GUI.EditorWindow.New() +Debug.Log(inspect(mainWindow._type)) + +local v = GameLab.Engine.Math.Vector4.New(1,2,3,4) +Debug.Log(inspect(v)) + +local V4 = GameLab.Engine.Math.Vector4.Extend("V4", "GameLab.Engine.Math") + while true do app:PullMessage() diff --git a/Resources/boot.lua b/Resources/boot.lua index 2c8ef45..9fdf4a9 100644 --- a/Resources/boot.lua +++ b/Resources/boot.lua @@ -8,13 +8,22 @@ local engineCLibs = "./DefaultContent/Libraries/?.dll" local editorCLibs = "./Libraries/?.dll"
package.cpath=package.cpath .. ";" .. engineCLibs .. ";" .. editorCLibs
+-- debugging
+require("LuaPanda").start("127.0.0.1",8818)
+
-- redirect
print = GameLab.Debug.Log
--- setup gamelab modules
+-- "macros"
+GAMELAB_PROFILE = true
+GAMELAB_DEBUG = true
+
+-- load gamelab modules
require "GameLab"
require "GameLab.Engine"
require "GameLab.Engine.Math"
require "GameLab.Editor"
+require "GameLab.Editor.GUI"
-require("LuaPanda").start("127.0.0.1",8818)
\ No newline at end of file +-- launch editor
+dofile("./Scripts/EditorApplication.lua")
\ No newline at end of file diff --git a/Runtime/Graphics/Shader.cpp b/Runtime/Graphics/Shader.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Runtime/Graphics/Shader.cpp diff --git a/Runtime/Graphics/Shader.h b/Runtime/Graphics/Shader.h new file mode 100644 index 0000000..b22a05f --- /dev/null +++ b/Runtime/Graphics/Shader.h @@ -0,0 +1,6 @@ +#pragma once
+
+class Shader
+{
+
+};
diff --git a/Runtime/Lua/LuaBind/LuaBind.h b/Runtime/Lua/LuaBind/LuaBind.h index 1494b5c..e12c4b9 100644 --- a/Runtime/Lua/LuaBind/LuaBind.h +++ b/Runtime/Lua/LuaBind/LuaBind.h @@ -1,10 +1,6 @@ #ifndef __LUA_BIND_H__ #define __LUA_BIND_H__ -// -// (port) library 用于给lua注册类和函数 -// - #include "LuaBindState.h" #include "LuaBindVM.h" #include "LuaBindRef.h" diff --git a/Runtime/Lua/LuaBind/LuaBindState.cpp b/Runtime/Lua/LuaBind/LuaBindState.cpp index fa2952f..ecb8196 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.cpp +++ b/Runtime/Lua/LuaBind/LuaBindState.cpp @@ -8,7 +8,10 @@ namespace LuaBind { - + OnRegisterClassHandler onRegisterFactoryClass; + + std::string g_NameSpace = ""; + State::State(lua_State* state) : mState(state) { @@ -42,6 +45,8 @@ namespace LuaBind void State::PushGlobalNamespace() { + g_NameSpace.clear(); + #if false int top = GetTop(); @@ -71,6 +76,8 @@ namespace LuaBind { assert(IsNamespace(-1)); + g_NameSpace += (g_NameSpace.length() > 0 ? "." : "") + (std::string)name; + int top = GetTop(); lua_getfield(mState, -1, name); diff --git a/Runtime/Lua/LuaBind/LuaBindState.h b/Runtime/Lua/LuaBind/LuaBindState.h index 1f236df..ec2a350 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.h +++ b/Runtime/Lua/LuaBind/LuaBindState.h @@ -9,13 +9,16 @@ namespace LuaBind { - class VM; class Enum; class StrongRef; class WeakRef; typedef void (*ErrorHandler) (cc8 * msg); + typedef void(*OnRegisterClassHandler)(State&, int clsIdx, std::string clsName, std::string pkgName); + + extern OnRegisterClassHandler onRegisterFactoryClass; + extern std::string g_NameSpace; // 对lua_State的代理,除了保存一个lua_State的引用不保存其他内容。一个实例的metatable如下: // class table @@ -264,6 +267,182 @@ namespace LuaBind }; + // 注册工厂,注册class table,以type name为键设置在名称空间上。在注册阶段不会设置元表,等到New方法调用的时候才会。 + template<class TYPE> + void State::RegisterFactory() + { + cc8* type = TYPE::GetFactoryName(); + + lua_State* L = mState; + State& state = *this; + + int top = lua_gettop(L); // namespace table + assert(lua_istable(L, top)); + + // class table + lua_newtable(L); + int clsIdx = lua_gettop(L); + TYPE::RegisterClassShared(state); + TYPE::RegisterFactoryClass(state); + TYPE::RegisterClass(state); + + // 自定义流程 + if (onRegisterFactoryClass) + onRegisterFactoryClass(state, clsIdx, type, g_NameSpace); + + // 检测TYPE里面是否没有注册必须的方法 +#define _assertmethod(I, NAME) \ + GetField(I, NAME); \ + assert(IsType(-1, LUA_TFUNCTION)); \ + Pop(); + + //_assertmethod(-1, "New"); +#undef _assertmethod + +#if LUA_BIND_ENABLE_NATIVE_EXTEND + // .Extend() + lua_pushvalue(state, -1); // class table + lua_pushcclosure(state, TYPE::_ExtendFactory, 1); + lua_setfield(state, -2, "Extend"); +#endif + + // class["__index"] = class + lua_pushvalue(state, -1); // class table + lua_setfield(state, -2, "__index"); + + TYPE::SetClassTableRef(state, -1); + + SetField(top, type); + + // reset top + lua_settop(L, top); + + // 后处理 + TYPE::RegisterPostprocess(state); + } + + // 注册单例 + template<typename TYPE> + void State::RegisterSingleton() + { + lua_State* L = mState; + State& state = *this; + + int top = lua_gettop(L); // namespace table + assert(lua_istable(L, top)); + + // class table. + lua_newtable(L); + TYPE::RegisterClassShared(state); + TYPE::RegisterSingletonClass(state); + TYPE::RegisterClass(state); + + TYPE::SetClassTableRef(state, -1); + + lua_pushvalue(state, -1); + lua_setfield(state, -2, "__index"); + +#if LUA_BIND_ENABLE_NATIVE_EXTEND + // .Extend() + lua_pushvalue(state, -1); // class table + lua_pushcclosure(state, TYPE::_ExtendSingleton, 1); + lua_setfield(state, -2, "Extend"); +#endif + + cc8* type = TYPE::GetSingletonName(); + SetField(top, type); + + // reset top + lua_settop(L, top); + + // 后处理 + TYPE::RegisterPostprocess(state); + } + + template<typename TYPE> + void State::SetField(int idx, cc8* key, TYPE value) + { + if (IsTableOrUserdata(idx)) + { + idx = AbsIndex(idx); + this->Push(value); + lua_setfield(mState, idx, key); + } + } + + template<typename TYPE> + void State::SetFieldByIndex(int idx, int key, TYPE value) + { + if (IsTableOrUserdata(idx)) + { + idx = AbsIndex(idx); + this->Push(value); + lua_rawseti(mState, idx, key); + } + } + + template<typename TYPE> + TYPE State::GetField(int idx, cc8* key, TYPE value) + { + GetField(idx, key); + TYPE result = GetValue < TYPE >(-1, value); + this->Pop(); + + return result; + } + + template<typename TYPE> + TYPE State::GetField(int idx, int key, TYPE value) + { + GetField(idx, key); + TYPE result = GetValue < TYPE >(-1, value); + Pop(); + + return result; + } + + template<typename TYPE> + TYPE* State::GetUserdata(int idx) + { + void* p = nullptr; + + if (IsType(idx, LUA_TUSERDATA)) + { + p = *(void**)lua_touserdata(mState, idx); + } + + return static_cast<TYPE*>(p); + } + + template<typename TYPE> + TYPE* State::CheckUserdata(int idx) + { + if (IsType(idx, LUA_TUSERDATA)) + { + if (lua_getmetatable(mState, idx)) // ref table + { + if (lua_getmetatable(mState, -1)) // member table + { + if (lua_getmetatable(mState, -1)) // class table + { + TYPE::PushClassTable(*this); // target class table + if (lua_rawequal(mState, -1, -2)) + { + Pop(4); // ref\member\class\target class + TYPE* udata = GetUserdata<TYPE>(idx); + return udata; // userdata + } + Pop(2); // target class table\class table + } + Pop(1); // member table + } + Pop(1); // ref table + } + } + luaL_typerror(mState, idx, TYPE::GetClassName()); + return nullptr; + } + } #endif
\ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindState.inc b/Runtime/Lua/LuaBind/LuaBindState.inc index 4e7090d..3c5990e 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.inc +++ b/Runtime/Lua/LuaBind/LuaBindState.inc @@ -1,180 +1,4 @@ namespace LuaBind { - // - // 注册工厂,注册class table,以type name为键设置在名称空间上。在注册阶段不会设置元表,等到New方法调用的时候才会。 - // - template<class TYPE> - void State::RegisterFactory() - { - cc8* type = TYPE::GetFactoryName(); - - lua_State* L = mState; - State& state = *this; - - int top = lua_gettop(L); // namespace table - assert(lua_istable(L, top)); - - // class table - lua_newtable(L); - TYPE::RegisterClassShared(state); - TYPE::RegisterFactoryClass(state); - TYPE::RegisterClass(state); - - // 检测TYPE里面是否没有注册必须的方法 -#define _assertmethod(I, NAME) \ - GetField(I, NAME); \ - assert(IsType(-1, LUA_TFUNCTION)); \ - Pop(); - - //_assertmethod(-1, "New"); - -#undef _assertmethod - -#if LUA_BIND_ENABLE_NATIVE_EXTEND - // .Extend() - lua_pushvalue(state, -1); // class table - lua_pushcclosure(state, TYPE::_ExtendFactory, 1); - lua_setfield(state, -2, "Extend"); -#endif - - // class["__index"] = class - lua_pushvalue(state, -1); // class table - lua_setfield(state, -2, "__index"); - - TYPE::SetClassTableRef(state, -1); - - SetField(top, type); - - // reset top - lua_settop(L, top); - - // 后处理 - TYPE::RegisterPostprocess(state); - } - - // - // Singleton - // - template<typename TYPE> - void State::RegisterSingleton() - { - lua_State* L = mState; - State& state = *this; - - int top = lua_gettop(L); // namespace table - assert(lua_istable(L, top)); - - // class table. - lua_newtable(L); - TYPE::RegisterClassShared(state); - TYPE::RegisterSingletonClass(state); - TYPE::RegisterClass(state); - - TYPE::SetClassTableRef(state, -1); - - lua_pushvalue(state, -1); - lua_setfield(state, -2, "__index"); - -#if LUA_BIND_ENABLE_NATIVE_EXTEND - // .Extend() - lua_pushvalue(state, -1); // class table - lua_pushcclosure(state, TYPE::_ExtendSingleton, 1); - lua_setfield(state, -2, "Extend"); -#endif - - cc8* type = TYPE::GetSingletonName(); - SetField(top, type); - - // reset top - lua_settop(L, top); - - // 后处理 - TYPE::RegisterPostprocess(state); - } - - template<typename TYPE> - void State::SetField(int idx, cc8* key, TYPE value) - { - if (IsTableOrUserdata(idx)) - { - idx = AbsIndex(idx); - this->Push(value); - lua_setfield(mState, idx, key); - } - } - - template<typename TYPE> - void State::SetFieldByIndex(int idx, int key, TYPE value) - { - if (IsTableOrUserdata(idx)) - { - idx = AbsIndex(idx); - this->Push(value); - lua_rawseti(mState, idx, key); - } - } - - template<typename TYPE> - TYPE State::GetField(int idx, cc8* key, TYPE value) - { - GetField(idx, key); - TYPE result = GetValue < TYPE >(-1, value); - this->Pop(); - - return result; - } - - template<typename TYPE> - TYPE State::GetField(int idx, int key, TYPE value) - { - GetField(idx, key); - TYPE result = GetValue < TYPE >(-1, value); - Pop(); - - return result; - } - - template<typename TYPE> - TYPE* State::GetUserdata(int idx) - { - void* p = nullptr; - - if (IsType(idx, LUA_TUSERDATA)) - { - p = *(void**)lua_touserdata(mState, idx); - } - - return static_cast<TYPE*>(p); - } - - template<typename TYPE> - TYPE* State::CheckUserdata(int idx) - { - if (IsType(idx, LUA_TUSERDATA)) - { - if (lua_getmetatable(mState, idx)) // ref table - { - if (lua_getmetatable(mState, -1)) // member table - { - if (lua_getmetatable(mState, -1)) // class table - { - TYPE::PushClassTable(*this); // target class table - if (lua_rawequal(mState, -1, -2)) - { - Pop(4); // ref\member\class\target class - TYPE* udata = GetUserdata<TYPE>(idx); - return udata; // userdata - } - Pop(2); // target class table\class table - } - Pop(1); // member table - } - Pop(1); // ref table - } - } - luaL_typerror(mState, idx, TYPE::GetClassName()); - return nullptr; - } - }
\ No newline at end of file |