diff options
| -rw-r--r-- | Bitmap.cs | 29 | ||||
| -rw-r--r-- | Mouse.cs | 6 | ||||
| -rw-r--r-- | MouseNet.cs | 2 | ||||
| -rw-r--r-- | Oneko.cs | 6 | ||||
| -rw-r--r-- | OnekoNet.cs | 2 |
5 files changed, 33 insertions, 12 deletions
@@ -1,5 +1,6 @@ using System.Buffers.Binary; using System.IO.Compression; +using System.Numerics; using Raylib_cs; namespace OnekoOnline; @@ -12,10 +13,16 @@ class Bitmap : IDisposable readonly byte[] SerializedData; - public Bitmap(Image img) + const int MaxWidth = 256; + const int MaxHeight = 256; + + public Bitmap(Image img, int MaxW = 256, int MaxH = 256) { Width = img.Width; Height = img.Height; + //Crop image if too big + if (Width > MaxW || Height > MaxH) Raylib.ImageCrop(ref img, new Rectangle(0f, 0f, MaxW, MaxH)); + Texture = Raylib.LoadTextureFromImage(img); //Get Data for Serialization @@ -59,21 +66,21 @@ class Bitmap : IDisposable Raylib.UnloadImage(img); } - public static Bitmap FromFile(string path) + public static Bitmap FromFile(string path, int MaxW = MaxWidth, int MaxH = MaxHeight) { if (!File.Exists(path) || new FileInfo(path).Length > 40000 || !path.Contains(".png")) return new Bitmap(Raylib.GenImageChecked(32, 32, 4, 4, Color.BLACK, Color.PINK)); byte[] memory = File.ReadAllBytes(path); - return FromPNGMemory(memory); + return FromPNGMemory(memory, MaxW, MaxH); } - public static Bitmap FromPNGMemory(byte[] memory) + public static Bitmap FromPNGMemory(byte[] memory, int MaxW = MaxWidth, int MaxH = MaxHeight) { - return new Bitmap(Raylib.LoadImageFromMemory(".png", memory)); + return new Bitmap(Raylib.LoadImageFromMemory(".png", memory), MaxW, MaxH); } - public static Bitmap Deserialize(ReadOnlySpan<byte> span) + public static Bitmap Deserialize(ReadOnlySpan<byte> span, int MaxW = MaxWidth, int MaxH = MaxHeight, byte[]? fallbackImage = null) { byte[] compressed = span.ToArray(); byte[] data; @@ -87,8 +94,18 @@ class Bitmap : IDisposable int width = BinaryPrimitives.ReadInt16LittleEndian(data.AsSpan(0, sizeof(short))); int height = BinaryPrimitives.ReadInt16LittleEndian(data.AsSpan(sizeof(short), sizeof(short))); + + bool TooBig = width > MaxW || height > MaxH; + if (TooBig) { + width = Math.Clamp(width, 0, MaxW); + height = Math.Clamp(height, 0, MaxH); + if (fallbackImage != null) return FromPNGMemory(fallbackImage, MaxW, MaxH); + } + Image img = Raylib.GenImageChecked(width, height, 4, 4, Color.PINK, Color.BLACK); + if (TooBig) return new Bitmap(img); + int i = sizeof(short)*2; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { @@ -15,6 +15,8 @@ abstract class Mouse : Drawable public static Action<Mouse>? Clicked; + protected readonly static byte[] FallbackImg = EmbeddedResources.GetResource("misc.cursor.png"); + public Mouse() : base() { DrawOrder = 100; @@ -23,10 +25,10 @@ abstract class Mouse : Drawable string CursorPath = OnekoOnline.Config.GetValue("CursorSpritePath", "misc/cursor.png"); if (File.Exists(CursorPath) && new FileInfo(CursorPath).Length < 2500) { - Cursor = Bitmap.FromPNGMemory(File.ReadAllBytes(CursorPath)); + Cursor = Bitmap.FromPNGMemory(File.ReadAllBytes(CursorPath), 32, 32); } else { Console.WriteLine("The cursor PNG was either mising or too big. Using the default."); - Cursor = Bitmap.FromPNGMemory(EmbeddedResources.GetResource("misc.cursor.png")); + Cursor = Bitmap.FromPNGMemory(FallbackImg); } } diff --git a/MouseNet.cs b/MouseNet.cs index bb2987b..457b245 100644 --- a/MouseNet.cs +++ b/MouseNet.cs @@ -37,7 +37,7 @@ class MouseNet : Mouse public static void SpawnNetMouse(ClientUser user) { - Bitmap cursor = Bitmap.Deserialize(user.CursorSprite); + Bitmap cursor = Bitmap.Deserialize(user.CursorSprite, 32, 32, FallbackImg); MouseNet NewMouse = new(user, cursor); } }
\ No newline at end of file @@ -17,6 +17,8 @@ abstract class Oneko : Drawable public string Name = "Oneko"; + protected readonly static byte[] FallbackImg = EmbeddedResources.GetResource("nekos.oneko.png"); + public Oneko() : base() { Size = new(32,32); @@ -25,10 +27,10 @@ abstract class Oneko : Drawable string SpriteSheetPath = OnekoOnline.Config.GetValue("SpriteSheetPath", "nekos/oneko.png"); if (File.Exists(SpriteSheetPath) && new FileInfo(SpriteSheetPath).Length < 128*256*3) { - SpriteSheet = Bitmap.FromPNGMemory(File.ReadAllBytes(SpriteSheetPath)); + SpriteSheet = Bitmap.FromPNGMemory(File.ReadAllBytes(SpriteSheetPath), 256, 128); } else { Console.WriteLine("Path to spritesheet was invalid or the file was too big, using the default."); - SpriteSheet = Bitmap.FromPNGMemory(EmbeddedResources.GetResource("nekos.oneko.png")); + SpriteSheet = Bitmap.FromPNGMemory(FallbackImg, 256, 128); } } diff --git a/OnekoNet.cs b/OnekoNet.cs index 6bee562..81829c8 100644 --- a/OnekoNet.cs +++ b/OnekoNet.cs @@ -44,7 +44,7 @@ class OnekoNet : Oneko public static void SpawnNetNeko(ClientUser user) { if (!user.SpectatorMode && !NetNekos.ContainsKey(user.Id)) { - Bitmap spriteSheet = Bitmap.Deserialize(user.SpriteSheet); + Bitmap spriteSheet = Bitmap.Deserialize(user.SpriteSheet, 256, 128, FallbackImg); NetNekos.Add(user.Id, new OnekoNet(spriteSheet, user)); } } |
