EloiStree / HelloWarcraftQAXR

Learn coding by creating bot and script for QA on Wow and other games through XR.
1 stars 0 forks source link

Topic: Send UDP message with LUA and C# in console mode #79

Open EloiStree opened 6 months ago

EloiStree commented 6 months ago

Something I need to learn to teach LUA on game from UDP message send to the OMI manager.

To learn and do later:

Chat GPT proposition:

-- your_script.lua

-- Function to send UDP message
function sendUdpMessage(message)
    udp_send(message)
end

-- Function to print to console
function printToConsole(message)
    console_print(message)
end

-- Example usage
sendUdpMessage("Hello from Lua to UDP!")
printToConsole("Hello from Lua to Console!")
using System;
using System.Net;
using System.Net.Sockets;
using NLua;

class Program
{
    static void Main()
    {
        // Create a Lua interpreter
        using (Lua lua = new Lua())
        {
            // Expose C# functions to Lua
            lua.RegisterFunction("udp_send", null, typeof(Program).GetMethod("SendUdpMessage"));
            lua.RegisterFunction("console_print", null, typeof(Program).GetMethod("PrintToConsole"));

            // Run Lua script
            RunLuaScript(lua, "your_script.lua");
        }

        Console.ReadLine(); // Keep the console window open
    }

    static void RunLuaScript(Lua lua, string scriptPath)
    {
        try
        {
            // Load and execute Lua script
            lua.DoFile(scriptPath);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error running Lua script: {ex.Message}");
        }
    }

    // Lua-exposed function to send UDP message
    public static void SendUdpMessage(string message)
    {
        // Replace with your UDP settings
        string serverIp = "127.0.0.1";
        int serverPort = 12345;

        using (UdpClient udpClient = new UdpClient(serverIp, serverPort))
        {
            byte[] data = System.Text.Encoding.UTF8.GetBytes(message);
            udpClient.Send(data, data.Length);
        }
    }

    // Lua-exposed function to print to console
    public static void PrintToConsole(string message)
    {
        Console.WriteLine(message);
    }
}