Torch
Loading...
Searching...
No Matches
BaseFactory.h
1#pragma once
2
3#include "ResourceType.h"
4#include "n64/Cartridge.h"
5#include <cstdint>
6#include <iostream>
7#include <any>
8#include <memory>
9#include <vector>
10#include <string>
11#include <variant>
12#include <optional>
13#include <yaml-cpp/yaml.h>
14#include <filesystem>
15#include <strhash64/StrHash64.h>
16#include "lib/binarytools/BinaryWriter.h"
17#include "lib/binarytools/BinaryReader.h"
18
19#ifdef BUILD_UI
20#define IMGUI_DEFINE_MATH_OPERATORS
21#include "imgui.h"
22#endif
23
24namespace fs = std::filesystem;
25
26struct ParseResultData;
27
28#define REGISTER(type, c) { ExportType::type, std::make_shared<c>() },
29
30#define SEGMENT_OFFSET(a) ((uint32_t)(a) & 0x00FFFFFF)
31#define SEGMENT_NUMBER(x) (((uint32_t)(x) >> 24) & 0xFF)
32// I would love to use 0x01000000, but the stupid compiler takes it as 0x01
33#define IS_SEGMENTED(x) ((SEGMENT_NUMBER(x) > 0) && (SEGMENT_NUMBER(x) < 0x20))
34#define IS_VIRTUAL_SEGMENT(x) (SEGMENT_NUMBER(x) >= 0x80)
35#define ASSET_PTR(x) ((IS_SEGMENTED(x) || IS_VIRTUAL_SEGMENT(x)) ? SEGMENT_OFFSET(x) : (x))
36
37#define tab_t "\t"
38#define fourSpaceTab " "
39
41 uint32_t start;
42 uint32_t end;
43};
44
45typedef std::optional<std::variant<size_t, OffsetEntry>> ExportResult;
46
47enum class ExportType {
48 Header,
49 Code,
50 Binary,
51 Modding,
52 XML
53};
54
55template<typename T>
56std::optional<T> GetNode(YAML::Node& node, const std::string& key) {
57 if(!node[key]) {
58 return std::nullopt;
59 }
60
61 return std::optional<T>(node[key].as<T>());
62}
63
64template<typename T>
65T GetSafeNode(YAML::Node& node, const std::string& key) {
66 if(!node[key]) {
67 auto dump = YAML::Dump(node);
68
69 if (node["symbol"]) {
70 throw std::runtime_error("Yaml asset missing the '" + key + "' node for '" + node["symbol"].as<std::string>() + "'\nProblematic YAML:\n" + dump);
71 } else {
72 throw std::runtime_error("Yaml asset missing the '" + key + "' node\nProblematic YAML:\n" + dump);
73 }
74 }
75
76 return node[key].as<T>();
77}
78
79template<typename T>
80T GetSafeNode(YAML::Node& node, const std::string& key, const T& def) {
81 if(!node[key]) {
82 return def;
83 }
84
85 return node[key].as<T>();
86}
87
89
90template<typename T>
91class GenericData : public IParsedData {
92public:
93 T mData;
94};
95
97public:
98 virtual ExportResult Export(std::ostream& write, std::shared_ptr<IParsedData> data, std::string& entryName, YAML::Node& node, std::string* replacement) = 0;
99 static void WriteHeader(LUS::BinaryWriter& write, Torch::ResourceType resType, int32_t version);
100};
101
103public:
104 virtual std::optional<std::shared_ptr<IParsedData>> parse(std::vector<uint8_t>& buffer, YAML::Node& data) = 0;
105 virtual std::optional<std::shared_ptr<IParsedData>> parse_modding(std::vector<uint8_t>& buffer, YAML::Node& data) {
106 return std::nullopt;
107 }
108 std::optional<std::shared_ptr<BaseExporter>> GetExporter(ExportType type) {
109 auto exporters = this->GetExporters();
110 if (exporters.find(type) != exporters.end()) {
111 return exporters[type];
112 }
113 return std::nullopt;
114 }
115 virtual bool SupportModdedAssets() {
116 return false;
117 }
118 virtual bool HasModdedDependencies() {
119 return false;
120 }
121 virtual uint32_t GetAlignment() {
122 return 4;
123 }
124 virtual bool IsDialogPackRoot() const {
125 return false;
126 }
127 virtual void PreprocessConfig(YAML::Node& cfg, N64::Cartridge* cart) {}
128 virtual std::optional<std::shared_ptr<IParsedData>> CreateDataPointer() {
129 return std::nullopt;
130 }
131 // When true, the viewer's default preview renders this factory's Code
132 // exporter output as read-only text. Only enable for exporters that just
133 // format to the stream (no file writes or other side effects).
134 virtual bool CanPreviewCode() {
135 return false;
136 }
137private:
138 virtual std::unordered_map<ExportType, std::shared_ptr<BaseExporter>> GetExporters() {
139 return {};
140 }
141};
142
143#ifdef BUILD_UI
144// Base preview renderer. The default implementation (see BaseFactory.cpp) shows
145// the asset's Code-exporter text when the factory opts in via CanPreviewCode(),
146// otherwise its YAML config. Subclasses override DrawUI for richer visuals.
147class BaseFactoryUI {
148public:
149 virtual ~BaseFactoryUI() = default;
150
151 virtual float GetItemHeight(const ParseResultData& data);
152 virtual void DrawUI(const ParseResultData& data);
153};
154#endif
Definition BaseFactory.h:96
Definition BaseFactory.h:102
Definition BaseFactory.h:91
Definition BaseFactory.h:88
Definition Cartridge.h:15
Definition BaseFactory.h:40
Definition Companion.h:123