WordleDS/source/json.cpp
Pk11 37f3516804
Online word synchronization (#21)
* WIP get words from internet

Using a (to be created) mirror since I don't want to bloat this up with HTTPS and the official API is HTTPS only
Untested so far

* wifi.hpp

* Remove unused includes

* Derp fix

* Derp fix part 2

* Fix errors

* Try fix build error

* Allow loading JSON directly

* Update share URL

* Restore word list order, add known guess order

* Use the word order

* Wi-Fi fixes

* Use IDs from internet and save to mod.json

* Simplify image drawing

* WIP Improve graphics – move popup to layer, main menu icons to sprites

Allows the popup to be used in other places like settings, will allow easily only showing the update sprite if using an online mod

* Further simplify image drawing, bug fix

* Load main menu button graphics/sprites

positions TODO

* Fix palettes

* Add update button, fix some bugs

* Fix updating words

* Only allow infinite when word order outdated

* Minor cleanup

* Remove unused include

* Add Python script to update mod.json

* Fix backwards compatibility with old mods

Note that the old bgBottomBox is simply unused, the new popupBox will simply be used instead
2023-03-02 19:32:58 -06:00

77 lines
1.8 KiB
C++

#include "json.hpp"
#include <nds.h>
#include <stdio.h>
// sassert but it fixes the brightness
#undef sassert
#define sassert(e,...) ((e) ? (void)0 : (setBrightness(2, 0), __sassert(__FILE__, __LINE__, #e, __VA_ARGS__)))
Json::Json(void) {
_json = std::shared_ptr<cJSON>(cJSON_CreateObject(), [](cJSON *p) { cJSON_Delete(p); });
}
Json::Json(const char *str, bool fromFile) {
if(fromFile) {
FILE *file = fopen(str, "r");
if(!file)
return;
fseek(file, 0, SEEK_END);
size_t fsize = ftell(file);
if(fsize == 0) {
fclose(file);
return;
}
fseek(file, 0, SEEK_SET);
char *buffer = new char[fsize];
if(fread(buffer, 1, fsize, file) != fsize) {
fclose(file);
delete[] buffer;
return;
}
fclose(file);
_json = std::shared_ptr<cJSON>(cJSON_ParseWithLength(buffer, fsize), [](cJSON *p) { cJSON_Delete(p); });
delete[] buffer;
} else {
_json = std::shared_ptr<cJSON>(cJSON_Parse(str), [](cJSON *p) { cJSON_Delete(p); });
}
}
std::string Json::dump() const {
char *charStr = cJSON_PrintUnformatted(_json.get());
std::string str(charStr);
free(charStr);
return str;
}
Json Json::set(cJSON *item, const char *name) {
if(isArray()) {
sassert(name == nullptr, "name must be nullptr for arrays");
cJSON_AddItemToArray(_json.get(), item);
} else if(isObject()) {
sassert(name != nullptr, "name must NOT be nullptr for\nobjects");
if(contains(name))
cJSON_ReplaceItemInObjectCaseSensitive(_json.get(), name, item);
else
cJSON_AddItemToObject(_json.get(), name, item);
} else {
sassert(false, "set can only be used on arrays\nand objects");
}
return item;
}
Json Json::set(const std::vector<std::string> &val, const char *name) {
Json array = create(false, name);
for(const std::string &str : val) {
array.set(str.c_str());
}
return array;
}