113 static Companion* Instance;
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;
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;
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) {}
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) {}
141 void Init(ExportType type);
143 bool NodeHasChanges(
const std::string&
string);
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; }
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()); }
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; }
162 std::optional<ParseResultData> GetParseDataByAddr(uint32_t addr);
163 std::optional<ParseResultData> GetParseDataBySymbol(
const std::string& symbol);
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);
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);
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; }
191 BinaryWrapper* GetCurrentWrapper() {
return this->gCurrentWrapper; }
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);
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;
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;
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;
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;
239 void ProcessFile(YAML::Node root);
240 void ParseEnums(std::string& file);
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);