yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
resource_labels.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <array>
5#include <cctype>
6#include <sstream>
7#include <string>
8#include <vector>
9
10#include "absl/strings/str_format.h"
11#include "absl/strings/str_split.h"
12#include "absl/strings/strip.h"
15
16// External declarations for Hyrule Magic sprite names (defined in sprite.cc)
17namespace yaze::zelda3 {
18extern const char* const kSpriteNames[];
19extern const size_t kSpriteNameCount;
20} // namespace yaze::zelda3
21
22namespace yaze {
23namespace zelda3 {
24
25// ============================================================================
26// Resource Type String Conversion
27// ============================================================================
28
30 switch (type) {
32 return "sprite";
34 return "room";
36 return "entrance";
38 return "item";
40 return "overlord";
42 return "overworld_map";
44 return "music";
46 return "graphics";
48 return "room_effect";
50 return "room_tag";
52 return "tile_type";
53 }
54 return "unknown";
55}
56
57ResourceType StringToResourceType(const std::string& type_str) {
58 if (type_str == "sprite")
60 if (type_str == "room")
62 if (type_str == "entrance")
64 if (type_str == "item")
66 if (type_str == "overlord")
68 if (type_str == "overworld_map")
70 if (type_str == "music")
72 if (type_str == "graphics")
74 if (type_str == "room_effect")
76 if (type_str == "room_tag")
78 if (type_str == "tile_type")
80 // Default fallback
82}
83
84namespace {
85
87 int id) {
88 auto lookup = [&](const std::string& key) -> std::string {
89 auto it = labels.find(key);
90 if (it != labels.end() && !it->second.empty()) {
91 return it->second;
92 }
93 return "";
94 };
95
96 // Canonical storage uses decimal keys.
97 if (std::string decimal = lookup(std::to_string(id)); !decimal.empty()) {
98 return decimal;
99 }
100
101 // Compatibility: older Oracle label bundles often use prefixed hex keys.
102 const std::string hex_upper = absl::StrFormat("%X", id);
103 const std::string hex_lower = absl::StrFormat("%x", id);
104 const std::array<std::string, 8> keys = {
105 absl::StrFormat("0x%s", hex_lower), absl::StrFormat("0x%s", hex_upper),
106 absl::StrFormat("0X%s", hex_upper), absl::StrFormat("$%s", hex_upper),
107 absl::StrFormat("0x%02X", id), absl::StrFormat("0x%03X", id),
108 absl::StrFormat("0x%04X", id), absl::StrFormat("$%02X", id),
109 };
110
111 for (const auto& key : keys) {
112 if (std::string label = lookup(key); !label.empty()) {
113 return label;
114 }
115 }
116
117 return "";
118}
119
121 uint8_t id, const ResourceLabelProvider::ProjectLabels* project_labels) {
122 if (project_labels) {
123 const auto it = project_labels->find("item_receipt");
124 if (it != project_labels->end()) {
125 return LookupProjectLabel(it->second, id);
126 }
127 }
128 return "";
129}
130
132 int id) {
133 // The project registry can be loaded independently of a hack manifest in
134 // real Oracle projects (see ProjectPathsTest::
135 // OpenInjectsOracleDungeonRoomLabelsIntoProjectFile). Don't gate registry
136 // lookups on manifest->loaded().
137 if (!manifest || !manifest->HasProjectRegistry()) {
138 return "";
139 }
140
141 for (const auto& dungeon : manifest->project_registry().dungeons) {
142 for (const auto& room : dungeon.rooms) {
143 if (room.id == id && !room.name.empty()) {
144 return room.name;
145 }
146 }
147 }
148 return "";
149}
150
151} // namespace
152
153// ============================================================================
154// Global Provider Instance
155// ============================================================================
156
158 static ResourceLabelProvider instance;
159 return instance;
160}
161
162// ============================================================================
163// ResourceLabelProvider Implementation
164// ============================================================================
165
166std::string ResourceLabelProvider::GetLabel(ResourceType type, int id) const {
167 std::string type_str = ResourceTypeToString(type);
168
169 // 1. Room names in the project registry are canonical for Oracle project
170 // files. Prefer them over merged resource labels so stale legacy
171 // oracle_room_labels.json / manually imported labels cannot mask the current
172 // dungeons.json room names.
173 if (type == ResourceType::kRoom) {
174 if (std::string registry_label =
175 LookupProjectRegistryRoomLabel(hack_manifest_, id);
176 !registry_label.empty()) {
177 return registry_label;
178 }
179 }
180
181 // 2. Check project-specific labels.
182 // Accept decimal keys and prefixed hex keys for compatibility with older
183 // Oracle label bundles.
184 if (project_labels_) {
185 auto type_it = project_labels_->find(type_str);
186 if (type_it != project_labels_->end()) {
187 if (std::string project_label = LookupProjectLabel(type_it->second, id);
188 !project_label.empty()) {
189 return project_label;
190 }
191 }
192 }
193
194 // 3. Check Hack Manifest for ASM-defined labels
196 if (type == ResourceType::kRoomTag) {
197 std::string manifest_label = hack_manifest_->GetRoomTagLabel(id);
198 if (!manifest_label.empty()) {
199 return manifest_label;
200 }
201 }
202 // Future: Add message label lookup if we add GetMessageLabel to HackManifest
203 }
204
205 // 4. For sprites, check Hyrule Magic names if preferred
206 if (type == ResourceType::kSprite && prefer_hmagic_) {
207 std::string hmagic = GetHMagicLabel(type, id);
208 if (!hmagic.empty()) {
209 return hmagic;
210 }
211 }
212
213 // 5. Fall back to vanilla labels
214 return GetVanillaLabel(type, id);
215}
216
217std::string ResourceLabelProvider::GetLabel(const std::string& type_str,
218 int id) const {
219 ResourceType type = StringToResourceType(type_str);
220 return GetLabel(type, id);
221}
222
224 const std::string& label) {
225 if (!project_labels_) {
226 return;
227 }
228 std::string type_str = ResourceTypeToString(type);
229 (*project_labels_)[type_str][std::to_string(id)] = label;
230}
231
233 if (!project_labels_) {
234 return;
235 }
236 std::string type_str = ResourceTypeToString(type);
237 auto type_it = project_labels_->find(type_str);
238 if (type_it != project_labels_->end()) {
239 type_it->second.erase(std::to_string(id));
240 }
241}
242
244 if (!project_labels_) {
245 return false;
246 }
247 std::string type_str = ResourceTypeToString(type);
248 auto type_it = project_labels_->find(type_str);
249 if (type_it == project_labels_->end()) {
250 return false;
251 }
252 return !LookupProjectLabel(type_it->second, id).empty();
253}
254
256 int id) const {
257 switch (type) {
259 const auto& names = Zelda3Labels::GetSpriteNames();
260 if (id >= 0 && static_cast<size_t>(id) < names.size()) {
261 return names[id];
262 }
263 return absl::StrFormat("Sprite %02X", id);
264 }
265 case ResourceType::kRoom: {
266 const auto& names = Zelda3Labels::GetRoomNames();
267 if (id >= 0 && static_cast<size_t>(id) < names.size()) {
268 return names[id];
269 }
270 return absl::StrFormat("Room %03X", id);
271 }
273 const auto& names = Zelda3Labels::GetEntranceNames();
274 if (id >= 0 && static_cast<size_t>(id) < names.size()) {
275 return names[id];
276 }
277 return absl::StrFormat("Entrance %02X", id);
278 }
279 case ResourceType::kItem: {
280 const auto& names = Zelda3Labels::GetItemNames();
281 if (id >= 0 && static_cast<size_t>(id) < names.size()) {
282 return names[id];
283 }
284 return absl::StrFormat("Item %02X", id);
285 }
287 const auto& names = Zelda3Labels::GetOverlordNames();
288 if (id >= 0 && static_cast<size_t>(id) < names.size()) {
289 return names[id];
290 }
291 return absl::StrFormat("Overlord %02X", id);
292 }
294 const auto& names = Zelda3Labels::GetOverworldMapNames();
295 if (id >= 0 && static_cast<size_t>(id) < names.size()) {
296 return names[id];
297 }
298 return absl::StrFormat("Map %02X", id);
299 }
301 const auto& names = Zelda3Labels::GetMusicTrackNames();
302 if (id >= 0 && static_cast<size_t>(id) < names.size()) {
303 return names[id];
304 }
305 return absl::StrFormat("Music %02X", id);
306 }
308 const auto& names = Zelda3Labels::GetGraphicsSheetNames();
309 if (id >= 0 && static_cast<size_t>(id) < names.size()) {
310 return names[id];
311 }
312 return absl::StrFormat("GFX %02X", id);
313 }
315 const auto& names = Zelda3Labels::GetRoomEffectNames();
316 if (id >= 0 && static_cast<size_t>(id) < names.size()) {
317 return names[id];
318 }
319 return absl::StrFormat("Effect %02X", id);
320 }
322 const auto& names = Zelda3Labels::GetRoomTagNames();
323 if (id >= 0 && static_cast<size_t>(id) < names.size()) {
324 return names[id];
325 }
326 return absl::StrFormat("Tag %02X", id);
327 }
329 const auto& names = Zelda3Labels::GetTileTypeNames();
330 if (id >= 0 && static_cast<size_t>(id) < names.size()) {
331 return names[id];
332 }
333 return absl::StrFormat("Tile %02X", id);
334 }
335 }
336 return absl::StrFormat("Unknown %02X", id);
337}
338
340 int id) const {
341 // Hyrule Magic names are only available for sprites
342 if (type != ResourceType::kSprite) {
343 return "";
344 }
345
346 // Use the kSpriteNames array from sprite_names.h
347 if (id >= 0 && static_cast<size_t>(id) < kSpriteNameCount) {
348 return kSpriteNames[id];
349 }
350 return "";
351}
352
354 switch (type) {
356 return 256;
358 return 297;
360 return 133;
362 return static_cast<int>(Zelda3Labels::GetItemNames().size());
364 return static_cast<int>(Zelda3Labels::GetOverlordNames().size());
366 return 160;
368 return static_cast<int>(Zelda3Labels::GetMusicTrackNames().size());
370 return static_cast<int>(Zelda3Labels::GetGraphicsSheetNames().size());
372 return static_cast<int>(Zelda3Labels::GetRoomEffectNames().size());
374 return static_cast<int>(Zelda3Labels::GetRoomTagNames().size());
376 return static_cast<int>(Zelda3Labels::GetTileTypeNames().size());
377 }
378 return 0;
379}
380
383 if (!project_labels_) {
384 return nullptr;
385 }
386 std::string type_str = ResourceTypeToString(type);
387 auto it = project_labels_->find(type_str);
388 if (it == project_labels_->end()) {
389 return nullptr;
390 }
391 return &it->second;
392}
393
399
400// ============================================================================
401// ZScream DefaultNames.txt Import/Export
402// ============================================================================
403
405 const std::string& content) {
406 if (!project_labels_) {
407 return absl::FailedPreconditionError(
408 "Project labels not initialized. Open a project first.");
409 }
410
411 std::istringstream stream(content);
412 std::string line;
413 std::string current_section;
414 int line_index = 0;
415 int line_number = 0;
416
417 while (std::getline(stream, line)) {
418 line_number++;
419
420 // Trim whitespace
421 std::string trimmed = std::string(absl::StripAsciiWhitespace(line));
422
423 // Skip empty lines and comments
424 if (trimmed.empty() || trimmed.substr(0, 2) == "//") {
425 continue;
426 }
427
428 // Check for section headers
429 if (trimmed.front() == '[' && trimmed.back() == ']') {
430 current_section = trimmed.substr(1, trimmed.length() - 2);
431 line_index = 0; // Reset line index for new section
432 continue;
433 }
434
435 // Parse the line based on current section
436 if (!current_section.empty()) {
437 ParseZScreamLine(trimmed, current_section, line_index);
438 }
439 }
440
441 return absl::OkStatus();
442}
443
444bool ResourceLabelProvider::ParseZScreamLine(const std::string& line,
445 const std::string& section,
446 int& line_index) {
447 if (!project_labels_) {
448 return false;
449 }
450
451 // Determine resource type and format based on section
452 std::string resource_type;
453 bool has_hex_prefix = false;
454
455 if (section == "Sprites Names") {
456 resource_type = "sprite";
457 has_hex_prefix = true; // Format: "00 Raven"
458 } else if (section == "Rooms Names") {
459 resource_type = "room";
460 has_hex_prefix = false; // Format: just "Room Name" by line order
461 } else if (section == "Chests Items") {
462 resource_type = "item_receipt";
463 has_hex_prefix = true; // Format: "00 Fighter sword and shield"
464 } else if (section == "Tags Names") {
465 resource_type = "room_tag";
466 has_hex_prefix = false; // Format: just "Tag Name" by line order
467 } else {
468 // Unknown section, skip
469 return false;
470 }
471
472 std::string id_str;
473 std::string label;
474
475 if (has_hex_prefix) {
476 // Format: "XX Label Name" where XX is hex
477 size_t space_pos = line.find(' ');
478 if (space_pos == std::string::npos || space_pos < 2) {
479 // No space found or too short for hex prefix
480 return false;
481 }
482
483 std::string hex_str = line.substr(0, space_pos);
484 label = line.substr(space_pos + 1);
485
486 // Parse hex to int
487 try {
488 int id = std::stoi(hex_str, nullptr, 16);
489 id_str = std::to_string(id);
490 } catch (...) {
491 return false;
492 }
493 } else {
494 // Line index determines the ID
495 id_str = std::to_string(line_index);
496 label = line;
497 line_index++;
498 }
499
500 // Trim the label
501 label = std::string(absl::StripAsciiWhitespace(label));
502
503 // Store the label
504 if (!label.empty()) {
505 (*project_labels_)[resource_type][id_str] = label;
506 }
507
508 return true;
509}
510
512 std::ostringstream output;
513
514 output << "//Do not use brackets [] in naming\n";
515
516 // Export sprites
517 output << "[Sprites Names]\n";
518 for (int i = 0; i < 256; ++i) {
519 std::string label = GetLabel(ResourceType::kSprite, i);
520 output << absl::StrFormat("%02X %s\n", i, label);
521 }
522
523 // Export rooms
524 output << "\n[Rooms Names]\n";
525 for (int i = 0; i < 297; ++i) {
526 std::string label = GetLabel(ResourceType::kRoom, i);
527 output << label << "\n";
528 }
529
530 // ZScream chest IDs index the receipt table, not the generic item catalog.
531 output << "\n[Chests Items]\n";
532 for (int i = 0; i <= 0xFF; ++i) {
533 const auto id = static_cast<uint8_t>(i);
534 if (i >= static_cast<int>(Zelda3Labels::GetItemReceiptNames().size()) &&
535 LookupProjectReceiptLabel(id, project_labels_).empty()) {
536 continue;
537 }
538 output << absl::StrFormat("%02X %s\n", i,
540 }
541
542 // Export room tags
543 output << "\n[Tags Names]\n";
545 for (int i = 0; i < tag_count; ++i) {
546 std::string label = GetLabel(ResourceType::kRoomTag, i);
547 output << label << "\n";
548 }
549
550 return output.str();
551}
552
553// ============================================================================
554// Oracle of Secrets Sprite Registry Import
555// ============================================================================
556
558 const std::string& csv_content) {
559 // Create local storage if project_labels_ not set
560 static ProjectLabels local_labels;
561 if (!project_labels_) {
562 project_labels_ = &local_labels;
563 }
564
565 std::istringstream stream(csv_content);
566 std::string line;
567 int line_number = 0;
568 int imported_count = 0;
569
570 while (std::getline(stream, line)) {
571 line_number++;
572
573 // Trim whitespace
574 std::string trimmed = std::string(absl::StripAsciiWhitespace(line));
575
576 // Skip empty lines and header row
577 if (trimmed.empty() || line_number == 1) {
578 continue;
579 }
580
581 // Parse CSV: name,id,paths,group,notes,allow_dupe
582 std::vector<std::string> fields = absl::StrSplit(trimmed, ',');
583 if (fields.size() < 2) {
584 continue; // Need at least name and id
585 }
586
587 std::string name = std::string(absl::StripAsciiWhitespace(fields[0]));
588 std::string id_str = std::string(absl::StripAsciiWhitespace(fields[1]));
589
590 // Parse sprite ID (format: $XX or 0xXX or just XX)
591 int sprite_id = -1;
592 if (id_str.empty()) {
593 continue;
594 }
595
596 // Handle $XX format
597 if (id_str[0] == '$') {
598 id_str = id_str.substr(1);
599 }
600 // Handle 0xXX format
601 if (id_str.size() >= 2 && id_str[0] == '0' &&
602 (id_str[1] == 'x' || id_str[1] == 'X')) {
603 id_str = id_str.substr(2);
604 }
605
606 try {
607 sprite_id = std::stoi(id_str, nullptr, 16);
608 } catch (...) {
609 continue; // Skip invalid IDs
610 }
611
612 if (sprite_id < 0 || sprite_id > 255) {
613 continue; // Invalid sprite ID range
614 }
615
616 // Store the sprite name
617 (*project_labels_)["sprite"][std::to_string(sprite_id)] = name;
618 imported_count++;
619 }
620
621 if (imported_count == 0) {
622 return absl::InvalidArgumentError(
623 "No valid sprite entries found in registry CSV");
624 }
625
626 return absl::OkStatus();
627}
628
630 uint8_t id, const ResourceLabelProvider::ProjectLabels* project_labels) {
631 if (const auto label = LookupProjectReceiptLabel(id, project_labels);
632 !label.empty()) {
633 return label;
634 }
635 const auto& names = Zelda3Labels::GetItemReceiptNames();
636 if (id < names.size())
637 return names[id];
638 return absl::StrFormat("Unknown item %02X", id);
639}
640
642 uint8_t id, const ResourceLabelProvider::ProjectLabels* project_labels) {
643 if (!LookupProjectReceiptLabel(id, project_labels).empty())
644 return "project";
645 return id < Zelda3Labels::GetItemReceiptNames().size() ? "vanilla_receipt"
646 : "unknown";
647}
648
649std::string GetItemReceiptLabel(uint8_t id) {
650 return GetItemReceiptLabel(id, GetResourceLabels().GetAllProjectLabels());
651}
652
653const char* GetItemReceiptLabelSource(uint8_t id) {
655 GetResourceLabels().GetAllProjectLabels());
656}
657
658} // namespace zelda3
659} // namespace yaze
Loads and queries the hack manifest JSON for yaze-ASM integration.
const ProjectRegistry & project_registry() const
bool HasProjectRegistry() const
std::string GetRoomTagLabel(uint8_t tag_id) const
Get the human-readable label for a room tag ID.
bool loaded() const
Check if the manifest has been loaded.
Unified interface for accessing resource labels with project overrides.
std::string ExportToZScreamFormat() const
Export project labels to ZScream DefaultNames.txt format.
void SetProjectLabel(ResourceType type, int id, const std::string &label)
Set a project-specific label override.
bool HasProjectLabel(ResourceType type, int id) const
Check if a project-specific label exists.
void ClearAllProjectLabels()
Clear all project labels.
int GetResourceCount(ResourceType type) const
Get the count of resources for a given type.
std::unordered_map< std::string, std::string > LabelMap
const core::HackManifest * hack_manifest_
std::string GetLabel(ResourceType type, int id) const
Get a label for a resource by type and ID.
bool ParseZScreamLine(const std::string &line, const std::string &section, int &line_index)
std::unordered_map< std::string, LabelMap > ProjectLabels
absl::Status ImportFromZScreamFormat(const std::string &content)
Import labels from ZScream DefaultNames.txt format.
std::string GetVanillaLabel(ResourceType type, int id) const
Get the vanilla (default) label for a resource.
const LabelMap * GetProjectLabelsForType(ResourceType type) const
Get all project labels for a given type.
std::string GetHMagicLabel(ResourceType type, int id) const
Get the Hyrule Magic label for a resource (sprites only)
void ClearProjectLabel(ResourceType type, int id)
Clear a project-specific label (revert to default)
absl::Status ImportOracleSpriteRegistry(const std::string &csv_content)
Import sprite labels from Oracle of Secrets registry.csv format.
std::string LookupProjectRegistryRoomLabel(const core::HackManifest *manifest, int id)
std::string LookupProjectReceiptLabel(uint8_t id, const ResourceLabelProvider::ProjectLabels *project_labels)
std::string LookupProjectLabel(const ResourceLabelProvider::LabelMap &labels, int id)
Zelda 3 specific classes and functions.
ResourceType
Enumeration of all supported resource types for labeling.
const char * GetItemReceiptLabelSource(uint8_t id, const ResourceLabelProvider::ProjectLabels *project_labels)
const size_t kSpriteNameCount
Definition sprite.cc:296
const char *const kSpriteNames[]
Definition sprite.cc:295
std::string GetItemReceiptLabel(uint8_t id, const ResourceLabelProvider::ProjectLabels *project_labels)
std::string ResourceTypeToString(ResourceType type)
Convert ResourceType enum to string key for storage.
ResourceType StringToResourceType(const std::string &type_str)
Convert string key to ResourceType enum.
ResourceLabelProvider & GetResourceLabels()
Get the global ResourceLabelProvider instance.
std::vector< DungeonEntry > dungeons
static const std::vector< std::string > & GetRoomNames()
static const std::vector< std::string > & GetItemNames()
static const std::vector< std::string > & GetEntranceNames()
static const std::vector< std::string > & GetGraphicsSheetNames()
static const std::vector< std::string > & GetMusicTrackNames()
static const std::vector< std::string > & GetItemReceiptNames()
static const std::vector< std::string > & GetTileTypeNames()
static const std::vector< std::string > & GetOverlordNames()
static const std::vector< std::string > & GetSpriteNames()
static const std::vector< std::string > & GetOverworldMapNames()
static const std::vector< std::string > & GetRoomEffectNames()
static const std::vector< std::string > & GetRoomTagNames()