From 4f4427b357667d30bf6594408b16872f0c8acf7c Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 1 Mar 2018 08:17:33 +1100 Subject: [PATCH] Allow using 3 and 6 char codes for hex colours. --- MCGalaxy/Chat/Colors.cs | 30 ++++++++++++++------- MCGalaxy/Commands/CommandParser.cs | 9 +++---- MCGalaxy/Commands/Information/CmdMapInfo.cs | 2 +- MCGalaxy/Drawing/Image/ImagePrintDrawOp.cs | 16 +++++------ MCGalaxy/Levels/BlockQueue.cs | 2 +- MCGalaxy/Levels/Level.Blocks.cs | 2 +- MCGalaxy/Levels/Zone.cs | 10 +++---- MCGalaxy/Player/Player.CPE.cs | 10 +++---- MCGalaxy/util/Utils.cs | 9 ------- 9 files changed, 42 insertions(+), 48 deletions(-) diff --git a/MCGalaxy/Chat/Colors.cs b/MCGalaxy/Chat/Colors.cs index 19bb8ca55..a28d60a8c 100644 --- a/MCGalaxy/Chat/Colors.cs +++ b/MCGalaxy/Chat/Colors.cs @@ -314,13 +314,16 @@ namespace MCGalaxy { /// Parses an #RRGGBB hex color string. - public static ColorDesc ParseHex(string hex) { - if (hex.Length > 0 && hex[0] == '#') hex = hex.Remove(0, 1); - if (!(hex.Length == 3 || hex.Length == 6)) { - throw new ArgumentException("hex must be either 3 or 6 chars long"); - } + public static bool TryParseHex(string hex, out ColorDesc c) { + c = default(ColorDesc); + if (hex == null || hex.Length == 0) return false; + if (hex[0] == '#') hex = hex.Remove(0, 1); + if (!(hex.Length == 3 || hex.Length == 6)) return false; - ColorDesc c = default(ColorDesc); + for (int i = 0; i < hex.Length; i++) { + if (Hex(hex[i]) == -1) return false; + } + int R, G, B; if (hex.Length == 6) { R = (Hex(hex[0]) << 4) | Hex(hex[1]); @@ -331,20 +334,27 @@ namespace MCGalaxy { G = Hex(hex[1]); G |= (G << 4); B = Hex(hex[2]); B |= (B << 4); } - + c.R = (byte)R; c.G = (byte)G; c.B = (byte)B; c.A = 255; + return true; + } + + /// Parses an #RRGGBB hex color string. + public static ColorDesc ParseHex(string hex) { + ColorDesc c; + if (!TryParseHex(hex, out c)) throw new ArgumentException("invalid input"); return c; } - /// Gets the index of the given hex character. - public static int Hex(char value) { + static int Hex(char value) { if (value >= '0' && value <= '9') return (int)(value - '0'); if (value >= 'a' && value <= 'f') return (int)(value - 'a') + 10; if (value >= 'A' && value <= 'F') return (int)(value - 'A') + 10; - throw new ArgumentException("Non hex char: " + value); + + return -1; } } diff --git a/MCGalaxy/Commands/CommandParser.cs b/MCGalaxy/Commands/CommandParser.cs index e515d2348..de469c12f 100644 --- a/MCGalaxy/Commands/CommandParser.cs +++ b/MCGalaxy/Commands/CommandParser.cs @@ -135,14 +135,11 @@ namespace MCGalaxy.Commands { /// Attempts to parse the given argument as a hex color. public static bool GetHex(Player p, string input, ref ColorDesc col) { - if (input.Length > 0 && input[0] == '#') - input = input.Substring(1); - - if (!Utils.IsValidHex(input)) { + ColorDesc tmp; + if (!Colors.TryParseHex(input, out tmp)) { Player.Message(p, "\"#{0}\" is not a valid HEX color.", input); return false; } - - col = Colors.ParseHex(input); return true; + col = tmp; return true; } internal static bool GetCoords(Player p, string[] args, int argsOffset, ref Vec3S32 P) { diff --git a/MCGalaxy/Commands/Information/CmdMapInfo.cs b/MCGalaxy/Commands/Information/CmdMapInfo.cs index 39f769b51..083e3e9db 100644 --- a/MCGalaxy/Commands/Information/CmdMapInfo.cs +++ b/MCGalaxy/Commands/Information/CmdMapInfo.cs @@ -174,7 +174,7 @@ namespace MCGalaxy.Commands.Info { Color(cfg.CloudColor), Color(cfg.LightColor), Color(cfg.ShadowColor)); Player.Message(p, "Water level: &b{0}%S, Bedrock offset: &b{1}%S, Clouds height: &b{2}%S, Max fog distance: &b{3}", - cfg.EdgeLevel,cfg.SidesOffset, cfg.CloudsHeight, cfg.MaxFogDistance); + cfg.EdgeLevel, cfg.SidesOffset, cfg.CloudsHeight, cfg.MaxFogDistance); Player.Message(p, "Edge Block: &b{0}%S, Horizon Block: &b{1}", Block.GetName(p, cfg.EdgeBlock), Block.GetName(p, cfg.HorizonBlock)); Player.Message(p, "Clouds speed: &b{0}%%S, Weather speed: &b{1}%", diff --git a/MCGalaxy/Drawing/Image/ImagePrintDrawOp.cs b/MCGalaxy/Drawing/Image/ImagePrintDrawOp.cs index f59a8c762..1f1a6fc84 100644 --- a/MCGalaxy/Drawing/Image/ImagePrintDrawOp.cs +++ b/MCGalaxy/Drawing/Image/ImagePrintDrawOp.cs @@ -74,22 +74,22 @@ namespace MCGalaxy.Drawing.Ops { PaletteEntry[] front = new PaletteEntry[Palette.Entries.Length]; PaletteEntry[] back = new PaletteEntry[Palette.Entries.Length]; - ColorDesc sun = Colors.ParseHex("FFFFFF"); - ColorDesc dark = Colors.ParseHex("9B9B9B"); - if (Utils.IsValidHex(Level.Config.LightColor)) { - sun = Colors.ParseHex(Level.Config.LightColor); + ColorDesc sun, dark, bright; + if (!Colors.TryParseHex(Level.Config.LightColor, out sun)) { + sun = Colors.ParseHex("FFFFFF"); } - if (Utils.IsValidHex(Level.Config.ShadowColor)) { - dark = Colors.ParseHex(Level.Config.ShadowColor); + if (!Colors.TryParseHex(Level.Config.ShadowColor, out dark)) { + dark = Colors.ParseHex("9B9B9B"); } + bright = Colors.ParseHex("FFFFFF"); for (int i = 0; i < Palette.Entries.Length; i++) { PaletteEntry entry = Palette.Entries[i]; BlockDefinition def = Level.GetBlockDef(entry.Block); if (def != null && def.FullBright) { - front[i] = Multiply(entry, Colors.ParseHex("FFFFFF")); - back[i] = Multiply(entry, Colors.ParseHex("FFFFFF")); + front[i] = Multiply(entry, bright); + back[i] = Multiply(entry, bright); } else { front[i] = Multiply(entry, sun); back[i] = Multiply(entry, dark); diff --git a/MCGalaxy/Levels/BlockQueue.cs b/MCGalaxy/Levels/BlockQueue.cs index 478519364..e4fa8044f 100644 --- a/MCGalaxy/Levels/BlockQueue.cs +++ b/MCGalaxy/Levels/BlockQueue.cs @@ -30,7 +30,7 @@ namespace MCGalaxy { const int posShift = 32; const int idShift = 12; - const int blockMask = 0x7FF; + const int blockMask = (1 << 12) - 1; public static void Loop(SchedulerTask task) { Level[] loaded = LevelInfo.Loaded.Items; diff --git a/MCGalaxy/Levels/Level.Blocks.cs b/MCGalaxy/Levels/Level.Blocks.cs index 278a81a3b..5a5ff92de 100644 --- a/MCGalaxy/Levels/Level.Blocks.cs +++ b/MCGalaxy/Levels/Level.Blocks.cs @@ -55,7 +55,7 @@ namespace MCGalaxy { public BlockID GetBlock(ushort x, ushort y, ushort z, out int index) { if (x >= Width || y >= Height || z >= Length || blocks == null) { index = -1; return Block.Invalid; } index = x + Width * (z + y * Length); - byte raw = blocks[x + Width * (z + y * Length)]; + byte raw = blocks[index]; return raw != Block.custom_block ? raw : (BlockID)(Block.Extended | GetExtTileNoCheck(x, y, z)); } diff --git a/MCGalaxy/Levels/Zone.cs b/MCGalaxy/Levels/Zone.cs index c3104bd91..5dc5b3763 100644 --- a/MCGalaxy/Levels/Zone.cs +++ b/MCGalaxy/Levels/Zone.cs @@ -114,14 +114,14 @@ namespace MCGalaxy { MaxX == lvl.Width - 1 && MaxY == lvl.Height - 1 && MaxZ == lvl.Length - 1; } - public bool Shows { get { return Config.ShowAlpha != 0 && Config.ShowColor != ""; } } + public bool Shows { get { return Config.ShowAlpha != 0 && Config.ShowColor.Length > 0; } } public void Show(Player p) { if (!p.Supports(CpeExt.SelectionCuboid) || !Shows) return; - ColorDesc col = Colors.ParseHex(Config.ShowColor); - p.Send(Packet.MakeSelection( - ID, Config.Name, new Vec3U16(MinX, MinY, MinZ), - new Vec3U16((ushort)(MaxX + 1), (ushort)(MaxY + 1), (ushort)(MaxZ + 1)), + ColorDesc col; Colors.TryParseHex(Config.ShowColor, out col); + Vec3U16 min = new Vec3U16(MinX, MinY, MinZ); + Vec3U16 max = new Vec3U16((ushort)(MaxX + 1), (ushort)(MaxY + 1), (ushort)(MaxZ + 1)); + p.Send(Packet.MakeSelection(ID, Config.Name, min, max, col.R, col.G, col.B, (byte)Config.ShowAlpha, p.hasCP437)); } diff --git a/MCGalaxy/Player/Player.CPE.cs b/MCGalaxy/Player/Player.CPE.cs index f01b9f5ea..482b22a12 100644 --- a/MCGalaxy/Player/Player.CPE.cs +++ b/MCGalaxy/Player/Player.CPE.cs @@ -136,14 +136,10 @@ namespace MCGalaxy { } public void SendEnvColor(byte type, string hex) { - if (String.IsNullOrEmpty(hex)) { - Send(Packet.EnvColor(type, -1, -1, -1)); return; - } - - try { - ColorDesc c = Colors.ParseHex(hex); + ColorDesc c; + if (Colors.TryParseHex(hex, out c)) { Send(Packet.EnvColor(type, c.R, c.G, c.B)); - } catch (ArgumentException) { + } else { Send(Packet.EnvColor(type, -1, -1, -1)); } } diff --git a/MCGalaxy/util/Utils.cs b/MCGalaxy/util/Utils.cs index 2fe465a27..e8f179109 100644 --- a/MCGalaxy/util/Utils.cs +++ b/MCGalaxy/util/Utils.cs @@ -25,15 +25,6 @@ namespace MCGalaxy { /// The absolute path on disc of the folder MCGalaxy.exe is currently running from. public static string FolderPath { get { return AppDomain.CurrentDomain.BaseDirectory; } } - - public static bool IsValidHex(string hex) { - if (hex.Length != 6) return false; - - for (int i = 0; i < hex.Length; i++) { - if (!Colors.IsStandard(hex[i])) return false; - } - return true; - } public static string Hex(byte r, byte g, byte b) { return "#" + r.ToString("X2") + g.ToString("X2") + b.ToString("X2");