diff --git a/tools/src/asset_processor/assets/animation.cpp b/tools/src/asset_processor/assets/animation.cpp index 1fbd0d43..73923e3f 100644 --- a/tools/src/asset_processor/assets/animation.cpp +++ b/tools/src/asset_processor/assets/animation.cpp @@ -1,8 +1,7 @@ #include "animation.h" #include "reader.h" -#include -#include #include +#include void AnimationAsset::convertToHumanReadable(const std::vector& baserom) { Reader reader(baserom, start, size); @@ -29,9 +28,8 @@ void AnimationAsset::convertToHumanReadable(const std::vector& baserom) { u8 keyframe_count = reader.read_u8(); lines.push_back(fmt::format("\t.byte {} @ keyframe count\n", keyframe_count)); } - std::ofstream out(assetPath); + auto file = util::open_file(assetPath, "w"); for (const auto& line : lines) { - out << line; + std::fputs(line.c_str(), file.get()); } - out.close(); } diff --git a/tools/src/asset_processor/assets/asset.cpp b/tools/src/asset_processor/assets/asset.cpp index ff81e810..ebb0d700 100644 --- a/tools/src/asset_processor/assets/asset.cpp +++ b/tools/src/asset_processor/assets/asset.cpp @@ -2,6 +2,6 @@ #include void BaseAsset::extractBinary(const std::vector& baserom) { - auto file = util::open_file(path.string(), "w"); + auto file = util::open_file(path, "w"); std::fwrite(baserom.data() + start, 1, static_cast(size), file.get()); } diff --git a/tools/src/asset_processor/assets/exitlist.cpp b/tools/src/asset_processor/assets/exitlist.cpp index ad71dba7..f73d4a0c 100644 --- a/tools/src/asset_processor/assets/exitlist.cpp +++ b/tools/src/asset_processor/assets/exitlist.cpp @@ -1,8 +1,7 @@ #include "exitlist.h" #include "reader.h" -#include -#include #include +#include void ExitListAsset::convertToHumanReadable(const std::vector& baserom) { Reader reader(baserom, start, size); @@ -41,9 +40,8 @@ void ExitListAsset::convertToHumanReadable(const std::vector& baserom) { lines.emplace_back("\n"); } - std::ofstream out(assetPath); + auto file = util::open_file(assetPath, "w"); for (const auto& line : lines) { - out << line; + std::fputs(line.c_str(), file.get()); } - out.close(); } \ No newline at end of file diff --git a/tools/src/asset_processor/assets/frameobjlists.cpp b/tools/src/asset_processor/assets/frameobjlists.cpp index f196abae..db1b80ca 100644 --- a/tools/src/asset_processor/assets/frameobjlists.cpp +++ b/tools/src/asset_processor/assets/frameobjlists.cpp @@ -1,9 +1,8 @@ #include "frameobjlists.h" #include "reader.h" #include -#include -#include #include +#include void FrameObjListsAsset::convertToHumanReadable(const std::vector& baserom) { Reader reader(baserom, start, size); @@ -73,9 +72,8 @@ void FrameObjListsAsset::convertToHumanReadable(const std::vector& baserom } } - std::ofstream out(assetPath); + auto file = util::open_file(assetPath, "w"); for (const auto& line : lines) { - out << line; + std::fputs(line.c_str(), file.get()); } - out.close(); } \ 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 f20d48ec..c64df2e1 100644 --- a/tools/src/asset_processor/assets/macroasm.cpp +++ b/tools/src/asset_processor/assets/macroasm.cpp @@ -1,5 +1,6 @@ #include "macroasm.h" -#include +#include +#include std::filesystem::path BaseMacroAsmAsset::generateAssetPath() { std::filesystem::path asset_path = path; @@ -14,7 +15,6 @@ std::filesystem::path BaseMacroAsmAsset::generateBuildPath() { void BaseMacroAsmAsset::extractBinary(const std::vector& baserom) { BaseAsset::extractBinary(baserom); // Create dummy .s file that just incbins the .bin file. - std::ofstream out(assetPath); - out << "\t.incbin " << path << "\n"; - out.close(); + auto file = util::open_file(assetPath, "w"); + fmt::print(file.get(), "\t.incbin \"{}\"\n", path.native()); } \ 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 1c383877..1bc575fb 100644 --- a/tools/src/asset_processor/assets/midi.cpp +++ b/tools/src/asset_processor/assets/midi.cpp @@ -2,11 +2,9 @@ #include "reader.h" #include "util.h" #include -#include -#include -#include #include #include +#include extern std::string gBaseromPath; @@ -33,21 +31,20 @@ void MidiAsset::extractBinary(const std::vector& baserom) { // Extract tracks { - auto file = util::open_file(tracksPath.string(), "w"); + auto file = util::open_file(tracksPath, "w"); std::fwrite(baserom.data() + start, 1, static_cast(headerOffset), file.get()); } // Extract header { - auto file = util::open_file(headerPath.string(), "w"); + auto file = util::open_file(headerPath, "w"); std::fwrite(baserom.data() + start + headerOffset, 1, static_cast(size - headerOffset), file.get()); } // Create dummy .s file. - std::ofstream out(buildPath); - out << "\t.incbin " << tracksPath << "\n"; - out << label << "::\n"; - out << "\t.incbin " << headerPath << "\n"; - out.close(); + auto file = util::open_file(buildPath, "w"); + fmt::print(file.get(), "\t.incbin \"{}\"\n", tracksPath.native()); + fmt::print(file.get(), "{}::\n", label); + fmt::print(file.get(), "\t.incbin \"{}\"\n", headerPath.native()); } void MidiAsset::parseOptions(std::vector& commonParams, std::vector& agb2midParams) { diff --git a/tools/src/asset_processor/assets/spriteframe.cpp b/tools/src/asset_processor/assets/spriteframe.cpp index 5c303e4b..24e69a4a 100644 --- a/tools/src/asset_processor/assets/spriteframe.cpp +++ b/tools/src/asset_processor/assets/spriteframe.cpp @@ -1,8 +1,7 @@ #include "spriteframe.h" #include "reader.h" -#include -#include #include +#include void SpriteFrameAsset::convertToHumanReadable(const std::vector& baserom) { Reader reader(baserom, start, size); @@ -18,9 +17,8 @@ void SpriteFrameAsset::convertToHumanReadable(const std::vector& baserom) lines.emplace_back("\n"); } - std::ofstream out(assetPath); + auto file = util::open_file(assetPath, "w"); for (const auto& line : lines) { - out << line; + std::fputs(line.c_str(), file.get()); } - out.close(); } \ No newline at end of file diff --git a/tools/src/asset_processor/offsets.h b/tools/src/asset_processor/offsets.h index 2117e804..83c41dac 100644 --- a/tools/src/asset_processor/offsets.h +++ b/tools/src/asset_processor/offsets.h @@ -9,7 +9,7 @@ class OffsetCalculator { public: OffsetCalculator(const std::filesystem::path& offsetsFile, int baseOffset_); void addAsset(int start, const std::string& symbol); - int getLastEnd() const { + [[nodiscard]] int getLastEnd() const { return lastEnd; } void setLastEnd(int lastEnd_) { diff --git a/tools/src/util/util/file.h b/tools/src/util/util/file.h index e7054826..9c3c0132 100644 --- a/tools/src/util/util/file.h +++ b/tools/src/util/util/file.h @@ -3,7 +3,9 @@ #include #include +#include #include + namespace util { namespace { auto file_ptr_deleter = [](std::FILE* file) { std::fclose(file); }; @@ -11,8 +13,13 @@ auto file_ptr_deleter = [](std::FILE* file) { std::fclose(file); }; using file_ptr_t = std::unique_ptr; -file_ptr_t open_file(std::string_view filename, std::string_view mode) { +inline file_ptr_t open_file(std::string_view filename, std::string_view mode) { return { std::fopen(filename.data(), mode.data()), file_ptr_deleter }; } + +inline file_ptr_t open_file(const std::string& filename, std::string_view mode) { + return { std::fopen(filename.c_str(), mode.data()), file_ptr_deleter }; +} + } // namespace util #endif // TOOLS_FILE_H