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", "Settings file not found, creating defaults at: %s",
32 settings_file_path_.c_str());
33 return Save();
34 }
35
37 if (data.empty()) {
38 return absl::OkStatus(); // Empty file, use defaults.
39 }
40
41 std::istringstream ss(data);
42 std::string line;
43 while (std::getline(ss, line)) {
44 size_t eq_pos = line.find('=');
45 if (eq_pos == std::string::npos)
46 continue;
47
48 std::string key = line.substr(0, eq_pos);
49 std::string val = line.substr(eq_pos + 1);
50
51 // General
52 if (key == "font_global_scale") {
53 prefs_.font_global_scale = std::stof(val);
54 } else if (key == "backup_rom") {
55 prefs_.backup_rom = (val == "1");
56 } else if (key == "save_new_auto") {
57 prefs_.save_new_auto = (val == "1");
58 } else if (key == "autosave_enabled") {
59 prefs_.autosave_enabled = (val == "1");
60 } else if (key == "autosave_interval") {
61 prefs_.autosave_interval = std::stof(val);
62 } else if (key == "recent_files_limit") {
63 prefs_.recent_files_limit = std::stoi(val);
64 } else if (key == "last_rom_path") {
66 } else if (key == "last_project_path") {
68 } else if (key == "show_welcome_on_startup") {
69 prefs_.show_welcome_on_startup = (val == "1");
70 } else if (key == "restore_last_session") {
71 prefs_.restore_last_session = (val == "1");
72 } else if (key == "prefer_hmagic_sprite_names") {
74 }
75 // Editor Behavior
76 else if (key == "backup_before_save") {
77 prefs_.backup_before_save = (val == "1");
78 } else if (key == "default_editor") {
79 prefs_.default_editor = std::stoi(val);
80 }
81 // Performance
82 else if (key == "vsync") {
83 prefs_.vsync = (val == "1");
84 } else if (key == "target_fps") {
85 prefs_.target_fps = std::stoi(val);
86 } else if (key == "cache_size_mb") {
87 prefs_.cache_size_mb = std::stoi(val);
88 } else if (key == "undo_history_size") {
89 prefs_.undo_history_size = std::stoi(val);
90 }
91 // AI Agent
92 else if (key == "ai_provider") {
93 prefs_.ai_provider = std::stoi(val);
94 } else if (key == "ollama_url") {
95 prefs_.ollama_url = val;
96 } else if (key == "gemini_api_key") {
98 } else if (key == "ai_temperature") {
99 prefs_.ai_temperature = std::stof(val);
100 } else if (key == "ai_max_tokens") {
101 prefs_.ai_max_tokens = std::stoi(val);
102 } else if (key == "ai_proactive") {
103 prefs_.ai_proactive = (val == "1");
104 } else if (key == "ai_auto_learn") {
105 prefs_.ai_auto_learn = (val == "1");
106 } else if (key == "ai_multimodal") {
107 prefs_.ai_multimodal = (val == "1");
108 }
109 // CLI Logging
110 else if (key == "log_level") {
111 prefs_.log_level = std::stoi(val);
112 } else if (key == "log_to_file") {
113 prefs_.log_to_file = (val == "1");
114 } else if (key == "log_file_path") {
115 prefs_.log_file_path = val;
116 } else if (key == "log_ai_requests") {
117 prefs_.log_ai_requests = (val == "1");
118 } else if (key == "log_rom_operations") {
119 prefs_.log_rom_operations = (val == "1");
120 } else if (key == "log_gui_automation") {
121 prefs_.log_gui_automation = (val == "1");
122 } else if (key == "log_proposals") {
123 prefs_.log_proposals = (val == "1");
124 }
125 // Panel Shortcuts (format: panel_shortcut.panel_id=shortcut)
126 else if (key.substr(0, 15) == "panel_shortcut.") {
127 std::string panel_id = key.substr(15);
128 prefs_.panel_shortcuts[panel_id] = val;
129 }
130 // Backward compatibility for card_shortcut
131 else if (key.substr(0, 14) == "card_shortcut.") {
132 std::string panel_id = key.substr(14);
133 prefs_.panel_shortcuts[panel_id] = val;
134 }
135 // Sidebar State
136 else if (key == "sidebar_visible") {
137 prefs_.sidebar_visible = (val == "1");
138 } else if (key == "sidebar_panel_expanded") {
139 prefs_.sidebar_panel_expanded = (val == "1");
140 } else if (key == "sidebar_active_category") {
142 }
143 // Status Bar
144 else if (key == "show_status_bar") {
145 prefs_.show_status_bar = (val == "1");
146 }
147 }
148 ImGui::GetIO().FontGlobalScale = prefs_.font_global_scale;
149 } catch (const std::exception& e) {
150 return absl::InternalError(
151 absl::StrFormat("Failed to load user settings: %s", e.what()));
152 }
153 return absl::OkStatus();
154}
155
156absl::Status UserSettings::Save() {
157 try {
158 std::ostringstream ss;
159 // General
160 ss << "font_global_scale=" << prefs_.font_global_scale << "\n";
161 ss << "backup_rom=" << (prefs_.backup_rom ? 1 : 0) << "\n";
162 ss << "save_new_auto=" << (prefs_.save_new_auto ? 1 : 0) << "\n";
163 ss << "autosave_enabled=" << (prefs_.autosave_enabled ? 1 : 0) << "\n";
164 ss << "autosave_interval=" << prefs_.autosave_interval << "\n";
165 ss << "recent_files_limit=" << prefs_.recent_files_limit << "\n";
166 ss << "last_rom_path=" << prefs_.last_rom_path << "\n";
167 ss << "last_project_path=" << prefs_.last_project_path << "\n";
168 ss << "show_welcome_on_startup=" << (prefs_.show_welcome_on_startup ? 1 : 0)
169 << "\n";
170 ss << "restore_last_session=" << (prefs_.restore_last_session ? 1 : 0)
171 << "\n";
172 ss << "prefer_hmagic_sprite_names=" << (prefs_.prefer_hmagic_sprite_names ? 1 : 0)
173 << "\n";
174
175 // Editor Behavior
176 ss << "backup_before_save=" << (prefs_.backup_before_save ? 1 : 0) << "\n";
177 ss << "default_editor=" << prefs_.default_editor << "\n";
178
179 // Performance
180 ss << "vsync=" << (prefs_.vsync ? 1 : 0) << "\n";
181 ss << "target_fps=" << prefs_.target_fps << "\n";
182 ss << "cache_size_mb=" << prefs_.cache_size_mb << "\n";
183 ss << "undo_history_size=" << prefs_.undo_history_size << "\n";
184
185 // AI Agent
186 ss << "ai_provider=" << prefs_.ai_provider << "\n";
187 ss << "ollama_url=" << prefs_.ollama_url << "\n";
188 ss << "gemini_api_key=" << prefs_.gemini_api_key << "\n";
189 ss << "ai_temperature=" << prefs_.ai_temperature << "\n";
190 ss << "ai_max_tokens=" << prefs_.ai_max_tokens << "\n";
191 ss << "ai_proactive=" << (prefs_.ai_proactive ? 1 : 0) << "\n";
192 ss << "ai_auto_learn=" << (prefs_.ai_auto_learn ? 1 : 0) << "\n";
193 ss << "ai_multimodal=" << (prefs_.ai_multimodal ? 1 : 0) << "\n";
194
195 // CLI Logging
196 ss << "log_level=" << prefs_.log_level << "\n";
197 ss << "log_to_file=" << (prefs_.log_to_file ? 1 : 0) << "\n";
198 ss << "log_file_path=" << prefs_.log_file_path << "\n";
199 ss << "log_ai_requests=" << (prefs_.log_ai_requests ? 1 : 0) << "\n";
200 ss << "log_rom_operations=" << (prefs_.log_rom_operations ? 1 : 0) << "\n";
201 ss << "log_gui_automation=" << (prefs_.log_gui_automation ? 1 : 0) << "\n";
202 ss << "log_proposals=" << (prefs_.log_proposals ? 1 : 0) << "\n";
203
204 // Panel Shortcuts
205 for (const auto& [panel_id, shortcut] : prefs_.panel_shortcuts) {
206 ss << "panel_shortcut." << panel_id << "=" << shortcut << "\n";
207 }
208
209 // Sidebar State
210 ss << "sidebar_visible=" << (prefs_.sidebar_visible ? 1 : 0) << "\n";
211 ss << "sidebar_panel_expanded=" << (prefs_.sidebar_panel_expanded ? 1 : 0) << "\n";
212 ss << "sidebar_active_category=" << prefs_.sidebar_active_category << "\n";
213
214 // Status Bar
215 ss << "show_status_bar=" << (prefs_.show_status_bar ? 1 : 0) << "\n";
216
218 } catch (const std::exception& e) {
219 return absl::InternalError(
220 absl::StrFormat("Failed to save user settings: %s", e.what()));
221 }
222 return absl::OkStatus();
223}
224
225} // namespace editor
226} // 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