yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
project_commands.cc
Go to the documentation of this file.
2
3#include "app/core/project.h"
4#include "util/file_util.h"
5#include "util/bps.h"
6#include "util/macro.h"
7#include <filesystem>
8#include <iostream>
9
10namespace yaze {
11namespace cli {
12namespace handlers {
13
15 const resources::ArgumentParser& parser,
16 resources::OutputFormatter& formatter) {
17 auto project_opt = parser.GetString("project_name");
18
19 if (!project_opt.has_value()) {
20 return absl::InvalidArgumentError("Missing required argument: project_name");
21 }
22
23 std::string project_name = project_opt.value();
24
25 core::YazeProject project;
26 auto status = project.Create(project_name, ".");
27 if (!status.ok()) {
28 return status;
29 }
30
31 formatter.AddField("status", "success");
32 formatter.AddField("message", "Successfully initialized project: " + project_name);
33 formatter.AddField("project_name", project_name);
34
35 return absl::OkStatus();
36}
37
39 const resources::ArgumentParser& parser,
40 resources::OutputFormatter& formatter) {
41 core::YazeProject project;
42 auto status = project.Open(".");
43 if (!status.ok()) {
44 return status;
45 }
46
47 Rom build_rom;
48 status = build_rom.LoadFromFile(project.rom_filename);
49 if (!status.ok()) {
50 return status;
51 }
52
53 // Apply BPS patches - cross-platform with std::filesystem
54 namespace fs = std::filesystem;
55 std::vector<std::string> bps_files;
56
57 try {
58 for (const auto& entry : fs::directory_iterator(project.patches_folder)) {
59 if (entry.path().extension() == ".bps") {
60 bps_files.push_back(entry.path().string());
61 }
62 }
63 } catch (const fs::filesystem_error& e) {
64 // Patches folder doesn't exist or not accessible
65 }
66
67 for (const auto& patch_file : bps_files) {
68 std::vector<uint8_t> patch_data;
69 auto patch_contents = util::LoadFile(patch_file);
70 std::copy(patch_contents.begin(), patch_contents.end(),
71 std::back_inserter(patch_data));
72 std::vector<uint8_t> patched_rom;
73 util::ApplyBpsPatch(build_rom.vector(), patch_data, patched_rom);
74 build_rom.LoadFromData(patched_rom);
75 }
76
77 // Run asar on assembly files - cross-platform
78 std::vector<std::string> asm_files;
79 try {
80 for (const auto& entry : fs::directory_iterator(project.patches_folder)) {
81 if (entry.path().extension() == ".asm") {
82 asm_files.push_back(entry.path().string());
83 }
84 }
85 } catch (const fs::filesystem_error& e) {
86 // No asm files
87 }
88
89 // TODO: Implement ASM patching functionality
90 // for (const auto& asm_file : asm_files) {
91 // // Apply ASM patches here
92 // }
93
94 std::string output_file = project.name + ".sfc";
95 status = build_rom.SaveToFile({.save_new = true, .filename = output_file});
96 if (!status.ok()) {
97 return status;
98 }
99
100 formatter.AddField("status", "success");
101 formatter.AddField("message", "Successfully built project: " + project.name);
102 formatter.AddField("project_name", project.name);
103 formatter.AddField("output_file", output_file);
104
105 return absl::OkStatus();
106}
107
108} // namespace handlers
109} // namespace cli
110} // namespace yaze
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:71
absl::Status LoadFromFile(const std::string &filename, bool z3_load=true)
Definition rom.cc:289
absl::Status LoadFromData(const std::vector< uint8_t > &data, bool z3_load=true)
Definition rom.cc:381
auto vector() const
Definition rom.h:207
absl::Status SaveToFile(const SaveSettings &settings)
Definition rom.cc:536
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
Utility for parsing common CLI argument patterns.
std::optional< std::string > GetString(const std::string &name) const
Parse a named argument (e.g., –format=json or –format json)
Utility for consistent output formatting across commands.
void AddField(const std::string &key, const std::string &value)
Add a key-value pair.
absl::Status ApplyBpsPatch(const std::vector< uint8_t > &source, const std::vector< uint8_t > &patch, std::vector< uint8_t > &output)
Definition bps.cc:79
std::string LoadFile(const std::string &filename)
Loads the entire contents of a file into a string.
Definition file_util.cc:23
Main namespace for the application.
Modern project structure with comprehensive settings consolidation.
Definition project.h:78
absl::Status Open(const std::string &project_path)
Definition project.cc:115
absl::Status Create(const std::string &project_name, const std::string &base_path)
Definition project.cc:76
std::string rom_filename
Definition project.h:86
std::string patches_folder
Definition project.h:93