Cube  Diff

Differences From Artifact [45f7f8cf15]:

To Artifact [e072f98aae]:


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







-
-
-
+
+
+

-
+







-
+








-
+








-
+
















-
+








void
alias(OFString *name, OFString *action)
{
	Alias *alias = Identifier.identifiers[name];

	if (alias == nil)
		Identifier.identifiers[name] = [Alias aliasWithName:name
		                                             action:action
		                                          persisted:true];
		Identifier.identifiers[name] = [Alias aliasWithName: name
		                                             action: action
		                                          persisted: true];
	else {
		if ([alias isKindOfClass:Alias.class])
		if ([alias isKindOfClass: Alias.class])
			alias.action = action;
		else
			conoutf(
			    @"cannot redefine builtin %@ with an alias", name);
	}
}

COMMAND(alias, ARG_2STR, ^(OFString *name, OFString *action) {
COMMAND(alias, ARG_2STR, ^ (OFString *name, OFString *action) {
	alias(name, action);
})

void
setvar(OFString *name, int i)
{
	Variable *variable = Identifier.identifiers[name];

	if ([variable isKindOfClass:Variable.class])
	if ([variable isKindOfClass: Variable.class])
		*variable.storage = i;
}

int
getvar(OFString *name)
{
	Variable *variable = Identifier.identifiers[name];

	if ([variable isKindOfClass:Variable.class])
	if ([variable isKindOfClass: Variable.class])
		return *variable.storage;

	return 0;
}

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

OFString *
getalias(OFString *name)
{
	Alias *alias = Identifier.identifiers[name];

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

	return nil;
}

// parse any nested set of () or []
static char *
94
95
96
97
98
99
100
101
102


103
104
105
106
107
108
109
94
95
96
97
98
99
100


101
102
103
104
105
106
107
108
109







-
-
+
+







			return NULL;
		}
	}
	char *s = strndup(word, *p - word - 1);
	if (left == '(') {
		OFString *t;
		@try {
			t = [OFString
			    stringWithFormat:@"%d", execute(@(s), true)];
			t = [OFString stringWithFormat:
			    @"%d", execute(@(s), true)];
		} @finally {
			free(s);
		}
		s = strdup(t.UTF8String);
	}
	return s;
}
136
137
138
139
140
141
142
143

144
145
146
147




148
149
150

151
152
153
154
155
156
157
158
159
160

161
162
163
164
165
166
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

193
194
195
196
197
198
199
136
137
138
139
140
141
142

143
144



145
146
147
148
149
150

151
152
153
154
155
156
157
158
159
160

161
162
163
164
165
166
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

193
194
195
196
197
198
199
200







-
+

-
-
-
+
+
+
+


-
+









-
+









-
+


-
+

-
+







-
-
+
+


-
+




-
+







}

// find value of ident referenced with $ in exp
OFString *
lookup(OFString *n)
{
	__kindof Identifier *identifier =
	    Identifier.identifiers[[n substringFromIndex:1]];
	    Identifier.identifiers[[n substringFromIndex: 1]];

	if ([identifier isKindOfClass:Variable.class]) {
		return [OFString stringWithFormat:@"%d", *[identifier storage]];
	} else if ([identifier isKindOfClass:Alias.class])
	if ([identifier isKindOfClass: Variable.class]) {
		return [OFString stringWithFormat:
		    @"%d", *[identifier storage]];
	} else if ([identifier isKindOfClass: Alias.class])
		return [identifier action];

	conoutf(@"unknown alias lookup: %@", [n substringFromIndex:1]);
	conoutf(@"unknown alias lookup: %@", [n substringFromIndex: 1]);
	return n;
}

int
executeIdentifier(__kindof Identifier *identifier,
    OFArray<OFString *> *arguments, bool isDown)
{
	if (identifier == nil) {
		@try {
			return [arguments[0] intValueWithBase:0];
			return [arguments[0] intValueWithBase: 0];
		} @catch (OFInvalidFormatException *e) {
			conoutf(@"unknown command: %@", arguments[0]);
			return 0;
		} @catch (OFOutOfRangeException *e) {
			conoutf(@"invalid value: %@", arguments[0]);
			return 0;
		}
	}

	if ([identifier isKindOfClass:Command.class])
	if ([identifier isKindOfClass: Command.class])
		// game defined commands use very ad-hoc function signature,
		// and just call it
		return [identifier callWithArguments:arguments isDown:isDown];
		return [identifier callWithArguments: arguments isDown: isDown];

	if ([identifier isKindOfClass:Variable.class]) {
	if ([identifier isKindOfClass: Variable.class]) {
		if (!isDown)
			return 0;

		// game defined variables
		if (arguments.count < 2 || arguments[1].length == 0)
			[identifier printValue];
		else
			[identifier
			    setValue:[arguments[1] cube_intValueWithBase:0]];
			[identifier setValue:
			    [arguments[1] cube_intValueWithBase: 0]];
	}

	if ([identifier isKindOfClass:Alias.class]) {
	if ([identifier isKindOfClass: Alias.class]) {
		// alias, also used as functions and (global) variables
		for (int i = 1; i < arguments.count; i++) {
			// set any arguments as (global) arg values so
			// functions can access them
			OFString *t = [OFString stringWithFormat:@"arg%d", i];
			OFString *t = [OFString stringWithFormat: @"arg%d", i];
			alias(t, arguments[i]);
		}

		return execute([identifier action], isDown);
	}

	return 0;
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
432
433
434
435
436
437
438
439
440
441

442
443
444
445
446
447
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
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
432
433
434
435
436
437
438
439
440
441
442
443

444
445
446
447
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







-
-
+
+







-
+


















-
-
+
+










-
-
+
+

-
+
-
-
-
+
+
+













-
+














-
+

-
+



-
+









-
-
-
+
+
+
+




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

-
+

-
-
-
+
+
+



-
-
+
+

-
+


-
+

-
-
-
-
+
+
+
+


-
-
+
+





-
+










-
+


-
-
+
+


-
+








-
+




-
+










-
+



-
-
+
+


-
+













-
+












-
+



-
+



-
+



-
+



-
+



-
+



-
+



-
+



-
-
+
+


-
+



-
+


		}

		p += strcspn(p, ";\n\0");
		// more statements if this isn't the end of the string
		cont = *p++ != 0;
		OFString *c = w[0];
		// strip irc-style command prefix
		if ([c hasPrefix:@"/"]) {
			c = [c substringFromIndex:1];
		if ([c hasPrefix: @"/"]) {
			c = [c substringFromIndex: 1];
			w[0] = c;
		}
		// empty statement
		if (c.length == 0)
			continue;

		val = executeIdentifier(Identifier.identifiers[c],
		    [OFArray arrayWithObjects:w count:numargs], isDown);
		    [OFArray arrayWithObjects: w count: numargs], isDown);
	}

	return val;
}

// tab-completion of all identifiers

int completesize = 0, completeidx = 0;

void
resetcomplete()
{
	completesize = 0;
}

void
complete(OFMutableString *s)
{
	if (![s hasPrefix:@"/"])
		[s insertString:@"/" atIndex:0];
	if (![s hasPrefix: @"/"])
		[s insertString: @"/" atIndex: 0];

	if (s.length == 1)
		return;

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

	__block int idx = 0;
	[Identifier.identifiers enumerateKeysAndObjectsUsingBlock:^(
	    OFString *name, __kindof Identifier *identifier, bool *stop) {
	[Identifier.identifiers enumerateKeysAndObjectsUsingBlock:
	    ^ (OFString *name, __kindof Identifier *identifier, bool *stop) {
		if (strncmp(identifier.name.UTF8String, s.UTF8String + 1,
		        completesize) == 0 &&
		    completesize) == 0 && idx++ == completeidx)
		    idx++ == completeidx)
			[s replaceCharactersInRange:OFMakeRange(1, s.length - 1)
			                 withString:identifier.name];
			[s replaceCharactersInRange: OFMakeRange(
							 1, s.length - 1)
					 withString: identifier.name];
	}];

	completeidx++;

	if (completeidx >= idx)
		completeidx = 0;
}

bool
execfile(OFIRI *cfgfile)
{
	OFString *command;
	@try {
		command = [OFString stringWithContentsOfIRI:cfgfile];
		command = [OFString stringWithContentsOfIRI: cfgfile];
	} @catch (OFOpenItemFailedException *e) {
		return false;
	} @catch (OFReadFailedException *e) {
		return false;
	}

	execute(command, true);
	return true;
}

void
exec(OFString *cfgfile)
{
	if (!execfile([Cube.sharedInstance.userDataIRI
	        IRIByAppendingPathComponent:cfgfile]) &&
	    IRIByAppendingPathComponent: cfgfile]) &&
	    !execfile([Cube.sharedInstance.gameDataIRI
	        IRIByAppendingPathComponent:cfgfile]))
	    IRIByAppendingPathComponent: cfgfile]))
		conoutf(@"could not read \"%@\"", cfgfile);
}

COMMAND(exec, ARG_1STR, ^(OFString *cfgfile) {
COMMAND(exec, ARG_1STR, ^ (OFString *cfgfile) {
	exec(cfgfile);
})

void
writecfg()
{
	OFStream *stream;
	@try {
		OFIRI *IRI = [Cube.sharedInstance.userDataIRI
		    IRIByAppendingPathComponent:@"config.cfg"];
		stream = [[OFIRIHandler handlerForIRI:IRI] openItemAtIRI:IRI
		                                                    mode:@"w"];
		    IRIByAppendingPathComponent: @"config.cfg"];
		stream = [[OFIRIHandler handlerForIRI: IRI]
		    openItemAtIRI: IRI
			     mode: @"w"];
	} @catch (id e) {
		return;
	}

	[stream writeString:
	[stream writeString:@"// automatically written on exit, do not modify\n"
	                    @"// delete this file to have defaults.cfg "
	                    @"overwrite these settings\n"
	                    @"// modify settings in game, or put settings in "
	                    @"autoexec.cfg to override anything\n"
	                    @"\n"];
	    @"// automatically written on exit, do not modify\n"
	    @"// delete this file to have defaults.cfg overwrite these "
	    @"settings\n"
	    @"// modify settings in game, or put settings in autoexec.cfg to "
	    @"override anything\n"
	    @"\n"];
	writeclientinfo(stream);
	[stream writeString:@"\n"];
	[stream writeString: @"\n"];

	[Identifier.identifiers enumerateKeysAndObjectsUsingBlock:^(
	    OFString *name, __kindof Identifier *identifier, bool *stop) {
		if (![identifier isKindOfClass:Variable.class] ||
	[Identifier.identifiers enumerateKeysAndObjectsUsingBlock:
	    ^ (OFString *name, __kindof Identifier *identifier, bool *stop) {
		if (![identifier isKindOfClass: Variable.class] ||
		    ![identifier persisted])
			return;

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

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

	[Identifier.identifiers enumerateKeysAndObjectsUsingBlock:^(
	    OFString *name, __kindof Identifier *identifier, bool *stop) {
		if (![identifier isKindOfClass:Alias.class] ||
		    [identifier.name hasPrefix:@"nextmap_"])
	[Identifier.identifiers enumerateKeysAndObjectsUsingBlock:
	    ^ (OFString *name, Alias *alias, bool *stop) {
		if (![alias isKindOfClass: Alias.class] ||
		    [alias.name hasPrefix: @"nextmap_"])
			return;

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

	[stream close];
}

COMMAND(writecfg, ARG_NONE, ^{
COMMAND(writecfg, ARG_NONE, ^ {
	writecfg();
})

// 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(OFString *name, int v)
{
	alias(name, [OFString stringWithFormat:@"%d", v]);
	alias(name, [OFString stringWithFormat: @"%d", v]);
}

COMMAND(if, ARG_3STR, ^(OFString *cond, OFString *thenp, OFString *elsep) {
	execute((![cond hasPrefix:@"0"] ? thenp : elsep), true);
COMMAND(if, ARG_3STR, ^ (OFString *cond, OFString *thenp, OFString *elsep) {
	execute((![cond hasPrefix: @"0"] ? thenp : elsep), true);
})

COMMAND(loop, ARG_2STR, ^(OFString *times, OFString *body) {
COMMAND(loop, ARG_2STR, ^ (OFString *times, OFString *body) {
	int t = times.cube_intValue;

	for (int i = 0; i < t; i++) {
		intset(@"i", i);
		execute(body, true);
	}
})

COMMAND(while, ARG_2STR, ^(OFString *cond, OFString *body) {
COMMAND(while, ARG_2STR, ^ (OFString *cond, OFString *body) {
	while (execute(cond, true))
		execute(body, true);
})

COMMAND(onrelease, ARG_DWN1, ^(bool on, OFString *body) {
COMMAND(onrelease, ARG_DWN1, ^ (bool on, OFString *body) {
	if (!on)
		execute(body, true);
})

void
concat(OFString *s)
{
	alias(@"s", s);
}

COMMAND(concat, ARG_VARI, ^(OFString *s) {
COMMAND(concat, ARG_VARI, ^ (OFString *s) {
	concat(s);
})

COMMAND(concatword, ARG_VARI, ^(OFString *s) {
	concat([s stringByReplacingOccurrencesOfString:@" " withString:@""]);
COMMAND(concatword, ARG_VARI, ^ (OFString *s) {
	concat([s stringByReplacingOccurrencesOfString: @" " withString: @""]);
})

COMMAND(listlen, ARG_1EST, ^(OFString *a_) {
COMMAND(listlen, ARG_1EST, ^ (OFString *a_) {
	const char *a = a_.UTF8String;

	if (!*a)
		return 0;

	int n = 0;
	while (*a)
		if (*a++ == ' ')
			n++;

	return n + 1;
})

COMMAND(at, ARG_2STR, ^(OFString *s_, OFString *pos) {
COMMAND(at, ARG_2STR, ^ (OFString *s_, OFString *pos) {
	int n = pos.cube_intValue;
	char *copy __attribute__((__cleanup__(cleanup))) =
	    strdup(s_.UTF8String);
	char *s = copy;
	for (int i = 0; i < n; i++) {
		s += strcspn(s, " \0");
		s += strspn(s, " ");
	}
	s[strcspn(s, " \0")] = 0;
	concat(@(s));
})

COMMAND(+, ARG_2EXP, ^(int a, int b) {
COMMAND(+, ARG_2EXP, ^ (int a, int b) {
	return a + b;
})

COMMAND(*, ARG_2EXP, ^(int a, int b) {
COMMAND(*, ARG_2EXP, ^ (int a, int b) {
	return a * b;
})

COMMAND(-, ARG_2EXP, ^(int a, int b) {
COMMAND(-, ARG_2EXP, ^ (int a, int b) {
	return a - b;
})

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

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

COMMAND(=, ARG_2EXP, ^(int a, int b) {
COMMAND(=, ARG_2EXP, ^ (int a, int b) {
	return (int)(a == b);
})

COMMAND(<, ARG_2EXP, ^(int a, int b) {
COMMAND(<, ARG_2EXP, ^ (int a, int b) {
	return (int)(a < b);
})

COMMAND(>, ARG_2EXP, ^(int a, int b) {
COMMAND(>, ARG_2EXP, ^ (int a, int b) {
	return (int)(a > b);
})

COMMAND(strcmp, ARG_2EST, ^(OFString *a, OFString *b) {
	return [a isEqual:b];
COMMAND(strcmp, ARG_2EST, ^ (OFString *a, OFString *b) {
	return [a isEqual: b];
})

COMMAND(rnd, ARG_1EXP, ^(int a) {
COMMAND(rnd, ARG_1EXP, ^ (int a) {
	return (a > 0 ? rnd(a) : 0);
})

COMMAND(millis, ARG_1EXP, ^(int unused) {
COMMAND(millis, ARG_1EXP, ^ (int unused) {
	return lastmillis;
})