yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
palette.cc
Go to the documentation of this file.
1#include "absl/flags/declare.h"
2#include "absl/flags/flag.h"
5#include "cli/cli.h"
6#include "zelda3/game_data.h"
7
8ABSL_DECLARE_FLAG(std::string, rom);
9
10namespace yaze {
11namespace cli {
12
13// Forward declarations for legacy functions
14absl::Status HandlePaletteExportLegacy(const std::vector<std::string>& arg_vec);
15absl::Status HandlePaletteImportLegacy(const std::vector<std::string>& arg_vec);
16
17// Legacy Palette constructor removed - using new CommandHandler system
18
19// Legacy Palette class removed - using new CommandHandler system
20// This implementation should be moved to PaletteCommandHandler
21absl::Status HandlePaletteLegacy(const std::vector<std::string>& arg_vec) {
22 if (arg_vec.empty()) {
23 return absl::InvalidArgumentError(
24 "Usage: palette <export|import> [options]");
25 }
26
27 const std::string& action = arg_vec[0];
28 std::vector<std::string> new_args(arg_vec.begin() + 1, arg_vec.end());
29
30 if (action == "export") {
31 return HandlePaletteExportLegacy(new_args);
32 } else if (action == "import") {
33 return HandlePaletteImportLegacy(new_args);
34 }
35
36 return absl::InvalidArgumentError("Invalid action for palette command.");
37}
38
39// Legacy PaletteExport class removed - using new CommandHandler system
41 const std::vector<std::string>& arg_vec) {
42 std::string group_name;
43 int palette_id = -1;
44 std::string output_file;
45
46 for (size_t i = 0; i < arg_vec.size(); ++i) {
47 const std::string& arg = arg_vec[i];
48 if ((arg == "--group") && i + 1 < arg_vec.size()) {
49 group_name = arg_vec[++i];
50 } else if ((arg == "--id") && i + 1 < arg_vec.size()) {
51 palette_id = std::stoi(arg_vec[++i]);
52 } else if ((arg == "--to") && i + 1 < arg_vec.size()) {
53 output_file = arg_vec[++i];
54 }
55 }
56
57 if (group_name.empty() || palette_id == -1 || output_file.empty()) {
58 return absl::InvalidArgumentError(
59 "Usage: palette export --group <group> --id <id> --to <file>");
60 }
61
62 std::string rom_file = absl::GetFlag(FLAGS_rom);
63 if (rom_file.empty()) {
64 return absl::InvalidArgumentError(
65 "ROM file must be provided via --rom flag.");
66 }
67
68 Rom rom;
69 rom.LoadFromFile(rom_file);
70 if (!rom.is_loaded()) {
71 return absl::AbortedError("Failed to load ROM.");
72 }
73
74 // Load game data for palette access
75 zelda3::GameData game_data;
76 auto load_status = zelda3::LoadGameData(rom, game_data);
77 if (!load_status.ok()) {
78 return absl::AbortedError("Failed to load game data.");
79 }
80
81 auto palette_group = game_data.palette_groups.get_group(group_name);
82 if (!palette_group) {
83 return absl::NotFoundError("Palette group not found.");
84 }
85
86 auto palette = palette_group->palette(palette_id);
87 if (palette.empty()) {
88 return absl::NotFoundError("Palette not found.");
89 }
90
91 std::vector<SDL_Color> sdl_palette;
92 for (const auto& color : palette) {
93 SDL_Color sdl_color;
94 sdl_color.r = color.rgb().x;
95 sdl_color.g = color.rgb().y;
96 sdl_color.b = color.rgb().z;
97 sdl_color.a = 255;
98 sdl_palette.push_back(sdl_color);
99 }
100
101 auto status = gfx::SaveCol(output_file, sdl_palette);
102 if (!status.ok()) {
103 return status;
104 }
105
106 std::cout << "Successfully exported palette " << palette_id << " from group "
107 << group_name << " to " << output_file << std::endl;
108
109 return absl::OkStatus();
110}
111
112// Legacy PaletteImport class removed - using new CommandHandler system
114 const std::vector<std::string>& arg_vec) {
115 std::string group_name;
116 int palette_id = -1;
117 std::string input_file;
118
119 for (size_t i = 0; i < arg_vec.size(); ++i) {
120 const std::string& arg = arg_vec[i];
121 if ((arg == "--group") && i + 1 < arg_vec.size()) {
122 group_name = arg_vec[++i];
123 } else if ((arg == "--id") && i + 1 < arg_vec.size()) {
124 palette_id = std::stoi(arg_vec[++i]);
125 } else if ((arg == "--from") && i + 1 < arg_vec.size()) {
126 input_file = arg_vec[++i];
127 }
128 }
129
130 if (group_name.empty() || palette_id == -1 || input_file.empty()) {
131 return absl::InvalidArgumentError(
132 "Usage: palette import --group <group> --id <id> --from <file>");
133 }
134
135 std::string rom_file = absl::GetFlag(FLAGS_rom);
136 if (rom_file.empty()) {
137 return absl::InvalidArgumentError(
138 "ROM file must be provided via --rom flag.");
139 }
140
141 Rom rom;
142 rom.LoadFromFile(rom_file);
143 if (!rom.is_loaded()) {
144 return absl::AbortedError("Failed to load ROM.");
145 }
146
147 // Load game data for palette access
148 zelda3::GameData game_data;
149 auto load_status = zelda3::LoadGameData(rom, game_data);
150 if (!load_status.ok()) {
151 return absl::AbortedError("Failed to load game data.");
152 }
153
154 auto sdl_palette = gfx::DecodeColFile(input_file);
155 if (sdl_palette.empty()) {
156 return absl::AbortedError("Failed to load palette file.");
157 }
158
160 for (const auto& sdl_color : sdl_palette) {
161 snes_palette.AddColor(
162 gfx::SnesColor(sdl_color.r, sdl_color.g, sdl_color.b));
163 }
164
165 auto* palette_group = game_data.palette_groups.get_group(group_name);
166 if (!palette_group) {
167 return absl::NotFoundError("Palette group not found.");
168 }
169
170 // Replace the palette at the specified index
171 auto* pal = palette_group->mutable_palette(palette_id);
172 *pal = snes_palette;
173
174 // TODO: Implement saving the modified palette back to the ROM.
175 auto save_status = rom.SaveToFile({.save_new = false});
176 if (!save_status.ok()) {
177 return save_status;
178 }
179
180 std::cout << "Successfully imported palette " << palette_id << " to group "
181 << group_name << " from " << input_file << std::endl;
182 std::cout << "✅ ROM saved to: " << rom.filename() << std::endl;
183
184 return absl::OkStatus();
185}
186
187} // namespace cli
188} // 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:28
absl::Status LoadFromFile(const std::string &filename, const LoadOptions &options=LoadOptions::Defaults())
Definition rom.cc:155
auto filename() const
Definition rom.h:145
absl::Status SaveToFile(const SaveSettings &settings)
Definition rom.cc:291
bool is_loaded() const
Definition rom.h:132
SNES Color container.
Definition snes_color.h:110
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
absl::Status HandlePaletteLegacy(const std::vector< std::string > &arg_vec)
Definition palette.cc:21
absl::Status HandlePaletteExportLegacy(const std::vector< std::string > &arg_vec)
Definition palette.cc:40
absl::Status HandlePaletteImportLegacy(const std::vector< std::string > &arg_vec)
Definition palette.cc:113
absl::Status SaveCol(std::string_view filename, const std::vector< SDL_Color > &palette)
Save Col file (palette data)
std::vector< SDL_Color > DecodeColFile(const std::string_view filename)
Decode color file.
absl::Status LoadGameData(Rom &rom, GameData &data, const LoadOptions &options)
Loads all Zelda3-specific game data from a generic ROM.
Definition game_data.cc:123
ABSL_DECLARE_FLAG(std::string, rom)
SNES color palette.
PaletteGroup * get_group(const std::string &group_name)
gfx::PaletteGroupMap palette_groups
Definition game_data.h:89
struct snes_palette snes_palette
SNES color palette.