Simplify platform api a little bit

This commit is contained in:
UnknownShadow200 2018-06-04 14:02:58 +10:00
parent 29bca54ef2
commit 5809bd13f6
12 changed files with 75 additions and 105 deletions

View File

@ -30,8 +30,5 @@ namespace ClassicalSharp.Events {
public void RaiseTabEntryRemoved(byte id) { idArgs.Id = id; Raise(TabListEntryRemoved, idArgs); } public void RaiseTabEntryRemoved(byte id) { idArgs.Id = id; Raise(TabListEntryRemoved, idArgs); }
} }
public sealed class IdEventArgs : EventArgs { public sealed class IdEventArgs : EventArgs { public byte Id; }
public byte Id;
}
} }

View File

@ -86,11 +86,7 @@ namespace ClassicalSharp.Events {
public string Text; public string Text;
} }
public sealed class ColourCodeEventArgs : EventArgs { public sealed class ColourCodeEventArgs : EventArgs { public char Code; }
/// <summary> ASCII colour code that was changed. </summary>
public char Code;
}
public sealed class TextureEventArgs : EventArgs { public sealed class TextureEventArgs : EventArgs {

View File

@ -19,14 +19,8 @@ namespace ClassicalSharp.Events {
} }
public sealed class BlockChangedEventArgs : EventArgs { public sealed class BlockChangedEventArgs : EventArgs {
/// <summary> Location within the world the block was updated at. </summary>
public Vector3I Coords; public Vector3I Coords;
/// <summary> Block ID that was at the given location before. </summary>
public BlockID OldBlock; public BlockID OldBlock;
/// <summary> Block ID that is now at the given location. </summary>
public BlockID Block; public BlockID Block;
} }
} }

View File

@ -25,17 +25,8 @@ namespace ClassicalSharp.Events {
EnvVarEventArgs envArgs = new EnvVarEventArgs(); EnvVarEventArgs envArgs = new EnvVarEventArgs();
} }
public sealed class LoadingEventArgs : EventArgs { public sealed class LoadingEventArgs : EventArgs { public float Progress; }
public sealed class EnvVarEventArgs : EventArgs { public EnvVar Var; }
/// <summary> Percentage of the map that has been fully decompressed, or generated. </summary>
public float Progress;
}
public sealed class EnvVarEventArgs : EventArgs {
/// <summary> Map environment variable that was changed. </summary>
public EnvVar Var;
}
public enum EnvVar { public enum EnvVar {
SidesBlock, SidesBlock,

View File

@ -152,7 +152,7 @@ namespace ClassicalSharp {
/// <summary> Reads a bitmap from the stream (converting it to 32 bits per pixel if necessary), /// <summary> Reads a bitmap from the stream (converting it to 32 bits per pixel if necessary),
/// and updates the native texture for it. </summary> /// and updates the native texture for it. </summary>
public bool UpdateTexture(ref int texId, string file, byte[] data, bool setSkinType) { 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; if (!ValidateBitmap(file, bmp)) return false;
Graphics.DeleteTexture(ref texId); Graphics.DeleteTexture(ref texId);
@ -230,11 +230,11 @@ namespace ClassicalSharp {
void TextureChangedCore(object sender, TextureEventArgs e) { void TextureChangedCore(object sender, TextureEventArgs e) {
if (e.Name == "terrain.png") { if (e.Name == "terrain.png") {
Bitmap atlas = Platform.ReadBmp32Bpp(Drawer2D, e.Data); Bitmap atlas = Platform.ReadBmp(Drawer2D, e.Data);
if (ChangeTerrainAtlas(atlas)) return; if (ChangeTerrainAtlas(atlas)) return;
atlas.Dispose(); atlas.Dispose();
} else if (e.Name == "default.png") { } else if (e.Name == "default.png") {
Bitmap bmp = Platform.ReadBmp32Bpp(Drawer2D, e.Data); Bitmap bmp = Platform.ReadBmp(Drawer2D, e.Data);
Drawer2D.SetFontBitmap(bmp); Drawer2D.SetFontBitmap(bmp);
Events.RaiseChatFontChanged(); Events.RaiseChatFontChanged();
} }

View File

@ -47,13 +47,8 @@ namespace ClassicalSharp.GraphicsAPI {
/// <summary> Creates a new native texture with the specified dimensions and using the /// <summary> Creates a new native texture with the specified dimensions and using the
/// image data encapsulated by the Bitmap instance. </summary> /// image data encapsulated by the Bitmap instance. </summary>
/// <remarks> Note that should make every effort you can to ensure that the dimensions of the bitmap /// <remarks> 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. <br/> /// are powers of two, because otherwise they will not display properly on certain graphics cards. </remarks>
/// This method returns -1 if the input image is not a 32bpp format. </remarks>
public int CreateTexture(Bitmap bmp, bool managedPool, bool mipmaps) { 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); bmpBuffer.SetData(bmp, true, true);
return CreateTexture(bmpBuffer, managedPool, mipmaps); return CreateTexture(bmpBuffer, managedPool, mipmaps);
} }

View File

@ -268,7 +268,7 @@ namespace ClassicalSharp.Network {
object DownloadContent(Request request, HttpWebResponse response) { object DownloadContent(Request request, HttpWebResponse response) {
if (request.Type == RequestType.Bitmap) { if (request.Type == RequestType.Bitmap) {
MemoryStream data = DownloadBytes(response); MemoryStream data = DownloadBytes(response);
Bitmap bmp = Platform.ReadBmp32Bpp(drawer, data); Bitmap bmp = Platform.ReadBmp(drawer, data);
if (bmp == null) { if (bmp == null) {
Utils.LogDebug("Failed to download from: " + request.Url); Utils.LogDebug("Failed to download from: " + request.Url);

View File

@ -12,28 +12,19 @@ namespace ClassicalSharp {
/// <summary> Abstracts away platform specific operations. </summary> /// <summary> Abstracts away platform specific operations. </summary>
public static class Platform { public static class Platform {
public static string AppDirectory; public static string AppDirectory;
public static bool ValidBitmap(Bitmap bmp) { public static Bitmap ReadBmp(IDrawer2D drawer, byte[] data) {
// Mono seems to be returning a bitmap with a native pointer of zero in some weird cases. return ReadBmp(drawer, new MemoryStream(data));
// 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) { public static Bitmap ReadBmp(IDrawer2D drawer, Stream src) {
return ReadBmp32Bpp(drawer, new MemoryStream(data)); #if !ANDROID
} Bitmap bmp = new Bitmap(src);
#else
Bitmap bmp = BitmapFactory.DecodeStream(src);
#endif
public static Bitmap ReadBmp32Bpp(IDrawer2D drawer, Stream src) {
Bitmap bmp = ReadBmp(src);
if (!ValidBitmap(bmp)) return null; if (!ValidBitmap(bmp)) return null;
if (!Is32Bpp(bmp)) drawer.ConvertTo32Bpp(ref bmp); if (!Is32Bpp(bmp)) drawer.ConvertTo32Bpp(ref bmp);
return bmp; return bmp;
@ -47,14 +38,6 @@ namespace ClassicalSharp {
#endif #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) { public static void WriteBmp(Bitmap bmp, Stream dst) {
#if !ANDROID #if !ANDROID
bmp.Save(dst, ImageFormat.Png); bmp.Save(dst, ImageFormat.Png);
@ -63,6 +46,19 @@ namespace ClassicalSharp {
#endif #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) { public static bool Is32Bpp(Bitmap bmp) {
#if !ANDROID #if !ANDROID
PixelFormat format = bmp.PixelFormat; PixelFormat format = bmp.PixelFormat;
@ -73,6 +69,7 @@ namespace ClassicalSharp {
#endif #endif
} }
static string FullPath(string relPath) { return Path.Combine(AppDirectory, relPath); } static string FullPath(string relPath) { return Path.Combine(AppDirectory, relPath); }
public static FileStream FileOpen(string relPath) { public static FileStream FileOpen(string relPath) {

View File

@ -39,7 +39,7 @@ namespace ClassicalSharp.Textures {
void TextureChanged(object sender, TextureEventArgs e) { void TextureChanged(object sender, TextureEventArgs e) {
if (e.Name == "animations.png" || e.Name == "animation.png") { 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); animsBuffer = new FastBitmap(animBmp, true, true);
} else if (e.Name == "animations.txt" || e.Name == "animation.txt") { } else if (e.Name == "animations.txt" || e.Name == "animation.txt") {
MemoryStream stream = new MemoryStream(e.Data); MemoryStream stream = new MemoryStream(e.Data);

View File

@ -109,7 +109,7 @@ namespace ClassicalSharp.Textures {
static Bitmap GetBitmap(IDrawer2D drawer, Stream src) { static Bitmap GetBitmap(IDrawer2D drawer, Stream src) {
try { try {
return Platform.ReadBmp32Bpp(drawer, src); return Platform.ReadBmp(drawer, src);
} catch (ArgumentException ex) { } catch (ArgumentException ex) {
ErrorHandler.LogError("Cache.GetBitmap", ex); ErrorHandler.LogError("Cache.GetBitmap", ex);
return null; return null;

View File

@ -57,14 +57,14 @@ namespace Launcher {
if (filename == "default.png") { if (filename == "default.png") {
if (fontPng) return; if (fontPng) return;
Bitmap bmp = Platform.ReadBmp32Bpp(Drawer, data); Bitmap bmp = Platform.ReadBmp(Drawer, data);
Drawer.SetFontBitmap(bmp); Drawer.SetFontBitmap(bmp);
useBitmappedFont = !Options.GetBool(OptionsKey.UseChatFont, false); useBitmappedFont = !Options.GetBool(OptionsKey.UseChatFont, false);
fontPng = true; fontPng = true;
} else if (filename == "terrain.png") { } else if (filename == "terrain.png") {
if (terrainPng) return; if (terrainPng) return;
Bitmap bmp = Platform.ReadBmp32Bpp(Drawer, data); Bitmap bmp = Platform.ReadBmp(Drawer, data);
MakeClassicTextures(bmp); MakeClassicTextures(bmp);
bmp.Dispose(); bmp.Dispose();
terrainPng = true; terrainPng = true;

View File

@ -50,7 +50,7 @@ namespace Launcher.Patcher {
ExtractClassic(); ExtractClassic();
ExtractModern(); ExtractModern();
if (pngGuiPatch != null) { if (pngGuiPatch != null) {
using (Bitmap gui = Platform.ReadBmp32Bpp(drawer, pngGuiPatch)) using (Bitmap gui = Platform.ReadBmp(drawer, pngGuiPatch))
writer.WriteNewImage(gui, "gui.png"); writer.WriteNewImage(gui, "gui.png");
} }
if (patchedTerrain) { if (patchedTerrain) {
@ -103,8 +103,8 @@ namespace Launcher.Patcher {
if (!existing.Contains(entry.Filename)) if (!existing.Contains(entry.Filename))
writer.WriteZipEntry(entry, data); writer.WriteZipEntry(entry, data);
} else if (!existing.Contains("terrain.png")){ } else if (!existing.Contains("terrain.png")){
terrainBmp = Platform.ReadBmp32Bpp(drawer, data); terrainBmp = Platform.ReadBmp(drawer, data);
using (Bitmap mask = Platform.ReadBmp32Bpp(drawer, pngTerrainPatch)) { using (Bitmap mask = Platform.ReadBmp(drawer, pngTerrainPatch)) {
CopyTile( 0, 0, 3 * 16, 3 * 16, mask, terrainBmp); CopyTile( 0, 0, 3 * 16, 3 * 16, mask, terrainBmp);
CopyTile(16, 0, 6 * 16, 3 * 16, mask, terrainBmp); CopyTile(16, 0, 6 * 16, 3 * 16, mask, terrainBmp);
CopyTile(32, 0, 6 * 16, 2 * 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"; 6 2 0 0 16 32 0";
void PatchBlock(byte[] data, int x, int y) { 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); CopyTile(0, 0, x * 16, y * 16, bmp, terrainBmp);
} }
patchedTerrain = true; patchedTerrain = true;
} }
void PatchAnim(byte[] data) { 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) { for (int i = 0; i < bmp.Height; i += 16) {
CopyTile(0, i, i, 0, bmp, animsBmp); CopyTile(0, i, i, 0, bmp, animsBmp);
} }