10#include "absl/strings/str_format.h"
11#include "absl/strings/str_split.h"
12#include "absl/strings/strip.h"
42 return "overworld_map";
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")
88 auto lookup = [&](
const std::string& key) -> std::string {
89 auto it = labels.find(key);
90 if (it != labels.end() && !it->second.empty()) {
97 if (std::string decimal = lookup(std::to_string(
id)); !decimal.empty()) {
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),
111 for (
const auto& key : keys) {
112 if (std::string label = lookup(key); !label.empty()) {
144 if (std::string project_label = LookupProjectLabel(type_it->second,
id);
145 !project_label.empty()) {
146 return project_label;
155 if (!manifest_label.empty()) {
156 return manifest_label;
165 if (!hmagic.empty()) {
181 const std::string& label) {
186 (*project_labels_)[type_str][std::to_string(
id)] = label;
196 type_it->second.erase(std::to_string(
id));
209 return !LookupProjectLabel(type_it->second,
id).empty();
217 if (
id >= 0 &&
static_cast<size_t>(
id) < names.size()) {
220 return absl::StrFormat(
"Sprite %02X",
id);
224 if (
id >= 0 &&
static_cast<size_t>(
id) < names.size()) {
227 return absl::StrFormat(
"Room %03X",
id);
231 if (
id >= 0 &&
static_cast<size_t>(
id) < names.size()) {
234 return absl::StrFormat(
"Entrance %02X",
id);
238 if (
id >= 0 &&
static_cast<size_t>(
id) < names.size()) {
241 return absl::StrFormat(
"Item %02X",
id);
245 if (
id >= 0 &&
static_cast<size_t>(
id) < names.size()) {
248 return absl::StrFormat(
"Overlord %02X",
id);
252 if (
id >= 0 &&
static_cast<size_t>(
id) < names.size()) {
255 return absl::StrFormat(
"Map %02X",
id);
259 if (
id >= 0 &&
static_cast<size_t>(
id) < names.size()) {
262 return absl::StrFormat(
"Music %02X",
id);
266 if (
id >= 0 &&
static_cast<size_t>(
id) < names.size()) {
269 return absl::StrFormat(
"GFX %02X",
id);
273 if (
id >= 0 &&
static_cast<size_t>(
id) < names.size()) {
276 return absl::StrFormat(
"Effect %02X",
id);
280 if (
id >= 0 &&
static_cast<size_t>(
id) < names.size()) {
283 return absl::StrFormat(
"Tag %02X",
id);
287 if (
id >= 0 &&
static_cast<size_t>(
id) < names.size()) {
290 return absl::StrFormat(
"Tile %02X",
id);
293 return absl::StrFormat(
"Unknown %02X",
id);
362 const std::string& content) {
364 return absl::FailedPreconditionError(
365 "Project labels not initialized. Open a project first.");
368 std::istringstream stream(content);
370 std::string current_section;
374 while (std::getline(stream, line)) {
378 std::string trimmed = std::string(absl::StripAsciiWhitespace(line));
381 if (trimmed.empty() || trimmed.substr(0, 2) ==
"//") {
386 if (trimmed.front() ==
'[' && trimmed.back() ==
']') {
387 current_section = trimmed.substr(1, trimmed.length() - 2);
393 if (!current_section.empty()) {
398 return absl::OkStatus();
402 const std::string& section,
409 std::string resource_type;
410 bool has_hex_prefix =
false;
412 if (section ==
"Sprites Names") {
413 resource_type =
"sprite";
414 has_hex_prefix =
true;
415 }
else if (section ==
"Rooms Names") {
416 resource_type =
"room";
417 has_hex_prefix =
false;
418 }
else if (section ==
"Chests Items") {
419 resource_type =
"item";
420 has_hex_prefix =
true;
421 }
else if (section ==
"Tags Names") {
422 resource_type =
"room_tag";
423 has_hex_prefix =
false;
432 if (has_hex_prefix) {
434 size_t space_pos = line.find(
' ');
435 if (space_pos == std::string::npos || space_pos < 2) {
440 std::string hex_str = line.substr(0, space_pos);
441 label = line.substr(space_pos + 1);
445 int id = std::stoi(hex_str,
nullptr, 16);
446 id_str = std::to_string(
id);
452 id_str = std::to_string(line_index);
458 label = std::string(absl::StripAsciiWhitespace(label));
461 if (!label.empty()) {
462 (*project_labels_)[resource_type][id_str] = label;
469 std::ostringstream output;
471 output <<
"//Do not use brackets [] in naming\n";
474 output <<
"[Sprites Names]\n";
475 for (
int i = 0; i < 256; ++i) {
477 output << absl::StrFormat(
"%02X %s\n", i, label);
481 output <<
"\n[Rooms Names]\n";
482 for (
int i = 0; i < 297; ++i) {
484 output << label <<
"\n";
488 output <<
"\n[Chests Items]\n";
490 for (
int i = 0; i < item_count; ++i) {
492 output << absl::StrFormat(
"%02X %s\n", i, label);
496 output <<
"\n[Tags Names]\n";
498 for (
int i = 0; i < tag_count; ++i) {
500 output << label <<
"\n";
511 const std::string& csv_content) {
518 std::istringstream stream(csv_content);
521 int imported_count = 0;
523 while (std::getline(stream, line)) {
527 std::string trimmed = std::string(absl::StripAsciiWhitespace(line));
530 if (trimmed.empty() || line_number == 1) {
535 std::vector<std::string> fields = absl::StrSplit(trimmed,
',');
536 if (fields.size() < 2) {
540 std::string
name = std::string(absl::StripAsciiWhitespace(fields[0]));
541 std::string id_str = std::string(absl::StripAsciiWhitespace(fields[1]));
545 if (id_str.empty()) {
550 if (id_str[0] ==
'$') {
551 id_str = id_str.substr(1);
554 if (id_str.size() >= 2 && id_str[0] ==
'0' &&
555 (id_str[1] ==
'x' || id_str[1] ==
'X')) {
556 id_str = id_str.substr(2);
560 sprite_id = std::stoi(id_str,
nullptr, 16);
565 if (sprite_id < 0 || sprite_id > 255) {
570 (*project_labels_)[
"sprite"][std::to_string(sprite_id)] =
name;
574 if (imported_count == 0) {
575 return absl::InvalidArgumentError(
576 "No valid sprite entries found in registry CSV");
579 return absl::OkStatus();
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_
ProjectLabels * project_labels_
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 §ion, 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 LookupProjectLabel(const ResourceLabelProvider::LabelMap &labels, int id)
Zelda 3 specific classes and functions.
ResourceType
Enumeration of all supported resource types for labeling.
const size_t kSpriteNameCount
const char *const kSpriteNames[]
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.
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 > & 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()