mirror of
https://github.com/LostArtefacts/TRX.git
synced 2025-05-03 07:08:01 +03:00
update JSON library
This commit is contained in:
parent
38bb80bdbb
commit
2a9bbfc078
5 changed files with 3522 additions and 1266 deletions
11
src/config.c
11
src/config.c
|
@ -10,7 +10,7 @@
|
||||||
T1MConfig.OPT = JSONGetBooleanValue(json, QUOTE(OPT)); \
|
T1MConfig.OPT = JSONGetBooleanValue(json, QUOTE(OPT)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
static int8_t ReadBarShowingMode(json_value* root, const char* name)
|
static int8_t ReadBarShowingMode(struct json_value_s* root, const char* name)
|
||||||
{
|
{
|
||||||
const char* value_str = JSONGetStringValue(root, name);
|
const char* value_str = JSONGetStringValue(root, name);
|
||||||
if (!value_str) {
|
if (!value_str) {
|
||||||
|
@ -23,8 +23,8 @@ static int8_t ReadBarShowingMode(json_value* root, const char* name)
|
||||||
return T1M_BSM_DEFAULT;
|
return T1M_BSM_DEFAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int8_t
|
static int8_t ReadBarLocationConfig(
|
||||||
ReadBarLocationConfig(json_value* root, const char* name, int8_t default_value)
|
struct json_value_s* root, const char* name, int8_t default_value)
|
||||||
{
|
{
|
||||||
const char* value_str = JSONGetStringValue(root, name);
|
const char* value_str = JSONGetStringValue(root, name);
|
||||||
if (!value_str) {
|
if (!value_str) {
|
||||||
|
@ -64,7 +64,8 @@ int T1MReadConfig()
|
||||||
fread(cfg_data, 1, cfg_size, fp);
|
fread(cfg_data, 1, cfg_size, fp);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
json_value* json = json_parse((const json_char*)cfg_data, cfg_size);
|
struct json_value_s* json = json_parse_ex(
|
||||||
|
cfg_data, cfg_size, json_parse_flags_allow_json5, NULL, NULL, NULL);
|
||||||
|
|
||||||
READ_BOOL(disable_healing_between_levels);
|
READ_BOOL(disable_healing_between_levels);
|
||||||
READ_BOOL(disable_medpacks);
|
READ_BOOL(disable_medpacks);
|
||||||
|
@ -93,7 +94,7 @@ int T1MReadConfig()
|
||||||
T1MConfig.enemy_healthbar_location = ReadBarLocationConfig(
|
T1MConfig.enemy_healthbar_location = ReadBarLocationConfig(
|
||||||
json, "enemy_healthbar_location", T1M_BL_VBOTTOM | T1M_BL_HLEFT);
|
json, "enemy_healthbar_location", T1M_BL_VBOTTOM | T1M_BL_HLEFT);
|
||||||
|
|
||||||
json_value_free(json);
|
free(json);
|
||||||
free(cfg_data);
|
free(cfg_data);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -1,39 +1,42 @@
|
||||||
#include "json_utils.h"
|
#include "json_utils.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
json_value* JSONGetField(
|
struct json_value_s* JSONGetField(struct json_value_s* root, const char* name)
|
||||||
json_value* root, json_type field_type, const char* name, int* pIndex)
|
|
||||||
{
|
{
|
||||||
if (root == NULL || root->type != json_object) {
|
if (root == NULL || root->type != json_type_object) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
json_value* result = NULL;
|
struct json_object_s* object = json_value_as_object(root);
|
||||||
unsigned int len = name ? strlen(name) : 0;
|
struct json_object_element_s* item = object->start;
|
||||||
unsigned int i = pIndex ? *pIndex : 0;
|
while (item) {
|
||||||
for (; i < root->u.object.length; ++i) {
|
if (!strcmp(item->name->string, name)) {
|
||||||
if (root->u.object.values[i].value->type == field_type) {
|
return item->value;
|
||||||
if (!name
|
|
||||||
|| (len == root->u.object.values[i].name_length
|
|
||||||
&& !strncmp(root->u.object.values[i].name, name, len))) {
|
|
||||||
result = root->u.object.values[i].value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
item = item->next;
|
||||||
}
|
}
|
||||||
if (pIndex) {
|
return NULL;
|
||||||
*pIndex = i;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int JSONGetBooleanValue(json_value* root, const char* name)
|
int8_t JSONGetBooleanValue(struct json_value_s* root, const char* name)
|
||||||
{
|
{
|
||||||
json_value* field = JSONGetField(root, json_boolean, name, NULL);
|
struct json_value_s* field = JSONGetField(root, name);
|
||||||
return field ? field->u.boolean : 0;
|
if (!field
|
||||||
|
|| (field->type != json_type_true && field->type != json_type_false)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return field->type == json_type_true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* JSONGetStringValue(json_value* root, const char* name)
|
const char* JSONGetStringValue(struct json_value_s* root, const char* name)
|
||||||
{
|
{
|
||||||
json_value* field = JSONGetField(root, json_string, name, NULL);
|
struct json_value_s* field = JSONGetField(root, name);
|
||||||
return field ? field->u.string.ptr : NULL;
|
if (!field || field->type != json_type_string) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
struct json_string_s* string = json_value_as_string(field);
|
||||||
|
if (!string) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
const char* ret = string->string;
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,8 @@
|
||||||
|
|
||||||
#include "json-parser/json.h"
|
#include "json-parser/json.h"
|
||||||
|
|
||||||
json_value* JSONGetField(
|
struct json_value_s* JSONGetField(struct json_value_s* root, const char* name);
|
||||||
json_value* root, json_type field_type, const char* name, int* pIndex);
|
int8_t JSONGetBooleanValue(struct json_value_s* root, const char* name);
|
||||||
|
const char* JSONGetStringValue(struct json_value_s* root, const char* name);
|
||||||
int JSONGetBooleanValue(json_value* root, const char* name);
|
|
||||||
const char* JSONGetStringValue(json_value* root, const char* name);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue