Cube  Check-in [d35fd65699]

Overview
Comment:Clean up identifiers
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: d35fd65699e79d96fc0588fff075f73b0dd7eb6ebfa0f6cffd0b0a3940e5fecd
User & Date: js on 2025-03-07 21:02:39
Other Links: manifest | tags
Context
2025-03-07
21:16
Clean up variables check-in: 5e43ae9916 user: js tags: trunk
21:02
Clean up identifiers check-in: d35fd65699 user: js tags: trunk
19:55
Migrate more strings check-in: 5ef6284dcf user: js tags: trunk
Changes

Added src/Alias.h version [257d20d71a].

Added src/Alias.m version [2901519ebd].

Added src/Command.h version [40b405f271].

Added src/Command.mm version [5f4325b0d6].

Renamed and modified src/Ident.h [e4880063ad] to src/Identifier.h [9069c402ca].

1
2
3
4
5
6
7

8
9
10


11
12
13
14


15
16
17
18
1
2
3
4



5



6
7




8
9

10
11
12




-
-
-
+
-
-
-
+
+
-
-
-
-
+
+
-



#import <ObjFW/ObjFW.h>

OF_ASSUME_NONNULL_BEGIN

enum IdentType { ID_VAR, ID_COMMAND, ID_ALIAS };

@interface Ident : OFObject
@interface Identifier : OFObject
@property (nonatomic) enum IdentType type;
@property (copy, nonatomic) OFString *name;
@property (nonatomic) int min, max;           // ID_VAR
@property (readonly, copy, nonatomic) OFString *name;

@property (nonatomic) int *storage;           // ID_VAR
@property (nonatomic) void (*fun)();          // ID_VAR, ID_COMMAND
@property (nonatomic) int narg;               // ID_VAR, ID_COMMAND
@property (copy, nonatomic) OFString *action; // ID_ALIAS
- (instancetype)init OF_UNAVAILABLE;
- (instancetype)initWithName:(OFString *)name;
@property (nonatomic) bool persist;
@end

OF_ASSUME_NONNULL_END

Renamed and modified src/Ident.m [2e1b68d918] to src/Identifier.m [dfc38df5ef].

1

2
3









4

1
2

3
4
5
6
7
8
9
10
11
12
-
+

-
+
+
+
+
+
+
+
+
+

#import "Ident.h"
#import "Identifier.h"

@implementation Ident
@implementation Identifier
- (instancetype)initWithName:(OFString *)name
{
	self = [super init];

	_name = [name copy];

	return self;
}
@end

Added src/Variable.h version [c2a873c551].

Added src/Variable.m version [f42a4c7759].

Renamed and modified src/command.mm [b47ac34d19] to src/commands.mm [e0ed2b9cbb].

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
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







+
+
-
+
+















-
+




-
+

-
-
+
+
-
-
-
-
+
+

+
+
-
+
+

-
-
+
+








-
-
+
+

-
-
-
+
-
-
-
-
-
-
-
-
+
+
+
+
+

+
+
+
-
+







-
+





-
+





-
+





+
-
-
+
+
+
+
+



-
+

-
-
+
+
+

-
-
+
-
-
+
-
-

-
+
-







// command.cpp: implements the parsing and execution of a tiny script language
// which is largely backwards compatible with the quake console language.

#include "cube.h"

#include <memory>

#import "Alias.h"
#import "Command.h"
#import "Ident.h"
#import "Identifier.h"
#import "Variable.h"

void
itoa(char *s, int i)
{
	sprintf_s(s)("%d", i);
}

char *
exchangestr(char *o, const char *n)
{
	gp()->deallocstr(o);
	return newstring(n);
}

// contains ALL vars/commands/aliases
OFMutableDictionary<OFString *, Ident *> *idents;
static OFMutableDictionary<OFString *, __kindof Identifier *> *identifiers;

void
alias(OFString *name, OFString *action)
{
	Ident *b = idents[name];
	Alias *alias = identifiers[name];

	if (b == nil) {
		Ident *b = [[Ident alloc] init];
	if (alias == nil) {
		alias = [[Alias alloc] initWithName:name
		b.type = ID_ALIAS;
		b.name = name;
		b.action = action;
		b.persist = true;
		                             action:action
		                          persisted:true];

		if (identifiers == nil)
			identifiers = [[OFMutableDictionary alloc] init];
		idents[b.name] = b;

		identifiers[name] = alias;
	} else {
		if (b.type == ID_ALIAS)
			b.action = action;
		if ([alias isKindOfClass:[Alias class]])
			alias.action = action;
		else
			conoutf(
			    @"cannot redefine builtin %@ with an alias", name);
	}
}
COMMAND(alias, ARG_2STR)

int
variable(OFString *name, int min, int cur, int max, int *storage, void (*fun)(),
    bool persist)
variable(OFString *name, int min, int cur, int max, int *storage,
    void (*function)(), bool persisted)
{
	if (idents == nil)
		idents = [[OFMutableDictionary alloc] init];

	Variable *variable = [[Variable alloc] initWithName:name
	Ident *v = [[Ident alloc] init];
	v.type = ID_VAR;
	v.name = name;
	v.min = min;
	v.max = max;
	v.storage = storage;
	v.fun = fun;
	v.persist = persist;
	                                                min:min
	                                                max:max
	                                            storage:storage
	                                           function:function
	                                          persisted:persisted];

	if (identifiers == nil)
		identifiers = [[OFMutableDictionary alloc] init];

	idents[name] = v;
	identifiers[name] = variable;

	return cur;
}

void
setvar(OFString *name, int i)
{
	*idents[name].storage = i;
	*[identifiers[name] storage] = i;
}

int
getvar(OFString *name)
{
	return *idents[name].storage;
	return *[identifiers[name] storage];
}

bool
identexists(OFString *name)
{
	return (idents[name] != nil);
	return (identifiers[name] != nil);
}

OFString *
getalias(OFString *name)
{
	Alias *alias = identifiers[name];
	Ident *i = idents[name];
	return i != nil && i.type == ID_ALIAS ? i.action : nil;

	if ([alias isKindOfClass:[Alias class]])
		return alias.action;

	return nil;
}

bool
addcommand(OFString *name, void (*fun)(), int narg)
addcommand(OFString *name, void (*function)(), int argumentsTypes)
{
	if (idents == nil)
		idents = [[OFMutableDictionary alloc] init];
	Command *command = [[Command alloc] initWithName:name
	                                        function:function
	                                  argumentsTypes:argumentsTypes];

	@autoreleasepool {
		Ident *c = [[Ident alloc] init];
	if (identifiers == nil)
		c.type = ID_COMMAND;
		c.name = name;
		identifiers = [[OFMutableDictionary alloc] init];
		c.fun = fun;
		c.narg = narg;

		idents[name] = c;
	identifiers[name] = command;
	}

	return false;
}

char *
parseexp(char *&p, int right) // parse any nested set of () or []
{
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
169
170
171
172
173
174
175

176
177

178





179
180
181


182
183


184
185
186
187
188
189
190







-
+

-
+
-
-
-
-
-
+
+
+
-
-
+
+
-
-







	return newstring(word, p - word);
}

char *
lookup(char *n) // find value of ident referenced with $ in exp
{
	@autoreleasepool {
		Ident *ID = idents[@(n + 1)];
		__kindof Identifier *identifier = identifiers[@(n + 1)];

		if (ID != nil) {
		if ([identifier isKindOfClass:[Variable class]]) {
			switch (ID.type) {
			case ID_VAR:
				string t;
				itoa(t, *(ID.storage));
				return exchangestr(n, t);
			string t;
			itoa(t, *[identifier storage]);
			return exchangestr(n, t);
			case ID_ALIAS:
				return exchangestr(n, ID.action.UTF8String);
		} else if ([identifier isKindOfClass:[Alias class]])
			return exchangestr(n, [identifier action].UTF8String);
			}
		}
	}

	conoutf(@"unknown alias lookup: %s", n + 1);
	return n;
}

int
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
394


395
396
397
398
399
400
401

402

403
404


405
406
407
408
409
410
411
412
413

414

415

416

417
418
419
420
421

422
423
424





425
426
427
428
429
430
431
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







-
+

-
+




-
-
+
+
+
-


-
-
-
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-






+
-
+

-
+
+









+
-
+

+
-
+





+
-
-
-
+
+
+
+
+







		char *c = w[0];
		if (*c == '/')
			c++; // strip irc-style command prefix
		if (!*c)
			continue; // empty statement

		@autoreleasepool {
			Ident *ID = idents[@(c)];
			__kindof Identifier *identifier = identifiers[@(c)];

			if (ID == nil) {
			if (identifier == nil) {
				val = ATOI(c);
				if (!val && *c != '0')
					conoutf(@"unknown command: %s", c);
			} else {
				switch (ID.type) {
				// game defined commands
				if ([identifier
				        isKindOfClass:[Command class]]) {
					// game defined commands
				case ID_COMMAND:
					// use very ad-hoc function signature,
					// and just call it
					switch (ID.narg) {
					case ARG_1INT:
						if (isdown)
					val = [identifier
							((void(__cdecl *)(
							    int))ID.fun)(
					    callWithArguments:w
							    ATOI(w[1]));
						break;
					case ARG_2INT:
						if (isdown)
							((void(__cdecl *)(
							    int, int))ID.fun)(
							    ATOI(w[1]),
							    ATOI(w[2]));
						break;
					case ARG_3INT:
						if (isdown)
							((void(__cdecl *)(int,
							    int, int))ID.fun)(
							    ATOI(w[1]),
							    ATOI(w[2]),
							    ATOI(w[3]));
						break;
					case ARG_4INT:
						if (isdown)
							((void(__cdecl *)(int,
							    int, int,
					         numArguments:numargs
							    int))ID.fun)(
							    ATOI(w[1]),
							    ATOI(w[2]),
							    ATOI(w[3]),
							    ATOI(w[4]));
						break;
					case ARG_NONE:
						if (isdown)
					               isDown:isdown];
							((void(__cdecl *)())
							        ID.fun)();
						break;
					case ARG_1STR:
						if (isdown) {
							@autoreleasepool {
								((void(
								    __cdecl *)(
								    OFString *))
								        ID.fun)(
								    @(w[1]));
							}
						}
						break;
					case ARG_2STR:
						if (isdown) {
							@autoreleasepool {
								((void(
								    __cdecl *)(
								    OFString *,
								    OFString *))
								        ID.fun)(
								    @(w[1]),
								    @(w[2]));
							}
						}
						break;
					case ARG_3STR:
						if (isdown) {
							@autoreleasepool {
								((void(
								    __cdecl *)(
								    OFString *,
								    OFString *,
								    OFString *))
								        ID.fun)(
								    @(w[1]),
								    @(w[2]),
								    @(w[3]));
							}
						}
						break;
					case ARG_5STR:
						if (isdown) {
							@autoreleasepool {
								((void(
								    __cdecl *)(
								    OFString *,
								    OFString *,
								    OFString *,
								    OFString *,
								    OFString *))
								        ID.fun)(
								    @(w[1]),
								    @(w[2]),
								    @(w[3]),
								    @(w[4]),
								    @(w[5]));
							}
						}
						break;
					case ARG_DOWN:
						((void(__cdecl *)(bool))ID.fun)(
						    isdown);
						break;
					case ARG_DWN1:
						((void(__cdecl *)(
						    bool, char *))ID.fun)(
						    isdown, w[1]);
						break;
					case ARG_1EXP:
						if (isdown)
							val = ((int(__cdecl *)(
							    int))ID.fun)(
							    execute(w[1]));
						break;
					case ARG_2EXP:
						if (isdown)
							val = ((int(__cdecl *)(
							    int, int))ID.fun)(
							    execute(w[1]),
							    execute(w[2]));
						break;
					case ARG_1EST:
						if (isdown)
							val = ((int(__cdecl *)(
							    char *))ID.fun)(
							    w[1]);
						break;
					case ARG_2EST:
						if (isdown)
							val = ((int(__cdecl *)(
							    char *,
							    char *))ID.fun)(
							    w[1], w[2]);
						break;
					case ARG_VARI:
						if (isdown) {
							// limit, remove
							string r;
							r[0] = 0;
							for (int i = 1;
							     i < numargs; i++) {
								// make
								// string-list
								// out of all
								// arguments
								strcat_s(
								    r, w[i]);
								if (i ==
								    numargs - 1)
				} else if ([identifier
				               isKindOfClass:[Variable
									break;
								strcat_s(
								    r, " ");
							}
							((void(__cdecl *)(
							    char *))ID.fun)(r);
							break;
						}
					}
					break;

				// game defined variables
				                                 class]]) {
					// game defined variables
				case ID_VAR:
					if (isdown) {
						if (!w[1][0])
							// var with no value
							// just prints its
							// current value
							conoutf(@"%s = %d", c,
							    *[identifier
							    *ID.storage);
							        storage]);
						else {
							if (ID.min > ID.max) {
							if ([identifier min] >
							    [identifier max]) {
								conoutf(
								    @"variable "
								    @"is "
								    @"read-"
								    @"only");
							} else {
								int i1 =
								    ATOI(w[1]);
								if (i1 <
								        [identifier
								        ID.min ||
								            min] ||
								    i1 >
								        [identifier
								        ID.max) {
								            max]) {
									// clamp
									// to
									// valid
									// range
									i1 =
									    i1 < [identifier
									    i1 < ID.min
									        ? ID.min
									        : ID.max;
									             min]
									        ? [identifier
									              min]
									        : [identifier
									              max];
									conoutf(
									    @"v"
									    @"a"
									    @"l"
									    @"i"
									    @"d"
									    @" "
448
449
450
451
452
453
454

455
456



457

458

459
460
461



462
463
464
465
466
467


468
469
470
471
472
473




474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490


491
492
493
494
495
496
497
498
499
500
501
502
503

504
505
506
507
508
509
510
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







+
-
-
+
+
+

+
-
+


-
+
+
+




-
-
+
+


-
-
-
-
+
+
+
+
-














-
-
+
+












-
+







									    @"%"
									    @"d"
									    @"."
									    @"."
									    @"%"
									    @"d",
									    c,
									    [identifier
									    ID.min,
									    ID.max);
									        min],
									    [identifier
									        max]);
								}
								*[identifier
								*ID.storage =
								    storage] =
								    i1;
							}
							if (ID.fun)
							if ([identifier
							        function] !=
							    NULL)
								// call trigger
								// function if
								// available
								((void(__cdecl
								        *)())ID
								        .fun)();
								        *)())[identifier
								        function])();
						}
					}
					break;

				// alias, also used as functions and (global)
				// variables
				} else if ([identifier
				               isKindOfClass:[Alias class]]) {
					// alias, also used as functions and
					// (global) variables
				case ID_ALIAS:
					for (int i = 1; i < numargs; i++) {
						@autoreleasepool {
							// set any arguments as
							// (global) arg values
							// so functions can
							// access them
							OFString *t = [OFString
							    stringWithFormat:
							        @"arg%d", i];
							alias(t, @(w[i]));
						}
					}
					// create new string here because alias
					// could rebind itself
					char *action =
					    newstring(ID.action.UTF8String);
					char *action = newstring(
					    [identifier action].UTF8String);
					val = execute(action, isdown);
					gp()->deallocstr(action);
					break;
				}
			}
		}
		loopj(numargs) gp()->deallocstr(w[j]);
	}

	return val;
}

// tab-completion of all idents
// tab-completion of all identifiers

int completesize = 0, completeidx = 0;

void
resetcomplete()
{
	completesize = 0;
529
530
531
532
533
534
535
536
537
538



539
540
541
542

543
544
545
546
547
548
549
388
389
390
391
392
393
394



395
396
397
398
399
400

401
402
403
404
405
406
407
408







-
-
-
+
+
+



-
+








		if (!completesize) {
			completesize = strlen(s) - 1;
			completeidx = 0;
		}

		__block int idx = 0;
		[idents enumerateKeysAndObjectsUsingBlock:^(
		    OFString *name, Ident *ident, bool *stop) {
			if (strncmp(ident.name.UTF8String, s + 1,
		[identifiers enumerateKeysAndObjectsUsingBlock:^(
		    OFString *name, Identifier *identifier, bool *stop) {
			if (strncmp(identifier.name.UTF8String, s + 1,
			        completesize) == 0 &&
			    idx++ == completeidx) {
				strcpy_s(s, "/");
				strcat_s(s, ident.name.UTF8String);
				strcat_s(s, identifier.name.UTF8String);
			}
		}];

		completeidx++;

		if (completeidx >= idx)
			completeidx = 0;
598
599
600
601
602
603
604
605
606
607
608





609
610



611
612
613
614
615
616
617
618
619
620
621
622








623
624
625
626
627
628
629
457
458
459
460
461
462
463




464
465
466
467
468


469
470
471
472
473
474
475
476
477






478
479
480
481
482
483
484
485
486
487
488
489
490
491
492







-
-
-
-
+
+
+
+
+
-
-
+
+
+






-
-
-
-
-
-
+
+
+
+
+
+
+
+







	            @"settings\n"
	            @"// modify settings in game, or put settings in "
	            @"autoexec.cfg to override anything\n"
	            @"\n"];
	writeclientinfo(stream);
	[stream writeString:@"\n"];

	[idents enumerateKeysAndObjectsUsingBlock:^(
	    OFString *name, Ident *ident, bool *stop) {
		if (ident.type == ID_VAR && ident.persist) {
			[stream
	[identifiers enumerateKeysAndObjectsUsingBlock:^(
	    OFString *name, __kindof Identifier *identifier, bool *stop) {
		if (![identifier isKindOfClass:[Variable class]] ||
		    ![identifier persisted])
			return;
			    writeFormat:@"%@ %d\n", ident.name, *ident.storage];
		}

		[stream writeFormat:@"%@ %d\n", identifier.name,
		        *[identifier storage]];
	}];
	[stream writeString:@"\n"];

	writebinds(stream);
	[stream writeString:@"\n"];

	[idents enumerateKeysAndObjectsUsingBlock:^(
	    OFString *name, Ident *ident, bool *stop) {
		if (ident.type == ID_ALIAS &&
		    ![ident.name hasPrefix:@"nextmap_"])
			[stream writeFormat:@"alias \"%@\" [%@]\n", ident.name,
			        ident.action];
	[identifiers enumerateKeysAndObjectsUsingBlock:^(
	    OFString *name, __kindof Identifier *identifier, bool *stop) {
		if (![identifier isKindOfClass:[Alias class]] ||
		    [identifier.name hasPrefix:@"nextmap_"])
			return;

		[stream writeFormat:@"alias \"%@\" [%@]\n", identifier.name,
		        [identifier action]];
	}];

	[stream close];
}

COMMAND(writecfg, ARG_NONE)

Modified src/meson.build from [784a6d82d7] to [4579a2b4bb].

1
2


3
4

5
6
7
8
9

10
11
12
13
14

15
16
17
18
19
20
21
1
2
3
4
5

6
7
8
9
10
11
12
13
14
15
16

17
18
19
20
21
22
23
24


+
+

-
+





+




-
+







executable('client',
  [
    'Alias.m',
    'Command.mm',
    'Cube.mm',
    'Ident.m',
    'Identifier.m',
    'KeyMapping.m',
    'MD2.mm',
    'MapModelInfo.m',
    'Menu.m',
    'MenuItem.m',
    'Variable.m',
    'client.mm',
    'clientextras.mm',
    'clientgame.mm',
    'clients2c.mm',
    'command.mm',
    'commands.mm',
    'console.mm',
    'editing.mm',
    'entities.mm',
    'init.mm',
    'menus.mm',
    'monster.mm',
    'physics.mm',