yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
rom_sandbox_manager.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cstdlib>
5
6#include "absl/status/status.h"
7#include "absl/status/statusor.h"
8#include "absl/strings/str_cat.h"
9#include "absl/strings/str_format.h"
10#include "absl/time/clock.h"
11#include "absl/time/time.h"
12#include "util/macro.h"
13
14namespace yaze {
15namespace cli {
16
17namespace {
18
19std::filesystem::path DetermineDefaultRoot() {
20 if (const char* env_root = std::getenv("YAZE_SANDBOX_ROOT")) {
21 return std::filesystem::path(env_root);
22 }
23 std::error_code ec;
24 auto temp_dir = std::filesystem::temp_directory_path(ec);
25 if (ec) {
26 // Fallback to current working directory if temp is unavailable.
27 return std::filesystem::current_path() / "yaze" / "sandboxes";
28 }
29 return temp_dir / "yaze" / "sandboxes";
30}
31
32std::filesystem::path ResolveUniqueDirectory(const std::filesystem::path& root,
33 absl::string_view id) {
34 return root / std::string(id);
35}
36
37} // namespace
38
40 static RomSandboxManager* instance = new RomSandboxManager();
41 return *instance;
42}
43
45 : root_directory_(DetermineDefaultRoot()) {}
46
47void RomSandboxManager::SetRootDirectory(const std::filesystem::path& root) {
48 std::lock_guard<std::mutex> lock(mutex_);
49 root_directory_ = root;
51}
52
53const std::filesystem::path& RomSandboxManager::RootDirectory() const {
54 return root_directory_;
55}
56
58 std::error_code ec;
59 if (!std::filesystem::exists(root_directory_, ec)) {
60 if (!std::filesystem::create_directories(root_directory_, ec) && ec) {
61 return absl::InternalError(
62 absl::StrCat("Failed to create sandbox root at ",
63 root_directory_.string(), ": ", ec.message()));
64 }
65 }
66 return absl::OkStatus();
67}
68
70 absl::Time now = absl::Now();
71 std::string time_component =
72 absl::FormatTime("%Y%m%dT%H%M%S", now, absl::LocalTimeZone());
73 ++sequence_;
74 return absl::StrCat(time_component, "-", sequence_);
75}
76
77absl::StatusOr<RomSandboxManager::SandboxMetadata>
78RomSandboxManager::CreateSandbox(Rom& rom, absl::string_view description) {
79 if (!rom.is_loaded()) {
80 return absl::FailedPreconditionError(
81 "Cannot create sandbox: ROM is not loaded");
82 }
83
84 std::filesystem::path source_path(rom.filename());
85 if (source_path.empty()) {
86 return absl::FailedPreconditionError(
87 "Cannot create sandbox: ROM filename is empty");
88 }
89
90 std::unique_lock<std::mutex> lock(mutex_);
92
93 std::string id = GenerateSandboxIdLocked();
94 std::filesystem::path sandbox_dir =
95 ResolveUniqueDirectory(root_directory_, id);
96 lock.unlock();
97
98 std::error_code ec;
99 if (!std::filesystem::create_directories(sandbox_dir, ec) && ec) {
100 return absl::InternalError(
101 absl::StrCat("Failed to create sandbox directory at ",
102 sandbox_dir.string(), ": ", ec.message()));
103 }
104
105 std::filesystem::path sandbox_rom_path = sandbox_dir / source_path.filename();
106
107 Rom::SaveSettings settings;
108 settings.filename = sandbox_rom_path.string();
109 settings.save_new = false;
110 settings.backup = false;
111
112 absl::Status save_status = rom.SaveToFile(settings);
113 if (!save_status.ok()) {
114 std::error_code cleanup_ec;
115 std::filesystem::remove_all(sandbox_dir, cleanup_ec);
116 return save_status;
117 }
118
119 lock.lock();
121 .id = id,
122 .directory = sandbox_dir,
123 .rom_path = sandbox_rom_path,
124 .source_rom = source_path.string(),
125 .description = std::string(description),
126 .created_at = absl::Now(),
127 };
129
130 return sandboxes_.at(id);
131}
132
133absl::StatusOr<RomSandboxManager::SandboxMetadata>
135 std::lock_guard<std::mutex> lock(mutex_);
136 if (!active_sandbox_id_.has_value()) {
137 return absl::NotFoundError("No active sandbox");
138 }
139 auto it = sandboxes_.find(*active_sandbox_id_);
140 if (it == sandboxes_.end()) {
141 return absl::NotFoundError("Active sandbox metadata missing");
142 }
143 return it->second;
144}
145
146absl::StatusOr<std::filesystem::path> RomSandboxManager::ActiveSandboxRomPath()
147 const {
148 ASSIGN_OR_RETURN(auto meta, ActiveSandbox());
149 return meta.rom_path;
150}
151
152std::vector<RomSandboxManager::SandboxMetadata>
154 std::lock_guard<std::mutex> lock(mutex_);
155 std::vector<SandboxMetadata> list;
156 list.reserve(sandboxes_.size());
157 for (const auto& [_, metadata] : sandboxes_) {
158 list.push_back(metadata);
159 }
160 std::sort(list.begin(), list.end(),
161 [](const SandboxMetadata& a, const SandboxMetadata& b) {
162 return a.created_at < b.created_at;
163 });
164 return list;
165}
166
167absl::Status RomSandboxManager::RemoveSandbox(const std::string& id) {
168 std::lock_guard<std::mutex> lock(mutex_);
169 auto it = sandboxes_.find(id);
170 if (it == sandboxes_.end()) {
171 return absl::NotFoundError("Sandbox not found");
172 }
173 std::error_code ec;
174 std::filesystem::remove_all(it->second.directory, ec);
175 if (ec) {
176 return absl::InternalError(
177 absl::StrCat("Failed to remove sandbox directory: ", ec.message()));
178 }
179 sandboxes_.erase(it);
180 if (active_sandbox_id_.has_value() && *active_sandbox_id_ == id) {
181 active_sandbox_id_.reset();
182 }
183 return absl::OkStatus();
184}
185
187 absl::Duration max_age) {
188 std::vector<std::string> to_remove;
189 {
190 std::lock_guard<std::mutex> lock(mutex_);
191 absl::Time threshold = absl::Now() - max_age;
192 for (const auto& [id, metadata] : sandboxes_) {
193 if (metadata.created_at < threshold) {
194 to_remove.push_back(id);
195 }
196 }
197 }
198
199 int removed = 0;
200 for (const auto& id : to_remove) {
201 absl::Status status = RemoveSandbox(id);
202 if (!status.ok()) {
203 return status;
204 }
205 ++removed;
206 }
207 return removed;
208}
209
210} // namespace cli
211} // namespace yaze
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:24
auto filename() const
Definition rom.h:141
absl::Status SaveToFile(const SaveSettings &settings)
Definition rom.cc:165
bool is_loaded() const
Definition rom.h:128
absl::StatusOr< SandboxMetadata > CreateSandbox(Rom &rom, absl::string_view description)
absl::Status RemoveSandbox(const std::string &id)
void SetRootDirectory(const std::filesystem::path &root)
absl::StatusOr< SandboxMetadata > ActiveSandbox() const
absl::StatusOr< int > CleanupOlderThan(absl::Duration max_age)
std::filesystem::path root_directory_
std::vector< SandboxMetadata > ListSandboxes() const
static RomSandboxManager & Instance()
std::optional< std::string > active_sandbox_id_
absl::StatusOr< std::filesystem::path > ActiveSandboxRomPath() const
const std::filesystem::path & RootDirectory() const
std::unordered_map< std::string, SandboxMetadata > sandboxes_
#define ASSIGN_OR_RETURN(type_variable_name, expression)
Definition macro.h:62
std::filesystem::path ResolveUniqueDirectory(const std::filesystem::path &root, absl::string_view id)
#define RETURN_IF_ERROR(expr)
Definition snes.cc:22
std::string filename
Definition rom.h:29