Nuclide
Software Development Kit for id Technology (BETA)
defs.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
17#define bool float
18#define true 1
19#define false 0
20
25var bool autocvar_g_logTimestamps = false;
26
27typedef entity id;
28
29typedef enum
30{
31 LOGLEVEL_NONE,
32 LOGLEVEL_ERRORS,
33 LOGLEVEL_WARNINGS,
34 LOGLEVEL_DEBUG,
35} logLevel_t;
36
37#define LOGLEVEL_DEFAULT LOGLEVEL_WARNINGS
38var logLevel_t autocvar_g_logLevel = LOGLEVEL_DEFAULT;
39
40#define printf(...) print(sprintf(__VA_ARGS__))
41
42#ifdef DOXYGEN
44#define enumflags enum
45#endif
46
47void
48_NSLog(string msg)
49{
50 if (autocvar_g_logTimestamps)
51 print(sprintf("^9%f ^7%s\n", time, msg));
52 else
53 print(sprintf("^7%s\n", msg));
54}
55
56void
57_NSError(string functionName, string msg)
58{
59 if (autocvar_g_logTimestamps)
60 print(sprintf("^9%f ^1%s^7: %s\n", time, functionName, msg));
61 else
62 print(sprintf("^1%s^7: %s\n", functionName, msg));
63}
64
65void
66_NSWarning(string functionName, string msg)
67{
68 if (autocvar_g_logTimestamps)
69 print(sprintf("^9%f ^3%s^7: %s\n", time, functionName, msg));
70 else
71 print(sprintf("^3%s^7: %s\n", functionName, msg));
72}
73
74void
75_NSAssert(bool condition, string function, string descr)
76{
77 if (!condition) {
78 print(strcat("^1Assertion failed in ", function, ", reason: ", descr, "\n"));
79#ifndef MENU
80 breakpoint();
81#endif
82 }
83}
84
89#define NSLog(...) if (autocvar_g_logLevel >= LOGLEVEL_DEBUG) _NSLog(sprintf(__VA_ARGS__))
90
95#define NSError(...) if (autocvar_g_logLevel >= LOGLEVEL_ERRORS) _NSError(__FUNC__, sprintf(__VA_ARGS__))
96
101#define NSWarning(...) if (autocvar_g_logLevel >= LOGLEVEL_WARNINGS) _NSWarning(__FUNC__, sprintf(__VA_ARGS__))
102
109#define NSAssert(condition, ...) if (autocvar_g_logLevel >= LOGLEVEL_ERRORS) _NSAssert(condition, __FUNC__, sprintf(__VA_ARGS__))
110
111typedef enumflags
112{
113 SEARCH_INSENSITIVE,
114 SEARCH_FULLPACKAGE,
115 SEARCH_ALLOWDUPES,
116 SEARCH_FORCESEARCH,
117 SEARCH_MULTISEARCH,
118 SEARCH_NAMESORT
119} searchFlags_t;
120
121const vector g_vec_null = [0.0f, 0.0f, 0.0f];
122
123/* the console needs some attention too. */
124var float g_initTime;
125
126void
127InitPrint(string functionName)
128{
129 int chars = 51i;
130 int charsLeft;
131 int charExtra;
132 string sideLeft = "";
133 string sideRight = "";
134
135 if (functionName == __NULL__) {
136 NSLog("---------------------------------------------------");
137 return;
138 }
139
140 /* word and padding */
141 chars = chars - (int)strlen(functionName) - 2i;
142 charsLeft = chars / 2i;
143 charExtra = chars % 2i;
144
145 for (int i = 0i; i < charsLeft; i++)
146 sideLeft = strcat(sideLeft,"-");
147
148 for (int i = 0i; i < (charsLeft + charExtra); i++) {
149 sideRight = strcat(sideRight,"-");
150 }
151
152 NSLog( "%s %s %s", sideLeft, functionName, sideRight);
153}
154
155var string g_lastInitFunc;
156void
157_InitStart(string functionName)
158{
159 if (g_initTime != 0)
160 error(sprintf("Called InitStart() without InitEnd()ing %s!", g_lastInitFunc));
161
162 InitPrint(functionName);
163 g_lastInitFunc = functionName;
164 g_initTime = gettime(1);
165}
166
167#define InitStart() _InitStart(__FUNC__)
168
169void
170_InitEnd(void)
171{
172 float endTime = gettime(1);
173 NSLog("loaded in %.1f seconds", (endTime - g_initTime));
174 NSLog("---------------------------------------------------");
175 g_initTime = 0;
176}
177
178#define InitEnd() _InitEnd()
179
181#define entity_def(x, ...) const string x[] = { __VA_ARGS__ }
182
184#define thread(x) if (fork()) { x; abort(); }
185
186#define STRING_SET(x) ((x != __NULL__) && (x != ""))
187
188bool
189fileExists(string filePath)
190{
191 if (filePath != "") /* not empty */
192 if not(whichpack(filePath)) /* not present on disk */
193 return false;
194
195 return true;
196}
197string
198Util_ExtensionFromString(string inputString)
199{
200 int modelNameLength = strlen(inputString);
201 return substring(inputString, modelNameLength - 3, modelNameLength);
202}
203
204
205/* other parts of shared to include */
206
207#include "math.h"
208#include "memory.h"
209#include "platform.h"
210#include "colors.h"
211#include "materials.h"
212#include "teams.h"
213#include "events.h"
214#include "flags.h"
215
216 // end of common
typedef enumflags
Defines the valid alignment flags for text fields.
Definition: font.h:37
string Util_ExtensionFromString(string inputString)
Extract the file extension from a given file name string.
Definition: defs.h:198