Lua: new Image userdata (& cursor mod fix)
This commit is contained in:
parent
463205c09c
commit
53c7c3775f
@ -3,5 +3,9 @@
|
||||
"name" : "opensb_base",
|
||||
"priority" : -9999,
|
||||
"friendlyName" : "OpenStarbound Assets",
|
||||
"requires" : ["base"]
|
||||
"requires" : ["base"],
|
||||
"scripts" : {
|
||||
//"onLoad" : ["/scripts/opensb/assets/onload.lua"],
|
||||
"postLoad" : ["/scripts/opensb/assets/postload.lua"]
|
||||
}
|
||||
}
|
0
assets/opensb/scripts/opensb/assets/onload.lua
Normal file
0
assets/opensb/scripts/opensb/assets/onload.lua
Normal file
11
assets/opensb/scripts/opensb/assets/postload.lua
Normal file
11
assets/opensb/scripts/opensb/assets/postload.lua
Normal 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
|
@ -19,6 +19,7 @@
|
||||
#include "StarSha256.hpp"
|
||||
#include "StarDataStreamDevices.hpp"
|
||||
#include "StarLua.hpp"
|
||||
#include "StarImageLuaBindings.hpp"
|
||||
#include "StarUtilityLuaBindings.hpp"
|
||||
|
||||
namespace Star {
|
||||
@ -120,6 +121,14 @@ Assets::Assets(Settings settings, StringList assetSources) {
|
||||
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 {
|
||||
return b ? scan(a.value(), *b) : scan(a.value());
|
||||
});
|
||||
@ -211,6 +220,9 @@ Assets::Assets(Settings settings, StringList assetSources) {
|
||||
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;
|
||||
@ -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
|
||||
auto patchData = patchJson.toObject();
|
||||
result = jsonMerge(result, patchData);
|
||||
result = jsonMergeNulling(result, patchData);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
@ -52,7 +52,7 @@ IODevicePtr MemoryAssetSource::open(String const& path) {
|
||||
}
|
||||
|
||||
ByteArrayPtr assetData;
|
||||
StreamOffset assetPos;
|
||||
StreamOffset assetPos = 0;
|
||||
String name;
|
||||
};
|
||||
|
||||
|
@ -127,6 +127,7 @@ SET (star_core_HEADERS
|
||||
StarWorkerPool.hpp
|
||||
StarXXHash.hpp
|
||||
StarZSTDCompression.hpp
|
||||
scripting/StarImageLuaBindings.hpp
|
||||
scripting/StarUtilityLuaBindings.hpp
|
||||
)
|
||||
|
||||
@ -184,6 +185,7 @@ SET (star_core_SOURCES
|
||||
StarUuid.cpp
|
||||
StarWorkerPool.cpp
|
||||
StarZSTDCompression.cpp
|
||||
scripting/StarImageLuaBindings.cpp
|
||||
scripting/StarUtilityLuaBindings.cpp
|
||||
)
|
||||
|
||||
|
@ -1026,13 +1026,25 @@ Json jsonMerge(Json const& base, Json const& merger) {
|
||||
res.first->second = jsonMerge(res.first->second, p.second);
|
||||
}
|
||||
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) {
|
||||
|
@ -291,6 +291,8 @@ DataStream& operator>>(DataStream& ds, JsonObject& m);
|
||||
// is the combination of both objects, but for each repeated key jsonMerge is
|
||||
// called recursively on both values to determine the result.
|
||||
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>
|
||||
Json jsonMerge(Json const& base, Json const& merger, T const&... rest);
|
||||
|
28
source/core/scripting/StarImageLuaBindings.cpp
Normal file
28
source/core/scripting/StarImageLuaBindings.cpp
Normal 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;
|
||||
}
|
||||
|
||||
}
|
17
source/core/scripting/StarImageLuaBindings.hpp
Normal file
17
source/core/scripting/StarImageLuaBindings.hpp
Normal 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();
|
||||
};
|
||||
|
||||
}
|
@ -22,6 +22,7 @@
|
||||
#include "StarBehaviorDatabase.hpp"
|
||||
#include "StarDamageDatabase.hpp"
|
||||
#include "StarDungeonGenerator.hpp"
|
||||
#include "StarImageLuaBindings.hpp"
|
||||
|
||||
namespace Star {
|
||||
|
||||
@ -31,6 +32,7 @@ LuaCallbacks LuaBindings::makeRootCallbacks() {
|
||||
auto root = Root::singletonPtr();
|
||||
|
||||
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, Json>("makeCurrentVersionedJson", bind(RootCallbacks::makeCurrentVersionedJson, 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());
|
||||
}
|
||||
|
||||
Image LuaBindings::RootCallbacks::assetImage(Root* root, String const& path) {
|
||||
return *root->assets()->image(path);
|
||||
}
|
||||
|
||||
Json LuaBindings::RootCallbacks::assetJson(Root* root, String const& path) {
|
||||
return root->assets()->json(path);
|
||||
}
|
||||
|
@ -7,12 +7,14 @@
|
||||
namespace Star {
|
||||
|
||||
STAR_CLASS(Root);
|
||||
STAR_CLASS(Image);
|
||||
|
||||
namespace LuaBindings {
|
||||
LuaCallbacks makeRootCallbacks();
|
||||
|
||||
namespace RootCallbacks {
|
||||
String assetData(Root* root, String const& path);
|
||||
Image assetImage(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);
|
||||
|
Loading…
Reference in New Issue
Block a user