2014-03-15 16:59:03 +00:00
|
|
|
// Emacs style mode select -*- C++ -*-
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Copyright (C) 1993-1996 by id Software, Inc.
|
|
|
|
// Copyright (C) 1998-2000 by DooM Legacy Team.
|
|
|
|
//
|
|
|
|
// 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 2
|
|
|
|
// 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.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
/// \file
|
|
|
|
/// \brief Main program, simply calls D_SRB2Main and D_SRB2Loop, the high level loop.
|
|
|
|
|
|
|
|
#include "../doomdef.h"
|
|
|
|
#include "../m_argv.h"
|
|
|
|
#include "../d_main.h"
|
2019-12-24 01:21:58 +00:00
|
|
|
#include "../m_misc.h"/* path shit */
|
2014-03-15 16:59:03 +00:00
|
|
|
#include "../i_system.h"
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2019-09-08 05:29:09 +00:00
|
|
|
#include "time.h" // For log timestamps
|
|
|
|
|
2014-07-25 23:10:24 +00:00
|
|
|
#ifdef HAVE_SDL
|
2014-03-15 16:59:03 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_TTF
|
|
|
|
#include "SDL.h"
|
|
|
|
#include "i_ttf.h"
|
|
|
|
#endif
|
|
|
|
|
2016-01-29 05:33:11 +00:00
|
|
|
#if defined (_WIN32) && !defined (main)
|
2016-02-08 01:53:27 +00:00
|
|
|
//#define SDLMAIN
|
2016-01-29 05:33:11 +00:00
|
|
|
#endif
|
|
|
|
|
2014-03-15 16:59:03 +00:00
|
|
|
#ifdef SDLMAIN
|
|
|
|
#include "SDL_main.h"
|
|
|
|
#elif defined(FORCESDLMAIN)
|
|
|
|
extern int SDL_main(int argc, char *argv[]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LOGMESSAGES
|
|
|
|
FILE *logstream = NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef DOXYGEN
|
|
|
|
#ifndef O_TEXT
|
|
|
|
#define O_TEXT 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef O_SEQUENTIAL
|
|
|
|
#define O_SEQUENTIAL 0
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2017-09-16 19:26:04 +00:00
|
|
|
#if defined (_WIN32)
|
2014-03-15 16:59:03 +00:00
|
|
|
#include "../win32/win_dbg.h"
|
|
|
|
typedef BOOL (WINAPI *p_IsDebuggerPresent)(VOID);
|
|
|
|
#endif
|
|
|
|
|
2017-09-16 19:26:04 +00:00
|
|
|
#if defined (_WIN32)
|
2014-03-15 16:59:03 +00:00
|
|
|
static inline VOID MakeCodeWritable(VOID)
|
|
|
|
{
|
|
|
|
#ifdef USEASM // Disable write-protection of code segment
|
|
|
|
DWORD OldRights;
|
|
|
|
const DWORD NewRights = PAGE_EXECUTE_READWRITE;
|
|
|
|
PBYTE pBaseOfImage = (PBYTE)GetModuleHandle(NULL);
|
|
|
|
PIMAGE_DOS_HEADER dosH =(PIMAGE_DOS_HEADER)pBaseOfImage;
|
|
|
|
PIMAGE_NT_HEADERS ntH = (PIMAGE_NT_HEADERS)(pBaseOfImage + dosH->e_lfanew);
|
|
|
|
PIMAGE_OPTIONAL_HEADER oH = (PIMAGE_OPTIONAL_HEADER)
|
|
|
|
((PBYTE)ntH + sizeof (IMAGE_NT_SIGNATURE) + sizeof (IMAGE_FILE_HEADER));
|
|
|
|
LPVOID pA = pBaseOfImage+oH->BaseOfCode;
|
|
|
|
SIZE_T pS = oH->SizeOfCode;
|
|
|
|
#if 1 // try to find the text section
|
|
|
|
PIMAGE_SECTION_HEADER ntS = IMAGE_FIRST_SECTION (ntH);
|
|
|
|
WORD s;
|
|
|
|
for (s = 0; s < ntH->FileHeader.NumberOfSections; s++)
|
|
|
|
{
|
|
|
|
if (memcmp (ntS[s].Name, ".text\0\0", 8) == 0)
|
|
|
|
{
|
|
|
|
pA = pBaseOfImage+ntS[s].VirtualAddress;
|
|
|
|
pS = ntS[s].Misc.VirtualSize;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!VirtualProtect(pA,pS,NewRights,&OldRights))
|
|
|
|
I_Error("Could not make code writable\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/** \brief The main function
|
|
|
|
|
|
|
|
\param argc number of arg
|
|
|
|
\param *argv string table
|
|
|
|
|
|
|
|
\return int
|
|
|
|
*/
|
2018-01-04 19:29:29 +00:00
|
|
|
#if defined (__GNUC__) && (__GNUC__ >= 4)
|
|
|
|
#pragma GCC diagnostic ignored "-Wmissing-noreturn"
|
|
|
|
#endif
|
|
|
|
|
2014-03-15 16:59:03 +00:00
|
|
|
#ifdef FORCESDLMAIN
|
|
|
|
int SDL_main(int argc, char **argv)
|
|
|
|
#else
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
const char *logdir = NULL;
|
2019-09-08 05:29:09 +00:00
|
|
|
char logfile[MAX_WADPATH];
|
2014-03-15 16:59:03 +00:00
|
|
|
myargc = argc;
|
|
|
|
myargv = argv; /// \todo pull out path to exe from this string
|
|
|
|
|
|
|
|
#ifdef HAVE_TTF
|
2017-09-16 19:26:04 +00:00
|
|
|
#ifdef _WIN32
|
2014-03-15 16:59:03 +00:00
|
|
|
I_StartupTTF(FONTPOINTSIZE, SDL_INIT_VIDEO|SDL_INIT_AUDIO, SDL_SWSURFACE);
|
|
|
|
#else
|
|
|
|
I_StartupTTF(FONTPOINTSIZE, SDL_INIT_VIDEO, SDL_SWSURFACE);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LOGMESSAGES
|
2019-09-08 05:29:09 +00:00
|
|
|
if (!M_CheckParm("-nolog"))
|
|
|
|
{
|
|
|
|
time_t my_time;
|
|
|
|
struct tm * timeinfo;
|
2019-12-23 23:31:50 +00:00
|
|
|
const char *format;
|
2019-12-24 01:21:58 +00:00
|
|
|
const char *reldir;
|
|
|
|
int left;
|
2019-12-24 01:40:43 +00:00
|
|
|
boolean fileabs;
|
2019-09-20 16:22:09 +00:00
|
|
|
|
|
|
|
logdir = D_Home();
|
|
|
|
|
2019-09-08 05:29:09 +00:00
|
|
|
my_time = time(NULL);
|
|
|
|
timeinfo = localtime(&my_time);
|
|
|
|
|
2019-12-23 23:31:50 +00:00
|
|
|
if (M_CheckParm("-logfile") && M_IsNextParm())
|
2019-12-24 01:40:43 +00:00
|
|
|
{
|
2019-12-23 23:31:50 +00:00
|
|
|
format = M_GetNextParm();
|
2019-12-24 01:40:43 +00:00
|
|
|
fileabs = M_IsPathAbsolute(format);
|
|
|
|
}
|
2019-12-23 23:31:50 +00:00
|
|
|
else
|
2019-12-24 01:40:43 +00:00
|
|
|
{
|
2019-12-23 23:34:27 +00:00
|
|
|
format = "log-%Y-%m-%d_%H-%M-%S.txt";
|
2019-12-24 01:40:43 +00:00
|
|
|
fileabs = false;
|
|
|
|
}
|
2019-12-23 23:31:50 +00:00
|
|
|
|
2019-12-24 01:40:43 +00:00
|
|
|
if (fileabs)
|
2019-12-24 01:21:58 +00:00
|
|
|
{
|
2019-12-24 01:40:43 +00:00
|
|
|
strftime(logfile, sizeof logfile, format, timeinfo);
|
|
|
|
|
|
|
|
M_MkdirEachUntil(logfile,
|
|
|
|
M_PathParts(logdir) - 1,
|
|
|
|
M_PathParts(logfile) - 1, 0755);
|
2019-12-24 01:21:58 +00:00
|
|
|
}
|
|
|
|
else
|
2019-09-08 05:29:09 +00:00
|
|
|
{
|
2019-12-24 01:40:43 +00:00
|
|
|
if (M_CheckParm("-logdir") && M_IsNextParm())
|
|
|
|
reldir = M_GetNextParm();
|
|
|
|
else
|
|
|
|
reldir = "logs";
|
|
|
|
|
|
|
|
if (M_IsPathAbsolute(reldir))
|
|
|
|
{
|
|
|
|
left = snprintf(logfile, sizeof logfile,
|
|
|
|
"%s"PATHSEP, reldir);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#ifdef DEFAULTDIR
|
|
|
|
if (logdir)
|
|
|
|
{
|
|
|
|
left = snprintf(logfile, sizeof logfile,
|
|
|
|
"%s"PATHSEP DEFAULTDIR PATHSEP"%s"PATHSEP, logdir, reldir);
|
|
|
|
}
|
|
|
|
else
|
2019-12-24 01:21:58 +00:00
|
|
|
#endif/*DEFAULTDIR*/
|
2019-12-24 01:40:43 +00:00
|
|
|
{
|
|
|
|
left = snprintf(logfile, sizeof logfile,
|
|
|
|
"."PATHSEP"%s"PATHSEP, reldir);
|
|
|
|
}
|
2019-12-24 01:21:58 +00:00
|
|
|
#endif/*LOGMESSAGES*/
|
|
|
|
|
2019-12-24 01:40:43 +00:00
|
|
|
M_MkdirEach(logfile, M_PathParts(logdir) - 1, 0755);
|
2019-12-24 01:21:58 +00:00
|
|
|
|
2019-12-24 01:40:43 +00:00
|
|
|
strftime(&logfile[left], sizeof logfile - left, format, timeinfo);
|
|
|
|
}
|
2019-12-24 01:21:58 +00:00
|
|
|
|
|
|
|
logstream = fopen(logfile, "wt");
|
2019-09-08 05:29:09 +00:00
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
|
|
|
|
//I_OutputMsg("I_StartupSystem() ...\n");
|
|
|
|
I_StartupSystem();
|
2017-09-16 19:26:04 +00:00
|
|
|
#if defined (_WIN32)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2016-06-01 01:30:18 +00:00
|
|
|
#if 0 // just load the DLL
|
2014-03-15 16:59:03 +00:00
|
|
|
p_IsDebuggerPresent pfnIsDebuggerPresent = (p_IsDebuggerPresent)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsDebuggerPresent");
|
|
|
|
if ((!pfnIsDebuggerPresent || !pfnIsDebuggerPresent())
|
|
|
|
#ifdef BUGTRAP
|
|
|
|
&& !InitBugTrap()
|
|
|
|
#endif
|
|
|
|
)
|
2017-09-16 19:59:35 +00:00
|
|
|
#endif
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
|
|
|
LoadLibraryA("exchndl.dll");
|
|
|
|
}
|
|
|
|
}
|
2017-12-27 01:24:08 +00:00
|
|
|
#ifndef __MINGW32__
|
2014-03-15 16:59:03 +00:00
|
|
|
prevExceptionFilter = SetUnhandledExceptionFilter(RecordExceptionInfo);
|
2017-12-27 01:24:08 +00:00
|
|
|
#endif
|
2014-03-15 16:59:03 +00:00
|
|
|
MakeCodeWritable();
|
|
|
|
#endif
|
|
|
|
// startup SRB2
|
2015-01-01 19:50:31 +00:00
|
|
|
CONS_Printf("Setting up SRB2...\n");
|
2014-03-15 16:59:03 +00:00
|
|
|
D_SRB2Main();
|
2019-09-20 16:22:09 +00:00
|
|
|
#ifdef LOGMESSAGES
|
|
|
|
if (!M_CheckParm("-nolog"))
|
|
|
|
CONS_Printf("Logfile: %s\n", logfile);
|
|
|
|
#endif
|
2015-01-01 19:50:31 +00:00
|
|
|
CONS_Printf("Entering main game loop...\n");
|
2014-03-15 16:59:03 +00:00
|
|
|
// never return
|
|
|
|
D_SRB2Loop();
|
|
|
|
|
|
|
|
#ifdef BUGTRAP
|
|
|
|
// This is safe even if BT didn't start.
|
|
|
|
ShutdownBugTrap();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// return to OS
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|