SLIGHT optimizations?

This commit is contained in:
Michael 2017-05-26 17:06:38 -04:00
parent 798f0e5920
commit 97e22b35ad
10 changed files with 677 additions and 381 deletions

View file

@ -31,6 +31,8 @@ using System.Reflection;
using System.Windows.Forms;
using ShiftOS.Engine;
using ShiftOS.WinForms.Tools;
using System.Linq;
using System.Threading;
namespace ShiftOS.WinForms.Applications
{
@ -540,400 +542,425 @@ namespace ShiftOS.WinForms.Applications
{
flbody.Controls.Clear();
List<ShifterSetting> cats = new List<ShifterSetting>();
IEnumerable<ShifterSetting> cats = this.settings.Where(x => x.SubCategory == subcat && x.Category == cat && x.Field.FlagFullfilled(LoadedSkin)).OrderBy(x=>x.Name);
foreach (var c in this.settings)
new Thread(() =>
{
if (c.SubCategory == subcat && c.Category == cat)
foreach (var c in cats)
{
if (c.Field.FlagFullfilled(LoadedSkin))
Label lbl = null;
int labelHeight = 0;
Desktop.InvokeOnWorkerThread(() =>
{
if (!cats.Contains(c))
{
cats.Add(c);
}
}
}
}
foreach(var c in cats)
{
var lbl = new Label();
int labelHeight = 0;
lbl.AutoSize = true;
lbl.Text = c.Name + ":";
flbody.Controls.Add(lbl);
lbl.TextAlign = ContentAlignment.MiddleLeft;
lbl.Show();
//Cool - label's in.
if(c.Field.FieldType == typeof(Point))
{
var width = new TextBox();
var height = new TextBox();
labelHeight = width.Height; //irony?
width.Width = 30;
height.Width = width.Width;
width.Text = ((Point)c.Field.GetValue(this.LoadedSkin)).X.ToString();
height.Text = ((Point)c.Field.GetValue(this.LoadedSkin)).Y.ToString();
flbody.SetFlowBreak(height, true);
ControlManager.SetupControl(width);
ControlManager.SetupControl(height);
flbody.Controls.Add(width);
width.Show();
flbody.Controls.Add(height);
height.Show();
EventHandler tc = (o, a) =>
lbl = new Label();
lbl.AutoSize = true;
lbl.Text = c.Name + ":";
flbody.Controls.Add(lbl);
lbl.TextAlign = ContentAlignment.MiddleLeft;
lbl.Show();
});
//Cool - label's in.
if (c.Field.FieldType == typeof(Point))
{
try
{
int x = Convert.ToInt32(width.Text);
int y = Convert.ToInt32(height.Text);
int oldx = ((Point)c.Field.GetValue(this.LoadedSkin)).X;
int oldy = ((Point)c.Field.GetValue(this.LoadedSkin)).Y;
if(x != oldx || y != oldy)
{
c.Field.SetValue(LoadedSkin, new Point(x, y));
CodepointValue += 200;
}
}
catch
TextBox width = null;
TextBox height = null;
Desktop.InvokeOnWorkerThread(() =>
{
width = new TextBox();
height = new TextBox();
labelHeight = width.Height; //irony?
width.Width = 30;
height.Width = width.Width;
width.Text = ((Point)c.Field.GetValue(this.LoadedSkin)).X.ToString();
height.Text = ((Point)c.Field.GetValue(this.LoadedSkin)).Y.ToString();
}
InvokeSetup(cat);
};
flbody.SetFlowBreak(height, true);
ControlManager.SetupControl(width);
ControlManager.SetupControl(height);
width.TextChanged += tc;
height.TextChanged += tc;
flbody.Controls.Add(width);
width.Show();
flbody.Controls.Add(height);
height.Show();
}
else if(c.Field.FieldType == typeof(string))
{
var str = new TextBox();
str.Width = 120;
ControlManager.SetupControl(str);
labelHeight = str.Height;
str.Text = c.Field.GetValue(LoadedSkin).ToString();
flbody.SetFlowBreak(str, true);
str.TextChanged += (o, a) =>
{
c.Field.SetValue(LoadedSkin, str.Text); CodepointValue += 100;
InvokeSetup(cat);
};
flbody.Controls.Add(str);
str.Show();
}
else if(c.Field.FieldType == typeof(byte[]))
{
//We'll assume that this is an image file.
var color = new Button();
color.Width = 40;
labelHeight = color.Height;
//just so it's flat like the system.
ControlManager.SetupControl(color);
flbody.SetFlowBreak(color, true);
color.BackgroundImage = SkinEngine.ImageFromBinary((byte[])c.Field.GetValue(this.LoadedSkin));
color.Click += (o, a) =>
{
AppearanceManager.SetupDialog(new GraphicPicker(color.BackgroundImage, c.Name, GetLayout(c.Field.GetImageName()), new Action<byte[], Image, ImageLayout>((col, gdiImg, layout) =>
{
c.Field.SetValue(LoadedSkin, col);
color.BackgroundImage = SkinEngine.ImageFromBinary(col);
color.BackgroundImageLayout = layout;
LoadedSkin.SkinImageLayouts[c.Field.GetImageName()] = layout;
CodepointValue += 700;
InvokeSetup(cat);
})));
};
flbody.Controls.Add(color);
color.Show();
}
else if (c.Field.FieldType == typeof(Size))
{
var width = new TextBox();
var height = new TextBox();
width.Width = 30;
height.Width = width.Width;
labelHeight = width.Height;
flbody.SetFlowBreak(height, true);
width.Text = ((Size)c.Field.GetValue(this.LoadedSkin)).Width.ToString();
height.Text = ((Size)c.Field.GetValue(this.LoadedSkin)).Height.ToString();
ControlManager.SetupControl(width);
ControlManager.SetupControl(height);
flbody.Controls.Add(width);
width.Show();
flbody.Controls.Add(height);
height.Show();
EventHandler tc = (o, a) =>
{
try
{
int x = Convert.ToInt32(width.Text);
int y = Convert.ToInt32(height.Text);
int oldx = ((Size)c.Field.GetValue(this.LoadedSkin)).Width;
int oldy = ((Size)c.Field.GetValue(this.LoadedSkin)).Height;
if (x != oldx || y != oldy)
EventHandler tc = (o, a) =>
{
c.Field.SetValue(LoadedSkin, new Size(x, y));
CodepointValue += 200;
}
}
catch
try
{
int x = Convert.ToInt32(width.Text);
int y = Convert.ToInt32(height.Text);
int oldx = ((Point)c.Field.GetValue(this.LoadedSkin)).X;
int oldy = ((Point)c.Field.GetValue(this.LoadedSkin)).Y;
if (x != oldx || y != oldy)
{
c.Field.SetValue(LoadedSkin, new Point(x, y));
CodepointValue += 200;
}
}
catch
{
width.Text = ((Point)c.Field.GetValue(this.LoadedSkin)).X.ToString();
height.Text = ((Point)c.Field.GetValue(this.LoadedSkin)).Y.ToString();
}
InvokeSetup(cat);
};
width.TextChanged += tc;
height.TextChanged += tc;
});
}
else if (c.Field.FieldType == typeof(string))
{
Desktop.InvokeOnWorkerThread(() =>
{
var str = new TextBox();
str.Width = 120;
ControlManager.SetupControl(str);
labelHeight = str.Height;
str.Text = c.Field.GetValue(LoadedSkin).ToString();
flbody.SetFlowBreak(str, true);
str.TextChanged += (o, a) =>
{
c.Field.SetValue(LoadedSkin, str.Text); CodepointValue += 100;
InvokeSetup(cat);
};
flbody.Controls.Add(str);
str.Show();
});
}
else if (c.Field.FieldType == typeof(byte[]))
{
Desktop.InvokeOnWorkerThread(() =>
{
//We'll assume that this is an image file.
var color = new Button();
color.Width = 40;
labelHeight = color.Height;
//just so it's flat like the system.
ControlManager.SetupControl(color);
flbody.SetFlowBreak(color, true);
color.BackgroundImage = SkinEngine.ImageFromBinary((byte[])c.Field.GetValue(this.LoadedSkin));
color.Click += (o, a) =>
{
AppearanceManager.SetupDialog(new GraphicPicker(color.BackgroundImage, c.Name, GetLayout(c.Field.GetImageName()), new Action<byte[], Image, ImageLayout>((col, gdiImg, layout) =>
{
c.Field.SetValue(LoadedSkin, col);
color.BackgroundImage = SkinEngine.ImageFromBinary(col);
color.BackgroundImageLayout = layout;
LoadedSkin.SkinImageLayouts[c.Field.GetImageName()] = layout;
CodepointValue += 700;
InvokeSetup(cat);
})));
};
flbody.Controls.Add(color);
color.Show();
});
}
else if (c.Field.FieldType == typeof(Size))
{
Desktop.InvokeOnWorkerThread(() =>
{
var width = new TextBox();
var height = new TextBox();
width.Width = 30;
height.Width = width.Width;
labelHeight = width.Height;
flbody.SetFlowBreak(height, true);
width.Text = ((Size)c.Field.GetValue(this.LoadedSkin)).Width.ToString();
height.Text = ((Size)c.Field.GetValue(this.LoadedSkin)).Height.ToString();
}
InvokeSetup(cat);
ControlManager.SetupControl(width);
ControlManager.SetupControl(height);
};
flbody.Controls.Add(width);
width.Show();
flbody.Controls.Add(height);
height.Show();
width.TextChanged += tc;
height.TextChanged += tc;
}
else if(c.Field.FieldType == typeof(bool))
{
var check = new CheckBox();
check.Checked = ((bool)c.Field.GetValue(LoadedSkin));
labelHeight = check.Height;
check.CheckedChanged += (o, a) =>
{
c.Field.SetValue(LoadedSkin, check.Checked);
CodepointValue += 50;
InvokeSetup(cat);
};
flbody.SetFlowBreak(check, true);
flbody.Controls.Add(check);
check.Show();
}
else if(c.Field.FieldType == typeof(Font))
{
var name = new ComboBox();
var size = new TextBox();
var style = new ComboBox();
name.Width = 120;
labelHeight = name.Height;
size.Width = 40;
style.Width = 80;
flbody.SetFlowBreak(style, true);
ControlManager.SetupControl(name);
ControlManager.SetupControl(size);
ControlManager.SetupControl(style);
//populate the font name box
foreach(var font in FontFamily.Families)
{
name.Items.Add(font.Name);
}
name.Text = ((Font)c.Field.GetValue(LoadedSkin)).Name;
size.Text = ((Font)c.Field.GetValue(LoadedSkin)).Size.ToString();
//populate the style box
foreach(var s in (FontStyle[])Enum.GetValues(typeof(FontStyle)))
{
style.Items.Add(s.ToString());
}
style.Text = ((Font)c.Field.GetValue(LoadedSkin)).Style.ToString();
name.SelectedIndexChanged += (o, a) =>
{
var en = (FontStyle[])Enum.GetValues(typeof(FontStyle));
var f = en[style.SelectedIndex];
c.Field.SetValue(LoadedSkin, new Font(name.Text, (float)Convert.ToDouble(size.Text), f));
CodepointValue += 100;
InvokeSetup(cat);
};
style.SelectedIndexChanged += (o, a) =>
{
var en = (FontStyle[])Enum.GetValues(typeof(FontStyle));
var f = en[style.SelectedIndex];
c.Field.SetValue(LoadedSkin, new Font(name.Text, (float)Convert.ToDouble(size.Text), f));
CodepointValue += 50;
InvokeSetup(cat);
};
size.TextChanged += (o, a) =>
{
try
{
var en = (FontStyle[])Enum.GetValues(typeof(FontStyle));
var f = en[style.SelectedIndex];
c.Field.SetValue(LoadedSkin, new Font(name.Text, (float)Convert.ToDouble(size.Text), f));
}
catch
{
size.Text = ((Font)c.Field.GetValue(LoadedSkin)).Size.ToString();
}
CodepointValue += 50;
InvokeSetup(cat);
};
flbody.Controls.Add(name);
flbody.Controls.Add(size);
flbody.Controls.Add(style);
name.Show();
size.Show();
style.Show();
}
else if(c.Field.FieldType == typeof(Color))
{
var color = new Button();
color.Width = 40;
labelHeight = color.Height;
//just so it's flat like the system.
ControlManager.SetupControl(color);
color.BackColor = ((Color)c.Field.GetValue(LoadedSkin));
color.Click += (o, a) =>
{
AppearanceManager.SetupDialog(new ColorPicker((Color)c.Field.GetValue(LoadedSkin), c.Name, new Action<Color>((col) =>
{
color.BackColor = col;
c.Field.SetValue(LoadedSkin, col);
CodepointValue += 300;
InvokeSetup(cat);
})));
};
flbody.SetFlowBreak(color, true);
color.Tag = "keepbg";
flbody.Controls.Add(color);
color.Show();
}
else if(c.Field.FieldType.IsEnum == true)
{
var cBox = new ComboBox();
cBox.Width = 150;
ControlManager.SetupControl(cBox);
foreach(var itm in Enum.GetNames(c.Field.FieldType))
{
cBox.Items.Add(itm);
}
cBox.Text = c.Field.GetValue(LoadedSkin).ToString();
cBox.SelectedIndexChanged += (o, a) =>
{
c.Field.SetValue(LoadedSkin, Enum.Parse(c.Field.FieldType, cBox.Text));
InvokeSetup(cat);
};
labelHeight = cBox.Height;
flbody.Controls.Add(cBox);
cBox.Show();
flbody.SetFlowBreak(cBox, true);
}
else if(c.Field.FieldType == typeof(int))
{
if (c.Field.HasShifterEnumMask())
{
var name = new ComboBox();
name.Width = 120;
ControlManager.SetupControl(name);
string[] items = c.Field.GetShifterEnumMask();
foreach(var item in items)
{
name.Items.Add(item);
}
name.SelectedIndex = (int)c.Field.GetValue(LoadedSkin);
name.SelectedIndexChanged += (o, a) =>
{
c.Field.SetValue(LoadedSkin, name.SelectedIndex);
CodepointValue += 75;
InvokeSetup(cat);
};
labelHeight = name.Height;
flbody.Controls.Add(name);
name.Show();
flbody.SetFlowBreak(name, true);
}
else
{
var width = new TextBox();
width.Width = 30;
width.Text = ((int)c.Field.GetValue(this.LoadedSkin)).ToString();
ControlManager.SetupControl(width);
labelHeight = width.Height;
flbody.Controls.Add(width);
width.Show();
EventHandler tc = (o, a) =>
{
try
EventHandler tc = (o, a) =>
{
int x = Convert.ToInt32(width.Text);
int oldx = ((int)c.Field.GetValue(this.LoadedSkin));
if (x != oldx)
try
{
c.Field.SetValue(LoadedSkin, x);
CodepointValue += 75;
int x = Convert.ToInt32(width.Text);
int y = Convert.ToInt32(height.Text);
int oldx = ((Size)c.Field.GetValue(this.LoadedSkin)).Width;
int oldy = ((Size)c.Field.GetValue(this.LoadedSkin)).Height;
if (x != oldx || y != oldy)
{
c.Field.SetValue(LoadedSkin, new Size(x, y));
CodepointValue += 200;
}
}
}
catch
catch
{
width.Text = ((Size)c.Field.GetValue(this.LoadedSkin)).Width.ToString();
height.Text = ((Size)c.Field.GetValue(this.LoadedSkin)).Height.ToString();
}
InvokeSetup(cat);
};
width.TextChanged += tc;
height.TextChanged += tc;
});
}
else if (c.Field.FieldType == typeof(bool))
{
Desktop.InvokeOnWorkerThread(() =>
{
var check = new CheckBox();
check.Checked = ((bool)c.Field.GetValue(LoadedSkin));
labelHeight = check.Height;
check.CheckedChanged += (o, a) =>
{
width.Text = ((int)c.Field.GetValue(this.LoadedSkin)).ToString();
c.Field.SetValue(LoadedSkin, check.Checked);
CodepointValue += 50;
InvokeSetup(cat);
};
flbody.SetFlowBreak(check, true);
flbody.Controls.Add(check);
check.Show();
});
}
else if (c.Field.FieldType == typeof(Font))
{
Desktop.InvokeOnWorkerThread(() =>
{
var name = new ComboBox();
var size = new TextBox();
var style = new ComboBox();
name.Width = 120;
labelHeight = name.Height;
size.Width = 40;
style.Width = 80;
flbody.SetFlowBreak(style, true);
ControlManager.SetupControl(name);
ControlManager.SetupControl(size);
ControlManager.SetupControl(style);
//populate the font name box
foreach (var font in FontFamily.Families)
{
name.Items.Add(font.Name);
}
InvokeSetup(cat);
name.Text = ((Font)c.Field.GetValue(LoadedSkin)).Name;
};
size.Text = ((Font)c.Field.GetValue(LoadedSkin)).Size.ToString();
width.TextChanged += tc;
flbody.SetFlowBreak(width, true);
//populate the style box
foreach (var s in (FontStyle[])Enum.GetValues(typeof(FontStyle)))
{
style.Items.Add(s.ToString());
}
style.Text = ((Font)c.Field.GetValue(LoadedSkin)).Style.ToString();
name.SelectedIndexChanged += (o, a) =>
{
var en = (FontStyle[])Enum.GetValues(typeof(FontStyle));
var f = en[style.SelectedIndex];
c.Field.SetValue(LoadedSkin, new Font(name.Text, (float)Convert.ToDouble(size.Text), f));
CodepointValue += 100;
InvokeSetup(cat);
};
style.SelectedIndexChanged += (o, a) =>
{
var en = (FontStyle[])Enum.GetValues(typeof(FontStyle));
var f = en[style.SelectedIndex];
c.Field.SetValue(LoadedSkin, new Font(name.Text, (float)Convert.ToDouble(size.Text), f));
CodepointValue += 50;
InvokeSetup(cat);
};
size.TextChanged += (o, a) =>
{
try
{
var en = (FontStyle[])Enum.GetValues(typeof(FontStyle));
var f = en[style.SelectedIndex];
c.Field.SetValue(LoadedSkin, new Font(name.Text, (float)Convert.ToDouble(size.Text), f));
}
catch
{
size.Text = ((Font)c.Field.GetValue(LoadedSkin)).Size.ToString();
}
CodepointValue += 50;
InvokeSetup(cat);
};
flbody.Controls.Add(name);
flbody.Controls.Add(size);
flbody.Controls.Add(style);
name.Show();
size.Show();
style.Show();
});
}
else if (c.Field.FieldType == typeof(Color))
{
Desktop.InvokeOnWorkerThread(() =>
{
var color = new Button();
color.Width = 40;
labelHeight = color.Height;
//just so it's flat like the system.
ControlManager.SetupControl(color);
color.BackColor = ((Color)c.Field.GetValue(LoadedSkin));
color.Click += (o, a) =>
{
AppearanceManager.SetupDialog(new ColorPicker((Color)c.Field.GetValue(LoadedSkin), c.Name, new Action<Color>((col) =>
{
color.BackColor = col;
c.Field.SetValue(LoadedSkin, col);
CodepointValue += 300;
InvokeSetup(cat);
})));
};
flbody.SetFlowBreak(color, true);
color.Tag = "keepbg";
flbody.Controls.Add(color);
color.Show();
});
}
else if (c.Field.FieldType.IsEnum == true)
{
Desktop.InvokeOnWorkerThread(() =>
{
var cBox = new ComboBox();
cBox.Width = 150;
ControlManager.SetupControl(cBox);
foreach (var itm in Enum.GetNames(c.Field.FieldType))
{
cBox.Items.Add(itm);
}
cBox.Text = c.Field.GetValue(LoadedSkin).ToString();
cBox.SelectedIndexChanged += (o, a) =>
{
c.Field.SetValue(LoadedSkin, Enum.Parse(c.Field.FieldType, cBox.Text));
InvokeSetup(cat);
};
labelHeight = cBox.Height;
flbody.Controls.Add(cBox);
cBox.Show();
flbody.SetFlowBreak(cBox, true);
});
}
else if (c.Field.FieldType == typeof(int))
{
Desktop.InvokeOnWorkerThread(() =>
{
if (c.Field.HasShifterEnumMask())
{
var name = new ComboBox();
name.Width = 120;
ControlManager.SetupControl(name);
string[] items = c.Field.GetShifterEnumMask();
foreach (var item in items)
{
name.Items.Add(item);
}
name.SelectedIndex = (int)c.Field.GetValue(LoadedSkin);
name.SelectedIndexChanged += (o, a) =>
{
c.Field.SetValue(LoadedSkin, name.SelectedIndex);
CodepointValue += 75;
InvokeSetup(cat);
};
labelHeight = name.Height;
flbody.Controls.Add(name);
name.Show();
flbody.SetFlowBreak(name, true);
}
else
{
var width = new TextBox();
width.Width = 30;
width.Text = ((int)c.Field.GetValue(this.LoadedSkin)).ToString();
ControlManager.SetupControl(width);
labelHeight = width.Height;
flbody.Controls.Add(width);
width.Show();
EventHandler tc = (o, a) =>
{
try
{
int x = Convert.ToInt32(width.Text);
int oldx = ((int)c.Field.GetValue(this.LoadedSkin));
if (x != oldx)
{
c.Field.SetValue(LoadedSkin, x);
CodepointValue += 75;
}
}
catch
{
width.Text = ((int)c.Field.GetValue(this.LoadedSkin)).ToString();
}
InvokeSetup(cat);
};
width.TextChanged += tc;
flbody.SetFlowBreak(width, true);
}
});
}
Desktop.InvokeOnWorkerThread(() =>
{
lbl.AutoSize = false;
lbl.Width = (int)this.CreateGraphics().MeasureString(lbl.Text, SkinEngine.LoadedSkin.MainFont).Width + 15;
lbl.Height = labelHeight;
lbl.TextAlign = ContentAlignment.MiddleLeft;
});
if (!string.IsNullOrWhiteSpace(c.Description))
{
Desktop.InvokeOnWorkerThread(() =>
{
var desc = new Label();
flbody.SetFlowBreak(desc, true);
desc.Text = c.Description;
desc.AutoSize = true;
flbody.Controls.Add(desc);
desc.Show();
});
}
}
lbl.AutoSize = false;
lbl.Width = (int)this.CreateGraphics().MeasureString(lbl.Text, SkinEngine.LoadedSkin.MainFont).Width + 15;
lbl.Height = labelHeight;
lbl.TextAlign = ContentAlignment.MiddleLeft;
if (!string.IsNullOrWhiteSpace(c.Description))
{
var desc = new Label();
flbody.SetFlowBreak(desc, true);
desc.Text = c.Description;
desc.AutoSize = true;
flbody.Controls.Add(desc);
desc.Show();
}
}
}).Start();
}
public ImageLayout GetLayout(string name)

View file

@ -442,6 +442,10 @@ namespace ShiftOS.WinForms.Applications
}).Start();
}
public static string RemoteSystemName { get; set; }
public static string RemoteUser { get; set; }
public static string RemotePass { get; set; }
[Story("first_steps")]
public static void FirstSteps()
{

View file

@ -0,0 +1,161 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using ShiftOS.Engine;
using ShiftOS.Objects;
namespace ShiftOS.WinForms.Servers
{
[Namespace("rts")]
[Server("Remote Terminal Server", 21)]
//[RequiresUpgrade("story_hacker101_breakingthebonds")] //Uncomment when story is implemented.
public class RemoteTerminalServer : Server
{
public void MessageReceived(ServerMessage msg)
{
var rtsMessage = JsonConvert.DeserializeObject<RTSMessage>(msg.Contents);
if (msg.Name == "disconnected")
{
if (Applications.Terminal.IsInRemoteSystem == true)
{
if (Applications.Terminal.RemoteSystemName == rtsMessage.SenderSystemName)
{
if(Applications.Terminal.RemoteUser == rtsMessage.Username)
if(Applications.Terminal.RemotePass == rtsMessage.Password)
{
Applications.Terminal.IsInRemoteSystem = false;
Applications.Terminal.RemoteSystemName = "";
Applications.Terminal.RemoteUser = "";
Applications.Terminal.RemotePass = "";
TerminalBackend.PrefixEnabled = true;
TerminalBackend.PrintPrompt();
}
}
}
return;
}
string currentUserName = SaveSystem.CurrentUser.Username;
var user = SaveSystem.CurrentSave.Users.FirstOrDefault(x => x.Username == rtsMessage.Username && x.Password == rtsMessage.Password);
if(user == null)
{
ServerManager.SendMessageToIngameServer(rtsMessage.SenderSystemName, 0, "Access denied.", "The username and password you have provided was denied.");
return;
}
else
{
SaveSystem.CurrentUser = user;
string cmd = rtsMessage.Namespace + "." + rtsMessage.Command + JsonConvert.SerializeObject(rtsMessage.Arguments);
TerminalBackend.InvokeCommand(cmd, true);
ServerManager.SendMessageToIngameServer(rtsMessage.SenderSystemName, 1, "writeline", TerminalBackend.LastCommandBuffer);
ServerManager.SendMessageToIngameServer(rtsMessage.SenderSystemName, 1, "write", $"{rtsMessage.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ");
SaveSystem.CurrentUser = SaveSystem.Users.FirstOrDefault(x => x.Username == currentUserName);
}
}
[Command("connect")]
[RequiresArgument("sysname")]
[RequiresArgument("username")]
[RequiresArgument("password")]
public static bool Connect(Dictionary<string, object> args)
{
string sysname = args["sysname"].ToString();
string username = args["username"].ToString();
string password = args["password"].ToString();
bool connectionFinished = false;
new Thread(() =>
{
Thread.Sleep(10000);
if (connectionFinished == false)
{
Applications.Terminal.IsInRemoteSystem = false;
Applications.Terminal.RemoteSystemName = "";
Applications.Terminal.RemoteUser = "";
Applications.Terminal.RemotePass = "";
TerminalBackend.PrefixEnabled = true;
Console.WriteLine("[rts] Connection failed, target system did not respond.");
TerminalBackend.PrintPrompt();
}
}).Start();
ServerMessageReceived smr = null;
smr = (msg) =>
{
if (msg.Name == "msgtosys")
{
var m = JsonConvert.DeserializeObject<ServerMessage>(msg.Contents);
if (m.GUID.Split('|')[2] != ServerManager.thisGuid.ToString())
{
connectionFinished = true;
ServerManager.MessageReceived -= smr;
}
}
};
ServerManager.MessageReceived += smr;
ServerManager.SendMessageToIngameServer(sysname, 21, "cmd", JsonConvert.SerializeObject(new RTSMessage
{
SenderSystemName = SaveSystem.CurrentSave.SystemName,
Username = username,
Password = password,
Namespace = "trm",
Command = "clear"
}));
Applications.Terminal.IsInRemoteSystem = true;
Applications.Terminal.RemoteSystemName = sysname;
Applications.Terminal.RemoteUser = username;
Applications.Terminal.RemotePass = password;
TerminalBackend.PrefixEnabled = false;
return true;
}
}
[Server("Generic port 0", 0)]
public class InfoboxServer : Server
{
public void MessageReceived(ServerMessage msg)
{
Infobox.Show(msg.Name, msg.Contents);
}
}
[Server("Generic port 1", 1)]
public class ConsoleServer : Server
{
public void MessageReceived(ServerMessage msg)
{
switch (msg.Name)
{
case "write":
Console.Write(msg.Contents);
break;
case "writeline":
Console.WriteLine(msg.Contents);
break;
}
}
}
public class RTSMessage
{
public string SenderSystemName { get; set; }
public string Namespace { get; set; }
public string Command { get; set; }
public Dictionary<string, object> Arguments { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
}

View file

@ -363,6 +363,7 @@
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="ScriptingTestFunctions.cs" />
<Compile Include="Servers\RemoteTerminalServer.cs" />
<Compile Include="ShiftnetSites\AppscapeMain.cs">
<SubType>UserControl</SubType>
</Compile>

View file

@ -144,10 +144,7 @@ namespace ShiftOS.WinForms.Tools
public static void SetupControl(Control ctrl)
{
Desktop.InvokeOnWorkerThread(new Action(() =>
{
ctrl.SuspendLayout();
}));
if (!(ctrl is MenuStrip) && !(ctrl is ToolStrip) && !(ctrl is StatusStrip) && !(ctrl is ContextMenuStrip))
{
string tag = "";
@ -306,7 +303,6 @@ namespace ShiftOS.WinForms.Tools
{
MakeDoubleBuffered(ctrl);
ctrl.ResumeLayout();
});
ControlSetup?.Invoke(ctrl);
}
@ -330,17 +326,18 @@ namespace ShiftOS.WinForms.Tools
public static void SetupControls(Control frm, bool runInThread = true)
{
SetupControl(frm);
frm.Click += (o, a) =>
{
Desktop.HideAppLauncher();
};
ThreadStart ts = () =>
{
for (int i = 0; i < frm.Controls.Count; i++)
var ctrls = frm.Controls.ToList();
for (int i = 0; i < ctrls.Count(); i++)
{
SetupControls(frm.Controls[i], false);
SetupControls(ctrls[i]);
}
SetupControl(frm);
};

View file

@ -117,10 +117,6 @@ namespace ShiftOS.Engine
}
List<string> orphaned = new List<string>();
if (Utils.FileExists("0:/dev_orphaned_lang.txt"))
{
orphaned = JsonConvert.DeserializeObject<List<string>>(Utils.ReadAllText("0:/dev_orphaned_lang.txt")); // if this file exists read from it and put in list orphaned
}
int start_index = 0;

View file

@ -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
{
/// <summary>
/// Occurs when someone sends a message to the server.
/// </summary>
/// <param name="msg">The message from the client.</param>
void MessageReceived(ServerMessage msg);
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false)]
public class ServerAttribute : Attribute
{
public ServerAttribute(string name, int port)
{
Name = name;
Port = port;
}
/// <summary>
/// Gets the name of the server.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets the port of the server.
/// </summary>
public int Port { get; }
}
}

View file

@ -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
/// </summary>
public static event Action<string> 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<ServerMessage>(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);

View file

@ -124,6 +124,7 @@
</Compile>
<Compile Include="SaveSystem.cs" />
<Compile Include="Scripting.cs" />
<Compile Include="Server.cs" />
<Compile Include="ServerManager.cs" />
<Compile Include="ShiftnetSite.cs" />
<Compile Include="Shiftorium.cs" />

View file

@ -84,6 +84,11 @@ namespace ShiftOS.Engine
/// </summary>
public static string LastCommand = "";
/// <summary>
/// Gets the output of the last command.
/// </summary>
public static string LastCommandBuffer { get; private set; }
/// <summary>
/// Invokes a ShiftOS terminal command.
/// </summary>
@ -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);
}