Allow extensions to define options (#590)

* Allow extensions to define their own options

* Fixes

* Remove logging
This commit is contained in:
Christian Semmler 2025-07-12 19:30:29 -07:00 committed by GitHub
parent 42bac60ec5
commit 232ef07b51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 38 additions and 10 deletions

View File

@ -1128,13 +1128,19 @@ bool IsleApp::LoadConfig()
strcpy(m_savePath, savePath);
#ifdef EXTENSIONS
std::vector<const char*> keys;
keys.resize(iniparser_getsecnkeys(dict, "extensions"));
iniparser_getseckeys(dict, "extensions", keys.data());
for (const char* key : keys) {
for (const char* key : Extensions::availableExtensions) {
if (iniparser_getboolean(dict, key, 0)) {
Extensions::Enable(key);
std::vector<const char*> extensionKeys;
const char* section = SDL_strchr(key, ':') + 1;
extensionKeys.resize(iniparser_getsecnkeys(dict, section));
iniparser_getseckeys(dict, section, extensionKeys.data());
std::map<std::string, std::string> extensionDict;
for (const char* key : extensionKeys) {
extensionDict[key] = iniparser_getstring(dict, key, NULL);
}
Extensions::Enable(key, std::move(extensionDict));
}
}
#endif

View File

@ -3,6 +3,7 @@
#include "lego1_export.h"
#include <functional>
#include <map>
#include <optional>
#include <string>
@ -10,7 +11,7 @@ namespace Extensions
{
constexpr const char* availableExtensions[] = {"extensions:texture loader"};
LEGO1_EXPORT void Enable(const char* p_key);
LEGO1_EXPORT void Enable(const char* p_key, std::map<std::string, std::string> p_options);
template <typename T>
struct Extension {

View File

@ -3,16 +3,24 @@
#include "extensions/extensions.h"
#include "legotextureinfo.h"
#include <array>
#include <map>
namespace Extensions
{
class TextureLoader {
public:
static void Initialize();
static bool PatchTexture(LegoTextureInfo* p_textureInfo);
static std::map<std::string, std::string> options;
static bool enabled;
private:
static constexpr const char* texturePath = "/textures/";
static constexpr std::array<std::pair<std::string_view, std::string_view>, 1> defaults = {
{{"texture loader:texture path", "/textures/"}}
};
private:
static SDL_Surface* FindTexture(const char* p_name);
};

View File

@ -4,12 +4,14 @@
#include <SDL3/SDL_log.h>
void Extensions::Enable(const char* p_key)
void Extensions::Enable(const char* p_key, std::map<std::string, std::string> p_options)
{
for (const char* key : availableExtensions) {
if (!SDL_strcasecmp(p_key, key)) {
if (!SDL_strcasecmp(p_key, "extensions:texture loader")) {
TextureLoader::options = std::move(p_options);
TextureLoader::enabled = true;
TextureLoader::Initialize();
}
SDL_Log("Enabled extension: %s", p_key);

View File

@ -2,8 +2,18 @@
using namespace Extensions;
std::map<std::string, std::string> TextureLoader::options;
bool TextureLoader::enabled = false;
void TextureLoader::Initialize()
{
for (const auto& option : defaults) {
if (!options.count(option.first.data())) {
options[option.first.data()] = option.second;
}
}
}
bool TextureLoader::PatchTexture(LegoTextureInfo* p_textureInfo)
{
SDL_Surface* surface = FindTexture(p_textureInfo->m_name);
@ -88,6 +98,7 @@ bool TextureLoader::PatchTexture(LegoTextureInfo* p_textureInfo)
SDL_Surface* TextureLoader::FindTexture(const char* p_name)
{
SDL_Surface* surface;
const char* texturePath = options["texture loader:texture path"].c_str();
MxString path = MxString(MxOmni::GetHD()) + texturePath + p_name + ".bmp";
path.MapPathToFilesystem();