Torch
Loading...
Searching...
No Matches
Companion.h
1#pragma once
2
3#include <string>
4#include <optional>
5#include <filesystem>
6#include <vector>
7#include <fstream>
8#include <unordered_map>
9#include <unordered_set>
10#include <variant>
11#include "factories/BaseFactory.h"
12#include "n64/Cartridge.h"
13#include "utils/Decompressor.h"
14#include "factories/TextureFactory.h"
15
16class BinaryWrapper;
17namespace fs = std::filesystem;
18
19enum class ParseMode {
20 Default,
21 Directory
22};
23
24enum class GBIVersion {
25 f3db,
26 f3d,
27 f3dex,
28 f3dexb,
29 f3dex2,
30};
31
32enum class GBIMinorVersion {
33 None,
34 Mk64,
35 SM64
36};
37
38enum class TableMode {
39 Reference,
40 Append
41};
42
43enum class ArchiveType {
44 None,
45 OTR,
46 O2R,
47};
48
50 std::unordered_map<uint32_t, uint32_t> global;
51 std::unordered_map<uint32_t, uint32_t> local;
52 std::unordered_map<uint32_t, uint32_t> temporal;
53};
54
55struct Table {
56 std::string name;
57 uint32_t start;
58 uint32_t end;
59 TableMode mode;
60 int32_t index_size;
61};
62
63struct VRAMEntry {
64 uint32_t addr;
65 uint32_t offset;
66};
67
68struct WriteEntry {
69 std::string name;
70 uint32_t addr;
71 uint32_t alignment;
72 std::string buffer;
73 std::optional<std::string> comment;
74 std::optional<uint32_t> endptr;
75};
76
77struct GBIConfig {
78 GBIVersion version = GBIVersion::f3d;
79 GBIMinorVersion subversion = GBIMinorVersion::None;
80 bool useFloats = false;
81};
82
84 GBIConfig gbi;
85 SegmentConfig segment;
86 std::string outputPath;
87 std::string moddingPath;
88 ExportType exporterType;
89 ParseMode parseMode;
90 ArchiveType otrMode;
91 bool debug;
92 bool modding;
93 bool textureDefines;
94};
95
97 std::string name;
98 std::string type;
99 YAML::Node node;
100 std::optional<std::shared_ptr<IParsedData>> data;
101
102 uint32_t GetOffset() {
103 return GetSafeNode<uint32_t>(node, "offset");
104 }
105
106 std::optional<std::string> GetSymbol() {
107 return GetSafeNode<std::string>(node, "symbol");
108 }
109};
110
111class Companion {
112public:
113 static Companion* Instance;
114
115 explicit Companion(std::filesystem::path rom, const ArchiveType otr, const bool debug, const bool modding = false,
116 const std::string& srcDir = "", const std::string& destPath = "") : gCartridge(nullptr),
117 gSourceDirectory(srcDir), gDestinationDirectory(destPath) {
118 this->gRomPath = rom;
119 this->gConfig.otrMode = otr;
120 this->gConfig.debug = debug;
121 this->gConfig.modding = modding;
122 this->gConfig.textureDefines = false;
123 }
124
125 explicit Companion(std::vector<uint8_t> rom, const ArchiveType otr, const bool debug, const bool modding = false,
126 const std::string& srcDir = "", const std::string& destPath = "") : gCartridge(nullptr),
127 gSourceDirectory(srcDir), gDestinationDirectory(destPath) {
128 this->gRomData = rom;
129 this->gConfig.otrMode = otr;
130 this->gConfig.debug = debug;
131 this->gConfig.modding = modding;
132 this->gConfig.textureDefines = false;
133 }
134
135 explicit Companion(std::filesystem::path rom, const ArchiveType otr, const bool debug, const std::string& srcDir = "", const std::string& destPath = "") :
136 Companion(rom, otr, debug, false, srcDir, destPath) {}
137
138 explicit Companion(std::vector<uint8_t> rom, const ArchiveType otr, const bool debug, const std::string& srcDir = "", const std::string& destPath = "") :
139 Companion(rom, otr, debug, false, srcDir, destPath) {}
140
141 void Init(ExportType type);
142
143 bool NodeHasChanges(const std::string& string);
144
145 void Process();
146
147 bool IsOTRMode() const { return (this->gConfig.otrMode != ArchiveType::None); }
148 bool IsDebug() const { return this->gConfig.debug; }
149 bool AddTextureDefines() const { return this->gConfig.textureDefines; }
150
151 N64::Cartridge* GetCartridge() const { return this->gCartridge.get(); }
152 std::vector<uint8_t>& GetRomData() { return this->gRomData; }
153 std::string GetOutputPath() { return this->gConfig.outputPath; }
154 std::string GetDestRelativeOutputPath() { return RelativePathToDestDir(GetOutputPath()); }
155
156 GBIVersion GetGBIVersion() const { return this->gConfig.gbi.version; }
157 GBIMinorVersion GetGBIMinorVersion() const { return this->gConfig.gbi.subversion; }
158 std::unordered_map<std::string, std::vector<YAML::Node>> GetCourseMetadata() { return this->gCourseMetadata; }
159 std::optional<std::string> GetEnumFromValue(const std::string& key, int id);
160 bool IsUsingIndividualIncludes() const { return this->gIndividualIncludes; }
161
162 std::optional<ParseResultData> GetParseDataByAddr(uint32_t addr);
163 std::optional<ParseResultData> GetParseDataBySymbol(const std::string& symbol);
164
165 std::optional<std::uint32_t> GetFileOffsetFromSegmentedAddr(uint8_t segment) const;
166 std::optional<std::shared_ptr<BaseFactory>> GetFactory(const std::string& type);
167 uint32_t PatchVirtualAddr(uint32_t addr);
168 std::optional<std::tuple<std::string, YAML::Node>> GetNodeByAddr(uint32_t addr);
169 std::optional<std::string> GetStringByAddr(uint32_t addr);
170 std::optional<std::tuple<std::string, YAML::Node>> GetSafeNodeByAddr(const uint32_t addr, std::string type);
171 std::optional<std::string> GetSafeStringByAddr(const uint32_t addr, std::string type);
172 std::optional<std::vector<std::tuple<std::string, YAML::Node>>> GetNodesByType(const std::string& type);
173 std::string GetSymbolFromAddr(uint32_t addr, bool validZero = false);
174
175 std::optional<std::uint32_t> GetFileOffset(void) const { return this->gCurrentFileOffset; };
176 std::optional<std::uint32_t> GetCurrSegmentNumber(void) const { return this->gCurrentSegmentNumber; };
177 CompressionType GetCurrCompressionType(void) const { return this->gCurrentCompressionType; };
178 std::optional<VRAMEntry> GetCurrentVRAM(void) const { return this->gCurrentVram; };
179 std::optional<Table> SearchTable(uint32_t addr);
180
181 static std::string CalculateHash(const std::vector<uint8_t>& data);
182 static void Pack(const std::string& folder, const std::string& output, const ArchiveType otrMode);
183 std::string NormalizeAsset(const std::string& name) const;
184 std::string RelativePath(const std::string& path) const;
185 std::string RelativePathToSrcDir(const std::string& path) const;
186 std::string RelativePathToDestDir(const std::string& path) const;
187 void RegisterCompanionFile(const std::string path, std::vector<char> data);
188 void SetAdditionalFiles(const std::vector<std::string>& files) { this->gAdditionalFiles = files; }
189
190 TorchConfig& GetConfig() { return this->gConfig; }
191 BinaryWrapper* GetCurrentWrapper() { return this->gCurrentWrapper; }
192
193 std::optional<std::tuple<std::string, YAML::Node>> RegisterAsset(const std::string& name, YAML::Node& node);
194 std::optional<YAML::Node> AddAsset(YAML::Node asset);
195private:
196 TorchConfig gConfig;
197 YAML::Node gModdingConfig;
198 fs::path gSourceDirectory;
199 fs::path gDestinationDirectory;
200 fs::path gCurrentDirectory;
201 std::string gCurrentHash;
202 std::string gAssetPath;
203 std::vector<uint8_t> gRomData;
204 std::optional<std::filesystem::path> gRomPath;
205 bool gNodeForceProcessing = false;
206 bool gIndividualIncludes = false;
207 YAML::Node gHashNode;
208 std::shared_ptr<N64::Cartridge> gCartridge;
209 std::unordered_map<std::string, std::vector<YAML::Node>> gCourseMetadata;
210 std::unordered_map<std::string, std::unordered_map<int32_t, std::string>> gEnums;
211 BinaryWrapper* gCurrentWrapper;
212
213 // Temporal Variables
214 std::string gCurrentFile;
215 std::string gCurrentVirtualPath;
216 std::string gFileHeader;
217 bool gEnablePadGen = false;
218 uint32_t gCurrentPad = 0;
219 uint32_t gCurrentFileOffset;
220 uint32_t gCurrentSegmentNumber;
221 std::optional<VRAMEntry> gCurrentVram;
222 CompressionType gCurrentCompressionType = CompressionType::None;
223 std::vector<Table> gTables;
224 std::vector<std::string> gCurrentExternalFiles;
225 std::unordered_map<int, std::string> gManualSegments;
226 std::unordered_set<std::string> gProcessedFiles;
227
228 std::unordered_map<std::string, std::vector<char>> gCompanionFiles;
229 std::unordered_map<std::string, std::vector<ParseResultData>> gParseResults;
230 std::vector<std::string> gAdditionalFiles;
231
232 std::unordered_map<std::string, std::string> gModdedAssetPaths;
233 std::variant<std::vector<std::string>, std::string> gWriteOrder;
234 std::unordered_map<std::string, std::shared_ptr<BaseFactory>> gFactories;
235 std::unordered_map<std::string, std::map<std::string, std::vector<WriteEntry>>> gWriteMap;
236 std::unordered_map<std::string, std::tuple<uint32_t, uint32_t>> gVirtualAddrMap;
237 std::unordered_map<std::string, std::unordered_map<uint32_t, std::tuple<std::string, YAML::Node>>> gAddrMap;
238
239 void ProcessFile(YAML::Node root);
240 void ParseEnums(std::string& file);
241 void ParseHash();
242 void ParseModdingConfig();
243 void ParseCurrentFileConfig(YAML::Node node);
244 void RegisterFactory(const std::string& type, const std::shared_ptr<BaseFactory>& factory);
245 void ExtractNode(YAML::Node& node, std::string& name, BinaryWrapper* binary);
246 void ProcessTables(YAML::Node& rom);
247 void LoadYAMLRecursively(const std::string &dirPath, std::vector<YAML::Node> &result, bool skipRoot);
248 std::optional<ParseResultData> ParseNode(YAML::Node& node, std::string& name);
249};
Definition BinaryWrapper.h:7
Definition Cartridge.h:15
Definition Companion.h:77
Definition Companion.h:96
Definition Companion.h:49
Definition Companion.h:55
Definition Companion.h:83
Definition Companion.h:63
Definition Companion.h:68