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
19namespace fs = std::filesystem;
20
21#define REGISTER(type, c) { ExportType::type, std::make_shared<c>() },
22
23#define SEGMENT_OFFSET(a) ((uint32_t)(a) & 0x00FFFFFF)
24#define SEGMENT_NUMBER(x) (((uint32_t)(x) >> 24) & 0xFF)
25// I would love to use 0x01000000, but the stupid compiler takes it as 0x01
26#define IS_SEGMENTED(x) ((SEGMENT_NUMBER(x) > 0) && (SEGMENT_NUMBER(x) < 0x20))
27#define ASSET_PTR(x) (IS_SEGMENTED(x) ? SEGMENT_OFFSET(x) : (x))
28
29#define tab_t "\t"
30#define fourSpaceTab " "
31
33 uint32_t start;
34 uint32_t end;
35};
36
37typedef std::optional<std::variant<size_t, OffsetEntry>> ExportResult;
38
39enum class ExportType {
40 Header,
41 Code,
42 Binary,
43 Modding,
44 XML
45};
46
47template<typename T>
48std::optional<T> GetNode(YAML::Node& node, const std::string& key) {
49 if(!node[key]) {
50 return std::nullopt;
51 }
52
53 return std::optional<T>(node[key].as<T>());
54}
55
56template<typename T>
57T GetSafeNode(YAML::Node& node, const std::string& key) {
58 if(!node[key]) {
59 auto dump = YAML::Dump(node);
60
61 if (node["symbol"]) {
62 throw std::runtime_error("Yaml asset missing the '" + key + "' node for '" + node["symbol"].as<std::string>() + "'\nProblematic YAML:\n" + dump);
63 } else {
64 throw std::runtime_error("Yaml asset missing the '" + key + "' node\nProblematic YAML:\n" + dump);
65 }
66 }
67
68 return node[key].as<T>();
69}
70
71template<typename T>
72T GetSafeNode(YAML::Node& node, const std::string& key, const T& def) {
73 if(!node[key]) {
74 return def;
75 }
76
77 return node[key].as<T>();
78}
79
81
82template<typename T>
83class GenericData : public IParsedData {
84public:
85 T mData;
86};
87
89public:
90 virtual ExportResult Export(std::ostream& write, std::shared_ptr<IParsedData> data, std::string& entryName, YAML::Node& node, std::string* replacement) = 0;
91 static void WriteHeader(LUS::BinaryWriter& write, Torch::ResourceType resType, int32_t version);
92};
93
95public:
96 virtual std::optional<std::shared_ptr<IParsedData>> parse(std::vector<uint8_t>& buffer, YAML::Node& data) = 0;
97 virtual std::optional<std::shared_ptr<IParsedData>> parse_modding(std::vector<uint8_t>& buffer, YAML::Node& data) {
98 return std::nullopt;
99 }
100 std::optional<std::shared_ptr<BaseExporter>> GetExporter(ExportType type) {
101 auto exporters = this->GetExporters();
102 if (exporters.find(type) != exporters.end()) {
103 return exporters[type];
104 }
105 return std::nullopt;
106 }
107 virtual bool SupportModdedAssets() {
108 return false;
109 }
110 virtual bool HasModdedDependencies() {
111 return false;
112 }
113 virtual uint32_t GetAlignment() {
114 return 4;
115 }
116 virtual std::optional<std::shared_ptr<IParsedData>> CreateDataPointer() {
117 return std::nullopt;
118 }
119private:
120 virtual std::unordered_map<ExportType, std::shared_ptr<BaseExporter>> GetExporters() {
121 return {};
122 }
123};
Definition BaseFactory.h:88
Definition BaseFactory.h:94
Definition BaseFactory.h:83
Definition BaseFactory.h:80
Definition BaseFactory.h:32