Skip to content

Commit

Permalink
Fix issue caused by illegal, empty <transporters> blocks.
Browse files Browse the repository at this point in the history
It appears that we have been writing empty <transporters> blocks
into every vehicle for the past 6 or so years.  These blocks should
not exist in, e.g., combat vehicles, but apparently went unnoticed
due to loose handling of the relevant block when empty.

This change adds a new test prior to processing these empty blocks
on file load, a function to support this test, and a conditional
around writing the <transporters> block to prevent writing empty
blocks in every .BLK file.
  • Loading branch information
Sleet01 committed Aug 16, 2023
1 parent 355a192 commit 900a5a1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
22 changes: 13 additions & 9 deletions megamek/src/megamek/common/loaders/BLKFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -597,16 +597,20 @@ public static BuildingBlock getBlock(Entity t) {

blk.writeBlockData("motion_type", t.getMovementModeAsString());

String[] transporter_array = new String[t.getTransports().size()];
int index = 0;
for (Transporter transporter : t.getTransports()) {
transporter_array[index] = transporter.toString();
if (t.isPodMountedTransport(transporter)) {
transporter_array[index] += ":omni";
if(t.getTransports().size() > 0) {
// We should only write the transporters block for units that can and do
// have transporter bays. Empty Transporters blocks cause issues.
String[] transporter_array = new String[t.getTransports().size()];
int index = 0;
for (Transporter transporter : t.getTransports()) {
transporter_array[index] = transporter.toString();
if (t.isPodMountedTransport(transporter)) {
transporter_array[index] += ":omni";
}
index++;
}
index++;
blk.writeBlockData("transporters", transporter_array);
}
blk.writeBlockData("transporters", transporter_array);

if (!t.isConventionalInfantry()) {
if (t instanceof Aero) {
Expand Down Expand Up @@ -1105,7 +1109,7 @@ public static void encode(String fileName, Entity t) {
}

protected void addTransports(Entity e) throws EntityLoadingException {
if (dataFile.exists("transporters")) {
if (dataFile.exists("transporters") && dataFile.containsData("transporters")) {
String[] transporters = dataFile.getDataAsString("transporters");
HashSet<Integer> usedBayNumbers = new HashSet<>();

Expand Down
16 changes: 16 additions & 0 deletions megamek/src/megamek/common/util/BuildingBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -707,4 +707,20 @@ public boolean exists(String blockName) {

return true;
}

/**
* Checks if a block exists and has data.
*/
public boolean containsData(String blockName) {
if(!exists(blockName)){
return false;
}
// If the end index is the next line after the start index,
// the block is empty.
// Otherwise it contains data.
int start = findStartIndex(blockName);
int end = findEndIndex(blockName);
return (end - start) > 1;

}
}

0 comments on commit 900a5a1

Please sign in to comment.