yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
command.h
Go to the documentation of this file.
1#ifndef YAZE_CLI_COMMAND_HANDLER_H
2#define YAZE_CLI_COMMAND_HANDLER_H
3
4#include <cstdint>
5#include <iostream>
6#include <memory>
7#include <sstream>
8#include <string>
9#include <unordered_map>
10#include <vector>
11
12#include "absl/status/status.h"
13#include "app/core/common.h"
14#include "app/core/constants.h"
15#include "app/rom.h"
16#include "asar-dll-bindings/c/asar.h"
17
18namespace yaze {
19namespace cli {
20
37
38 public:
39 explicit ColorModifier(ColorCode pCode) : code(pCode) {}
40 friend std::ostream& operator<<(std::ostream& os, const ColorModifier& mod) {
41 return os << "\033[" << mod.code << "m";
42 }
43};
44
46 public:
47 CommandHandler() = default;
48 virtual ~CommandHandler() = default;
49 virtual absl::Status handle(const std::vector<std::string>& arg_vec) = 0;
50
52};
53
54class ApplyPatch : public CommandHandler {
55 public:
56 absl::Status handle(const std::vector<std::string>& arg_vec) override;
57};
58
59class AsarPatch : public CommandHandler {
60 public:
61 absl::Status handle(const std::vector<std::string>& arg_vec) override;
62};
63
65 public:
66 absl::Status handle(const std::vector<std::string>& arg_vec) override;
67};
68
72class Open : public CommandHandler {
73 public:
74 absl::Status handle(const std::vector<std::string>& arg_vec) override {
78 auto const& arg = arg_vec[0];
80 std::cout << "Title: " << green << rom_.title() << std::endl;
81 std::cout << reset << "Size: " << blue << "0x" << std::hex << rom_.size()
82 << reset << std::endl;
83 return absl::OkStatus();
84 }
85};
86
90class Backup : public CommandHandler {
91 public:
92 absl::Status handle(const std::vector<std::string>& arg_vec) override {
94 if (arg_vec.size() == 2) {
95 // Optional filename added
96 RETURN_IF_ERROR(rom_.SaveToFile(/*backup=*/true, false, arg_vec[1]))
97 } else {
98 RETURN_IF_ERROR(rom_.SaveToFile(/*backup=*/true))
99 }
100 return absl::OkStatus();
101 }
102};
103
104// Compress Graphics
105class Compress : public CommandHandler {
106 public:
107 absl::Status handle(const std::vector<std::string>& arg_vec) override;
108};
109
110// Decompress (Export) Graphics
111//
112// -e <rom_file> <bin_file> --mode=<optional:mode>
113//
114// mode:
116 public:
117 absl::Status handle(const std::vector<std::string>& arg_vec) override;
118};
119
126class SnesToPc : public CommandHandler {
127 public:
128 absl::Status handle(const std::vector<std::string>& arg_vec) override {
129 auto arg = arg_vec[0];
130 std::stringstream ss(arg.data());
131 uint32_t snes_address;
132 ss >> std::hex >> snes_address;
133 uint32_t pc_address = app::core::SnesToPc(snes_address);
134 std::cout << std::hex << pc_address << std::endl;
135 return absl::OkStatus();
136 }
137};
138
145class PcToSnes : public CommandHandler {
146 public:
147 absl::Status handle(const std::vector<std::string>& arg_vec) override {
148 auto arg = arg_vec[0];
149 std::stringstream ss(arg.data());
150 uint32_t pc_address;
151 ss >> std::hex >> pc_address;
152 uint32_t snes_address = app::core::PcToSnes(pc_address);
154 std::cout << "SNES LoROM Address: ";
155 std::cout << blue << "$" << std::uppercase << std::hex << snes_address
156 << "\n";
157 return absl::OkStatus();
158 }
159};
160
168 public:
169 absl::Status handle(const std::vector<std::string>& arg_vec) override {
171
172 std::stringstream ss(arg_vec[1].data());
173 uint32_t offset;
174 ss >> std::hex >> offset;
175 uint32_t length = 0x01;
176 if (!arg_vec[2].empty()) {
177 length = std::stoi(arg_vec[2]);
178 }
179
180 if (length > 1) {
181 auto returned_bytes_status = rom_.ReadByteVector(offset, length);
182 if (!returned_bytes_status.ok()) {
183 return returned_bytes_status.status();
184 }
185 auto returned_bytes = returned_bytes_status.value();
186 for (const auto& each : returned_bytes) {
187 std::cout << each;
188 }
189 std::cout << std::endl;
190 } else {
191 auto byte = rom_.ReadByte(offset);
192 std::cout << std::hex << byte.value() << std::endl;
193 }
194
195 return absl::OkStatus();
196 }
197};
198
206 public:
207 absl::Status handle(const std::vector<std::string>& arg_vec) override;
208};
209
216class Expand : public CommandHandler {
217 public:
218 absl::Status handle(const std::vector<std::string>& arg_vec) override {
220
221 std::stringstream ss(arg_vec[1].data());
222 uint32_t size;
223 ss >> std::hex >> size;
224
225 rom_.Expand(size);
226
227 std::cout << "Successfully expanded ROM to " << std::hex << size
228 << std::endl;
229
230 return absl::OkStatus();
231 }
232};
233
237struct Commands {
238 std::unordered_map<std::string, std::shared_ptr<CommandHandler>> handlers = {
239 {"-a", std::make_shared<ApplyPatch>()},
240 {"-asar", std::make_shared<AsarPatch>()},
241 {"-c", std::make_shared<CreatePatch>()},
242 {"-o", std::make_shared<Open>()},
243 {"-b", std::make_shared<Backup>()},
244 {"-x", std::make_shared<Expand>()},
245 {"-i", std::make_shared<Compress>()}, // Import
246 {"-e", std::make_shared<Decompress>()}, // Export
247 {"-s", std::make_shared<SnesToPc>()},
248 {"-p", std::make_shared<PcToSnes>()},
249 {"-t", std::make_shared<Tile16Transfer>()},
250 {"-r", std::make_shared<ReadFromRom>()} // Read from Rom
251 };
252};
253
254} // namespace cli
255} // namespace yaze
256
257#endif
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:136
absl::Status SaveToFile(bool backup, bool save_new=false, std::string filename="")
Saves the Rom data to a file.
Definition rom.cc:277
auto size() const
Definition rom.h:460
void Expand(int size)
Expand the Rom data to a specified size.
Definition rom.h:219
auto title() const
Definition rom.h:459
absl::Status LoadFromFile(const std::string &filename, bool z3_load=true)
Definition rom.cc:168
absl::StatusOr< uint8_t > ReadByte(int offset)
Definition rom.h:249
absl::StatusOr< std::vector< uint8_t > > ReadByteVector(uint32_t offset, uint32_t length)
Definition rom.h:280
absl::Status handle(const std::vector< std::string > &arg_vec) override
Definition patch.cc:6
absl::Status handle(const std::vector< std::string > &arg_vec) override
Definition patch.cc:27
Backup a Rom file.
Definition command.h:90
absl::Status handle(const std::vector< std::string > &arg_vec) override
Definition command.h:92
friend std::ostream & operator<<(std::ostream &os, const ColorModifier &mod)
Definition command.h:40
ColorModifier(ColorCode pCode)
Definition command.h:39
virtual ~CommandHandler()=default
virtual absl::Status handle(const std::vector< std::string > &arg_vec)=0
absl::Status handle(const std::vector< std::string > &arg_vec) override
Definition compress.cc:6
absl::Status handle(const std::vector< std::string > &arg_vec) override
Definition patch.cc:46
absl::Status handle(const std::vector< std::string > &arg_vec) override
Definition compress.cc:11
Expand a Rom file.
Definition command.h:216
absl::Status handle(const std::vector< std::string > &arg_vec) override
Definition command.h:218
Open a Rom file and display information about it.
Definition command.h:72
absl::Status handle(const std::vector< std::string > &arg_vec) override
Definition command.h:74
Convert a PC address to a SNES address.
Definition command.h:145
absl::Status handle(const std::vector< std::string > &arg_vec) override
Definition command.h:147
Read from a Rom file.
Definition command.h:167
absl::Status handle(const std::vector< std::string > &arg_vec) override
Definition command.h:169
Convert a SNES address to a PC address.
Definition command.h:126
absl::Status handle(const std::vector< std::string > &arg_vec) override
Definition command.h:128
Transfer tile 16 data from one Rom to another.
Definition command.h:205
absl::Status handle(const std::vector< std::string > &arg_vec) override
#define RETURN_IF_ERROR(expression)
Definition constants.h:62
uint32_t PcToSnes(uint32_t addr)
Definition common.h:234
uint32_t SnesToPc(uint32_t addr) noexcept
Definition common.h:226
@ BG_GREEN
Definition command.h:31
@ FG_RESET
Definition command.h:28
@ BG_BLUE
Definition command.h:32
@ FG_MAGENTA
Definition command.h:26
@ BG_DEFAULT
Definition command.h:33
@ FG_DEFAULT
Definition command.h:27
@ FG_UNDERLINE
Definition command.h:29
@ FG_YELLOW
Definition command.h:24
@ FG_GREEN
Definition command.h:23
@ FG_BLUE
Definition command.h:25
Definition common.cc:22
Command handler for the CLI.
Definition command.h:237
std::unordered_map< std::string, std::shared_ptr< CommandHandler > > handlers
Definition command.h:238