/
dd_uinit.cpp
193 lines (166 loc) · 5.06 KB
/
dd_uinit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/** @file dd_uinit.cpp Engine Initialization (Unix).
*
* @authors Copyright © 2004-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2006-2007 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <locale.h>
#include <de/c_wrapper.h>
#include <de/App>
#ifdef UNIX
# include "library.h"
#endif
#include "de_base.h"
#include "de_graphics.h"
#include "de_console.h"
#include "de_system.h"
#include "de_play.h"
#include "de_network.h"
#include "de_misc.h"
#include "filesys/fs_util.h"
#include "dd_uinit.h"
#include <de/App>
#ifdef __CLIENT__
# include <de/DisplayMode>
#endif
application_t app;
#ifdef __CLIENT__
static int initDGL(void)
{
return (int) Sys_GLPreInit();
}
#endif
static void determineGlobalPaths(application_t* app)
{
assert(app);
// By default, make sure the working path is the home folder.
de::App::setCurrentWorkPath(de::App::app().nativeHomePath());
#ifndef MACOSX
if(getenv("HOME"))
{
filename_t homePath;
directory_t* temp;
dd_snprintf(homePath, FILENAME_T_MAXLEN, "%s/%s/runtime/", getenv("HOME"),
DENG2_APP->unixHomeFolderName().toLatin1().constData());
temp = Dir_New(homePath);
Dir_mkpath(Dir_Path(temp));
app->usingHomeDir = Dir_SetCurrent(Dir_Path(temp));
if(app->usingHomeDir)
{
strncpy(ddRuntimePath, Dir_Path(temp), FILENAME_T_MAXLEN);
}
Dir_Delete(temp);
}
#endif
// The -userdir option sets the working directory.
if(CommandLine_CheckWith("-userdir", 1))
{
filename_t runtimePath;
directory_t* temp;
strncpy(runtimePath, CommandLine_NextAsPath(), FILENAME_T_MAXLEN);
Dir_CleanPath(runtimePath, FILENAME_T_MAXLEN);
// Ensure the path is closed with a directory separator.
F_AppendMissingSlashCString(runtimePath, FILENAME_T_MAXLEN);
temp = Dir_New(runtimePath);
app->usingUserDir = Dir_SetCurrent(Dir_Path(temp));
if(app->usingUserDir)
{
strncpy(ddRuntimePath, Dir_Path(temp), FILENAME_T_MAXLEN);
#ifndef MACOSX
app->usingHomeDir = false;
#endif
}
Dir_Delete(temp);
}
#ifndef MACOSX
if(!app->usingHomeDir && !app->usingUserDir)
#else
if(!app->usingUserDir)
#endif
{
// The current working directory is the runtime dir.
directory_t* temp = Dir_NewFromCWD();
strncpy(ddRuntimePath, Dir_Path(temp), FILENAME_T_MAXLEN);
Dir_Delete(temp);
}
/**
* Determine the base path. Unless overridden on the command line this is
* determined according to the the build configuration.
* Usually this is something like "/usr/share/deng/".
*/
if(CommandLine_CheckWith("-basedir", 1))
{
strncpy(ddBasePath, CommandLine_NextAsPath(), FILENAME_T_MAXLEN);
}
else
{
#ifdef MACOSX
strncpy(ddBasePath, "./", FILENAME_T_MAXLEN);
#else
strncpy(ddBasePath, DENG_BASE_DIR, FILENAME_T_MAXLEN);
#endif
// Also check the system config files.
UnixInfo_GetConfigValue("paths", "basedir", ddBasePath, FILENAME_T_MAXLEN);
}
Dir_CleanPath(ddBasePath, FILENAME_T_MAXLEN);
Dir_MakeAbsolutePath(ddBasePath, FILENAME_T_MAXLEN);
// Ensure it ends with a directory separator.
F_AppendMissingSlashCString(ddBasePath, FILENAME_T_MAXLEN);
}
dd_bool DD_Unix_Init(void)
{
dd_bool failed = true;
memset(&app, 0, sizeof(app));
// We wish to use U.S. English formatting for time and numbers.
setlocale(LC_ALL, "en_US.UTF-8");
DD_InitCommandLine();
Library_Init();
// Determine our basedir and other global paths.
determineGlobalPaths(&app);
if(!DD_EarlyInit())
{
Sys_MessageBox(MBT_ERROR, DOOMSDAY_NICENAME, "Error during early init.", 0);
}
#ifdef __CLIENT__
else if(!initDGL())
{
Sys_MessageBox(MBT_ERROR, DOOMSDAY_NICENAME, "Error initializing DGL.", 0);
}
#endif
else
{
// Everything okay so far.
failed = false;
}
return !failed;
}
/**
* Shuts down the engine.
*/
void DD_Shutdown(void)
{
// Shutdown all subsystems.
DD_ShutdownAll();
Plug_UnloadAll();
Library_Shutdown();
#ifdef __CLIENT__
DisplayMode_Shutdown();
#endif
}