public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { this.lua_extructor = lua_extructor; try { lua.CreateTable(); lua.CreateTable(); lua.PushManagedFunction(MetaToStringDelegate); lua.SetField(-2, "__tostring"); lua.SetMetaTable(-2); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "tostring"); lua.Push(-3); lua.MCall(1, 1); string get_val = lua.GetString(-1); lua.Pop(2); if (get_val != to_str_msg) { throw new CreateMetaTableException("Recieved string is incorrect"); } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); this.lua_extructor = lua_extructor; try { // Create metatable lua.CreateTable(); lua.PushManagedFunction(this.eq_func); lua.SetField(-2, "__eq"); // Create first table to compare lua.CreateTable(); lua.PushNumber(1); lua.SetField(-2, "A"); lua.Push(-2); lua.SetMetaTable(-2); // Create second table to compare lua.CreateTable(); lua.PushNumber(2); lua.SetField(-2, "A"); lua.Push(-3); lua.SetMetaTable(-2); // Get compare results bool equal_result = lua.Equal(-1, -2); bool raw_equal_result = lua.RawEqual(-1, -2); lua.Pop(3); if (!equal_result) { throw new EqualityTestException("ILua.Equal returned false but must return true"); } if (raw_equal_result) { throw new EqualityTestException("ILua.RawEqual returned true but must return false"); } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
internal GlobalContext(ILua lua) { this.lua = lua; lua.GetField(-10002, "SERVER"); isServerSide = lua.GetBool(-1); module_contexts = new Dictionary <string, Tuple <GmodNetModuleAssemblyLoadContext, List <GCHandle> > >(); int managed_func_type_id = lua.CreateMetaTable("ManagedFunction"); unsafe { lua.PushCFunction(&ManagedFunctionMetaMethods.ManagedDelegateGC); } lua.SetField(-2, "__gc"); lua.Pop(1); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.PushNumber(managed_func_type_id); lua.SetField(-2, ManagedFunctionMetaMethods.ManagedFunctionIdField); lua.Pop(1); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.CreateTable(); lua.PushManagedFunction(LoadModule); lua.SetField(-2, "load"); lua.PushManagedFunction(UnloadModule); lua.SetField(-2, "unload"); lua.SetField(-2, "dotnet"); lua.Pop(1); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { string key = Guid.NewGuid().ToString(); string value = Guid.NewGuid().ToString(); lua.CreateTable(); lua.PushString(value); lua.SetField(-2, key); lua.PushNil(); lua.Next(-2); string received_key = lua.GetString(-2); string received_value = lua.GetString(-1); lua.Pop(3); if (received_key != key || received_value != value) { throw new NextTestException("Received key value pair is invalid"); } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public void PushToLua(ILua lua) { lua.CreateTable(); lua.PushString(Name); lua.SetField(-2, "Name"); lua.PushVector(NodeSize); lua.SetField(-2, "NodeSize"); }
public void PushToLua(ILua lua) { lua.CreateTable(); lua.PushVector(Coordinates); lua.SetField(-2, "Coordinates"); lua.PushVector(FootPos); lua.SetField(-2, "FootPos"); }
public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) { // Creating global table lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.CreateTable(); lua.SetField(-2, "CNode"); lua.Pop(); if (is_serverside) { PathManager.Load(lua); GridManager.Load(lua); } }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { string table_name = Guid.NewGuid().ToString(); string field_name = Guid.NewGuid().ToString(); Random rand = new Random(); double rand_num = rand.NextDouble(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.CreateTable(); lua.PushNumber(rand_num); lua.SetField(-2, field_name); lua.SetField(-2, table_name); lua.Pop(1); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, table_name); if (lua.GetType(-1) != (int)TYPES.TABLE) { throw new CreateTableException("Type check failed"); } lua.GetField(-1, field_name); double get_num = lua.GetNumber(-1); if (get_num != rand_num) { throw new CreateTableException("Wrong number recieved"); } lua.Pop(3); taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { this.lua_extructor = lua_extructor; // Create new type this.type_id = lua.CreateMetaTable("SetTableAndRawSetTestType"); lua.PushManagedFunction(this.newIndexImpl); lua.SetField(-2, "__newindex"); lua.Pop(1); // Create test table lua.CreateTable(); lua.PushString(random1); lua.SetField(-2, "Val"); lua.PushMetaTable(this.type_id); lua.SetMetaTable(-2); // Test SetTable lua.PushString("Val"); lua.PushString(random1 + random1); lua.SetTable(-3); lua.GetField(-1, "Val"); string received_string = lua.GetString(-1); lua.Pop(1); if (received_string != random1 + random1) { throw new Exception("SetTable didn't set a value for an existing key"); } lua.PushString("ArbitraryKey"); lua.PushString("ArbitraryString"); lua.SetTable(-3); lua.GetField(-1, "ArbitraryKey"); int received_type = lua.GetType(-1); lua.Pop(1); if (received_type != (int)TYPES.NIL) { throw new Exception("SetTable ignored overriden __newindex"); } lua.PushString("Val2"); lua.PushString(random2); lua.RawSet(-3); lua.GetField(-1, "Val2"); string received_string2 = lua.GetString(-1); lua.Pop(1); if (received_string2 != random2) { throw new Exception("RawSet didn't set a value to a key"); } lua.Pop(1); lua.PushMetaTable(type_id); lua.PushNil(); lua.SetField(-2, "__newindex"); lua.Pop(1); taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) { // create usertype identifier UserType_Id = lua.CreateMetaTable("Example_UserType"); // set meta-methods // see http://www.tutorialspoint.com/lua/lua_metatables.htm for all meta-methods // also checkout https://www.lua.org/pil/13.html for explanation about meta-methods/tables // __index method // __index is called when you trying to index usertype ex: // MyCSString.Length // MyCSString:ToCharArray() // FindMetaTable("Example_UserType").ToCharArray(MyCSString) lua.PushManagedFunction((lua) => { IntPtr ptr = lua.GetUserType(1, UserType_Id); if (ptr != IntPtr.Zero) { string indexingname = lua.GetString(2); switch (indexingname) { // pushing simple number case "Length": GCHandle gCHandle = GCHandle.FromIntPtr(ptr); string csString = (string)gCHandle.Target; lua.PushNumber(csString.Length); break; // pushing function case "ToCharArray": lua.PushManagedFunction((lua) => { IntPtr ptr = lua.GetUserType(1, UserType_Id); GCHandle gCHandle = GCHandle.FromIntPtr(ptr); string csString = (string)gCHandle.Target; lua.CreateTable(); char[] charArray = csString.ToCharArray(); for (int i = 0; i < charArray.Length; i++) { lua.PushNumber(i); lua.PushString(charArray[i].ToString()); lua.SetTable(-3); } return(1); }); break; case "Clone": lua.PushManagedFunction((lua) => { IntPtr ptr1 = lua.GetUserType(1, UserType_Id); GCHandle gCHandle1 = GCHandle.FromIntPtr(ptr1); string csString1 = (string)gCHandle1.Target; string csString2 = (string)csString1.Clone(); GCHandle gCHandle2 = GCHandle.Alloc(csString2, GCHandleType.Weak); IntPtr ptr2 = GCHandle.ToIntPtr(gCHandle2); lua.PushUserType(ptr2, UserType_Id); return(1); }); break; case "IndexOf": lua.PushManagedFunction((lua) => { IntPtr ptr = lua.GetUserType(1, UserType_Id); GCHandle gCHandle = GCHandle.FromIntPtr(ptr); string csString = (string)gCHandle.Target; string toFind; if (lua.IsType(2, TYPES.STRING)) { toFind = lua.GetString(2); } else if (lua.IsType(2, UserType_Id)) { IntPtr ptr2 = lua.GetUserType(2, UserType_Id); GCHandle gCHandle2 = GCHandle.FromIntPtr(ptr2); toFind = (string)gCHandle2.Target; } else { return(0); } int indexOf = csString.IndexOf(toFind); lua.PushNumber(indexOf); return(1); }); break; case "Contains": lua.PushManagedFunction((lua) => { IntPtr ptr = lua.GetUserType(1, UserType_Id); GCHandle gCHandle = GCHandle.FromIntPtr(ptr); string csString = (string)gCHandle.Target; string toFind; if (lua.IsType(2, TYPES.STRING)) { toFind = lua.GetString(2); } else if (lua.IsType(2, UserType_Id)) { IntPtr ptr2 = lua.GetUserType(2, UserType_Id); GCHandle gCHandle2 = GCHandle.FromIntPtr(ptr2); toFind = (string)gCHandle2.Target; } else { return(0); } bool contains = csString.Contains(toFind); lua.PushBool(contains); return(1); }); break; default: lua.PushNil(); break; } } else { Console.WriteLine("nil passed to __index"); lua.PushNil(); } // function will return 1 result return(1); }); lua.SetField(-2, "__index"); lua.PushManagedFunction((lua) => { // 1 - stands for 1st passed to function argument IntPtr ptr = lua.GetUserType(1, UserType_Id); if (ptr != IntPtr.Zero) { GCHandle gCHandle = GCHandle.FromIntPtr(ptr); string csString = (string)gCHandle.Target; lua.PushString(csString); } else { Console.WriteLine("nil passed to __tostring"); lua.PushNil(); } return(1); }); lua.SetField(-2, "__tostring"); // equal (==) method lua.PushManagedFunction((lua) => { IntPtr ptr1 = lua.GetUserType(1, UserType_Id); IntPtr ptr2 = lua.GetUserType(2, UserType_Id); // if we have same pointers, then objects are same if (ptr1 == ptr2) { lua.PushBool(true); } // check if both pointers not zero else if (ptr1 != IntPtr.Zero && ptr2 != IntPtr.Zero) { GCHandle gCHandle1 = GCHandle.FromIntPtr(ptr1); GCHandle gCHandle2 = GCHandle.FromIntPtr(ptr2); string csString1 = (string)gCHandle1.Target; string csString2 = (string)gCHandle2.Target; lua.PushBool(csString1 == csString2); } // some of pointers is Zero, we'll not compare them else { lua.PushBool(false); } return(1); }); lua.SetField(-2, "__eq"); // Dispose() in lua lua.PushManagedFunction((lua) => { // 1 - stands for 1st passed to function argument IntPtr ptr = lua.GetUserType(1, UserType_Id); if (ptr != IntPtr.Zero) { GCHandle gCHandle = GCHandle.FromIntPtr(ptr); string csString = (string)gCHandle.Target; Console.WriteLine($"csString ({csString}) is garbage collected"); gCHandle.Free(); } else { Console.WriteLine("nil passed to __gc"); } return(0); }); lua.SetField(-2, "__gc"); lua.Pop(); // now we need to somehow create it lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.PushManagedFunction((lua) => { string csString = lua.GetString(1); GCHandle gCHandle = GCHandle.Alloc(csString, GCHandleType.Weak); IntPtr ptr = GCHandle.ToIntPtr(gCHandle); lua.PushUserType(ptr, UserType_Id); return(1); }); lua.SetField(-2, "CreateCSString"); lua.Pop(); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { this.lua_extructor = lua_extructor; this.NewTypeId = lua.CreateMetaTable("TestType1"); lua.PushManagedFunction(this.ToStringImpl); lua.SetField(-2, "__tostring"); lua.Pop(1); //Create new table to test newly created metatable lua.CreateTable(); lua.PushMetaTable(this.NewTypeId); lua.SetMetaTable(-2); /* * if(!lua.IsType(-1, this.NewTypeId)) * { * throw new Exception("Received type id is invalid"); * } * * string received_type_name = lua.GetTypeName(lua.GetType(-1)); * * if(received_type_name != "TestType1") * { * throw new Exception("Received type name is invalid"); * } */ lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "tostring"); lua.Push(-3); lua.MCall(1, 1); if (lua.GetString(-1) != this.RandomString) { throw new Exception("Metatable method __tostring returned incorrect string"); } lua.Pop(3); lua.PushMetaTable(NewTypeId); lua.PushNil(); lua.SetField(-2, "__tostring"); lua.Pop(1); taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { this.lua_extructor = lua_extructor; // Create new metatable and populate it this.TypeId = lua.CreateMetaTable("GetTableAndRawGetMetaTable"); lua.PushManagedFunction(this.indexDelegate); lua.SetField(-2, "__index"); lua.Pop(1); // Create a test table lua.CreateTable(); lua.PushString(this.RandomString2); lua.SetField(-2, "TestVal"); lua.PushMetaTable(this.TypeId); lua.SetMetaTable(-2); // Test GetTable lua.PushString("TestVal"); lua.GetTable(-2); string receivedString1 = lua.GetString(-1); if (receivedString1 != this.RandomString2) { throw new Exception("GetTable returned invalid string on existing key"); } lua.Pop(1); lua.PushString("ArbitraryString"); lua.GetTable(-2); string receivedString11 = lua.GetString(-1); if (receivedString11 != this.RandomString1) { throw new Exception("GetTable returned invalid string on non-existing key"); } lua.Pop(1); // Test RawGet lua.PushString("TestVal"); lua.RawGet(-2); string receivedString2 = lua.GetString(-1); if (receivedString2 != this.RandomString2) { throw new Exception("RawGet returned invalid string on existing key"); } lua.Pop(1); lua.PushString("ArbitraryString"); lua.RawGet(-2); int received_type = lua.GetType(-1); if (received_type != (int)TYPES.NIL) { throw new Exception("RawGet didn't return NIL on non-existing key"); } lua.Pop(1); lua.Pop(lua.Top()); lua.PushMetaTable(TypeId); lua.PushNil(); lua.SetField(-2, "__index"); lua.Pop(1); taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
void IModule.Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) { if (is_serverside) { throw new Exception("Must be loaded from client"); } string platformIdentifier = OperatingSystem.IsWindows() ? "win-x64" : "linux-x64"; assembly_context.SetCustomNativeLibraryResolver((ctx, str) => { if (str.Contains("sourcesdkc")) { if (sourcesdkc == IntPtr.Zero) { Console.WriteLine("loading sourcesdkc"); sourcesdkc = NativeLibrary.Load($"./garrysmod/lua/bin/Modules/GetRenderTargetExample/runtimes/{platformIdentifier}/native/sourcesdkc"); Console.WriteLine($"loaded sourcesdkc: {sourcesdkc != IntPtr.Zero}"); } return(sourcesdkc); } return(IntPtr.Zero); }); if (interfaceh.Sys_LoadInterface("vguimatsurface", ISurface.VGUI_SURFACE_INTERFACE_VERSION, out _, out IntPtr isurfacePtr)) { surface = new(isurfacePtr); } else { throw new Exception("failed loading vguimatsurface"); } if (interfaceh.Sys_LoadInterface("materialsystem", IMaterialSystem.MATERIAL_SYSTEM_INTERFACE_VERSION, out _, out IntPtr materialSystemPtr)) { materialSystem = new(materialSystemPtr); } else { Console.WriteLine("failed loading materialsystem"); } lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "GetRenderTarget"); lua.PushString("ExampleRTwithAlpha"); lua.PushNumber(512); lua.PushNumber(512); lua.MCall(3, 1); rt = lua.GetUserType(-1, (int)TYPES.TEXTURE); lua.Pop(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "CreateMaterial"); lua.PushString("ExampleRTwithAlpha_Mat"); lua.PushString("UnlitGeneric"); lua.CreateTable(); { lua.PushString("$basetexture"); lua.PushString("ExampleRTwithAlpha"); lua.SetTable(-3); lua.PushString("$translucent"); lua.PushString("1"); lua.SetTable(-3); } lua.MCall(3, 1); mat = lua.GetUserType(-1, (int)TYPES.MATERIAL); lua.Pop(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "hook"); lua.GetField(-1, "Add"); lua.PushString("HUDPaint"); lua.PushString("ExampleRTwithAlpha_Render"); lua.PushManagedFunction(Render); lua.MCall(3, 0); lua.Pop(); }