From e7727fa671a6709c0630b268e1a2a78d6ddffd06 Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Wed, 7 Oct 2026 00:43:57 -0400 Subject: [PATCH 1/2] reset inited flags and fix language bugs The unit tests parse the tables once per test; a parse teardown should reset flags so that subsequent tests work properly. weapon_close() and ship_close() now reset Weapons_inited and Ships_inited, so a later init parses the tables again instead of skipping them. lcl_xstr_close() now resets Xstr_inited, and lcl_xstr_init() skips parsing if the tables are already loaded. This uses a separate Xstr_tables_parsed flag, since FRED and qtfred set Xstr_inited without parsing anything. lcl_close() now clears Lcl_languages, and lcl_init() only builds the language list when it is empty. Previously a second lcl_init() added English again at index 1 and skipped the built-in languages. Game shutdown now calls lcl_close() instead of lcl_xstr_close(). Co-Authored-By: Claude Fable 5.1 Co-Authored-By: Claude Opus 5.5 --- code/localization/localize.cpp | 48 ++++++++++++++++++++++------------ code/ship/ship.cpp | 2 ++ code/weapon/weapons.cpp | 2 ++ freespace2/freespace.cpp | 2 +- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/code/localization/localize.cpp b/code/localization/localize.cpp index 78868daf554..a9e7973ea6d 100644 --- a/code/localization/localize.cpp +++ b/code/localization/localize.cpp @@ -78,6 +78,9 @@ typedef struct { SCP_unordered_map Xstr_table_map; bool Xstr_inited = false; +// Distinct from Xstr_inited. FRED and qtfred set Xstr_inited without parsing so that XSTR works without actually localizing anything. +static bool Xstr_tables_parsed = false; + // table/mission externalization stuff -------------------- #define PARSE_TEXT_BUF_SIZE PARSE_BUF_SIZE @@ -190,26 +193,28 @@ void lcl_init(int lang_init) // initialize encryption encrypt_init(); - // set up the first language (which should be English) - Lcl_languages.push_back(Lcl_builtin_languages[0]); + if (Lcl_languages.empty()) { + // set up the first language (which should be English) + Lcl_languages.push_back(Lcl_builtin_languages[0]); - // check string.tbl to see which languages we support - try - { - parse_stringstbl_quick("strings.tbl"); - } - catch (const parse::ParseException& e) - { - mprintf(("TABLES: Unable to parse '%s'! Error message = %s.\n", "strings.tbl", e.what())); - } + // check string.tbl to see which languages we support + try + { + parse_stringstbl_quick("strings.tbl"); + } + catch (const parse::ParseException& e) + { + mprintf(("TABLES: Unable to parse '%s'! Error message = %s.\n", "strings.tbl", e.what())); + } - parse_modular_table(NOX("*-lcl.tbm"), parse_stringstbl_quick); + parse_modular_table(NOX("*-lcl.tbm"), parse_stringstbl_quick); - // if we only have one language at this point, we need to setup the builtin languages as we might be dealing with an old style strings.tbl - // which doesn't support anything beyond the builtin languages. Note, we start at i = 1 because we added the first language above. - if (!No_built_in_languages && (static_cast(Lcl_languages.size()) == 1)) { - for (int i=1; i(Lcl_languages.size()) == 1)) { + for (int i=1; i Date: Tue, 10 Nov 2026 21:46:56 -0500 Subject: [PATCH 2/2] reset leftover table state at the start of each parse ship_init() now resets the fighter/bomber special-case state and clears Wing_formations before parsing, and weapon_init() clears the pending proximity lists and Default_cmeasure_name. Otherwise a second parse kept values from the first: a stale Fighter_bomber_valid could index Ship_type_counts with -1, and every wing formation was reported as a duplicate. Also update comments that described ship_close() and weapon_close() as shutdown-only, and the test comment that said Xstr_inited stays set. Co-Authored-By: Claude Opus 5.5 --- code/ship/ship.cpp | 8 +++++--- code/ship/ship.h | 2 +- code/weapon/weapon.h | 2 +- code/weapon/weapons.cpp | 5 ++++- test/src/options/test_options_manager.cpp | 8 ++++---- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/code/ship/ship.cpp b/code/ship/ship.cpp index 84ca1ca45fc..d3b64b1f2c5 100644 --- a/code/ship/ship.cpp +++ b/code/ship/ship.cpp @@ -6365,6 +6365,9 @@ void ship_init() Ship_types.erase(Ship_types.begin() + idx); } + Fighter_bomber_valid = false; + Ship_type_fighter = Ship_type_bomber = Ship_type_fighter_bomber = -1; + // See whether this mod defines an explicit "fighter/bomber" type (which would take priority over the special usage), and if not, whether the component types exist if (ship_type_name_lookup(Fighter_bomber_type_name) < 0) { @@ -6411,6 +6414,7 @@ void ship_init() //Parse main TBL first Removed_ships.clear(); Ship_info.clear(); + Wing_formations.clear(); Hud_parsed_ships.clear(); parse_shiptbl("ships.tbl"); @@ -16834,9 +16838,7 @@ int ship_find_repair_ship( object *requester_obj, object **ship_we_found ) } /** - * Called in game_shutdown() to free malloced memory - * - * NOTE: do not call this function. It is only called from ::game_shutdown() + * Frees the ship tables and other memory allocated by ship_init() */ void ship_close() { diff --git a/code/ship/ship.h b/code/ship/ship.h index 52789d22783..2c7116269ab 100644 --- a/code/ship/ship.h +++ b/code/ship/ship.h @@ -1974,7 +1974,7 @@ extern void ship_wing_cleanup( int shipnum, wing *wingp ); extern void wing_maybe_cleanup( wing *wingp, int team = -1 ); extern int ship_find_repair_ship( object *requester_obj, object **ship_we_found = NULL ); -extern void ship_close(); // called in game_shutdown() to free malloced memory +extern void ship_close(); // frees the ship tables extern void ship_assign_sound_all(); diff --git a/code/weapon/weapon.h b/code/weapon/weapon.h index ecb122486fd..13bbd9b3f9e 100644 --- a/code/weapon/weapon.h +++ b/code/weapon/weapon.h @@ -1000,7 +1000,7 @@ inline int weapon_info_size() void weapon_init(); // called at game startup void weapon_post_ship_init(); // called after ship_init() to resolve mine proximity ship type/class names -void weapon_close(); // called at game shutdown +void weapon_close(); // frees the weapon tables void weapon_level_init(); // called before the start of each level void weapon_render(object* obj, model_draw_list *scene); void weapon_delete( object * obj ); diff --git a/code/weapon/weapons.cpp b/code/weapon/weapons.cpp index a66915dfcb2..c6dc3fd6850 100644 --- a/code/weapon/weapons.cpp +++ b/code/weapon/weapons.cpp @@ -5150,6 +5150,9 @@ void weapon_init() { if ( !Weapons_inited ) { Spawn_names.clear(); + Pending_proximity_type_names.clear(); + Pending_proximity_class_names.clear(); + Default_cmeasure_name[0] = '\0'; // parse weapons.tbl Removed_weapons.clear(); @@ -5174,7 +5177,7 @@ void weapon_init() /** - * Call from game_shutdown() only!! + * Frees the weapon tables */ void weapon_close() { diff --git a/test/src/options/test_options_manager.cpp b/test/src/options/test_options_manager.cpp index 93861a34668..b6bc5c7f2aa 100644 --- a/test/src/options/test_options_manager.cpp +++ b/test/src/options/test_options_manager.cpp @@ -106,10 +106,10 @@ TEST_F(OptionsManagerOverride, LaterOverrideReplacesEarlierOne) // type) rather than getCurrentValueDescription(), because several options' display functions call XSTR() for // localized display strings (e.g. "On"/"Off", the shadow quality tiers). XSTR() hits an Int3() unless // lcl_xstr_init() ran first, and the OptionsManagerOverride fixture never runs it. Only FSTestFixture does, -// and Xstr_inited stays set for the rest of the process afterwards, so under --gtest_shuffle a display call -// from this fixture crashes or passes purely by test order. getValue() only exercises the deserializer, which -// is exactly the contract these tests are checking anyway. The one test that must go through a display -// function uses the OptionsManagerOverrideLocalized fixture below instead. +// and its teardown resets Xstr_inited, so a display call from this fixture would hit the Int3() regardless +// of test order. getValue() only exercises the deserializer, which is exactly the contract these tests are +// checking anyway. The one test that must go through a display function uses the +// OptionsManagerOverrideLocalized fixture below instead. TEST_F(OptionsManagerOverride, BoolOverrideRoundTrips) {