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