yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
user_settings.cc
Go to the documentation of this file.
2
3#include <fstream>
4#include <sstream>
5
6#include "absl/strings/str_format.h"
8#include "imgui/imgui.h"
9#include "util/file_util.h"
10#include "util/log.h"
11#include "util/platform_paths.h"
12
13namespace yaze {
14namespace editor {
15
17 auto config_dir_status = util::PlatformPaths::GetConfigDirectory();
18 if (config_dir_status.ok()) {
19 settings_file_path_ = (*config_dir_status / "yaze_settings.ini").string();
20 } else {
21 LOG_WARN("UserSettings",
22 "Could not determine config directory. Using local.");
23 settings_file_path_ = "yaze_settings.ini";
24 }
25}
26
27absl::Status UserSettings::Load() {
28 try {
29 // If file doesn't exist, save defaults immediately
31 LOG_INFO("UserSettings",
32 "Settings file not found, creating defaults at: %s",
33 settings_file_path_.c_str());
34 return Save();
35 }
36
38 if (data.empty()) {
39 return absl::OkStatus(); // Empty file, use defaults.
40 }
41
42 std::istringstream ss(data);
43 std::string line;
44 while (std::getline(ss, line)) {
45 size_t eq_pos = line.find('=');
46 if (eq_pos == std::string::npos)
47 continue;
48
49 std::string key = line.substr(0, eq_pos);
50 std::string val = line.substr(eq_pos + 1);
51
52 // General
53 if (key == "font_global_scale") {
54 prefs_.font_global_scale = std::stof(val);
55 } else if (key == "backup_rom") {
56 prefs_.backup_rom = (val == "1");
57 } else if (key == "save_new_auto") {
58 prefs_.save_new_auto = (val == "1");
59 } else if (key == "autosave_enabled") {
60 prefs_.autosave_enabled = (val == "1");
61 } else if (key == "autosave_interval") {
62 prefs_.autosave_interval = std::stof(val);
63 } else if (key == "recent_files_limit") {
64 prefs_.recent_files_limit = std::stoi(val);
65 } else if (key == "last_rom_path") {
67 } else if (key == "last_project_path") {
69 } else if (key == "show_welcome_on_startup") {
70 prefs_.show_welcome_on_startup = (val == "1");
71 } else if (key == "restore_last_session") {
72 prefs_.restore_last_session = (val == "1");
73 } else if (key == "prefer_hmagic_sprite_names") {
75 }
76 // Editor Behavior
77 else if (key == "backup_before_save") {
78 prefs_.backup_before_save = (val == "1");
79 } else if (key == "default_editor") {
80 prefs_.default_editor = std::stoi(val);
81 }
82 // Performance
83 else if (key == "vsync") {
84 prefs_.vsync = (val == "1");
85 } else if (key == "target_fps") {
86 prefs_.target_fps = std::stoi(val);
87 } else if (key == "cache_size_mb") {
88 prefs_.cache_size_mb = std::stoi(val);
89 } else if (key == "undo_history_size") {
90 prefs_.undo_history_size = std::stoi(val);
91 }
92 // AI Agent
93 else if (key == "ai_provider") {
94 prefs_.ai_provider = std::stoi(val);
95 } else if (key == "ollama_url") {
96 prefs_.ollama_url = val;
97 } else if (key == "gemini_api_key") {
99 } else if (key == "ai_temperature") {
100 prefs_.ai_temperature = std::stof(val);
101 } else if (key == "ai_max_tokens") {
102 prefs_.ai_max_tokens = std::stoi(val);
103 } else if (key == "ai_proactive") {
104 prefs_.ai_proactive = (val == "1");
105 } else if (key == "ai_auto_learn") {
106 prefs_.ai_auto_learn = (val == "1");
107 } else if (key == "ai_multimodal") {
108 prefs_.ai_multimodal = (val == "1");
109 }
110 // CLI Logging
111 else if (key == "log_level") {
112 prefs_.log_level = std::stoi(val);
113 } else if (key == "log_to_file") {
114 prefs_.log_to_file = (val == "1");
115 } else if (key == "log_file_path") {
116 prefs_.log_file_path = val;
117 } else if (key == "log_ai_requests") {
118 prefs_.log_ai_requests = (val == "1");
119 } else if (key == "log_rom_operations") {
120 prefs_.log_rom_operations = (val == "1");
121 } else if (key == "log_gui_automation") {
122 prefs_.log_gui_automation = (val == "1");
123 } else if (key == "log_proposals") {
124 prefs_.log_proposals = (val == "1");
125 }
126 // Panel Shortcuts (format: panel_shortcut.panel_id=shortcut)
127 else if (key.substr(0, 15) == "panel_shortcut.") {
128 std::string panel_id = key.substr(15);
129 prefs_.panel_shortcuts[panel_id] = val;
130 }
131 // Backward compatibility for card_shortcut
132 else if (key.substr(0, 14) == "card_shortcut.") {
133 std::string panel_id = key.substr(14);
134 prefs_.panel_shortcuts[panel_id] = val;
135 }
136 // Sidebar State
137 else if (key == "sidebar_visible") {
138 prefs_.sidebar_visible = (val == "1");
139 } else if (key == "sidebar_panel_expanded") {
140 prefs_.sidebar_panel_expanded = (val == "1");
141 } else if (key == "sidebar_active_category") {
143 }
144 // Status Bar
145 else if (key == "show_status_bar") {
146 prefs_.show_status_bar = (val == "1");
147 }
148 }
149 ImGui::GetIO().FontGlobalScale = prefs_.font_global_scale;
150 } catch (const std::exception& e) {
151 return absl::InternalError(
152 absl::StrFormat("Failed to load user settings: %s", e.what()));
153 }
154 return absl::OkStatus();
155}
156
157absl::Status UserSettings::Save() {
158 try {
159 std::ostringstream ss;
160 // General
161 ss << "font_global_scale=" << prefs_.font_global_scale << "\n";
162 ss << "backup_rom=" << (prefs_.backup_rom ? 1 : 0) << "\n";
163 ss << "save_new_auto=" << (prefs_.save_new_auto ? 1 : 0) << "\n";
164 ss << "autosave_enabled=" << (prefs_.autosave_enabled ? 1 : 0) << "\n";
165 ss << "autosave_interval=" << prefs_.autosave_interval << "\n";
166 ss << "recent_files_limit=" << prefs_.recent_files_limit << "\n";
167 ss << "last_rom_path=" << prefs_.last_rom_path << "\n";
168 ss << "last_project_path=" << prefs_.last_project_path << "\n";
169 ss << "show_welcome_on_startup=" << (prefs_.show_welcome_on_startup ? 1 : 0)
170 << "\n";
171 ss << "restore_last_session=" << (prefs_.restore_last_session ? 1 : 0)
172 << "\n";
173 ss << "prefer_hmagic_sprite_names="
174 << (prefs_.prefer_hmagic_sprite_names ? 1 : 0) << "\n";
175
176 // Editor Behavior
177 ss << "backup_before_save=" << (prefs_.backup_before_save ? 1 : 0) << "\n";
178 ss << "default_editor=" << prefs_.default_editor << "\n";
179
180 // Performance
181 ss << "vsync=" << (prefs_.vsync ? 1 : 0) << "\n";
182 ss << "target_fps=" << prefs_.target_fps << "\n";
183 ss << "cache_size_mb=" << prefs_.cache_size_mb << "\n";
184 ss << "undo_history_size=" << prefs_.undo_history_size << "\n";
185
186 // AI Agent
187 ss << "ai_provider=" << prefs_.ai_provider << "\n";
188 ss << "ollama_url=" << prefs_.ollama_url << "\n";
189 ss << "gemini_api_key=" << prefs_.gemini_api_key << "\n";
190 ss << "ai_temperature=" << prefs_.ai_temperature << "\n";
191 ss << "ai_max_tokens=" << prefs_.ai_max_tokens << "\n";
192 ss << "ai_proactive=" << (prefs_.ai_proactive ? 1 : 0) << "\n";
193 ss << "ai_auto_learn=" << (prefs_.ai_auto_learn ? 1 : 0) << "\n";
194 ss << "ai_multimodal=" << (prefs_.ai_multimodal ? 1 : 0) << "\n";
195
196 // CLI Logging
197 ss << "log_level=" << prefs_.log_level << "\n";
198 ss << "log_to_file=" << (prefs_.log_to_file ? 1 : 0) << "\n";
199 ss << "log_file_path=" << prefs_.log_file_path << "\n";
200 ss << "log_ai_requests=" << (prefs_.log_ai_requests ? 1 : 0) << "\n";
201 ss << "log_rom_operations=" << (prefs_.log_rom_operations ? 1 : 0) << "\n";
202 ss << "log_gui_automation=" << (prefs_.log_gui_automation ? 1 : 0) << "\n";
203 ss << "log_proposals=" << (prefs_.log_proposals ? 1 : 0) << "\n";
204
205 // Panel Shortcuts
206 for (const auto& [panel_id, shortcut] : prefs_.panel_shortcuts) {
207 ss << "panel_shortcut." << panel_id << "=" << shortcut << "\n";
208 }
209
210 // Sidebar State
211 ss << "sidebar_visible=" << (prefs_.sidebar_visible ? 1 : 0) << "\n";
212 ss << "sidebar_panel_expanded=" << (prefs_.sidebar_panel_expanded ? 1 : 0)
213 << "\n";
214 ss << "sidebar_active_category=" << prefs_.sidebar_active_category << "\n";
215
216 // Status Bar
217 ss << "show_status_bar=" << (prefs_.show_status_bar ? 1 : 0) << "\n";
218
220 } catch (const std::exception& e) {
221 return absl::InternalError(
222 absl::StrFormat("Failed to save user settings: %s", e.what()));
223 }
224 return absl::OkStatus();
225}
226
227} // namespace editor
228} // namespace yaze
static absl::StatusOr< std::filesystem::path > GetConfigDirectory()
Get the user-specific configuration directory for YAZE.
static bool Exists(const std::filesystem::path &path)
Check if a file or directory exists.
#define LOG_WARN(category, format,...)
Definition log.h:107
#define LOG_INFO(category, format,...)
Definition log.h:105
void SaveFile(const std::string &filename, const std::string &contents)
Definition file_util.cc:56
std::string LoadFile(const std::string &filename)
Loads the entire contents of a file into a string.
Definition file_util.cc:23
std::unordered_map< std::string, std::string > panel_shortcuts