From 97e22b35ada5898fdcb2556628f764d927cff913 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 26 May 2017 17:06:38 -0400 Subject: SLIGHT optimizations? --- ShiftOS_TheReturn/Localization.cs | 4 --- ShiftOS_TheReturn/Server.cs | 40 +++++++++++++++++++++ ShiftOS_TheReturn/ServerManager.cs | 62 +++++++++++++++++++++++++++++++++ ShiftOS_TheReturn/ShiftOS.Engine.csproj | 1 + ShiftOS_TheReturn/TerminalBackend.cs | 9 ++++- 5 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 ShiftOS_TheReturn/Server.cs (limited to 'ShiftOS_TheReturn') diff --git a/ShiftOS_TheReturn/Localization.cs b/ShiftOS_TheReturn/Localization.cs index 2c701c9..5d848b0 100644 --- a/ShiftOS_TheReturn/Localization.cs +++ b/ShiftOS_TheReturn/Localization.cs @@ -117,10 +117,6 @@ namespace ShiftOS.Engine } List orphaned = new List(); - if (Utils.FileExists("0:/dev_orphaned_lang.txt")) - { - orphaned = JsonConvert.DeserializeObject>(Utils.ReadAllText("0:/dev_orphaned_lang.txt")); // if this file exists read from it and put in list orphaned - } int start_index = 0; diff --git a/ShiftOS_TheReturn/Server.cs b/ShiftOS_TheReturn/Server.cs new file mode 100644 index 0000000..ddbd15b --- /dev/null +++ b/ShiftOS_TheReturn/Server.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShiftOS.Objects; + +namespace ShiftOS.Engine +{ + public interface Server + { + /// + /// Occurs when someone sends a message to the server. + /// + /// The message from the client. + void MessageReceived(ServerMessage msg); + } + + [AttributeUsage(AttributeTargets.Class, AllowMultiple=false)] + public class ServerAttribute : Attribute + { + public ServerAttribute(string name, int port) + { + Name = name; + Port = port; + } + + + /// + /// Gets the name of the server. + /// + public string Name { get; } + + /// + /// Gets the port of the server. + /// + public int Port { get; } + + } +} diff --git a/ShiftOS_TheReturn/ServerManager.cs b/ShiftOS_TheReturn/ServerManager.cs index 95e86e9..217b9ee 100644 --- a/ShiftOS_TheReturn/ServerManager.cs +++ b/ShiftOS_TheReturn/ServerManager.cs @@ -36,6 +36,8 @@ using static ShiftOS.Engine.SaveSystem; using Newtonsoft.Json; using System.Net.Sockets; using System.Diagnostics; +using System.IO; +using System.Reflection; namespace ShiftOS.Engine { @@ -104,6 +106,43 @@ Ping: {ServerManager.DigitalSocietyPing} ms /// public static event Action GUIDReceived; + private static void delegateToServer(ServerMessage msg) + { + string[] split = msg.GUID.Split('|'); + bool finished = false; + foreach (var exec in Directory.GetFiles(Environment.CurrentDirectory)) + { + if(exec.ToLower().EndsWith(".exe") || exec.ToLower().EndsWith(".dll")) + { + try + { + var asm = Assembly.LoadFile(exec); + foreach(var type in asm.GetTypes().Where(x => x.GetInterfaces().Contains(typeof(Server)))) + { + var attrib = type.GetCustomAttributes().FirstOrDefault(x => x is ServerAttribute) as ServerAttribute; + if(attrib != null) + { + if(split[0] == SaveSystem.CurrentSave.SystemName && split[1] == attrib.Port.ToString()) + { + if (Shiftorium.UpgradeAttributesUnlocked(type)) + { + type.GetMethods(BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(x => x.Name == "MessageReceived")?.Invoke(Activator.CreateInstance(type), null); + finished = true; + } + } + } + } + } + catch { } + } + } + if (finished == false) + { + Forward(split[2], "Error", $"{split[0]}:{split[1]}: connection refused"); + } + } + + private static void ServerManager_MessageReceived(ServerMessage msg) { switch(msg.Name) @@ -119,12 +158,35 @@ Ping: {ServerManager.DigitalSocietyPing} ms })); } break; + case "msgtosys": + try + { + var m = JsonConvert.DeserializeObject(msg.Contents); + if(m.GUID.Split('|')[2] != thisGuid.ToString()) + { + delegateToServer(m); + } + } + catch { } + break; case "getguid_reply": GUIDReceived?.Invoke(msg.Contents); break; } } + public static void SendMessageToIngameServer(string sysname, int port, string title, string contents) + { + var smsg = new ServerMessage + { + Name = title, + GUID = $"{sysname}|{port}|{thisGuid.ToString()}", + Contents = contents + }; + Forward("all", "msgtosys", JsonConvert.SerializeObject(smsg)); + + } + public static void Detach_ServerManager_MessageReceived() { MessageReceived -= new ServerMessageReceived(ServerManager_MessageReceived); diff --git a/ShiftOS_TheReturn/ShiftOS.Engine.csproj b/ShiftOS_TheReturn/ShiftOS.Engine.csproj index 8b48023..9d7d696 100644 --- a/ShiftOS_TheReturn/ShiftOS.Engine.csproj +++ b/ShiftOS_TheReturn/ShiftOS.Engine.csproj @@ -124,6 +124,7 @@ + diff --git a/ShiftOS_TheReturn/TerminalBackend.cs b/ShiftOS_TheReturn/TerminalBackend.cs index c8619b5..b18e27c 100644 --- a/ShiftOS_TheReturn/TerminalBackend.cs +++ b/ShiftOS_TheReturn/TerminalBackend.cs @@ -84,6 +84,11 @@ namespace ShiftOS.Engine /// public static string LastCommand = ""; + /// + /// Gets the output of the last command. + /// + public static string LastCommandBuffer { get; private set; } + /// /// Invokes a ShiftOS terminal command. /// @@ -395,8 +400,10 @@ namespace ShiftOS.Engine } string buffer = tw.ToString(); + LastCommandBuffer = buffer; Console.SetOut(new TerminalTextWriter()); - Console.Write(buffer); + if(!isRemote) + Console.Write(buffer); } -- cgit v1.2.3