aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/Applications/Shiftnet.cs
diff options
context:
space:
mode:
Diffstat (limited to 'ShiftOS.WinForms/Applications/Shiftnet.cs')
-rw-r--r--ShiftOS.WinForms/Applications/Shiftnet.cs301
1 files changed, 105 insertions, 196 deletions
diff --git a/ShiftOS.WinForms/Applications/Shiftnet.cs b/ShiftOS.WinForms/Applications/Shiftnet.cs
index 54a8aa6..7f5d3c1 100644
--- a/ShiftOS.WinForms/Applications/Shiftnet.cs
+++ b/ShiftOS.WinForms/Applications/Shiftnet.cs
@@ -35,6 +35,8 @@ using ShiftOS.Engine;
using Newtonsoft.Json;
using static ShiftOS.Engine.SkinEngine;
using ShiftOS.WinForms.Tools;
+using System.IO;
+using System.Reflection;
namespace ShiftOS.WinForms.Applications
{
@@ -50,216 +52,34 @@ namespace ShiftOS.WinForms.Applications
[WinOpen("shiftnet")]
[RequiresUpgrade("victortran_shiftnet")]
[DefaultIcon("iconShiftnet")]
- public partial class Shiftnet : UserControl, IShiftOSWindow
+ public partial class Shiftnet : UserControl, IShiftOSWindow, IShiftnetClient
{
public Shiftnet()
{
InitializeComponent();
- ServerManager.MessageReceived += (msg) =>
- {
- try
- {
- if (msg.Name == "shiftnet_file")
- {
- if (Objects.ShiftFS.Utils.FileExists("0:/md.txt"))
- {
- this.Invoke(new Action(() =>
- {
- wbcanvas.DocumentText = ConstructHtml(Objects.ShiftFS.Utils.ReadAllText("0:/md.txt"));
- }));
- }
- else
- {
- this.Invoke(new Action(() =>
- {
- wbcanvas.DocumentText = ConstructHtml(msg.Contents);
- }));
- }
- }
- }
- catch
- {
-
- }
- };
- }
-
- public string ConstructHtml(string markdown)
- {
- var TerminalForeColor = ControlManager.ConvertColor(SkinEngine.LoadedSkin.TerminalForeColorCC);
- var TerminalBackColor = ControlManager.ConvertColor(SkinEngine.LoadedSkin.TerminalBackColorCC);
- string html = $@"<html>
- <head>
- <style>
- body {{
- background-color: rgb({LoadedSkin.ControlColor.R}, {LoadedSkin.ControlColor.G}, {LoadedSkin.ControlColor.B});
- color: rgb({LoadedSkin.ControlTextColor.R}, {LoadedSkin.ControlTextColor.G}, {LoadedSkin.ControlTextColor.B});
- font-family: ""{LoadedSkin.MainFont.Name}"";
- font-size: {LoadedSkin.MainFont.SizeInPoints}pt;
- }}
-
- h1 {{
- font-family: ""{LoadedSkin.HeaderFont.Name}"";
- font-size: {LoadedSkin.HeaderFont.SizeInPoints}pt;
- }}
-
- h2 {{
- font-family: ""{LoadedSkin.Header2Font.Name}"";
- font-size: {LoadedSkin.Header2Font.SizeInPoints}pt;
- }}
-
- h3 {{
- font-family: ""{LoadedSkin.Header3Font.Name}"";
- font-size: {LoadedSkin.Header3Font.SizeInPoints}pt;
- }}
-
- pre, code {{
- font-family: ""{LoadedSkin.TerminalFont.Name}"";
- font-size: {LoadedSkin.TerminalFont.SizeInPoints}pt;
- color: rgb({TerminalForeColor.R}, {TerminalForeColor.G}, {TerminalForeColor.B});
- background-color: rgb({TerminalBackColor.R}, {TerminalBackColor.G}, {TerminalBackColor.B});
- }}
- </style>
- </head>
- <body>
- <markdown/>
- </body>
-</html>";
-
- string body = CommonMark.CommonMarkConverter.Convert(markdown);
- for (int i = 0; i <= Encoding.UTF8.GetBytes(body).Length; i += DownloadManager.GetDownloadSpeed())
- {
- //halt the page load until 'download' finishes.
- }
- html = html.Replace("<markdown/>", body);
- return html;
}
public string CurrentUrl { get; set; }
- private void wbcanvas_Navigating(object sender, WebBrowserNavigatingEventArgs e)
- {
- string Url = e.Url.ToString().Replace("http://", "");
- if (CurrentUrl != Url.ToString() && !Url.ToString().StartsWith("about:"))
- {
- e.Cancel = true;
- Future.Clear();
- if (Url.StartsWith("runsyscmd/"))
- {
- ProcessShiftnetCmd(Url.Replace("runsyscmd/", ""));
- }
-
- ShiftnetNavigate(Url.ToString());
- }
- }
-
- public void ProcessShiftnetCmd(string cmd)
- {
- var args = cmd.Split('/');
- switch (args[0])
- {
- case "setsnsub":
- for (int i = 0; i < DownloadManager.GetAllSubscriptions().Length; i++)
- {
- if (DownloadManager.GetAllSubscriptions()[i].Name == args[1])
- {
- var sub = DownloadManager.GetAllSubscriptions()[i];
- Infobox.PromptYesNo("Shiftnet", $"Are you sure you want to switch your system's Shiftnet subscription to {sub.Name} by {sub.Company}?{Environment.NewLine}{Environment.NewLine}Cost per month: {sub.CostPerMonth} CP{Environment.NewLine}Download speed: {sub.DownloadSpeed} bytes per second", new Action<bool>((answer) =>
- {
- if (answer == true)
- {
- if (SaveSystem.CurrentSave.Codepoints >= sub.CostPerMonth)
- {
- //Initial fee gets deducted.
- SaveSystem.CurrentSave.Codepoints -= sub.CostPerMonth;
- //Then we set the subscription.
- SaveSystem.CurrentSave.ShiftnetSubscription = i;
- //Then we say that we have paid this month.
- SaveSystem.CurrentSave.LastMonthPaid = DateTime.Now.Month;
- //Then we send our save to the MUD.
- SaveSystem.SaveGame();
-
- }
- else
- {
- //User can't afford this subscription.
- Infobox.Show("Shiftnet - Not enough Codepoints", $"You cannot afford to pay for this subscription at this time. You need {sub.CostPerMonth - SaveSystem.CurrentSave.Codepoints} more Codepoints.");
- }
- }
- }));
- }
- }
- return;
- }
- }
-
public Stack<string> History = new Stack<string>();
public Stack<string> Future = new Stack<string>();
- public void ShiftnetNavigate(string Url, bool pushHistory = true)
- {
- if (Url.EndsWith(".rnp") || !Url.Contains("."))
- {
- if (!string.IsNullOrEmpty(CurrentUrl) && pushHistory)
- History.Push(CurrentUrl);
- CurrentUrl = Url;
- ServerManager.SendMessage("shiftnet_get", JsonConvert.SerializeObject(new
- {
- url = Url
- }));
- txturl.Text = Url;
-
- }
- else
- {
- ServerMessageReceived smr = null;
- smr = (msg) =>
- {
- if (msg.Name == "download_meta")
- {
- var bytes = JsonConvert.DeserializeObject<byte[]>(msg.Contents);
- string destPath = null;
- string ext = Url.Split('.')[Url.Split('.').Length - 1];
- this.Invoke(new Action(() =>
- {
- FileSkimmerBackend.GetFile(new[] { ext }, FileOpenerStyle.Save, new Action<string>((file) =>
- {
- destPath = file;
- }));
- }));
- while (string.IsNullOrEmpty(destPath))
- {
-
- }
- var d = new Download
- {
- ShiftnetUrl = Url,
- Destination = destPath,
- Bytes = bytes,
- Progress = 0,
- };
- DownloadManager.StartDownload(d);
- this.Invoke(new Action(() =>
- {
- AppearanceManager.SetupWindow(new Downloader());
- }));
- ServerManager.MessageReceived -= smr;
- }
- };
- ServerManager.MessageReceived += smr;
- ServerManager.SendMessage("download_start", Url);
- }
- }
+ public IShiftnetSite CurrentPage = null;
public void OnLoad()
{
- ShiftnetNavigate("shiftnet/main");
+ NavigateToUrl("shiftnet/main");
}
public void OnSkinLoad()
{
- ShiftnetNavigate(CurrentUrl);
+ CurrentPage?.OnSkinLoad();
+ btnback.Location = new Point(2, 2);
+ btnforward.Location = new Point(btnback.Left + btnback.Width + 2, 2);
+ txturl.Location = new Point(btnforward.Left + btnforward.Width + 2, 2);
+ txturl.Width = flcontrols.Width - btnback.Width - 2 - btnforward.Width - 2 - (btngo.Width*2) - 2;
+ btngo.Location = new Point(flcontrols.Width - btngo.Width - 2, 2);
}
public bool OnUnload()
@@ -269,6 +89,7 @@ namespace ShiftOS.WinForms.Applications
public void OnUpgrade()
{
+ CurrentPage?.OnUpgrade();
}
private void btnback_Click(object sender, EventArgs e)
@@ -279,7 +100,7 @@ namespace ShiftOS.WinForms.Applications
if (!string.IsNullOrEmpty(hist))
{
Future.Push(hist);
- ShiftnetNavigate(hist, false);
+ NavigateToUrl(hist);
}
}
catch
@@ -295,7 +116,8 @@ namespace ShiftOS.WinForms.Applications
string fut = Future.Pop();
if (!string.IsNullOrEmpty(fut))
{
- ShiftnetNavigate(fut);
+ History.Push(CurrentUrl);
+ NavigateToUrl(fut);
}
}
catch
@@ -309,8 +131,8 @@ namespace ShiftOS.WinForms.Applications
if (!string.IsNullOrWhiteSpace(txturl.Text))
{
Future.Clear();
-
- ShiftnetNavigate(txturl.Text);
+ History.Push(CurrentUrl);
+ NavigateToUrl(txturl.Text);
}
}
@@ -323,8 +145,95 @@ namespace ShiftOS.WinForms.Applications
}
}
- private void wbcanvas_Navigated(object sender, WebBrowserNavigatedEventArgs e)
+ public void NavigateToUrl(string url)
+ {
+ txturl.Text = url;
+ foreach(var exe in Directory.GetFiles(Environment.CurrentDirectory))
+ {
+ if(exe.EndsWith(".exe") || exe.EndsWith(".dll"))
+ {
+ try
+ {
+ var asm = Assembly.LoadFile(exe);
+ foreach (var type in asm.GetTypes())
+ {
+ if (type.GetInterfaces().Contains(typeof(IShiftnetSite)))
+ {
+ if (type.BaseType == typeof(UserControl))
+ {
+ var attribute = type.GetCustomAttributes(false).FirstOrDefault(x => x is ShiftnetSiteAttribute) as ShiftnetSiteAttribute;
+ if (attribute != null)
+ {
+ if (attribute.Url == url)
+ {
+ if (Shiftorium.UpgradeAttributesUnlocked(type))
+ {
+ var obj = (IShiftnetSite)Activator.CreateInstance(type, null);
+ obj.GoToUrl += (u) =>
+ {
+ History.Push(u);
+ NavigateToUrl(u);
+ };
+ obj.GoBack += () =>
+ {
+ string u = History.Pop();
+ Future.Push(u);
+ NavigateToUrl(u);
+ };
+ CurrentPage = obj;
+ this.pnlcanvas.Controls.Clear();
+ this.pnlcanvas.Controls.Add((UserControl)obj);
+ ((UserControl)obj).Show();
+ ((UserControl)obj).Dock = DockStyle.Fill;
+ obj.OnUpgrade();
+ obj.OnSkinLoad();
+ obj.Setup();
+ return;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ pnlcanvas.Controls.Clear();
+ var tlbl = new Label();
+ tlbl.Text = "Server error in \"" + url + "\" application.";
+ tlbl.Tag = "header1";
+ tlbl.AutoSize = true;
+ tlbl.Location = new Point(10, 10);
+ tlbl.Dock = DockStyle.Top;
+ pnlcanvas.Controls.Add(tlbl);
+ tlbl.Show();
+
+ var crash = new Label();
+ crash.Dock = DockStyle.Fill;
+ crash.AutoSize = false;
+ crash.Text = ex.ToString();
+ pnlcanvas.Controls.Add(crash);
+ crash.Show();
+ crash.BringToFront();
+ ControlManager.SetupControls(pnlcanvas);
+ return;
+ }
+ }
+ }
+ pnlcanvas.Controls.Clear();
+ var lbl = new Label();
+ lbl.Text = "Page not found!";
+ lbl.Tag = "header1";
+ lbl.AutoSize = true;
+ lbl.Location = new Point(10, 10);
+ pnlcanvas.Controls.Add(lbl);
+ lbl.Show();
+
+ }
+
+ public void RefreshSite()
{
+ NavigateToUrl(CurrentUrl);
}
}
}