From 51faec5c5080d75f94ec7303bbef96cce2963d04 Mon Sep 17 00:00:00 2001 From: csunday95 Date: Tue, 29 Dec 2020 04:12:55 -0800 Subject: [PATCH] Close #13637: Refactor sprite compiler for filesystem efficiency - sprite building would save a file with just the sprite file header and then immediately open it again at the beginning of compilation - sprite file generation is now entirely in memory until the final output file is saved on success - added validation of no file activity in the failed build test case; failed builds will not generate a file or edit an existing one --- src/openrct2/CmdlineSprite.cpp | 30 +++--- test/tests/CLITests.cpp | 98 +++++++++++++++++++ test/tests/testdata/sprites/badManifest.json | 8 ++ test/tests/testdata/sprites/example.dat | Bin 0 -> 8587 bytes test/tests/testdata/sprites/manifest.json | 5 + test/tests/testdata/sprites/result.dat | Bin 0 -> 8 bytes test/tests/tests.vcxproj | 6 ++ 7 files changed, 129 insertions(+), 18 deletions(-) create mode 100644 test/tests/CLITests.cpp create mode 100644 test/tests/testdata/sprites/badManifest.json create mode 100644 test/tests/testdata/sprites/example.dat create mode 100644 test/tests/testdata/sprites/manifest.json create mode 100644 test/tests/testdata/sprites/result.dat diff --git a/src/openrct2/CmdlineSprite.cpp b/src/openrct2/CmdlineSprite.cpp index bb97194498..9c4a7daa9d 100644 --- a/src/openrct2/CmdlineSprite.cpp +++ b/src/openrct2/CmdlineSprite.cpp @@ -589,10 +589,11 @@ int32_t cmdline_for_sprite(const char** argv, int32_t argc) bool silent = (argc >= 4 && strcmp(argv[3], "silent") == 0); - SpriteFile baseFile; - baseFile.Header.num_entries = 0; - baseFile.Header.total_size = 0; - baseFile.Save(spriteFilePath); + // keep sprite file entirely in memory until ready to write out a complete, + // correct file + SpriteFile spriteFile; + spriteFile.Header.num_entries = 0; + spriteFile.Header.total_size = 0; fprintf(stdout, "Building: %s\n", spriteFilePath); @@ -632,25 +633,18 @@ int32_t cmdline_for_sprite(const char** argv, int32_t argc) return -1; } - auto spriteFile = SpriteFile::Open(spriteFilePath); - if (!spriteFile.has_value()) - { - fprintf(stderr, "Unable to open sprite file: %s\nCanceling\n", spriteFilePath); - return -1; - } - - spriteFile->AddImage(importResult.value()); - - if (!spriteFile->Save(spriteFilePath)) - { - fprintf(stderr, "Could not save sprite file: %s\nCanceling\n", imagePath.c_str()); - return -1; - } + spriteFile.AddImage(importResult.value()); if (!silent) fprintf(stdout, "Added: %s\n", imagePath.c_str()); } + if (!spriteFile.Save(spriteFilePath)) + { + log_error("Could not save sprite file, cancelling."); + return -1; + } + free(directoryPath); fprintf(stdout, "Finished\n"); diff --git a/test/tests/CLITests.cpp b/test/tests/CLITests.cpp new file mode 100644 index 0000000000..92be6408f1 --- /dev/null +++ b/test/tests/CLITests.cpp @@ -0,0 +1,98 @@ + +#include "TestData.h" + +#include +#include +#include +#include +#include +#include + +class CommandLineTests : public testing::Test +{ +public: + static std::string SpriteTestDataPath() + { + return Path::Combine(TestData::GetBasePath(), "sprites"); + } + + static std::string ManifestFilePath() + { + return Path::Combine(SpriteTestDataPath(), "manifest.json"); + } + + static std::string BadManifestFilePath() + { + return Path::Combine(SpriteTestDataPath(), "badManifest.json"); + } + + static std::string ExampleSpriteFilePath() + { + return Path::Combine(SpriteTestDataPath(), "example.dat"); + } + + static std::string BuildOutputfilePath() + { + return Path::Combine(SpriteTestDataPath(), "result.dat"); + } + + static bool CompareSpriteFiles(std::string original, std::string generated) + { + std::ifstream originalFile(original, std::ios::binary | std::ifstream::in); + std::ifstream generatedFile(generated, std::ios::binary | std::ifstream::in); + if (!(originalFile.is_open() && generatedFile.is_open())) + { + return false; + } + if (originalFile.tellg() != generatedFile.tellg()) + { + return false; + } + return std::equal( + std::istreambuf_iterator(originalFile.rdbuf()), std::istreambuf_iterator(), + std::istreambuf_iterator(generatedFile.rdbuf())); + } +}; + +TEST_F(CommandLineTests, cmdline_cmdline_for_sprite_details) +{ + std::string exampleFilePath = ExampleSpriteFilePath(); + const char* detailsCmd[3] = { "details", exampleFilePath.c_str() }; + + int32_t result = cmdline_for_sprite(detailsCmd, 2); + // need to come up with some way to extract stdout/stderr stream if we want to + // fully test this module + ASSERT_EQ(result, 1); +} + +TEST_F(CommandLineTests, cmdline_cmdline_for_sprite_build) +{ + std::string manifestFilePath = ManifestFilePath(); + std::string outputfilePath = BuildOutputfilePath(); + const char* detailsCmd[3] = { "build", outputfilePath.c_str(), manifestFilePath.c_str() }; + + int32_t result = cmdline_for_sprite(detailsCmd, 3); + ASSERT_EQ(result, 1); + // compare the resulting output file and assert its identical to expected + ASSERT_TRUE(CompareSpriteFiles(ExampleSpriteFilePath(), outputfilePath)); +} + +TEST_F(CommandLineTests, cmdline_cmdline_for_sprite_failed_build) +{ + // run on correct manifest file + std::string manifestFilePath = ManifestFilePath(); + std::string outputfilePath = BuildOutputfilePath(); + const char* detailsCmd[3] = { "build", outputfilePath.c_str(), manifestFilePath.c_str() }; + int32_t result = cmdline_for_sprite(detailsCmd, 3); + ASSERT_EQ(result, 1); + ASSERT_TRUE(CompareSpriteFiles(ExampleSpriteFilePath(), outputfilePath)); + + // now use bad manifest and make sure output file is not edited + std::string badManifestFilePath = BadManifestFilePath(); + detailsCmd[2] = badManifestFilePath.c_str(); + result = cmdline_for_sprite(detailsCmd, 3); + // check the command failed + ASSERT_EQ(result, -1); + // validate the target file was unchanged + ASSERT_TRUE(CompareSpriteFiles(ExampleSpriteFilePath(), outputfilePath)); +} diff --git a/test/tests/testdata/sprites/badManifest.json b/test/tests/testdata/sprites/badManifest.json new file mode 100644 index 0000000000..b3e46d3469 --- /dev/null +++ b/test/tests/testdata/sprites/badManifest.json @@ -0,0 +1,8 @@ +[ + { + "path": "../images/logo.png" + }, + { + "path": "../images/FAKEFILE.png" + } +] diff --git a/test/tests/testdata/sprites/example.dat b/test/tests/testdata/sprites/example.dat new file mode 100644 index 0000000000000000000000000000000000000000..c81bf87a4234626e337566b66771847ab8a5710e GIT binary patch literal 8587 zcmZQ%U|=X#WB`K(h6XUr0wNh%8Mzqw8ATZ781)!k7!w#985c7iV!X@vg^`Czjme2A zfvJIM5z{fIH%ubTcFbAKGnh{>|6m^n(wivc0Z132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zyy zEMaC2(s7+@iW$gf+_r1hK#YReG07a{0xr8XV6`AIZmtOyAQQOkKtj^sAnCIN>E+5c tGm`={daS@Iq@=*^?6L+?;Kak#VFOmc#l_WT3linJ-eLz5X|m_y0sswAFBAX( literal 0 HcmV?d00001 diff --git a/test/tests/testdata/sprites/manifest.json b/test/tests/testdata/sprites/manifest.json new file mode 100644 index 0000000000..5af8f76b68 --- /dev/null +++ b/test/tests/testdata/sprites/manifest.json @@ -0,0 +1,5 @@ +[ + { + "path": "../images/logo.png" + } +] diff --git a/test/tests/testdata/sprites/result.dat b/test/tests/testdata/sprites/result.dat new file mode 100644 index 0000000000000000000000000000000000000000..1b1cb4d44c57c2d7a5122870fa6ac3e62ff7e94e GIT binary patch literal 8 JcmZR80ssIA00961 literal 0 HcmV?d00001 diff --git a/test/tests/tests.vcxproj b/test/tests/tests.vcxproj index 574ac7a22b..d192c57318 100644 --- a/test/tests/tests.vcxproj +++ b/test/tests/tests.vcxproj @@ -57,6 +57,7 @@ + @@ -78,5 +79,10 @@ + + + + + \ No newline at end of file