public void LoadOldSettings() { //Read XML settings try { XmlDocument xmlDoc = new XmlDocument(); if (File.Exists(backupOldSettingsFile) && !File.Exists(oldSettingsFile)) { DarkLog.Debug("[Settings]: Restoring old player settings file!"); File.Move(backupOldSettingsFile, oldSettingsFile); } xmlDoc.Load(oldSettingsFile); playerName = xmlDoc.SelectSingleNode("/settings/global/@username").Value; cacheSize = int.Parse(xmlDoc.SelectSingleNode("/settings/global/@cache-size").Value); disclaimerAccepted = Int32.Parse(xmlDoc.SelectSingleNode("/settings/global/@disclaimer").Value); string floatArrayString = xmlDoc.SelectSingleNode("/settings/global/@player-color").Value; string[] floatArrayStringSplit = floatArrayString.Split(','); float redColor = float.Parse(floatArrayStringSplit[0].Trim()); float greenColor = float.Parse(floatArrayStringSplit[1].Trim()); float blueColor = float.Parse(floatArrayStringSplit[2].Trim()); //Bounds checking - Gotta check up on those players :) if (redColor < 0f) { redColor = 0f; } if (redColor > 1f) { redColor = 1f; } if (greenColor < 0f) { greenColor = 0f; } if (greenColor > 1f) { greenColor = 1f; } if (blueColor < 0f) { blueColor = 0f; } if (blueColor > 1f) { blueColor = 1f; } playerColor = new Color(redColor, greenColor, blueColor, 1f); chatKey = (KeyCode)Int32.Parse(xmlDoc.SelectSingleNode("/settings/global/@chat-key").Value); screenshotKey = (KeyCode)Int32.Parse(xmlDoc.SelectSingleNode("/settings/global/@screenshot-key").Value); selectedFlag = xmlDoc.SelectSingleNode("/settings/global/@selected-flag").Value; compressionEnabled = Boolean.Parse(xmlDoc.SelectSingleNode("/settings/global/@compression").Value); revertEnabled = Boolean.Parse(xmlDoc.SelectSingleNode("/settings/global/@revert").Value); toolbarType = (DMPToolbarType)Int32.Parse(xmlDoc.SelectSingleNode("/settings/global/@toolbar").Value); XmlNodeList serverNodeList = xmlDoc.GetElementsByTagName("server"); servers.Clear(); foreach (XmlNode xmlNode in serverNodeList) { ServerEntry newServer = new ServerEntry(); newServer.name = xmlNode.Attributes["name"].Value; newServer.address = xmlNode.Attributes["address"].Value; Int32.TryParse(xmlNode.Attributes["port"].Value, out newServer.port); servers.Add(newServer); } SaveSettings(); } catch (Exception e) { DarkLog.Debug("Error loading old settings: " + e); } }
public void LoadSettings() { //Read XML settings try { bool saveXMLAfterLoad = false; XmlDocument xmlDoc = new XmlDocument(); if (File.Exists(backupSettingsFile) && !File.Exists(settingsFile)) { DarkLog.Debug("Restoring player settings file!"); File.Copy(backupSettingsFile, settingsFile); } if (!File.Exists(settingsFile)) { xmlDoc.LoadXml(newXMLString()); playerName = DEFAULT_PLAYER_NAME; xmlDoc.Save(settingsFile); } if (!File.Exists(backupSettingsFile)) { DarkLog.Debug("Backing up player token and settings file!"); File.Copy(settingsFile, backupSettingsFile); } xmlDoc.Load(settingsFile); playerName = xmlDoc.SelectSingleNode("/settings/global/@username").Value; try { cacheSize = Int32.Parse(xmlDoc.SelectSingleNode("/settings/global/@cache-size").Value); } catch { DarkLog.Debug("Adding cache size to settings file"); saveXMLAfterLoad = true; cacheSize = DEFAULT_CACHE_SIZE; } try { disclaimerAccepted = Int32.Parse(xmlDoc.SelectSingleNode("/settings/global/@disclaimer").Value); } catch { DarkLog.Debug("Adding disclaimer to settings file"); saveXMLAfterLoad = true; } try { string floatArrayString = xmlDoc.SelectSingleNode("/settings/global/@player-color").Value; string[] floatArrayStringSplit = floatArrayString.Split(','); float redColor = float.Parse(floatArrayStringSplit[0].Trim()); float greenColor = float.Parse(floatArrayStringSplit[1].Trim()); float blueColor = float.Parse(floatArrayStringSplit[2].Trim()); //Bounds checking - Gotta check up on those players :) if (redColor < 0f) { redColor = 0f; } if (redColor > 1f) { redColor = 1f; } if (greenColor < 0f) { greenColor = 0f; } if (greenColor > 1f) { greenColor = 1f; } if (blueColor < 0f) { blueColor = 0f; } if (blueColor > 1f) { blueColor = 1f; } playerColor = new Color(redColor, greenColor, blueColor, 1f); } catch { DarkLog.Debug("Adding player color to settings file"); saveXMLAfterLoad = true; playerColor = PlayerColorWorker.GenerateRandomColor(); } try { chatKey = (KeyCode)Int32.Parse(xmlDoc.SelectSingleNode("/settings/global/@chat-key").Value); } catch { DarkLog.Debug("Adding chat key to settings file"); saveXMLAfterLoad = true; chatKey = KeyCode.BackQuote; } try { screenshotKey = (KeyCode)Int32.Parse(xmlDoc.SelectSingleNode("/settings/global/@screenshot-key").Value); } catch { DarkLog.Debug("Adding screenshot key to settings file"); saveXMLAfterLoad = true; chatKey = KeyCode.F8; } try { selectedFlag = xmlDoc.SelectSingleNode("/settings/global/@selected-flag").Value; } catch { DarkLog.Debug("Adding selected flag to settings file"); saveXMLAfterLoad = true; selectedFlag = "Squad/Flags/default"; } try { compressionEnabled = Boolean.Parse(xmlDoc.SelectSingleNode("/settings/global/@compression").Value); } catch { DarkLog.Debug("Adding compression flag to settings file"); compressionEnabled = true; } try { revertEnabled = Boolean.Parse(xmlDoc.SelectSingleNode("/settings/global/@revert").Value); } catch { DarkLog.Debug("Adding revert flag to settings file"); revertEnabled = true; } try { toolbarType = (DMPToolbarType)Int32.Parse(xmlDoc.SelectSingleNode("/settings/global/@toolbar").Value); } catch { DarkLog.Debug("Adding toolbar flag to settings file"); toolbarType = DMPToolbarType.BLIZZY_IF_INSTALLED; } XmlNodeList serverNodeList = xmlDoc.GetElementsByTagName("server"); servers = new List <ServerEntry>(); foreach (XmlNode xmlNode in serverNodeList) { ServerEntry newServer = new ServerEntry(); newServer.name = xmlNode.Attributes["name"].Value; newServer.address = xmlNode.Attributes["address"].Value; Int32.TryParse(xmlNode.Attributes["port"].Value, out newServer.port); servers.Add(newServer); } if (saveXMLAfterLoad) { SaveSettings(); } } catch (Exception e) { DarkLog.Debug("XML Exception: " + e); } //Read player token try { //Restore backup if needed if (File.Exists(backupPublicKeyFile) && File.Exists(backupPrivateKeyFile) && (!File.Exists(publicKeyFile) || !File.Exists(privateKeyFile))) { DarkLog.Debug("Restoring backed up keypair!"); File.Copy(backupPublicKeyFile, publicKeyFile, true); File.Copy(backupPrivateKeyFile, privateKeyFile, true); } //Load or create token file if (File.Exists(privateKeyFile) && File.Exists(publicKeyFile)) { playerPublicKey = File.ReadAllText(publicKeyFile); playerPrivateKey = File.ReadAllText(privateKeyFile); } else { DarkLog.Debug("Creating new keypair!"); GenerateNewKeypair(); } //Save backup token file if needed if (!File.Exists(backupPublicKeyFile) || !File.Exists(backupPrivateKeyFile)) { DarkLog.Debug("Backing up keypair"); File.Copy(publicKeyFile, backupPublicKeyFile, true); File.Copy(privateKeyFile, backupPrivateKeyFile, true); } } catch { DarkLog.Debug("Error processing keypair, creating new keypair"); GenerateNewKeypair(); DarkLog.Debug("Backing up keypair"); File.Copy(publicKeyFile, backupPublicKeyFile, true); File.Copy(privateKeyFile, backupPrivateKeyFile, true); } }
public void LoadSettings() { try { if (File.Exists(backupOldSettingsFile) || File.Exists(oldSettingsFile)) { DarkLog.Debug("[Settings]: Loading old settings"); LoadOldSettings(); SaveSettings(); File.Delete(backupOldSettingsFile); File.Delete(oldSettingsFile); } bool saveAfterLoad = false; ConfigNode mainNode = new ConfigNode(); if (File.Exists(backupSettingsFile) && !File.Exists(settingsFile)) { DarkLog.Debug("[Settings]: Restoring backup file"); File.Copy(backupSettingsFile, settingsFile); } if (!File.Exists(settingsFile)) { mainNode = GetDefaultSettings(); playerName = DEFAULT_PLAYER_NAME; mainNode.Save(settingsFile); } if (!File.Exists(backupSettingsFile)) { DarkLog.Debug("[Settings]: Backing up settings"); File.Copy(settingsFile, backupSettingsFile); } mainNode = ConfigNode.Load(settingsFile); ConfigNode settingsNode = mainNode.GetNode("SETTINGS"); ConfigNode playerNode = settingsNode.GetNode("PLAYER"); ConfigNode bindingsNode = settingsNode.GetNode("KEYBINDINGS"); playerName = playerNode.GetValue("name"); if (!int.TryParse(settingsNode.GetValue("cacheSize"), out cacheSize)) { DarkLog.Debug("[Settings]: Adding cache size to settings file"); cacheSize = DEFAULT_CACHE_SIZE; saveAfterLoad = true; } if (!int.TryParse(settingsNode.GetValue("disclaimer"), out disclaimerAccepted)) { DarkLog.Debug("[Settings]: Adding disclaimer to settings file"); disclaimerAccepted = 0; saveAfterLoad = true; } if (!int.TryParse(settingsNode.GetValue("serverlist-mode"), out serverlistMode)) { DarkLog.Debug("[Settings]: Adding serverlist-mode to settings file"); serverlistMode = 0; saveAfterLoad = true; } if (!playerNode.TryGetValue("color", ref playerColor)) { DarkLog.Debug("[Settings]: Adding color to settings file"); playerColor = PlayerColorWorker.GenerateRandomColor(); saveAfterLoad = true; } int chatKey = (int)KeyCode.BackQuote, screenshotKey = (int)KeyCode.F8; if (!int.TryParse(bindingsNode.GetValue("chat"), out chatKey)) { DarkLog.Debug("[Settings]: Adding chat key to settings file"); this.chatKey = KeyCode.BackQuote; saveAfterLoad = true; } else { this.chatKey = (KeyCode)chatKey; } if (!int.TryParse(bindingsNode.GetValue("screenshot"), out screenshotKey)) { DarkLog.Debug("[Settings]: Adding screenshot key to settings file"); this.screenshotKey = KeyCode.F8; saveAfterLoad = true; } else { this.screenshotKey = (KeyCode)screenshotKey; } if (!playerNode.TryGetValue("flag", ref selectedFlag)) { DarkLog.Debug("[Settings]: Adding selected flag to settings file"); selectedFlag = "Squad/Flags/default"; saveAfterLoad = true; } if (!settingsNode.TryGetValue("compression", ref compressionEnabled)) { DarkLog.Debug("[Settings]: Adding compression flag to settings file"); compressionEnabled = true; saveAfterLoad = true; } if (!settingsNode.TryGetValue("revert", ref revertEnabled)) { DarkLog.Debug("[Settings]: Adding revert flag to settings file"); revertEnabled = true; saveAfterLoad = true; } string interpolatorString = null; int interpolatorInt = 0; try { //Make sure we haven't saved to the old int type if (settingsNode.TryGetValue("interpolation", ref interpolatorString) && !int.TryParse(interpolatorString, out interpolatorInt)) { interpolatorType = (InterpolatorType)Enum.Parse(typeof(InterpolatorType), interpolatorString); } else { DarkLog.Debug("[Settings]: Adding interpolation flag to settings file"); interpolatorType = InterpolatorType.INTERPOLATE1S; saveAfterLoad = true; } } catch { interpolatorType = InterpolatorType.INTERPOLATE1S; saveAfterLoad = true; } int toolbarType; if (!int.TryParse(settingsNode.GetValue("toolbar"), out toolbarType)) { DarkLog.Debug("[Settings]: Adding toolbar flag to settings file"); this.toolbarType = DMPToolbarType.BLIZZY_IF_INSTALLED; saveAfterLoad = true; } else { this.toolbarType = (DMPToolbarType)toolbarType; } ConfigNode serversNode = settingsNode.GetNode("SERVERS"); servers.Clear(); if (serversNode.HasNode("SERVER")) { foreach (ConfigNode serverNode in serversNode.GetNodes("SERVER")) { ServerEntry newServer = new ServerEntry(); newServer.name = serverNode.GetValue("name"); newServer.address = serverNode.GetValue("address"); serverNode.TryGetValue("port", ref newServer.port); servers.Add(newServer); } } if (saveAfterLoad) { SaveSettings(); } } catch (Exception e) { DarkLog.Debug("Error while loading settings:"); DarkLog.Debug(e.ToString()); } //Read player token try { //Restore backup if needed if (File.Exists(backupPublicKeyFile) && File.Exists(backupPrivateKeyFile) && (!File.Exists(publicKeyFile) || !File.Exists(privateKeyFile))) { DarkLog.Debug("[Settings]: Restoring backed up keypair!"); File.Copy(backupPublicKeyFile, publicKeyFile, true); File.Copy(backupPrivateKeyFile, privateKeyFile, true); } //Load or create token file if (File.Exists(privateKeyFile) && File.Exists(publicKeyFile)) { playerPublicKey = File.ReadAllText(publicKeyFile); playerPrivateKey = File.ReadAllText(privateKeyFile); } else { DarkLog.Debug("[Settings]: Creating new keypair!"); GenerateNewKeypair(); } //Save backup token file if needed if (!File.Exists(backupPublicKeyFile) || !File.Exists(backupPrivateKeyFile)) { DarkLog.Debug("[Settings]: Backing up keypair"); File.Copy(publicKeyFile, backupPublicKeyFile, true); File.Copy(privateKeyFile, backupPrivateKeyFile, true); } } catch { DarkLog.Debug("Error processing keypair, creating new keypair"); GenerateNewKeypair(); DarkLog.Debug("[Settings]: Backing up keypair"); File.Copy(publicKeyFile, backupPublicKeyFile, true); File.Copy(privateKeyFile, backupPrivateKeyFile, true); } }
public void LoadSettings() { //Read XML settings try { bool saveXMLAfterLoad = false; XmlDocument xmlDoc = new XmlDocument(); if (File.Exists(backupSettingsFile) && !File.Exists(settingsFile)) { DarkLog.Debug("Restoring player settings file!"); File.Copy(backupSettingsFile, settingsFile); } if (!File.Exists(settingsFile)) { xmlDoc.LoadXml(newXMLString()); playerName = DEFAULT_PLAYER_NAME; xmlDoc.Save(settingsFile); } if (!File.Exists(backupSettingsFile)) { DarkLog.Debug("Backing up player token and settings file!"); File.Copy(settingsFile, backupSettingsFile); } xmlDoc.Load(settingsFile); playerName = xmlDoc.SelectSingleNode("/settings/global/@username").Value; try { cacheSize = Int32.Parse(xmlDoc.SelectSingleNode("/settings/global/@cache-size").Value); } catch { DarkLog.Debug("Adding cache size to settings file"); saveXMLAfterLoad = true; cacheSize = DEFAULT_CACHE_SIZE; } try { disclaimerAccepted = Int32.Parse(xmlDoc.SelectSingleNode("/settings/global/@disclaimer").Value); } catch { DarkLog.Debug("Adding disclaimer to settings file"); saveXMLAfterLoad = true; } try { string floatArrayString = xmlDoc.SelectSingleNode("/settings/global/@player-color").Value; string[] floatArrayStringSplit = floatArrayString.Split(','); float redColor = float.Parse(floatArrayStringSplit[0].Trim()); float greenColor = float.Parse(floatArrayStringSplit[1].Trim()); float blueColor = float.Parse(floatArrayStringSplit[2].Trim()); //Bounds checking - Gotta check up on those players :) if (redColor < 0f) { redColor = 0f; } if (redColor > 1f) { redColor = 1f; } if (greenColor < 0f) { greenColor = 0f; } if (greenColor > 1f) { greenColor = 1f; } if (blueColor < 0f) { blueColor = 0f; } if (blueColor > 1f) { blueColor = 1f; } playerColor = new Color(redColor, greenColor, blueColor, 1f); OptionsWindow.fetch.loadEventHandled = false; } catch { DarkLog.Debug("Adding player color to settings file"); saveXMLAfterLoad = true; playerColor = PlayerColorWorker.GenerateRandomColor(); OptionsWindow.fetch.loadEventHandled = false; } try { chatKey = (KeyCode)Int32.Parse(xmlDoc.SelectSingleNode("/settings/global/@chat-key").Value); } catch { DarkLog.Debug("Adding chat key to settings file"); saveXMLAfterLoad = true; chatKey = KeyCode.BackQuote; } try { screenshotKey = (KeyCode)Int32.Parse(xmlDoc.SelectSingleNode("/settings/global/@screenshot-key").Value); } catch { DarkLog.Debug("Adding screenshot key to settings file"); saveXMLAfterLoad = true; chatKey = KeyCode.F8; } try { selectedFlag = xmlDoc.SelectSingleNode("/settings/global/@selected-flag").Value; } catch { DarkLog.Debug("Adding selected flag to settings file"); saveXMLAfterLoad = true; selectedFlag = "Squad/Flags/default"; } try { compressionEnabled = Boolean.Parse(xmlDoc.SelectSingleNode("/settings/global/@compression").Value); } catch { DarkLog.Debug("Adding compression flag to settings file"); compressionEnabled = true; } try { revertEnabled = Boolean.Parse(xmlDoc.SelectSingleNode("/settings/global/@revert").Value); } catch { DarkLog.Debug("Adding revert flag to settings file"); revertEnabled = true; } try { toolbarType = (DMPToolbarType)Int32.Parse(xmlDoc.SelectSingleNode("/settings/global/@toolbar").Value); } catch { DarkLog.Debug("Adding toolbar flag to settings file"); toolbarType = DMPToolbarType.BLIZZY_IF_INSTALLED; } XmlNodeList serverNodeList = xmlDoc.GetElementsByTagName("server"); servers = new List<ServerEntry>(); foreach (XmlNode xmlNode in serverNodeList) { ServerEntry newServer = new ServerEntry(); newServer.name = xmlNode.Attributes["name"].Value; newServer.address = xmlNode.Attributes["address"].Value; Int32.TryParse(xmlNode.Attributes["port"].Value, out newServer.port); servers.Add(newServer); } if (saveXMLAfterLoad) { SaveSettings(); } } catch (Exception e) { DarkLog.Debug("XML Exception: " + e); } //Read player token try { //Restore backup if needed if (File.Exists(backupPublicKeyFile) && File.Exists(backupPrivateKeyFile) && (!File.Exists(publicKeyFile) || !File.Exists(privateKeyFile))) { DarkLog.Debug("Restoring backed up keypair!"); File.Copy(backupPublicKeyFile, publicKeyFile, true); File.Copy(backupPrivateKeyFile, privateKeyFile, true); } //Load or create token file if (File.Exists(privateKeyFile) && File.Exists(publicKeyFile)) { playerPublicKey = File.ReadAllText(publicKeyFile); playerPrivateKey = File.ReadAllText(privateKeyFile); } else { DarkLog.Debug("Creating new keypair!"); GenerateNewKeypair(); } //Save backup token file if needed if (!File.Exists(backupPublicKeyFile) || !File.Exists(backupPrivateKeyFile)) { DarkLog.Debug("Backing up keypair"); File.Copy(publicKeyFile, backupPublicKeyFile, true); File.Copy(privateKeyFile, backupPrivateKeyFile, true); } } catch { DarkLog.Debug("Error processing keypair, creating new keypair"); GenerateNewKeypair(); DarkLog.Debug("Backing up keypair"); File.Copy(publicKeyFile, backupPublicKeyFile, true); File.Copy(privateKeyFile, backupPrivateKeyFile, true); } }