-
-
Notifications
You must be signed in to change notification settings - Fork 515
Expand file tree
/
Copy pathxrCore.cpp
More file actions
393 lines (340 loc) · 11.1 KB
/
Copy pathxrCore.cpp
File metadata and controls
393 lines (340 loc) · 11.1 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// xrCore.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#pragma hdrstop
#if defined(XR_PLATFORM_WINDOWS)
#include <mmsystem.h>
#include <objbase.h>
#pragma comment(lib, "winmm.lib")
#elif defined(XR_PLATFORM_POSIX)
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <unistd.h>
#endif
#include "xrCore.h"
#include "xrCore/_std_extensions.h"
#include "Threading/TaskManager.hpp"
#include <SDL3/SDL.h>
#if __has_include(".GitInfo.hpp")
#include ".GitInfo.hpp"
#endif
#include "Compression/compression_ppmd_stream.h"
extern compression::ppmd::stream* trained_model;
XRCORE_API xrCore Core;
static u32 init_counter = 0;
#define DO_EXPAND(...) __VA_ARGS__##1
#define EXPAND(VAL) DO_EXPAND(VAL)
#ifdef CI
#if EXPAND(CI) == 1
#undef CI
#endif
#endif
#ifdef APPVEYOR
#if EXPAND(APPVEYOR) == 1
#undef APPVEYOR
#endif
#endif
#ifdef GITHUB_ACTIONS
#if EXPAND(GITHUB_ACTIONS) == 1
#undef GITHUB_ACTIONS
#endif
#endif
#ifndef GIT_INFO_CURRENT_COMMIT
#define GIT_INFO_CURRENT_COMMIT "unknown"
#endif
#ifndef GIT_INFO_CURRENT_BRANCH
#define GIT_INFO_CURRENT_BRANCH "unknown"
#endif
void SDLLogOutput(void* userdata, int category, SDL_LogPriority priority, const char* message);
const pcstr xrCore::buildDate = __DATE__;
const pcstr xrCore::buildCommit = GIT_INFO_CURRENT_COMMIT;
const pcstr xrCore::buildBranch = GIT_INFO_CURRENT_BRANCH;
void SanitizeString(pcstr str)
{
pstr mut_str = const_cast<pstr>(str);
while (*mut_str != '\0')
{
switch (*mut_str)
{
case '\\':
case '/':
case ',':
case '.':
*mut_str = '_';
[[fallthrough]];
default:
++mut_str;
}
}
}
xrCore::xrCore()
: ApplicationName{}, ApplicationPath{},
WorkingPath{},
UserName{}, CompName{},
Params(nullptr), dwFrame(0),
PluginMode(false)
{
CalculateBuildId();
}
void xrCore::CalculateBuildId()
{
const int startDay = 31;
const int startMonth = 1;
const int startYear = 1999;
const char* monthId[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
const int daysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int days;
int months = 0;
int years;
string16 month;
string256 buffer;
xr_strcpy(buffer, buildDate);
sscanf(buffer, "%s %d %d", month, &days, &years);
for (int i = 0; i < 12; i++)
{
if (xr_stricmp(monthId[i], month))
continue;
months = i;
break;
}
buildId = (years - startYear) * 365 + days - startDay;
for (int i = 0; i < months; i++)
buildId += daysInMonth[i];
for (int i = 0; i < startMonth - 1; i++)
buildId -= daysInMonth[i];
}
void xrCore::PrintBuildInfo()
{
Msg("%s %s build %d, %s (%s)", ApplicationName, XRAY_BUILD_CONFIGURATION, buildId, buildDate, XRAY_BUILD_CONFIGURATION2);
pcstr name = "Custom";
pcstr buildUniqueId = nullptr;
pcstr buildId = nullptr;
pcstr builder = nullptr;
pcstr commit = GetBuildCommit();
pcstr branch = GetBuildBranch();
#if defined(CI)
# if defined(APPVEYOR)
name = "AppVeyor";
buildUniqueId = APPVEYOR_BUILD_ID;
buildId = APPVEYOR_BUILD_VERSION;
builder = APPVEYOR_ACCOUNT_NAME;
# elif defined(GITHUB_ACTIONS)
name = "GitHub Actions";
buildUniqueId = GITHUB_RUN_ID;
buildId = GITHUB_RUN_NUMBER;
builder = GITHUB_REPOSITORY;
#else
# pragma TODO("PrintBuildInfo for other CIs")
name = "CI";
builder = "Unknown CI";
# endif
#endif
string512 buf;
strconcat(buf, name, " build "); // "%s build "
if (buildId)
{
strconcat(buf, buf, buildId, " "); // "id "
if (buildUniqueId)
strconcat(buf, buf, "(", buildUniqueId, ") "); // "(unique id) "
}
strconcat(buf, buf, "from commit[", commit, "]"); // "from commit[hash]"
strconcat(buf, buf, " branch[", branch, "]"); // " branch[name]"
if (builder)
strconcat(buf, buf, " (built by ", builder, ")"); // " (built by builder)"
Log(buf); // "%s build %s from commit[%s] branch[%s] (built by %s)"
}
void xrCore::Initialize(pcstr _ApplicationName, pcstr commandLine, bool init_fs, pcstr fs_fname, bool plugin)
{
ZoneScoped;
xr_strcpy(ApplicationName, _ApplicationName);
PrintBuildInfo();
if (0 == init_counter)
{
#if defined(XR_ARCHITECTURE_X86) || defined(XR_ARCHITECTURE_X64)
R_ASSERT2(CPU::HasSSE2, "Your CPU must support SSE2.");
#endif
PluginMode = plugin;
if (commandLine)
Params = xr_strdup(commandLine);
else
Params = xr_strdup("");
#if defined(XR_PLATFORM_WINDOWS)
timeBeginPeriod(1);
CoInitializeEx(nullptr, COINIT_MULTITHREADED); // needed for OpenAL initialization
string_path fn, dr, di;
// application path
GetModuleFileName(GetModuleHandle("xrCore"), fn, sizeof(fn));
_splitpath(fn, dr, di, nullptr, nullptr);
strconcat(sizeof(ApplicationPath), ApplicationPath, dr, di);
#elif defined(XR_PLATFORM_POSIX)
const char* base_path = SDL_GetBasePath();
if (base_path)
{
SDL_strlcpy(ApplicationPath, base_path, sizeof(ApplicationPath));
}
else
{
char* pref_path = nullptr;
if (strstr(Core.Params, "-shoc") || strstr(Core.Params, "-soc"))
pref_path = SDL_GetPrefPath("GSC Game World", "S.T.A.L.K.E.R. - Shadow of Chernobyl");
else if (strstr(Core.Params, "-cs"))
pref_path = SDL_GetPrefPath("GSC Game World", "S.T.A.L.K.E.R. - Clear Sky");
else
pref_path = SDL_GetPrefPath("GSC Game World", "S.T.A.L.K.E.R. - Call of Pripyat");
SDL_strlcpy(ApplicationPath, pref_path ? pref_path : "", sizeof(ApplicationPath));
SDL_free(pref_path);
}
#else
# error Select or add implementation for your platform
#endif
#ifdef _EDITOR
// working path
if (strstr(Params, "-wf"))
{
string_path c_name;
sscanf(strstr(Core.Params, "-wf ") + 4, "%[^ ] ", c_name);
SetCurrentDirectory(c_name);
}
#endif
#if defined(XR_PLATFORM_WINDOWS)
GetCurrentDirectory(sizeof(WorkingPath), WorkingPath);
#elif defined(XR_PLATFORM_POSIX)
getcwd(WorkingPath, sizeof(WorkingPath));
#else
# error Select or add implementation for your platform
#endif
#if defined(XR_PLATFORM_WINDOWS)
// User/Comp Name
DWORD sz_user = sizeof(UserName);
GetUserName(UserName, &sz_user);
DWORD sz_comp = sizeof(CompName);
GetComputerName(CompName, &sz_comp);
#elif defined(XR_PLATFORM_POSIX)
uid_t uid = geteuid();
struct passwd *pw = getpwuid(uid);
if (pw)
{
strncpy(UserName, pw->pw_gecos, sizeof(UserName) - 1);
if (UserName[0] == '\0')
strncpy(UserName, pw->pw_name, sizeof(UserName) - 1);
}
else
Msg("! Failed to get user name");
if (gethostname(CompName, sizeof(CompName)) == 0)
CompName[sizeof(CompName) - 1] = '\0';
else
Msg("! Failed to get computer name");
#else
# error Select or add implementation for your platform
#endif
SanitizeString(UserName);
SanitizeString(CompName);
#ifdef DEBUG
Msg("UserName: %s", UserName);
Msg("ComputerName: %s", CompName);
#endif
Memory._initialize();
SDL_SetLogOutputFunction(SDLLogOutput, nullptr);
Msg("\ncommand line %s\n", Params);
_initialize_cpu();
TaskScheduler = xr_make_unique<TaskManager>();
TaskScheduler->SpawnThreads();
// xrDebug::Initialize ();
rtc_initialize();
xr_FS = xr_make_unique<CLocatorAPI>();
xr_EFS = xr_make_unique<EFS_Utils>();
//. R_ASSERT (co_res==S_OK);
}
if (init_fs)
{
u32 flags = 0u;
if (strstr(Params, "-build") != nullptr)
flags |= CLocatorAPI::flBuildCopy;
if (strstr(Params, "-ebuild") != nullptr)
flags |= CLocatorAPI::flBuildCopy | CLocatorAPI::flEBuildCopy;
#ifdef DEBUG
if (strstr(Params, "-cache"))
flags |= CLocatorAPI::flCacheFiles;
else
flags &= ~CLocatorAPI::flCacheFiles;
#endif // DEBUG
#ifdef _EDITOR // for EDITORS - no cache
flags &= ~CLocatorAPI::flCacheFiles;
#endif // _EDITOR
// TODO Add proper check for CMake Windows build
#if !defined(XR_PLATFORM_WINDOWS)
if (xr_stricmp(ApplicationPath, CMAKE_INSTALL_FULL_DATAROOTDIR) != 0)
flags |= CLocatorAPI::flScanAppRoot;
#endif
#ifndef _EDITOR
#ifndef ELocatorAPIH
if (strstr(Params, "-file_activity") != nullptr)
flags |= CLocatorAPI::flDumpFileActivity;
#endif
#endif
FS._initialize(flags, nullptr, fs_fname);
EFS._initialize();
}
init_counter++;
}
void xrCore::_destroy()
{
--init_counter;
if (0 == init_counter)
{
ZoneScoped;
FS._destroy();
EFS._destroy();
xr_FS = nullptr;
xr_EFS = nullptr;
if (trained_model)
{
void* buffer = trained_model->buffer();
xr_free(buffer);
xr_delete(trained_model);
}
TaskScheduler = nullptr;
xr_free(Params);
Memory._destroy();
#ifdef XR_PLATFORM_WINDOWS
CoUninitialize();
timeEndPeriod(1);
#endif
}
}
void SDLLogOutput(void* /*userdata*/, int category, SDL_LogPriority priority, const char* message)
{
pcstr from;
switch (category)
{
case SDL_LOG_CATEGORY_APPLICATION: from = "application"; break;
case SDL_LOG_CATEGORY_ERROR: from = "error"; break;
case SDL_LOG_CATEGORY_ASSERT: from = "assert"; break;
case SDL_LOG_CATEGORY_SYSTEM: from = "system"; break;
case SDL_LOG_CATEGORY_AUDIO: from = "audio"; break;
case SDL_LOG_CATEGORY_VIDEO: from = "video"; break;
case SDL_LOG_CATEGORY_RENDER: from = "render"; break;
case SDL_LOG_CATEGORY_INPUT: from = "input"; break;
case SDL_LOG_CATEGORY_TEST: from = "test"; break;
case SDL_LOG_CATEGORY_CUSTOM: from = "custom"; break;
default: from = "unknown"; break;
}
char mark;
pcstr type;
switch (priority)
{
case SDL_LOG_PRIORITY_VERBOSE: mark = '%'; type = "verbose"; break;
case SDL_LOG_PRIORITY_DEBUG: mark = '#'; type = "debug"; break;
case SDL_LOG_PRIORITY_INFO: mark = '='; type = "info"; break;
case SDL_LOG_PRIORITY_WARN: mark = '~'; type = "warn"; break;
case SDL_LOG_PRIORITY_ERROR: mark = '!'; type = "error"; break;
case SDL_LOG_PRIORITY_CRITICAL: mark = '$'; type = "critical"; break;
default: mark = ' '; type = "unknown"; break;
}
static constexpr pcstr format = "%c [sdl][%s][%s]: %s";
const size_t size = sizeof(mark) + sizeof(from) + sizeof(type) + sizeof(format) + sizeof(message);
pstr buf = (pstr)xr_alloca(size);
xr_sprintf(buf, size, format, mark, from, type, message);
Log(buf);
}