Nuclide
Software Development Kit for id Technology
global.h
1/*
2 * Copyright (c) 2016-2024 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
17var bool autocvar_g_logTimestamps = false;
18
19typedef enum
20{
21 LOGLEVEL_NONE,
22 LOGLEVEL_ERRORS,
23 LOGLEVEL_WARNINGS,
24 LOGLEVEL_DEBUG,
25} logLevel_t;
26
27#define LOGLEVEL_DEFAULT LOGLEVEL_WARNINGS
28var logLevel_t autocvar_g_logLevel = LOGLEVEL_DEFAULT;
29
30#define printf(...) print(sprintf(__VA_ARGS__))
31
32#ifdef DOXYGEN
34#define enumflags enum
35#endif
36
37void
38_NSLog(string msg)
39{
40 if (autocvar_g_logTimestamps)
41 print(sprintf("^9%f ^7%s\n", time, msg));
42 else
43 print(sprintf("^7%s\n", msg));
44}
45
46void
47_NSError(string functionName, string msg)
48{
49 if (autocvar_g_logTimestamps)
50 print(sprintf("^9%f ^1%s^7: %s\n", time, functionName, msg));
51 else
52 print(sprintf("^1%s^7: %s\n", functionName, msg));
53}
54
55void
56_NSWarning(string functionName, string msg)
57{
58 if (autocvar_g_logTimestamps)
59 print(sprintf("^9%f ^3%s^7: %s\n", time, functionName, msg));
60 else
61 print(sprintf("^3%s^7: %s\n", functionName, msg));
62}
63
64void
65_NSAssert(bool condition, string function, string descr)
66{
67 if (!condition) {
68 print(strcat("^1Assertion failed in ", function, ", reason: ", descr, "\n"));
69#ifndef MENU
70 breakpoint();
71#endif
72 }
73}
74
79#define NSLog(...) if (autocvar_g_logLevel >= LOGLEVEL_DEBUG) _NSLog(sprintf(__VA_ARGS__))
80
85#define NSError(...) if (autocvar_g_logLevel >= LOGLEVEL_ERRORS) _NSError(__FUNC__, sprintf(__VA_ARGS__))
86
91#define NSWarning(...) if (autocvar_g_logLevel >= LOGLEVEL_WARNINGS) _NSWarning(__FUNC__, sprintf(__VA_ARGS__))
92
99#define NSAssert(condition, ...) if (autocvar_g_logLevel >= LOGLEVEL_ERRORS) _NSAssert(condition, __FUNC__, sprintf(__VA_ARGS__))
100
101typedef enumflags
102{
103 SEARCH_INSENSITIVE,
104 SEARCH_FULLPACKAGE,
105 SEARCH_ALLOWDUPES,
106 SEARCH_FORCESEARCH,
107 SEARCH_MULTISEARCH,
108 SEARCH_NAMESORT
109} searchFlags_t;
110
111const vector g_vec_null = [0.0f, 0.0f, 0.0f];
112
113/* the console needs some attention too. */
114var float g_initTime;
115
116void
117InitPrint(string functionName)
118{
119 int chars = 51i;
120 int charsLeft;
121 int charExtra;
122 string sideLeft = "";
123 string sideRight = "";
124
125 if (functionName == __NULL__) {
126 NSLog("---------------------------------------------------");
127 return;
128 }
129
130 /* word and padding */
131 chars = chars - (int)strlen(functionName) - 2i;
132 charsLeft = chars / 2i;
133 charExtra = chars % 2i;
134
135 for (int i = 0i; i < charsLeft; i++)
136 sideLeft = strcat(sideLeft,"-");
137
138 for (int i = 0i; i < (charsLeft + charExtra); i++) {
139 sideRight = strcat(sideRight,"-");
140 }
141
142 NSLog( "%s %s %s", sideLeft, functionName, sideRight);
143}
144
145var string g_lastInitFunc;
146void
147_InitStart(string functionName)
148{
149 if (g_initTime != 0)
150 error(sprintf("Called InitStart() without InitEnd()ing %s!", g_lastInitFunc));
151
152 InitPrint(functionName);
153 g_lastInitFunc = functionName;
154 g_initTime = gettime(1);
155}
156
157#define InitStart() _InitStart(__FUNC__)
158
159void
160_InitEnd(void)
161{
162 float endTime = gettime(1);
163 NSLog("loaded in %.1f seconds", (endTime - g_initTime));
164 NSLog("---------------------------------------------------");
165 g_initTime = 0;
166}
167
168#define InitEnd() _InitEnd()
169
171#define entity_def(x, ...) const string x[] = { __VA_ARGS__ }
172
174#define thread(x) if (fork()) { x; abort(); }
175
176#define STRING_SET(x) ((x != __NULL__) && (x != ""))
177
178bool
179fileExists(string filePath)
180{
181 if (filePath != "") /* not empty */
182 if not(whichpack(filePath)) /* not present on disk */
183 return false;
184
185 return true;
186}
187
188/* other parts of shared to include */
189
190
191#include "teams.h"
192#include "events.h"
193#include "flags.h"
typedef enumflags
Defines the valid alignment flags for text fields.
Definition: font.h:37
noref const vector g_vec_null
Definition: math.h:36