1
2 #include "settings.h"
3
4 #include "ResourceManager.h"
5 #include "common/misc.h"
6 #include "Utils/Logger.h"
7 #include "input.h"
8
9 #include <SDL.h>
10 #include <cstdint>
11 #include <cstdio>
12 #include <cstdlib>
13 #include <cstring>
14 #include <string>
15 #include <vector>
16
17 const uint32_t SETTINGS_VERSION = (('7' << 24) + ('S' << 16) + ('X' << 8) + 'N'); // serves as both a version and magic
18
19 Settings normal_settings;
20 Settings *settings = &normal_settings;
21
tryload(Settings * setfile)22 static bool tryload(Settings *setfile)
23 {
24 FILE *fp;
25
26 std::string path = ResourceManager::getInstance()->getPrefPath("settings.dat");
27
28 LOG_INFO("Loading settings...");
29
30 fp = myfopen(widen(path).c_str(), widen("rb").c_str());
31 if (!fp)
32 {
33 LOG_ERROR("Couldn't open file {}.", path);
34 return 1;
35 }
36
37 setfile->version = 0;
38 fread(setfile, sizeof(Settings), 1, fp);
39 if (setfile->version != SETTINGS_VERSION)
40 {
41 LOG_ERROR("Wrong settings version {:#04x}.", setfile->version);
42 fclose(fp);
43 return 1;
44 }
45
46 fclose(fp);
47 std::vector<std::string> langs = ResourceManager::getInstance()->languages();
48 bool found = false;
49 for (auto &l : langs)
50 {
51 if (strcmp(settings->language, l.c_str()) == 0)
52 {
53 found = true;
54 break;
55 }
56 }
57 if (!found)
58 {
59 memset(setfile->language, 0, 256);
60 strncpy(setfile->language, "english", 255);
61 }
62
63 #if defined(__VITA__) || defined(__SWITCH__)
64 setfile->resolution = 1;
65 #endif
66
67 return 0;
68 }
69
settings_load(Settings * setfile)70 bool settings_load(Settings *setfile)
71 {
72 if (!setfile)
73 setfile = &normal_settings;
74
75 if (tryload(settings))
76 {
77 LOG_INFO("No saved settings; using defaults.");
78
79 memset(setfile, 0, sizeof(Settings));
80 #if defined(__VITA__) || defined(__SWITCH__)
81 setfile->resolution = 1;
82 #else
83 setfile->resolution = 2; // 640x480 Windowed, should be safe value
84 #endif
85 setfile->last_save_slot = 0;
86 setfile->fullscreen = false;
87
88 setfile->sound_enabled = true;
89 setfile->music_enabled = 1; // both Boss and Regular music
90 setfile->new_music = 0;
91 setfile->rumble = false;
92 setfile->sfx_volume = 100;
93 setfile->music_volume = 100;
94 setfile->music_interpolation = 0;
95 setfile->animated_facepics = true;
96 setfile->lights = false;
97 setfile->control_scheme = false;
98 memset(setfile->language, 0, 256);
99 strncpy(setfile->language, "english", 255);
100
101 return 1;
102 }
103 else
104 {
105 input_set_mappings(settings->input_mappings);
106 }
107
108 if (settings->control_scheme)
109 {
110 ACCEPT_BUTTON = FIREKEY;
111 DECLINE_BUTTON = JUMPKEY;
112 }
113 else
114 {
115 ACCEPT_BUTTON = JUMPKEY;
116 DECLINE_BUTTON = FIREKEY;
117 }
118
119 return 0;
120 }
121
122 /*
123 void c------------------------------() {}
124 */
125
settings_save(Settings * setfile)126 bool settings_save(Settings *setfile)
127 {
128 FILE *fp;
129
130 std::string path = ResourceManager::getInstance()->getPrefPath("settings.dat");
131
132 if (!setfile)
133 setfile = &normal_settings;
134
135 LOG_INFO("Writing settings...");
136 fp = myfopen(widen(path).c_str(), widen("wb").c_str());
137 if (!fp)
138 {
139 LOG_ERROR("Couldn't open file {}.", path);
140 return 1;
141 }
142
143 for (int i = 0; i < INPUT_COUNT; i++)
144 setfile->input_mappings[i] = input_get_mapping(i);
145
146 setfile->version = SETTINGS_VERSION;
147 fwrite(setfile, sizeof(Settings), 1, fp);
148
149 fclose(fp);
150 return 0;
151 }
152