public static void Save() { CheckIfLoaded(); const string tempFileName = Paths.PlayerDBFileName + ".temp"; lock ( SaveLoadLocker ) { PlayerInfo[] listCopy = PlayerInfoList; Stopwatch sw = Stopwatch.StartNew(); using (FileStream fs = OpenWrite(tempFileName)) { using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, BufferSize)) { writer.WriteLine("{0} {1} {2}", maxID, FormatVersion, Header); StringBuffer sb = new StringBuffer(2048); for (int i = 0; i < listCopy.Length; i++) { listCopy[i].Serialize(sb); writer.WriteLine(sb.value, 0, sb.length); sb.length = 0; } } } sw.Stop(); Logger.Log(LogType.Debug, "PlayerDB.Save: Saved player database ({0} records) in {1}ms", Trie.Count, sw.ElapsedMilliseconds); try { Paths.MoveOrReplaceFile(tempFileName, Paths.PlayerDBFileName); } catch (Exception ex) { Logger.Log(LogType.Error, "PlayerDB.Save: An error occurred while trying to save PlayerDB: {0}", ex); } } }
internal static void Save() { lock ( BanListLock ) { CheckIfLoaded(); const string tempFile = Paths.IPBanListFileName + ".temp"; Stopwatch sw = Stopwatch.StartNew(); using (StreamWriter writer = File.CreateText(tempFile)) { writer.WriteLine("{0} {1}", FormatVersion, Header); foreach (IPBanInfo entry in Bans.Values) { writer.WriteLine(entry.Serialize()); } } try { Paths.MoveOrReplaceFile(tempFile, Paths.IPBanListFileName); sw.Stop(); Logger.Log(LogType.Debug, "IPBanList.Save: Saved IP-ban database ({0} records) in {1}ms", Count, sw.ElapsedMilliseconds); } catch (Exception ex) { Logger.Log(LogType.Error, "IPBanList.Save: An error occurred while trying to save ban list file: {0}", ex); } } }
static void Beat([NotNull] SchedulerTask scheduledTask) { if (Server.IsShuttingDown) { return; } if (ConfigKey.HeartbeatEnabled.Enabled()) { SendHeartBeat(); } else { // If heartbeats are disabled, the server data is written // to a text file instead (heartbeatdata.txt) string[] data = { Salt, Server.InternalIP.ToString(), Server.Port.ToStringInvariant(), Server.CountPlayers(false).ToStringInvariant(), ConfigKey.MaxPlayers.GetString(), Color.StripColors(ConfigKey.ServerName.GetString(),false), ConfigKey.IsPublic.GetString(), ConfigKey.HeartbeatUrl.GetString() }; const string tempFile = Paths.HeartbeatDataFileName + ".tmp"; File.WriteAllLines(tempFile, data, Encoding.ASCII); Paths.MoveOrReplaceFile(tempFile, Paths.HeartbeatDataFileName); } }
/// <summary> Saves the current world list to worlds.xml. Thread-safe. </summary> public static void SaveWorldList() { // Save world list XDocument doc = new XDocument(); XElement root = new XElement("fCraftWorldList"); lock ( SyncRoot ) { foreach (World world in Worlds) { root.Add(world.Serialize()); } foreach (Rank mainedRank in RankManager.Ranks) { if (mainedRank.MainWorld == null || mainedRank.MainWorld == MainWorld) { continue; } XElement mainedRankEl = new XElement(RankMainXmlTagName); mainedRankEl.Add(new XAttribute("rank", mainedRank.FullName)); mainedRankEl.Add(new XAttribute("world", mainedRank.MainWorld.Name)); root.Add(mainedRankEl); } root.Add(new XAttribute("main", MainWorld.Name)); doc.Add(root); doc.Save(WorldListTempFileName); Paths.MoveOrReplaceFile(WorldListTempFileName, Paths.WorldListFileName); } }
static void Beat(SchedulerTask scheduledTask) { if (Server.IsShuttingDown) { return; } if (ConfigKey.HeartbeatEnabled.Enabled()) { SendMinecraftNetBeat(); if (ConfigKey.IsPublic.Enabled() && ConfigKey.HeartbeatToWoMDirect.Enabled()) { SendWoMDirectBeat(); } } else { // If heartbeats are disabled, the server data is written // to a text file instead (heartbeatdata.txt) string[] data = new[] { Salt, Server.InternalIP.ToString(), Server.Port.ToString(CultureInfo.InvariantCulture), Server.CountPlayers(false).ToString(CultureInfo.InvariantCulture), ConfigKey.MaxPlayers.GetString(), ConfigKey.ServerName.GetString(), ConfigKey.IsPublic.GetString() }; const string tempFile = Paths.HeartbeatDataFileName + ".tmp"; File.WriteAllLines(tempFile, data, Encoding.ASCII); Paths.MoveOrReplaceFile(tempFile, Paths.HeartbeatDataFileName); } }
/// <summary> Saves the whole database. </summary> public void Save() { const string tempFileName = Paths.PlayerDBFileName + ".bin.temp"; using (PlayerDB.GetReadLock()) { using (FileStream fs = OpenWrite(tempFileName)) { BinaryWriter writer = new BinaryWriter(fs); writer.Write(FormatVersion); writer.Write(maxID); writer.Write(RankManager.Ranks.Count); foreach (Rank rank in RankManager.Ranks) { writer.Write((byte)rank.Index); writer.Write(rank.FullName); } int total = PlayerDB.List.Count; writer.Write(total); for (int i = 0; i < total; i++) { SaveBinaryFormat0(PlayerDB.List[i], writer); } } try { Paths.MoveOrReplaceFile(tempFileName, Paths.PlayerDBFileName + ".bin"); } catch (Exception ex) { Logger.Log(LogType.Error, "PlayerDB.SaveBinary: An error occurred while trying to save PlayerDB: {0}", ex); } } }
internal static void Save() { if (!IsLoaded) { return; } Logger.Log(LogType.Debug, "IPBanList.Save: Saving IP ban list ({0} records).", Bans.Count); const string tempFile = Paths.IPBanListFileName + ".temp"; lock ( BanListLock ) { using (StreamWriter writer = File.CreateText(tempFile)) { writer.WriteLine("{0} {1}", FormatVersion, Header); foreach (IPBanInfo entry in Bans.Values) { writer.WriteLine(entry.Serialize()); } } } try { Paths.MoveOrReplaceFile(tempFile, Paths.IPBanListFileName); } catch (Exception ex) { Logger.Log(LogType.Error, "IPBanList.Save: An error occurred while trying to save ban list file: {0}", ex); } }
void TrimFile(int maxCapacity) { if (maxCapacity == 0) { using (File.Create(FileName)) { } return; } string tempFileName = FileName + ".tmp"; using (FileStream source = File.OpenRead(FileName)) { int entries = (int)(source.Length / BlockDBEntry.Size); if (entries <= maxCapacity) { return; } // skip beginning of the file (that's where old entries are) source.Seek((entries - maxCapacity) * BlockDBEntry.Size, SeekOrigin.Begin); // copy end of the existing file to a new one using (FileStream destination = File.Create(tempFileName)) { while (source.Position < source.Length) { int bytesRead = source.Read(ioBuffer, 0, ioBuffer.Length); destination.Write(ioBuffer, 0, bytesRead); } } } Paths.MoveOrReplaceFile(tempFileName, FileName); }
public static void Save(string fileName) { JsonObject root = new JsonObject(); root.Add("ConfigVersion", CurrentVersion); //TODO: Save rank list // Save log options List <string> consoleOptions = new List <string>(); for (int i = 0; i < Logger.ConsoleOptions.Length; i++) { if (Logger.ConsoleOptions[i]) { consoleOptions.Add(((LogType)i).ToString()); } } root.Add("ConsoleOptions", consoleOptions.ToString()); List <string> logFileOptions = new List <string>(); for (int i = 0; i < Logger.LogFileOptions.Length; i++) { if (Logger.LogFileOptions[i]) { logFileOptions.Add(((LogType)i).ToString()); } } root.Add("LogFileOptions", logFileOptions.ToString()); // Save general settings JsonObject settings = new JsonObject(); foreach (ConfigKey key in Enum.GetValues(typeof(ConfigKey))) { settings.Add(key.ToString(), key.GetRawString()); } root.Add("Settings", settings); // Save PlayerDB settings JsonObject playerDBInfo = new JsonObject(); playerDBInfo.Add("Engine", PlayerDB.ProviderType); if (PlayerDB.MySqlProviderSettings != null) { playerDBInfo.Add("MySqlSettings", PlayerDB.MySqlProviderSettings); } if (PlayerDB.FlatfileProviderSettings != null) { playerDBInfo.Add("FlatfileSettings", PlayerDB.FlatfileProviderSettings); } root.Add("PlayerDB", playerDBInfo); string tempFileName = fileName + ".tmp"; File.WriteAllText(tempFileName, root.Serialize()); Paths.MoveOrReplaceFile(tempFileName, fileName); }
/// <summary> Saves this map to a file in the default format (FCMv4). </summary> /// <returns> Whether the saving succeeded. </returns> public bool Save([NotNull] string fileName) { if (fileName == null) { throw new ArgumentNullException("fileName"); } string tempFileName = fileName + ".temp"; // save to a temporary file try { HasChangedSinceSave = false; if (!MapUtility.TrySave(this, tempFileName, SaveFormat)) { HasChangedSinceSave = true; } } catch (IOException ex) { HasChangedSinceSave = true; Logger.Log(LogType.Error, "Map.Save: Unable to open file \"{0}\" for writing: {1}", tempFileName, ex); if (File.Exists(tempFileName)) { File.Delete(tempFileName); } return(false); } // move newly-written file into its permanent destination try { Paths.MoveOrReplaceFile(tempFileName, fileName); Logger.Log(LogType.SystemActivity, "Saved map to {0}", fileName); HasChangedSinceBackup = true; } catch (Exception ex) { HasChangedSinceSave = true; Logger.Log(LogType.Error, "Map.Save: Error trying to replace file \"{0}\": {1}", fileName, ex); if (File.Exists(tempFileName)) { File.Delete(tempFileName); } return(false); } return(true); }
/// <summary> Saves the current world list to worlds.xml. Thread-safe. </summary> public static void SaveWorldList() { const string worldListTempFileName = Paths.WorldListFileName + ".tmp"; // Save world list lock ( SyncRoot ) { XDocument doc = new XDocument(); XElement root = new XElement("fCraftWorldList"); foreach (World world in Worlds) { XElement temp = new XElement("World"); temp.Add(new XAttribute("name", world.Name)); if (world.AccessSecurity.HasRestrictions) { temp.Add(world.AccessSecurity.Serialize(AccessSecurityXmlTagName)); } if (world.BuildSecurity.HasRestrictions) { temp.Add(world.BuildSecurity.Serialize(BuildSecurityXmlTagName)); } // save backup settings switch (world.BackupEnabledState) { case YesNoAuto.Yes: temp.Add(new XAttribute("backup", world.BackupInterval.ToSecondsString())); break; case YesNoAuto.No: temp.Add(new XAttribute("backup", 0)); break; } if (world.Preload) { temp.Add(new XAttribute("noUnload", true)); } if (world.IsHidden) { temp.Add(new XAttribute("hidden", true)); } temp.Add(world.BlockDB.SaveSettings()); World world1 = world; // keeping ReSharper happy foreach (Rank mainedRank in RankManager.Ranks.Where(r => r.MainWorld == world1)) { temp.Add(new XElement(RankMainXmlTagName, mainedRank.FullName)); } // save loaded/map-changed information if (!String.IsNullOrEmpty(world.LoadedBy)) { temp.Add(new XElement("LoadedBy", world.LoadedBy)); } if (world.LoadedOn != DateTime.MinValue) { temp.Add(new XElement("LoadedOn", world.LoadedOn.ToUnixTime())); } if (!String.IsNullOrEmpty(world.MapChangedBy)) { temp.Add(new XElement("MapChangedBy", world.MapChangedBy)); } if (world.MapChangedOn != DateTime.MinValue) { temp.Add(new XElement("MapChangedOn", world.MapChangedOn.ToUnixTime())); } // save environmental settings XElement elEnv = new XElement(EnvironmentXmlTagName); if (world.CloudColor > -1) { elEnv.Add(new XAttribute("cloud", world.CloudColor)); } if (world.FogColor > -1) { elEnv.Add(new XAttribute("fog", world.FogColor)); } if (world.SkyColor > -1) { elEnv.Add(new XAttribute("sky", world.SkyColor)); } if (world.EdgeLevel > -1) { elEnv.Add(new XAttribute("level", world.EdgeLevel)); } if (world.EdgeBlock != Block.Water) { elEnv.Add(new XAttribute("edge", world.EdgeBlock)); } if (elEnv.HasAttributes) { temp.Add(elEnv); } // save lock information if (world.IsLocked) { temp.Add(new XAttribute("locked", true)); if (!String.IsNullOrEmpty(world.LockedBy)) { temp.Add(new XElement("LockedBy", world.LockedBy)); } if (world.LockedOn != DateTime.MinValue) { temp.Add(new XElement("LockedOn", world.LockedOn.ToUnixTime())); } } else { if (!String.IsNullOrEmpty(world.UnlockedBy)) { temp.Add(new XElement("UnlockedBy", world.UnlockedBy)); } if (world.UnlockedOn != DateTime.MinValue) { temp.Add(new XElement("UnlockedOn", world.UnlockedOn.ToUnixTime())); } } root.Add(temp); } root.Add(new XAttribute("main", MainWorld.Name)); doc.Add(root); doc.Save(worldListTempFileName); Paths.MoveOrReplaceFile(worldListTempFileName, Paths.WorldListFileName); } }
public static bool Save() { XDocument file = new XDocument(); XElement config = new XElement(ConfigXmlRootName); config.Add(new XAttribute("version", CurrentVersion)); XElement settings = new XElement("Settings"); // save general settings foreach (ConfigSection section in Enum.GetValues(typeof(ConfigSection))) { settings.Add(new XComment(section.ToString())); foreach (ConfigKey key in KeySections[section]) { XElement keyPairEl = new XElement("ConfigKey"); keyPairEl.Add(new XAttribute("key", key)); keyPairEl.Add(new XAttribute("value", Settings[(int)key])); keyPairEl.Add(new XAttribute("default", key.GetDefault())); settings.Add(keyPairEl); } } config.Add(settings); // save console options XElement consoleOptions = new XElement("ConsoleOptions"); for (int i = 0; i < Logger.ConsoleOptions.Length; i++) { if (Logger.ConsoleOptions[i]) { consoleOptions.Add(new XElement(((LogType)i).ToString())); } } config.Add(consoleOptions); // save logfile options XElement logFileOptions = new XElement("LogFileOptions"); for (int i = 0; i < Logger.LogFileOptions.Length; i++) { if (Logger.LogFileOptions[i]) { logFileOptions.Add(new XElement(((LogType)i).ToString())); } } config.Add(logFileOptions); // save ranks XElement ranksTag = new XElement("Ranks"); foreach (Rank rank in RankManager.Ranks) { ranksTag.Add(rank.Serialize()); } config.Add(ranksTag); if (RankManager.LegacyRankMapping.Count > 0) { // save legacy rank mapping XElement legacyRankMappingTag = new XElement("LegacyRankMapping"); legacyRankMappingTag.Add( new XComment( "Legacy rank mapping is used for compatibility if cases when ranks are renamed or deleted.")); foreach (KeyValuePair <string, string> pair in RankManager.LegacyRankMapping) { XElement rankPair = new XElement("LegacyRankPair"); rankPair.Add(new XAttribute("from", pair.Key), new XAttribute("to", pair.Value)); legacyRankMappingTag.Add(rankPair); } config.Add(legacyRankMappingTag); } file.Add(config); // write out the changes string tempFileName = Paths.ConfigFileName + ".temp"; file.Save(tempFileName); Paths.MoveOrReplaceFile(tempFileName, Paths.ConfigFileName); return(true); }