Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/utility/asset_packer.cpp
diff options
context:
space:
mode:
authorKae <80987908+Novaenia@users.noreply.github.com>2023-06-20 14:33:09 +1000
committerKae <80987908+Novaenia@users.noreply.github.com>2023-06-20 14:33:09 +1000
commit6352e8e3196f78388b6c771073f9e03eaa612673 (patch)
treee23772f79a7fbc41bc9108951e9e136857484bf4 /source/utility/asset_packer.cpp
parent6741a057e5639280d85d0f88ba26f000baa58f61 (diff)
everything everywhere
all at once
Diffstat (limited to 'source/utility/asset_packer.cpp')
-rw-r--r--source/utility/asset_packer.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/source/utility/asset_packer.cpp b/source/utility/asset_packer.cpp
new file mode 100644
index 0000000..7777264
--- /dev/null
+++ b/source/utility/asset_packer.cpp
@@ -0,0 +1,79 @@
+#include "StarPackedAssetSource.hpp"
+#include "StarTime.hpp"
+#include "StarJsonExtra.hpp"
+#include "StarFile.hpp"
+#include "StarVersionOptionParser.hpp"
+
+using namespace Star;
+
+int main(int argc, char** argv) {
+ try {
+ double startTime = Time::monotonicTime();
+
+ VersionOptionParser optParse;
+ optParse.setSummary("Packs asset folder into a starbound .pak file");
+ optParse.addParameter("c", "configFile", OptionParser::Optional, "JSON file with ignore lists and ordering info");
+ optParse.addSwitch("s", "Enable server mode");
+ optParse.addSwitch("v", "Verbose, list each file added");
+ optParse.addArgument("assets folder path", OptionParser::Required, "Path to the assets to be packed");
+ optParse.addArgument("output filename", OptionParser::Required, "Output pak file");
+
+ auto opts = optParse.commandParseOrDie(argc, argv);
+
+ String assetsFolderPath = opts.arguments.at(0);
+ String outputFilename = opts.arguments.at(1);
+
+ StringList ignoreFiles;
+ StringList extensionOrdering;
+ if (opts.parameters.contains("c")) {
+ String configFile = opts.parameters.get("c").first();
+ String configFileContents;
+ try {
+ configFileContents = File::readFileString(configFile);
+ } catch (IOException const& e) {
+ cerrf("Could not open specified configFile: %s\n", configFile);
+ cerrf("For the following reason: %s\n", outputException(e, false));
+ return 1;
+ }
+
+ Json configFileJson;
+ try {
+ configFileJson = Json::parseJson(configFileContents);
+ } catch (JsonParsingException const& e) {
+ cerrf("Could not parse the specified configFile: %s\n", configFile);
+ cerrf("For the following reason: %s\n", outputException(e, false));
+ return 1;
+ }
+
+ try {
+ ignoreFiles = jsonToStringList(configFileJson.get("globalIgnore", JsonArray()));
+ if (opts.switches.contains("s"))
+ ignoreFiles.appendAll(jsonToStringList(configFileJson.get("serverIgnore", JsonArray())));
+ extensionOrdering = jsonToStringList(configFileJson.get("extensionOrdering", JsonArray()));
+ } catch (JsonException const& e) {
+ cerrf("Could not read the asset_packer config file %s\n", configFile);
+ cerrf("%s\n", outputException(e, false));
+ return 1;
+ }
+ }
+
+ bool verbose = opts.parameters.contains("v");
+
+ function<void(size_t, size_t, String, String, bool)> BuildProgressCallback;
+ auto progressCallback = [verbose](size_t, size_t, String filePath, String assetPath) {
+ if (verbose)
+ coutf("Adding file '%s' to the target pak as '%s'\n", filePath, assetPath);
+ };
+
+ outputFilename = File::relativeTo(File::fullPath(File::dirName(outputFilename)), File::baseName(outputFilename));
+ DirectoryAssetSource directorySource(assetsFolderPath, ignoreFiles);
+ PackedAssetSource::build(directorySource, outputFilename, extensionOrdering, progressCallback);
+
+ coutf("Output packed assets to %s in %ss\n", outputFilename, Time::monotonicTime() - startTime);
+ return 0;
+
+ } catch (std::exception const& e) {
+ cerrf("Exception caught: %s\n", outputException(e, true));
+ return 1;
+ }
+}