Cube  Diff

Differences From Artifact [56e5f5018d]:

To Artifact [a0d92d1a92]:


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









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













-
+

















-
-
+
+


-
+


-
+




















-
+






-
+







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

enum { ID_VAR, ID_COMMAND, ID_ALIAS };

@interface Ident : OFObject
@property (nonatomic) int type; // one of ID_* above
@property (nonatomic) char *name;
@property (nonatomic) int min, max;  // ID_VAR
@property (nonatomic) int *storage;  // ID_VAR
@property (nonatomic) void (*fun)(); // ID_VAR, ID_COMMAND
@property (nonatomic) int narg;      // ID_VAR, ID_COMMAND
@property (nonatomic) char *action;  // ID_ALIAS
@property (copy, nonatomic) OFString *name;
@property (nonatomic) int min, max;           // ID_VAR
@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
@property (nonatomic) bool persist;
@end

@implementation Ident
@end

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

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

// contains ALL vars/commands/aliases
OFMutableDictionary<OFString *, Ident *> *idents;

void
alias(char *name, char *action)
{
	@autoreleasepool {
		Ident *b = idents[@(name)];

		if (!b) {
			Ident *b = [[Ident alloc] init];
			b.type = ID_ALIAS;
			b.name = newstring(name);
			b.action = newstring(action);
			b.name = @(name);
			b.action = @(action);
			b.persist = true;

			idents[@(name)] = b;
			idents[b.name] = b;
		} else {
			if (b.type == ID_ALIAS)
				b.action = exchangestr(b.action, action);
				b.action = @(action);
			else
				conoutf(
				    @"cannot redefine builtin %s with an alias",
				    name);
		}
	}
}

COMMAND(alias, ARG_2STR)

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

	@autoreleasepool {
		Ident *v = [[Ident alloc] init];
		v.type = ID_VAR;
		v.name = name;
		v.name = @(name);
		v.min = min;
		v.max = max;
		v.storage = storage;
		v.fun = fun;
		v.persist = persist;

		idents[@(name)] = v;
		idents[v.name] = v;
	}

	return cur;
}

void
setvar(char *name, int i)
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
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







-
-
+
+

-
-
-
+
+
-



-
+











-
+







identexists(char *name)
{
	@autoreleasepool {
		return (idents[@(name)] != nil);
	}
}

char *
getalias(char *name)
OFString *
getalias(OFString *name)
{
	@autoreleasepool {
		Ident *i = idents[@(name)];
		return i != nil && i.type == ID_ALIAS ? i.action : NULL;
	Ident *i = idents[name];
	return i != nil && i.type == ID_ALIAS ? i.action : nil;
	}
}

bool
addcommand(char *name, void (*fun)(), int narg)
addcommand(OFString *name, void (*fun)(), int narg)
{
	if (idents == nil)
		idents = [[OFMutableDictionary alloc] init];

	@autoreleasepool {
		Ident *c = [[Ident alloc] init];
		c.type = ID_COMMAND;
		c.name = name;
		c.fun = fun;
		c.narg = narg;

		idents[@(name)] = c;
		idents[name] = c;
	}

	return false;
}

char *
parseexp(char *&p, int right) // parse any nested set of () or []
202
203
204
205
206
207
208
209

210
211
212
213
214
215
216
200
201
202
203
204
205
206

207
208
209
210
211
212
213
214







-
+







		if (ID != nil) {
			switch (ID.type) {
			case ID_VAR:
				string t;
				itoa(t, *(ID.storage));
				return exchangestr(n, t);
			case ID_ALIAS:
				return exchangestr(n, ID.action);
				return exchangestr(n, ID.action.UTF8String);
			}
		}
	}

	conoutf(@"unknown alias lookup: %s", n + 1);
	return n;
}
294
295
296
297
298
299
300
301

302
303
304
305
306
307
308
292
293
294
295
296
297
298

299
300
301
302
303
304
305
306







-
+







							    ATOI(w[4]));
						break;
					case ARG_NONE:
						if (isdown)
							((void(__cdecl *)())
							        ID.fun)();
						break;
					case ARG_1STR:
					case ARG_1CSTR:
						if (isdown)
							((void(__cdecl *)(
							    char *))ID.fun)(
							    w[1]);
						break;
					case ARG_2STR:
						if (isdown)
381
382
383
384
385
386
387











388
389
390
391
392
393
394
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







+
+
+
+
+
+
+
+
+
+
+







								strcat_s(
								    r, " ");
							}
							((void(__cdecl *)(
							    char *))ID.fun)(r);
							break;
						}
					case ARG_1STR:
						if (isdown) {
							@autoreleasepool {
								((void(
								    __cdecl *)(
								    OFString *))
								        ID.fun)(
								    @(w[1]));
							}
						}
						break;
					}
					break;

				// game defined variables
				case ID_VAR:
					if (isdown) {
						if (!w[1][0])
474
475
476
477
478
479
480
481


482
483
484
485
486
487
488
483
484
485
486
487
488
489

490
491
492
493
494
495
496
497
498







-
+
+







						// (global) arg values so
						// functions can access them
						sprintf_sd(t)("arg%d", i);
						alias(t, w[i]);
					}
					// create new string here because alias
					// could rebind itself
					char *action = newstring(ID.action);
					char *action =
					    newstring(ID.action.UTF8String);
					val = execute(action, isdown);
					gp()->deallocstr(action);
					break;
				}
			}
		}
		loopj(numargs) gp()->deallocstr(w[j]);
515
516
517
518
519
520
521
522

523
524
525

526
527
528
529
530
531
532
525
526
527
528
529
530
531

532
533
534

535
536
537
538
539
540
541
542







-
+


-
+







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

561
562
563
564
565
566
567
568


569
570
571
572
573
574
575
576
577
578




579
580
581
582
583
584

585
586
587
588
589
590
591
571
572
573
574
575
576
577

578
579
580
581
582
583
584
585
586



587
588
589
590
591
592
593
594
595

596
597
598
599
600
601
602
603







-
+
+







-
-
-
+
+
+
+





-
+







	           "settings\n// modify settings in game, or put settings in "
	           "autoexec.cfg to override anything\n\n");
	writeclientinfo(f);
	fprintf(f, "\n");
	[idents enumerateKeysAndObjectsUsingBlock:^(
	    OFString *name, Ident *ident, bool *stop) {
		if (ident.type == ID_VAR && ident.persist) {
			fprintf(f, "%s %d\n", ident.name, *ident.storage);
			fprintf(f, "%s %d\n", ident.name.UTF8String,
			    *ident.storage);
		}
	}];
	fprintf(f, "\n");
	writebinds(f);
	fprintf(f, "\n");
	[idents enumerateKeysAndObjectsUsingBlock:^(
	    OFString *name, Ident *ident, bool *stop) {
		if (ident.type == ID_ALIAS && !strstr(ident.name, "nextmap_")) {
			fprintf(
			    f, "alias \"%s\" [%s]\n", ident.name, ident.action);
		if (ident.type == ID_ALIAS &&
		    !strstr(ident.name.UTF8String, "nextmap_")) {
			fprintf(f, "alias \"%s\" [%s]\n", ident.name.UTF8String,
			    ident.action.UTF8String);
		}
	}];
	fclose(f);
}

COMMAND(writecfg, ARG_NONE);
COMMAND(writecfg, ARG_NONE)

// below the commands that implement a small imperative language. thanks to the
// semantics of
// () and [] expressions, any control construct can be defined trivially.

void
intset(char *name, int v)
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673









674
675
676
677
678
679
680


681
682
683
684
685
686


687
688
689
690
691
692


693
694
695
696
697
698


699
700
701
702
703
704


705
706
707
708
709
710


711
712
713
714
715
716


717
718
719
720
721
722

723
724
725
726
727
728
729

730
731
732
733
734
735
736

737
738
739
740
741
742
743

670
671
672
673
674
675
676









677
678
679
680
681
682
683
684
685
686
687
688
689
690
691

692
693
694
695
696
697
698

699
700
701
702
703
704
705

706
707
708
709
710
711
712

713
714
715
716
717
718
719

720
721
722
723
724
725
726

727
728
729
730
731
732
733

734
735
736
737
738
739
740

741
742
743
744
745
746
747

748
749
750
751
752
753
754

755
756
757
758
759
760
761

762







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






-
+
+





-
+
+





-
+
+





-
+
+





-
+
+





-
+
+





-
+
+





-
+






-
+






-
+






-
+
{
	int n = atoi(pos);
	loopi(n) s += strspn(s += strcspn(s, " \0"), " ");
	s[strcspn(s, " \0")] = 0;
	concat(s);
}

COMMANDN(loop, loopa, ARG_2STR);
COMMANDN(while, whilea, ARG_2STR);
COMMANDN(if, ifthen, ARG_3STR);
COMMAND(onrelease, ARG_DWN1);
COMMAND(exec, ARG_1STR);
COMMAND(concat, ARG_VARI);
COMMAND(concatword, ARG_VARI);
COMMAND(at, ARG_2STR);
COMMAND(listlen, ARG_1EST);
COMMANDN(loop, loopa, ARG_2STR)
COMMANDN(while, whilea, ARG_2STR)
COMMANDN(if, ifthen, ARG_3STR)
COMMAND(onrelease, ARG_DWN1)
COMMAND(exec, ARG_1CSTR)
COMMAND(concat, ARG_VARI)
COMMAND(concatword, ARG_VARI)
COMMAND(at, ARG_2STR)
COMMAND(listlen, ARG_1EST)

int
add(int a, int b)
{
	return a + b;
}
COMMANDN(+, add, ARG_2EXP);
COMMANDN(+, add, ARG_2EXP)

int
mul(int a, int b)
{
	return a * b;
}
COMMANDN(*, mul, ARG_2EXP);
COMMANDN(*, mul, ARG_2EXP)

int
sub(int a, int b)
{
	return a - b;
}
COMMANDN(-, sub, ARG_2EXP);
COMMANDN(-, sub, ARG_2EXP)

int
divi(int a, int b)
{
	return b ? a / b : 0;
}
COMMANDN(div, divi, ARG_2EXP);
COMMANDN(div, divi, ARG_2EXP)

int
mod(int a, int b)
{
	return b ? a % b : 0;
}
COMMAND(mod, ARG_2EXP);
COMMAND(mod, ARG_2EXP)

int
equal(int a, int b)
{
	return (int)(a == b);
}
COMMANDN(=, equal, ARG_2EXP);
COMMANDN(=, equal, ARG_2EXP)

int
lt(int a, int b)
{
	return (int)(a < b);
}
COMMANDN(<, lt, ARG_2EXP);
COMMANDN(<, lt, ARG_2EXP)

int
gt(int a, int b)
{
	return (int)(a > b);
}
COMMANDN(>, gt, ARG_2EXP);
COMMANDN(>, gt, ARG_2EXP)

int
strcmpa(char *a, char *b)
{
	return strcmp(a, b) == 0;
}
COMMANDN(strcmp, strcmpa, ARG_2EST);
COMMANDN(strcmp, strcmpa, ARG_2EST)

int
rndn(int a)
{
	return a > 0 ? rnd(a) : 0;
}
COMMANDN(rnd, rndn, ARG_1EXP);
COMMANDN(rnd, rndn, ARG_1EXP)

int
explastmillis()
{
	return lastmillis;
}
COMMANDN(millis, explastmillis, ARG_1EXP);
COMMANDN(millis, explastmillis, ARG_1EXP)