Pad arguments for commands

FossilOrigin-Name: 852e3a48c314a7a6eb6e5c9894e439a4be10e4fc09164981c7ba27fededc84eb
This commit is contained in:
Jonathan Schleifer 2025-03-08 13:12:32 +00:00
parent a06c4638a3
commit f0b20e15d8
2 changed files with 56 additions and 13 deletions

View file

@ -2,6 +2,22 @@
#include <cube.h> #include <cube.h>
static OFArray<OFString *> *
padArguments(OFArray<OFString *> *arguments, size_t count)
{
OFMutableArray<OFString *> *copy;
if (arguments.count >= count)
return arguments;
copy = [arguments mutableCopy];
while (copy.count < count)
[copy addObject:@""];
[copy makeImmutable];
return copy;
}
@implementation Command @implementation Command
- (instancetype)initWithName:(OFString *)name - (instancetype)initWithName:(OFString *)name
function:(void (*)())function function:(void (*)())function
@ -19,83 +35,108 @@
{ {
switch (_argumentsTypes) { switch (_argumentsTypes) {
case ARG_1INT: case ARG_1INT:
if (isDown) if (isDown) {
arguments = padArguments(arguments, 2);
((void(__cdecl *)(int))_function)( ((void(__cdecl *)(int))_function)(
(int)[arguments[1] longLongValueWithBase:0]); (int)[arguments[1] longLongValueWithBase:0]);
}
break; break;
case ARG_2INT: case ARG_2INT:
if (isDown) if (isDown) {
arguments = padArguments(arguments, 3);
((void(__cdecl *)(int, int))_function)( ((void(__cdecl *)(int, int))_function)(
(int)[arguments[1] longLongValueWithBase:0], (int)[arguments[1] longLongValueWithBase:0],
(int)[arguments[2] longLongValueWithBase:0]); (int)[arguments[2] longLongValueWithBase:0]);
}
break; break;
case ARG_3INT: case ARG_3INT:
if (isDown) if (isDown) {
arguments = padArguments(arguments, 4);
((void(__cdecl *)(int, int, int))_function)( ((void(__cdecl *)(int, int, int))_function)(
(int)[arguments[1] longLongValueWithBase:0], (int)[arguments[1] longLongValueWithBase:0],
(int)[arguments[2] longLongValueWithBase:0], (int)[arguments[2] longLongValueWithBase:0],
(int)[arguments[3] longLongValueWithBase:0]); (int)[arguments[3] longLongValueWithBase:0]);
}
break; break;
case ARG_4INT: case ARG_4INT:
if (isDown) if (isDown) {
arguments = padArguments(arguments, 5);
((void(__cdecl *)(int, int, int, int))_function)( ((void(__cdecl *)(int, int, int, int))_function)(
(int)[arguments[1] longLongValueWithBase:0], (int)[arguments[1] longLongValueWithBase:0],
(int)[arguments[2] longLongValueWithBase:0], (int)[arguments[2] longLongValueWithBase:0],
(int)[arguments[3] longLongValueWithBase:0], (int)[arguments[3] longLongValueWithBase:0],
(int)[arguments[4] longLongValueWithBase:0]); (int)[arguments[4] longLongValueWithBase:0]);
}
break; break;
case ARG_NONE: case ARG_NONE:
if (isDown) if (isDown)
((void(__cdecl *)())_function)(); ((void(__cdecl *)())_function)();
break; break;
case ARG_1STR: case ARG_1STR:
if (isDown) if (isDown) {
arguments = padArguments(arguments, 2);
((void(__cdecl *)(OFString *))_function)(arguments[1]); ((void(__cdecl *)(OFString *))_function)(arguments[1]);
}
break; break;
case ARG_2STR: case ARG_2STR:
if (isDown) if (isDown) {
arguments = padArguments(arguments, 3);
((void(__cdecl *)(OFString *, OFString *))_function)( ((void(__cdecl *)(OFString *, OFString *))_function)(
arguments[1], arguments[2]); arguments[1], arguments[2]);
}
break; break;
case ARG_3STR: case ARG_3STR:
if (isDown) if (isDown) {
arguments = padArguments(arguments, 4);
((void(__cdecl *)( ((void(__cdecl *)(
OFString *, OFString *, OFString *))_function)( OFString *, OFString *, OFString *))_function)(
arguments[1], arguments[2], arguments[3]); arguments[1], arguments[2], arguments[3]);
}
break; break;
case ARG_5STR: case ARG_5STR:
if (isDown) if (isDown) {
arguments = padArguments(arguments, 6);
((void(__cdecl *)(OFString *, OFString *, OFString *, ((void(__cdecl *)(OFString *, OFString *, OFString *,
OFString *, OFString *))_function)(arguments[1], OFString *, OFString *))_function)(arguments[1],
arguments[2], arguments[3], arguments[4], arguments[2], arguments[3], arguments[4],
arguments[5]); arguments[5]);
}
break; break;
case ARG_DOWN: case ARG_DOWN:
((void(__cdecl *)(bool))_function)(isDown); ((void(__cdecl *)(bool))_function)(isDown);
break; break;
case ARG_DWN1: case ARG_DWN1:
arguments = padArguments(arguments, 2);
((void(__cdecl *)(bool, OFString *))_function)( ((void(__cdecl *)(bool, OFString *))_function)(
isDown, arguments[1]); isDown, arguments[1]);
break; break;
case ARG_1EXP: case ARG_1EXP:
if (isDown) if (isDown) {
arguments = padArguments(arguments, 2);
return ((int(__cdecl *)(int))_function)( return ((int(__cdecl *)(int))_function)(
execute(arguments[1])); execute(arguments[1]));
}
break; break;
case ARG_2EXP: case ARG_2EXP:
if (isDown) if (isDown) {
arguments = padArguments(arguments, 3);
return ((int(__cdecl *)(int, int))_function)( return ((int(__cdecl *)(int, int))_function)(
execute(arguments[1]), execute(arguments[2])); execute(arguments[1]), execute(arguments[2]));
}
break; break;
case ARG_1EST: case ARG_1EST:
if (isDown) if (isDown) {
arguments = padArguments(arguments, 2);
return ((int(__cdecl *)(OFString *))_function)( return ((int(__cdecl *)(OFString *))_function)(
arguments[1]); arguments[1]);
}
break; break;
case ARG_2EST: case ARG_2EST:
if (isDown) if (isDown) {
arguments = padArguments(arguments, 3);
return ((int(__cdecl *)(OFString *, return ((int(__cdecl *)(OFString *,
OFString *))_function)(arguments[1], arguments[2]); OFString *))_function)(arguments[1], arguments[2]);
}
break; break;
case ARG_VARI: case ARG_VARI:
if (isDown) if (isDown)

View file

@ -345,12 +345,14 @@ bool
execfile(OFString *cfgfile) execfile(OFString *cfgfile)
{ {
@autoreleasepool { @autoreleasepool {
OFString *command;
@try { @try {
execute([OFString stringWithContentsOfFile:cfgfile]); command = [OFString stringWithContentsOfFile:cfgfile];
} @catch (id e) { } @catch (id e) {
return false; return false;
} }
execute(command);
return true; return true;
} }
} }