From e01eba80e89060fa41b256d9106a8c2bed9d0a8a Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 22 Jul 2016 23:21:10 +1000 Subject: [PATCH] Combine .env and .properties files into one. --- Commands/Information/CmdMapInfo.cs | 78 ++++++++++++++---------------- Levels/IO/LvlProperties.cs | 32 +----------- Levels/Level.cs | 30 ++++++++---- Levels/LevelEnv.cs | 4 +- Player/Player.CPE.cs | 2 +- Server/Server.Fields.cs | 2 +- Server/Server.Init.cs | 44 +++++++++++++++++ Server/Server.cs | 5 +- 8 files changed, 110 insertions(+), 87 deletions(-) diff --git a/Commands/Information/CmdMapInfo.cs b/Commands/Information/CmdMapInfo.cs index 8aa852f04..ccfca57c1 100644 --- a/Commands/Information/CmdMapInfo.cs +++ b/Commands/Information/CmdMapInfo.cs @@ -185,7 +185,7 @@ namespace MCGalaxy.Commands { EdgeLevel = lvl.EdgeLevel; CloudsHeight = lvl.CloudsHeight; MaxFog = lvl.MaxFogDistance; CloudsSpeed = lvl.CloudsSpeed; WeatherSpeed = lvl.WeatherSpeed; - EdgeBlock = lvl.EdgeBlock; HorizonBlock = lvl.HorizonBlock; + EdgeBlock = (byte)lvl.EdgeBlock; HorizonBlock = (byte)lvl.HorizonBlock; WeatherFade = lvl.WeatherFade; TerrainUrl = lvl.terrainUrl != "" ? @@ -205,52 +205,46 @@ namespace MCGalaxy.Commands { string path = LevelInfo.FindPropertiesFile(name); if (path != null) PropertiesFile.Read(path, ParseProperty, '='); - - path = "levels/level properties/" + name + ".env"; - if (File.Exists(path)) - PropertiesFile.Read(path, ParseEnv, '='); if (Authors == null) Authors = ""; } void ParseProperty(string key, string value) { switch (key.ToLower()) { - case "physics": Physics = int.Parse(value); break; - case "guns": Guns = bool.Parse(value); break; - case "texture": TerrainUrl = value; break; - case "texturepack": TextureUrl = value; break; - case "clouds-speed": CloudsSpeed = int.Parse(value); break; - case "weather-speed": WeatherSpeed = int.Parse(value); break; - case "weather-fade": WeatherFade = int.Parse(value); break; - case "useblockdb": blockDB = bool.Parse(value); break; - case "realmowner": RealmOwner = value; break; - - case "perbuild": build = GetPerm(value); break; - case "pervisit": visit = GetPerm(value); break; - case "perbuildmax": buildmax = GetPerm(value); break; - case "pervisitmax": visitmax = GetPerm(value); break; - case "visitwhitelist": VisitWhitelist = Parse(value); break; - case "visitblacklist": VisitBlacklist = Parse(value); break; - - case "authors": Authors = value; break; - case "roundsplayed": TotalRounds = int.Parse(value); break; - case "RoundsHumanWon": HumanRounds = int.Parse(value); break; - case "likes": Likes = int.Parse(value); break; - case "dislikes": Dislikes = int.Parse(value); break; - } - } - - void ParseEnv(string key, string value) { - switch (key.ToLower()) { - case "cloudcolor": Clouds = value; break; - case "fogcolor": Fog = value; break; - case "skycolor": Sky = value; break; - case "shadowcolor": Shadow = value; break; - case "lightcolor": Light = value; break; - case "edgeblock": EdgeBlock = byte.Parse(value); break; - case "edgelevel": EdgeLevel = short.Parse(value); break; - case "cloudsheight": CloudsHeight = short.Parse(value); break; - case "maxfog": MaxFog = short.Parse(value); break; - case "horizonblock": HorizonBlock = byte.Parse(value); break; + case "physics": Physics = int.Parse(value); break; + case "guns": Guns = bool.Parse(value); break; + case "useblockdb": blockDB = bool.Parse(value); break; + case "realmowner": RealmOwner = value; break; + + case "perbuild": build = GetPerm(value); break; + case "pervisit": visit = GetPerm(value); break; + case "perbuildmax": buildmax = GetPerm(value); break; + case "pervisitmax": visitmax = GetPerm(value); break; + case "visitwhitelist": VisitWhitelist = Parse(value); break; + case "visitblacklist": VisitBlacklist = Parse(value); break; + + case "authors": Authors = value; break; + case "roundsplayed": TotalRounds = int.Parse(value); break; + case "RoundsHumanWon": HumanRounds = int.Parse(value); break; + case "likes": Likes = int.Parse(value); break; + case "dislikes": Dislikes = int.Parse(value); break; + + case "cloudcolor": Clouds = value; break; + case "fogcolor": Fog = value; break; + case "skycolor": Sky = value; break; + case "shadowcolor": Shadow = value; break; + case "lightcolor": Light = value; break; + + case "edgeblock": EdgeBlock = byte.Parse(value); break; + case "edgelevel": EdgeLevel = short.Parse(value); break; + case "horizonblock": HorizonBlock = byte.Parse(value); break; + case "cloudsheight": CloudsHeight = short.Parse(value); break; + case "maxfog": MaxFog = short.Parse(value); break; + + case "texture": TerrainUrl = value; break; + case "texturepack": TextureUrl = value; break; + case "clouds-speed": CloudsSpeed = int.Parse(value); break; + case "weather-speed": WeatherSpeed = int.Parse(value); break; + case "weather-fade": WeatherFade = int.Parse(value); break; } } diff --git a/Levels/IO/LvlProperties.cs b/Levels/IO/LvlProperties.cs index b1273566f..c575b8d59 100644 --- a/Levels/IO/LvlProperties.cs +++ b/Levels/IO/LvlProperties.cs @@ -29,15 +29,6 @@ namespace MCGalaxy.Levels.IO { Server.s.Log("Failed to save level properties!"); Logger.WriteError(ex); return; - } - - path = Path.ChangeExtension(path, ".env"); - try { - using (CP437Writer writer = new CP437Writer(path)) - WriteEnvProperties(level, writer); - } catch (Exception ex) { - Server.s.Log("Failed to save environment properties"); - Logger.WriteError(ex); } } @@ -48,32 +39,13 @@ namespace MCGalaxy.Levels.IO { ConfigElement.Serialise(Server.levelConfig, " settings", w, level); } - static void WriteEnvProperties(Level level, StreamWriter writer) { - if(level.CloudColor != null) - writer.WriteLine("CloudColor = " + level.CloudColor); - if (level.SkyColor != null) - writer.WriteLine("SkyColor = " + level.SkyColor); - if (level.LightColor != null) - writer.WriteLine("LightColor = " + level.LightColor); - if (level.ShadowColor != null) - writer.WriteLine("ShadowColor = " + level.ShadowColor); - if (level.FogColor != null) - writer.WriteLine("FogColor = " + level.FogColor); - - writer.WriteLine("EdgeLevel = " + level.EdgeLevel); - writer.WriteLine("CloudsHeight = " + level.CloudsHeight); - writer.WriteLine("MaxFog = " + level.MaxFogDistance); - writer.WriteLine("EdgeBlock = " + level.EdgeBlock); - writer.WriteLine("HorizonBlock = " + level.HorizonBlock); - } - public static void Load(Level level, string path) { PropertiesFile.Read(path, ref level, PropLineProcessor); } - public static void LoadEnv(Level level, string name) { - string path = "levels/level properties/" + name + ".env"; + public static void LoadEnv(Level level) { + string path = "levels/level properties/" + level.name + ".env"; PropertiesFile.Read(path, ref level, EnvLineProcessor); } diff --git a/Levels/Level.cs b/Levels/Level.cs index 7d0b46f2b..a6428a318 100644 --- a/Levels/Level.cs +++ b/Levels/Level.cs @@ -117,31 +117,39 @@ namespace MCGalaxy { public bool growTrees; [ConfigBool("Guns", "General", null, false)] public bool guns = false; - public byte jailrotx, jailroty; + /// Color of the clouds (RGB packed into an int). Set to -1 to use client defaults. - public string CloudColor = null; + [ConfigString("CloudColor", "Env", null, "", true)] + public string CloudColor = ""; /// Color of the fog (RGB packed into an int). Set to -1 to use client defaults. - public string FogColor = null; + [ConfigString("FogColor", "Env", null, "", true)] + public string FogColor = ""; /// Color of the sky (RGB packed into an int). Set to -1 to use client defaults. - public string SkyColor = null; + [ConfigString("SkyColor", "Env", null, "", true)] + public string SkyColor = ""; /// Color of the blocks in shadows (RGB packed into an int). Set to -1 to use client defaults. - public string ShadowColor = null; + [ConfigString("ShadowColor", "Env", null, "", true)] + public string ShadowColor = ""; /// Color of the blocks in the light (RGB packed into an int). Set to -1 to use client defaults. - public string LightColor = null; + [ConfigString("LightColor", "Env", null, "", true)] + public string LightColor = ""; /// Elevation of the "ocean" that surrounds maps. Default is map height / 2. + [ConfigInt("EdgeLevel", "Env", null, -1, short.MinValue, short.MaxValue)] public int EdgeLevel; /// Elevation of the clouds. Default is map height + 2. + [ConfigInt("CloudsHeight", "Env", null, -1, short.MinValue, short.MaxValue)] public int CloudsHeight; /// Max fog distance the client can see. /// Default is 0, meaning use the client-side defined maximum fog distance. + [ConfigInt("MaxFog", "Env", null, 0, short.MinValue, short.MaxValue)] public int MaxFogDistance; /// Clouds speed, in units of 256ths. Default is 256 (1 speed). @@ -157,13 +165,16 @@ namespace MCGalaxy { public int WeatherFade = 128; /// The block which will be displayed on the horizon. - public byte HorizonBlock = Block.water; + [ConfigInt("HorizonBlock", "Env", null, Block.water, 0, 255)] + public int HorizonBlock = Block.water; /// The block which will be displayed on the edge of the map. - public byte EdgeBlock = Block.blackrock; + [ConfigInt("EdgeBlock", "Env", null, Block.blackrock, 0, 255)] + public int EdgeBlock = Block.blackrock; public BlockDefinition[] CustomBlockDefs; + [ConfigInt("JailX", "Jail", null, 0, 0, 65535)] public int jailx; [ConfigInt("JailY", "Jail", null, 0, 0, 65535)] @@ -594,7 +605,8 @@ namespace MCGalaxy { LvlProperties.Load(level, propsPath); else Server.s.Log(".properties file for level " + level.name + " was not found."); - LvlProperties.LoadEnv(level, level.name); + // Backwards compatibility for older levels which had .env files. + LvlProperties.LoadEnv(level); } catch (Exception e) { Server.ErrorLog(e); } diff --git a/Levels/LevelEnv.cs b/Levels/LevelEnv.cs index 1ee5352ee..761de3165 100644 --- a/Levels/LevelEnv.cs +++ b/Levels/LevelEnv.cs @@ -49,7 +49,7 @@ namespace MCGalaxy { } public static void SetBlock(Player p, string value, EnvProp prop, - string variable, byte defValue, ref byte target) { + string variable, byte defValue, ref int target) { if (IsResetString(value)) { Player.Message(p, "Reset {0} for {0}%S to normal", variable, p.level.name); target = defValue; @@ -93,7 +93,7 @@ namespace MCGalaxy { SendEnvColorPackets(p, envType, value); } - static bool CheckBlock(Player p, string value, string variable, ref byte modify) { + static bool CheckBlock(Player p, string value, string variable, ref int modify) { byte extBlock = 0; int block = DrawCmd.GetBlock(p, value, out extBlock, false); if (block == -1 || block == Block.Zero) return false; diff --git a/Player/Player.CPE.cs b/Player/Player.CPE.cs index 5c9ba2fd8..09a506231 100644 --- a/Player/Player.CPE.cs +++ b/Player/Player.CPE.cs @@ -159,7 +159,7 @@ namespace MCGalaxy string lastUrl = ""; public void SendCurrentMapAppearance() { - byte side = level.EdgeBlock, edge = level.HorizonBlock; + byte side = (byte)level.EdgeBlock, edge = (byte)level.HorizonBlock; if (side >= Block.CpeCount && !hasBlockDefs) side = level.GetFallback(side); if (edge >= Block.CpeCount && !hasBlockDefs) diff --git a/Server/Server.Fields.cs b/Server/Server.Fields.cs index 7f400b9b8..ba2232828 100644 --- a/Server/Server.Fields.cs +++ b/Server/Server.Fields.cs @@ -50,7 +50,7 @@ namespace MCGalaxy { public event VoidHandler OnSettingsUpdate; public static IRCBot IRC; - public static Thread locationChecker, blockThread; + public static Thread locationChecker; public static WebServer APIServer, InfoServer; public static DateTime StartTime, StartTimeLocal; diff --git a/Server/Server.Init.cs b/Server/Server.Init.cs index 4e6eeba36..d8e7209f6 100644 --- a/Server/Server.Init.cs +++ b/Server/Server.Init.cs @@ -161,6 +161,50 @@ namespace MCGalaxy { } } + void CombineEnvFiles() { + if (!Directory.Exists("levels/level properties")) return; + try { + string[] files = Directory.GetFiles("levels/level properties", "*.env"); + if (files.Length == 0) return; + + Server.s.Log("Combining " + files.Length + " .env and .properties files.."); + foreach (string envFile in files) { + try { + Combine(envFile); + } catch (Exception ex) { + Server.s.Log("Error while trying to combine .env and .properties file"); + Server.ErrorLog(ex); + } + } + Server.s.Log("Finished combining .env and .properties files."); + } catch (Exception ex) { + Server.ErrorLog(ex); + } + } + + static void Combine(string envFile) { + string name = Path.GetFileNameWithoutExtension(envFile); + string propFile = LevelInfo.FindPropertiesFile(name); + List lines = new List(); + string line = null; + + if (propFile != null) { + using (StreamReader r = new StreamReader(propFile)) { + while ((line = r.ReadLine()) != null) + lines.Add(line); + } + } + + using (StreamReader r = new StreamReader(envFile)) { + while ((line = r.ReadLine()) != null) + lines.Add(line); + } + + propFile = LevelInfo.PropertiesPath(name); + CP437Writer.WriteAllLines(propFile, lines.ToArray()); + File.Delete(envFile); + } + void SetupSocket() { Log("Creating listening socket on port " + port + "... "); Setup(); diff --git a/Server/Server.cs b/Server/Server.cs index 79df36810..35723df76 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -105,14 +105,15 @@ namespace MCGalaxy { Level[] loaded = LevelInfo.Loaded.Items; foreach (Level l in loaded) l.Unload(); - + + Background.QueueOnce(CombineEnvFiles); Background.QueueOnce(LoadMainLevel); Plugin.Load(); Background.QueueOnce(LoadPlayerLists); Background.QueueOnce(LoadAutoloadCommands); Background.QueueOnce(MovePreviousLevelFiles); - Background.QueueOnce(SetupSocket); + Background.QueueOnce(SetupSocket); Background.QueueOnce(InitTimers); Background.QueueOnce(InitRest); Background.QueueOnce(InitHeartbeat);