diff --git a/ClassicalSharp/Events/EntityEvents.cs b/ClassicalSharp/Events/EntityEvents.cs index a28956d23..2213b84ae 100644 --- a/ClassicalSharp/Events/EntityEvents.cs +++ b/ClassicalSharp/Events/EntityEvents.cs @@ -1,37 +1,34 @@ -// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; - -namespace ClassicalSharp.Events { - - /// Contains events related to the spawning/despawning of entities, - /// and the creation/removal of tab list entries. - public sealed class EntityEvents : EventsBase { - - IdEventArgs idArgs = new IdEventArgs(); - - /// Raised when an entity is spawned in the current world. - public event EventHandler Added; - public void RaiseAdded(byte id) { idArgs.Id = id; Raise(Added, idArgs); } - - /// Raised when an entity is despawned from the current world. - public event EventHandler Removed; - public void RaiseRemoved(byte id) { idArgs.Id = id; Raise(Removed, idArgs); } - - /// Raised when a tab list entry is created. - public event EventHandler TabListEntryAdded; - public void RaiseTabEntryAdded(byte id) { idArgs.Id = id; Raise(TabListEntryAdded, idArgs); } - - /// Raised when a tab list entry is modified. - public event EventHandler TabListEntryChanged; - public void RaiseTabListEntryChanged(byte id) { idArgs.Id = id; Raise(TabListEntryChanged, idArgs); } - - /// Raised when a tab list entry is removed. - public event EventHandler TabListEntryRemoved; - public void RaiseTabEntryRemoved(byte id) { idArgs.Id = id; Raise(TabListEntryRemoved, idArgs); } - } - - public sealed class IdEventArgs : EventArgs { - - public byte Id; - } -} +// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 +using System; + +namespace ClassicalSharp.Events { + + /// Contains events related to the spawning/despawning of entities, + /// and the creation/removal of tab list entries. + public sealed class EntityEvents : EventsBase { + + IdEventArgs idArgs = new IdEventArgs(); + + /// Raised when an entity is spawned in the current world. + public event EventHandler Added; + public void RaiseAdded(byte id) { idArgs.Id = id; Raise(Added, idArgs); } + + /// Raised when an entity is despawned from the current world. + public event EventHandler Removed; + public void RaiseRemoved(byte id) { idArgs.Id = id; Raise(Removed, idArgs); } + + /// Raised when a tab list entry is created. + public event EventHandler TabListEntryAdded; + public void RaiseTabEntryAdded(byte id) { idArgs.Id = id; Raise(TabListEntryAdded, idArgs); } + + /// Raised when a tab list entry is modified. + public event EventHandler TabListEntryChanged; + public void RaiseTabListEntryChanged(byte id) { idArgs.Id = id; Raise(TabListEntryChanged, idArgs); } + + /// Raised when a tab list entry is removed. + public event EventHandler TabListEntryRemoved; + public void RaiseTabEntryRemoved(byte id) { idArgs.Id = id; Raise(TabListEntryRemoved, idArgs); } + } + + public sealed class IdEventArgs : EventArgs { public byte Id; } +} diff --git a/ClassicalSharp/Events/Events.cs b/ClassicalSharp/Events/Events.cs index d44ac0f17..adf228168 100644 --- a/ClassicalSharp/Events/Events.cs +++ b/ClassicalSharp/Events/Events.cs @@ -86,11 +86,7 @@ namespace ClassicalSharp.Events { public string Text; } - public sealed class ColourCodeEventArgs : EventArgs { - - /// ASCII colour code that was changed. - public char Code; - } + public sealed class ColourCodeEventArgs : EventArgs { public char Code; } public sealed class TextureEventArgs : EventArgs { diff --git a/ClassicalSharp/Events/UserEvents.cs b/ClassicalSharp/Events/UserEvents.cs index 6930aeb40..5b8048248 100644 --- a/ClassicalSharp/Events/UserEvents.cs +++ b/ClassicalSharp/Events/UserEvents.cs @@ -19,14 +19,8 @@ namespace ClassicalSharp.Events { } public sealed class BlockChangedEventArgs : EventArgs { - - /// Location within the world the block was updated at. public Vector3I Coords; - - /// Block ID that was at the given location before. public BlockID OldBlock; - - /// Block ID that is now at the given location. public BlockID Block; } } diff --git a/ClassicalSharp/Events/WorldEvents.cs b/ClassicalSharp/Events/WorldEvents.cs index 6b40af213..5b16fa1e6 100644 --- a/ClassicalSharp/Events/WorldEvents.cs +++ b/ClassicalSharp/Events/WorldEvents.cs @@ -25,17 +25,8 @@ namespace ClassicalSharp.Events { EnvVarEventArgs envArgs = new EnvVarEventArgs(); } - public sealed class LoadingEventArgs : EventArgs { - - /// Percentage of the map that has been fully decompressed, or generated. - public float Progress; - } - - public sealed class EnvVarEventArgs : EventArgs { - - /// Map environment variable that was changed. - public EnvVar Var; - } + public sealed class LoadingEventArgs : EventArgs { public float Progress; } + public sealed class EnvVarEventArgs : EventArgs { public EnvVar Var; } public enum EnvVar { SidesBlock, diff --git a/ClassicalSharp/Game/Game.cs b/ClassicalSharp/Game/Game.cs index 67a3bbf4f..f5bfb4601 100644 --- a/ClassicalSharp/Game/Game.cs +++ b/ClassicalSharp/Game/Game.cs @@ -152,7 +152,7 @@ namespace ClassicalSharp { /// Reads a bitmap from the stream (converting it to 32 bits per pixel if necessary), /// and updates the native texture for it. public bool UpdateTexture(ref int texId, string file, byte[] data, bool setSkinType) { - using (Bitmap bmp = Platform.ReadBmp32Bpp(Drawer2D, data)) { + using (Bitmap bmp = Platform.ReadBmp(Drawer2D, data)) { if (!ValidateBitmap(file, bmp)) return false; Graphics.DeleteTexture(ref texId); @@ -230,11 +230,11 @@ namespace ClassicalSharp { void TextureChangedCore(object sender, TextureEventArgs e) { if (e.Name == "terrain.png") { - Bitmap atlas = Platform.ReadBmp32Bpp(Drawer2D, e.Data); + Bitmap atlas = Platform.ReadBmp(Drawer2D, e.Data); if (ChangeTerrainAtlas(atlas)) return; atlas.Dispose(); } else if (e.Name == "default.png") { - Bitmap bmp = Platform.ReadBmp32Bpp(Drawer2D, e.Data); + Bitmap bmp = Platform.ReadBmp(Drawer2D, e.Data); Drawer2D.SetFontBitmap(bmp); Events.RaiseChatFontChanged(); } diff --git a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs index 95ea474d2..17e69aec4 100644 --- a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs +++ b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs @@ -47,13 +47,8 @@ namespace ClassicalSharp.GraphicsAPI { /// Creates a new native texture with the specified dimensions and using the /// image data encapsulated by the Bitmap instance. /// Note that should make every effort you can to ensure that the dimensions of the bitmap - /// are powers of two, because otherwise they will not display properly on certain graphics cards.
- /// This method returns -1 if the input image is not a 32bpp format.
+ /// are powers of two, because otherwise they will not display properly on certain graphics cards. public int CreateTexture(Bitmap bmp, bool managedPool, bool mipmaps) { - if (!Platform.Is32Bpp(bmp)) { - throw new ArgumentOutOfRangeException("Bitmap must be 32bpp"); - } - bmpBuffer.SetData(bmp, true, true); return CreateTexture(bmpBuffer, managedPool, mipmaps); } diff --git a/ClassicalSharp/Network/Utils/AsyncDownloader.cs b/ClassicalSharp/Network/Utils/AsyncDownloader.cs index f915c959d..e6a78218e 100644 --- a/ClassicalSharp/Network/Utils/AsyncDownloader.cs +++ b/ClassicalSharp/Network/Utils/AsyncDownloader.cs @@ -268,7 +268,7 @@ namespace ClassicalSharp.Network { object DownloadContent(Request request, HttpWebResponse response) { if (request.Type == RequestType.Bitmap) { MemoryStream data = DownloadBytes(response); - Bitmap bmp = Platform.ReadBmp32Bpp(drawer, data); + Bitmap bmp = Platform.ReadBmp(drawer, data); if (bmp == null) { Utils.LogDebug("Failed to download from: " + request.Url); diff --git a/ClassicalSharp/Platform/Platform.cs b/ClassicalSharp/Platform/Platform.cs index 05415f4dc..b5d3dc943 100644 --- a/ClassicalSharp/Platform/Platform.cs +++ b/ClassicalSharp/Platform/Platform.cs @@ -12,28 +12,19 @@ namespace ClassicalSharp { /// Abstracts away platform specific operations. public static class Platform { - public static string AppDirectory; - public static bool ValidBitmap(Bitmap bmp) { - // Mono seems to be returning a bitmap with a native pointer of zero in some weird cases. - // We can detect this as property access raises an ArgumentException. - try { - int height = bmp.Height; - PixelFormat format = bmp.PixelFormat; - // make sure these are not optimised out - return height != -1 && format != PixelFormat.Undefined; - } catch (ArgumentException) { - return false; - } - } - - public static Bitmap ReadBmp32Bpp(IDrawer2D drawer, byte[] data) { - return ReadBmp32Bpp(drawer, new MemoryStream(data)); + public static Bitmap ReadBmp(IDrawer2D drawer, byte[] data) { + return ReadBmp(drawer, new MemoryStream(data)); } - public static Bitmap ReadBmp32Bpp(IDrawer2D drawer, Stream src) { - Bitmap bmp = ReadBmp(src); + public static Bitmap ReadBmp(IDrawer2D drawer, Stream src) { + #if !ANDROID + Bitmap bmp = new Bitmap(src); + #else + Bitmap bmp = BitmapFactory.DecodeStream(src); + #endif + if (!ValidBitmap(bmp)) return null; if (!Is32Bpp(bmp)) drawer.ConvertTo32Bpp(ref bmp); return bmp; @@ -46,14 +37,6 @@ namespace ClassicalSharp { return Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888); #endif } - - public static Bitmap ReadBmp(Stream src) { - #if !ANDROID - return new Bitmap(src); - #else - return BitmapFactory.DecodeStream(src); - #endif - } public static void WriteBmp(Bitmap bmp, Stream dst) { #if !ANDROID @@ -62,7 +45,20 @@ namespace ClassicalSharp { bmp.Compress(Bitmap.CompressFormat.Png, 100, dst); #endif } - + + static bool ValidBitmap(Bitmap bmp) { + // Mono seems to be returning a bitmap with a native pointer of zero in some weird cases. + // We can detect this as property access raises an ArgumentException. + try { + int height = bmp.Height; + PixelFormat format = bmp.PixelFormat; + // make sure these are not optimised out + return height != -1 && format != PixelFormat.Undefined; + } catch (ArgumentException) { + return false; + } + } + public static bool Is32Bpp(Bitmap bmp) { #if !ANDROID PixelFormat format = bmp.PixelFormat; @@ -73,6 +69,7 @@ namespace ClassicalSharp { #endif } + static string FullPath(string relPath) { return Path.Combine(AppDirectory, relPath); } public static FileStream FileOpen(string relPath) { diff --git a/ClassicalSharp/TexturePack/Animations.cs b/ClassicalSharp/TexturePack/Animations.cs index 480c8a893..803703b78 100644 --- a/ClassicalSharp/TexturePack/Animations.cs +++ b/ClassicalSharp/TexturePack/Animations.cs @@ -39,7 +39,7 @@ namespace ClassicalSharp.Textures { void TextureChanged(object sender, TextureEventArgs e) { if (e.Name == "animations.png" || e.Name == "animation.png") { - animBmp = Platform.ReadBmp32Bpp(game.Drawer2D, e.Data); + animBmp = Platform.ReadBmp(game.Drawer2D, e.Data); animsBuffer = new FastBitmap(animBmp, true, true); } else if (e.Name == "animations.txt" || e.Name == "animation.txt") { MemoryStream stream = new MemoryStream(e.Data); diff --git a/ClassicalSharp/TexturePack/TexturePack.cs b/ClassicalSharp/TexturePack/TexturePack.cs index f212ab009..98bf322f4 100644 --- a/ClassicalSharp/TexturePack/TexturePack.cs +++ b/ClassicalSharp/TexturePack/TexturePack.cs @@ -109,7 +109,7 @@ namespace ClassicalSharp.Textures { static Bitmap GetBitmap(IDrawer2D drawer, Stream src) { try { - return Platform.ReadBmp32Bpp(drawer, src); + return Platform.ReadBmp(drawer, src); } catch (ArgumentException ex) { ErrorHandler.LogError("Cache.GetBitmap", ex); return null; diff --git a/Launcher2/LauncherWindow.Background.cs b/Launcher2/LauncherWindow.Background.cs index 4039d8e5d..aba864a25 100644 --- a/Launcher2/LauncherWindow.Background.cs +++ b/Launcher2/LauncherWindow.Background.cs @@ -57,14 +57,14 @@ namespace Launcher { if (filename == "default.png") { if (fontPng) return; - Bitmap bmp = Platform.ReadBmp32Bpp(Drawer, data); + Bitmap bmp = Platform.ReadBmp(Drawer, data); Drawer.SetFontBitmap(bmp); useBitmappedFont = !Options.GetBool(OptionsKey.UseChatFont, false); fontPng = true; } else if (filename == "terrain.png") { if (terrainPng) return; - Bitmap bmp = Platform.ReadBmp32Bpp(Drawer, data); + Bitmap bmp = Platform.ReadBmp(Drawer, data); MakeClassicTextures(bmp); bmp.Dispose(); terrainPng = true; diff --git a/Launcher2/Patcher/ResourcePatcher.cs b/Launcher2/Patcher/ResourcePatcher.cs index 9ee33c42b..c1cc21d6f 100644 --- a/Launcher2/Patcher/ResourcePatcher.cs +++ b/Launcher2/Patcher/ResourcePatcher.cs @@ -50,7 +50,7 @@ namespace Launcher.Patcher { ExtractClassic(); ExtractModern(); if (pngGuiPatch != null) { - using (Bitmap gui = Platform.ReadBmp32Bpp(drawer, pngGuiPatch)) + using (Bitmap gui = Platform.ReadBmp(drawer, pngGuiPatch)) writer.WriteNewImage(gui, "gui.png"); } if (patchedTerrain) { @@ -103,8 +103,8 @@ namespace Launcher.Patcher { if (!existing.Contains(entry.Filename)) writer.WriteZipEntry(entry, data); } else if (!existing.Contains("terrain.png")){ - terrainBmp = Platform.ReadBmp32Bpp(drawer, data); - using (Bitmap mask = Platform.ReadBmp32Bpp(drawer, pngTerrainPatch)) { + terrainBmp = Platform.ReadBmp(drawer, data); + using (Bitmap mask = Platform.ReadBmp(drawer, pngTerrainPatch)) { CopyTile( 0, 0, 3 * 16, 3 * 16, mask, terrainBmp); CopyTile(16, 0, 6 * 16, 3 * 16, mask, terrainBmp); CopyTile(32, 0, 6 * 16, 2 * 16, mask, terrainBmp); @@ -228,14 +228,14 @@ namespace Launcher.Patcher { 6 2 0 0 16 32 0"; void PatchBlock(byte[] data, int x, int y) { - using (Bitmap bmp = Platform.ReadBmp32Bpp(drawer, data)) { + using (Bitmap bmp = Platform.ReadBmp(drawer, data)) { CopyTile(0, 0, x * 16, y * 16, bmp, terrainBmp); } patchedTerrain = true; } void PatchAnim(byte[] data) { - using (Bitmap bmp = Platform.ReadBmp32Bpp(drawer, data)) { + using (Bitmap bmp = Platform.ReadBmp(drawer, data)) { for (int i = 0; i < bmp.Height; i += 16) { CopyTile(0, i, i, 0, bmp, animsBmp); }