Cleanup sdlmain.cpp

Remove unused global variables, rewrite internal functions, remove `-game_checksum` cmdline option.
This commit is contained in:
Azamat H. Hackimov 2024-08-20 00:30:25 +03:00
parent c6ab519652
commit 30f8fefa0b
2 changed files with 75 additions and 98 deletions

View File

@ -26,6 +26,7 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <filesystem> #include <filesystem>
#include <map>
#ifndef WIN32 #ifndef WIN32
#include <unistd.h> #include <unistd.h>
@ -52,14 +53,12 @@
#include "descent.h" #include "descent.h"
#include "dedicated_server.h" #include "dedicated_server.h"
#include "init.h" #include "init.h"
#include "osiris_dll.h"
std::filesystem::path orig_pwd; std::filesystem::path orig_pwd;
static volatile char already_tried_signal_cleanup = 0; static volatile char already_tried_signal_cleanup = 0;
void just_exit() {
void just_exit(void) {
ddio_InternalClose(); // try to reset serial port. ddio_InternalClose(); // try to reset serial port.
SDL_Quit(); SDL_Quit();
@ -91,55 +90,43 @@ void fatal_signal_handler(int signum) {
just_exit(); just_exit();
} // else } // else
break; break;
case SIGXCPU: default:
case SIGXFSZ:
break; break;
} }
_exit(-10); _exit(-10);
} }
void safe_signal_handler(int signum) {}
void install_signal_handlers() { void install_signal_handlers() {
struct sigaction sact{}, fact{}; struct sigaction fact{};
memset(&sact, 0, sizeof(sact));
memset(&fact, 0, sizeof(fact)); memset(&fact, 0, sizeof(fact));
sact.sa_handler = safe_signal_handler;
fact.sa_handler = fatal_signal_handler; fact.sa_handler = fatal_signal_handler;
if (sigaction(SIGHUP, &fact, NULL)) const std::map<int, std::string> signals = {
fprintf(stderr, "SIG: Unable to install SIGHUP\n"); {SIGHUP, "SIGHUP"},
if (sigaction(SIGABRT, &fact, NULL)) {SIGABRT, "SIGABRT"},
fprintf(stderr, "SIG: Unable to install SIGABRT\n"); {SIGINT, "SIGINT"},
if (sigaction(SIGINT, &fact, NULL)) {SIGBUS, "SIGBUS"},
fprintf(stderr, "SIG: Unable to install SIGINT\n"); {SIGFPE, "SIGFPE"},
if (sigaction(SIGBUS, &fact, NULL)) {SIGILL, "SIGILL"},
fprintf(stderr, "SIG: Unable to install SIGBUS\n"); {SIGQUIT, "SIGQUIT"},
if (sigaction(SIGFPE, &fact, NULL)) {SIGSEGV, "SIGSEGV"},
fprintf(stderr, "SIG: Unable to install SIGFPE\n"); {SIGTERM, "SIGTERM"},
if (sigaction(SIGILL, &fact, NULL)) {SIGXCPU, "SIGXCPU"},
fprintf(stderr, "SIG: Unable to install SIGILL\n"); {SIGXFSZ, "SIGXFSZ"},
if (sigaction(SIGQUIT, &fact, NULL)) {SIGVTALRM, "SIGVTALRM"},
fprintf(stderr, "SIG: Unable to install SIGQUIT\n"); {SIGTRAP, "SIGTRAP"},
if (sigaction(SIGSEGV, &fact, NULL)) };
fprintf(stderr, "SIG: Unable to install SIGSEGV\n");
if (sigaction(SIGTERM, &fact, NULL)) for (const auto &signal : signals) {
fprintf(stderr, "SIG: Unable to install SIGTERM\n"); if (sigaction(signal.first, &fact, nullptr) != 0) {
if (sigaction(SIGXCPU, &fact, NULL)) LOG_WARNING << "SIG: Unable to install " << signal.second;
fprintf(stderr, "SIG: Unable to install SIGXCPU\n"); }
if (sigaction(SIGXFSZ, &fact, NULL)) }
fprintf(stderr, "SIG: Unable to install SIGXFSZ\n");
if (sigaction(SIGVTALRM, &fact, NULL))
fprintf(stderr, "SIG: Unable to install SIGVTALRM\n");
if (sigaction(SIGTRAP, &fact, NULL))
fprintf(stderr, "SIG: Unable to install SIGTRAP\n");
} }
#else #else
void install_signal_handlers() { void install_signal_handlers() { SetUnhandledExceptionFilter(RecordExceptionInfo); }
SetUnhandledExceptionFilter(RecordExceptionInfo);
}
#endif #endif
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -300,7 +287,7 @@ int main(int argc, char *argv[]) {
} }
// !!! FIXME: Don't use an event filter! // !!! FIXME: Don't use an event filter!
SDL_SetEventFilter(d3SDLEventFilter, NULL); SDL_SetEventFilter(d3SDLEventFilter, nullptr);
install_signal_handlers(); install_signal_handlers();
int winArg = FindArgChar("-windowed", 'w'); int winArg = FindArgChar("-windowed", 'w');
@ -309,79 +296,68 @@ int main(int argc, char *argv[]) {
if ((fsArg) && (winArg)) { if ((fsArg) && (winArg)) {
LOG_FATAL.printf("ERROR: %s AND %s specified!", GameArgs[winArg], GameArgs[fsArg]); LOG_FATAL.printf("ERROR: %s AND %s specified!", GameArgs[winArg], GameArgs[fsArg]);
return (0); return (0);
} // if }
if (FindArg("-game_checksum")) { // Initialize our OS Object. This could be a game dependant OS object, or a default OS object.
extern tOSIRISModuleInit Osiris_module_init; // Once we create it, if successful, we can start the game.
extern void Osiris_CreateModuleInitStruct(tOSIRISModuleInit * st); int flags = 0;
extern uint32_t Osiris_CreateGameChecksum(void);
Osiris_CreateModuleInitStruct(&Osiris_module_init); if (!FindArgChar("-dedicated", 'd')) {
uint32_t checksum = Osiris_CreateGameChecksum();
printf("Descent 3\n");
printf("Game Checksum: %u\n", checksum);
return (0);
} else {
/*initialize our OS Object. This could be a game dependant OS object, or a default OS object.
once we create it, if successful, we can start the game.
*/
int flags = 0;
if (!FindArgChar("-dedicated", 'd')) {
#ifndef DEDICATED #ifndef DEDICATED
// check for a renderer // check for a renderer
if (FindArgChar("-nomousegrab", 'm')) { if (FindArgChar("-nomousegrab", 'm')) {
flags |= APPFLAG_NOMOUSECAPTURE; flags |= APPFLAG_NOMOUSECAPTURE;
ddio_MouseSetGrab(false); ddio_MouseSetGrab(false);
} }
SDL_SetRelativeMouseMode(ddio_MouseGetGrab() ? SDL_TRUE : SDL_FALSE); SDL_SetRelativeMouseMode(ddio_MouseGetGrab() ? SDL_TRUE : SDL_FALSE);
if (!FindArg("-sharedmemory")) {
flags |= APPFLAG_NOSHAREDMEMORY;
}
flags |= APPFLAG_WINDOWEDMODE;
if (!FindArg("-sharedmemory")) {
flags |= APPFLAG_NOSHAREDMEMORY;
}
flags |= APPFLAG_WINDOWEDMODE;
#else #else
LOG_FATAL << "Error: \"--dedicated\" or \"-d\" flag required"; LOG_FATAL << "Error: \"--dedicated\" or \"-d\" flag required";
return 0; return 0;
#endif #endif
} else { } else {
// Dedicated Server Mode // Dedicated Server Mode
flags |= OEAPP_CONSOLE; flags |= OEAPP_CONSOLE;
// service flag overrides others here in the group // service flag overrides others here in the group
if (FindArg("-service")) { if (FindArg("-service")) {
flags |= APPFLAG_USESERVICE; flags |= APPFLAG_USESERVICE;
}
} }
}
bool run_d3 = true; bool run_d3 = true;
#if defined(POSIX) #if defined(POSIX)
if (flags & APPFLAG_USESERVICE) { if (flags & APPFLAG_USESERVICE) {
run_d3 = false; run_d3 = false;
pid_t np = fork(); pid_t np = fork();
if (np == -1) { if (np == -1) {
LOG_WARNING << "Unable to fork process"; LOG_WARNING << "Unable to fork process";
}
if (np == 0) {
run_d3 = true;
np = setsid();
pid_t pp = getpid();
LOG_INFO.printf("Successfully forked process [new sid=%d pid=%d]", np, pp);
}
} }
if (np == 0) {
run_d3 = true;
np = setsid();
pid_t pp = getpid();
LOG_INFO.printf("Successfully forked process [new sid=%d pid=%d]", np, pp);
}
}
#endif #endif
if (run_d3) { if (run_d3) {
oeD3LnxApp d3(flags); oeD3LnxApp d3(flags);
oeD3LnxDatabase dbase; oeD3LnxDatabase dbase;
StartDedicatedServer(); StartDedicatedServer();
PreInitD3Systems(); PreInitD3Systems();
d3.init(); d3.init();
d3.run(); d3.run();
}
} }
just_exit(); just_exit();

View File

@ -379,7 +379,8 @@ int opengl_Setup(oeApplication *app, const int *width, const int *height) {
gl_library[0] = 0; gl_library[0] = 0;
} }
mprintf(0, "OpenGL: Attempting to use \"%s\" for OpenGL\n", gl_library[0] ? gl_library : "[system default library]"); LOG_INFO.printf("OpenGL: Attempting to use \"%s\" for OpenGL",
gl_library[0] ? gl_library : "[system default library]");
// ryan's adds. 04/18/2000...SDL stuff on 04/25/2000 // ryan's adds. 04/18/2000...SDL stuff on 04/25/2000
bool success = true; bool success = true;
@ -516,7 +517,7 @@ int opengl_Setup(oeApplication *app, const int *width, const int *height) {
Uint16 ramp[256]; Uint16 ramp[256];
SDL_CalculateGammaRamp(Render_preferred_state.gamma, ramp); SDL_CalculateGammaRamp(Render_preferred_state.gamma, ramp);
SDL_SetWindowGammaRamp(GSDLWindow, ramp, ramp, ramp); SDL_SetWindowGammaRamp(GSDLWindow, ramp, ramp, ramp);
} // else }
if (ParentApplication) { if (ParentApplication) {
reinterpret_cast<oeLnxApplication *>(ParentApplication)->set_sizepos(0, 0, *width, *height); reinterpret_cast<oeLnxApplication *>(ParentApplication)->set_sizepos(0, 0, *width, *height);