From 794d5fc97cb01a70c8579dc08962b667edf5a29e Mon Sep 17 00:00:00 2001 From: Henny022p Date: Tue, 23 Nov 2021 20:22:22 +0100 Subject: [PATCH] cleaned asset_processor to build without warnings --- tools/src/asset_processor/CMakeLists.txt | 3 +- tools/src/asset_processor/assets/aif.cpp | 16 ++-- .../src/asset_processor/assets/animation.cpp | 23 +++--- tools/src/asset_processor/assets/asset.cpp | 8 +- tools/src/asset_processor/assets/asset.h | 31 ++++---- tools/src/asset_processor/assets/exitlist.cpp | 37 +++++----- .../asset_processor/assets/frameobjlists.cpp | 45 ++++++------ tools/src/asset_processor/assets/gfx.cpp | 18 ++--- tools/src/asset_processor/assets/macroasm.cpp | 12 +-- tools/src/asset_processor/assets/midi.cpp | 73 ++++++++++--------- tools/src/asset_processor/assets/palette.cpp | 16 ++-- .../asset_processor/assets/spriteframe.cpp | 15 ++-- tools/src/asset_processor/assets/tileset.cpp | 14 ++-- tools/src/asset_processor/main.cpp | 17 +++-- tools/src/asset_processor/offsets.cpp | 8 +- tools/src/asset_processor/offsets.h | 4 +- tools/src/asset_processor/reader.cpp | 7 -- tools/src/asset_processor/reader.h | 20 ++--- tools/src/asset_processor/util.cpp | 8 ++ tools/src/asset_processor/util.h | 11 --- 20 files changed, 190 insertions(+), 196 deletions(-) diff --git a/tools/src/asset_processor/CMakeLists.txt b/tools/src/asset_processor/CMakeLists.txt index 4e08c83a..387b4ab5 100644 --- a/tools/src/asset_processor/CMakeLists.txt +++ b/tools/src/asset_processor/CMakeLists.txt @@ -2,6 +2,7 @@ file(GLOB_RECURSE sources *.cpp) add_executable(asset_processor ${sources}) target_include_directories(asset_processor PRIVATE .) -target_link_libraries(asset_processor PRIVATE nlohmann_json::nlohmann_json filesystem) +target_link_libraries(asset_processor PRIVATE project_settings) +target_link_libraries(asset_processor PRIVATE fmt::fmt nlohmann_json::nlohmann_json filesystem) install(TARGETS asset_processor RUNTIME DESTINATION bin) diff --git a/tools/src/asset_processor/assets/aif.cpp b/tools/src/asset_processor/assets/aif.cpp index f0c056af..19324ac4 100644 --- a/tools/src/asset_processor/assets/aif.cpp +++ b/tools/src/asset_processor/assets/aif.cpp @@ -2,8 +2,8 @@ #include "util.h" std::filesystem::path AifAsset::generateAssetPath() { - std::filesystem::path path = this->path; - return path.replace_extension(".aif"); + std::filesystem::path asset_path = path; + return asset_path.replace_extension(".aif"); } void AifAsset::convertToHumanReadable(const std::vector& baserom) { @@ -11,17 +11,17 @@ void AifAsset::convertToHumanReadable(const std::vector& baserom) { std::filesystem::path toolsPath = "tools"; std::vector cmd; - cmd.push_back(toolsPath / "aif2pcm" / "aif2pcm"); - cmd.push_back(this->path); - cmd.push_back(this->assetPath); + cmd.push_back(toolsPath / "bin" / "aif2pcm"); + cmd.push_back(path); + cmd.push_back(assetPath); check_call(cmd); } void AifAsset::buildToBinary() { std::filesystem::path toolsPath = "tools"; std::vector cmd; - cmd.push_back(toolsPath / "aif2pcm" / "aif2pcm"); - cmd.push_back(this->assetPath); - cmd.push_back(this->path); + cmd.push_back(toolsPath / "bin" / "aif2pcm"); + cmd.push_back(assetPath); + cmd.push_back(path); check_call(cmd); } \ No newline at end of file diff --git a/tools/src/asset_processor/assets/animation.cpp b/tools/src/asset_processor/assets/animation.cpp index 334d8093..1fbd0d43 100644 --- a/tools/src/asset_processor/assets/animation.cpp +++ b/tools/src/asset_processor/assets/animation.cpp @@ -2,33 +2,34 @@ #include "reader.h" #include #include +#include void AnimationAsset::convertToHumanReadable(const std::vector& baserom) { - Reader reader(baserom, this->start, this->size); + Reader reader(baserom, start, size); std::vector lines; bool end_of_animation = false; - while (!end_of_animation && reader.cursor + 3 < this->size) { + while (!end_of_animation && reader.cursor + 3 < size) { u8 frame_index = reader.read_u8(); u8 keyframe_duration = reader.read_u8(); u8 bitfield = reader.read_u8(); u8 bitfield2 = reader.read_u8(); end_of_animation = (bitfield2 & 0x80) != 0; - lines.push_back(string_format("\tkeyframe frame_index=%d", frame_index)); - lines.push_back(opt_param(", duration=%d", 0, keyframe_duration)); - lines.push_back(opt_param(", bitfield=0x%x", 0, bitfield)); - lines.push_back(opt_param(", bitfield2=0x%x", 0, bitfield2)); - lines.push_back("\n"); + lines.push_back(fmt::format("\tkeyframe frame_index={}", frame_index)); + lines.push_back(opt_param(", duration={}", 0, keyframe_duration)); + lines.push_back(opt_param(", bitfield={:#x}", 0, bitfield)); + lines.push_back(opt_param(", bitfield2={:#x}", 0, bitfield2)); + lines.emplace_back("\n"); } if (!end_of_animation) { - lines.push_back("@ TODO why no terminator?\n"); + lines.emplace_back("@ TODO why no terminator?\n"); } - while (reader.cursor < this->size) { + while (reader.cursor < size) { u8 keyframe_count = reader.read_u8(); - lines.push_back(string_format("\t.byte %d @ keyframe count\n", keyframe_count)); + lines.push_back(fmt::format("\t.byte {} @ keyframe count\n", keyframe_count)); } - std::ofstream out(this->assetPath); + std::ofstream out(assetPath); for (const auto& line : lines) { out << line; } diff --git a/tools/src/asset_processor/assets/asset.cpp b/tools/src/asset_processor/assets/asset.cpp index 139f11ad..d75f50b9 100644 --- a/tools/src/asset_processor/assets/asset.cpp +++ b/tools/src/asset_processor/assets/asset.cpp @@ -2,10 +2,10 @@ #include void BaseAsset::extractBinary(const std::vector& baserom) { - auto first = baserom.begin() + this->start; - auto last = baserom.begin() + this->start + this->size; + auto first = baserom.begin() + start; + auto last = baserom.begin() + start + size; std::vector data(first, last); - std::fstream file(this->path, std::ios::out | std::ios::binary); - file.write(&data[0], data.size()); + std::fstream file(path, std::ios::out | std::ios::binary); + file.write(&data[0], static_cast(data.size())); file.close(); } \ No newline at end of file diff --git a/tools/src/asset_processor/assets/asset.h b/tools/src/asset_processor/assets/asset.h index 96231b0b..f34485a2 100644 --- a/tools/src/asset_processor/assets/asset.h +++ b/tools/src/asset_processor/assets/asset.h @@ -6,27 +6,27 @@ #include #include #include +#include class BaseAsset { public: - BaseAsset(std::filesystem::path path, int start, int size, const nlohmann::json& asset) - : path(path), start(start), size(size), asset(asset) { + BaseAsset(std::filesystem::path path_, int start_, int size_, const nlohmann::json& asset_) + : path(std::move(path_)), start(start_), size(size_), asset(asset_) { } virtual ~BaseAsset() = default; void setup() { // Cannot call virtual functions in constructor, so another function call is necessary - assetPath = this->generateAssetPath(); - buildPath = this->generateBuildPath(); + assetPath = generateAssetPath(); + buildPath = generateBuildPath(); } // Extract the binary segment for this asset from the baserom and store it in a separate file. virtual void extractBinary(const std::vector& baserom); // Convert the binary data for this asset to a human readable form. - virtual void convertToHumanReadable(const std::vector& baserom) { - (void)baserom; + virtual void convertToHumanReadable([[maybe_unused]] const std::vector& baserom) { } // Build the asset from the human readable form back to the binary. @@ -34,30 +34,31 @@ class BaseAsset { } // Returns the path to the binary file extracted from the baserom. - std::filesystem::path getPath() const { + [[nodiscard]] std::filesystem::path getPath() const { return path; } + // Returns the path to the human readable asset file. - std::filesystem::path getAssetPath() const { + [[nodiscard]] std::filesystem::path getAssetPath() const { return assetPath; } + // Returns the path to the resulting file after building. - std::filesystem::path getBuildPath() const { + [[nodiscard]] std::filesystem::path getBuildPath() const { return buildPath; } // Returns the base of the filename of the asset. - std::string getSymbol() const { + [[nodiscard]] std::string getSymbol() const { // Need to get the stem twice to remove both of the .4bpp.lz extensions. - return (this->path.stem()).stem(); + return (path.stem()).stem(); } // Returns the start address of this asset. - int getStart() const { + [[nodiscard]] int getStart() const { return start; } - protected: std::filesystem::path path; std::filesystem::path assetPath; @@ -68,10 +69,10 @@ class BaseAsset { private: virtual std::filesystem::path generateAssetPath() { - return this->path; + return path; } virtual std::filesystem::path generateBuildPath() { - return this->path; + return path; } }; diff --git a/tools/src/asset_processor/assets/exitlist.cpp b/tools/src/asset_processor/assets/exitlist.cpp index c4e193c6..ad71dba7 100644 --- a/tools/src/asset_processor/assets/exitlist.cpp +++ b/tools/src/asset_processor/assets/exitlist.cpp @@ -2,11 +2,12 @@ #include "reader.h" #include #include +#include void ExitListAsset::convertToHumanReadable(const std::vector& baserom) { - Reader reader(baserom, this->start, this->size); + Reader reader(baserom, start, size); std::vector lines; - while (reader.cursor < this->size) { + while (reader.cursor < size) { u16 transition_type = reader.read_u16(); u16 x_pos = reader.read_u16(); u16 y_pos = reader.read_u16(); @@ -21,26 +22,26 @@ void ExitListAsset::convertToHumanReadable(const std::vector& baserom) { u16 unknown_5 = reader.read_u16(); u16 padding_1 = reader.read_u16(); if (transition_type == 0xffff) { - lines.push_back("\texit_list_end\n"); + lines.emplace_back("\texit_list_end\n"); break; } - lines.push_back(string_format("\texit transition=%d", transition_type)); - lines.push_back(opt_param(", x=0x%x", 0, x_pos)); - lines.push_back(opt_param(", y=0x%x", 0, y_pos)); - lines.push_back(opt_param(", destX=0x%x", 0, dest_x)); - lines.push_back(opt_param(", destY=0x%x", 0, dest_y)); - lines.push_back(opt_param(", screenEdge=0x%x", 0, screen_edge)); - lines.push_back(opt_param(", destArea=0x%x", 0, dest_area)); - lines.push_back(opt_param(", destRoom=0x%x", 0, dest_room)); - lines.push_back(opt_param(", unknown=0x%x", 0, unknown_2)); - lines.push_back(opt_param(", unknown2=0x%x", 0, unknown_3)); - lines.push_back(opt_param(", unknown3=0x%x", 0, unknown_4)); - lines.push_back(opt_param(", unknown4=0x%x", 0, unknown_5)); - lines.push_back(opt_param(", padding=0x%x", 0, padding_1)); - lines.push_back("\n"); + lines.push_back(fmt::format("\texit transition={}", transition_type)); + lines.push_back(opt_param(", x={:#x}", 0, x_pos)); + lines.push_back(opt_param(", y={:#x}", 0, y_pos)); + lines.push_back(opt_param(", destX={:#x}", 0, dest_x)); + lines.push_back(opt_param(", destY={:#x}", 0, dest_y)); + lines.push_back(opt_param(", screenEdge={:#x}", 0, screen_edge)); + lines.push_back(opt_param(", destArea={:#x}", 0, dest_area)); + lines.push_back(opt_param(", destRoom={:#x}", 0, dest_room)); + lines.push_back(opt_param(", unknown={:#x}", 0, unknown_2)); + lines.push_back(opt_param(", unknown2={:#x}", 0, unknown_3)); + lines.push_back(opt_param(", unknown3={:#x}", 0, unknown_4)); + lines.push_back(opt_param(", unknown4={:#x}", 0, unknown_5)); + lines.push_back(opt_param(", padding={:#x}", 0, padding_1)); + lines.emplace_back("\n"); } - std::ofstream out(this->assetPath); + std::ofstream out(assetPath); for (const auto& line : lines) { out << line; } diff --git a/tools/src/asset_processor/assets/frameobjlists.cpp b/tools/src/asset_processor/assets/frameobjlists.cpp index fa78fddd..f196abae 100644 --- a/tools/src/asset_processor/assets/frameobjlists.cpp +++ b/tools/src/asset_processor/assets/frameobjlists.cpp @@ -3,17 +3,18 @@ #include #include #include +#include void FrameObjListsAsset::convertToHumanReadable(const std::vector& baserom) { - Reader reader(baserom, this->start, this->size); + Reader reader(baserom, start, size); - std::vector first_level; - std::vector second_level; + std::vector first_level; + std::vector second_level; std::vector lines; - lines.push_back("@ First level of offsets\n"); + lines.emplace_back("@ First level of offsets\n"); - while (reader.cursor < this->size) { + while (reader.cursor < size) { if (std::find(first_level.begin(), first_level.end(), reader.cursor) != first_level.end()) { // End of first level break; @@ -21,58 +22,58 @@ void FrameObjListsAsset::convertToHumanReadable(const std::vector& baserom u32 pointer = reader.read_u32(); first_level.push_back(pointer); - lines.push_back(string_format("\t.4byte 0x%x\n", pointer)); + lines.push_back(fmt::format("\t.4byte {:#x}\n", pointer)); } - lines.push_back("\n@ Second level of offsets\n"); + lines.emplace_back("\n@ Second level of offsets\n"); - while (reader.cursor < this->size) { + while (reader.cursor < size) { if (std::find(second_level.begin(), second_level.end(), reader.cursor) != second_level.end()) { // End of second level break; } u32 pointer = reader.read_u32(); second_level.push_back(pointer); - lines.push_back(string_format("\t.4byte 0x%x\n", pointer)); + lines.push_back(fmt::format("\t.4byte {:#x}\n", pointer)); } - int max_second_level = *std::max_element(second_level.begin(), second_level.end()); + u32 max_second_level = *std::max_element(second_level.begin(), second_level.end()); - while (reader.cursor < this->size) { - if (reader.cursor > max_second_level) { + while (reader.cursor < size) { + if (static_cast(reader.cursor) > max_second_level) { break; } if (std::find(second_level.begin(), second_level.end(), reader.cursor) == second_level.end()) { // Find nearest next value that is in the second level int next = -1; for (const auto& i : second_level) { - if (i > reader.cursor && (next == -1 || i < next)) { - next = i; + if (i > static_cast(reader.cursor) && (next == -1 || i < static_cast(next))) { + next = static_cast(i); } } int diff = next - reader.cursor; - lines.push_back(string_format("@ Skipping %d bytes\n", diff)); + lines.push_back(fmt::format("@ Skipping {} bytes\n", diff)); for (int i = 0; i < diff; i++) { u8 byte = reader.read_u8(); - lines.push_back(string_format("\t.byte %d\n", byte)); + lines.push_back(fmt::format("\t.byte {}\n", byte)); } } u8 num_objects = reader.read_u8(); - lines.push_back(string_format("\t.byte %d @ num_objs\n", num_objects)); + lines.push_back(fmt::format("\t.byte {} @ num_objs\n", num_objects)); for (int i = 0; i < num_objects; i++) { s8 x_offset = reader.read_s8(); s8 y_offset = reader.read_s8(); u8 bitfield = reader.read_u8(); u16 bitfield2 = reader.read_u16(); - lines.push_back(string_format("\tobj x=%d, y=%d", x_offset, y_offset)); - lines.push_back(opt_param(", bitfield=0x%x", 0, bitfield)); - lines.push_back(opt_param(", bitfield2=0x%x", 0, bitfield2)); - lines.push_back("\n"); + lines.push_back(fmt::format("\tobj x={}, y={}", x_offset, y_offset)); + lines.push_back(opt_param(", bitfield={:#x}", 0, bitfield)); + lines.push_back(opt_param(", bitfield2={:#x}", 0, bitfield2)); + lines.emplace_back("\n"); } } - std::ofstream out(this->assetPath); + std::ofstream out(assetPath); for (const auto& line : lines) { out << line; } diff --git a/tools/src/asset_processor/assets/gfx.cpp b/tools/src/asset_processor/assets/gfx.cpp index 03feca77..39a11e3c 100644 --- a/tools/src/asset_processor/assets/gfx.cpp +++ b/tools/src/asset_processor/assets/gfx.cpp @@ -3,7 +3,7 @@ #include std::filesystem::path GfxAsset::generateAssetPath() { - std::filesystem::path pngPath = this->path; + std::filesystem::path pngPath = path; if (pngPath.extension() == ".lz") { pngPath.replace_extension(""); } @@ -16,11 +16,11 @@ void GfxAsset::convertToHumanReadable(const std::vector& baserom) { std::filesystem::path toolsPath = "tools"; std::vector cmd; - cmd.push_back(toolsPath / "gbagfx" / "gbagfx"); - cmd.push_back(this->path); - cmd.push_back(this->assetPath); - if (this->asset.contains("options")) { - for (const auto& it : this->asset["options"].items()) { + cmd.push_back(toolsPath / "bin" / "gbagfx"); + cmd.push_back(path); + cmd.push_back(assetPath); + if (asset.contains("options")) { + for (const auto& it : asset["options"].items()) { cmd.push_back("-" + it.key()); cmd.push_back(to_string(it.value())); } @@ -31,8 +31,8 @@ void GfxAsset::convertToHumanReadable(const std::vector& baserom) { void GfxAsset::buildToBinary() { std::filesystem::path toolsPath = "tools"; std::vector cmd; - cmd.push_back(toolsPath / "gbagfx" / "gbagfx"); - cmd.push_back(this->assetPath); - cmd.push_back(this->path); + cmd.push_back(toolsPath / "bin" / "gbagfx"); + cmd.push_back(assetPath); + cmd.push_back(path); check_call(cmd); } \ No newline at end of file diff --git a/tools/src/asset_processor/assets/macroasm.cpp b/tools/src/asset_processor/assets/macroasm.cpp index c87f355b..f20d48ec 100644 --- a/tools/src/asset_processor/assets/macroasm.cpp +++ b/tools/src/asset_processor/assets/macroasm.cpp @@ -2,19 +2,19 @@ #include std::filesystem::path BaseMacroAsmAsset::generateAssetPath() { - std::filesystem::path path = this->path; - return path.replace_extension(".s"); + std::filesystem::path asset_path = path; + return asset_path.replace_extension(".s"); } std::filesystem::path BaseMacroAsmAsset::generateBuildPath() { - std::filesystem::path path = this->path; - return path.replace_extension(".s"); + std::filesystem::path build_path = path; + return build_path.replace_extension(".s"); } void BaseMacroAsmAsset::extractBinary(const std::vector& baserom) { BaseAsset::extractBinary(baserom); // Create dummy .s file that just incbins the .bin file. - std::ofstream out(this->assetPath); - out << "\t.incbin " << this->path << "\n"; + std::ofstream out(assetPath); + out << "\t.incbin " << path << "\n"; out.close(); } \ No newline at end of file diff --git a/tools/src/asset_processor/assets/midi.cpp b/tools/src/asset_processor/assets/midi.cpp index 6444b05e..1a3f1852 100644 --- a/tools/src/asset_processor/assets/midi.cpp +++ b/tools/src/asset_processor/assets/midi.cpp @@ -5,51 +5,52 @@ #include #include #include +#include extern std::string gBaseromPath; std::filesystem::path MidiAsset::generateAssetPath() { - std::filesystem::path path = this->path; - return path.replace_extension(".mid"); + std::filesystem::path asset_path = path; + return asset_path.replace_extension(".mid"); } std::filesystem::path MidiAsset::generateBuildPath() { - std::filesystem::path path = this->path; - return path.replace_extension(".s"); + std::filesystem::path build_path = path; + return build_path.replace_extension(".s"); } void MidiAsset::extractBinary(const std::vector& baserom) { // Custom extraction as we need a label in the middle. - std::string label = this->path.stem(); + std::string label = path.stem(); - std::filesystem::path tracksPath = this->path; + std::filesystem::path tracksPath = path; tracksPath.replace_filename(label + "_tracks.bin"); - std::filesystem::path headerPath = this->path; + std::filesystem::path headerPath = path; headerPath.replace_filename(label + "_header.bin"); - int headerOffset = this->asset["options"]["headerOffset"]; + int headerOffset = asset["options"]["headerOffset"]; // Extract tracks { - auto first = baserom.begin() + this->start; - auto last = baserom.begin() + this->start + headerOffset; + auto first = baserom.begin() + start; + auto last = baserom.begin() + start + headerOffset; std::vector data(first, last); std::fstream file(tracksPath, std::ios::out | std::ios::binary); - file.write(&data[0], data.size()); + file.write(&data[0], static_cast(data.size())); file.close(); } // Extract header { - auto first = baserom.begin() + this->start + headerOffset; - auto last = baserom.begin() + this->start + this->size; + auto first = baserom.begin() + start + headerOffset; + auto last = baserom.begin() + start + size; std::vector data(first, last); std::fstream file(headerPath, std::ios::out | std::ios::binary); - file.write(&data[0], data.size()); + file.write(&data[0], static_cast(data.size())); file.close(); } // Create dummy .s file. - std::ofstream out(this->buildPath); + std::ofstream out(buildPath); out << "\t.incbin " << tracksPath << "\n"; out << label << "::\n"; out << "\t.incbin " << headerPath << "\n"; @@ -59,35 +60,35 @@ void MidiAsset::extractBinary(const std::vector& baserom) { void MidiAsset::parseOptions(std::vector& commonParams, std::vector& agb2midParams) { bool exactGateTime = true; - for (const auto& it : this->asset["options"].items()) { + for (const auto& it : asset["options"].items()) { const std::string& key = it.key(); if (key == "group" || key == "G") { - commonParams.push_back("-G"); + commonParams.emplace_back("-G"); commonParams.push_back(to_string(it.value())); } else if (key == "priority" || key == "P") { - commonParams.push_back("-P"); + commonParams.emplace_back("-P"); commonParams.push_back(to_string(it.value())); } else if (key == "reverb" || key == "R") { - commonParams.push_back("-R"); + commonParams.emplace_back("-R"); commonParams.push_back(to_string(it.value())); } else if (key == "nominator") { - agb2midParams.push_back("-n"); + agb2midParams.emplace_back("-n"); agb2midParams.push_back(to_string(it.value())); } else if (key == "denominator") { - agb2midParams.push_back("-d"); + agb2midParams.emplace_back("-d"); agb2midParams.push_back(to_string(it.value())); } else if (key == "timeChanges") { const nlohmann::json& value = it.value(); if (value.is_array()) { // Multiple time changes for (const auto& change : value) { - agb2midParams.push_back("-t"); + agb2midParams.emplace_back("-t"); agb2midParams.push_back(to_string(change["nominator"])); agb2midParams.push_back(to_string(change["denominator"])); agb2midParams.push_back(to_string(change["time"])); } } else { - agb2midParams.push_back("-t"); + agb2midParams.emplace_back("-t"); agb2midParams.push_back(to_string(value["nominator"])); agb2midParams.push_back(to_string(value["denominator"])); agb2midParams.push_back(to_string(value["time"])); @@ -107,7 +108,7 @@ void MidiAsset::parseOptions(std::vector& commonParams, std::vector } if (exactGateTime) { - commonParams.push_back("-E"); + commonParams.emplace_back("-E"); } } @@ -117,26 +118,26 @@ void MidiAsset::convertToHumanReadable(const std::vector& baserom) { // Convert the options std::vector commonParams; std::vector agb2midParams; - this->parseOptions(commonParams, agb2midParams); + parseOptions(commonParams, agb2midParams); - int headerOffset = this->asset["options"]["headerOffset"]; + int headerOffset = asset["options"]["headerOffset"]; std::filesystem::path toolPath = "tools"; std::vector cmd; - cmd.push_back(toolPath / "agb2mid" / "agb2mid"); + cmd.push_back(toolPath / "bin" / "agb2mid"); cmd.push_back(gBaseromPath); - cmd.push_back(string_format("0x%x", this->start + headerOffset)); + cmd.push_back(fmt::format("{:#x}", start + headerOffset)); cmd.push_back(gBaseromPath); // TODO deduplicate? - cmd.push_back(this->assetPath); + cmd.push_back(assetPath); cmd.insert(cmd.end(), commonParams.begin(), commonParams.end()); cmd.insert(cmd.end(), agb2midParams.begin(), agb2midParams.end()); check_call(cmd); // We also need to build the mid to an s file here, so we get shiftability after converting. cmd.clear(); - cmd.push_back(toolPath / "mid2agb" / "mid2agb"); - cmd.push_back(this->assetPath); - cmd.push_back(this->buildPath); + cmd.push_back(toolPath / "bin" / "mid2agb"); + cmd.push_back(assetPath); + cmd.push_back(buildPath); cmd.insert(cmd.end(), commonParams.begin(), commonParams.end()); check_call(cmd); } @@ -145,12 +146,12 @@ void MidiAsset::buildToBinary() { // Convert the options std::vector commonParams; std::vector agb2midParams; - this->parseOptions(commonParams, agb2midParams); + parseOptions(commonParams, agb2midParams); std::filesystem::path toolPath = "tools"; std::vector cmd; - cmd.push_back(toolPath / "mid2agb" / "mid2agb"); - cmd.push_back(this->assetPath); - cmd.push_back(this->buildPath); + cmd.push_back(toolPath / "bin" / "mid2agb"); + cmd.push_back(assetPath); + cmd.push_back(buildPath); cmd.insert(cmd.end(), commonParams.begin(), commonParams.end()); check_call(cmd); } \ No newline at end of file diff --git a/tools/src/asset_processor/assets/palette.cpp b/tools/src/asset_processor/assets/palette.cpp index 74cf94c3..f37504e7 100644 --- a/tools/src/asset_processor/assets/palette.cpp +++ b/tools/src/asset_processor/assets/palette.cpp @@ -2,8 +2,8 @@ #include "util.h" std::filesystem::path PaletteAsset::generateAssetPath() { - std::filesystem::path path = this->path; - return path.replace_extension(".pal"); + std::filesystem::path asset_path = path; + return asset_path.replace_extension(".pal"); } void PaletteAsset::convertToHumanReadable(const std::vector& baserom) { @@ -11,17 +11,17 @@ void PaletteAsset::convertToHumanReadable(const std::vector& baserom) { std::filesystem::path toolsPath = "tools"; std::vector cmd; - cmd.push_back(toolsPath / "gbagfx" / "gbagfx"); - cmd.push_back(this->path); - cmd.push_back(this->assetPath); + cmd.push_back(toolsPath / "bin" / "gbagfx"); + cmd.push_back(path); + cmd.push_back(assetPath); check_call(cmd); } void PaletteAsset::buildToBinary() { std::filesystem::path toolsPath = "tools"; std::vector cmd; - cmd.push_back(toolsPath / "gbagfx" / "gbagfx"); - cmd.push_back(this->assetPath); - cmd.push_back(this->path); + cmd.push_back(toolsPath / "bin" / "gbagfx"); + cmd.push_back(assetPath); + cmd.push_back(path); check_call(cmd); } \ No newline at end of file diff --git a/tools/src/asset_processor/assets/spriteframe.cpp b/tools/src/asset_processor/assets/spriteframe.cpp index 6aea52cf..5c303e4b 100644 --- a/tools/src/asset_processor/assets/spriteframe.cpp +++ b/tools/src/asset_processor/assets/spriteframe.cpp @@ -2,22 +2,23 @@ #include "reader.h" #include #include +#include void SpriteFrameAsset::convertToHumanReadable(const std::vector& baserom) { - Reader reader(baserom, this->start, this->size); + Reader reader(baserom, start, size); std::vector lines; - while (reader.cursor < this->size) { + while (reader.cursor < size) { u8 num_gfx_tiles = reader.read_u8(); u8 unk = reader.read_u8(); u16 first_gfx_tile_index = reader.read_u16(); - lines.push_back(string_format("\tsprite_frame first_tile_index=0x%x", first_gfx_tile_index)); - lines.push_back(opt_param(", num_tiles=%d", 0, num_gfx_tiles)); - lines.push_back(opt_param(", unknown=0x%x", 0, unk)); - lines.push_back("\n"); + lines.push_back(fmt::format("\tsprite_frame first_tile_index={:#x}", first_gfx_tile_index)); + lines.push_back(opt_param(", num_tiles={}", 0, num_gfx_tiles)); + lines.push_back(opt_param(", unknown={:#x}", 0, unk)); + lines.emplace_back("\n"); } - std::ofstream out(this->assetPath); + std::ofstream out(assetPath); for (const auto& line : lines) { out << line; } diff --git a/tools/src/asset_processor/assets/tileset.cpp b/tools/src/asset_processor/assets/tileset.cpp index 52caaa6b..cd564f33 100644 --- a/tools/src/asset_processor/assets/tileset.cpp +++ b/tools/src/asset_processor/assets/tileset.cpp @@ -2,7 +2,7 @@ #include "util.h" std::filesystem::path TilesetAsset::generateAssetPath() { - std::filesystem::path pngPath = this->path; + std::filesystem::path pngPath = path; if (pngPath.extension() == ".lz") { pngPath.replace_extension(""); } @@ -15,9 +15,9 @@ void TilesetAsset::convertToHumanReadable(const std::vector& baserom) { std::filesystem::path toolsPath = "tools"; std::vector cmd; - cmd.push_back(toolsPath / "gbagfx" / "gbagfx"); - cmd.push_back(this->path); - cmd.push_back(this->assetPath); + cmd.push_back(toolsPath / "bin" / "gbagfx"); + cmd.push_back(path); + cmd.push_back(assetPath); cmd.push_back("-mwidth"); cmd.push_back("32"); check_call(cmd); @@ -26,8 +26,8 @@ void TilesetAsset::convertToHumanReadable(const std::vector& baserom) { void TilesetAsset::buildToBinary() { std::filesystem::path toolsPath = "tools"; std::vector cmd; - cmd.push_back(toolsPath / "gbagfx" / "gbagfx"); - cmd.push_back(this->assetPath); - cmd.push_back(this->path); + cmd.push_back(toolsPath / "bin" / "gbagfx"); + cmd.push_back(assetPath); + cmd.push_back(path); check_call(cmd); } \ No newline at end of file diff --git a/tools/src/asset_processor/main.cpp b/tools/src/asset_processor/main.cpp index 347f27c5..c62860a0 100644 --- a/tools/src/asset_processor/main.cpp +++ b/tools/src/asset_processor/main.cpp @@ -13,6 +13,7 @@ #include #include #include +#include using nlohmann::json; @@ -107,12 +108,12 @@ int main(int argc, char** argv) { // Read baserom. std::ifstream file(gBaseromPath, std::ios::binary | std::ios::ate); - std::streamsize size = file.tellg(); + auto size = file.tellg(); file.seekg(0, std::ios::beg); - std::vector baserom(size); + std::vector baserom(static_cast(size)); if (!file.read(baserom.data(), size)) { - std::cerr << "Could not read baserom " << gBaseromPath << std::endl; + fmt::print(stderr, "Could not read baserom {}\n", gBaseromPath); std::exit(1); } file.close(); @@ -141,7 +142,7 @@ int main(int argc, char** argv) { std::unique_ptr offsetCalculator; - uint currentOffset = 0; + int currentOffset = 0; for (const auto& asset : assets) { if (asset.contains("offsets")) { // Offset definition if (asset["offsets"].contains(gVariant)) { @@ -221,7 +222,7 @@ std::unique_ptr getAssetHandlerByType(const std::filesystem::path& pa start = asset["starts"][gVariant]; } - std::string type = ""; + std::string type; if (asset.contains("type")) { type = asset["type"]; } @@ -256,11 +257,11 @@ std::unique_ptr getAssetHandlerByType(const std::filesystem::path& pa type == "map_collision" || type == "unknown") { // TODO implement conversions assetHandler = std::make_unique(path, start, size, asset); - } else if (type == "") { + } else if (type.empty()) { // Unknown binary asset assetHandler = std::make_unique(path, start, size, asset); } else { - std::cerr << "Error: Unimplemented asset type `" << type << "`" << std::endl; + fmt::print(stderr, "Error: Unimplemented asset type \"{}\"", type); std::exit(1); } assetHandler->setup(); @@ -287,7 +288,7 @@ bool shouldExtractAsset(const std::filesystem::path& path, const std::filesystem void extractAsset(std::unique_ptr& assetHandler, const std::vector& baserom) { // Create the parent directory - std::filesystem::path parentDir = std::filesystem::path(assetHandler->getPath()); + std::filesystem::path parentDir = assetHandler->getPath(); parentDir.remove_filename(); std::filesystem::create_directories(parentDir); diff --git a/tools/src/asset_processor/offsets.cpp b/tools/src/asset_processor/offsets.cpp index 48ed4139..bff5b1a5 100644 --- a/tools/src/asset_processor/offsets.cpp +++ b/tools/src/asset_processor/offsets.cpp @@ -1,9 +1,9 @@ #include "offsets.h" -OffsetCalculator::OffsetCalculator(std::filesystem::path outputFile, int baseOffset) : baseOffset(baseOffset) { - output = std::ofstream(outputFile); +OffsetCalculator::OffsetCalculator(const std::filesystem::path& outputFile, int baseOffset_) + : output(outputFile), baseOffset(baseOffset_) { } -void OffsetCalculator::addAsset(int start, std::string symbol) { - this->output << "\t.equiv offset_" << symbol << ", " << start - this->baseOffset << std::endl; +void OffsetCalculator::addAsset(int start, const std::string& symbol) { + output << "\t.equiv offset_" << symbol << ", " << start - baseOffset << std::endl; } \ No newline at end of file diff --git a/tools/src/asset_processor/offsets.h b/tools/src/asset_processor/offsets.h index bf9fef4e..8a90cfea 100644 --- a/tools/src/asset_processor/offsets.h +++ b/tools/src/asset_processor/offsets.h @@ -7,8 +7,8 @@ class OffsetCalculator { public: - OffsetCalculator(std::filesystem::path offsetsFile, int baseOffset); - void addAsset(int start, std::string symbol); + OffsetCalculator(const std::filesystem::path& offsetsFile, int baseOffset_); + void addAsset(int start, const std::string& symbol); private: std::ofstream output; diff --git a/tools/src/asset_processor/reader.cpp b/tools/src/asset_processor/reader.cpp index 4ce7842e..f1d28293 100644 --- a/tools/src/asset_processor/reader.cpp +++ b/tools/src/asset_processor/reader.cpp @@ -1,10 +1,3 @@ #include "reader.h" #include "util.h" #include - -std::string opt_param(const std::string& format, int defaultVal, int value) { - if (value != defaultVal) { - return string_format(format, value); - } - return ""; -} \ No newline at end of file diff --git a/tools/src/asset_processor/reader.h b/tools/src/asset_processor/reader.h index 4ab548de..d375735b 100644 --- a/tools/src/asset_processor/reader.h +++ b/tools/src/asset_processor/reader.h @@ -1,7 +1,7 @@ #ifndef READER_H #define READER_H -#include +#include #include typedef uint8_t u8; @@ -21,26 +21,22 @@ class Reader { data = std::vector(first, last); } - u8 read_u8() { - return this->data[this->cursor++]; + s8 read_s8() { + return data[static_cast(cursor++)]; } - s8 read_s8() { - return (s8)this->read_u8(); + u8 read_u8() { + return static_cast(read_s8()); } u16 read_u16() { - u16 val = (u8)this->data[this->cursor] + (((u8)this->data[this->cursor + 1]) << 8); - this->cursor += 2; - return val; + return static_cast(read_u8() + (read_u8() << 8)); } 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; + return static_cast(read_u16() + (read_u16() << 16)); } + int cursor = 0; private: diff --git a/tools/src/asset_processor/util.cpp b/tools/src/asset_processor/util.cpp index f6ac050f..a0effa3c 100644 --- a/tools/src/asset_processor/util.cpp +++ b/tools/src/asset_processor/util.cpp @@ -1,5 +1,6 @@ #include "util.h" #include +#include void check_call(const std::vector& cmd) { std::string cmdstr; @@ -18,3 +19,10 @@ void check_call(const std::vector& cmd) { std::exit(1); } } + +std::string opt_param(const std::string& format, int defaultVal, int value) { + if (value != defaultVal) { + return fmt::format(format, value); + } + return ""; +} diff --git a/tools/src/asset_processor/util.h b/tools/src/asset_processor/util.h index f7e5c5f5..111b4e47 100644 --- a/tools/src/asset_processor/util.h +++ b/tools/src/asset_processor/util.h @@ -20,17 +20,6 @@ typedef int64_t s64; void check_call(const std::vector& cmd); -template 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_s); - auto buf = std::make_unique(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 \ No newline at end of file