SaveTool: use Logger.

This commit is contained in:
Guillaume Jacquemin 2022-11-22 10:34:25 +01:00
parent c3a9c8dd31
commit 8625f8835c
3 changed files with 64 additions and 48 deletions

View file

@ -35,6 +35,7 @@
#include <wtsapi32.h> #include <wtsapi32.h>
#include "../FontAwesome/IconsFontAwesome5.h" #include "../FontAwesome/IconsFontAwesome5.h"
#include "../Logger/Logger.h"
using namespace Containers::Literals; using namespace Containers::Literals;
@ -53,6 +54,7 @@ SaveTool::SaveTool(const Arguments& arguments):
tweak.enable(""_s, "../../"_s); tweak.enable(""_s, "../../"_s);
#endif #endif
LOG_INFO("Configuring OpenGL renderer.");
GL::Renderer::enable(GL::Renderer::Feature::Blending); GL::Renderer::enable(GL::Renderer::Feature::Blending);
GL::Renderer::enable(GL::Renderer::Feature::ScissorTest); GL::Renderer::enable(GL::Renderer::Feature::ScissorTest);
GL::Renderer::disable(GL::Renderer::Feature::FaceCulling); GL::Renderer::disable(GL::Renderer::Feature::FaceCulling);
@ -62,25 +64,21 @@ SaveTool::SaveTool(const Arguments& arguments):
GL::Renderer::setBlendEquation(GL::Renderer::BlendEquation::Add, GL::Renderer::setBlendEquation(GL::Renderer::BlendEquation::Add,
GL::Renderer::BlendEquation::Add); GL::Renderer::BlendEquation::Add);
Utility::Debug{} << "Renderer initialisation successful."; LOG_INFO("Configuring SDL2.");
Utility::Debug{} << "===Configuring SDL2===";
{
Utility::Debug d{};
d << "Enabling clickthrough...";
if(SDL_VERSION_ATLEAST(2, 0, 5)) { if(SDL_VERSION_ATLEAST(2, 0, 5)) {
if(SDL_SetHintWithPriority(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1", SDL_HINT_OVERRIDE) == SDL_TRUE) { if(SDL_SetHintWithPriority(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1", SDL_HINT_OVERRIDE) == SDL_TRUE) {
d << "success!"_s; LOG_INFO("Clickthrough is enabled.");
} else {
d << "error: hint couldn't be set."_s;
} }
} else { else {
d << "error: SDL2 is too old (version < 2.0.5)."_s; LOG_WARNING("Clickthrough is disabled.");
} }
} }
else {
LOG_WARNING_FORMAT("Clickthrough is disabled: SDL2 version is too old ({}.{}.{})",
SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL);
}
LOG_INFO("Registering custom events.");
if((_initEventId = SDL_RegisterEvents(3)) == UnsignedInt(-1)) { if((_initEventId = SDL_RegisterEvents(3)) == UnsignedInt(-1)) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error",
"SDL_RegisterEvents() failed in SaveTool::SaveTool(). Exiting...", window()); "SDL_RegisterEvents() failed in SaveTool::SaveTool(). Exiting...", window());
@ -91,26 +89,27 @@ SaveTool::SaveTool(const Arguments& arguments):
_updateEventId = _initEventId + 1; _updateEventId = _initEventId + 1;
_fileEventId = _initEventId + 2; _fileEventId = _initEventId + 2;
LOG_INFO("Initialising the timer subsystem.");
if(SDL_InitSubSystem(SDL_INIT_TIMER) != 0) { if(SDL_InitSubSystem(SDL_INIT_TIMER) != 0) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error initialising the app", SDL_GetError(), window()); LOG_ERROR(SDL_GetError());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error initialising the app",
SDL_GetError(), window());
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
return; return;
} }
Utility::Debug{} << "SDL2 configuration successful.";
Utility::Debug{} << "===Initialising the Save Tool===";
initialiseGui(); initialiseGui();
if(!initialiseToolDirectories()) { if(!initialiseToolDirectories()) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error initialising the app", _lastError.data(), window()); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error initialising the app",
_lastError.data(), window());
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
return; return;
} }
if(!findGameDataDirectory()) { if(!findGameDataDirectory()) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error initialising the app", _lastError.data(), window()); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error initialising the app",
_lastError.data(), window());
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
return; return;
} }
@ -122,6 +121,7 @@ SaveTool::SaveTool(const Arguments& arguments):
return interval; return interval;
}, this); }, this);
if(_gameCheckTimerId == 0) { if(_gameCheckTimerId == 0) {
LOG_ERROR(SDL_GetError());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", SDL_GetError(), window()); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", SDL_GetError(), window());
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
return; return;
@ -138,6 +138,7 @@ SaveTool::SaveTool(const Arguments& arguments):
break; break;
} }
LOG_INFO("Initialising update checker.");
curl_global_init(CURL_GLOBAL_DEFAULT); curl_global_init(CURL_GLOBAL_DEFAULT);
if(_checkUpdatesOnStartup) { if(_checkUpdatesOnStartup) {
_queue.addToast(Toast::Type::Default, "Checking for updates..."_s); _queue.addToast(Toast::Type::Default, "Checking for updates..."_s);
@ -150,9 +151,6 @@ SaveTool::SaveTool(const Arguments& arguments):
GL::DebugOutput::setEnabled(GL::DebugOutput::Source::Api, GL::DebugOutput::Type::Other, {131185}, false); GL::DebugOutput::setEnabled(GL::DebugOutput::Source::Api, GL::DebugOutput::Type::Other, {131185}, false);
} }
Utility::Debug{} << "Initialisation successful.";
Utility::Debug{} << "===Running main loop===";
if(_skipDisclaimer) { if(_skipDisclaimer) {
_uiState = UiState::Initialising; _uiState = UiState::Initialising;
_initThread = std::thread{[this]{ initialiseManager(); }}; _initThread = std::thread{[this]{ initialiseManager(); }};
@ -160,14 +158,14 @@ SaveTool::SaveTool(const Arguments& arguments):
} }
SaveTool::~SaveTool() { SaveTool::~SaveTool() {
Utility::Debug{} << "===Perfoming cleanup==="; LOG_INFO("Cleaning up.");
Utility::Debug{} << "Shutting libcurl down..."; LOG_INFO("Shutting libcurl down.");
curl_global_cleanup(); curl_global_cleanup();
SDL_RemoveTimer(_gameCheckTimerId); SDL_RemoveTimer(_gameCheckTimerId);
Utility::Debug{} << "Saving the configuration..."; LOG_INFO("Saving the configuration.");
_conf.setValue("cheat_mode"_s, _cheatMode); _conf.setValue("cheat_mode"_s, _cheatMode);
_conf.setValue("startup_update_check"_s, _checkUpdatesOnStartup); _conf.setValue("startup_update_check"_s, _checkUpdatesOnStartup);
@ -186,7 +184,7 @@ SaveTool::~SaveTool() {
_conf.save(); _conf.save();
Utility::Debug{} << "Exiting..."; LOG_INFO("Exiting.");
} }
void SaveTool::drawEvent() { void SaveTool::drawEvent() {

View file

@ -26,6 +26,7 @@
#include "../FontAwesome/IconsFontAwesome5.h" #include "../FontAwesome/IconsFontAwesome5.h"
#include "../FontAwesome/IconsFontAwesome5Brands.h" #include "../FontAwesome/IconsFontAwesome5Brands.h"
#include "../Logger/Logger.h"
#include "SaveTool.h" #include "SaveTool.h"
@ -48,7 +49,7 @@ void SaveTool::initEvent(SDL_Event& event) {
} }
void SaveTool::initialiseConfiguration() { void SaveTool::initialiseConfiguration() {
Utility::Debug{} << "Reading configuration file..."; LOG_INFO("Reading configuration file.");
if(_conf.hasValue("cheat_mode"_s)) { if(_conf.hasValue("cheat_mode"_s)) {
_cheatMode = _conf.value<bool>("cheat_mode"_s); _cheatMode = _conf.value<bool>("cheat_mode"_s);
@ -88,7 +89,7 @@ void SaveTool::initialiseConfiguration() {
} }
void SaveTool::initialiseGui() { void SaveTool::initialiseGui() {
Utility::Debug{} << "Initialising ImGui..."; LOG_INFO("Initialising Dear ImGui.");
ImGui::CreateContext(); ImGui::CreateContext();
@ -138,6 +139,8 @@ void SaveTool::initialiseGui() {
} }
void SaveTool::initialiseManager() { void SaveTool::initialiseManager() {
LOG_INFO("Initialising the profile manager.");
SDL_Event event; SDL_Event event;
SDL_zero(event); SDL_zero(event);
event.type = _initEventId; event.type = _initEventId;
@ -154,7 +157,7 @@ void SaveTool::initialiseManager() {
} }
auto SaveTool::initialiseToolDirectories() -> bool { auto SaveTool::initialiseToolDirectories() -> bool {
Utility::Debug{} << "Initialising Save Tool directories..."; LOG_INFO("Initialising Save Tool directories.");
_backupsDir = Utility::Path::join(Utility::Path::split(*Utility::Path::executableLocation()).first(), "backups"); _backupsDir = Utility::Path::join(Utility::Path::split(*Utility::Path::executableLocation()).first(), "backups");
_stagingDir = Utility::Path::join(Utility::Path::split(*Utility::Path::executableLocation()).first(), "staging"); _stagingDir = Utility::Path::join(Utility::Path::split(*Utility::Path::executableLocation()).first(), "staging");
@ -164,17 +167,17 @@ auto SaveTool::initialiseToolDirectories() -> bool {
//_stylesDir = Utility::Directory::join(_armouryDir, "styles"); //_stylesDir = Utility::Directory::join(_armouryDir, "styles");
if(!Utility::Path::exists(_backupsDir)) { if(!Utility::Path::exists(_backupsDir)) {
Utility::Debug{} << "Backups directory not found, creating..."; LOG_WARNING("Backups directory not found, creating...");
if(!Utility::Path::make(_backupsDir)) { if(!Utility::Path::make(_backupsDir)) {
Utility::Error{} << (_lastError = "Couldn't create the backups directory."); LOG_ERROR(_lastError = "Couldn't create the backups directory.");
return false; return false;
} }
} }
if(!Utility::Path::exists(_stagingDir)) { if(!Utility::Path::exists(_stagingDir)) {
Utility::Debug{} << "Staging directory not found, creating..."; LOG_WARNING("Staging directory not found, creating...");
if(!Utility::Path::make(_stagingDir)) { if(!Utility::Path::make(_stagingDir)) {
Utility::Error{} << (_lastError = "Couldn't create the backups directory."); LOG_ERROR(_lastError = "Couldn't create the staging directory.");
return false; return false;
} }
} }
@ -215,20 +218,21 @@ auto SaveTool::initialiseToolDirectories() -> bool {
} }
auto SaveTool::findGameDataDirectory() -> bool { auto SaveTool::findGameDataDirectory() -> bool {
Utility::Debug{} << "Searching for the game's save directory..."; LOG_INFO("Searching for the game's save directory.");
wchar_t* localappdata_path = nullptr; wchar_t* localappdata_path = nullptr;
Containers::ScopeGuard guard{localappdata_path, CoTaskMemFree}; Containers::ScopeGuard guard{localappdata_path, CoTaskMemFree};
if(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_NO_APPCONTAINER_REDIRECTION, nullptr, &localappdata_path) != S_OK) if(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_NO_APPCONTAINER_REDIRECTION, nullptr, &localappdata_path) != S_OK)
{ {
Utility::Error{} << (_lastError = "SHGetKnownFolderPath() failed in SaveTool::findGameDataDirectory()"_s); _lastError = Utility::format("SHGetKnownFolderPath() failed with error code {}.", GetLastError());
LOG_ERROR(_lastError);
return false; return false;
} }
_gameDataDir = Utility::Path::join(Utility::Path::fromNativeSeparators(Utility::Unicode::narrow(localappdata_path)), "MASS_Builder"_s); _gameDataDir = Utility::Path::join(Utility::Path::fromNativeSeparators(Utility::Unicode::narrow(localappdata_path)), "MASS_Builder"_s);
if(!Utility::Path::exists(_gameDataDir)) { if(!Utility::Path::exists(_gameDataDir)) {
Utility::Error{} << (_lastError = _gameDataDir + " wasn't found. Make sure to play the game at least once."_s); LOG_ERROR(_lastError = _gameDataDir + " wasn't found. Make sure to play the game at least once."_s);
return false; return false;
} }
@ -240,10 +244,12 @@ auto SaveTool::findGameDataDirectory() -> bool {
} }
void SaveTool::initialiseMassManager() { void SaveTool::initialiseMassManager() {
LOG_INFO("Initialising the M.A.S.S. manager.");
_massManager.emplace(_saveDir, _currentProfile->account(), _currentProfile->isDemo(), _stagingDir); _massManager.emplace(_saveDir, _currentProfile->account(), _currentProfile->isDemo(), _stagingDir);
} }
void SaveTool::initialiseFileWatcher() { void SaveTool::initialiseFileWatcher() {
LOG_INFO("Initialising the file watcher.");
_fileWatcher.emplace(); _fileWatcher.emplace();
_watchIDs[SaveDir] = _fileWatcher->addWatch(_saveDir, this, false); _watchIDs[SaveDir] = _fileWatcher->addWatch(_saveDir, this, false);
_watchIDs[StagingDir] = _fileWatcher->addWatch(_stagingDir, this, false); _watchIDs[StagingDir] = _fileWatcher->addWatch(_stagingDir, this, false);

View file

@ -20,6 +20,8 @@
#include <curl/curl.h> #include <curl/curl.h>
#include "../Logger/Logger.h"
#include "SaveTool.h" #include "SaveTool.h"
void SaveTool::updateCheckEvent(SDL_Event& event) { void SaveTool::updateCheckEvent(SDL_Event& event) {
@ -27,20 +29,26 @@ void SaveTool::updateCheckEvent(SDL_Event& event) {
if(event.user.code == CurlInitFailed) { if(event.user.code == CurlInitFailed) {
_queue.addToast(Toast::Type::Error, "Couldn't initialise libcurl. Update check aborted."_s); _queue.addToast(Toast::Type::Error, "Couldn't initialise libcurl. Update check aborted."_s);
LOG_ERROR("Couldn't initialise libcurl. Update check aborted.");
return; return;
} }
else if(event.user.code == CurlError) { else if(event.user.code == CurlError) {
Containers::String error{static_cast<char*>(event.user.data2), CURL_ERROR_SIZE, nullptr}; Containers::String error{static_cast<char*>(event.user.data2), CURL_ERROR_SIZE, nullptr};
_queue.addToast(Toast::Type::Error, error, std::chrono::milliseconds{5000}); _queue.addToast(Toast::Type::Error, error, std::chrono::milliseconds{5000});
_queue.addToast(Toast::Type::Error, static_cast<char*>(event.user.data1), std::chrono::milliseconds{5000}); _queue.addToast(Toast::Type::Error, static_cast<char*>(event.user.data1),
std::chrono::milliseconds{5000});
LOG_ERROR_FORMAT("{}: {}", static_cast<char*>(event.user.data1), static_cast<char*>(event.user.data2));
return; return;
} }
else if(event.user.code == CurlTimeout) { else if(event.user.code == CurlTimeout) {
_queue.addToast(Toast::Type::Error, "The request timed out."_s); _queue.addToast(Toast::Type::Error, "The request timed out."_s);
LOG_ERROR("The request timed out.");
return; return;
} }
else if(event.user.code != 200) { else if(event.user.code != 200) {
_queue.addToast(Toast::Type::Error, Utility::format("The request failed with error code {}", event.user.code)); _queue.addToast(Toast::Type::Error,
Utility::format("The request failed with error code {}.", event.user.code));
LOG_ERROR_FORMAT("The request failed with error code {}.", event.user.code);
return; return;
} }
@ -71,7 +79,7 @@ void SaveTool::updateCheckEvent(SDL_Event& event) {
bool prerelease = false; bool prerelease = false;
bool operator==(const Version& other) const { bool operator==(const Version& other) const {
return fullVersion == other.fullVersion; return fullVersion == other.fullVersion && prerelease == other.prerelease;
} }
bool operator>(const Version& other) const { bool operator>(const Version& other) const {
if((fullVersion > other.fullVersion) || if((fullVersion > other.fullVersion) ||
@ -90,24 +98,28 @@ void SaveTool::updateCheckEvent(SDL_Event& event) {
static const Version current_ver{SAVETOOL_VERSION}; static const Version current_ver{SAVETOOL_VERSION};
Containers::String response{static_cast<char*>(event.user.data1), strlen(static_cast<char*>(event.user.data1)), nullptr}; auto str = static_cast<char*>(event.user.data1);
Containers::String response{str, strlen(str), nullptr};
auto components = response.split('\n'); auto components = response.split('\n');
Version latest_ver{components.front()}; Version latest_ver{components.front()};
if(latest_ver > current_ver) { if(latest_ver > current_ver) {
_queue.addToast(Toast::Type::Warning, "Your version is out of date.\nCheck the settings for more information."_s, _queue.addToast(Toast::Type::Warning,
"Your version is out of date.\nCheck the settings for more information."_s,
std::chrono::milliseconds{5000}); std::chrono::milliseconds{5000});
_updateAvailable = true; _updateAvailable = true;
_latestVersion = Containers::String{latest_ver}; _latestVersion = Containers::String{latest_ver};
_releaseLink = Utility::format("https://williamjcm.ovh/git/williamjcm/MassBuilderSaveTool/releases/tag/v{}", components.front()); _releaseLink = Utility::format("https://williamjcm.ovh/git/williamjcm/MassBuilderSaveTool/releases/tag/v{}",
components.front());
_downloadLink = components.back(); _downloadLink = components.back();
} }
else if(latest_ver == current_ver || (current_ver > latest_ver && current_ver.prerelease)) { else if(latest_ver == current_ver || (current_ver > latest_ver && current_ver.prerelease)) {
_queue.addToast(Toast::Type::Success, "The application is already up to date."_s); _queue.addToast(Toast::Type::Success, "The application is already up to date."_s);
} }
else if(current_ver > latest_ver && !current_ver.prerelease) { else if(current_ver > latest_ver && !current_ver.prerelease) {
_queue.addToast(Toast::Type::Warning, "Your version is more recent than the latest one in the repo. How???"_s); _queue.addToast(Toast::Type::Warning,
"Your version is more recent than the latest one in the repo. How???"_s);
} }
} }
@ -137,7 +149,7 @@ void SaveTool::checkForUpdates() {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_body); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_body);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer.data()); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer.data());
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L); curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 0L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 10000L); curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 10000L);
auto code = curl_easy_perform(curl); auto code = curl_easy_perform(curl);