- DP now defines MACOSX when relevant
[divverent/darkplaces.git] / sys_shared.c
1
2 #include "quakedef.h"
3 # include <time.h>
4 #ifndef WIN32
5 # include <unistd.h>
6 # include <fcntl.h>
7 # include <dlfcn.h>
8 #endif
9
10 qboolean sys_nostdout = false;
11
12 static char sys_timestring[128];
13 char *Sys_TimeString(const char *timeformat)
14 {
15         time_t mytime = time(NULL);
16         strftime(sys_timestring, sizeof(sys_timestring), timeformat, localtime(&mytime));
17         return sys_timestring;
18 }
19
20
21 extern qboolean host_shuttingdown;
22 void Sys_Quit (void)
23 {
24         host_shuttingdown = true;
25         Host_Shutdown();
26         exit(0);
27 }
28
29 char engineversion[128];
30
31 void Sys_Shared_EarlyInit(void)
32 {
33         const char* os;
34
35         Memory_Init ();
36         Log_Init ();
37
38         COM_InitArgv();
39         COM_InitGameType();
40
41 #if defined(__linux__)
42         os = "Linux";
43 #elif defined(WIN32)
44         os = "Windows";
45 #elif defined(__FreeBSD__)
46         os = "FreeBSD";
47 #elif defined(__NetBSD__)
48         os = "NetBSD";
49 #elif defined(__OpenBSD__)
50         os = "OpenBSD";
51 #elif defined(MACOSX)
52         os = "Mac OS X";
53 #else
54         os = "Unknown";
55 #endif
56         snprintf (engineversion, sizeof (engineversion), "%s %s %s", gamename, os, buildstring);
57
58 // COMMANDLINEOPTION: Console: -nostdout disables text output to the terminal the game was launched from
59         if (COM_CheckParm("-nostdout"))
60                 sys_nostdout = 1;
61         else
62                 Con_Printf("%s\n", engineversion);
63 }
64
65 void Sys_Shared_LateInit(void)
66 {
67 }
68
69 /*
70 ===============================================================================
71
72 DLL MANAGEMENT
73
74 ===============================================================================
75 */
76
77 qboolean Sys_LoadLibrary (const char* dllname, dllhandle_t* handle, const dllfunction_t *fcts)
78 {
79         const dllfunction_t *func;
80         dllhandle_t dllhandle;
81
82         if (handle == NULL)
83                 return false;
84
85         // Initializations
86         for (func = fcts; func && func->name != NULL; func++)
87                 *func->funcvariable = NULL;
88
89         // Load the DLL
90 #ifdef WIN32
91         dllhandle = LoadLibrary (dllname);
92 #else
93         dllhandle = dlopen (dllname, RTLD_LAZY);
94 #endif
95         if (! dllhandle)
96         {
97                 Con_Printf ("Can't load \"%s\".\n", dllname);
98                 return false;
99         }
100
101         // Get the function adresses
102         for (func = fcts; func && func->name != NULL; func++)
103                 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
104                 {
105                         Con_Printf ("Missing function \"%s\" - broken library!\n", func->name);
106                         Sys_UnloadLibrary (&dllhandle);
107                         return false;
108                 }
109
110         *handle = dllhandle;
111         Con_Printf("\"%s\" loaded.\n", dllname);
112         return true;
113 }
114
115 void Sys_UnloadLibrary (dllhandle_t* handle)
116 {
117         if (handle == NULL || *handle == NULL)
118                 return;
119
120 #ifdef WIN32
121         FreeLibrary (*handle);
122 #else
123         dlclose (*handle);
124 #endif
125
126         *handle = NULL;
127 }
128
129 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
130 {
131 #ifdef WIN32
132         return (void *)GetProcAddress (handle, name);
133 #else
134         return (void *)dlsym (handle, name);
135 #endif
136 }
137