TRX/src/config.c

215 lines
7.2 KiB
C
Raw Normal View History

#include "config.h"
2021-03-14 13:50:30 +01:00
2021-03-18 16:14:28 +01:00
#include "filesystem.h"
2021-03-21 21:17:46 +01:00
#include "global/const.h"
#include "global/vars.h"
2021-12-04 22:06:41 +01:00
#include "specific/s_shell.h"
2021-03-18 21:03:32 +01:00
#include "json.h"
2021-10-31 18:29:50 +01:00
#include "log.h"
2021-11-16 11:49:49 +01:00
#include "memory.h"
2021-03-14 13:50:30 +01:00
#include <string.h>
2021-02-22 21:36:00 +01:00
#define Q(x) #x
#define QUOTE(x) Q(x)
2021-03-07 15:59:00 +01:00
#define READ_PRIMITIVE(func, opt, default_value) \
do { \
g_Config.opt = \
func(root_obj, Config_ProcessKey(QUOTE(opt)), default_value); \
2021-03-07 15:59:00 +01:00
} while (0)
#define READ_BOOL(opt, default_value) \
2021-03-18 20:53:12 +01:00
READ_PRIMITIVE(json_object_get_bool, opt, default_value)
2021-03-07 15:59:00 +01:00
#define READ_INTEGER(opt, default_value) \
2022-02-05 01:34:21 +01:00
READ_PRIMITIVE(json_object_get_int, opt, default_value)
2021-10-23 21:31:03 +02:00
#define READ_FLOAT(opt, default_value) \
2022-02-05 01:34:21 +01:00
READ_PRIMITIVE(json_object_get_double, opt, default_value)
2021-03-07 15:59:00 +01:00
#define READ_ENUM(opt, default_value, enum_map) \
2021-02-22 21:36:00 +01:00
do { \
g_Config.opt = Config_ReadEnum( \
root_obj, Config_ProcessKey(QUOTE(opt)), default_value, enum_map); \
2021-02-22 21:36:00 +01:00
} while (0)
2021-11-24 17:37:21 +01:00
CONFIG g_Config = { 0 };
2021-10-14 12:56:43 +02:00
2021-11-24 16:48:16 +01:00
static const char *m_T1MGlobalSettingsPath = "cfg/Tomb1Main.json5";
2021-11-04 21:41:33 +01:00
typedef struct ENUM_MAP {
const char *text;
int value;
} ENUM_MAP;
const ENUM_MAP m_BarShowingModes[] = {
{ "default", BSM_DEFAULT },
{ "flashing-or-default", BSM_FLASHING_OR_DEFAULT },
{ "flashing-only", BSM_FLASHING_ONLY },
{ "always", BSM_ALWAYS },
{ "never", BSM_NEVER },
{ "ps1", BSM_PS1 },
{ NULL, -1 },
};
const ENUM_MAP m_BarLocations[] = {
{ "top-left", BL_TOP_LEFT },
{ "top-center", BL_TOP_CENTER },
{ "top-right", BL_TOP_RIGHT },
{ "bottom-left", BL_BOTTOM_LEFT },
{ "bottom-center", BL_BOTTOM_CENTER },
{ "bottom-right", BL_BOTTOM_RIGHT },
{ NULL, -1 },
};
const ENUM_MAP m_BarColors[] = {
{ "gold", BC_GOLD }, { "blue", BC_BLUE }, { "grey", BC_GREY },
{ "red", BC_RED }, { "silver", BC_SILVER }, { "green", BC_GREEN },
{ "gold2", BC_GOLD2 }, { "blue2", BC_BLUE2 }, { "pink", BC_PINK },
{ NULL, -1 },
};
const ENUM_MAP m_ScreenshotFormats[] = {
{ "jpg", SCREENSHOT_FORMAT_JPEG },
{ "jpeg", SCREENSHOT_FORMAT_JPEG },
{ "png", SCREENSHOT_FORMAT_PNG },
{ NULL, -1 },
};
static const char *Config_ProcessKey(const char *key);
static int Config_ReadEnum(
struct json_object_s *obj, const char *name, int8_t default_value,
const ENUM_MAP *enum_map);
static const char *Config_ProcessKey(const char *key)
{
return strchr(key, '.') ? strrchr(key, '.') + 1 : key;
}
static int Config_ReadEnum(
struct json_object_s *obj, const char *name, int8_t default_value,
const ENUM_MAP *enum_map)
{
2021-03-18 20:53:12 +01:00
const char *value_str = json_object_get_string(obj, name, NULL);
if (value_str) {
while (enum_map->text) {
if (!strcmp(value_str, enum_map->text)) {
return enum_map->value;
}
enum_map++;
2021-03-07 15:59:00 +01:00
}
}
return default_value;
}
2021-11-30 12:25:56 +01:00
bool Config_ReadFromJSON(const char *cfg_data)
2021-03-07 15:59:00 +01:00
{
2021-11-24 18:20:05 +01:00
bool result = false;
2021-03-18 20:53:12 +01:00
struct json_value_s *root;
2021-03-11 14:54:59 +01:00
struct json_parse_result_s parse_result;
2021-03-18 20:53:12 +01:00
root = json_parse_ex(
2021-03-07 15:59:00 +01:00
cfg_data, strlen(cfg_data), json_parse_flags_allow_json5, NULL, NULL,
2021-03-11 14:54:59 +01:00
&parse_result);
2021-11-24 18:20:05 +01:00
if (root) {
result = true;
} else {
2021-03-25 18:18:18 +01:00
LOG_ERROR(
2021-03-11 14:54:59 +01:00
"failed to parse config file: %s in line %d, char %d",
2021-03-18 20:53:12 +01:00
json_get_error_description(parse_result.error),
2021-03-11 14:54:59 +01:00
parse_result.error_line_no, parse_result.error_row_no);
2021-03-19 01:09:02 +01:00
// continue to supply the default values
2021-03-11 14:54:59 +01:00
}
2021-03-07 15:59:00 +01:00
2021-03-18 20:53:12 +01:00
struct json_object_s *root_obj = json_value_as_object(root);
2021-11-02 21:03:42 +01:00
READ_BOOL(disable_healing_between_levels, false);
READ_BOOL(disable_medpacks, false);
READ_BOOL(disable_magnums, false);
READ_BOOL(disable_uzis, false);
READ_BOOL(disable_shotgun, false);
READ_BOOL(enable_detailed_stats, true);
2022-02-09 22:58:57 +01:00
READ_BOOL(enable_deaths_counter, true);
2021-11-02 21:03:42 +01:00
READ_BOOL(enable_enemy_healthbar, true);
READ_BOOL(enable_enhanced_look, true);
READ_BOOL(enable_shotgun_flash, true);
READ_BOOL(fix_shotgun_targeting, true);
2021-11-02 21:03:42 +01:00
READ_BOOL(enable_cheats, false);
READ_BOOL(enable_numeric_keys, true);
READ_BOOL(enable_tr3_sidesteps, true);
READ_BOOL(enable_braid, false);
READ_BOOL(enable_compass_stats, true);
READ_BOOL(enable_timer_in_inventory, true);
READ_BOOL(enable_smooth_bars, true);
READ_BOOL(enable_fade_effects, true);
2021-11-02 21:03:42 +01:00
READ_BOOL(fix_tihocan_secret_sound, true);
READ_BOOL(fix_pyramid_secret_trigger, true);
READ_BOOL(fix_secrets_killing_music, true);
READ_BOOL(fix_descending_glitch, false);
READ_BOOL(fix_wall_jump_glitch, false);
READ_BOOL(fix_bridge_collision, true);
READ_BOOL(fix_qwop_glitch, false);
READ_BOOL(fix_alligator_ai, true);
2021-03-07 15:59:00 +01:00
READ_INTEGER(fov_value, 65);
READ_INTEGER(resolution_width, -1);
READ_INTEGER(resolution_height, -1);
2021-11-02 21:03:42 +01:00
READ_BOOL(fov_vertical, true);
READ_BOOL(disable_demo, false);
READ_BOOL(disable_fmv, false);
READ_BOOL(disable_cine, false);
READ_BOOL(disable_music_in_menu, false);
READ_BOOL(enable_xbox_one_controller, false);
2021-10-23 21:31:03 +02:00
READ_FLOAT(brightness, 1.0);
2021-11-02 21:03:42 +01:00
READ_BOOL(enable_round_shadow, true);
READ_BOOL(enable_3d_pickups, true);
READ_FLOAT(rendering.anisotropy_filter, 16.0f);
READ_BOOL(walk_to_items, false);
READ_INTEGER(start_lara_hitpoints, LARA_HITPOINTS);
READ_ENUM(
healthbar_showing_mode, BSM_FLASHING_OR_DEFAULT, m_BarShowingModes);
READ_ENUM(airbar_showing_mode, BSM_DEFAULT, m_BarShowingModes);
READ_ENUM(healthbar_location, BL_TOP_LEFT, m_BarLocations);
READ_ENUM(airbar_location, BL_TOP_RIGHT, m_BarLocations);
READ_ENUM(enemy_healthbar_location, BL_BOTTOM_LEFT, m_BarLocations);
READ_ENUM(healthbar_color, BC_RED, m_BarColors);
READ_ENUM(airbar_color, BC_BLUE, m_BarColors);
READ_ENUM(enemy_healthbar_color, BC_GREY, m_BarColors);
READ_ENUM(screenshot_format, SCREENSHOT_FORMAT_JPEG, m_ScreenshotFormats);
2021-03-07 15:59:00 +01:00
CLAMP(g_Config.start_lara_hitpoints, 1, LARA_HITPOINTS);
2021-11-24 17:37:21 +01:00
CLAMP(g_Config.fov_value, 30, 255);
2021-03-07 15:59:00 +01:00
2021-03-18 20:53:12 +01:00
if (root) {
json_value_free(root);
2021-03-11 14:54:59 +01:00
}
return result;
2021-03-07 15:59:00 +01:00
}
2021-11-24 18:20:05 +01:00
bool Config_Read()
{
2021-11-24 18:20:05 +01:00
bool result = false;
2021-03-11 14:54:59 +01:00
char *cfg_data = NULL;
2021-11-24 18:20:05 +01:00
if (!File_Load(m_T1MGlobalSettingsPath, &cfg_data, NULL)) {
2021-11-24 16:48:16 +01:00
LOG_ERROR("Failed to open file '%s'", m_T1MGlobalSettingsPath);
2021-11-30 12:25:56 +01:00
result = Config_ReadFromJSON("");
2021-03-11 14:54:59 +01:00
goto cleanup;
}
2021-11-30 12:25:56 +01:00
result = Config_ReadFromJSON(cfg_data);
2021-11-24 17:37:21 +01:00
if (g_Config.resolution_width > 0) {
g_AvailableResolutions[RESOLUTIONS_SIZE - 1].width =
g_Config.resolution_width;
g_AvailableResolutions[RESOLUTIONS_SIZE - 1].height =
g_Config.resolution_height;
} else {
2021-11-24 17:37:21 +01:00
g_AvailableResolutions[RESOLUTIONS_SIZE - 1].width =
2021-12-04 22:06:41 +01:00
S_Shell_GetCurrentDisplayWidth();
2021-11-24 17:37:21 +01:00
g_AvailableResolutions[RESOLUTIONS_SIZE - 1].height =
2021-12-04 22:06:41 +01:00
S_Shell_GetCurrentDisplayHeight();
}
2021-03-11 14:54:59 +01:00
cleanup:
2022-01-07 03:14:07 +01:00
Memory_FreePointer(&cfg_data);
2021-03-11 14:54:59 +01:00
return result;
}