Skip to content

Commit 26c5152

Browse files
authored
Merge pull request #5985 from Goober5000/pof_file_valid_fname_checks
make model file checks consistent
2 parents 5066e49 + 4bf806c commit 26c5152

File tree

6 files changed

+49
-26
lines changed

6 files changed

+49
-26
lines changed

code/globalincs/pstypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ class camid
453453
// - is not "none"
454454
// - is not "<none>"
455455
inline bool VALID_FNAME(const char* x) {
456-
return strlen((x)) && stricmp((x), "none") != 0 && stricmp((x), "<none>") != 0;
456+
return (x[0] != '\0') && stricmp(x, "none") != 0 && stricmp(x, "<none>") != 0;
457457
}
458458
/**
459459
* @brief Checks if the specified string may be a valid file name

code/missionui/missionweaponchoice.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ void wl_load_icons(int weapon_class)
13321332

13331333
icon = &Wl_icons[weapon_class];
13341334

1335-
if (!Use_3d_weapon_icons || (wip->render_type == WRT_LASER && !strlen(wip->tech_model)))
1335+
if (!Use_3d_weapon_icons || (wip->render_type == WRT_LASER && !VALID_FNAME(wip->tech_model)))
13361336
{
13371337
first_frame = bm_load_animation(Weapon_info[weapon_class].icon_filename, &num_frames, nullptr, nullptr, nullptr, false, CF_TYPE_INTERFACE);
13381338

@@ -1343,9 +1343,9 @@ void wl_load_icons(int weapon_class)
13431343

13441344
multi_send_anti_timeout_ping();
13451345

1346-
if ( icon->model_index == -1 && ( ( strlen(wip->tech_model) && !strlen(wip->anim_filename) ) || (first_frame == -1) ) )
1346+
if ( icon->model_index == -1 && ( ( VALID_FNAME(wip->tech_model) && !VALID_FNAME(wip->anim_filename) ) || (first_frame == -1) ) )
13471347
{
1348-
if(strlen(wip->tech_model))
1348+
if(VALID_FNAME(wip->tech_model))
13491349
{
13501350
icon->model_index = model_load(wip->tech_model, 0, NULL, 0);
13511351
}
@@ -2742,7 +2742,7 @@ void weapon_select_do(float frametime)
27422742
weapon_info *wip = &Weapon_info[Selected_wl_class];
27432743

27442744
//Get the model
2745-
if (strlen(wip->tech_model)) {
2745+
if (VALID_FNAME(wip->tech_model)) {
27462746
modelIdx = model_load(wip->tech_model, 0, NULL, 0);
27472747
}
27482748
if (wip->render_type != WRT_LASER && modelIdx == -1) {

code/model/modelread.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3259,7 +3259,7 @@ void model_load_texture(polymodel *pm, int i, char *file)
32593259
//returns the number of the pof tech model if specified, otherwise number of pof model
32603260
int model_load(ship_info* sip, bool prefer_tech_model)
32613261
{
3262-
if (prefer_tech_model && sip->pof_file_tech[0] != '\0') {
3262+
if (prefer_tech_model && VALID_FNAME(sip->pof_file_tech)) {
32633263
// This cannot load into sip->subsystems, as this will overwrite the subsystems model_num to the
32643264
// techroom model, which is decidedly wrong for the mission itself.
32653265
return model_load(sip->pof_file_tech, 0, nullptr);
@@ -3298,6 +3298,11 @@ int model_load(const char* filename, int n_subsystems, model_subsystem* subsyst
32983298
return -1;
32993299
}
33003300

3301+
// Valid file
3302+
if (!VALID_FNAME(filename)) {
3303+
return -1;
3304+
}
3305+
33013306
TRACE_SCOPE(tracing::LoadModelFile);
33023307

33033308
mprintf(( "Loading model '%s' into slot '%i'\n", filename, num ));

code/ship/ship.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,7 +2941,7 @@ static void parse_ship_values(ship_info* sip, const bool is_template, const bool
29412941

29422942
// Goober5000 - if this is a modular table, and we're replacing an existing file name, and the file doesn't exist, don't replace it
29432943
if (replace)
2944-
if (sip->cockpit_pof_file[0] != '\0')
2944+
if (VALID_FNAME(sip->cockpit_pof_file))
29452945
if (!model_exists(temp))
29462946
valid = false;
29472947

@@ -3021,7 +3021,7 @@ static void parse_ship_values(ship_info* sip, const bool is_template, const bool
30213021

30223022
// Goober5000 - if this is a modular table, and we're replacing an existing file name, and the file doesn't exist, don't replace it
30233023
if (replace)
3024-
if (sip->pof_file[0] != '\0')
3024+
if (VALID_FNAME(sip->pof_file))
30253025
if (!model_exists(temp))
30263026
valid = false;
30273027

@@ -3041,7 +3041,7 @@ static void parse_ship_values(ship_info* sip, const bool is_template, const bool
30413041

30423042
// if this is a modular table, and we're replacing an existing file name, and the file doesn't exist, don't replace it
30433043
if (replace)
3044-
if (sip->pof_file_tech[0] != '\0')
3044+
if (VALID_FNAME(sip->pof_file_tech))
30453045
if (!cf_exists_full(temp, CF_TYPE_MODELS))
30463046
valid = false;
30473047

@@ -3102,7 +3102,7 @@ static void parse_ship_values(ship_info* sip, const bool is_template, const bool
31023102

31033103
// Goober5000 - if this is a modular table, and we're replacing an existing file name, and the file doesn't exist, don't replace it
31043104
if (replace)
3105-
if (sip->pof_file_hud[0] != '\0')
3105+
if (VALID_FNAME(sip->pof_file_hud))
31063106
if (!cf_exists_full(temp, CF_TYPE_MODELS))
31073107
valid = false;
31083108

@@ -3380,7 +3380,7 @@ static void parse_ship_values(ship_info* sip, const bool is_template, const bool
33803380
bool valid = true;
33813381

33823382
if (replace)
3383-
if (sip->generic_debris_pof_file[0] != '\0')
3383+
if (VALID_FNAME(sip->generic_debris_pof_file))
33843384
if (!cf_exists_full(temp, CF_TYPE_MODELS))
33853385
valid = false;
33863386

@@ -4346,7 +4346,7 @@ static void parse_ship_values(ship_info* sip, const bool is_template, const bool
43464346
{
43474347
stuff_vec3d(&sip->closeup_pos);
43484348
}
4349-
else if (first_time && strlen(sip->pof_file))
4349+
else if (first_time && VALID_FNAME(sip->pof_file))
43504350
{
43514351
//Calculate from the model file. This is inefficient, but whatever
43524352
int model_idx = model_load(sip->pof_file, 0, NULL);
@@ -10797,15 +10797,20 @@ int ship_create(matrix* orient, vec3d* pos, int ship_type, const char* ship_name
1079710797
shipp->clear();
1079810798
shipp->orders_allowed_against = ship_set_default_orders_against();
1079910799

10800+
if (!VALID_FNAME(sip->pof_file))
10801+
{
10802+
Error(LOCATION, "Cannot create ship %s; pof file is not valid", sip->name);
10803+
return -1;
10804+
}
1080010805
sip->model_num = model_load(sip->pof_file, sip->n_subsystems, &sip->subsystems[0]); // use the highest detail level
1080110806

10802-
if(strlen(sip->cockpit_pof_file))
10807+
if(VALID_FNAME(sip->cockpit_pof_file))
1080310808
{
1080410809
sip->cockpit_model_num = model_load(sip->cockpit_pof_file, 0, NULL);
1080510810
}
1080610811

1080710812
// maybe load an optional hud target model
10808-
if(strlen(sip->pof_file_hud)){
10813+
if(VALID_FNAME(sip->pof_file_hud)){
1080910814
// check to see if a "real" ship uses this model. if so, load it up for him so that subsystems are setup properly
1081010815
for(auto it = Ship_info.begin(); it != Ship_info.end(); ++it){
1081110816
if(!stricmp(it->pof_file, sip->pof_file_hud)){
@@ -10817,7 +10822,7 @@ int ship_create(matrix* orient, vec3d* pos, int ship_type, const char* ship_name
1081710822
sip->model_num_hud = model_load(sip->pof_file_hud, 0, NULL);
1081810823
}
1081910824

10820-
if (strlen(sip->generic_debris_pof_file)) {
10825+
if (VALID_FNAME(sip->generic_debris_pof_file)) {
1082110826
sip->generic_debris_model_num = model_load(sip->generic_debris_pof_file, 0, nullptr);
1082210827
if (sip->generic_debris_model_num >= 0) {
1082310828
polymodel* pm = model_get(sip->generic_debris_model_num);
@@ -11048,7 +11053,7 @@ static void ship_model_change(int n, int ship_type)
1104811053
}
1104911054

1105011055
if ( sip->cockpit_model_num == -1 ) {
11051-
if ( strlen(sip->cockpit_pof_file) ) {
11056+
if ( VALID_FNAME(sip->cockpit_pof_file) ) {
1105211057
sip->cockpit_model_num = model_load(sip->cockpit_pof_file, 0, NULL);
1105311058
}
1105411059
}

code/weapon/shockwave.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ int shockwave_create(int parent_objnum, vec3d* pos, shockwave_create_info* sci,
9393

9494
// try 2D shockwave first, then fall back to 3D, then fall back to default of either
9595
// this should be pretty fool-proof and allow quick change between 2D and 3D effects
96-
if ( strlen(sci->name) )
96+
if ( VALID_FNAME(sci->name) )
9797
info_index = shockwave_load(sci->name, false);
9898

99-
if ( (info_index < 0) && strlen(sci->pof_name) )
99+
if ( (info_index < 0) && VALID_FNAME(sci->pof_name) )
100100
info_index = shockwave_load(sci->pof_name, true);
101101

102102
if (info_index < 0) {
@@ -752,13 +752,13 @@ void shockwave_create_info_load(shockwave_create_info *sci)
752752
{
753753
int i = -1;
754754

755-
// shockwave_load() will return -1 if the filename is "none" or "<none>"
755+
// shockwave_load() will return -1 if the filename is "" or "none" or "<none>"
756756
// checking for that case lets us handle a situation where a 2D shockwave
757757
// of "none" was specified and a valid 3D shockwave was specified
758758

759-
if ( strlen(sci->name) )
759+
if ( VALID_FNAME(sci->name) )
760760
i = shockwave_load(sci->name, false);
761761

762-
if ( (i < 0) && strlen(sci->pof_name) )
762+
if ( (i < 0) && VALID_FNAME(sci->pof_name) )
763763
shockwave_load(sci->pof_name, true);
764764
}

code/weapon/weapons.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,10 +1914,18 @@ int parse_weapon(int subtype, bool replace, const char *filename)
19141914
}
19151915
}
19161916

1917+
// this was added in commit 9fea22d551 and is specifically for allowing countermeasures to be parsed using the weapons code;
1918+
// but it should be handled the same as $Model file:
19171919
if(optional_string("$Model:"))
19181920
{
1919-
wip->render_type = WRT_POF;
19201921
stuff_string(wip->pofbitmap_name, F_NAME, MAX_FILENAME_LEN);
1922+
1923+
if (VALID_FNAME(wip->pofbitmap_name))
1924+
wip->render_type = WRT_POF;
1925+
else
1926+
wip->render_type = WRT_NONE;
1927+
1928+
diag_printf("Model pof file -- %s\n", wip->pofbitmap_name );
19211929
}
19221930

19231931
// handle rearm rate - modified by Goober5000
@@ -6342,10 +6350,15 @@ int weapon_create( const vec3d *pos, const matrix *porient, int weapon_type, int
63426350

63436351
// make sure we are loaded and useable
63446352
if ( (wip->render_type == WRT_POF) && (wip->model_num < 0) ) {
6353+
if (!VALID_FNAME(wip->pofbitmap_name)) {
6354+
Error(LOCATION, "Cannot create weapon %s; pof file is not valid", wip->name);
6355+
return -1;
6356+
}
6357+
63456358
wip->model_num = model_load(wip->pofbitmap_name, 0, NULL);
63466359

63476360
if (wip->model_num < 0) {
6348-
Int3();
6361+
Error(LOCATION, "Cannot create weapon %s; model file %s could not be loaded", wip->name, wip->pofbitmap_name);
63496362
return -1;
63506363
}
63516364
}
@@ -7784,7 +7797,7 @@ void weapons_page_in()
77847797

77857798
wip->external_model_num = -1;
77867799

7787-
if ( strlen(wip->external_model_name) )
7800+
if (VALID_FNAME(wip->external_model_name))
77887801
wip->external_model_num = model_load( wip->external_model_name, 0, NULL );
77897802

77907803
if (wip->external_model_num == -1)
@@ -7877,7 +7890,7 @@ void weapons_page_in_cheats()
78777890

78787891
wip->external_model_num = -1;
78797892

7880-
if ( strlen(wip->external_model_name) )
7893+
if (VALID_FNAME(wip->external_model_name))
78817894
wip->external_model_num = model_load( wip->external_model_name, 0, NULL );
78827895

78837896
if (wip->external_model_num == -1)
@@ -7975,7 +7988,7 @@ bool weapon_page_in(int weapon_type)
79757988

79767989
wip->external_model_num = -1;
79777990

7978-
if (strlen(wip->external_model_name))
7991+
if (VALID_FNAME(wip->external_model_name))
79797992
wip->external_model_num = model_load(wip->external_model_name, 0, NULL);
79807993

79817994
if (wip->external_model_num == -1)

0 commit comments

Comments
 (0)