Nuclide
Software Development Kit for id Technology
platform.h
1/*
2 * Copyright (c) 2016-2022 Vera Visions LLC.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16
17typedef enum
18{
19 PLATFORM_UNINITIALIZED = -1,
20 PLATFORM_UNKNOWN = 0,
21 PLATFORM_PC,
22 PLATFORM_CONSOLE,
23 PLATFORM_TOUCH,
24 PLATFORM_WEB
25} platform;
26
27var platform g_platform = PLATFORM_UNINITIALIZED;
28
29/* fast way to query which system we're on */
30platform
31Platform_GetPlatform(void)
32{
33 if (g_platform == PLATFORM_UNINITIALIZED) {
34 string osname = cvar_string("sys_platform");
35 g_platform = PLATFORM_UNKNOWN;
36
37 switch (osname) {
38 case "Web":
39 g_platform = PLATFORM_WEB;
40 break;
41 case "WinCE":
42 case "Xbox":
43 g_platform = PLATFORM_CONSOLE;
44 break;
45 case "WinRT":
46 case "iOSSim":
47 case "iOS":
48 case "Android":
49 g_platform = PLATFORM_TOUCH;
50 break;
51 case "Unknown":
52 case "Nacl":
53 case "Win":
54 case "Win16":
55 case "Cygwin":
56 case "Linux":
57 case "Mac":
58 case "Apple":
59 case "FreeBSD":
60 case "OpenBSD":
61 case "NetBSD":
62 case "BSD":
63 case "MorphOS":
64 case "AmigaOS":
65 case "MacOS X":
66 case "Dos":
67 default:
68 g_platform = PLATFORM_PC;
69 break;
70 }
71 }
72
73 return g_platform;
74}
75
76bool
77Platform_FileInGamedir(string fileName, string gameDir)
78{
79 searchhandle fileSearch;
80 int fileCount = 0i;
81
82 fileSearch = search_begin(fileName, SEARCH_FULLPACKAGE, TRUE);
83 fileCount = search_getsize(fileSearch);
84
85 NSLog("Looking for %S in %S", fileName, gameDir);
86
87 /* doesn't exist */
88 if (fileCount <= 0) {
89 search_end(fileSearch);
90 return false;
91 }
92
93 for (int i = 0; i < fileCount; i++) {
94 string fileDir;
95 string fullPath = search_getpackagename(fileSearch, i);
96 fileDir = substring(fullPath, 0, strlen(gameDir)); /* only need to check same-ness */
97
98 if (fileDir == gameDir) {
99 NSLog("Found %S in %S", fileName, gameDir);
100 search_end(fileSearch);
101 return true;
102 }
103 }
104
105 NSError("Did not find %S in %S", fileName, gameDir);
106 search_end(fileSearch);
107
108 /* file exists but is in a different gamedir */
109 return false;
110}
111
112bool
113Platform_FileInCurrentGamedir(string fileName)
114{
115 string gameDir = cvar_string("game");
116 return Platform_FileInGamedir(fileName, gameDir);
117}