MassBuilderSaveTool/src/main.cpp

106 lines
3.9 KiB
C++
Raw Normal View History

// MassBuilderSaveTool
// Copyright (C) 2021-2024 Guillaume Jacquemin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2022-11-21 09:51:21 +01:00
#include <SDL_messagebox.h>
#include <cpuid.h>
2021-07-28 15:17:46 +02:00
#include <errhandlingapi.h>
#include <synchapi.h>
#include <winerror.h>
2024-04-14 18:30:39 +02:00
#include "Application/Application.h"
#include "Configuration/Configuration.h"
2022-11-21 10:17:13 +01:00
#include "Logger/MagnumLogBuffer.h"
2024-04-14 18:30:39 +02:00
#include "Utilities/Temp.h"
2022-11-21 09:48:33 +01:00
int main(int argc, char** argv) {
2022-11-21 10:17:13 +01:00
MagnumLogBuffer debug_intercept_buffer{EntryType::Info};
MagnumLogBuffer warning_intercept_buffer{EntryType::Warning};
MagnumLogBuffer error_intercept_buffer{EntryType::Error};
std::ostream debug_intercept_stream{&debug_intercept_buffer};
std::ostream warning_intercept_stream{&warning_intercept_buffer};
std::ostream error_intercept_stream{&error_intercept_buffer};
Debug debug_intercept{&debug_intercept_stream};
Warning warning_intercept{&warning_intercept_stream};
Error error_intercept{&error_intercept_stream};
logger().initialise();
LOG_INFO("Initialising M.A.S.S. Builder Save Tool version " SAVETOOL_VERSION_STRING ".");
2022-11-21 10:17:13 +01:00
if(!mbst::conf().valid()) {
LOG_ERROR_FORMAT("There was an error initialising the app: {}", mbst::conf().lastError());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error",
mbst::conf().lastError().cbegin(), nullptr);
return EXIT_FAILURE;
}
2024-04-14 18:30:39 +02:00
mbst::Utilities::emptyTempDir();
auto str = setlocale(LC_ALL, ".utf-8");
2022-11-21 10:17:13 +01:00
if(str) {
Containers::StringView locale{str};
LOG_INFO_FORMAT("Current locale: {}", locale);
if(!locale.hasSuffix(".utf8") && !locale.hasSuffix(".65001")) {
2022-11-21 10:17:13 +01:00
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error",
2024-03-09 17:38:52 +01:00
"Your system doesn't support the UTF-8 codepage.", nullptr);
2022-11-21 10:17:13 +01:00
return EXIT_FAILURE;
}
if(locale.hasSuffix(".65001")) {
LOG_INFO("Wine detected.");
mbst::conf().setRunningInWine(true);
}
2022-11-21 09:50:43 +01:00
}
2022-03-30 14:18:23 +02:00
auto mutex_handle = CreateMutexW(nullptr, 0, L"MassBuilderSaveTool");
2021-07-28 15:17:46 +02:00
if(!mutex_handle) {
2022-11-21 10:17:13 +01:00
LOG_ERROR_FORMAT("Error initialising the instance checker. GetLastError() returned {}.", GetLastError());
2022-11-21 09:47:21 +01:00
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error",
"There was an error initialising the single-instance checker.",nullptr);
2021-07-28 15:17:46 +02:00
return EXIT_FAILURE;
}
if(GetLastError() == ERROR_ALREADY_EXISTS) {
2022-11-21 10:17:13 +01:00
LOG_ERROR("Another instance of the application was detected.");
2022-11-21 09:47:21 +01:00
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error",
2021-07-28 15:17:46 +02:00
"There can be only one running instance of the application.",nullptr);
return EXIT_FAILURE;
}
2022-11-21 09:51:21 +01:00
std::uint32_t brand[12];
if (!__get_cpuid_max(0x80000004, nullptr)) {
2022-11-21 10:17:13 +01:00
LOG_WARNING("CPUID features not supported. Can't get the current processor's model.");
2022-11-21 09:51:21 +01:00
}
else {
__get_cpuid(0x80000002, brand+0x0, brand+0x1, brand+0x2, brand+0x3);
__get_cpuid(0x80000003, brand+0x4, brand+0x5, brand+0x6, brand+0x7);
__get_cpuid(0x80000004, brand+0x8, brand+0x9, brand+0xa, brand+0xb);
2022-11-21 10:17:13 +01:00
LOG_INFO_FORMAT("Processor: {}", Containers::arrayCast<const char>(brand).data());
2022-11-21 09:51:21 +01:00
}
mbst::Application app({argc, argv});
int result = app.exec();
2021-07-28 15:17:46 +02:00
ReleaseMutex(mutex_handle);
return result;
}