From fb8a872695232837844666cb0e477988cbd800c8 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Tue, 25 Mar 2025 23:38:08 +0000 Subject: [PATCH] Clean up Variable FossilOrigin-Name: 0ee94739cea768327664b83a23470a366e90ac45fd6c51537b5188268aa8e97c --- src/Command.h | 18 +++++----- src/Cube.m | 1 + src/Identifier.h | 5 ++- src/Identifier.m | 17 ++-------- src/Monster.m | 1 + src/Variable.h | 77 +++++++++++++++++++++++++++++++++++++++++-- src/clientgame.m | 1 + src/clients.m | 1 + src/commands.m | 69 ++++++++++++++++---------------------- src/cube.h | 45 ------------------------- src/editing.m | 25 ++++++++++---- src/physics.m | 1 + src/protos.h | 2 -- src/rendercubes.m | 1 + src/renderextras.m | 1 + src/rendergl.m | 1 + src/renderparticles.m | 1 + src/savegamedemo.m | 1 + src/sound.m | 1 + src/worldlight.m | 1 + 20 files changed, 145 insertions(+), 125 deletions(-) diff --git a/src/Command.h b/src/Command.h index 3b3ba0c..98a88c3 100644 --- a/src/Command.h +++ b/src/Command.h @@ -2,15 +2,15 @@ OF_ASSUME_NONNULL_BEGIN -#define COMMAND(name, nargs, block_) \ - OF_CONSTRUCTOR() \ - { \ - enqueueInit(^{ \ - [Identifier \ - addIdentifier:[Command commandWithName:@ #name \ - argumentsTypes:nargs \ - block:block_]]; \ - }); \ +#define COMMAND(name, nargs, block_) \ + OF_CONSTRUCTOR() \ + { \ + enqueueInit(^{ \ + Identifier.identifiers[@ #name] = \ + [Command commandWithName:@ #name \ + argumentsTypes:nargs \ + block:block_]; \ + }); \ } @interface Command: Identifier diff --git a/src/Cube.m b/src/Cube.m index ac74a4f..28211fe 100644 --- a/src/Cube.m +++ b/src/Cube.m @@ -4,6 +4,7 @@ #import "Command.h" #import "Player.h" +#import "Variable.h" OF_APPLICATION_DELEGATE(Cube) diff --git a/src/Identifier.h b/src/Identifier.h index af3274f..4348284 100644 --- a/src/Identifier.h +++ b/src/Identifier.h @@ -4,10 +4,9 @@ OF_ASSUME_NONNULL_BEGIN @interface Identifier: OFObject @property (readonly, copy, nonatomic) OFString *name; +@property (class, readonly, nonatomic) + OFMutableDictionary *identifiers; -+ (void)addIdentifier:(__kindof Identifier *)identifier; -+ (__kindof Identifier *)identifierForName:(OFString *)name; -+ (void)enumerateIdentifiersUsingBlock:(void (^)(__kindof Identifier *))block; - (instancetype)init OF_UNAVAILABLE; - (instancetype)initWithName:(OFString *)name; @end diff --git a/src/Identifier.m b/src/Identifier.m index e000cd4..6128868 100644 --- a/src/Identifier.m +++ b/src/Identifier.m @@ -10,22 +10,9 @@ static OFMutableDictionary *identifiers; identifiers = [[OFMutableDictionary alloc] init]; } -+ (void)addIdentifier:(__kindof Identifier *)identifier ++ (OFMutableDictionary *)identifiers { - identifiers[identifier.name] = identifier; -} - -+ (__kindof Identifier *)identifierForName:(OFString *)name -{ - return identifiers[name]; -} - -+ (void)enumerateIdentifiersUsingBlock:(void (^)(__kindof Identifier *))block -{ - [identifiers enumerateKeysAndObjectsUsingBlock:^( - OFString *name, __kindof Identifier *identifier, bool *stop) { - block(identifier); - }]; + return identifiers; } - (instancetype)initWithName:(OFString *)name diff --git a/src/Monster.m b/src/Monster.m index 0b4ce02..cfe42c6 100644 --- a/src/Monster.m +++ b/src/Monster.m @@ -6,6 +6,7 @@ #import "Entity.h" #import "Player.h" +#import "Variable.h" static OFMutableArray *monsters; static int nextmonster, spawnremain, numkilled, monstertotal, mtimestart; diff --git a/src/Variable.h b/src/Variable.h index 2796945..b26e743 100644 --- a/src/Variable.h +++ b/src/Variable.h @@ -2,24 +2,95 @@ OF_ASSUME_NONNULL_BEGIN +#define VARP(name, min_, cur, max_) \ + int name = cur; \ + \ + OF_CONSTRUCTOR() \ + { \ + enqueueInit(^{ \ + Variable *variable = \ + [Variable variableWithName:@ #name \ + min:min_ \ + max:max_ \ + storage:&name \ + function:NULL \ + persisted:true]; \ + Identifier.identifiers[@ #name] = variable; \ + }); \ + } +#define VAR(name, min_, cur, max_) \ + int name = cur; \ + \ + OF_CONSTRUCTOR() \ + { \ + enqueueInit(^{ \ + Variable *variable = \ + [Variable variableWithName:@ #name \ + min:min_ \ + max:max_ \ + storage:&name \ + function:NULL \ + persisted:false]; \ + Identifier.identifiers[@ #name] = variable; \ + }); \ + } +#define VARF(name, min_, cur, max_, body) \ + static void var_##name(); \ + static int name = cur; \ + \ + OF_CONSTRUCTOR() \ + { \ + enqueueInit(^{ \ + Variable *variable = \ + [Variable variableWithName:@ #name \ + min:min_ \ + max:max_ \ + storage:&name \ + function:var_##name \ + persisted:false]; \ + Identifier.identifiers[@ #name] = variable; \ + }); \ + } \ + \ + static void var_##name() { body; } +#define VARFP(name, min_, cur, max_, body) \ + static void var_##name(); \ + static int name = cur; \ + \ + OF_CONSTRUCTOR() \ + { \ + enqueueInit(^{ \ + Variable *variable = \ + [Variable variableWithName:@ #name \ + min:min_ \ + max:max_ \ + storage:&name \ + function:var_##name \ + persisted:true]; \ + Identifier.identifiers[@ #name] = variable; \ + }); \ + } \ + \ + static void var_##name() { body; } + @interface Variable: Identifier @property (readonly, nonatomic) int min, max; @property (readonly, nonatomic) int *storage; -@property (readonly, nonatomic) void (*__cdecl function)(); +@property (readonly, nullable, nonatomic) void (*function)(); @property (readonly, nonatomic) bool persisted; + (instancetype)variableWithName:(OFString *)name min:(int)min max:(int)max storage:(int *)storage - function:(void (*__cdecl)())function + function:(void (*_Nullable)())function persisted:(bool)persisted; - (instancetype)initWithName:(OFString *)name OF_UNAVAILABLE; - (instancetype)initWithName:(OFString *)name min:(int)min max:(int)max storage:(int *)storage - function:(void (*__cdecl)())function + function:(void (*_Nullable)())function persisted:(bool)persisted; - (void)printValue; - (void)setValue:(int)value; diff --git a/src/clientgame.m b/src/clientgame.m index 30ed77e..4fba916 100644 --- a/src/clientgame.m +++ b/src/clientgame.m @@ -8,6 +8,7 @@ #import "Monster.h" #import "OFString+Cube.h" #import "Player.h" +#import "Variable.h" int nextmode = 0; // nextmode becomes gamemode after next map load VAR(gamemode, 1, 0, 0); diff --git a/src/clients.m b/src/clients.m index e6bd9bf..67bc037 100644 --- a/src/clients.m +++ b/src/clients.m @@ -4,6 +4,7 @@ #import "Command.h" #import "Player.h" +#import "Variable.h" static ENetHost *clienthost = NULL; static int connecting = 0; diff --git a/src/commands.m b/src/commands.m index e2018b0..a8d6d42 100644 --- a/src/commands.m +++ b/src/commands.m @@ -18,12 +18,12 @@ cleanup(char **string) void alias(OFString *name, OFString *action) { - Alias *alias = [Identifier identifierForName:name]; + Alias *alias = Identifier.identifiers[name]; if (alias == nil) - [Identifier addIdentifier:[Alias aliasWithName:name - action:action - persisted:true]]; + Identifier.identifiers[name] = [Alias aliasWithName:name + action:action + persisted:true]; else { if ([alias isKindOfClass:Alias.class]) alias.action = action; @@ -37,23 +37,10 @@ COMMAND(alias, ARG_2STR, ^(OFString *name, OFString *action) { alias(name, action); }) -int -variable(OFString *name, int min, int cur, int max, int *storage, - void (*function)(), bool persisted) -{ - [Identifier addIdentifier:[Variable variableWithName:name - min:min - max:max - storage:storage - function:function - persisted:persisted]]; - return cur; -} - void setvar(OFString *name, int i) { - Variable *variable = [Identifier identifierForName:name]; + Variable *variable = Identifier.identifiers[name]; if ([variable isKindOfClass:Variable.class]) *variable.storage = i; @@ -62,7 +49,7 @@ setvar(OFString *name, int i) int getvar(OFString *name) { - Variable *variable = [Identifier identifierForName:name]; + Variable *variable = Identifier.identifiers[name]; if ([variable isKindOfClass:Variable.class]) return *variable.storage; @@ -73,13 +60,13 @@ getvar(OFString *name) bool identexists(OFString *name) { - return ([Identifier identifierForName:name] != nil); + return (Identifier.identifiers[name] != nil); } OFString * getalias(OFString *name) { - Alias *alias = [Identifier identifierForName:name]; + Alias *alias = Identifier.identifiers[name]; if ([alias isKindOfClass:Alias.class]) return alias.action; @@ -153,7 +140,7 @@ OFString * lookup(OFString *n) { __kindof Identifier *identifier = - [Identifier identifierForName:[n substringFromIndex:1]]; + Identifier.identifiers[[n substringFromIndex:1]]; if ([identifier isKindOfClass:Variable.class]) { return [OFString stringWithFormat:@"%d", *[identifier storage]]; @@ -260,7 +247,7 @@ execute(OFString *string, bool isDown) if (c.length == 0) continue; - val = executeIdentifier([Identifier identifierForName:c], + val = executeIdentifier(Identifier.identifiers[c], [OFArray arrayWithObjects:w count:numargs], isDown); } @@ -292,8 +279,8 @@ complete(OFMutableString *s) } __block int idx = 0; - [Identifier enumerateIdentifiersUsingBlock:^( - __kindof Identifier *identifier) { + [Identifier.identifiers enumerateKeysAndObjectsUsingBlock:^( + OFString *name, __kindof Identifier *identifier, bool *stop) { if (strncmp(identifier.name.UTF8String, s.UTF8String + 1, completesize) == 0 && idx++ == completeidx) @@ -359,29 +346,29 @@ writecfg() writeclientinfo(stream); [stream writeString:@"\n"]; - [Identifier - enumerateIdentifiersUsingBlock:^(__kindof Identifier *identifier) { - if (![identifier isKindOfClass:Variable.class] || - ![identifier persisted]) - return; + [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"]; writebinds(stream); [stream writeString:@"\n"]; - [Identifier - enumerateIdentifiersUsingBlock:^(__kindof Identifier *identifier) { - if (![identifier isKindOfClass:Alias.class] || - [identifier.name hasPrefix:@"nextmap_"]) - return; + [Identifier.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 writeFormat:@"alias \"%@\" [%@]\n", identifier.name, + [identifier action]]; + }]; [stream close]; } diff --git a/src/cube.h b/src/cube.h index 8adaf64..0064273 100644 --- a/src/cube.h +++ b/src/cube.h @@ -331,51 +331,6 @@ enum { ARG_VARI }; -// nasty macros for registering script functions, abuses globals to avoid -// excessive infrastructure -#define VARP(name, min, cur, max) \ - int name; \ - OF_CONSTRUCTOR() \ - { \ - enqueueInit(^{ \ - name = variable( \ - @ #name, min, cur, max, &name, NULL, true); \ - }); \ - } -#define VAR(name, min, cur, max) \ - int name; \ - OF_CONSTRUCTOR() \ - { \ - enqueueInit(^{ \ - name = variable( \ - @ #name, min, cur, max, &name, NULL, false); \ - }); \ - } -#define VARF(name, min, cur, max, body) \ - void var_##name(); \ - static int name; \ - OF_CONSTRUCTOR() \ - { \ - enqueueInit(^{ \ - name = variable( \ - @ #name, min, cur, max, &name, var_##name, false); \ - }); \ - } \ - void var_##name() { body; } -#define VARFP(name, min, cur, max, body) \ - void var_##name(); \ - static int name; \ - OF_CONSTRUCTOR() \ - { \ - enqueueInit(^{ \ - name = variable( \ - @ #name, min, cur, max, &name, var_##name, true); \ - }); \ - } \ - void var_##name() { body; } - -#define ATOI(s) strtol(s, NULL, 0) // supports hexadecimal numbers - #ifdef WIN32 # define WIN32_LEAN_AND_MEAN # include "windows.h" diff --git a/src/editing.m b/src/editing.m index 5a69c6d..45b7c8f 100644 --- a/src/editing.m +++ b/src/editing.m @@ -8,6 +8,7 @@ #import "Monster.h" #import "OFString+Cube.h" #import "Player.h" +#import "Variable.h" bool editmode = false; @@ -15,17 +16,27 @@ bool editmode = false; // invariant: all code assumes that these are kept inside MINBORD distance of // the edge of the map -struct block sel; +struct block sel = { 0, 0, 0, 0 }; OF_CONSTRUCTOR() { enqueueInit(^{ - sel = (struct block) { - variable(@"selx", 0, 0, 4096, &sel.x, NULL, false), - variable(@"sely", 0, 0, 4096, &sel.y, NULL, false), - variable(@"selxs", 0, 0, 4096, &sel.xs, NULL, false), - variable(@"selys", 0, 0, 4096, &sel.ys, NULL, false), - }; + static const struct { + OFString *name; + int *storage; + } vars[4] = { { @"selx", &sel.x }, { @"sely", &sel.y }, + { @"selxs", &sel.xs }, { @"selys", &sel.ys } }; + + for (size_t i = 0; i < 4; i++) { + Variable *variable = + [Variable variableWithName:vars[i].name + min:0 + max:4096 + storage:vars[i].storage + function:NULL + persisted:false]; + Identifier.identifiers[vars[i].name] = variable; + } }); } diff --git a/src/physics.m b/src/physics.m index 1db3d84..83ae106 100644 --- a/src/physics.m +++ b/src/physics.m @@ -11,6 +11,7 @@ #import "MapModelInfo.h" #import "Monster.h" #import "Player.h" +#import "Variable.h" // collide with player or monster static bool diff --git a/src/protos.h b/src/protos.h index a786c88..ec8d348 100644 --- a/src/protos.h +++ b/src/protos.h @@ -5,8 +5,6 @@ extern "C" { #endif // command -extern int variable(OFString *name, int min, int cur, int max, int *storage, - void (*fun)(), bool persist); extern void setvar(OFString *name, int i); extern int getvar(OFString *name); extern bool identexists(OFString *name); diff --git a/src/rendercubes.m b/src/rendercubes.m index 5feb70c..96cb728 100644 --- a/src/rendercubes.m +++ b/src/rendercubes.m @@ -4,6 +4,7 @@ #include "cube.h" #import "Command.h" +#import "Variable.h" static struct vertex *verts = NULL; int curvert; diff --git a/src/renderextras.m b/src/renderextras.m index 55e4af1..60598b9 100644 --- a/src/renderextras.m +++ b/src/renderextras.m @@ -5,6 +5,7 @@ #import "Command.h" #import "Entity.h" #import "Player.h" +#import "Variable.h" void line(int x1, int y1, float z1, int x2, int y2, float z2) diff --git a/src/rendergl.m b/src/rendergl.m index b9c001a..9d50f82 100644 --- a/src/rendergl.m +++ b/src/rendergl.m @@ -8,6 +8,7 @@ #import "Monster.h" #import "OFString+Cube.h" #import "Player.h" +#import "Variable.h" #ifdef DARWIN # define GL_COMBINE_EXT GL_COMBINE_ARB diff --git a/src/renderparticles.m b/src/renderparticles.m index 55b18e7..c9b4fd7 100644 --- a/src/renderparticles.m +++ b/src/renderparticles.m @@ -3,6 +3,7 @@ #include "cube.h" #import "Player.h" +#import "Variable.h" #define MAXPARTICLES 10500 const int NUMPARTCUTOFF = 20; diff --git a/src/savegamedemo.m b/src/savegamedemo.m index 75f7272..827d2e8 100644 --- a/src/savegamedemo.m +++ b/src/savegamedemo.m @@ -7,6 +7,7 @@ #import "Entity.h" #import "Monster.h" #import "Player.h" +#import "Variable.h" #ifdef OF_BIG_ENDIAN static const int islittleendian = 0; diff --git a/src/sound.m b/src/sound.m index 03ff3dc..8accdc6 100644 --- a/src/sound.m +++ b/src/sound.m @@ -2,6 +2,7 @@ #import "Command.h" #import "Player.h" +#import "Variable.h" #include diff --git a/src/worldlight.m b/src/worldlight.m index 578a3a5..cb09af9 100644 --- a/src/worldlight.m +++ b/src/worldlight.m @@ -5,6 +5,7 @@ #import "DynamicEntity.h" #import "Entity.h" #import "Monster.h" +#import "Variable.h" extern bool hasoverbright;