public static JsEncoder.TableValue ToTable(UserHostList Value) { JsEncoder.TableValue t = new JsEncoder.TableValue(); JsEncoder.TableValue ft = new JsEncoder.TableValue(); JsEncoder.TableValue rt = new JsEncoder.TableValue(); t[1] = ft; t[2] = rt; HostInformation[] fa = Value.FavoriteHosts.ToArray(); HostInformation[] ra = Value.RecentHosts.ToArray(); int fl = fa.Length; int rl = ra.Length; for (int i = 0; i < fl; i++) { JsEncoder.TableValue item = HostInformation.ToTable(fa[i]); ft[i + 1] = item; } for (int i = 0; i < rl; i++) { JsEncoder.TableValue item = HostInformation.ToTable(ra[i]); rt[i + 1] = item; } return(t); }
public void Save() { if (_IsLoaded) { DirectoryIO.CreateDirectory(DataSavePath); String HostListAsText = JsEncoder.EncoderStream.EncodeTable(UserHostList.ToTable(HostList)); String ConfigAsText; // This won't be null after the region below #region ConfigFileSaving { JsEncoder.TableValue ConfigTable = new JsEncoder.TableValue(); ConfigTable[new JsEncoder.StringValue("UserName")] = new JsEncoder.StringValue(UserName); ConfigTable[new JsEncoder.StringValue("WorkingPort")] = new JsEncoder.IntValue(WorkingPort); ConfigAsText = JsEncoder.EncoderStream.EncodeTable(ConfigTable); } #endregion String VersionFileBackupPath = String.Concat(VersionFilePath, ".bak"); String ConfigFileBackupPath = String.Concat(ConfigFilePath, ".bak"); String HostListFileBackupPath = String.Concat(HostListFilePath, ".bak"); if (FileIO.Exists(VersionFileBackupPath)) { FileIO.Delete(VersionFileBackupPath); } if (FileIO.Exists(ConfigFileBackupPath)) { FileIO.Delete(ConfigFileBackupPath); } if (FileIO.Exists(HostListFileBackupPath)) { FileIO.Delete(HostListFileBackupPath); } if (FileIO.Exists(VersionFilePath)) { FileIO.Move(VersionFilePath, VersionFileBackupPath); } if (FileIO.Exists(ConfigFilePath)) { FileIO.Move(ConfigFilePath, ConfigFileBackupPath); } if (FileIO.Exists(HostListFilePath)) { FileIO.Move(HostListFilePath, HostListFileBackupPath); } FileIO.WriteAllText(VersionFilePath, NetworkConfig.VersionString, System.Text.Encoding.Unicode); FileIO.WriteAllText(ConfigFilePath, ConfigAsText, System.Text.Encoding.Unicode); FileIO.WriteAllText(HostListFilePath, HostListAsText, System.Text.Encoding.Unicode); } }
public static UserHostList FromTable(JsEncoder.TableValue Value) { JsEncoder.TableValue ft = (JsEncoder.TableValue)Value[1]; JsEncoder.TableValue rt = (JsEncoder.TableValue)Value[2]; UserHostList r = new UserHostList(); for (int i = 1; ft.ContainsKey(i); i++) { JsEncoder.IAbstractValue item = ft[i]; JsEncoder.TableValue v = (JsEncoder.TableValue)item; r.FavoriteHosts.Add(HostInformation.FromTable(v)); } for (int i = 1; rt.ContainsKey(i); i++) { JsEncoder.IAbstractValue item = rt[i]; JsEncoder.TableValue v = (JsEncoder.TableValue)item; r.RecentHosts.Add(HostInformation.FromTable(v)); } return(r); }
public bool Load() { bool r = false; bool ve = FileIO.Exists(VersionFilePath); if (ve) { String VersionFileContents = FileIO.ReadAllText(VersionFilePath, System.Text.Encoding.Unicode); if (VersionFileContents == NetworkConfig.VersionString) { r = true; } else { String IncompatPath = String.Concat(DataSavePath, "_", VersionFileContents); if (DirectoryIO.Exists(IncompatPath)) { DirectoryIO.Delete(IncompatPath, true); } DirectoryIO.Move(DataSavePath, IncompatPath); System.Windows.Forms.MessageBox.Show(String.Concat( "The application tried to load Js ChatterBox files that were incompatible ", "with this version. They have been moved to \"", IncompatPath, "\".")); } } if (r) { try { #region ConfigFileLoading { String ConfigText = FileIO.ReadAllText(ConfigFilePath, System.Text.Encoding.Unicode); JsEncoder.TableValue ConfigTable = (JsEncoder.TableValue)JsEncoder.DecoderStream.DecodeValue(ConfigText); UserName = ((JsEncoder.StringValue)ConfigTable[new JsEncoder.StringValue("UserName")]).Value; WorkingPort = ((JsEncoder.IntValue)ConfigTable[new JsEncoder.StringValue("WorkingPort")]).Value; } #endregion String HostListFileContents = FileIO.ReadAllText(HostListFilePath, System.Text.Encoding.Unicode); JsEncoder.TableValue HostListTable = (JsEncoder.TableValue)JsEncoder.DecoderStream.DecodeValue(HostListFileContents); HostList = UserHostList.FromTable(HostListTable); } catch (Exception e) { String BaseIncompatPath = String.Concat(DataSavePath, "_Damaged<N>"); String FinalIncompatPath = null; int i = 0; while (FinalIncompatPath == null) { i++; FinalIncompatPath = BaseIncompatPath.Replace("<N>", i.ToString()); if (DirectoryIO.Exists(FinalIncompatPath)) { FinalIncompatPath = null; } } DirectoryIO.Move(DataSavePath, FinalIncompatPath); r = false; System.Windows.Forms.MessageBox.Show(String.Concat( "An error occured while loading your save files. They have been moved to \"", FinalIncompatPath, "\" and you will now get the defaults. ", "The error message is \"", e.Message, "\".")); } } if (!r) { UserName = "******"; HostList = new UserHostList(); WorkingPort = NetworkConfig.DefaultServerPort; } _IsLoaded = true; return(r); }