public static LoadSaveResult SaveLoadouts(string filename = "loads.json") { LineLog.Debug($"Saving loads to {filename}"); if (Loads.Count > 0) { try { // keep a single recent backup if (File.Exists(filename)) { File.Copy(filename, filename + ".backup", true); } var str = JsonConvert.SerializeObject(Loads, Formatting.Indented); File.WriteAllText(filename, str); return(LoadSaveResult.Success); } catch (Exception e) { LineLog.Error($"Error while saving loads {e.GetType()}", e); throw; } } return(LoadSaveResult.EmptyFile); }
public static LoadSaveResult LoadBuilds(string filename = "builds.json") { if (!File.Exists(filename)) { LineLog.Error($"{filename} wasn't found."); return(LoadSaveResult.FileNotFound); } LineLog.Debug($"Loading {filename} as builds."); var bstr = File.ReadAllText(filename); return(LoadBuildData(bstr)); }
public static LoadSaveResult SaveBuilds(string filename = "builds.json") { LineLog.Debug($"Saving builds to {filename}"); // TODO: fix this mess foreach (Build bb in Builds) { if (bb.Mon != null && bb.Mon.FullName != "Missingno") { if (!bb.DownloadAwake || (Program.Data.GetMonster(bb.Mon.FullName) != null && Program.Data.GetMonster(bb.Mon.FullName).FullName != "Missingno")) { bb.MonName = bb.Mon.FullName; bb.MonId = bb.Mon.Id; } else { if (Program.Data.GetMonster(bb.Mon.Id).FullName != "Missingno") { bb.MonId = bb.Mon.Id; bb.MonName = Program.Data.GetMonster(bb.Mon.Id).FullName; } } } } // only write if there are builds, may save some files if (Program.Builds.Count > 0) { try { // keep a single recent backup if (File.Exists(filename)) { File.Copy(filename, filename + ".backup", true); } var str = JsonConvert.SerializeObject(Program.Builds, Formatting.Indented); File.WriteAllText(filename, str); return(LoadSaveResult.Success); } catch (Exception e) { LineLog.Error($"Error while saving builds {e.GetType()}", e); throw; //MessageBox.Show(e.ToString()); } } return(LoadSaveResult.Failure); }
/// <summary> /// Dispatches a thread to listen for the incoming Remote App connection /// </summary> public void Start() { try { try { //if (!NetAclChecker.HasFirewall("WebSocketMulti", true, true, true, 80)) // NetAclChecker.AddFirewall("WebSocketMulti", true, true, true, 80); //if (!NetAclChecker.HasFirewall("WebSocketMulti", true, true, true, 81)) // NetAclChecker.AddFirewall("WebSocketMulti", true, true, true, 81); NetAclChecker.AddAddress($"http://*:7676/"); listener = new HttpListener(); listener.Prefixes.Add("http://*:7676/"); listener.Start(); } catch { LineLog.Error("Failed to bind to *, binding to localhost"); listener = new HttpListener(); listener.Prefixes.Add("http://localhost:7676/"); listener.Start(); } LineLog.Info("Server is listening on " + listener.Prefixes.First()); isRunning = true; } catch (Exception e) { LineLog.Error("Failed to start server", e); throw; } Task.Factory.StartNew(() => { try { while (isRunning) { //LineLog.Info("Waiting for a connection..."); var context = listener.GetContext(); new Thread(() => RemoteManageLoop(context)).Start(); } } catch (Exception e) { LineLog.Error("Failed while running server", e); } isRunning = false; }, TaskCreationOptions.LongRunning); }
public static LoadSaveResult SaveGoals(string filename = "goals.json") { LineLog.Debug($"Saving loads to {filename}"); try { // keep a single recent backup var str = JsonConvert.SerializeObject(Goals, Formatting.Indented); File.WriteAllText(filename, str); return(LoadSaveResult.Success); } catch (Exception e) { LineLog.Error($"Error while saving loads {e.GetType()}", e); throw; } }
public static LoadSaveResult LoadLoadouts(string filename = "loads.json") { try { string text = File.ReadAllText(filename); var lloads = JsonConvert.DeserializeObject <Loadout[]>(text); Loads.Clear(); foreach (var load in lloads) { if (load.RuneIDs != null) { for (int i = 0; i < 6; i++) { var ids = load.RuneIDs[i]; load.Runes[i] = Program.Data.Runes.FirstOrDefault(r => r.Id == ids); if (load.Runes[i] != null) { load.Runes[i].UsedInBuild = true; if (load.HasManageStats) { foreach (var ms in load.ManageStats[i]) { load.Runes[i].ManageStats.AddOrUpdate(ms.Key, ms.Value, (s, d) => ms.Value); } } } } } load.Shrines = Data.Shrines; Loads.Add(load); } return(LoadSaveResult.Success); } catch (Exception e) { LineLog.Error($"Error while loading loads {e.GetType()}", e); //MessageBox.Show("Error occurred loading Save JSON.\r\n" + e.GetType() + "\r\nInformation is saved to error_save.txt"); File.WriteAllText("error_loads.txt", e.ToString()); throw; } }
public static LoadSaveResult LoadGoals(string filename = "goals.json") { try { LineLog.Debug("Loading goals"); if (File.Exists(filename)) { string text = File.ReadAllText(filename); Goals = JsonConvert.DeserializeObject <Goals>(text); } else { Goals = new Goals(); } return(LoadSaveResult.Success); } catch (Exception e) { LineLog.Error($"Error while loading goals {e.GetType()}", e); File.WriteAllText("error_goals.txt", e.ToString()); throw; } }
public async void RemoteManageLoop(HttpListenerContext context) { try { LineLog.Debug("serving: " + context.Request.RawUrl); var req = context.Request; var resp = context.Response; var msg = getResponse(req); resp.StatusCode = (int)msg.StatusCode; LineLog.Debug("returning: " + resp.StatusCode); foreach (var h in msg.Headers) { foreach (var v in h.Value) { resp.Headers.Add(h.Key, v); } } if (resp.StatusCode != 303) { using (var output = resp.OutputStream) { byte[] outBytes = Encoding.UTF8.GetBytes("Critical Failure"); bool expires = false; if (msg.Content is StringContent) { var qw = msg.Content as StringContent; string qq = await qw.ReadAsStringAsync(); resp.ContentType = mimeTypes.Where(t => req.Url.AbsolutePath.EndsWith(t.Key)).FirstOrDefault().Value; if (resp.ContentType == null) { resp.ContentType = "text/html"; // Default to HTML } outBytes = Encoding.UTF8.GetBytes(qq); } else if (msg.Content is FileContent) { var qw = msg.Content as FileContent; resp.ContentType = qw.Type; if (resp.ContentType == null) { resp.ContentType = "application/octet-stream"; // Should always be set, but bin just incase } //resp.Headers.Add("Content-Disposition", $"attachment; filename=\"{qw.FileName}\""); outBytes = await qw.ReadAsByteArrayAsync(); } else if (msg.Content is ByteArrayContent) { var qw = msg.Content as ByteArrayContent; resp.ContentType = mimeTypes.Where(t => req.Url.AbsolutePath.EndsWith(t.Key)).FirstOrDefault().Value; if (resp.ContentType == null) { resp.ContentType = "application/octet-stream"; // Default to binary } outBytes = await qw.ReadAsByteArrayAsync(); } else if (msg.Content is StreamContent) { var qw = msg.Content as StreamContent; using (var ms = new MemoryStream()) { var stream = await qw.ReadAsStreamAsync(); stream.CopyTo(ms); //resp.ContentType = "application/octet-stream" outBytes = ms.ToArray(); } } //resp.Headers.Add("Content-language", "en-au"); resp.Headers.Add("Access-Control-Allow-Origin: *"); resp.Headers.Add("Access-Control-Allow-Methods: *"); if (expires) { resp.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate"); resp.Headers.Add("Pragma", "no-cache"); resp.Headers.Add("Expires", "Wed, 16 Jul 1969 13:32:00 UTC"); } if ((outBytes.Length > 0) && (resp.ContentType != "image/png")) // Don't compress empty responses or compressed file types { var enc = req.Headers.GetValues("Accept-Encoding"); if (enc?.Contains("gzip") ?? false) { using (MemoryStream ms = new MemoryStream()) using (System.IO.Compression.GZipStream gs = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress)) { gs.Write(outBytes, 0, outBytes.Length); gs.Flush(); gs.Close(); // https://stackoverflow.com/questions/3722192/how-do-i-use-gzipstream-with-system-io-memorystream#comment3929538_3722263 ms.Flush(); outBytes = ms.ToArray(); } resp.Headers.Add("Content-Encoding", "gzip"); } else if (enc?.Any(a => a.ToLowerInvariant() == "deflate") ?? false) { using (MemoryStream ms = new MemoryStream()) { using (System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Compress)) { ds.Write(outBytes, 0, outBytes.Length); ds.Flush(); } ms.Flush(); outBytes = ms.ToArray(); } resp.Headers.Add("Content-Encoding", "deflate"); } } resp.ContentLength64 = outBytes.Length; try { output.Write(outBytes, 0, outBytes.Length); } catch (Exception ex) { Program.LineLog.Error("Failed to write " + ex.GetType().ToString() + " " + ex.Message); } } } else { resp.OutputStream.Close(); } } catch (Exception e) { LineLog.Error("Something went wrong.", e); var resp = context.Response; resp.StatusCode = 500; using (var output = resp.OutputStream) { byte[] outBytes = Encoding.UTF8.GetBytes(e.GetType() + ": " + e.Message); resp.ContentLength64 = outBytes.Length; try { output.Write(outBytes, 0, outBytes.Length); } catch (Exception ex) { Program.LineLog.Error("Failed to write " + ex.GetType().ToString() + " " + ex.Message); } } } }