Skip to content

Separate logic into Core and add a basic cli - #253

Open
ZehMatt wants to merge 2 commits into
OpenLoco:masterfrom
ZehMatt:cli-mode
Open

Separate logic into Core and add a basic cli#253
ZehMatt wants to merge 2 commits into
OpenLoco:masterfrom
ZehMatt:cli-mode

Conversation

@ZehMatt

@ZehMatt ZehMatt commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI review requested due to automatic review settings July 28, 2026 00:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors shared non-UI logic out of the Avalonia GUI into a new Core library and introduces a new Cli executable (locoobj) that reuses that core logic for headless/batch operations on Locomotion .dat objects.

Changes:

  • Added Core project for shared functionality (palette loading, object I/O, validation, image table import/export, batch operations).
  • Updated GUI and tests to reference Core and use core helpers instead of GUI-local implementations.
  • Added Cli project implementing several object/image operations via a minimal custom argument parser and batch processor.

Reviewed changes

Copilot reviewed 35 out of 36 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
Tests/Tests.csproj Adds Core reference for test compilation.
Tests/IdempotenceTests.cs Switches palette loading to Core.PaletteMapLoader.
ObjectEditor.sln Adds Core and Cli projects to the solution.
Gui/ViewModels/MainWindowViewModel.cs Uses PaletteMapLoader instead of Avalonia asset loading for default palette.
Gui/ViewModels/Loco/ObjectEditorViewModel.cs Moves validation/save logic to Core (ObjectValidation, ObjectFile).
Gui/ViewModels/Graphics/ImageViewModel.cs Delegates crop-region computation to Core.GraphicsElementOperations.
Gui/ViewModels/Graphics/ImageTableViewModel.cs Delegates sprites.json and image import/export work to Core.ImageTableIo.
Gui/Models/ObjectEditorContext.cs Delegates image-table group config bootstrapping/loading to Core.ImageTableGroupsConfig.
Gui/Gui.csproj Removes embedded resource previously used for image table grouping; adds Core reference.
Core/Validation/ObjectValidation.cs New shared object validation and OpenGraphics rules validation.
Core/PaletteMapLoader.cs New embedded-palette loader for GUI/tests/CLI reuse.
Core/Operations/ObjectOperations.cs New shared image-table operations (strip/crop/offset edits).
Core/Operations/BatchProcessor.cs New batch runner for applying operations across many .dat files.
Core/Objects/ObjectFile.cs New shared object file load/save and .dat enumeration helpers.
Core/Objects/LocoObjectFile.cs New shared “loaded file” record tying filename/header/object together.
Core/ImageTableGroupsConfig.cs New shared loader/installer for image-table grouping config.
Core/Graphics/ImageTableIo.cs New shared sprites.json + PNG import/export and offset-apply helpers.
Core/Graphics/GraphicsElementOperations.cs New shared GraphicsElement image/data operations (decode/crop/offset edits).
Core/Graphics/GraphicsElementJson.cs Moves sprites.json DTO into Core.Graphics.
Core/Core.csproj New Core project definition; embeds palette and default grouping config.
Core/Assets/palette.png Adds embedded default palette for non-GUI usage.
Cli/Program.cs New locoobj entrypoint and command dispatch/help output.
Cli/ICommand.cs New command interface for CLI subcommands.
Cli/ExitCodes.cs Defines standard process exit codes for CLI.
Cli/ConsoleLogger.cs Minimal ILogger implementation for console output.
Cli/Commands/ValidateCommand.cs Adds validate command (optionally OpenGraphics rules).
Cli/Commands/StripImagesCommand.cs Adds strip-images command using BatchProcessor + ObjectOperations.
Cli/Commands/ReencodeCommand.cs Adds reencode command (rewrite with new Sawyer encoding).
Cli/Commands/OffsetsCommand.cs Adds offsets command (zero/center/translate).
Cli/Commands/InfoCommand.cs Adds info command (text or JSON summary of headers/images/strings).
Cli/Commands/ImportImagesCommand.cs Adds import-images command (sprites.json + PNGs into a .dat).
Cli/Commands/ExportImagesCommand.cs Adds export-images command (PNG export + sprites.json).
Cli/Commands/CropCommand.cs Adds crop command (crop transparent borders and adjust offsets).
Cli/CommandLine.cs New lightweight --option/--flag command line parser.
Cli/CommandContext.cs Centralizes common option handling, input discovery, palette/encoding resolution, reporting.
Cli/Cli.csproj New CLI project definition referencing Core and other shared projects.

Comment on lines +58 to +73
if (File.Exists(pathName))
{
var jsonVersion = ImageTableGrouper.ReadImageTableGroupVersion(logger, pathName);
if (jsonVersion == null || jsonVersion < VersionHelpers.GetCurrentAppVersion())
{
currentImageTableGroups = defaultImageTableGroups;
}
else
{
await File.WriteAllTextAsync(pathName, defaultImageTableGroups);
}
}
else
{
await File.WriteAllTextAsync(pathName, defaultImageTableGroups);
}
Comment on lines +54 to +65
var header = file.DatInfo.S5Header;

SawyerStreamWriter.Save(
fileName,
objectName ?? header.Name,
objectSource ?? header.ObjectSource.Convert(header.Name, header.Checksum),
encoding ?? file.DatInfo.ObjectHeader.Encoding,
file.LocoObject,
logger,
allowSavingAsVanillaObject);

return true;
Comment on lines +17 to +21
if (!ReferenceEquals(element.Image, image))
{
element.Image?.Dispose();
element.Image = image;
}
Comment on lines +24 to +28
foreach (var element in group.GraphicsElements)
{
element.Image?.Dispose();
element.Image = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants