aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael <[email protected]>2017-02-09 20:32:53 -0500
committerMichael <[email protected]>2017-02-09 20:32:53 -0500
commitde14574800ed21765e1ac6bba1d19b774f477c29 (patch)
treeb9d5ce4ce01e4e930e546ff23fc70a7ad32356bf
parentd6b2b155264e7a4b576c956530915f66d97b3eac (diff)
downloadshiftos_thereturn-de14574800ed21765e1ac6bba1d19b774f477c29.tar.gz
shiftos_thereturn-de14574800ed21765e1ac6bba1d19b774f477c29.tar.bz2
shiftos_thereturn-de14574800ed21765e1ac6bba1d19b774f477c29.zip
Compress shop items on server-side.
-rw-r--r--ShiftOS.Server/Program.cs41
-rw-r--r--ShiftOS.WinForms/Applications/Downloader.cs30
-rw-r--r--ShiftOS.WinForms/Applications/MUDControlCentre.cs4
3 files changed, 67 insertions, 8 deletions
diff --git a/ShiftOS.Server/Program.cs b/ShiftOS.Server/Program.cs
index c468503..352214d 100644
--- a/ShiftOS.Server/Program.cs
+++ b/ShiftOS.Server/Program.cs
@@ -34,6 +34,7 @@ using Newtonsoft.Json;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
+using System.IO.Compression;
namespace ShiftOS.Server
{
@@ -223,11 +224,39 @@ namespace ShiftOS.Server
File.WriteAllText(fPath, Encryption.Encrypt(contents));
}
- /// <summary>
- /// Interpret the specified msg.
- /// </summary>
- /// <param name="msg">Message.</param>
- public static void Interpret(ServerMessage msg)
+ public static string Compress(string s)
+ {
+ var bytes = Encoding.Unicode.GetBytes(s);
+ using (var msi = new MemoryStream(bytes))
+ using (var mso = new MemoryStream())
+ {
+ using (var gs = new GZipStream(mso, CompressionMode.Compress))
+ {
+ msi.CopyTo(gs);
+ }
+ return Convert.ToBase64String(mso.ToArray());
+ }
+ }
+
+ public static string Decompress(string s)
+ {
+ var bytes = Convert.FromBase64String(s);
+ using (var msi = new MemoryStream(bytes))
+ using (var mso = new MemoryStream())
+ {
+ using (var gs = new GZipStream(msi, CompressionMode.Decompress))
+ {
+ gs.CopyTo(mso);
+ }
+ return Encoding.Unicode.GetString(mso.ToArray());
+ }
+ }
+
+ /// <summary>
+ /// Interpret the specified msg.
+ /// </summary>
+ /// <param name="msg">Message.</param>
+ public static void Interpret(ServerMessage msg)
{
Dictionary<string, object> args = null;
@@ -465,7 +494,7 @@ Contents:
Contents = JsonConvert.SerializeObject(new
{
shop = shopName,
- itemdata = item
+ itemdata = Compress(Compress(JsonConvert.SerializeObject(item)))
})
}));
}
diff --git a/ShiftOS.WinForms/Applications/Downloader.cs b/ShiftOS.WinForms/Applications/Downloader.cs
index dfa2425..2f33462 100644
--- a/ShiftOS.WinForms/Applications/Downloader.cs
+++ b/ShiftOS.WinForms/Applications/Downloader.cs
@@ -12,6 +12,8 @@ using ShiftOS.Engine;
using Newtonsoft.Json;
using ShiftOS.WinForms.Controls;
using ShiftOS.WinForms.Tools;
+using System.IO;
+using System.IO.Compression;
namespace ShiftOS.WinForms.Applications
{
@@ -113,6 +115,34 @@ namespace ShiftOS.WinForms.Applications
public static event Action<Download> DownloadStarted;
+ public static string Compress(string s)
+ {
+ var bytes = Encoding.Unicode.GetBytes(s);
+ using (var msi = new MemoryStream(bytes))
+ using (var mso = new MemoryStream())
+ {
+ using (var gs = new GZipStream(mso, CompressionMode.Compress))
+ {
+ msi.CopyTo(gs);
+ }
+ return Convert.ToBase64String(mso.ToArray());
+ }
+ }
+
+ public static string Decompress(string s)
+ {
+ var bytes = Convert.FromBase64String(s);
+ using (var msi = new MemoryStream(bytes))
+ using (var mso = new MemoryStream())
+ {
+ using (var gs = new GZipStream(msi, CompressionMode.Decompress))
+ {
+ gs.CopyTo(mso);
+ }
+ return Encoding.Unicode.GetString(mso.ToArray());
+ }
+ }
+
public static void StartDownload(Download down)
{
var t = new Thread(() =>
diff --git a/ShiftOS.WinForms/Applications/MUDControlCentre.cs b/ShiftOS.WinForms/Applications/MUDControlCentre.cs
index 67c6c17..6336718 100644
--- a/ShiftOS.WinForms/Applications/MUDControlCentre.cs
+++ b/ShiftOS.WinForms/Applications/MUDControlCentre.cs
@@ -142,10 +142,10 @@ namespace ShiftOS.WinForms.Applications
}
else if(msg.Name == "shop_additem")
{
- var contents = JsonConvert.DeserializeObject<Dictionary<string, object>>(msg.Contents);
+ var contents = JsonConvert.DeserializeObject<Dictionary<string, string>>(msg.Contents);
if((string)contents["shop"] == CurrentShop.Name)
{
- CurrentShop.Items.Add(JsonConvert.DeserializeObject<ShopItem>(JsonConvert.SerializeObject(contents["itemdata"])));
+ CurrentShop.Items.Add(JsonConvert.DeserializeObject<ShopItem>(DownloadManager.Decompress(DownloadManager.Decompress(contents["itemdata"] as string))));
this.Invoke(new Action(PopulateShopView));
}
}