Lua: new Image userdata (& cursor mod fix)

This commit is contained in:
Kae 2024-03-17 01:53:46 +11:00
parent 463205c09c
commit 53c7c3775f
12 changed files with 105 additions and 9 deletions

View File

@ -3,5 +3,9 @@
"name" : "opensb_base", "name" : "opensb_base",
"priority" : -9999, "priority" : -9999,
"friendlyName" : "OpenStarbound Assets", "friendlyName" : "OpenStarbound Assets",
"requires" : ["base"] "requires" : ["base"],
"scripts" : {
//"onLoad" : ["/scripts/opensb/assets/onload.lua"],
"postLoad" : ["/scripts/opensb/assets/postload.lua"]
}
} }

View File

@ -0,0 +1,11 @@
-- revert cursor frames if a mod replaced cursors.png with a SD version again
if assets.image("/cursors/cursors.png"):size()[1] == 64 then
local path = "/cursors/opensb/revert.cursor.patch"
assets.add(path, '{"scale":null}')
assets.patch("/cursors/inspect.cursor", path)
assets.patch("/cursors/link.cursor", path)
assets.patch("/cursors/pointer.cursor", path)
path = "/cursors/opensb/revert.frames.patch"
assets.add(path, '{"frameGrid":{"size":[16,16]}}')
assets.patch("/cursors/cursors.frames", path)
end

View File

@ -19,6 +19,7 @@
#include "StarSha256.hpp" #include "StarSha256.hpp"
#include "StarDataStreamDevices.hpp" #include "StarDataStreamDevices.hpp"
#include "StarLua.hpp" #include "StarLua.hpp"
#include "StarImageLuaBindings.hpp"
#include "StarUtilityLuaBindings.hpp" #include "StarUtilityLuaBindings.hpp"
namespace Star { namespace Star {
@ -120,6 +121,14 @@ Assets::Assets(Settings settings, StringList assetSources) {
return String(assetBytes->ptr(), assetBytes->size()); return String(assetBytes->ptr(), assetBytes->size());
}); });
callbacks.registerCallback("image", [this](String const& path) -> Image {
auto assetImage = image(path);
if (assetImage->bytesPerPixel() == 3)
return assetImage->convert(PixelFormat::RGBA32);
else
return *assetImage;
});
callbacks.registerCallback("scan", [this](Maybe<String> const& a, Maybe<String> const& b) -> StringList { callbacks.registerCallback("scan", [this](Maybe<String> const& a, Maybe<String> const& b) -> StringList {
return b ? scan(a.value(), *b) : scan(a.value()); return b ? scan(a.value(), *b) : scan(a.value());
}); });
@ -211,6 +220,9 @@ Assets::Assets(Settings settings, StringList assetSources) {
addSource(strf("{}::{}", sourcePath, groupName), memoryAssets); addSource(strf("{}::{}", sourcePath, groupName), memoryAssets);
} }
} }
// clear any caching that may have been trigered by load scripts as they may no longer be valid
m_framesSpecifications.clear();
m_assetsCache.clear();
}; };
List<pair<String, AssetSourcePtr>> sources; List<pair<String, AssetSourcePtr>> sources;
@ -884,7 +896,7 @@ Json Assets::readJson(String const& path) const {
} }
} else if (patchJson.isType(Json::Type::Object)) { //Kae: Do a good ol' json merge instead if the .patch file is a Json object } else if (patchJson.isType(Json::Type::Object)) { //Kae: Do a good ol' json merge instead if the .patch file is a Json object
auto patchData = patchJson.toObject(); auto patchData = patchJson.toObject();
result = jsonMerge(result, patchData); result = jsonMergeNulling(result, patchData);
} }
} }
return result; return result;

View File

@ -52,7 +52,7 @@ IODevicePtr MemoryAssetSource::open(String const& path) {
} }
ByteArrayPtr assetData; ByteArrayPtr assetData;
StreamOffset assetPos; StreamOffset assetPos = 0;
String name; String name;
}; };

View File

@ -127,6 +127,7 @@ SET (star_core_HEADERS
StarWorkerPool.hpp StarWorkerPool.hpp
StarXXHash.hpp StarXXHash.hpp
StarZSTDCompression.hpp StarZSTDCompression.hpp
scripting/StarImageLuaBindings.hpp
scripting/StarUtilityLuaBindings.hpp scripting/StarUtilityLuaBindings.hpp
) )
@ -184,6 +185,7 @@ SET (star_core_SOURCES
StarUuid.cpp StarUuid.cpp
StarWorkerPool.cpp StarWorkerPool.cpp
StarZSTDCompression.cpp StarZSTDCompression.cpp
scripting/StarImageLuaBindings.cpp
scripting/StarUtilityLuaBindings.cpp scripting/StarUtilityLuaBindings.cpp
) )

View File

@ -1026,13 +1026,25 @@ Json jsonMerge(Json const& base, Json const& merger) {
res.first->second = jsonMerge(res.first->second, p.second); res.first->second = jsonMerge(res.first->second, p.second);
} }
return merged; return merged;
} else if (merger.type() == Json::Type::Null) {
return base;
} else {
return merger;
} }
return merger.type() == Json::Type::Null ? base : merger;
}
Json jsonMergeNulling(Json const& base, Json const& merger) {
if (base.type() == Json::Type::Object && merger.type() == Json::Type::Object) {
JsonObject merged = base.toObject();
for (auto const& p : merger.toObject()) {
if (p.second.isNull())
merged.erase(p.first);
else {
auto res = merged.insert(p);
if (!res.second)
res.first->second = jsonMergeNulling(res.first->second, p.second);
}
}
return merged;
}
return merger;
} }
bool jsonPartialMatch(Json const& base, Json const& compare) { bool jsonPartialMatch(Json const& base, Json const& compare) {

View File

@ -291,6 +291,8 @@ DataStream& operator>>(DataStream& ds, JsonObject& m);
// is the combination of both objects, but for each repeated key jsonMerge is // is the combination of both objects, but for each repeated key jsonMerge is
// called recursively on both values to determine the result. // called recursively on both values to determine the result.
Json jsonMerge(Json const& base, Json const& merger); Json jsonMerge(Json const& base, Json const& merger);
// Same as above, but applies null mergers.
Json jsonMergeNulling(Json const& base, Json const& merger);
template <typename... T> template <typename... T>
Json jsonMerge(Json const& base, Json const& merger, T const&... rest); Json jsonMerge(Json const& base, Json const& merger, T const&... rest);

View File

@ -0,0 +1,28 @@
#include "StarImageLuaBindings.hpp"
#include "StarLuaConverters.hpp"
#include "StarImage.hpp"
namespace Star {
LuaMethods<Image> LuaUserDataMethods<Image>::make() {
LuaMethods<Image> methods;
methods.registerMethodWithSignature<Vec2U, Image&>("size", mem_fn(&Image::size));
methods.registerMethodWithSignature<void, Image&, Vec2U, Image&>("drawInto", mem_fn(&Image::drawInto));
methods.registerMethodWithSignature<void, Image&, Vec2U, Image&>("copyInto", mem_fn(&Image::copyInto));
methods.registerMethod("set", [](Image& image, unsigned x, unsigned y, Color const& color) {
image.set(x, y, color.toRgba());
});
methods.registerMethod("get", [](Image& image, unsigned x, unsigned y) {
return Color::rgba(image.get(x, y));
});
methods.registerMethod("subImage", [](Image& image, Vec2U const& min, Vec2U const& size) {
return image.subImage(min, size);
});
return methods;
}
}

View File

@ -0,0 +1,17 @@
#pragma once
#include "StarLua.hpp"
namespace Star {
STAR_CLASS(Image);
template <>
struct LuaConverter<Image> : LuaUserDataConverter<Image> {};
template <>
struct LuaUserDataMethods<Image> {
static LuaMethods<Image> make();
};
}

View File

@ -22,6 +22,7 @@
#include "StarBehaviorDatabase.hpp" #include "StarBehaviorDatabase.hpp"
#include "StarDamageDatabase.hpp" #include "StarDamageDatabase.hpp"
#include "StarDungeonGenerator.hpp" #include "StarDungeonGenerator.hpp"
#include "StarImageLuaBindings.hpp"
namespace Star { namespace Star {
@ -31,6 +32,7 @@ LuaCallbacks LuaBindings::makeRootCallbacks() {
auto root = Root::singletonPtr(); auto root = Root::singletonPtr();
callbacks.registerCallbackWithSignature<String, String>("assetData", bind(RootCallbacks::assetData, root, _1)); callbacks.registerCallbackWithSignature<String, String>("assetData", bind(RootCallbacks::assetData, root, _1));
callbacks.registerCallbackWithSignature<Image, String>("assetImage", bind(RootCallbacks::assetImage, root, _1));
callbacks.registerCallbackWithSignature<Json, String>("assetJson", bind(RootCallbacks::assetJson, root, _1)); callbacks.registerCallbackWithSignature<Json, String>("assetJson", bind(RootCallbacks::assetJson, root, _1));
callbacks.registerCallbackWithSignature<Json, String, Json>("makeCurrentVersionedJson", bind(RootCallbacks::makeCurrentVersionedJson, root, _1, _2)); callbacks.registerCallbackWithSignature<Json, String, Json>("makeCurrentVersionedJson", bind(RootCallbacks::makeCurrentVersionedJson, root, _1, _2));
callbacks.registerCallbackWithSignature<Json, Json, String>("loadVersionedJson", bind(RootCallbacks::loadVersionedJson, root, _1, _2)); callbacks.registerCallbackWithSignature<Json, Json, String>("loadVersionedJson", bind(RootCallbacks::loadVersionedJson, root, _1, _2));
@ -255,6 +257,10 @@ String LuaBindings::RootCallbacks::assetData(Root* root, String const& path) {
return String(bytes->ptr(), bytes->size()); return String(bytes->ptr(), bytes->size());
} }
Image LuaBindings::RootCallbacks::assetImage(Root* root, String const& path) {
return *root->assets()->image(path);
}
Json LuaBindings::RootCallbacks::assetJson(Root* root, String const& path) { Json LuaBindings::RootCallbacks::assetJson(Root* root, String const& path) {
return root->assets()->json(path); return root->assets()->json(path);
} }

View File

@ -7,12 +7,14 @@
namespace Star { namespace Star {
STAR_CLASS(Root); STAR_CLASS(Root);
STAR_CLASS(Image);
namespace LuaBindings { namespace LuaBindings {
LuaCallbacks makeRootCallbacks(); LuaCallbacks makeRootCallbacks();
namespace RootCallbacks { namespace RootCallbacks {
String assetData(Root* root, String const& path); String assetData(Root* root, String const& path);
Image assetImage(Root* root, String const& path);
Json assetJson(Root* root, String const& path); Json assetJson(Root* root, String const& path);
Json makeCurrentVersionedJson(Root* root, String const& identifier, Json const& content); Json makeCurrentVersionedJson(Root* root, String const& identifier, Json const& content);
Json loadVersionedJson(Root* root, Json const& versionedJson, String const& expectedIdentifier); Json loadVersionedJson(Root* root, Json const& versionedJson, String const& expectedIdentifier);