Skip to content

Commit 261733c

Browse files
committed
feat(scripting/config): Adding FetchArraySize
1 parent 515685a commit 261733c

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

plugin_files/scripting/includes/swiftly/configuration.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ class Configuration
9191
else
9292
return 0;
9393
}
94+
95+
uint32_t FetchArraySize(const char *key)
96+
{
97+
void *configurationFetchArraySize = FetchFunctionPtr(nullptr, "scripting_Configuration_FetchArraySize");
98+
if (configurationFetchArraySize)
99+
return reinterpret_cast<Configuration_FetchArraySize>(configurationFetchArraySize)(key);
100+
else
101+
return 0;
102+
}
94103
};
95104

96105
extern Configuration *config;

plugin_files/scripting/includes/swiftly/swiftly_memory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ typedef void (*Commands_RegisterCommand)(const char *, const char *, void *);
5050
typedef void (*Commands_UnregisterCommand)(const char *);
5151

5252
typedef const char *(*Configuration_Fetch)(const char *);
53+
typedef uint32_t (*Configuration_FetchArraySize)(const char*);
5354

5455
typedef void (*Logger_CreateLogger)(const char *);
5556
typedef void (*Logger_WriteLog)(const char *, ELogType, const char *);

src/components/Plugins/src/scripting/Configuration.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,17 @@ SMM_API const char *scripting_Configuration_Fetch(const char *key)
5858

5959
std::any value = config.at(key);
6060
return SerializeData(value).c_str();
61+
}
62+
63+
SMM_API uint32 scripting_Configuration_FetchArraySize(const char *key)
64+
{
65+
if (key == nullptr)
66+
return 0;
67+
68+
std::map<std::string, unsigned int> arraySizes = g_Config->FetchConfigArraySizes();
69+
70+
if (arraySizes.find(key) == arraySizes.end())
71+
return 0;
72+
73+
return arraySizes.at(key);
6174
}

src/configuration/Configuration.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,33 @@ void LoadConfigPart(std::string key, rapidjson::Value &document);
9595

9696
void LoadValue(const char *key, const char *keyname, rapidjson::Value &value, std::string separator = ".")
9797
{
98+
std::string k = key + separator + keyname;
9899
if (value.IsBool())
99-
g_Config->SetValue(key + separator + keyname, value.GetBool());
100+
g_Config->SetValue(k, value.GetBool());
100101
else if (value.IsString())
101-
g_Config->SetValue(key + separator + keyname, std::string(value.GetString()));
102+
g_Config->SetValue(k, std::string(value.GetString()));
102103
else if (value.IsDouble())
103-
g_Config->SetValue(key + separator + keyname, value.GetDouble());
104+
g_Config->SetValue(k, value.GetDouble());
104105
else if (value.IsFloat())
105-
g_Config->SetValue(key + separator + keyname, value.GetFloat());
106+
g_Config->SetValue(k, value.GetFloat());
106107
else if (value.IsInt64())
107-
g_Config->SetValue(key + separator + keyname, value.GetInt64());
108+
g_Config->SetValue(k, value.GetInt64());
108109
else if (value.IsInt())
109-
g_Config->SetValue(key + separator + keyname, value.GetInt());
110+
g_Config->SetValue(k, value.GetInt());
110111
else if (value.IsUint64())
111-
g_Config->SetValue(key + separator + keyname, value.GetUint64());
112+
g_Config->SetValue(k, value.GetUint64());
112113
else if (value.IsUint())
113-
g_Config->SetValue(key + separator + keyname, value.GetUint());
114+
g_Config->SetValue(k, value.GetUint());
114115
else if (value.IsNull())
115-
g_Config->SetValue(key + separator + keyname, nullptr);
116+
g_Config->SetValue(k, nullptr);
116117
else if (value.IsObject())
117-
LoadConfigPart(key + separator + keyname, value);
118+
LoadConfigPart(k, value);
118119
else if (value.IsArray())
120+
{
121+
g_Config->SetArraySize(k, value.Size());
119122
for (size_t i = 0; i < value.Size(); i++)
120-
LoadValue((key + separator + keyname).c_str(), string_format("[%d]", i).c_str(), value[i], "");
123+
LoadValue(k.c_str(), string_format("[%d]", i).c_str(), value[i], "");
124+
}
121125
};
122126

123127
void LoadConfigPart(std::string key, rapidjson::Value &document)
@@ -129,6 +133,11 @@ void LoadConfigPart(std::string key, rapidjson::Value &document)
129133
}
130134
}
131135

136+
void Configuration::SetArraySize(std::string key, unsigned int size)
137+
{
138+
this->configArraySizes.insert(std::make_pair(key, size));
139+
}
140+
132141
void Configuration::LoadPluginConfigurations()
133142
{
134143
std::vector<std::string> configFiles = Files::FetchFileNames("addons/swiftly/configs/plugins");

src/configuration/Configuration.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Configuration
99
{
1010
private:
1111
std::map<std::string, std::any> config;
12+
std::map<std::string, unsigned int> configArraySizes;
1213
bool loaded = false;
1314

1415
public:
@@ -18,6 +19,9 @@ class Configuration
1819
void LoadPluginConfigurations();
1920

2021
std::map<std::string, std::any> FetchConfiguration() { return this->config; }
22+
std::map<std::string, unsigned int> FetchConfigArraySizes() { return this->configArraySizes; }
23+
24+
void SetArraySize(std::string key, unsigned int size);
2125

2226
template <typename T>
2327
T FetchValue(std::string key);

0 commit comments

Comments
 (0)