- Removed Con_SafePrint and Con_SafePrintf since they now does the same things as...
[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 #else
52         os = "Unknown";
53 #endif
54         snprintf (engineversion, sizeof (engineversion), "%s %s %s", gamename, os, buildstring);
55
56 // COMMANDLINEOPTION: Console: -nostdout disables text output to the terminal the game was launched from
57         if (COM_CheckParm("-nostdout"))
58                 sys_nostdout = 1;
59         else
60                 Con_Printf("%s\n", engineversion);
61 }
62
63 void Sys_Shared_LateInit(void)
64 {
65 }
66
67 /*
68 ===============================================================================
69
70 DLL MANAGEMENT
71
72 ===============================================================================
73 */
74
75 qboolean Sys_LoadLibrary (const char* dllname, dllhandle_t* handle, const dllfunction_t *fcts)
76 {
77         const dllfunction_t *func;
78         dllhandle_t dllhandle;
79
80         if (handle == NULL)
81                 return false;
82
83         // Initializations
84         for (func = fcts; func && func->name != NULL; func++)
85                 *func->funcvariable = NULL;
86
87         // Load the DLL
88 #ifdef WIN32
89         dllhandle = LoadLibrary (dllname);
90 #else
91         dllhandle = dlopen (dllname, RTLD_LAZY);
92 #endif
93         if (! dllhandle)
94         {
95                 Con_Printf ("Can't load \"%s\".\n", dllname);
96                 return false;
97         }
98
99         // Get the function adresses
100         for (func = fcts; func && func->name != NULL; func++)
101                 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
102                 {
103                         Con_Printf ("Missing function \"%s\" - broken library!\n", func->name);
104                         Sys_UnloadLibrary (&dllhandle);
105                         return false;
106                 }
107
108         *handle = dllhandle;
109         Con_Printf("\"%s\" loaded.\n", dllname);
110         return true;
111 }
112
113 void Sys_UnloadLibrary (dllhandle_t* handle)
114 {
115         if (handle == NULL || *handle == NULL)
116                 return;
117
118 #ifdef WIN32
119         FreeLibrary (*handle);
120 #else
121         dlclose (*handle);
122 #endif
123
124         *handle = NULL;
125 }
126
127 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
128 {
129 #ifdef WIN32
130         return (void *)GetProcAddress (handle, name);
131 #else
132         return (void *)dlsym (handle, name);
133 #endif
134 }
135