mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-16 11:06:06 -04:00
Simplify platform api a little bit
This commit is contained in:
parent
29bca54ef2
commit
5809bd13f6
@ -1,37 +1,34 @@
|
||||
// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
|
||||
using System;
|
||||
|
||||
namespace ClassicalSharp.Events {
|
||||
|
||||
/// <summary> Contains events related to the spawning/despawning of entities,
|
||||
/// and the creation/removal of tab list entries. </summary>
|
||||
public sealed class EntityEvents : EventsBase {
|
||||
|
||||
IdEventArgs idArgs = new IdEventArgs();
|
||||
|
||||
/// <summary> Raised when an entity is spawned in the current world. </summary>
|
||||
public event EventHandler<IdEventArgs> Added;
|
||||
public void RaiseAdded(byte id) { idArgs.Id = id; Raise(Added, idArgs); }
|
||||
|
||||
/// <summary> Raised when an entity is despawned from the current world. </summary>
|
||||
public event EventHandler<IdEventArgs> Removed;
|
||||
public void RaiseRemoved(byte id) { idArgs.Id = id; Raise(Removed, idArgs); }
|
||||
|
||||
/// <summary> Raised when a tab list entry is created. </summary>
|
||||
public event EventHandler<IdEventArgs> TabListEntryAdded;
|
||||
public void RaiseTabEntryAdded(byte id) { idArgs.Id = id; Raise(TabListEntryAdded, idArgs); }
|
||||
|
||||
/// <summary> Raised when a tab list entry is modified. </summary>
|
||||
public event EventHandler<IdEventArgs> TabListEntryChanged;
|
||||
public void RaiseTabListEntryChanged(byte id) { idArgs.Id = id; Raise(TabListEntryChanged, idArgs); }
|
||||
|
||||
/// <summary> Raised when a tab list entry is removed. </summary>
|
||||
public event EventHandler<IdEventArgs> 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 {
|
||||
|
||||
/// <summary> Contains events related to the spawning/despawning of entities,
|
||||
/// and the creation/removal of tab list entries. </summary>
|
||||
public sealed class EntityEvents : EventsBase {
|
||||
|
||||
IdEventArgs idArgs = new IdEventArgs();
|
||||
|
||||
/// <summary> Raised when an entity is spawned in the current world. </summary>
|
||||
public event EventHandler<IdEventArgs> Added;
|
||||
public void RaiseAdded(byte id) { idArgs.Id = id; Raise(Added, idArgs); }
|
||||
|
||||
/// <summary> Raised when an entity is despawned from the current world. </summary>
|
||||
public event EventHandler<IdEventArgs> Removed;
|
||||
public void RaiseRemoved(byte id) { idArgs.Id = id; Raise(Removed, idArgs); }
|
||||
|
||||
/// <summary> Raised when a tab list entry is created. </summary>
|
||||
public event EventHandler<IdEventArgs> TabListEntryAdded;
|
||||
public void RaiseTabEntryAdded(byte id) { idArgs.Id = id; Raise(TabListEntryAdded, idArgs); }
|
||||
|
||||
/// <summary> Raised when a tab list entry is modified. </summary>
|
||||
public event EventHandler<IdEventArgs> TabListEntryChanged;
|
||||
public void RaiseTabListEntryChanged(byte id) { idArgs.Id = id; Raise(TabListEntryChanged, idArgs); }
|
||||
|
||||
/// <summary> Raised when a tab list entry is removed. </summary>
|
||||
public event EventHandler<IdEventArgs> TabListEntryRemoved;
|
||||
public void RaiseTabEntryRemoved(byte id) { idArgs.Id = id; Raise(TabListEntryRemoved, idArgs); }
|
||||
}
|
||||
|
||||
public sealed class IdEventArgs : EventArgs { public byte Id; }
|
||||
}
|
||||
|
@ -86,11 +86,7 @@ namespace ClassicalSharp.Events {
|
||||
public string Text;
|
||||
}
|
||||
|
||||
public sealed class ColourCodeEventArgs : EventArgs {
|
||||
|
||||
/// <summary> ASCII colour code that was changed. </summary>
|
||||
public char Code;
|
||||
}
|
||||
public sealed class ColourCodeEventArgs : EventArgs { public char Code; }
|
||||
|
||||
public sealed class TextureEventArgs : EventArgs {
|
||||
|
||||
|
@ -19,14 +19,8 @@ namespace ClassicalSharp.Events {
|
||||
}
|
||||
|
||||
public sealed class BlockChangedEventArgs : EventArgs {
|
||||
|
||||
/// <summary> Location within the world the block was updated at. </summary>
|
||||
public Vector3I Coords;
|
||||
|
||||
/// <summary> Block ID that was at the given location before. </summary>
|
||||
public BlockID OldBlock;
|
||||
|
||||
/// <summary> Block ID that is now at the given location. </summary>
|
||||
public BlockID Block;
|
||||
}
|
||||
}
|
||||
|
@ -25,17 +25,8 @@ namespace ClassicalSharp.Events {
|
||||
EnvVarEventArgs envArgs = new EnvVarEventArgs();
|
||||
}
|
||||
|
||||
public sealed class LoadingEventArgs : EventArgs {
|
||||
|
||||
/// <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 sealed class LoadingEventArgs : EventArgs { public float Progress; }
|
||||
public sealed class EnvVarEventArgs : EventArgs { public EnvVar Var; }
|
||||
|
||||
public enum EnvVar {
|
||||
SidesBlock,
|
||||
|
@ -152,7 +152,7 @@ namespace ClassicalSharp {
|
||||
/// <summary> Reads a bitmap from the stream (converting it to 32 bits per pixel if necessary),
|
||||
/// and updates the native texture for it. </summary>
|
||||
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();
|
||||
}
|
||||
|
@ -47,13 +47,8 @@ namespace ClassicalSharp.GraphicsAPI {
|
||||
/// <summary> Creates a new native texture with the specified dimensions and using the
|
||||
/// 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
|
||||
/// are powers of two, because otherwise they will not display properly on certain graphics cards. <br/>
|
||||
/// This method returns -1 if the input image is not a 32bpp format. </remarks>
|
||||
/// are powers of two, because otherwise they will not display properly on certain graphics cards. </remarks>
|
||||
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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -12,28 +12,19 @@ namespace ClassicalSharp {
|
||||
|
||||
/// <summary> Abstracts away platform specific operations. </summary>
|
||||
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) {
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user