git repos / blockattack-game

commit 68c75938

Poul Sander · 2023-11-13 23:03
68c75938aca0d449bedf61b3fd3d27f329755909 patch · browse files
parent b7249250b5e545ce660b3e8a3b6e095449fa550d

Start supporting custom slots for themes

Changed files

M source/code/themes.cpp before
M source/code/themes.hpp before
diff --git a/source/code/themes.cpp b/source/code/themes.cpp index 37cbdd7..ed28123 100644 --- a/source/code/themes.cpp +++ b/source/code/themes.cpp
@@ -30,6 +30,7 @@ https://www.blockattack.net
#include <iostream>
#include "global.hpp"
#include <boost/algorithm/string/predicate.hpp>
+#include <fmt/core.h>
using json = nlohmann::json;
@@ -144,6 +145,10 @@ static void ReadThemeDataFromFile(const std::string& filename) {
std::cout << "Reading theme data from " << filename << "\n";
}
std::string s = sago::GetFileContent(filename);
+ if (s.empty()) {
+ std::cout << "File \"" << filename << "\" does not exit!\n";
+ return;
+ }
json j = json::parse(s);
ThemeFileData tfd = j;
for (auto& bg : tfd.background_data) {
@@ -173,7 +178,9 @@ static void InitBackGroundData() {
decoration_data[smileys.name] = smileys;
}
-
+static bool str_startswith(const std::string& s, const char* prefix) {
+ return (s.rfind(prefix, 0) == 0);
+}
void DumpThemeData() {
ThemeFileData tfd;
@@ -189,6 +196,32 @@ void DumpThemeData() {
sago::WriteFileContent("themes_dump.json", s);
}
+void ThemesSaveCustomSlots() {
+ ThemeFileData tfd;
+ for (const Theme& theme : themes) {
+ if ( str_startswith(theme.theme_name,"custom_slot_")) {
+ tfd.themes.push_back(theme);
+ }
+ }
+ json j = tfd;
+ std::string s = j.dump(4);
+ sago::WriteFileContent("custom_themes.json", s);
+}
+
+static void ThemesAddCustomSlot(std::vector<Theme>& themes, int slot_number) {
+ std::string slot_name = fmt::format("custom_slot_{}", slot_number);
+ for (const Theme& theme : themes) {
+ if (theme.theme_name == slot_name) {
+ //Custom slot already exists
+ return;
+ }
+ }
+ Theme new_theme;
+ new_theme.theme_name = slot_name;
+ themes.push_back(new_theme);
+ FillMissingFields(themes.back());
+}
+
void InitThemes() {
if (initialized) {
return;
@@ -203,6 +236,10 @@ void InitThemes() {
ReadThemeDataFromFile("themes/"+filename);
}
}
+ ReadThemeDataFromFile("custom_themes.json");
+ for (int i=0; i < 4; ++i) {
+ ThemesAddCustomSlot(themes, i);
+ }
DumpThemeData();
}
diff --git a/source/code/themes.hpp b/source/code/themes.hpp index ef0fb58..3da3f2d 100644 --- a/source/code/themes.hpp +++ b/source/code/themes.hpp
@@ -73,3 +73,9 @@ Theme getNextTheme();
* @return
*/
Theme getTheme(size_t theme_number);
+
+/**
+ * @brief Saves the themes starting with "custom_slot_" to "custom_themes.json"
+ *
+ */
+void ThemesSaveCustomSlots();