From ed2f2d825585b13b387651cd739a7864640b92ed Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Sat, 31 Mar 2018 17:07:59 +0200 Subject: [PATCH] also write private list from games.xml - also write private list from games.xml, prevent sneaky mistakes - rewrite some things a better way - close files --- CMakeLists.txt | 4 +- src/defs.h | 2 +- src/game.c | 48 +----------- src/game.h | 2 + src/games.xml | 51 ++----------- src/gamesxml2c.c | 188 ++++++++++++++++++++++++++++++++--------------- src/pixmaps.c | 2 +- 7 files changed, 145 insertions(+), 152 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ae65711..c84d8f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -506,7 +506,9 @@ add_custom_command ( DEPENDS gamesxml2c) # Define games.c filename for including in src/game.c -add_definitions (-DGAMES_INCLUDE="${CMAKE_BINARY_DIR}/games.c") +add_definitions (-DGAMES_C_INCLUDE="${CMAKE_BINARY_DIR}/games.c") +add_definitions (-DGAMES_H_INCLUDE="${CMAKE_BINARY_DIR}/games.h") +add_definitions (-DICONS_C_INCLUDE="${CMAKE_BINARY_DIR}/icons.c") # Make src/game.c depend on games.c so that src/game.c is rebuilt when games.c changes, # this must be manually specified due to the include name being a define diff --git a/src/defs.h b/src/defs.h index f2714a6..00fffd9 100644 --- a/src/defs.h +++ b/src/defs.h @@ -26,7 +26,7 @@ #include #include -#include "games.h" +#include GAMES_H_INCLUDE // max 0x8000, server->flags is unsigned enum server_flags { diff --git a/src/game.c b/src/game.c index e6f352c..e966271 100644 --- a/src/game.c +++ b/src/game.c @@ -154,53 +154,7 @@ struct unreal_private const char* suffix; }; - -static struct quake_private alienarena_private; -static struct quake_private cod_private; -static struct quake_private coduo_private; -static struct quake_private cod2_private; -static struct quake_private cod4_private; -static struct quake_private dday_private; -static struct quake_private doom3_private; -static struct quake_private etl_private; -static struct quake_private etqw_private; -static struct quake_private hl_private; -static struct quake_private iourt_private; -static struct quake_private jk2_private; -static struct quake_private jk3_private; -static struct quake_private mohaa_private; -static struct quake_private nexuiz_private; -static struct quake_private openarena_private; -static struct quake_private q1_private; -static struct quake_private q2_private; -static struct quake_private q3_private; -static struct quake_private q3rally_private; -static struct quake_private quake4_private; -static struct quake_private quetoo_private; -static struct quake_private qw_private; -static struct quake_private reaction_private; -static struct quake_private smokinguns_private; -static struct quake_private teeworlds_private; -static struct quake_private tremfusion_private; -static struct quake_private tremulousgpp_private; -static struct quake_private tremulous_private; -static struct quake_private turtlearena_private; -static struct quake_private unvanquished_private; -static struct quake_private warsow_private; -static struct quake_private wolfet_private; -static struct quake_private wolf_private; -static struct quake_private wop_private; -static struct quake_private xonotic_private; -static struct quake_private zeq2lite_private; - -static struct unreal_private aao_private = { NULL, ".aao" }; -static struct unreal_private postal2_private = { NULL, ".fuk" }; -static struct unreal_private rune_private = { NULL, ".run" }; -static struct unreal_private ut2004_private = { NULL, ".ut2" }; -static struct unreal_private ut2_private = { NULL, ".ut2" }; -static struct unreal_private ut_private = { NULL, ".unr" }; - -#include GAMES_INCLUDE +#include GAMES_C_INCLUDE struct gsname2type_s { diff --git a/src/game.h b/src/game.h index 612d2cd..ff2faf2 100644 --- a/src/game.h +++ b/src/game.h @@ -135,7 +135,9 @@ struct game { char *game_cfg; GData *games_data; GSList *custom_args; + /** game specific private data */ + // char *private_suffix; void *pd; }; diff --git a/src/games.xml b/src/games.xml index 8cc3c58..c3c26ae 100644 --- a/src/games.xml +++ b/src/games.xml @@ -31,6 +31,7 @@ + @@ -48,7 +49,6 @@ q2_prefs_load q3_update_prefs arena - alienarena_private UN_SERVER @@ -61,7 +61,7 @@ -ams aao.xpm armyops - aao_private + .aao BF1942_SERVER @@ -93,7 +93,6 @@ q3_prefs_load_common q3_update_prefs_common main - cod_private Q3_SERVER @@ -112,7 +111,6 @@ q3_prefs_load_common q3_update_prefs_common uo - coduo_private Q3_SERVER @@ -131,7 +129,6 @@ q3_prefs_load_common q3_update_prefs_common main - cod2_private Q3_SERVER @@ -150,7 +147,6 @@ q3_prefs_load_common q3_update_prefs_common main - cod4_private Q2_SERVER @@ -160,7 +156,6 @@ dday.xpm DDaynormandy dday - dday_private DESCENT3_SERVER @@ -206,7 +201,6 @@ base +net_clientRemoteConsolePassword +rconpassword - doom3_private Q3_SERVER @@ -223,7 +217,6 @@ q3_prefs_load_common q3_update_prefs_common etmain - etl_private Q3_SERVER @@ -248,7 +241,6 @@ base +net_clientRemoteConsolePassword +rconpassword - etqw_private HL_SERVER_OLD @@ -268,7 +260,6 @@ quake_has_map sv_os identify_os - hl_private HL_SERVER @@ -288,7 +279,6 @@ quake_has_map sv_os identify_os - hl_private HL2_SERVER @@ -365,7 +355,6 @@ q3_prefs_load_common q3_update_prefs_common base - jk2_private Q3_SERVER @@ -387,7 +376,6 @@ q3_prefs_load_common q3_update_prefs_common base - jk3_private KP_SERVER @@ -418,7 +406,6 @@ mohaa ~/.mohaa main - mohaa_private NETP_SERVER @@ -452,7 +439,6 @@ q3_prefs_load_common q3_update_prefs_common data - nexuiz_private Q3_SERVER @@ -471,7 +457,6 @@ q3_prefs_load_common q3_update_prefs baseq3 - openarena_private OTTD_SERVER @@ -500,7 +485,7 @@ postal2.xpm postal2mpdemo postal2mp - postal2_private + .fuk Q3_SERVER @@ -519,7 +504,6 @@ q3_prefs_load_common q3_update_prefs baseq3 - q3rally_private Q1_SERVER @@ -546,7 +530,6 @@ q1_prefs_load q1_update_prefs id1 - q1_private QW_SERVER @@ -577,7 +560,6 @@ qw_update_prefs qw id1 - qw_private Q2_SERVER @@ -607,7 +589,6 @@ q2_prefs_load q2_update_prefs baseq2 - q2_private Q3_SERVER @@ -640,7 +621,6 @@ q3_update_prefs demoq3 baseq3 - q3_private Q3_SERVER @@ -669,7 +649,6 @@ q4base +net_clientRemoteConsolePassword +rconpassword - quake4_private Q2_SERVER @@ -685,7 +664,6 @@ quetoo ~/.quetoo default - quetoo_private Q3_SERVER @@ -704,7 +682,6 @@ q3_prefs_load_common q3_update_prefs rq3 - reaction_private Q3_SERVER @@ -723,7 +700,6 @@ demomain main wolfmp - wolf_private UN_SERVER @@ -732,7 +708,7 @@ RUNESRV rune.xpm rune - rune_private + .run SAS_SERVER @@ -800,7 +776,6 @@ q3_prefs_load_common q3_update_prefs smokinguns - smokinguns_private SFS_SERVER @@ -849,7 +824,6 @@ teeworlds_exec teeworlds ~/.teeworlds - teeworlds_private Q3_SERVER @@ -870,7 +844,6 @@ q3_prefs_load_common q3_update_prefs base - tremfusion_private Q3_SERVER @@ -892,7 +865,6 @@ q3_prefs_load_common q3_update_prefs base - tremulous_private Q3_SERVER @@ -913,7 +885,6 @@ q3_prefs_load_common q3_update_prefs base - tremulousgpp_private T2_SERVER @@ -953,7 +924,6 @@ q3_prefs_load_common q3_update_prefs baseturtle - turtlearena_private UN_SERVER @@ -970,7 +940,7 @@ unreal_init_maps unreal_has_map ut - ut_private + .unr UN_SERVER @@ -984,7 +954,7 @@ ut2003_demo ut2003 ~/.ut2003 - ut2_private + .ut2 UN_SERVER @@ -1002,7 +972,7 @@ ut2004_cmd_or_dir_changed ut2004_update_prefs ~/.ut2004 - ut2004_private + .ut2 Q3_SERVER @@ -1024,7 +994,6 @@ q3_prefs_load_common q3_update_prefs pkg - unvanquished_private Q3_SERVER @@ -1045,7 +1014,6 @@ q3_prefs_load_common q3_update_prefs q3ut4 - iourt_private Q3_SERVER @@ -1081,7 +1049,6 @@ q3_prefs_load_common q3_update_prefs_common basewsw - warsow_private Q3_SERVER @@ -1098,7 +1065,6 @@ q3_prefs_load_common q3_update_prefs_common etmain - wolfet_private Q3_SERVER @@ -1117,7 +1083,6 @@ q3_prefs_load_common q3_update_prefs wop - wop_private Q3_SERVER @@ -1138,7 +1103,6 @@ q3_prefs_load_common q3_update_prefs_common data - xonotic_private Q3_SERVER @@ -1157,7 +1121,6 @@ q3_prefs_load_common q3_update_prefs ZEQ2 - zeq2lite_private GPS_SERVER diff --git a/src/gamesxml2c.c b/src/gamesxml2c.c index 374b26a..d33dc75 100644 --- a/src/gamesxml2c.c +++ b/src/gamesxml2c.c @@ -17,13 +17,18 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ +#include #include -#include #include +#include #include #include +#define MALLOC(var, type) var = malloc(sizeof(type)); if(!var) abort(); memset(var, 0, sizeof(type)); + +#define WARNING_HEADER "// DO NOT EDIT THIS FILE, AUTOMATICALLY GENERATED" + typedef enum { Tag_0, @@ -57,6 +62,7 @@ typedef enum TAG_prefs_load, TAG_update_prefs, TAG_default_home, + TAG_private_suffix, TAG_pd, TAG_end_basic = TAG_pd, @@ -113,7 +119,7 @@ static void tags_init() { add_tag(TAG_qstat_str, Tag_do_inherit, Tag_type_string, (xmlChar*) "qstat_str"); add_tag(TAG_qstat_option, Tag_do_inherit, Tag_type_string, (xmlChar*) "qstat_option"); add_tag(TAG_qstat_master_option, Tag_do_inherit, Tag_type_string, (xmlChar*) "qstat_master_option"); - add_tag(TAG_icon, Tag_do_inherit, Tag_type_string, (xmlChar*) "icon"); + add_tag(TAG_icon, Tag_do_inherit, Tag_type_string, (xmlChar*) "icon"); add_tag(TAG_parse_player, Tag_do_inherit, Tag_type_literal, (xmlChar*) "parse_player"); add_tag(TAG_parse_server, Tag_do_inherit, Tag_type_literal, (xmlChar*) "parse_server"); add_tag(TAG_analyze_serverinfo, Tag_do_inherit, Tag_type_literal, (xmlChar*) "analyze_serverinfo"); @@ -132,6 +138,7 @@ static void tags_init() { add_tag(TAG_prefs_load, Tag_no_inherit, Tag_type_literal, (xmlChar*) "prefs_load"); add_tag(TAG_update_prefs, Tag_no_inherit, Tag_type_literal, (xmlChar*) "update_prefs"); add_tag(TAG_default_home, Tag_no_inherit, Tag_type_string, (xmlChar*) "default_home"); + add_tag(TAG_private_suffix, Tag_no_inherit, Tag_type_string, (xmlChar*) "private_suffix"); add_tag(TAG_pd, Tag_no_inherit, Tag_type_address, (xmlChar*) "pd"); add_tag(TAG_data, Tag_no_inherit, Tag_type_literal, (xmlChar*) "data"); add_tag(TAG_main_mod, Tag_no_inherit, Tag_type_string, (xmlChar*) "main_mod"); @@ -142,22 +149,25 @@ static void tags_init() { } const xmlChar* tag_name(GameTag tag) { - if (tag < TAG_start_basic || tag >= TAG_invalid) + if (tag < TAG_start_basic || tag >= TAG_invalid) { return NULL; + } return tags[tag].name; } TagInherit tag_inherit(GameTag tag) { - if (tag < TAG_start_basic || tag >= TAG_invalid) + if (tag < TAG_start_basic || tag >= TAG_invalid) { return 0; + } return tags[tag].inherit; } TagType tag_type(GameTag tag) { - if (tag < TAG_start_basic || tag >= TAG_invalid) + if (tag < TAG_start_basic || tag >= TAG_invalid) { return 0; + } return tags[tag].type; } @@ -197,8 +207,6 @@ GameTag getGameTag(const xmlChar* str) { return tag; } -#define MALLOC(var, type) var = malloc(sizeof(type)); if(!var) abort(); memset(var, 0, sizeof(type)); - RawGame* parseGame(xmlDocPtr doc, xmlNodePtr node) { GameTag tag; xmlChar* val = NULL; @@ -207,7 +215,9 @@ RawGame* parseGame(xmlDocPtr doc, xmlNodePtr node) { MALLOC(rawgame, RawGame); for (node = node->xmlChildrenNode; node; node = node->next) { - if (node->type != XML_ELEMENT_NODE) continue; + if (node->type != XML_ELEMENT_NODE) { + continue; + } tag = getGameTag(node->name); @@ -244,7 +254,9 @@ GameList* parseGames(const char* filename) { GameList* list = NULL; doc = xmlParseFile(filename); - if (doc == NULL) return(NULL); + if (doc == NULL) { + return(NULL); + } node = xmlDocGetRootElement(doc); if (node == NULL) { @@ -276,30 +288,38 @@ GameList* parseGames(const char* filename) { return list; } -void printGame(FILE* cfile, RawGame* rg, RawGame* template) { +void printGame(FILE* games_c_file, RawGame* rg, RawGame* template) { GameTag tag; - fputs("\t{\n", cfile); + fputs("\t{\n", games_c_file); for (tag = TAG_start_basic; tag <= TAG_end_basic; ++tag) { xmlChar* val = rg->basic[tag]; - if (!val && rg->base && tag_inherit(tag)) + if (tag == TAG_private_suffix) { + continue; + } + + if (!val && rg->base && tag_inherit(tag)) { val = rg->base->basic[tag]; + } - if (!val) + if (!val) { val = template->basic[tag]; + } - if (!val || !xmlStrcmp(val, (xmlChar*) "NULL")) continue; + if (!val || !xmlStrcmp(val, (xmlChar*) "NULL")) { + continue; + } switch(tag_type(tag)) { case Tag_type_string: - fprintf(cfile, "\t\t.%-20s = \"%s\",\n", tag_name(tag), val); + fprintf(games_c_file, "\t\t.%-20s = \"%s\",\n", tag_name(tag), val); break; case Tag_type_address: - fprintf(cfile, "\t\t.%-20s = &%s,\n", tag_name(tag), val); + fprintf(games_c_file, "\t\t.%-20s = &%s,\n", tag_name(tag), val); break; case Tag_type_literal: - fprintf(cfile, "\t\t.%-20s = %s,\n", tag_name(tag), val); + fprintf(games_c_file, "\t\t.%-20s = %s,\n", tag_name(tag), val); break; case Tag_type_invalid: break; @@ -309,18 +329,22 @@ void printGame(FILE* cfile, RawGame* rg, RawGame* template) { for (tag = TAG_start_multi; tag <= TAG_end_multi; ++tag) { struct MultiTag* m = rg->multi[tag - TAG_start_multi]; - if (!m && rg->base && tag_inherit(tag)) + if (!m && rg->base && tag_inherit(tag)) { m = rg->base->multi[tag - TAG_start_multi]; + } - if (!m) + if (!m) { m = template->multi[tag - TAG_start_multi]; + } - if (!m || !m->val || !xmlStrcmp(m->val, (xmlChar*) "NULL")) continue; + if (!m || !m->val || !xmlStrcmp(m->val, (xmlChar*) "NULL")) { + continue; + } - fprintf(cfile, "\t\t.%-20s = stringlist%03u,\n", tag_name(tag), rg->num_multitags++); + fprintf(games_c_file, "\t\t.%-20s = stringlist%03u,\n", tag_name(tag), rg->num_multitags++); } - fputs("\t},\n", cfile); + fputs("\t},\n", games_c_file); } int main (int argc, char* argv[]) { @@ -328,22 +352,23 @@ int main (int argc, char* argv[]) { GameList* ptr = NULL; GameList* next = NULL; RawGame* template = NULL; - unsigned i = 0, type_count; + xmlChar *buf, *c; + unsigned i; if (argc < 3) return 1; - FILE* cfile = fopen(argv[2], "w");; - if (cfile == NULL) { + FILE* games_c_file = fopen(argv[2], "w");; + if (games_c_file == NULL) { fprintf(stderr, "%s: can't write to file\n", argv[2]); } - FILE* hfile = fopen(argv[3], "w");; - if (hfile == NULL) { + FILE* games_h_file = fopen(argv[3], "w");; + if (games_h_file == NULL) { fprintf(stderr, "%s: can't write to file\n", argv[3]); } - FILE* xfile = fopen(argv[4], "w");; - if (xfile == NULL) { + FILE* icons_c_file = fopen(argv[4], "w");; + if (icons_c_file == NULL) { fprintf(stderr, "%s: can't write to file\n", argv[4]); } @@ -383,56 +408,103 @@ int main (int argc, char* argv[]) { } } - fputs("// DO NOT EDIT THIS FILE, AUTOMATICALLY GENERATED\n", cfile); - fputs("// DO NOT EDIT THIS FILE, AUTOMATICALLY GENERATED\n", hfile); - fputs("// DO NOT EDIT THIS FILE, AUTOMATICALLY GENERATED\n", xfile); + fprintf(games_c_file, "%s\n", WARNING_HEADER); + fprintf(games_h_file, "%s\n", WARNING_HEADER); + fprintf(icons_c_file, "%s\n", WARNING_HEADER); - fputs("enum server_type {\n", hfile); + // write quake_private then unreal_private + for(i = 0; i <= 1; i++) { + for (ptr = list; ptr; ptr = ptr->next) { + buf = xmlStrdup(ptr->game->basic[TAG_type]); - for (ptr = list, type_count = 0; ptr; ptr = ptr->next, type_count++) { + for (c = buf; *c; ++c) { + if (*c == '_') { + *c = '\0'; + break; + } + *c = tolower(*c); + } + + ptr->game->basic[TAG_pd] = malloc(xmlStrlen(buf) + 9); + xmlStrPrintf(ptr->game->basic[TAG_pd], xmlStrlen(buf) + 9, "%s_private", buf); + + free(buf); + + if (ptr->game->basic[TAG_private_suffix] == NULL) { + if (i == 0) + fprintf(games_c_file, "static struct quake_private %s;\n", ptr->game->basic[TAG_pd]); + } + else { + if (i == 1) + fprintf(games_c_file, "static struct unreal_private %s = { NULL, \"%s\" };\n", ptr->game->basic[TAG_pd], ptr->game->basic[TAG_private_suffix]); + } + } + } + + // write stringlist + for (ptr = list, i = 0; ptr; ptr = ptr->next) { RawGame* rg = ptr->game; GameTag tag; - fprintf(hfile, "\t%s%s,\n", ptr->game->basic[TAG_type], type_count == 0 ? " = 0" : ""); - - if (ptr->game->basic[TAG_icon] != NULL) { - fprintf(xfile, "#include \"xpm/%s\"\n", ptr->game->basic[TAG_icon]); - } - for (tag = TAG_start_multi; tag <= TAG_end_multi; ++tag) { struct MultiTag* m = rg->multi[tag - TAG_start_multi]; - if (!m && rg->base && tag_inherit(tag)) + if (!m && rg->base && tag_inherit(tag)) { m = rg->base->multi[tag - TAG_start_multi]; - - if (!m) - m = template->multi[tag - TAG_start_multi]; - - if (!m || !m->val || !xmlStrcmp(m->val, (xmlChar*) "NULL")) continue; - - fprintf(cfile, "static char* stringlist%03u[] = {", i); - for (; m; m = m->next) { - fprintf(cfile, " \"%s\",", m->val); } - fputs(" NULL };\n", cfile); + + if (!m) { + m = template->multi[tag - TAG_start_multi]; + } + + if (!m || !m->val || !xmlStrcmp(m->val, (xmlChar*) "NULL")) { + continue; + } + + fprintf(games_c_file, "static char* stringlist%03u[] = {", i); + + for (; m; m = m->next) { + fprintf(games_c_file, " \"%s\",", m->val); + } + + fputs(" NULL };\n", games_c_file); ++i; } } - fputs("};\n", hfile); + fputs("struct game games[] = {\n", games_c_file); - fputs("struct game games[] = {\n", cfile); - - i = 0; - for (ptr = list; ptr; ptr = ptr->next) { + // write games list + for (ptr = list, i = 0; ptr; ptr = ptr->next) { RawGame* rg = ptr->game; rg->num_multitags = i; - printGame(cfile, rg, template); + printGame(games_c_file, rg, template); i = rg->num_multitags; + + free(ptr->game->basic[TAG_pd]); } - fputs("};\n", cfile); + fputs("};\n", games_c_file); + + fclose(games_c_file); + + fputs("enum server_type {\n", games_h_file); + + // write server_type enum + // write xpm include + for (ptr = list, i = 0; ptr; ptr = ptr->next, i++) { + fprintf(games_h_file, "\t%s%s,\n", ptr->game->basic[TAG_type], i == 0 ? " = 0" : ""); + + if (ptr->game->basic[TAG_icon] != NULL) { + fprintf(icons_c_file, "#include \"xpm/%s\"\n", ptr->game->basic[TAG_icon]); + } + } + + fputs("};\n", games_h_file); + + fclose(games_h_file); + fclose(icons_c_file); // TODO free diff --git a/src/pixmaps.c b/src/pixmaps.c index f31efb1..1df51b6 100644 --- a/src/pixmaps.c +++ b/src/pixmaps.c @@ -27,7 +27,7 @@ // hack to make dlsym work #define static -#include "icons.c" +#include ICONS_C_INCLUDE #include "xpm/update.xpm" #include "xpm/refresh.xpm"