changed a lot of Con_DPrint/Con_DPrintf calls to Con_Print/Con_Printf (non-technical...
[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
37         COM_InitArgv();
38         COM_InitGameType();
39
40 #if defined(__linux__)
41         os = "Linux";
42 #elif defined(WIN32)
43         os = "Windows";
44 #elif defined(__FreeBSD__)
45         os = "FreeBSD";
46 #elif defined(__NetBSD__)
47         os = "NetBSD";
48 #elif defined(__OpenBSD__)
49         os = "OpenBSD";
50 #else
51         os = "Unknown";
52 #endif
53         snprintf (engineversion, sizeof (engineversion), "%s %s %s", gamename, os, buildstring);
54
55 // COMMANDLINEOPTION: Console: -nostdout disables text output to the terminal the game was launched from
56         if (COM_CheckParm("-nostdout"))
57                 sys_nostdout = 1;
58         else
59                 Con_Printf("%s\n", engineversion);
60 }
61
62 void Sys_Shared_LateInit(void)
63 {
64 }
65
66 /*
67 ===============================================================================
68
69 DLL MANAGEMENT
70
71 ===============================================================================
72 */
73
74 qboolean Sys_LoadLibrary (const char* dllname, dllhandle_t* handle, const dllfunction_t *fcts)
75 {
76         const dllfunction_t *func;
77         dllhandle_t dllhandle;
78
79         if (handle == NULL)
80                 return false;
81
82         // Initializations
83         for (func = fcts; func && func->name != NULL; func++)
84                 *func->funcvariable = NULL;
85
86         // Load the DLL
87 #ifdef WIN32
88         dllhandle = LoadLibrary (dllname);
89 #else
90         dllhandle = dlopen (dllname, RTLD_LAZY);
91 #endif
92         if (! dllhandle)
93         {
94                 Con_Printf ("Can't load \"%s\".\n", dllname);
95                 return false;
96         }
97
98         // Get the function adresses
99         for (func = fcts; func && func->name != NULL; func++)
100                 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
101                 {
102                         Con_Printf ("Missing function \"%s\" - broken library!\n", func->name);
103                         Sys_UnloadLibrary (&dllhandle);
104                         return false;
105                 }
106
107         *handle = dllhandle;
108         Con_Printf("\"%s\" loaded.\n", dllname);
109         return true;
110 }
111
112 void Sys_UnloadLibrary (dllhandle_t* handle)
113 {
114         if (handle == NULL || *handle == NULL)
115                 return;
116
117 #ifdef WIN32
118         FreeLibrary (*handle);
119 #else
120         dlclose (*handle);
121 #endif
122
123         *handle = NULL;
124 }
125
126 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
127 {
128 #ifdef WIN32
129         return (void *)GetProcAddress (handle, name);
130 #else
131         return (void *)dlsym (handle, name);
132 #endif
133 }
134