using Maca134.Arma.DllExport; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using WebSocketSharp; using WebSocketSharp.Server; namespace arcas_dev_tools { public class Echo : WebSocketBehavior { protected override void OnMessage(MessageEventArgs e) { if (!e.Data.IsNullOrEmpty()) { string[] passthrough = e.Data.Split(':'); string key = passthrough[0]; string code = passthrough[1]; arcas_dev_tools.requests.Add($@" _return = [] call compile '{code}'; if (!isNil '_return') then {{ 'arcas_dev_tools' callExtension format['2:%1:{key}:#C72651', _return]; }}; "); } } } public class arcas_dev_tools { public static string BasePath => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); public static List<string> requests = new List<string>(); private static WebSocketServer wssv; private static bool initialized = false; static arcas_dev_tools() { AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { var name = new AssemblyName(args.Name).Name; var filename = Path.Combine(BasePath, name + ".dll"); return File.Exists(filename) ? Assembly.LoadFile(filename) : null; }; } [ArmaDllExport] public static string Invoke(string function, int outputSize) { try { List<string> output = new List<string>(); string[] passthrough = function.Split(':'); switch (passthrough[0]) { // Initialize case "0": if (initialized) { throw new Exception("DLL already initialized, is arma being stupid?"); } int port = 4648; if (passthrough[1] == "1") { port = 4649; } wssv = new WebSocketServer($"ws://127.0.0.1:{port}"); wssv.AddWebSocketService<Echo>("/Echo"); wssv.Start(); if (wssv.IsListening) { output.Add($"Listening on port {wssv.Port}"); initialized = true; } else { throw new Exception($"Failed to bind to port {wssv.Port}"); } break; // Check for requests case "1": if (requests.Count > 0) { output.Add(requests[0]); requests.RemoveAt(0); } break; // Update the gui case "2": wssv.WebSocketServices.BroadcastAsync($"{passthrough[2]}:{passthrough[1]}:{passthrough[3]}", null); break; // Update FPS and Threads case "3": wssv.WebSocketServices.BroadcastAsync($"1:{passthrough[1]}:{passthrough[2]}:{passthrough[3]}", null); break; // Default! default: throw new Exception($"Invalid command: {passthrough[0]}"); } string returned = "[true,["; for ( int i = 0; i < output.Count; i++ ) { if (i == 0) { returned += $"\"{output[i]}\""; } else { returned += $",\"{output[i]}\""; } } return $"{returned}]]"; } catch (Exception e) { return $"[false,[\"{e.Message}\"]]"; } } } }