Overview
Comment: | Clean up identifiers |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
d35fd65699e79d96fc0588fff075f73b |
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 | #import <ObjFW/ObjFW.h> OF_ASSUME_NONNULL_BEGIN | < < | < | | < < < > | < | 1 2 3 4 5 6 7 8 9 10 11 12 | #import <ObjFW/ObjFW.h> OF_ASSUME_NONNULL_BEGIN @interface Identifier : OFObject @property (readonly, copy, nonatomic) OFString *name; - (instancetype)init OF_UNAVAILABLE; - (instancetype)initWithName:(OFString *)name; @end OF_ASSUME_NONNULL_END |
Renamed and modified src/Ident.m [2e1b68d918] to src/Identifier.m [dfc38df5ef].
|
| | | > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 | #import "Identifier.h" @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 | // 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> | > > | > | | | | < < | | > > | > | | | | < < | < < < | | | | | > > > | | | | > | > > | > | < | > > < | < | < < | < | 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 "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 static OFMutableDictionary<OFString *, __kindof Identifier *> *identifiers; void alias(OFString *name, OFString *action) { Alias *alias = identifiers[name]; if (alias == nil) { alias = [[Alias alloc] initWithName:name action:action persisted:true]; if (identifiers == nil) identifiers = [[OFMutableDictionary alloc] init]; identifiers[name] = alias; } else { 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 (*function)(), bool persisted) { Variable *variable = [[Variable alloc] initWithName:name min:min max:max storage:storage function:function persisted:persisted]; if (identifiers == nil) identifiers = [[OFMutableDictionary alloc] init]; identifiers[name] = variable; return cur; } void setvar(OFString *name, int i) { *[identifiers[name] storage] = i; } int getvar(OFString *name) { return *[identifiers[name] storage]; } bool identexists(OFString *name) { return (identifiers[name] != nil); } OFString * getalias(OFString *name) { Alias *alias = identifiers[name]; if ([alias isKindOfClass:[Alias class]]) return alias.action; return nil; } bool addcommand(OFString *name, void (*function)(), int argumentsTypes) { Command *command = [[Command alloc] initWithName:name function:function argumentsTypes:argumentsTypes]; if (identifiers == nil) identifiers = [[OFMutableDictionary alloc] init]; identifiers[name] = command; return false; } char * parseexp(char *&p, int right) // parse any nested set of () or [] { |
︙ | ︙ | |||
167 168 169 170 171 172 173 | return newstring(word, p - word); } char * lookup(char *n) // find value of ident referenced with $ in exp { @autoreleasepool { | | | < < | | | < > | < < | 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 { __kindof Identifier *identifier = identifiers[@(n + 1)]; if ([identifier isKindOfClass:[Variable class]]) { string t; itoa(t, *[identifier storage]); return exchangestr(n, t); } 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 | char *c = w[0]; if (*c == '/') c++; // strip irc-style command prefix if (!*c) continue; // empty statement @autoreleasepool { | | | | > | < < < | < | < < < < < < < < < < < < < < < < < < < < | < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | < < < < < < < < < < | | < > | | > > | > | > | > | > | | 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 { __kindof Identifier *identifier = identifiers[@(c)]; if (identifier == nil) { val = ATOI(c); if (!val && *c != '0') conoutf(@"unknown command: %s", c); } else { if ([identifier isKindOfClass:[Command class]]) { // game defined commands // use very ad-hoc function signature, // and just call it val = [identifier callWithArguments:w numArguments:numargs isDown:isdown]; } else if ([identifier isKindOfClass:[Variable class]]) { // game defined variables if (isdown) { if (!w[1][0]) // var with no value // just prints its // current value conoutf(@"%s = %d", c, *[identifier storage]); else { if ([identifier min] > [identifier max]) { conoutf( @"variable " @"is " @"read-" @"only"); } else { int i1 = ATOI(w[1]); if (i1 < [identifier min] || i1 > [identifier max]) { // clamp // to // valid // range i1 = i1 < [identifier min] ? [identifier min] : [identifier max]; conoutf( @"v" @"a" @"l" @"i" @"d" @" " |
︙ | ︙ | |||
448 449 450 451 452 453 454 | @"%" @"d" @"." @"." @"%" @"d", c, | > | > | > | | > > | | | | | | < | | | | 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 min], [identifier max]); } *[identifier storage] = i1; } if ([identifier function] != NULL) // call trigger // function if // available ((void(__cdecl *)())[identifier function])(); } } } else if ([identifier isKindOfClass:[Alias class]]) { // alias, also used as functions and // (global) variables 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( [identifier action].UTF8String); val = execute(action, isdown); gp()->deallocstr(action); break; } } } loopj(numargs) gp()->deallocstr(w[j]); } return val; } // tab-completion of all identifiers int completesize = 0, completeidx = 0; void resetcomplete() { completesize = 0; |
︙ | ︙ | |||
529 530 531 532 533 534 535 | if (!completesize) { completesize = strlen(s) - 1; completeidx = 0; } __block int idx = 0; | | | | | | 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; [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, identifier.name.UTF8String); } }]; completeidx++; if (completeidx >= idx) completeidx = 0; |
︙ | ︙ | |||
598 599 600 601 602 603 604 | @"settings\n" @"// modify settings in game, or put settings in " @"autoexec.cfg to override anything\n" @"\n"]; writeclientinfo(stream); [stream writeString:@"\n"]; | | | > | | < | > > | | | | > > | | | 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"]; [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 writeString:@"\n"]; writebinds(stream); [stream writeString:@"\n"]; [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 | executable('client', [ 'Cube.mm', | > > | > | | 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', 'Identifier.m', 'KeyMapping.m', 'MD2.mm', 'MapModelInfo.m', 'Menu.m', 'MenuItem.m', 'Variable.m', 'client.mm', 'clientextras.mm', 'clientgame.mm', 'clients2c.mm', 'commands.mm', 'console.mm', 'editing.mm', 'entities.mm', 'init.mm', 'menus.mm', 'monster.mm', 'physics.mm', |
︙ | ︙ |