From ca48a137ec25f1002af4a1ab6e6e5047b5684ca6 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Sun, 22 Sep 2024 15:59:45 +1000 Subject: [PATCH] root.assetFrames & assets.frames --- doc/lua/openstarbound.md | 4 +++- source/base/StarAssets.cpp | 14 ++++++++++++++ source/base/StarAssets.hpp | 3 ++- source/game/scripting/StarRootLuaBindings.cpp | 7 +++++++ source/game/scripting/StarRootLuaBindings.hpp | 1 + 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/doc/lua/openstarbound.md b/doc/lua/openstarbound.md index ad8483d..23c0bbb 100644 --- a/doc/lua/openstarbound.md +++ b/doc/lua/openstarbound.md @@ -76,7 +76,9 @@ Returns the asset source path of an asset, or nil if the asset doesn't exist. If Without metadata: Returns an array with all the asset source paths. With metadata: Returns a table, key/value being source path/metadata. -#### `?` root.assetImage(`String` image) +#### `Image` root.assetImage(`String` image) + +#### `Json` root.assetFrames(`String` path) *TODO* diff --git a/source/base/StarAssets.cpp b/source/base/StarAssets.cpp index 4a10aab..ecd2068 100644 --- a/source/base/StarAssets.cpp +++ b/source/base/StarAssets.cpp @@ -102,6 +102,14 @@ Maybe FramesSpecification::getRect(String const& frame) const { } } +Json FramesSpecification::toJson() const { + return JsonObject{ + {"aliases", jsonFromMap(aliases)}, + {"frames", jsonFromMapV(frames, jsonFromRectU)}, + {"file", framesFile} + }; +} + Assets::Assets(Settings settings, StringList assetSources) { const char* AssetsPatchSuffix = ".patch"; const char* AssetsPatchListSuffix = ".patchlist"; @@ -139,6 +147,12 @@ Assets::Assets(Settings settings, StringList assetSources) { return *assetImage; }); + callbacks.registerCallback("frames", [this](String const& path) -> Json { + if (auto frames = imageFrames(path)) + return frames->toJson(); + return Json(); + }); + callbacks.registerCallback("scan", [this](Maybe const& a, Maybe const& b) -> StringList { return b ? scan(a.value(), *b) : scan(a.value()); }); diff --git a/source/base/StarAssets.hpp b/source/base/StarAssets.hpp index 6b454e8..e7b8611 100644 --- a/source/base/StarAssets.hpp +++ b/source/base/StarAssets.hpp @@ -27,7 +27,8 @@ struct FramesSpecification { // Get the target sub-rect of a given frame name (which can be an alias). // Returns nothing if the frame name is not found. Maybe getRect(String const& frame) const; - + // Converts to Json. + Json toJson() const; // The full path to the .frames file from which this was loaded. String framesFile; // Named sub-frames diff --git a/source/game/scripting/StarRootLuaBindings.cpp b/source/game/scripting/StarRootLuaBindings.cpp index 0ce7866..e1d7e3e 100644 --- a/source/game/scripting/StarRootLuaBindings.cpp +++ b/source/game/scripting/StarRootLuaBindings.cpp @@ -33,6 +33,7 @@ LuaCallbacks LuaBindings::makeRootCallbacks() { callbacks.registerCallbackWithSignature("assetData", bind(RootCallbacks::assetData, root, _1)); callbacks.registerCallbackWithSignature("assetImage", bind(RootCallbacks::assetImage, root, _1)); + callbacks.registerCallbackWithSignature("assetFrames", bind(RootCallbacks::assetFrames, root, _1)); callbacks.registerCallbackWithSignature("assetJson", bind(RootCallbacks::assetJson, root, _1)); callbacks.registerCallbackWithSignature("makeCurrentVersionedJson", bind(RootCallbacks::makeCurrentVersionedJson, root, _1, _2)); callbacks.registerCallbackWithSignature("loadVersionedJson", bind(RootCallbacks::loadVersionedJson, root, _1, _2)); @@ -262,6 +263,12 @@ Image LuaBindings::RootCallbacks::assetImage(Root* root, String const& path) { return *root->assets()->image(path); } +Json LuaBindings::RootCallbacks::assetFrames(Root* root, String const& path) { + if (auto frames = root->assets()->imageFrames(path)) + return frames->toJson(); + return Json(); +} + Json LuaBindings::RootCallbacks::assetJson(Root* root, String const& path) { return root->assets()->json(path); } diff --git a/source/game/scripting/StarRootLuaBindings.hpp b/source/game/scripting/StarRootLuaBindings.hpp index 0cee77f..b3eeddc 100644 --- a/source/game/scripting/StarRootLuaBindings.hpp +++ b/source/game/scripting/StarRootLuaBindings.hpp @@ -15,6 +15,7 @@ namespace LuaBindings { namespace RootCallbacks { String assetData(Root* root, String const& path); Image assetImage(Root* root, String const& path); + Json assetFrames(Root* root, String const& path); Json assetJson(Root* root, String const& path); Json makeCurrentVersionedJson(Root* root, String const& identifier, Json const& content); Json loadVersionedJson(Root* root, Json const& versionedJson, String const& expectedIdentifier);