feat: Cleans up UOP file handling. Adds automatic bounds.bin generation (#1744)

### Summary
* Unifies reading UOP File indexes
* Adds ArtData file reader to get graphic bounds
* Automatically generates Bounds.bin from art file if missing
* Adds [GenBounds command to regenerate the bounds.bin file. Useful for when the art file changes.
This commit is contained in:
kaczy93 2024-05-02 05:02:06 +02:00 • committed by GitHub
parent db0621b884
commit dc118d836f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 680 additions and 382 deletions

View file

@ -15,6 +15,7 @@
using System;
using System.IO;
using System.Threading;
using Server.Logging;
namespace Server;
@ -22,24 +23,133 @@ namespace Server;
public static class ItemBounds
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(ItemBounds));
private static readonly string _pathToBounds = Path.Combine(Core.BaseDirectory, "Data", "Binary", "Bounds.bin");
private static bool _isGenerating;
public static void Configure()
{
CommandSystem.Register("GenBounds", AccessLevel.Developer, GenBounds_OnCommand);
}
[Usage("GenBounds")]
[Description("Asynchronously generates the bounds.bin file from art.mul/artidx.mul or artLegacyMUL.uop to determine container boundaries.")]
private static void GenBounds_OnCommand(CommandEventArgs e)
{
GenerateBoundsFileAsync(e.Mobile);
}
static ItemBounds()
{
Table = new Rectangle2D[TileData.ItemTable.Length];
if (!File.Exists("Data/Binary/Bounds.bin"))
if (!File.Exists(_pathToBounds))
{
logger.Error("Data/Binary/Bounds.bin does not exist");
logger.Information("Generating {BoundsFilePath}...", "Bounds.bin");
try
{
GenerateBoundsFile();
logger.Information("Generated {BoundsFilePath} successfully.", "Bounds.bin");
}
catch (Exception ex)
{
logger.Error(ex, "Failed to generate {BoundsFilePath}", "Bounds.bin");
}
return;
}
using var fs = new FileStream(
"Data/Binary/Bounds.bin",
FileMode.Open,
FileAccess.Read,
FileShare.Read
GenerateTable();
}
public static Rectangle2D[] Table { get; private set; }
private static void GenerateBoundsFileAsync(Mobile m)
{
if (_isGenerating)
{
m?.SendMessage("Bounds file is already being generated.");
return;
}
_isGenerating = true;
ThreadPool.QueueUserWorkItem(
state =>
{
var from = state as Mobile;
logger.Information("Generating {BoundsFilePath}...", "Bounds.bin");
if (from != null)
{
Core.LoopContext.Post(() => from?.SendMessage("Generating bounds file..."));
}
try
{
var table = GenerateBoundsFile();
Core.LoopContext.Post(() => Table = table);
}
catch (Exception ex)
{
if (from != null)
{
Core.LoopContext.Post(() =>
{
from?.SendMessage("Failed to generate bounds file:");
from?.SendMessage(ex.Message);
}
);
}
logger.Error(ex, "Failed to generate {BoundsFilePath}", "Bounds.bin");
return;
}
if (from != null)
{
Core.LoopContext.Post(
() => from?.SendMessage(
$"Bounds file saved to {Path.GetRelativePath(Core.BaseDirectory, _pathToBounds)}."
)
);
}
logger.Information("Generated {BoundsFilePath} successfully.", "Bounds.bin");
_isGenerating = false;
},
m
);
var bin = new BinaryReader(fs);
}
private static Rectangle2D[] GenerateBoundsFile()
{
var table = new Rectangle2D[TileData.ItemTable.Length];
using var artData = new ArtData();
if (!artData.IsInitialized)
{
throw new FileNotFoundException("Unable to load art.mul/artidx.mul or artLegacyMUL.uop");
}
using var fs = new FileStream(_pathToBounds, FileMode.Create, FileAccess.Write);
using var bw = new BinaryWriter(fs);
for (var i = 0; i < table.Length; i++)
{
var bounds = artData.GetStaticBounds(i);
bw.Write((short)bounds.X);
bw.Write((short)bounds.Y);
bw.Write((short)(bounds.X + bounds.Width + 1));
bw.Write((short)(bounds.Y + bounds.Height + 1));
table[i] = bounds;
}
return table;
}
private static void GenerateTable()
{
using var fs = new FileStream(_pathToBounds, FileMode.Open, FileAccess.Read, FileShare.Read);
using var bin = new BinaryReader(fs);
var count = Math.Min(Table.Length, (int)(fs.Length / 8));
@ -50,11 +160,7 @@ public static class ItemBounds
int xMax = bin.ReadInt16();
int yMax = bin.ReadInt16();
Table[i].Set(xMin, yMin, xMax - xMin + 1, yMax - yMin + 1);
Table[i].Set(xMin, yMin, xMax - xMin, yMax - yMin);
}
bin.Close();
}
public static Rectangle2D[] Table { get; }
}