From 1937ea2684c9f3d0e55b10a6bcfa4d147b5dc93d Mon Sep 17 00:00:00 2001 From: haven1433 Date: Thu, 27 Nov 2025 23:05:51 -0600 Subject: [PATCH] bugfixes from discord --- src/HexManiac.Core/Models/Code/ScriptArg.cs | 6 +++--- src/HexManiac.Core/Models/Runs/ArrayRun.cs | 5 +++-- .../ViewModels/ImageEditorViewModel.cs | 13 +++++++------ .../ViewModels/Map/BlockMapViewModel.cs | 4 +++- src/HexManiac.Core/ViewModels/Map/Flags.cs | 6 +++--- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/HexManiac.Core/Models/Code/ScriptArg.cs b/src/HexManiac.Core/Models/Code/ScriptArg.cs index 264b2a8a..4a0569e1 100644 --- a/src/HexManiac.Core/Models/Code/ScriptArg.cs +++ b/src/HexManiac.Core/Models/Code/ScriptArg.cs @@ -1,10 +1,10 @@ using HavenSoft.HexManiac.Core.Models.Runs; using HavenSoft.HexManiac.Core.ViewModels; +using HavenSoft.HexManiac.Core.ViewModels.DataFormats; using System; using System.Collections.Generic; using System.Linq; using System.Text; -using HavenSoft.HexManiac.Core.ViewModels.DataFormats; namespace HavenSoft.HexManiac.Core.Models.Code { @@ -199,7 +199,7 @@ namespace HavenSoft.HexManiac.Core.Models.Code { if (Type == ArgType.Byte) { var error = Convert(model, token, out value); if (error != null) return error; - results.Add((byte)(value>>shift)); + results.Add((byte)(value >> shift)); } else if (Type == ArgType.Short) { var error = Convert(model, token, out value); if (error != null) return error; @@ -242,7 +242,7 @@ namespace HavenSoft.HexManiac.Core.Models.Code { labels.AddUnresolvedLabel(token, address); value = Pointer.NULL; } - value -= Pointer.NULL; + if (value < 0x02000000) value -= Pointer.NULL; results.Add((byte)value); results.Add((byte)(value >> 0x8)); results.Add((byte)(value >> 0x10)); diff --git a/src/HexManiac.Core/Models/Runs/ArrayRun.cs b/src/HexManiac.Core/Models/Runs/ArrayRun.cs index 297d62e4..c025ca23 100644 --- a/src/HexManiac.Core/Models/Runs/ArrayRun.cs +++ b/src/HexManiac.Core/Models/Runs/ArrayRun.cs @@ -1,5 +1,4 @@ using HavenSoft.HexManiac.Core.Models.Code; -using HavenSoft.HexManiac.Core.Models.Runs.Factory; using HavenSoft.HexManiac.Core.Models.Runs.Sprites; using HavenSoft.HexManiac.Core.ViewModels; using HavenSoft.HexManiac.Core.ViewModels.DataFormats; @@ -380,6 +379,7 @@ namespace HavenSoft.HexManiac.Core.Models.Runs { for (int j = 0; j < self.ElementCount; j++) { var segmentStart = self.Start + j * self.ElementLength + segmentOffset; if (recordSeg.CreateConcrete(model, segmentStart) is not ArrayRunEnumSegment enumSeg || enumSeg.EnumName != baseName) continue; + if (model.Count < segmentStart + recordSeg.Length) continue; if (model.ReadMultiByteValue(segmentStart, recordSeg.Length) != index) continue; yield return (segmentStart, segmentStart + recordSeg.Length - 1); } @@ -387,6 +387,7 @@ namespace HavenSoft.HexManiac.Core.Models.Runs { if (self.ElementContent[i] is ArrayRunEnumSegment segment && segment.EnumName == baseName) { for (int j = 0; j < self.ElementCount; j++) { var segmentStart = self.Start + j * self.ElementLength + segmentOffset; + if (model.Count < segmentStart + segment.Length) continue; if (model.ReadMultiByteValue(segmentStart, segment.Length) != index + segment.ValueOffset) continue; yield return (segmentStart, segmentStart + segment.Length - 1); } @@ -1439,7 +1440,7 @@ namespace HavenSoft.HexManiac.Core.Models.Runs { return list; } - private static (ElementContentType format, int formatLength, int segmentLength) ExtractSingleFormat(ReadOnlySpan segments,IDataModel model) { + private static (ElementContentType format, int formatLength, int segmentLength) ExtractSingleFormat(ReadOnlySpan segments, IDataModel model) { if (segments.Length >= 2 && MemoryExtensions.Equals(segments.Slice(0, 2), PCSRun.SharedFormatString, StringComparison.Ordinal)) { var format = ElementContentType.PCS; var formatLength = 2; diff --git a/src/HexManiac.Core/ViewModels/ImageEditorViewModel.cs b/src/HexManiac.Core/ViewModels/ImageEditorViewModel.cs index fd195ac1..c0d37355 100644 --- a/src/HexManiac.Core/ViewModels/ImageEditorViewModel.cs +++ b/src/HexManiac.Core/ViewModels/ImageEditorViewModel.cs @@ -315,7 +315,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels { HasMultiplePalettePages && model.GetNextRun(spriteAddress) is ITilemapRun tilemap && tilemap.Start == spriteAddress && - model.GetNextRun( tilemap.FindMatchingTileset(model)) is ITilesetRun tileset && + model.GetNextRun(tilemap.FindMatchingTileset(model)) is ITilesetRun tileset && tileset.TilesetFormat.BitsPerPixel == 4 && tilemap.BytesPerTile == 2; } @@ -540,7 +540,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels { var (x, y) = (point.X, point.Y); xOffset -= x; yOffset -= y; - var xPartial = xOffset / SpriteScale; + var xPartial = xOffset / SpriteScale; var yPartial = yOffset / SpriteScale; SpriteScale += 1; var xRange = (int)(PixelWidth * SpriteScale / 2); @@ -790,7 +790,8 @@ namespace HavenSoft.HexManiac.Core.ViewModels { private bool SpriteOnlyExpects16Colors() { var spriteAddress = model.ReadPointer(SpritePointer); - var spriteRun = (ISpriteRun)model.GetNextRun(spriteAddress); + var run = model.GetNextRun(spriteAddress); + if (run is not ISpriteRun spriteRun) return false; if (spriteRun is ITilesetRun tileset && tileset.TilesetFormat.BitsPerPixel == 4) return true; if (spriteRun is ITilemapRun) return false; if (spriteRun.SpriteFormat.BitsPerPixel < 8 && spriteRun.Pages == 1) return true; @@ -850,7 +851,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels { drawPoint = new Point(point.X - point.X % drawWidth, point.Y - point.Y % drawHeight); // allow editing the selected palette to match the tile if a tile is selected - var pageChange = (int)Math.Floor((float) parent.PaletteIndex(tile[0, 0]) / parent.Palette.Elements.Count); + var pageChange = (int)Math.Floor((float)parent.PaletteIndex(tile[0, 0]) / parent.Palette.Elements.Count); if (!spriteOnlyExpects16Colors && drawWidth == 8 && drawHeight == 8 && pageChange != 0) { parent.PalettePage += pageChange; pageChange = 0; @@ -1399,8 +1400,8 @@ namespace HavenSoft.HexManiac.Core.ViewModels { } else { underPixels = new int[selectionWidth, selectionHeight]; for (int x = 0; x < selectionWidth; x++) for (int y = 0; y < selectionHeight; y++) { - underPixels[x, y] = parent.pixels[selectionStart.X + x, selectionStart.Y + y]; - } + underPixels[x, y] = parent.pixels[selectionStart.X + x, selectionStart.Y + y]; + } parent.BlockPreview.Set(parent.PixelData, parent.PixelWidth, selectionStart, selectionWidth, selectionHeight); } diff --git a/src/HexManiac.Core/ViewModels/Map/BlockMapViewModel.cs b/src/HexManiac.Core/ViewModels/Map/BlockMapViewModel.cs index eadd515e..3344c7ea 100644 --- a/src/HexManiac.Core/ViewModels/Map/BlockMapViewModel.cs +++ b/src/HexManiac.Core/ViewModels/Map/BlockMapViewModel.cs @@ -902,7 +902,9 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Map { var canvas = new CanvasPixelViewModel(9 * 16, 9 * 16); for (int y = 0; y < 9; y++) { for (int x = 0; x < 9; x++) { - canvas.Draw(viewModel.BlockRenders[blockMap[x, y] & 0x3FF], x * 16, y * 16); + var blockIndex = blockMap[x, y] & 0x3FF; + var blockRender = blockIndex < viewModel.BlockRenders.Count ? viewModel.BlockRenders[0] : null; + canvas.Draw(blockRender, x * 16, y * 16); } } diff --git a/src/HexManiac.Core/ViewModels/Map/Flags.cs b/src/HexManiac.Core/ViewModels/Map/Flags.cs index c398a111..87f61bb6 100644 --- a/src/HexManiac.Core/ViewModels/Map/Flags.cs +++ b/src/HexManiac.Core/ViewModels/Map/Flags.cs @@ -82,7 +82,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Map { } foreach (var spot in GetAllScriptSpots(model, parser, GetAllTopLevelScripts(model), 0x29, 0x2A, 0x2B)) { - usedFlags.Add(model.ReadMultiByteValue(spot.Address + 1, 2)); + usedFlags.Add(model.ReadMultiByteValue(spot.Address + 1, 2)); } return usedFlags; @@ -241,7 +241,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Map { foreach (var spot in GetAllScriptSpots(model, parser, new[] { start }, 0x8A)) { var plantID = model[spot.Address + 1]; var berryID = model[spot.Address + 2]; - results[plantID] = new(spot.Address, berryID - 1); + results[plantID] = new(spot.Address, berryID - 1); } return results; } @@ -258,7 +258,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Map { // check rematch table var rematches = model.GetTableModel(HardcodeTablesModel.RematchTable); if (rematches != null) { - foreach(var rematch in rematches) { + foreach (var rematch in rematches) { if (!trainerFlags.Contains(rematch.GetValue("match1"))) continue; foreach (var match in new[] { "match2", "match3", "match4", "match5", "match6" }.Select(rematch.GetValue)) trainerFlags.Add(match); }