From ee70dd29cba2d817164f6425c144c6a06d30b76d Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Sun, 25 Jan 2026 00:08:41 +0000 Subject: [PATCH 01/23] Current work on Lua and Scripts unfinished because i have to get back to this later --- .gitmodules | 3 + Client/App/App.vcproj | 2706 +++++++++-------- Client/App/include/lua/LuaBridge.h | 46 + Client/App/include/script/LuaAtomicClasses.h | 101 + Client/App/include/script/LuaInstanceBridge.h | 29 + Client/App/include/script/LuaMemory.h | 20 + Client/App/include/script/LuaSignalBridge.h | 23 + Client/App/include/script/Script.h | 71 +- Client/App/include/script/ScriptContext.h | 130 + Client/App/include/script/ThreadRef.h | 66 + Client/App/include/util/ContentProvider.h | 12 +- Client/App/include/util/Debug.h | 14 +- Client/App/include/v8tree/Instance.h | 2 +- Client/App/lua-5.1.1 | 1 + Client/App/script/LuaAtomicClasses.cpp | 1 + Client/App/script/LuaInstanceBridge.cpp | 17 + Client/App/script/LuaMemory.cpp | 56 + Client/App/script/LuaSignalBridge.cpp | 8 + Client/App/script/Script.cpp | 128 + Client/App/script/ScriptContext.cpp | 109 + Client/App/script/ThreadRef.cpp | 8 + Client/App/util/ContentProvider.cpp | 5 + Client/App/util/Debug.cpp | 24 +- Client/App/v8tree/Instance.cpp | 4 + 24 files changed, 2232 insertions(+), 1352 deletions(-) create mode 100644 Client/App/include/lua/LuaBridge.h create mode 100644 Client/App/include/script/LuaAtomicClasses.h create mode 100644 Client/App/include/script/LuaInstanceBridge.h create mode 100644 Client/App/include/script/LuaMemory.h create mode 100644 Client/App/include/script/LuaSignalBridge.h create mode 100644 Client/App/include/script/ScriptContext.h create mode 100644 Client/App/include/script/ThreadRef.h create mode 160000 Client/App/lua-5.1.1 create mode 100644 Client/App/script/LuaAtomicClasses.cpp create mode 100644 Client/App/script/LuaInstanceBridge.cpp create mode 100644 Client/App/script/LuaMemory.cpp create mode 100644 Client/App/script/LuaSignalBridge.cpp create mode 100644 Client/App/script/Script.cpp create mode 100644 Client/App/script/ScriptContext.cpp create mode 100644 Client/App/script/ThreadRef.cpp diff --git a/.gitmodules b/.gitmodules index 57bf404c..7d18187a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "Client/Rendering/g3d"] path = Client/Rendering/g3d url = https://github.com/RBLXDecomp/g3d.git +[submodule "Client/App/lua-5.1.1"] + path = Client/App/lua-5.1.1 + url = https://github.com/pizzaboxer/lua.git diff --git a/Client/App/App.vcproj b/Client/App/App.vcproj index 69a3788c..8f8c3653 100644 --- a/Client/App/App.vcproj +++ b/Client/App/App.vcproj @@ -1,1321 +1,1385 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Client/App/include/lua/LuaBridge.h b/Client/App/include/lua/LuaBridge.h new file mode 100644 index 00000000..00933664 --- /dev/null +++ b/Client/App/include/lua/LuaBridge.h @@ -0,0 +1,46 @@ +#pragma once +#include "lua.h" +#include "boost/shared_ptr.hpp" + +namespace RBX +{ + namespace Lua + { + template + class Bridge + { + protected: + static const char* className; + + public: + static T* pushNewObject(lua_State*); + static T& getObject(lua_State*, size_t); + static void registerClass(lua_State*); + + protected: + static int on_index(lua_State*); + static int on_index(const T&, const char*, lua_State*); + static int on_newindex(lua_State*); + static void on_newindex(T&, const char*, lua_State*); + static int on_tostring(lua_State*); + static int on_tostring(const T&, lua_State*); + static int on_gc(lua_State*); + static int on_eq(lua_State*); + }; + + template + class SharedPtrBridge : protected Bridge, false> + { + public: + static void registerClass(lua_State*); + static void registerClassLibrary(lua_State* L) + { + lua_pushlightuserdata(L, push); + newweaktable(L, "v"); + lua_rawset(L, LUA_REGISTRYINDEX); + } + static void push(lua_State*, boost::shared_ptr); + static boost::shared_ptr getPtr(lua_State*, size_t); + }; + } +} diff --git a/Client/App/include/script/LuaAtomicClasses.h b/Client/App/include/script/LuaAtomicClasses.h new file mode 100644 index 00000000..4662e0d6 --- /dev/null +++ b/Client/App/include/script/LuaAtomicClasses.h @@ -0,0 +1,101 @@ +#pragma once +#include "v8datamodel/BrickColor.h" +#include "lua/LuaBridge.h" +#include "G3D/CoordinateFrame.h" +#include "G3D/Vector3.h" +#include "G3D/Color3.h" +#include "lauxlib.h" + +namespace RBX +{ + namespace Lua + { + class CoordinateFrameBridge : public Bridge + { + private: + static const luaL_Reg classLibrary[0]; + + public: + static void registerClassLibrary(lua_State* L) + { + luaL_register(L, className, classLibrary); + lua_pop(L, 1); + } + static void pushCoordinateFrame(lua_State*, G3D::CoordinateFrame); + + private: + static int newCoordinateFrame(lua_State*); + static int fromEulerAnglesXYZ(lua_State*); + static int fromAxisAngle(lua_State*); + static int on_add(lua_State*); + static int on_sub(lua_State*); + static int on_mul(lua_State*); + static int on_inverse(lua_State*); + static int on_toWorldSpace(lua_State*); + static int on_toObjectSpace(lua_State*); + static int on_pointToWorldSpace(lua_State*); + static int on_pointToObjectSpace(lua_State*); + static int on_vectorToWorldSpace(lua_State*); + static int on_vectorToObjectSpace(lua_State*); + static int on_toEulerAnglesXYZ(lua_State*); + static int on_components(lua_State*); + }; + + class Vector3Bridge : public Bridge + { + private: + static const luaL_Reg classLibrary[0]; + + public: + static void registerClassLibrary(lua_State* L) + { + luaL_register(L, className, classLibrary); + lua_pop(L, 1); + } + static void pushVector3(lua_State*, G3D::Vector3); + + private: + static int newVector3(lua_State*); + static int on_add(lua_State*); + static int on_sub(lua_State*); + static int on_mul(lua_State*); + static int on_div(lua_State*); + static int on_unm(lua_State*); + }; + + + class Color3Bridge : public Bridge + { + private: + static const luaL_Reg classLibrary[0]; + + public: + static void registerClassLibrary(lua_State* L) + { + luaL_register(L, className, classLibrary); + lua_pop(L, 1); + } + static void pushColor3(lua_State*, G3D::Color3); + + private: + static int newColor3(lua_State*); + }; + + class BrickColorBridge : public Bridge + { + private: + static const luaL_Reg classLibrary[0]; + + public: + static void registerClassLibrary(lua_State* L) + { + luaL_register(L, className, classLibrary); + lua_pop(L, 1); + } + + private: + static int newBrickColor(lua_State*); + static int randomBrickColor(lua_State*); + }; + } +} diff --git a/Client/App/include/script/LuaInstanceBridge.h b/Client/App/include/script/LuaInstanceBridge.h new file mode 100644 index 00000000..f2871d0f --- /dev/null +++ b/Client/App/include/script/LuaInstanceBridge.h @@ -0,0 +1,29 @@ +#pragma once +#include "lua/LuaBridge.h" +#include "reflection/reflection.h" +#include "lua.h" +#include "lauxlib.h" + +namespace RBX +{ + namespace Lua + { + void newweaktable(lua_State* L, const char* mode); + + class ObjectBridge : public SharedPtrBridge + { + private: + static const luaL_Reg classLibrary[0]; + + public: + static int callMemberFunction(lua_State*); + static void registerInstanceClassLibrary(lua_State* L) + { + luaL_register(L, "Instance", classLibrary); + lua_pop(L, 1); + } + static int newInstance(lua_State*); + static boost::shared_ptr getInstance(lua_State*, size_t); + }; + } +} diff --git a/Client/App/include/script/LuaMemory.h b/Client/App/include/script/LuaMemory.h new file mode 100644 index 00000000..b9bbc742 --- /dev/null +++ b/Client/App/include/script/LuaMemory.h @@ -0,0 +1,20 @@ +#pragma once + +class LuaAllocator +{ +private: + size_t heapSize; + size_t heapCount; + size_t maxHeapSize; + size_t maxHeapCount; + +public: + LuaAllocator(); + void clearHeapMax(); + void getHeapStats(size_t&, size_t&) const; + void getHeapStats(size_t& heapSize, size_t& heapCount, size_t& maxHeapSize, size_t& maxHeapCount) const; + void* alloc(void* ptr, size_t osize, size_t nsize); + +public: + static void* alloc(void* ud, void* ptr, size_t osize, size_t nsize); +}; diff --git a/Client/App/include/script/LuaSignalBridge.h b/Client/App/include/script/LuaSignalBridge.h new file mode 100644 index 00000000..1d3b5e80 --- /dev/null +++ b/Client/App/include/script/LuaSignalBridge.h @@ -0,0 +1,23 @@ +#pragma once +#include "lua/LuaBridge.h" +#include "reflection/reflection.h" +#include "boost/signals.hpp" + +namespace RBX +{ + namespace Lua + { + class SignalBridge : public SharedPtrBridge + { + public: + static int connect(lua_State*); + static int wait(lua_State*); + }; + + class SignalConnectionBridge : public Bridge + { + private: + static int disconnect(lua_State*); + }; + } +} diff --git a/Client/App/include/script/Script.h b/Client/App/include/script/Script.h index 298f1e49..0ac51181 100644 --- a/Client/App/include/script/Script.h +++ b/Client/App/include/script/Script.h @@ -1,29 +1,76 @@ #pragma once +#include "script/ScriptContext.h" +#include "v8tree/Instance.h" #include "util/Debug.h" +#include "boost/shared_ptr.hpp" namespace RBX { - class Script; class ScriptContext; - class IScriptOwner + extern const char* sScript; + class Script : public DescribedCreatable { - protected: - virtual IScriptOwner* scriptShouldRun(Script* script) = 0; - virtual void runScript(Script* script, ScriptContext* context) + private: + boost::shared_ptr embeddedSource; + ContentId scriptId; + bool disabled; + class IScriptOwner* owner; + + public: + static Reflection::BoundProp prop_Disabled; + static const Reflection::PropDescriptor prop_EmbeddedSourceCode; + static const Reflection::PropDescriptor prop_SourceCodeId; + + public: + Script(const Script&); + Script(); + virtual ~Script(); + + bool isDisabled() const { - RBXASSERT(0); + return disabled; } - virtual void releaseScript(Script* script) + + virtual bool askSetParent(const Instance*) const; + bool isCodeEmbedded() const; + boost::shared_ptr requestCode(); + + void setEmbeddedCode(const std::string& value); + const std::string& getEmbeddedCode() const { - RBXASSERT(0); + return *embeddedSource.get(); } - public: - //IScriptOwner(const IScriptOwner&); - IScriptOwner() + + const ContentId& getScriptId() const { + return scriptId; } + void setScriptId(const ContentId& value); + + protected: + virtual void onServiceProvider(const ServiceProvider* oldProvider, const ServiceProvider* newProvider); + virtual void onAncestorChanged(const AncestorChanged& event); + }; + + extern const char* sLocalScript; + class LocalScript : public DescribedCreatable + { + public: + LocalScript(); + virtual ~LocalScript(); + }; + + class IScriptOwner + { + friend class Script; + + protected: + virtual IScriptOwner* scriptShouldRun(Script* script); + virtual void runScript(Script* script, ScriptContext* context); + virtual void releaseScript(Script* script); + public: - //IScriptOwner& operator=(const IScriptOwner&); + IScriptOwner(); }; } diff --git a/Client/App/include/script/ScriptContext.h b/Client/App/include/script/ScriptContext.h new file mode 100644 index 00000000..6989ec5f --- /dev/null +++ b/Client/App/include/script/ScriptContext.h @@ -0,0 +1,130 @@ +#pragma once +#include "script/Script.h" +#include "script/LuaMemory.h" +#include "script/ThreadRef.h" +#include "v8tree/Instance.h" +#include "v8tree/Service.h" +#include "util/Events.h" +#include "util/RunStateOwner.h" +#include "boost/shared_ptr.hpp" +#include "boost/date_time.hpp" +#include "lua.h" + +namespace RBX +{ + class Script; // this is intentional, don't remove this + + // TODO: remove forward declarations when implemented + namespace Lua + { + class YieldingThreads{}; + class FunctionRef{}; + } + + namespace Security + { + enum Identities{}; + } + + namespace Stats + { + class Item{}; + } + + extern const char *sScriptContext; + class ScriptContext : public DescribedCreatable, + public Service, + public Listener, + public Listener + { + public: + enum Result + { + Success, + Yield, + Error + }; + + // TODO: come back to this when luasoft gets his stuff done + class ScriptImpersonator// : public Security::Impersonator + { + public: + ScriptImpersonator(lua_State*); + ~ScriptImpersonator(); + }; + + private: + lua_State* globalState; + Lua::ThreadRef commandLineSandbox; + std::set, std::allocator> scripts; + boost::posix_time::ptime nextPendingScripts; + std::vector, std::allocator>> pendingScripts; + boost::shared_ptr runService; + boost::scoped_ptr yieldEvent; + bool scriptsDisabled; + boost::shared_ptr statsItem; + + public: + boost::scoped_ptr allocator; + static Reflection::BoundProp propScriptsDisabled; + + public: + ScriptContext(const ScriptContext&); + ScriptContext(); + virtual ~ScriptContext(); + + // TODO: typedef these long repeated types somewhere? + std::auto_ptr>> call(const Lua::FunctionRef&, const std::vector>&); + void call(const Lua::FunctionRef&); + void call(const Lua::FunctionRef&, boost::function1>, boost::function2>); + std::auto_ptr>> executeInNewThread(Security::Identities, const char*, const char*, const std::vector>&); + void executeInNewThread(Security::Identities, const char*, const char*); + void executeInNewThread(Security::Identities, const char*, const char*, boost::function1>, boost::function2>); + ScriptContext::Result resume(lua_State*, int); + void resume(lua_State*, boost::function1>, boost::function2>); + void gc(); + size_t getThreadCount() const; + bool hasState() const; + lua_State* state(); + virtual XmlElement* write(); + + protected: + virtual void onServiceProvider(const ServiceProvider*, const ServiceProvider*); + virtual void onEvent(const RunService*, Heartbeat); + virtual void onEvent(const RunService*, RunTransition); + + private: + void onChangedScriptEnabled(const Reflection::PropertyDescriptor&); + + public: + void closeState(); + void addScript(Script*); + void removeScript(Script*); + + private: + void disassociateState(Script*); + void openState(); + void startScript(boost::shared_ptr