2021-10-14 18:54:29 +00:00
|
|
|
/*
|
2022-01-06 23:38:21 +00:00
|
|
|
Copyright 2021-2022 Peter Repukat - FlatspotSoftware
|
2021-10-14 18:54:29 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
#ifdef _WIN32
|
2021-10-15 13:02:36 +00:00
|
|
|
#define NOMINMAX
|
2021-10-14 18:54:29 +00:00
|
|
|
#include <Windows.h>
|
2022-06-06 17:53:06 +00:00
|
|
|
#include <DbgHelp.h>
|
2021-10-14 18:54:29 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-28 10:28:20 +00:00
|
|
|
#include <spdlog/sinks/basic_file_sink.h>
|
|
|
|
#include <spdlog/sinks/stdout_color_sinks.h>
|
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
2021-10-14 18:54:29 +00:00
|
|
|
#include "SteamTarget.h"
|
|
|
|
|
2021-10-28 09:41:27 +00:00
|
|
|
#include "OverlayLogSink.h"
|
|
|
|
#include "Settings.h"
|
2022-07-13 20:53:57 +00:00
|
|
|
#include <iostream>
|
2021-10-16 20:36:30 +00:00
|
|
|
|
2021-11-04 13:44:29 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
2022-01-15 18:21:35 +00:00
|
|
|
// default to high performance GPU
|
|
|
|
extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
|
|
|
|
extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
|
|
|
|
|
2021-11-04 13:44:29 +00:00
|
|
|
LONG Win32FaultHandler(struct _EXCEPTION_POINTERS* ExInfo)
|
|
|
|
|
|
|
|
{
|
|
|
|
std::string FaultTx = "";
|
|
|
|
switch (ExInfo->ExceptionRecord->ExceptionCode) {
|
|
|
|
case EXCEPTION_ACCESS_VIOLATION:
|
|
|
|
FaultTx = "ACCESS VIOLATION";
|
|
|
|
break;
|
|
|
|
case EXCEPTION_DATATYPE_MISALIGNMENT:
|
|
|
|
FaultTx = "DATATYPE MISALIGNMENT";
|
|
|
|
break;
|
|
|
|
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
|
|
|
|
FaultTx = "FLT DIVIDE BY ZERO";
|
|
|
|
break;
|
2022-06-06 17:53:06 +00:00
|
|
|
default:
|
|
|
|
FaultTx = "(unknown)";
|
2021-11-04 13:44:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int wsFault = ExInfo->ExceptionRecord->ExceptionCode;
|
|
|
|
PVOID CodeAdress = ExInfo->ExceptionRecord->ExceptionAddress;
|
|
|
|
|
|
|
|
spdlog::error("*** A Program Fault occurred:");
|
|
|
|
spdlog::error("*** Error code {:#x}: {}", wsFault, FaultTx);
|
|
|
|
spdlog::error("*** Address: {:#x}", (int)CodeAdress);
|
|
|
|
spdlog::error("*** Flags: {:#x}", ExInfo->ExceptionRecord->ExceptionFlags);
|
|
|
|
|
2022-06-06 17:53:06 +00:00
|
|
|
MINIDUMP_EXCEPTION_INFORMATION M;
|
|
|
|
HANDLE hDump_File;
|
|
|
|
|
|
|
|
auto path = std::filesystem::temp_directory_path()
|
|
|
|
.parent_path()
|
|
|
|
.parent_path()
|
|
|
|
.parent_path();
|
|
|
|
|
|
|
|
path /= "Roaming";
|
|
|
|
path /= "GlosSI";
|
|
|
|
if (!std::filesystem::exists(path))
|
|
|
|
std::filesystem::create_directories(path);
|
|
|
|
path /= "glossitarget.dmp";
|
|
|
|
|
|
|
|
M.ThreadId = GetCurrentThreadId();
|
|
|
|
M.ExceptionPointers = ExInfo;
|
|
|
|
M.ClientPointers = 0;
|
|
|
|
|
|
|
|
hDump_File = CreateFile(path.wstring().c_str(), GENERIC_WRITE, 0,
|
|
|
|
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
|
|
|
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),
|
|
|
|
hDump_File, MiniDumpNormal,
|
|
|
|
(ExInfo) ? &M : NULL, NULL, NULL);
|
|
|
|
|
|
|
|
CloseHandle(hDump_File);
|
|
|
|
|
2021-11-04 13:44:29 +00:00
|
|
|
/*if(want to continue)
|
|
|
|
{
|
|
|
|
ExInfo->ContextRecord->Eip++;
|
|
|
|
return EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return EXCEPTION_EXECUTE_HANDLER;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-10-17 19:04:41 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#ifdef CONSOLE
|
2021-10-20 19:47:24 +00:00
|
|
|
int main(int argc, char* argv[])
|
2021-10-17 19:04:41 +00:00
|
|
|
#else
|
|
|
|
int CALLBACK WinMain(
|
2021-10-20 19:47:24 +00:00
|
|
|
_In_ HINSTANCE hInstance,
|
|
|
|
_In_ HINSTANCE hPrevInstance,
|
|
|
|
_In_ LPSTR lpCmdLine,
|
|
|
|
_In_ int nCmdShow)
|
2021-10-17 19:04:41 +00:00
|
|
|
#endif
|
|
|
|
#else
|
2021-10-20 19:47:24 +00:00
|
|
|
int main(int argc, char* argv[])
|
2021-10-17 19:04:41 +00:00
|
|
|
#endif
|
2021-10-15 15:10:57 +00:00
|
|
|
{
|
2021-10-16 20:36:30 +00:00
|
|
|
const auto console_sink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
|
|
|
|
console_sink->set_level(spdlog::level::trace);
|
|
|
|
#ifdef _WIN32
|
2021-10-23 14:57:49 +00:00
|
|
|
auto path = std::filesystem::temp_directory_path()
|
2021-10-23 15:17:05 +00:00
|
|
|
.parent_path()
|
2021-10-23 14:57:49 +00:00
|
|
|
.parent_path()
|
|
|
|
.parent_path();
|
|
|
|
|
|
|
|
path /= "Roaming";
|
|
|
|
path /= "GlosSI";
|
|
|
|
if (!std::filesystem::exists(path))
|
|
|
|
std::filesystem::create_directories(path);
|
|
|
|
path /= "glossitarget.log";
|
2022-07-26 13:35:59 +00:00
|
|
|
// For "path.wstring()" to be usable here, SPDLOG_WCHAR_FILENAMES must be defined.
|
2022-07-13 20:53:57 +00:00
|
|
|
const auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(path.wstring(), true);
|
2021-10-16 20:36:30 +00:00
|
|
|
#else
|
2022-07-13 20:53:57 +00:00
|
|
|
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(L"/tmp/glossitarget.log", true);
|
2021-10-16 20:36:30 +00:00
|
|
|
#endif
|
|
|
|
file_sink->set_level(spdlog::level::trace);
|
2021-10-22 13:40:37 +00:00
|
|
|
|
|
|
|
const auto overlay_sink = std::make_shared<spdlog::sinks::overlay_sink_mt>();
|
|
|
|
#ifdef _DEBUG
|
2021-10-29 17:51:59 +00:00
|
|
|
overlay_sink->set_level(spdlog::level::debug);
|
2021-10-22 13:40:37 +00:00
|
|
|
#else
|
|
|
|
overlay_sink->set_level(spdlog::level::info);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::vector<spdlog::sink_ptr> sinks{file_sink, console_sink, overlay_sink};
|
2021-10-16 20:36:30 +00:00
|
|
|
auto logger = std::make_shared<spdlog::logger>("log", sinks.begin(), sinks.end());
|
|
|
|
logger->set_level(spdlog::level::trace);
|
2021-11-04 16:07:30 +00:00
|
|
|
logger->flush_on(spdlog::level::trace);
|
2021-10-16 20:36:30 +00:00
|
|
|
spdlog::set_default_logger(logger);
|
2021-11-04 13:44:29 +00:00
|
|
|
SetUnhandledExceptionFilter(static_cast<LPTOP_LEVEL_EXCEPTION_FILTER>(Win32FaultHandler));
|
2021-11-04 16:07:30 +00:00
|
|
|
auto exit = 1;
|
|
|
|
try {
|
|
|
|
#ifdef _WIN32
|
2022-07-13 20:53:57 +00:00
|
|
|
int numArgs;
|
|
|
|
LPWSTR* args = CommandLineToArgvW(GetCommandLine(), &numArgs);
|
|
|
|
std::wstring argsv = L"";
|
|
|
|
if (numArgs > 1) {
|
|
|
|
for (int i = 1; i < numArgs; i++)
|
|
|
|
argsv += i == 1 ? args[i] : std::wstring(L" ") + args[i];
|
2022-06-06 17:53:06 +00:00
|
|
|
}
|
|
|
|
Settings::Parse(argsv);
|
2022-07-13 20:53:57 +00:00
|
|
|
SteamTarget target;
|
|
|
|
#else // Code below is broken now due to parse requiring std::wstring instead of std:string. Sorry.
|
2022-06-06 17:53:06 +00:00
|
|
|
std::string argsv = "";
|
|
|
|
if (argc > 1) {
|
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
argsv += i == 1 ? argv[i] : std::string(" ") + argv[i];
|
|
|
|
}
|
|
|
|
Settings::Parse(argsv);
|
2022-07-13 20:53:57 +00:00
|
|
|
SteamTarget target;
|
2021-10-17 19:04:41 +00:00
|
|
|
#endif
|
2022-06-06 17:53:06 +00:00
|
|
|
exit = target.run();
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2021-11-04 12:07:07 +00:00
|
|
|
spdlog::error("Exception occured: {}", e.what());
|
2022-06-06 17:53:06 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
2021-11-04 12:07:07 +00:00
|
|
|
spdlog::error("Unknown exception occured");
|
|
|
|
}
|
2021-10-16 20:36:30 +00:00
|
|
|
spdlog::shutdown();
|
|
|
|
return exit;
|
2021-10-14 18:54:29 +00:00
|
|
|
}
|