Packages can now be .zip files

This commit is contained in:
David Benepe
2022-03-26 13:47:11 -04:00
parent 2e2ef0f1f5
commit 8049d7df7e
3 changed files with 1028 additions and 983 deletions
@@ -1,5 +1,8 @@
#include "compiler.h"
// Needs to be defined here.
#include "../libs/zip_file.hpp"
const std::string BASE_DIR_NAME = "vanilla";
const std::string EXT_DIR_NAME = "packages";
@@ -27,11 +30,15 @@ AssetCompiler::AssetCompiler() {
std::vector<std::string> packageOrder = get_array_from_json(packageOrderFilepath);
for(std::string &packageName : packageOrder) {
std::cout << "Applying package: " << packageName << std::endl;
std::string packageDir = packagesDirPath + "/" + packageName;
copy_stuff_from_folders(packageDir, outDirPath, packageName);
append_manifest_files(packageDir, outDirPath, packageName);
apply_package(packagesDirPath, packageName, outDirPath);
}
}
// Remove the temp directory if it exists.
if(path_exists(packagesDirPath + "/_temp")) {
delete_directory(packagesDirPath + "/_temp");
}
std::cout << "Done!" << std::endl;
}
@@ -39,19 +46,39 @@ AssetCompiler::~AssetCompiler() {
}
// TODO: Implement more checks. Maybe add option to skip verification?
void AssetCompiler::verify_json_files(std::string &folderPath) {
std::vector<std::string> jsonFiles = get_filenames_from_directory_with_extension(folderPath, ".json");
for(std::string &jsonFilePath : jsonFiles) {
// Need to make sure that all the json files within directories have a type in them.
std::string jsonFileType = get_string_from_json(jsonFilePath, "type");
if(jsonFileType == "") {
display_error_and_abort("\"", jsonFilePath, "\" does not have a type.");
void AssetCompiler::apply_package(std::string &packagesDirPath, std::string &packageName, std::string &outDirPath) {
std::string packageDir = packagesDirPath + "/" + packageName;
bool isZip = false;
if(ends_with(packageName, ".zip")) {
packageName = packageName.substr(0, packageName.length() - 4);
isZip = true;
} else if(!path_exists(packageDir) && path_exists(packageDir + ".zip")) {
packageDir += ".zip";
isZip = true;
}
// Extract zip to a temp directory and use that directory.
if(isZip) {
std::string tempDirPath = packagesDirPath + "/_temp";
create_directory(tempDirPath);
miniz_cpp::zip_file file(packageDir);
packageDir = tempDirPath + "/" + packageName;
// Make sure a subdirectory with the name of the package exists in the temp directory.
if(!file.has_file(packageName + "/")) {
create_directory(packageDir);
file.extractall(packageDir);
} else {
file.extractall(tempDirPath);
}
}
copy_stuff_from_folders(packageDir, outDirPath);
append_manifest_files(packageDir, outDirPath);
}
void AssetCompiler::copy_stuff_from_folders(std::string &packageDir, std::string &outDirPath, std::string &packageName) {
void AssetCompiler::copy_stuff_from_folders(std::string &packageDir, std::string &outDirPath) {
std::vector<fs::path> packageFolders = get_folders_from_directory_only(packageDir);
for(int i = 0; i < packageFolders.size(); i++) {
std::string folderPath = packageFolders[i].string();
@@ -60,7 +87,7 @@ void AssetCompiler::copy_stuff_from_folders(std::string &packageDir, std::string
}
}
void AssetCompiler::append_manifest_files(std::string &packageDir, std::string &outDirPath, std::string &packageName) {
void AssetCompiler::append_manifest_files(std::string &packageDir, std::string &outDirPath) {
std::vector<fs::path> packageFiles = get_filenames_from_directory_only_with_extension(packageDir, ".json");
std::string curDataPath, newDataPath;
for(int i = 0; i < packageFiles.size(); i++) {
@@ -75,4 +102,16 @@ void AssetCompiler::append_manifest_files(std::string &packageDir, std::string &
}
}
// TODO: Implement more checks. Maybe add option to skip verification?
void AssetCompiler::verify_json_files(std::string &folderPath) {
std::vector<std::string> jsonFiles = get_filenames_from_directory_with_extension(folderPath, ".json");
for(std::string &jsonFilePath : jsonFiles) {
// Need to make sure that all the json files within directories have a type in them.
std::string jsonFileType = get_string_from_json(jsonFilePath, "type");
if(jsonFileType == "") {
display_error_and_abort("\"", jsonFilePath, "\" does not have a type.");
}
}
}
@@ -14,7 +14,8 @@ public:
AssetCompiler();
~AssetCompiler();
private:
void copy_stuff_from_folders(std::string &packagesDirPath, std::string &outDirPath, std::string &packageName);
void append_manifest_files(std::string &packagesDirPath, std::string &outDirPath, std::string &packageName);
void apply_package(std::string &packagesDirPath, std::string &packageName, std::string &outDirPath);
void copy_stuff_from_folders(std::string &packagesDirPath, std::string &outDirPath);
void append_manifest_files(std::string &packagesDirPath, std::string &outDirPath);
void verify_json_files(std::string &folderPath);
};
File diff suppressed because it is too large Load Diff