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 assembly_context) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { lua.PushManagedFunction((lua) => { throw new Exception(random_string); }); lua.MCall(0, 0); throw new Exception("The managed exception was not caught by MCall."); } catch (Exception e) { if (e is GmodLuaException && e.Message.Contains(random_string)) { taskCompletion.TrySetResult(true); } else { taskCompletion.TrySetException(new Exception[] { e }); } } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { lua.PushManagedFunction((lua) => { lua.PushString(random_string); return(1); }); lua.MCall(0, 1); string ret_string = lua.GetString(-1); lua.Pop(1); if (ret_string != random_string) { throw new Exception("Return string is invalid"); } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
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 static void PushGlobalFunction(ILua lua, string key, Func <ILua, int> value) { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "CNode"); lua.PushManagedFunction(value); lua.SetField(-2, key); lua.Pop(); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { lua.PushManagedFunction((lua) => { int stack_items = lua.Top(); if (stack_items != 3) { throw new Exception("The number of items on the execution stack is incorrect"); } string first = lua.GetString(1); string second = lua.GetString(2); double third = lua.GetNumber(3); lua.Pop(3); lua.PushString(first + third); lua.PushString(third + second); return(2); }); lua.PushString(random_string_1); lua.PushString(random_string_2); lua.PushNumber(random_number); lua.MCall(3, 2); string ret_1 = lua.GetString(-2); string ret_2 = lua.GetString(-1); lua.Pop(2); if (ret_1 != random_string_1 + random_number) { throw new Exception("First return string is incorrect"); } if (ret_2 != random_number + random_string_2) { throw new Exception("Second return 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); }
public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) { this.lua = lua; this.isServerSide = is_serverside; this.current_load_context = assembly_context; this.OnTickDelegate = this.OnTick; this.OnTickIdentifier = Guid.NewGuid().ToString(); current_test = null; if (isServerSide) { try { lua.Log("Loading test runner"); //Register OnTick with Garry's Mod lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "hook"); lua.GetField(-1, "Add"); lua.PushString("Tick"); lua.PushString(this.OnTickIdentifier); lua.PushManagedFunction(this.OnTickDelegate); lua.MCall(3, 0); lua.Pop(2); //Get the list of tests ListOfTests = new List <ITest>(); if (typeof(Tests).Assembly.GetTypes().Any(type => typeof(ITest).IsAssignableFrom(type) && type != typeof(ITest))) { foreach (Type t in typeof(Tests).Assembly.GetTypes().Where(type => type != typeof(ITest) && typeof(ITest).IsAssignableFrom(type))) { ListOfTests.Add((ITest)Activator.CreateInstance(t)); } } lua.Log("There are " + ListOfTests.Count + " tests to run:"); foreach (ITest test in ListOfTests) { lua.Log(test.GetType().ToString()); } IsEverythingSuccessful = true; tests_start_time = DateTime.Now; lua.Log("Test runner was loaded!"); } catch (Exception e) { lua.Log("Test runner FAILED to start: " + e.GetType().ToString() + ". Exception message: " + e.Message); } } }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context) { try { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "hook"); lua.GetField(-1, "Add"); lua.PushString("Tick"); lua.PushString(hook_id); lua.PushManagedFunction(l => { try { if (counter < 33) { counter++; } else { l.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); l.GetField(-1, "hook"); l.GetField(-1, "Remove"); l.PushString("Tick"); l.PushString(hook_id); l.MCall(2, 0); l.Pop(2); if (counter == 33) { taskCompletion.TrySetResult(true); } } } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(0); }); lua.MCall(3, 0); lua.Pop(2); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { int stack_state = lua.Top(); custom_type_id = lua.CreateMetaTable(Guid.NewGuid().ToString()); lua.PushManagedFunction(lua => { long value = (long)lua.GetUserType(1, custom_type_id); lua.Pop(1); lua.PushString((value * 2).ToString()); return(1); }); lua.SetField(-2, "__call"); lua.Pop(1); lua.PushUserType((IntPtr)random_integer, custom_type_id); lua.MCall(0, 1); string ret_string = lua.GetString(-1); lua.Pop(1); lua.PushMetaTable(custom_type_id); lua.PushNil(); lua.SetField(-2, "__call"); lua.Pop(1); if ((stack_state - lua.Top()) != 0) { throw new Exception("Lua stack has some values left"); } if (ret_string != (random_integer * 2).ToString()) { throw new Exception("Return 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 _) { this.lua_extructor = lua_extructor; try { lua.PushManagedFunction(this.spawn_func); lua.MCall(0, 0); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public GmodInteropService(ILua lua) { disposed = false; onTickCallbackId = Guid.NewGuid().ToString(); getPlayersTasks = new ConcurrentQueue <TaskCompletionSource <List <string> > >(); lua.PushGlobalTable(); lua.GetField(-1, "hook"); lua.GetField(-1, "Add"); lua.PushString("Tick"); lua.PushString(onTickCallbackId); lua.PushManagedFunction(lua => { SetPlayerList(lua); return(0); }); lua.MCall(3, 0); lua.Pop(2); }
public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "print"); lua.PushString("Hello World RPC"); lua.MCall(1, 0); lua.Pop(); int DiscordRPC_update_callbacks(ILua lua) { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "print"); lua.PushString("It Works!"); lua.MCall(1, 0); lua.Pop(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "game"); lua.GetField(-1, "GetMap"); lua.MCall(0, 1); map = lua.GetString(-1); lua.Pop(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "game"); lua.GetField(-1, "SinglePlayer"); lua.MCall(0, 1); bool IsSinglePlayer = lua.GetBool(-1); lua.Pop(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "engine"); lua.GetField(-1, "ActiveGamemode"); lua.MCall(0, 1); gamemode = lua.GetString(-1); lua.Pop(); if (IsSinglePlayer) { activityDetails = "SinglePlayer"; } else { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "player"); lua.GetField(-1, "GetCount"); lua.MCall(0, 1); int players = (int)lua.GetNumber(-1); lua.Pop(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "game"); lua.GetField(-1, "MaxPlayers"); lua.MCall(0, 1); int maxPlayers = (int)lua.GetNumber(-1); lua.Pop(); activityDetails = $"{players}/{maxPlayers}"; } /*if (!is_serverside) * { * lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); * lua.GetField(-1, "LocalPlayer"); //team.GetScore(ply:Team()) * lua.MCall(0, 1); * * lua.GetField(-1, "Team"); * lua.Push(-2); * lua.MCall(0, 1); * double team = lua.GetNumber(-1); * lua.Pop(); * * lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); * lua.GetField(-1, "teams"); * lua.GetField(-1, "GetScore"); * lua.PushNumber(team); * lua.MCall(1, 1); * score = (int)lua.GetNumber(-1); * lua.Pop(); * }*/ UpdateActivity(); return(0); }; lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.PushManagedFunction(DiscordRPC_update_callbacks); lua.SetField(-2, "DiscordRPC_update_activity"); lua.Pop(1); discord = new Discord.Discord(client_ID, (UInt64)Discord.CreateFlags.Default); discord.SetLogHook(Discord.LogLevel.Debug, (level, message) => { Console.WriteLine("Log[{0}] {1}", level, message); }); var applicationManager = discord.GetApplicationManager(); // Get the current locale. This can be used to determine what text or audio the user wants. Console.WriteLine("Current Locale: {0}", applicationManager.GetCurrentLocale()); // Get the current branch. For example alpha or beta. Console.WriteLine("Current Branch: {0}", applicationManager.GetCurrentBranch()); // If you want to verify information from your game's server then you can // grab the access token and send it to your server. // // This automatically looks for an environment variable passed by the Discord client, // if it does not exist the Discord client will focus itself for manual authorization. // // By-default the SDK grants the identify and rpc scopes. // Read more at https://discordapp.com/developers/docs/topics/oauth2 // applicationManager.GetOAuth2Token((Discord.Result result, ref Discord.OAuth2Token oauth2Token) => // { // Console.WriteLine("Access Token {0}", oauth2Token.AccessToken); // }); var activityManager = discord.GetActivityManager(); var lobbyManager = discord.GetLobbyManager(); // Received when someone accepts a request to join or invite. // Use secrets to receive back the information needed to add the user to the group/party/match activityManager.OnActivityJoin += secret => { Console.WriteLine("OnJoin {0}", secret); lobbyManager.ConnectLobbyWithActivitySecret(secret, (Discord.Result result, ref Discord.Lobby lobby) => { Console.WriteLine("Connected to lobby: {0}", lobby.Id); lobbyManager.ConnectNetwork(lobby.Id); lobbyManager.OpenNetworkChannel(lobby.Id, 0, true); foreach (var user in lobbyManager.GetMemberUsers(lobby.Id)) { lobbyManager.SendNetworkMessage(lobby.Id, user.Id, 0, Encoding.UTF8.GetBytes(String.Format("Hello, {0}!", user.Username))); } UpdateActivity(); }); }; // Received when someone accepts a request to spectate activityManager.OnActivitySpectate += secret => { Console.WriteLine("OnSpectate {0}", secret); }; // A join request has been received. Render the request on the UI. activityManager.OnActivityJoinRequest += (ref Discord.User user) => { Console.WriteLine("OnJoinRequest {0} {1}", user.Id, user.Username); }; // An invite has been received. Consider rendering the user / activity on the UI. activityManager.OnActivityInvite += (Discord.ActivityActionType Type, ref Discord.User user, ref Discord.Activity activity2) => { Console.WriteLine("OnInvite {0} {1} {2}", Type, user.Username, activity2.Name); // activityManager.AcceptInvite(user.Id, result => // { // Console.WriteLine("AcceptInvite {0}", result); // }); }; // This is used to register the game in the registry such that Discord can find it. // This is only needed by games acquired from other platforms, like Steam. // activityManager.RegisterCommand(); var imageManager = discord.GetImageManager(); var userManager = discord.GetUserManager(); lobbyManager.OnLobbyMessage += (lobbyID, userID, data) => { Console.WriteLine("lobby message: {0} {1}", lobbyID, Encoding.UTF8.GetString(data)); }; lobbyManager.OnNetworkMessage += (lobbyId, userId, channelId, data) => { Console.WriteLine("network message: {0} {1} {2} {3}", lobbyId, userId, channelId, Encoding.UTF8.GetString(data)); }; lobbyManager.OnSpeaking += (lobbyID, userID, speaking) => { Console.WriteLine("lobby speaking: {0} {1} {2}", lobbyID, userID, speaking); }; UpdateActivity(); /* * var overlayManager = discord.GetOverlayManager(); * overlayManager.OnOverlayLocked += locked => * { * Console.WriteLine("Overlay Locked: {0}", locked); * }; * overlayManager.SetLocked(false); */ var storageManager = discord.GetStorageManager(); var contents = new byte[20000]; var random = new Random(); random.NextBytes(contents); Console.WriteLine("storage path: {0}", storageManager.GetPath()); storageManager.WriteAsync("foo", contents, res => { var files = storageManager.Files(); foreach (var file in files) { Console.WriteLine("file: {0} size: {1} last_modified: {2}", file.Filename, file.Size, file.LastModified); } storageManager.ReadAsyncPartial("foo", 400, 50, (result, data) => { Console.WriteLine("partial contents of foo match {0}", Enumerable.SequenceEqual(data, new ArraySegment <byte>(contents, 400, 50))); }); storageManager.ReadAsync("foo", (result, data) => { Console.WriteLine("length of contents {0} data {1}", contents.Length, data.Length); Console.WriteLine("contents of foo match {0}", Enumerable.SequenceEqual(data, contents)); Console.WriteLine("foo exists? {0}", storageManager.Exists("foo")); storageManager.Delete("foo"); Console.WriteLine("post-delete foo exists? {0}", storageManager.Exists("foo")); }); }); var storeManager = discord.GetStoreManager(); storeManager.OnEntitlementCreate += (ref Discord.Entitlement entitlement) => { Console.WriteLine("Entitlement Create1: {0}", entitlement.Id); }; // Start a purchase flow. // storeManager.StartPurchase(487507201519255552, result => // { // if (result == Discord.Result.Ok) // { // Console.WriteLine("Purchase Complete"); // } // else // { // Console.WriteLine("Purchase Canceled"); // } // }); // Get all entitlements. storeManager.FetchEntitlements(result => { if (result == Discord.Result.Ok) { foreach (var entitlement in storeManager.GetEntitlements()) { Console.WriteLine("entitlement: {0} - {1} {2}", entitlement.Id, entitlement.Type, entitlement.SkuId); } } }); // Get all SKUs. storeManager.FetchSkus(result => { if (result == Discord.Result.Ok) { foreach (var sku in storeManager.GetSkus()) { Console.WriteLine("sku: {0} - {1} {2}", sku.Name, sku.Price.Amount, sku.Price.Currency); } } }); updater = new Thread(UpdaterThread); updater.Start(); }
public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) { string platformIdentifier = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win-x64" : "linux-x64"; assembly_context.SetCustomNativeLibraryResolver((ctx, str) => { if (str.Contains("sourcesdkc")) { Console.WriteLine("loading sourcesdkc"); sourcesdkc = NativeLibrary.Load($"./garrysmod/lua/bin/Modules/SourceSDKTest/runtimes/{platformIdentifier}/native/sourcesdkc"); Console.WriteLine($"loaded sourcesdkc: {sourcesdkc != IntPtr.Zero}"); return(sourcesdkc); } return(IntPtr.Zero); }); Test(() => Dbg.Msg("Msg(string)\n")); Test(() => Dbg.Warning("Warning(string)\n")); Test(() => Dbg.Warning_SpewCallStack(100, "Warning_SpewCallStack(int, string)\n")); Test(() => Dbg.DevMsg("DevMsg(string)\n")); Test(() => Dbg.DevWarning("DevWarning(string)\n")); Test(() => Dbg.ConColorMsg(new Color(255, 255, 0), "ConColorMsg(in Color, string)\n")); Test(() => Dbg.ConMsg("ConMsg(string)\n")); Test(() => Dbg.ConDMsg("ConDMsg(string)\n")); // Test(() => Dbg.COM_TimestampedLog("COM_TimestampedLog(format = %s)", "COM_TimestampedLog")); Test(() => { unsafe { string path = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "filesystem_stdio.dll" : "filesystem_stdio.so"; if (!interfaceh.Sys_LoadInterface(path, FileSystem.FILESYSTEM_INTERFACE_VERSION, out IntPtr module, out IntPtr fSPtr)) { Console.WriteLine("failed loading FS"); } if (!interfaceh.Sys_LoadInterface(path, BaseFileSystem.BASEFILESYSTEM_INTERFACE_VERSION, out module, out IntPtr baseFSPtr)) { Console.WriteLine("failed loading BFS"); } if (fSPtr == IntPtr.Zero || baseFSPtr == IntPtr.Zero) { Console.WriteLine("unloading it"); NativeLibrary.Free(module); Console.WriteLine("unloaded ???"); return; } FileSystem fileSystem = new(fSPtr); BaseFileSystem baseFileSystem = new(baseFSPtr); Console.WriteLine("get tier"); Console.WriteLine(fileSystem.GetTier()); if (interfaceh.Sys_LoadInterface("vguimatsurface", ISurface.VGUI_SURFACE_INTERFACE_VERSION, out IntPtr isurfaceModule, out IntPtr isurfaceInterface)) { ISurface surface = new(isurfaceInterface); Console.WriteLine(surface.GetTier()); } fileSystem.PrintSearchPaths(); Console.WriteLine("add new path"); fileSystem.AddSearchPath("garrysmod", "GAME", SearchPathAdd_t.PATH_ADD_TO_HEAD); fileSystem.PrintSearchPaths(); IntPtr fileHandle = baseFileSystem.Open("resource/GameMenu.res", "rb", "GAME"); if (fileHandle != IntPtr.Zero) { uint size = baseFileSystem.Size(fileHandle); byte *buffPtr = stackalloc byte[(int)size]; IntPtr buffIntPtr = new(buffPtr); baseFileSystem.Read(buffIntPtr, (int)size, fileHandle); Console.WriteLine("Printing file contents"); Console.WriteLine(Encoding.UTF8.GetString(buffPtr, (int)size)); baseFileSystem.Close(fileHandle); } else { Console.WriteLine("not found file"); } } }); assembly_context.SetCustomNativeLibraryResolver(null); Debug.Assert(!failed); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); handle = lua.PushManagedFunction(DrawSomething); lua.SetField(-2, "DrawSomethingPLSWORK"); lua.Pop(); }
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(); }
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 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); }
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 _) { try { Random rand = new Random(); int curr_type; string curr_name; lua.PushNumber(rand.NextDouble()); curr_type = lua.GetType(-1); curr_name = lua.GetTypeName(curr_type); if (curr_type != (int)TYPES.NUMBER || curr_name != "number") { throw new CheckTypeException("number"); } lua.Pop(1); lua.PushString(Guid.NewGuid().ToString()); curr_type = lua.GetType(-1); curr_name = lua.GetTypeName(curr_type); if (curr_type != (int)TYPES.STRING || curr_name != "string") { throw new CheckTypeException("string"); } lua.Pop(1); lua.PushBool(true); curr_type = lua.GetType(-1); curr_name = lua.GetTypeName(curr_type); if (curr_type != (int)TYPES.BOOL || curr_name != "bool") { throw new CheckTypeException("bool"); } lua.Pop(1); lua.PushNil(); curr_type = lua.GetType(-1); curr_name = lua.GetTypeName(curr_type); if (curr_type != (int)TYPES.NIL || curr_name != "nil") { throw new CheckTypeException("nil"); } lua.Pop(1); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); curr_type = lua.GetType(-1); curr_name = lua.GetTypeName(curr_type); if (curr_type != (int)TYPES.TABLE || curr_name != "table") { throw new CheckTypeException("table"); } lua.Pop(1); lua.PushVector(new Vector3((float)rand.NextDouble())); curr_type = lua.GetType(-1); curr_name = lua.GetTypeName(curr_type); if (curr_type != (int)TYPES.Vector || curr_name != "vector") { throw new CheckTypeException("vector"); } lua.Pop(1); lua.PushAngle(new Vector3((float)rand.NextDouble())); curr_type = lua.GetType(-1); curr_name = lua.GetTypeName(curr_type); if (curr_type != (int)TYPES.ANGLE || curr_name != "angle") { throw new CheckTypeException("angle"); } lua.Pop(1); lua.PushManagedFunction(this.test_func); curr_type = lua.GetType(-1); curr_name = lua.GetTypeName(curr_type); if (curr_type != (int)TYPES.FUNCTION || curr_name != "function") { throw new CheckTypeException("function"); } 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; 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); }