public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { lua.PushManagedClosure(lua => { if (lua.Top() != 3) { throw new Exception("Closure execution stack has incorect number of items."); } string one = lua.GetString(1); string two = lua.GetString(2); double three = lua.GetNumber(3); lua.Pop(3); lua.PushString(one + three); lua.PushString(three + two); if (lua.Top() != 2) { throw new Exception("Closure execution stack has incorrect number of items after executtion."); } return(2); }, 0); lua.PushString(random1); lua.PushString(random2); lua.PushNumber(random3); lua.MCall(3, 2); string ret_1 = lua.GetString(-2); string ret_2 = lua.GetString(-1); lua.Pop(2); if (ret_1 != random1 + random3) { throw new Exception("First return string is incorrect."); } if (ret_2 != random3 + random2) { 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>(); try { Random rand = new Random(); int random_number = rand.Next(1, 500); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "tonumber"); lua.PushString(random_number.ToString()); try { lua.MCall(1, 1); } catch (GmodLuaException) { throw new MCallTestException("Exception was thrown by MCall, but it is not expected."); } int received_number = (int)lua.GetNumber(-1); if (received_number != random_number) { throw new MCallTestException("Numbers mismatch."); } lua.Pop(2); string random_error_message = Guid.NewGuid().ToString(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "assert"); lua.PushBool(false); lua.PushString(random_error_message); try { lua.MCall(2, 0); } catch (GmodLuaException e) { if (e.Message != random_error_message) { throw new MCallTestException("Error message is invalid"); } taskCompletion.TrySetResult(true); } catch (Exception) { throw new MCallTestException("Wrong exception type was thrown"); } } 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 { 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 assembly_context) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { int stack_state = lua.Top(); lua.PushString(random1); lua.PushString(random2); lua.PushManagedClosure(lua => { if (lua.Top() != 1) { throw new Exception("Managed closure execution stack has incorrect number of items"); } double num = lua.GetNumber(1); lua.Pop(1); string first = lua.GetString(GmodInterop.GetUpvalueIndex(1)); string second = lua.GetString(GmodInterop.GetUpvalueIndex(2)); lua.PushString(first + num + second); return(1); }, 2); if (lua.Top() != stack_state + 1) { throw new Exception("Wrong number of items left on the stack"); } lua.PushNumber(random3); lua.MCall(1, 1); string ret = lua.GetString(-1); lua.Pop(1); if (ret != random1 + random3 + random2) { 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 _) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { Random rand = new Random(); int first = rand.Next(1, 10000000); int second = rand.Next(1, 10000000); int third = rand.Next(1, 10000000); lua.PushNumber(first); lua.PushNumber(second); lua.PushNumber(third); lua.Insert(-2); int received_first = (int)lua.GetNumber(-3); int received_second = (int)lua.GetNumber(-2); int received_third = (int)lua.GetNumber(-1); lua.Pop(3); if (!(received_first == first && received_second == third && received_third == second)) { throw new InsertTestException("Received numbers are 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 _) { try { Random rand = new Random(); int random_number = rand.Next(1, 500); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "tonumber"); lua.PushString(random_number.ToString()); if (lua.PCall(1, 1, 0) != 0) { throw new PCallTest_NoError_Exception("PCall returned nonzero error code"); } double recieved_num = lua.GetNumber(-1); if (recieved_num != (double)random_number) { throw new PCallTest_NoError_Exception("Recieved value is invalid"); } lua.Pop(2); string random_error_msg = Guid.NewGuid().ToString(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "assert"); lua.PushBool(false); lua.PushString(random_error_msg); if (lua.PCall(2, 0, 0) == 0) { throw new PCallTest_Exception("assert(false, ...) didn't throw an error"); } string res_err_msg = lua.GetString(-1); if (res_err_msg != random_error_msg) { throw new PCallTest_Exception("Received error message is invalid"); } lua.Pop(2); taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
internal static int ManagedDelegateGC(IntPtr lua_state) { ILua lua = GmodInterop.GetLuaFromState(lua_state); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, ManagedFunctionIdField); int managed_delegate_type_id = (int)lua.GetNumber(-1); lua.Pop(2); IntPtr managed_delegate_handle = lua.GetUserType(1, managed_delegate_type_id); GCHandle.FromIntPtr(managed_delegate_handle).Free(); return(0); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { taskSource = new TaskCompletionSource <bool>(); try { string LuaNumId = Guid.NewGuid().ToString(); double[] Random_numbers = new double[10]; Random rand = new Random(); for (int i = 0; i < 10; i++) { Random_numbers[i] = rand.NextDouble(); } lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); for (int i = 0; i < 10; i++) { lua.PushNumber(Random_numbers[i]); lua.SetField(-2, LuaNumId + "Num" + i.ToString()); } for (int i = 0; i < 10; i++) { lua.GetField(-1, LuaNumId + "Num" + i.ToString()); double tmp = lua.GetNumber(-1); lua.Pop(1); if (tmp != Random_numbers[i]) { throw new PushNumberException(i, Random_numbers[i], tmp); } } taskSource.TrySetResult(true); } catch (Exception e) { taskSource.TrySetException(new Exception[] { e }); } return(taskSource.Task); }
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 assembly_context) { TaskCompletionSource <bool> taskCompletion = new(); try { unsafe { lua.PushCFunction(&TestFunc); lua.MCall(0, 2); string ret_string = lua.GetString(-1); double ret_number = lua.GetNumber(-2); lua.Pop(2); if (ret_string != random_string) { throw new Exception("Returned string is invalid"); } if (ret_number != random_number) { throw new Exception("Returned number is invalid"); } taskCompletion.TrySetResult(true); } } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
internal static int ManagedDelegateExecutor(IntPtr lua_state) { ILua lua = GmodInterop.GetLuaFromState(lua_state); try { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, ManagedFunctionIdField); int managed_delegate_type_id = (int)lua.GetNumber(-1); lua.Pop(2); IntPtr managed_delegate_handle = lua.GetUserType(GmodInterop.GetUpvalueIndex(1, false), managed_delegate_type_id); Func <ILua, int> managed_delegate = (Func <ILua, int>)GCHandle.FromIntPtr(managed_delegate_handle).Target; return(Math.Max(0, managed_delegate(lua))); } catch (Exception e) { lua.Pop(lua.Top()); lua.PushString(".NET Exception: " + e.ToString()); return(-1); } }
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 int Render(ILua lua) { //render.PushRenderTarget(textureRT) //cam.Start2D() // render.Clear(0, 0, 0, 0) lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "CurTime"); lua.MCall(0, 1); int CurTime = (int)lua.GetNumber(-1); lua.Pop(); // PushRenderTarget { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "render"); lua.GetField(-1, "PushRenderTarget"); lua.PushUserType(rt, (int)TYPES.TEXTURE); lua.MCall(1, 0); lua.Pop(); } // Start 2D { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "cam"); lua.GetField(-1, "Start2D"); lua.MCall(0, 0); lua.Pop(); } // Clear { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "render"); lua.GetField(-1, "Clear"); lua.PushNumber(0); lua.PushNumber(0); lua.PushNumber(0); lua.PushNumber(0); lua.MCall(4, 0); lua.Pop(); } // Draw Rects { if (surface is not null) { surface.DrawSetColor(255, 255, 255, 255); surface.DrawFilledRect(20, (100 + (((int)Math.Sin(CurTime)) * 50)), 50, 50); surface.DrawSetColor(255, 0, 0, 100); surface.DrawFilledRect(120, (100 + (((int)Math.Sin(CurTime)) * 50)), 50, 50); } else { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "surface"); lua.GetField(-1, "SetDrawColor"); lua.PushNumber(255); lua.PushNumber(255); lua.PushNumber(255); lua.PushNumber(255); lua.MCall(4, 0); lua.Pop(2); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "surface"); lua.GetField(-1, "DrawRect"); lua.PushNumber(20); lua.PushNumber((100 + (((int)Math.Sin(CurTime)) * 50))); lua.PushNumber(50); lua.PushNumber(50); lua.MCall(4, 0); lua.Pop(2); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "surface"); lua.GetField(-1, "SetDrawColor"); lua.PushNumber(255); lua.PushNumber(0); lua.PushNumber(0); lua.PushNumber(100); lua.MCall(4, 0); lua.Pop(2); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "surface"); lua.GetField(-1, "DrawRect"); lua.PushNumber(120); lua.PushNumber((100 + (((int)Math.Sin(CurTime)) * 50))); lua.PushNumber(50); lua.PushNumber(50); lua.MCall(4, 0); lua.Pop(2); } } // End2D { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "cam"); lua.GetField(-1, "End2D"); lua.MCall(0, 0); lua.Pop(); } // Pop { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "render"); lua.GetField(-1, "PopRenderTarget"); lua.MCall(0, 0); lua.Pop(); } // Draw it on screen { if (surface is not null) { surface.DrawSetColor(255, 255, 255, 255); } else { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "surface"); lua.GetField(-1, "SetDrawColor"); lua.PushNumber(255); lua.PushNumber(255); lua.PushNumber(255); lua.PushNumber(255); lua.MCall(4, 0); lua.Pop(); } lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "surface"); lua.GetField(-1, "SetMaterial"); lua.PushUserType(mat, (int)TYPES.MATERIAL); lua.MCall(1, 0); lua.Pop(); if (surface is not null) { surface.DrawTexturedRect(50, 50, 512, 512); } else { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "surface"); lua.GetField(-1, "DrawTexturedRect"); lua.PushNumber(50); lua.PushNumber(50); lua.PushNumber(512); lua.PushNumber(512); lua.MCall(4, 0); lua.Pop(); } } return(0); }