Rewrite asset processor in C++

This commit is contained in:
octorock
2021-11-06 00:37:37 +01:00
parent a6d133e42d
commit 8d6c3fe541
35 changed files with 27666 additions and 40 deletions
+7
View File
@@ -0,0 +1,7 @@
#include "asset.h"
class AifAsset : public BaseAsset {
public:
using BaseAsset::BaseAsset;
virtual void convertToHumanReadable(const std::vector<char>& baserom);
};
@@ -0,0 +1,7 @@
#include "asset.h"
class AnimationAsset : public BaseAsset {
public:
using BaseAsset::BaseAsset;
virtual void convertToHumanReadable(const std::vector<char>& baserom);
};
+34
View File
@@ -0,0 +1,34 @@
#ifndef ASSET_H
#define ASSET_H
#include <assert.h>
#include <string>
#include <filesystem>
#include <json.hpp>
#include "util.h"
class BaseAsset {
public:
BaseAsset(std::filesystem::path path, int start, int size, nlohmann::json asset)
: path(path), start(start), size(size), asset(asset) {
binaryPath = path;//.replace_extension("bin");
}
virtual void extractBinary(const std::vector<char>& baserom);
virtual void convertToHumanReadable(const std::vector<char>& baserom) {
(void)baserom;
assert("not implemented");
}
virtual void convertToBinary() {
assert("not implemented");
}
protected:
std::filesystem::path path;
std::filesystem::path binaryPath;
int start;
int size;
nlohmann::json asset;
};
#endif
+7
View File
@@ -0,0 +1,7 @@
#include "asset.h"
class ExitListAsset : public BaseAsset {
public:
using BaseAsset::BaseAsset;
virtual void convertToHumanReadable(const std::vector<char>& baserom);
};
@@ -0,0 +1,7 @@
#include "asset.h"
class FrameObjListsAsset : public BaseAsset {
public:
using BaseAsset::BaseAsset;
virtual void convertToHumanReadable(const std::vector<char>& baserom);
};
+7
View File
@@ -0,0 +1,7 @@
#include "asset.h"
class GfxAsset : public BaseAsset {
public:
using BaseAsset::BaseAsset;
virtual void convertToHumanReadable(const std::vector<char>& baserom);
};
File diff suppressed because it is too large Load Diff
+21
View File
@@ -0,0 +1,21 @@
#ifndef MAIN_H
#define MAIN_H
#include "util.h"
bool shouldExtractFile(const std::filesystem::path& path, const std::filesystem::file_time_type& configModified);
void extractFile(const std::filesystem::path& path, const nlohmann::json& asset, const int& start, const std::vector<char>& baserom);
enum Mode {
EXTRACT,
CONVERT,
BUILD
};
// Arguments
extern bool gVerbose;
extern Mode gMode;
extern std::string gVariant;
extern std::string gAssetsFolder;
extern std::string gBaseromPath;
#endif
+7
View File
@@ -0,0 +1,7 @@
#include "asset.h"
class MidiAsset : public BaseAsset {
public:
using BaseAsset::BaseAsset;
virtual void convertToHumanReadable(const std::vector<char>& baserom);
};
+7
View File
@@ -0,0 +1,7 @@
#include "asset.h"
class PaletteAsset : public BaseAsset {
public:
using BaseAsset::BaseAsset;
virtual void convertToHumanReadable(const std::vector<char>& baserom);
};
+69
View File
@@ -0,0 +1,69 @@
#ifndef READER_H
#define READER_H
#include <stdint.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
#include <vector>
class Reader {
public:
Reader(const std::vector<char>& baserom, int start, int size) {
auto first = baserom.begin() + start;
auto last = baserom.begin() + start + size;
data = std::vector<char>(first, last);
}
u8 read_u8() {
return this->data[this->cursor++];
}
s8 read_s8() {
return (s8)this->read_u8();
}
u16 read_u16() {
u16 val = (u8)this->data[this->cursor] + (((u8)this->data[this->cursor + 1]) << 8);
this->cursor += 2;
return val;
}
u32 read_u32() {
u32 val = ((u8)this->data[this->cursor]) + (((u8)this->data[this->cursor + 1]) << 8) +
(((u8)this->data[this->cursor + 2]) << 16) + (((u8)this->data[this->cursor + 3]) << 24);
this->cursor += 4;
return val;
}
int cursor = 0;
private:
std::vector<char> data;
};
// TODO move to utils?
#include <memory>
#include <string>
#include <stdexcept>
template <typename... Args> std::string string_format(const std::string& format, Args... args) {
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
if (size_s <= 0) {
throw std::runtime_error("Error during formatting.");
}
auto size = static_cast<size_t>(size_s);
auto buf = std::make_unique<char[]>(size);
std::snprintf(buf.get(), size, format.c_str(), args...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
}
std::string opt_param(const std::string& format, int defaultVal, int value);
#endif
@@ -0,0 +1,7 @@
#include "asset.h"
class SpriteFrameAsset : public BaseAsset {
public:
using BaseAsset::BaseAsset;
virtual void convertToHumanReadable(const std::vector<char>& baserom);
};
+7
View File
@@ -0,0 +1,7 @@
#include "asset.h"
class TilesetAsset : public BaseAsset {
public:
using BaseAsset::BaseAsset;
virtual void convertToHumanReadable(const std::vector<char>& baserom);
};
+23
View File
@@ -0,0 +1,23 @@
#ifndef UTIL_H
#define UTIL_H
#include <stdint.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <json.hpp>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
void check_call(const std::vector<std::string>& cmd);
std::string to_string(const nlohmann::json& j);
#endif