initial commit
This commit is contained in:
commit
47fd4d6d0c
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
bin/
|
||||
.vscode/
|
||||
CMakeFiles/
|
||||
*.cmake
|
||||
CMakeCache.txt
|
||||
Makefile
|
||||
*.sh
|
||||
12
CMakeLists.txt
Normal file
12
CMakeLists.txt
Normal file
@ -0,0 +1,12 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(warhammer2-save-manager)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
|
||||
set(GCC_COMPILE_FLAGS "-Wall -O3")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COMPILE_FLAGS}")
|
||||
|
||||
file(GLOB SOURCES "${CMAKE_BINARY_DIR}/src/*.cpp")
|
||||
add_executable(warhammer2-save-manager ${SOURCES})
|
||||
9
LICENSE
Normal file
9
LICENSE
Normal file
@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 ryuku (ryuku@ennui.software - https://git.ennui.software/)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
13
README.md
Normal file
13
README.md
Normal file
@ -0,0 +1,13 @@
|
||||
# warhammer2-save-manager
|
||||
|
||||
intended to be used to keep backup legendary saves for recovery in case of corruption
|
||||
|
||||
## wh2 save directories
|
||||
* Proton `<path/to/>SteamLibrary/steamapps/compatdata/594570/pfx/drive_c/users/steamuser/Application Data/The Creative Assembly/Warhammer2/save_games/`
|
||||
* Linux `/home/<user>/.local/share/Steam/userdata/<steamID3>/594570/local/Warhammer2/save_games/`
|
||||
|
||||
## steamID3
|
||||
|
||||
can be obtained from https://steamidfinder.com/
|
||||
you only want the end number: [U:1:**12345678**] `.../Steam/userdata/12345678/594570/...`
|
||||
alternatively, if there's only ever been a single Steam user, you can browse to `/home/<user>/.local/share/Steam/userdata/` and the non-zero directory is your steamID3
|
||||
26
src/common.h
Normal file
26
src/common.h
Normal file
@ -0,0 +1,26 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2021 ryuku (ryuku@ennui.software - https://git.ennui.software/)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <regex>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
namespace chrono = std::chrono;
|
||||
21
src/defines.h
Normal file
21
src/defines.h
Normal file
@ -0,0 +1,21 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2021 ryuku (ryuku@ennui.software - https://git.ennui.software/)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define MONITOR_DELAY 60000
|
||||
#define MAX_NUMBER_SAVES 5
|
||||
#define DELETE_ENABLED
|
||||
111
src/file.cpp
Normal file
111
src/file.cpp
Normal file
@ -0,0 +1,111 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2021 ryuku (ryuku@ennui.software - https://git.ennui.software/)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "file.h"
|
||||
#include "defines.h"
|
||||
|
||||
bool File::Validate(const std::string& path, File& file, const bool& isBackup)
|
||||
{
|
||||
if (!fs::exists(path) || !fs::is_regular_file(path)) return false;
|
||||
|
||||
#define FILE_REGEX R"(^.+\(Legendary\)\..+\.(\d+\.\d+)\.save)"
|
||||
static std::regex validBackup(FILE_REGEX R"(\.\d+-\d+$)");
|
||||
static std::regex validSave(FILE_REGEX "$");
|
||||
static std::smatch regMatch;
|
||||
|
||||
if (!std::regex_search(path, regMatch, isBackup ? validBackup : validSave) || regMatch.size() != 2) return false;
|
||||
|
||||
file.path = path;
|
||||
file.writeTime = fs::last_write_time(path);
|
||||
file.uid = regMatch[1].str();
|
||||
return true;
|
||||
}
|
||||
|
||||
const std::string& File::Path() { return path; }
|
||||
const char* File::CPath() { return path.c_str(); }
|
||||
const std::string& File::UID() { return uid; }
|
||||
|
||||
const fs::file_time_type& File::WriteTime() { return writeTime; }
|
||||
chrono::system_clock::time_point File::SystemWriteTime() { return chrono::time_point_cast<chrono::system_clock::duration>(writeTime - fs::file_time_type::clock::now() + chrono::system_clock::now()); }
|
||||
|
||||
std::string File::WriteTimeStr()
|
||||
{
|
||||
time_t tt = chrono::system_clock::to_time_t(SystemWriteTime());
|
||||
|
||||
std::string time(30, '\0');
|
||||
return time.substr(0, std::strftime(time.data(), time.size(), "%d%m%y-%H%M%S", std::localtime(&tt)));
|
||||
}
|
||||
|
||||
std::string File::Name()
|
||||
{
|
||||
size_t pos;
|
||||
if ((pos = path.find_last_of('/')) != std::string::npos || (pos = path.find_last_of('\\')) != std::string::npos) return path.substr(pos + 1);
|
||||
return path;
|
||||
}
|
||||
|
||||
std::string File::NameNoTime()
|
||||
{
|
||||
static std::regex noTimeRegex(R"((.+\.save)\.\d+-\d+)");
|
||||
static std::smatch regMatch;
|
||||
|
||||
std::string fileName = Name();
|
||||
if (!std::regex_search(fileName, regMatch, noTimeRegex) || regMatch.size() != 2) return fileName;
|
||||
return regMatch[1].str();
|
||||
}
|
||||
|
||||
bool File::Backup(const std::string& backupPath, File& outFile)
|
||||
{
|
||||
if (!fs::exists(backupPath) || !fs::is_directory(backupPath)) throw std::runtime_error("error: File::backup got invalid backup path");
|
||||
|
||||
// copy file
|
||||
std::string backupFile = backupPath + Name() + "." + WriteTimeStr();
|
||||
if (!fs::copy_file(path, backupFile, fs::copy_options::skip_existing))
|
||||
{
|
||||
printf("copy_fail: %s\n", backupFile.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
// ensure write time matches
|
||||
fs::last_write_time(backupFile, writeTime);
|
||||
|
||||
// validate copy
|
||||
if (!File::Validate(backupFile, outFile, true))
|
||||
{
|
||||
#ifdef DELETE_ENABLED
|
||||
if (fs::exists(backupFile) && fs::is_regular_file(backupFile)) fs::remove(backupFile);
|
||||
#endif
|
||||
|
||||
printf("backup_fail: %s\n", backupFile.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
File::REMERR File::Remove()
|
||||
{
|
||||
if (!fs::exists(path)) return REMERR::FILE_NOT_EXIST;
|
||||
if (!fs::is_regular_file(path)) return REMERR::NOT_REG_FILE;
|
||||
|
||||
#ifdef DELETE_ENABLED
|
||||
#warning DELETE_ENABLED is defined, files will be deleted
|
||||
return (File::REMERR)fs::remove(path);
|
||||
#else
|
||||
#warning DELETE_ENABLED is not defined, files will not be deleted
|
||||
printf("File::Remove: %s\n", path.c_str());
|
||||
return REMERR::REMOVE_SUCCESS;
|
||||
#endif
|
||||
}
|
||||
56
src/file.h
Normal file
56
src/file.h
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2021 ryuku (ryuku@ennui.software - https://git.ennui.software/)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
class File
|
||||
{
|
||||
protected:
|
||||
std::string path;
|
||||
std::string uid;
|
||||
fs::file_time_type writeTime;
|
||||
|
||||
public:
|
||||
static bool Validate(const std::string& path, File& file, const bool& isBackup);
|
||||
|
||||
File() = default;
|
||||
File(const std::string& file);
|
||||
File(const std::string& file, const fs::file_time_type& wt);
|
||||
|
||||
const std::string& Path();
|
||||
const char* CPath();
|
||||
const std::string& UID();
|
||||
|
||||
const fs::file_time_type& WriteTime();
|
||||
chrono::system_clock::time_point SystemWriteTime();
|
||||
std::string WriteTimeStr();
|
||||
|
||||
std::string Name();
|
||||
std::string NameNoTime();
|
||||
|
||||
bool Backup(const std::string& backupPath, File& outFile);
|
||||
|
||||
enum REMERR
|
||||
{
|
||||
FILE_NOT_EXIST = -2,
|
||||
NOT_REG_FILE,
|
||||
REMOVE_FAILED,
|
||||
REMOVE_SUCCESS
|
||||
};
|
||||
REMERR Remove();
|
||||
};
|
||||
250
src/main.cpp
Normal file
250
src/main.cpp
Normal file
@ -0,0 +1,250 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2021 ryuku (ryuku@ennui.software - https://git.ennui.software/)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "defines.h"
|
||||
|
||||
#include <thread>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <cerrno>
|
||||
#include <climits>
|
||||
|
||||
#ifndef MONITOR_DELAY
|
||||
#error MONITOR_DELAY needs to be defined
|
||||
#elif MONITOR_DELAY < 10000
|
||||
#error MONITOR_DELAY minimum is 10000
|
||||
#endif
|
||||
|
||||
#ifndef MAX_NUMBER_SAVES
|
||||
#error MAX_NUMBER_SAVES needs to be defined
|
||||
#elif MAX_NUMBER_SAVES < 1
|
||||
#error MAX_NUMBER_SAVES minimum is 1
|
||||
#elif MAX_NUMBER_SAVES > 20
|
||||
#warning MAX_NUMBER_SAVES is above 20, this can/will lead to large disk space usage.
|
||||
#endif
|
||||
|
||||
int errorMsg(const char* fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
printf(fmt, args);
|
||||
va_end(args);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int printSyntax(const char* binName)
|
||||
{
|
||||
return errorMsg(
|
||||
"syntax: %s [options] [wh2 save dir] [backup dir]\n"
|
||||
"-- options --\n"
|
||||
"%s -cmu <hours> <wh2 save dir> <backup dir> - clean missing uids aged <hours> and older\n",
|
||||
binName
|
||||
);
|
||||
}
|
||||
|
||||
bool appendSlash(std::string& path)
|
||||
{
|
||||
size_t slashPos;
|
||||
char slash;
|
||||
|
||||
if ((slashPos = path.find_last_of('/')) != std::string::npos) slash = '/';
|
||||
else if ((slashPos = path.find_last_of('\\')) != std::string::npos) slash = '\\';
|
||||
|
||||
if (slashPos == std::string::npos) return false;
|
||||
if (slashPos != path.length() - 1) path += slash;
|
||||
return true;
|
||||
}
|
||||
|
||||
void InitBackups()
|
||||
{
|
||||
std::uintmax_t backupSize = 0;
|
||||
std::uintmax_t missingSize = 0;
|
||||
|
||||
// organize backup files into UID groups
|
||||
for (const auto& f : fs::recursive_directory_iterator(backupPath))
|
||||
{
|
||||
// validate file
|
||||
File file;
|
||||
if (!File::Validate(f.path().string(), file, true)) continue;
|
||||
|
||||
// create uid group if necessary
|
||||
std::string uid = file.UID();
|
||||
if (uidGroups.find(uid) == uidGroups.end())
|
||||
{
|
||||
// if save in savePath is missing, add to missing uids
|
||||
std::string noTime = file.NameNoTime();
|
||||
if (!fs::exists(savePath + noTime))
|
||||
{
|
||||
if (missingUids.find(uid) == missingUids.end()) missingUids[uid] = std::vector<File>();
|
||||
missingUids[uid].push_back(file);
|
||||
missingSize += fs::file_size(file.Path());
|
||||
continue;
|
||||
}
|
||||
|
||||
UIDGroup group;
|
||||
if (!UIDGroup::Validate(savePath + noTime, group, false)) continue;
|
||||
uidGroups[uid] = group;
|
||||
}
|
||||
|
||||
uidGroups[uid].SortedInsert(file);
|
||||
backupSize += fs::file_size(file.Path());
|
||||
}
|
||||
|
||||
printf(
|
||||
"found %li uids\n"
|
||||
"found %li missing uids\n"
|
||||
"size of valid backups: %lu MiB\n"
|
||||
"size of missing uids: %lu MiB\n",
|
||||
uidGroups.size(), missingUids.size(), backupSize > 0 ? (backupSize / 1024) / 1024 : 0, missingSize > 0 ? (missingSize / 1024) / 1024 : 0
|
||||
);
|
||||
}
|
||||
|
||||
void MonitorSaves()
|
||||
{
|
||||
chrono::duration<int, std::milli> monitorDelay = chrono::milliseconds(MONITOR_DELAY);
|
||||
for (;; std::this_thread::sleep_for(monitorDelay))
|
||||
{
|
||||
// monitor save creation/modification
|
||||
for (auto f : fs::recursive_directory_iterator(savePath))
|
||||
{
|
||||
// make sure file is valid
|
||||
File file;
|
||||
if (!File::Validate(f.path().string(), file, false)) continue;
|
||||
|
||||
// check for existing group
|
||||
// skip unmodified files if group is found
|
||||
std::string uid = file.UID();
|
||||
if (uidGroups.find(uid) == uidGroups.end())
|
||||
{
|
||||
// if not a missing uid, create group
|
||||
// otherwise init with matching backups
|
||||
auto it = missingUids.find(uid);
|
||||
if (it == missingUids.end()) uidGroups[uid] = UIDGroup(file);
|
||||
else
|
||||
{
|
||||
printf("\e[0;36mfound_uid\e[0m: %s\n", uid.c_str());
|
||||
uidGroups[uid] = UIDGroup(file, backupPath);
|
||||
missingUids.erase(it);
|
||||
}
|
||||
}
|
||||
else if (file.WriteTime() == uidGroups[uid].GetSaveFile().WriteTime()) continue;
|
||||
uidGroups[uid].SetSaveFile(file);
|
||||
|
||||
// backup file
|
||||
if (!file.Backup(backupPath, file)) continue;
|
||||
uidGroups[uid].NewestInsert(file);
|
||||
printf("\e[0;32mbackup\e[0m: %s\n", uidGroups[uid].GetBackups().front().CPath());
|
||||
}
|
||||
|
||||
// monitor save deletion
|
||||
for (auto it = uidGroups.begin(); it != uidGroups.end();)
|
||||
{
|
||||
// move deleted uids into missingUids
|
||||
if (!fs::exists(it->second.GetSaveFile().Path()))
|
||||
{
|
||||
std::string uid = it->second.GetSaveFile().UID();
|
||||
printf("\e[0;33mmissing_uid\e[0m: %s\n", uid.c_str());
|
||||
missingUids[uid] = uidGroups[uid].GetBackups();
|
||||
it = uidGroups.erase(it);
|
||||
continue;
|
||||
}
|
||||
|
||||
it++;
|
||||
}
|
||||
|
||||
// clean missing uids
|
||||
if (cleanHours < 1) continue;
|
||||
for (auto mIt = missingUids.begin(); mIt != missingUids.end();)
|
||||
{
|
||||
// delete oldest backups first
|
||||
for (auto it = mIt->second.end() - 1; mIt->second.size() > 0;)
|
||||
{
|
||||
long age = chrono::duration_cast<chrono::hours>(chrono::system_clock::now() - it->SystemWriteTime()).count();
|
||||
if (age < cleanHours) break;
|
||||
|
||||
switch (it->Remove())
|
||||
{
|
||||
case File::REMERR::NOT_REG_FILE:
|
||||
mIt->second.clear();
|
||||
printf("cmu_error: non-regular file found, missing uid has been discarded\n");
|
||||
break;
|
||||
|
||||
case File::REMERR::FILE_NOT_EXIST:
|
||||
printf("cmu_missing_file: %s\n", it->CPath());
|
||||
mIt->second.erase(it);
|
||||
it = mIt->second.end() - 1;
|
||||
break;
|
||||
|
||||
case File::REMERR::REMOVE_SUCCESS:
|
||||
printf("\e[0;31mcmu_delete\e[0m: %lih %s\n", age, it->CPath());
|
||||
mIt->second.erase(it);
|
||||
it = mIt->second.end() - 1;
|
||||
break;
|
||||
|
||||
case File::REMERR::REMOVE_FAILED:
|
||||
printf("cmu_delete_fail: %s\n", it->CPath());
|
||||
it--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (mIt->second.size() == 0) mIt = missingUids.erase(mIt);
|
||||
else mIt++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
printf("warhammer2 save manager - ryuku@ennui.software - https://git.ennui.software/ryuku/warhammer2-save-manager\n");
|
||||
|
||||
if (argc < 3 || argc > 5) return printSyntax(argv[0]);
|
||||
|
||||
savePath = argv[1];
|
||||
backupPath = argv[2];
|
||||
|
||||
// clean missing uids option
|
||||
if (savePath == "-cmu")
|
||||
{
|
||||
if (argc < 5) return printSyntax(argv[0]);
|
||||
|
||||
// safe convert arg to long
|
||||
errno = 0;
|
||||
char* endptr;
|
||||
cleanHours = strtol(backupPath.c_str(), &endptr, 0);
|
||||
if (endptr == backupPath.c_str() || errno == ERANGE || cleanHours <= 0) return printSyntax(argv[0]);
|
||||
|
||||
savePath = argv[3];
|
||||
backupPath = argv[4];
|
||||
}
|
||||
|
||||
// verify directories
|
||||
if (savePath == backupPath) return errorMsg("error: save directory and backup directory are the same\n");
|
||||
if (!fs::exists(savePath)) return errorMsg("error: save directory does not exist\n");
|
||||
if (!fs::is_directory(savePath)) return errorMsg("error: save directory is not a directory\n");
|
||||
if (!appendSlash(savePath)) return errorMsg("error: could not determine path format from save directory\n");
|
||||
if (!fs::exists(backupPath)) return errorMsg("error: backup directory does not exist\n");
|
||||
if (!fs::is_directory(backupPath)) return errorMsg("error: backup directory is not a directory\n");
|
||||
if (!appendSlash(backupPath)) return errorMsg("error: could not determine path format from backup directory\n");
|
||||
|
||||
InitBackups();
|
||||
if (cleanHours > 0) printf("cleaning missing uids aged %lih and older\n", cleanHours);
|
||||
|
||||
MonitorSaves();
|
||||
return 0;
|
||||
}
|
||||
27
src/main.h
Normal file
27
src/main.h
Normal file
@ -0,0 +1,27 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2021 ryuku (ryuku@ennui.software - https://git.ennui.software/)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include "file.h"
|
||||
#include "uidgroup.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
long cleanHours = 0;
|
||||
std::string savePath;
|
||||
std::string backupPath;
|
||||
std::map<std::string, UIDGroup> uidGroups;
|
||||
std::map<std::string, std::vector<File>> missingUids;
|
||||
79
src/uidgroup.cpp
Normal file
79
src/uidgroup.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2021 ryuku (ryuku@ennui.software - https://git.ennui.software/)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "uidgroup.h"
|
||||
#include "defines.h"
|
||||
|
||||
UIDGroup::UIDGroup(File& file) : saveFile(file) { }
|
||||
UIDGroup::UIDGroup(File& file, const std::string& backupPath) : saveFile(file)
|
||||
{
|
||||
if (!fs::exists(backupPath) || !fs::is_directory(backupPath)) throw std::runtime_error("error: UIDGroup::UIDGroup got invalid backup path");
|
||||
|
||||
// find existing backups with matching UID
|
||||
for (const auto& f : fs::recursive_directory_iterator(backupPath))
|
||||
{
|
||||
File currFile;
|
||||
if (!File::Validate(f.path().string(), file, true) || currFile.UID() != file.UID()) continue;
|
||||
SortedInsert(currFile);
|
||||
}
|
||||
}
|
||||
|
||||
bool UIDGroup::Validate(const std::string& path, UIDGroup& group, const bool& isBackup)
|
||||
{
|
||||
if (!File::Validate(path, group.saveFile, isBackup)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
File UIDGroup::GetSaveFile() { return saveFile; }
|
||||
void UIDGroup::SetSaveFile(File& file) { saveFile = file; }
|
||||
|
||||
std::vector<File> UIDGroup::GetBackups() { return backups; }
|
||||
|
||||
void UIDGroup::SortedInsert(File& file)
|
||||
{
|
||||
auto it = backups.begin();
|
||||
while (it != backups.end())
|
||||
{
|
||||
if (file.WriteTime() > it->WriteTime()) break;
|
||||
it++;
|
||||
}
|
||||
|
||||
LimitedInsert(file, it);
|
||||
}
|
||||
|
||||
void UIDGroup::NewestInsert(File& file) { LimitedInsert(file, backups.begin()); }
|
||||
|
||||
void UIDGroup::LimitedInsert(File& file, const std::vector<File>::iterator& it)
|
||||
{
|
||||
backups.insert(it, file);
|
||||
if (backups.size() <= MAX_NUMBER_SAVES) return;
|
||||
|
||||
File oldFile = backups.back();
|
||||
switch (oldFile.Remove())
|
||||
{
|
||||
case File::REMERR::REMOVE_FAILED:
|
||||
printf("old_delete_fail: %s\n", oldFile.CPath());
|
||||
break;
|
||||
|
||||
case File::REMERR::REMOVE_SUCCESS:
|
||||
printf("\e[0;31mold_delete\e[0m: %s\n", oldFile.CPath());
|
||||
|
||||
case File::REMERR::NOT_REG_FILE:
|
||||
case File::REMERR::FILE_NOT_EXIST:
|
||||
backups.pop_back();
|
||||
break;
|
||||
}
|
||||
}
|
||||
45
src/uidgroup.h
Normal file
45
src/uidgroup.h
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2021 ryuku (ryuku@ennui.software - https://git.ennui.software/)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
#include "file.h"
|
||||
|
||||
class UIDGroup
|
||||
{
|
||||
protected:
|
||||
File saveFile;
|
||||
std::vector<File> backups;
|
||||
|
||||
public:
|
||||
UIDGroup() = default;
|
||||
UIDGroup(File& file);
|
||||
UIDGroup(File& file, const std::string& backupPath);
|
||||
|
||||
static bool Validate(const std::string& path, UIDGroup& group, const bool& isBackup = false);
|
||||
|
||||
File GetSaveFile();
|
||||
void SetSaveFile(File& file);
|
||||
|
||||
std::vector<File> GetBackups();
|
||||
|
||||
void SortedInsert(File& file);
|
||||
void NewestInsert(File& file);
|
||||
|
||||
private:
|
||||
void LimitedInsert(File& file, const std::vector<File>::iterator& it);
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user