Skip to content

Commit 2032dea

Browse files
committed
Harden up commands.c and add some X macro setup for defense fields.
1 parent 212f7c5 commit 2032dea

3 files changed

Lines changed: 53 additions & 20 deletions

File tree

engine/source/defense_fields.def

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
DEF_FIELD_INT(block_damage_adjust)
2+
DEF_FIELD_INT(block_damage_max)
3+
DEF_FIELD_INT(block_damage_min)
4+
DEF_FIELD_INT(blockpower)
5+
DEF_FIELD_FLOAT(blockratio)
6+
DEF_FIELD_INT(blockthreshold)
7+
DEF_FIELD_INT(blocktype)
8+
DEF_FIELD_INT(damage_adjust)
9+
DEF_FIELD_INT(damage_max)
10+
DEF_FIELD_INT(damage_min)
11+
DEF_FIELD_INT(death_config_flags)
12+
DEF_FIELD_FLOAT(factor)
13+
DEF_FIELD_FLOAT(knockdown)
14+
DEF_FIELD_PTR(meta_data)
15+
DEF_FIELD_INT(meta_tag)
16+
DEF_FIELD_INT(pain)

engine/source/openborscript/commands.c

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,56 @@ void freeCommandList(List *list)
88
free(list);
99
}
1010

11-
// attention: modifies usercommand to lowercase
12-
void *getCommandlistCommand(List *list, char* usercommand)
11+
/**
12+
* getCommandlistCommand
13+
* ---------------------
14+
* Refactor - Caskey, Damon V.
15+
* 2025-05-17
16+
*
17+
* Looks up a command in the given list by its name, case-insensitively.
18+
*
19+
* Notes:
20+
* - The search is case-insensitive.
21+
* - Memory is dynamically allocated for the lowercase copy and freed before return.
22+
*/
23+
void* getCommandlistCommand(List* list, const char* usercommand)
1324
{
14-
if (!usercommand || !usercommand[0])
15-
{
16-
goto fail;
25+
if (!list || !usercommand || !usercommand[0]) {
26+
return NULL;
1727
}
18-
lc(usercommand, strlen(usercommand));
19-
Node *n = List_GetNodeByName(list, usercommand);
20-
if(n)
21-
{
22-
return n->value;
28+
29+
size_t len = strlen(usercommand);
30+
char* buf = (char*)malloc(len + 1);
31+
32+
if (!buf){
33+
return NULL;
2334
}
24-
fail:
25-
return NULL;
35+
36+
memcpy(buf, usercommand, len + 1); // Copy including null terminator
37+
lc(buf, len);
38+
39+
Node* n = List_GetNodeByName(list, buf);
40+
free(buf);
41+
42+
return n ? n->value : NULL;
2643
}
2744

28-
modelCommands getModelCommand(List *list, char* usercommand)
45+
modelCommands getModelCommand(List *list, const char* usercommand)
2946
{
3047
return (modelCommands) getCommandlistCommand(list, usercommand);
3148
}
3249

33-
modelstxtCommands getModelstxtCommand(List *list, char *usercommand)
50+
modelstxtCommands getModelstxtCommand(List *list, const char *usercommand)
3451
{
3552
return (modelstxtCommands) getCommandlistCommand(list, usercommand);
3653
}
3754

38-
levelCommands getLevelCommand(List *list, char *usercommand)
55+
levelCommands getLevelCommand(List *list, const char *usercommand)
3956
{
4057
return (levelCommands) getCommandlistCommand(list, usercommand);
4158
}
4259

43-
levelOrderCommands getLevelOrderCommand(List *list, char *usercommand)
60+
levelOrderCommands getLevelOrderCommand(List *list, const char *usercommand)
4461
{
4562
return (levelOrderCommands) getCommandlistCommand(list, usercommand);
4663
}

engine/source/openborscript/commands.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -794,16 +794,16 @@ typedef enum
794794
void freeCommandList(List *list);
795795

796796
List *createModelCommandList(void);
797-
modelCommands getModelCommand(List *list, char *usercommand);
797+
modelCommands getModelCommand(List *list, const char *usercommand);
798798

799799
List *createModelstxtCommandList(void);
800-
modelstxtCommands getModelstxtCommand(List *list, char *usercommand);
800+
modelstxtCommands getModelstxtCommand(List *list, const char *usercommand);
801801

802802
List *createLevelCommandList(void);
803-
levelCommands getLevelCommand(List *list, char *usercommand);
803+
levelCommands getLevelCommand(List *list, const char *usercommand);
804804

805805
List *createLevelOrderCommandList(void);
806-
levelOrderCommands getLevelOrderCommand(List *list, char *usercommand);
806+
levelOrderCommands getLevelOrderCommand(List *list, const char *usercommand);
807807

808808
#endif
809809

0 commit comments

Comments
 (0)