From 63a6c72954b14383d046520a843b267b37c00049 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Fri, 7 Mar 2025 21:16:47 +0000 Subject: [PATCH] Clean up variables FossilOrigin-Name: 5e43ae9916e0f61b00913c7f5c72f9349e1afecab908fd09b193edfd3f10006d --- src/Variable.h | 6 ++-- src/Variable.m | 21 ------------ src/Variable.mm | 57 ++++++++++++++++++++++++++++++++ src/commands.mm | 88 +++---------------------------------------------- src/meson.build | 2 +- 5 files changed, 67 insertions(+), 107 deletions(-) delete mode 100644 src/Variable.m create mode 100644 src/Variable.mm diff --git a/src/Variable.h b/src/Variable.h index 8fbbf1f..1ff4255 100644 --- a/src/Variable.h +++ b/src/Variable.h @@ -5,7 +5,7 @@ OF_ASSUME_NONNULL_BEGIN @interface Variable : Identifier @property (readonly, nonatomic) int min, max; @property (readonly, nonatomic) int *storage; -@property (readonly, nonatomic) void (*function)(); +@property (readonly, nonatomic) void (*__cdecl function)(); @property (readonly, nonatomic) bool persisted; - (instancetype)initWithName:(OFString *)name OF_UNAVAILABLE; @@ -13,8 +13,10 @@ OF_ASSUME_NONNULL_BEGIN min:(int)min max:(int)max storage:(int *)storage - function:(void (*)())function + function:(void (*__cdecl)())function persisted:(bool)persisted; +- (void)printValue; +- (void)setValue:(int)value; @end OF_ASSUME_NONNULL_END diff --git a/src/Variable.m b/src/Variable.m deleted file mode 100644 index 6d2c67d..0000000 --- a/src/Variable.m +++ /dev/null @@ -1,21 +0,0 @@ -#import "Variable.h" - -@implementation Variable -- (instancetype)initWithName:(OFString *)name - min:(int)min - max:(int)max - storage:(int *)storage - function:(void (*)())function - persisted:(bool)persisted -{ - self = [super initWithName:name]; - - _min = min; - _max = max; - _storage = storage; - _function = function; - _persisted = persisted; - - return self; -} -@end diff --git a/src/Variable.mm b/src/Variable.mm new file mode 100644 index 0000000..e3920e7 --- /dev/null +++ b/src/Variable.mm @@ -0,0 +1,57 @@ +#import "Variable.h" + +#include "cube.h" + +@implementation Variable +- (instancetype)initWithName:(OFString *)name + min:(int)min + max:(int)max + storage:(int *)storage + function:(void (*__cdecl)())function + persisted:(bool)persisted +{ + self = [super initWithName:name]; + + _min = min; + _max = max; + _storage = storage; + _function = function; + _persisted = persisted; + + return self; +} + +- (void)printValue +{ + conoutf(@"%@ = %d", self.name, *_storage); +} + +- (void)setValue:(int)value +{ + bool outOfRange = false; + + if (_min > _max) { + conoutf(@"variable is read-only"); + return; + } + + if (value < _min) { + value = _min; + outOfRange = true; + } + + if (value > _max) { + value = _max; + outOfRange = true; + } + + if (outOfRange) + conoutf(@"valid range for %@ is %d..%d", self.name, _min, _max); + + *_storage = value; + + if (_function != NULL) + // call trigger function if available + _function(); +} +@end diff --git a/src/commands.mm b/src/commands.mm index a546bc6..3c4fd7d 100644 --- a/src/commands.mm +++ b/src/commands.mm @@ -243,89 +243,11 @@ execute(char *p, bool isdown) // all evaluation happens here, recursively // 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" - @" " - @"r" - @"a" - @"n" - @"g" - @"e" - @" " - @"f" - @"o" - @"r" - @" " - @"%" - @"s" - @" " - @"i" - @"s" - @" " - @"%" - @"d" - @"." - @"." - @"%" - @"d", - c, - [identifier - min], - [identifier - max]); - } - *[identifier - storage] = - i1; - } - if ([identifier - function] != - NULL) - // call trigger - // function if - // available - ((void(__cdecl - *)())[identifier - function])(); - } + [identifier printValue]; + else + [identifier + setValue:ATOI( + w[1])]; } } else if ([identifier isKindOfClass:[Alias class]]) { diff --git a/src/meson.build b/src/meson.build index 6f88c40..0206458 100644 --- a/src/meson.build +++ b/src/meson.build @@ -9,7 +9,7 @@ executable('client', 'MapModelInfo.m', 'Menu.m', 'MenuItem.m', - 'Variable.m', + 'Variable.mm', 'client.mm', 'clientextras.mm', 'clientgame.mm',