Added RemoveUnused mod example

This commit is contained in:
David
2024-04-11 13:10:18 -04:00
parent 61141c09e8
commit 60a3eef4e5
10 changed files with 108 additions and 6 deletions
+1 -2
View File
@@ -1,6 +1,5 @@
# Make `OPT` -g for better debugging info.
OPT := -O2
CC := gcc
CXX := g++
CFLAGS := -I . -Wall -Wextra -Wno-unused-parameter -pedantic -std=c99 $(OPT) -s
@@ -311,6 +311,12 @@ void FileHelper::delete_directory(const fs::path &path) {
}
}
void FileHelper::delete_file(const fs::path &path) {
if(fs::exists(path)) {
fs::remove(path);
}
}
void FileHelper::rename(const fs::path &oldPath, const fs::path &newPath) {
fs::rename(oldPath, newPath);
}
@@ -54,6 +54,7 @@ public:
static fs::path get_directory(const fs::path &path);
static void delete_directory(const fs::path &path);
static void delete_file(const fs::path &path);
static void rename(const fs::path &oldPath, const fs::path &newPath);
static void copy(const fs::path &oldPath, const fs::path &newPath, bool recursive = false);
@@ -9,6 +9,8 @@
#include "helpers/debugHelper.h"
#include "helpers/stringHelper.h"
#include "misc/settings.hpp"
// TODO: Support RapidJSON
#include "libs/json.hpp" // nlohmann JSON library. (https://github.com/nlohmann/json)
@@ -415,6 +417,54 @@ void json_merge_append(json &dst, json &patch) {
}
}
/******************************************************************/
void json_delete_file(DkrAssetsSettings &settings, json &dst, std::string filename) {
fs::path pathToAssets = settings.pathToAssets / settings.dkrVersion;
std::string folder = dst[json::json_pointer("/folder")];
fs::path fileToDelete = pathToAssets / folder / filename;
DebugHelper::info_verbose("Deleting file: ", fileToDelete);
FileHelper::delete_file(fileToDelete);
}
#define _JSON_HAS_KEY(json, key) (json.find(key) != json.end())
void json_check_for_removed_file_sections(DkrAssetsSettings &settings, json &dst, json &patch) {
json &order = dst[json::json_pointer("/files/order")];
json &sections = dst[json::json_pointer("/files/sections")];
json &patchSections = patch[json::json_pointer("/files/sections")];
if(!order.is_array() || !sections.is_object() || !patchSections.is_object()) {
// Not the correct json type, so don't bother!
return;
}
size_t index = 0;
while(index < order.size()) {
std::string key = order[index];
if(_JSON_HAS_KEY(patchSections, key)) { // Check if the section id exists in the patch
json &section = sections.at(key);
json &patchSection = patchSections.at(key);
if(_JSON_HAS_KEY(section, "filename") && _JSON_HAS_KEY(patchSection, "filename")) { // Make sure both the patch & dst have a filename.
if(!section.at("filename").is_null() && patchSection.at("filename").is_null()) { // Check if the filename becomes null
json_delete_file(settings, dst, section.at("filename")); // Delete the file if it exists.
order.erase(order.begin() + index); // Erase the index from the order.
sections.erase(key); // Erase the section.
continue;
}
}
}
index++;
}
}
#undef _JSON_HAS_KEY
/******************************************************************/
enum JsonMergeType {
MERGE_PATCH, // RFC 7386 - Arrays get overwritten.
MERGE_APPEND // Like MERGE_PATCH, but arrays get appended to.
@@ -430,7 +480,7 @@ const std::string DEFAULT_PATCH_TYPE = "patch-append";
/******************************************************************/
void JsonHelper::patch_json(const fs::path &dstPath, const fs::path &patchPath) {
void JsonHelper::patch_json(DkrAssetsSettings &settings, const fs::path &dstPath, const fs::path &patchPath) {
JsonFile *dstJsonFile = _load_json_from_cache(dstPath);
JsonFile *patchJsonFile = _load_json_from_cache(patchPath);
@@ -443,6 +493,8 @@ void JsonHelper::patch_json(const fs::path &dstPath, const fs::path &patchPath)
JSON_HELPER_DETAILS::JsonFileData *dstJson = dstJsonFile->get_data();
JSON_HELPER_DETAILS::JsonFileData *patchJson = patchJsonFile->get_data();
json_check_for_removed_file_sections(settings, dstJson->data, patchJson->data);
switch(mergeType) {
case JsonMergeType::MERGE_PATCH: // RFC 7386
// Arrays will get overwritten.
@@ -106,6 +106,8 @@ class StatJsonFile {
JSON_HELPER_DETAILS::JsonFileData *_data;
};
class DkrAssetsSettings;
/**
* Singleton class that deals with saving & loading json files.
*/
@@ -117,7 +119,7 @@ public:
}
bool get_file(fs::path filepath, JsonFile **out);
void patch_json(const fs::path &dst, const fs::path &patch);
void patch_json(DkrAssetsSettings &settings, const fs::path &dst, const fs::path &patch);
private:
std::unordered_map<std::string, JsonFile*> _fileCache;
@@ -5,7 +5,7 @@
CompileAssets::CompileAssets(DkrAssetsSettings &settings) : _settings(settings) {
_vanillaAssetsPath = _settings.pathToAssets / ".vanilla" / settings.dkrVersion;
_outAssetsPath = _settings.pathToAssets / settings.dkrVersion;
_outAssetsPath = _settings.pathToAssets / _settings.dkrVersion;
_statFile = new StatJsonFile(_settings.pathToCache / "compileAssetsCache.json");
@@ -210,7 +210,7 @@ void CompileAssets::_merge_or_copy_file(fs::path &modDir, fs::path &path) {
if(copyFile) {
FileHelper::copy(modPath, outPath);
} else { // Merge file
JsonHelper::get().patch_json(outPath, modPath);
JsonHelper::get().patch_json(_settings, outPath, modPath);
}
}
@@ -85,6 +85,12 @@ void AssetEnums::_write_single_asset_section_enums(const std::string &sectionId,
if(!fontSubfolder.empty()) {
fontJsonPath /= fontSubfolder;
}
if(sectionJson->is_value_null("/filename")) {
_cHeader.write_comment("No file");
return;
}
fontJsonPath /= sectionJson->get_string("/filename");
JsonFile *fontsJson;
DebugHelper::assert(JsonHelper::get().get_file(fontJsonPath, &fontsJson),
@@ -140,7 +146,14 @@ void AssetEnums::_write_deferred_asset_section_enums(const std::string &sectionI
std::vector<std::string> sectionOrder;
fromSectionJson->get_array<std::string>("/files/order", sectionOrder);
std::string sectionPtr = "/files/sections/";
for(std::string &assetBuildId : sectionOrder) {
// Don't include files that have null filenames.
if(fromSectionJson->is_value_null(sectionPtr + assetBuildId + "/filename")) {
continue;
}
// TODO: This only works for LevelNames.
// Will need to be modified later since Object Animations aren't one-to-one with Object Models.
sectionEnum.add_symbol(assetBuildId + idPostfix);