Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loading of old resizable equipment #6182

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ protected void loadEquipment(Entity t, String sName, int nLoc) throws EntityLoad
}
}
if (etype.isVariableSize()) {
if (size == 0) {
size = MtfFile.extractLegacySize(equipName);
}
mount.setSize(size);
}
if (etype.hasFlag(MiscType.F_CARGO)) {
Expand Down
3 changes: 3 additions & 0 deletions megamek/src/megamek/common/loaders/BLKBattleArmorFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ protected void loadEquipment(Entity t, String sName, int nLoc) throws EntityLoad
}
m.setSquadSupportWeapon(sswMounted);
if (etype.isVariableSize()) {
if (size == 0) {
size = MtfFile.extractLegacySize(equipName);
}
m.setSize(size);
}
} catch (LocationFullException ex) {
Expand Down
3 changes: 3 additions & 0 deletions megamek/src/megamek/common/loaders/BLKConvFighterFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ protected void loadEquipment(Entity t, String sName, int nLoc) throws EntityLoad
}
}
if (etype.isVariableSize()) {
if (size == 0) {
size = MtfFile.extractLegacySize(equipName);
}
mount.setSize(size);
}
} catch (LocationFullException ex) {
Expand Down
4 changes: 4 additions & 0 deletions megamek/src/megamek/common/loaders/BLKFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ protected void loadEquipment(Entity t, String sName, int nLoc)
}
}
if (etype.isVariableSize()) {
if (size == 0) {
size = MtfFile.extractLegacySize(equipName);
}

mount.setSize(size);
} else if (t.isSupportVehicle() && (mount.getType() instanceof InfantryWeapon)
&& size > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ protected void loadEquipment(Entity t, String sName, int nLoc) throws EntityLoad
}
}
if (etype.isVariableSize()) {
if (size == 0) {
size = MtfFile.extractLegacySize(equipName);
}
mount.setSize(size);
} else if (t.isSupportVehicle() && (mount.getType() instanceof InfantryWeapon)
&& size > 1) {
Expand Down
3 changes: 3 additions & 0 deletions megamek/src/megamek/common/loaders/BLKSmallCraftFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ protected void loadEquipment(Entity t, String sName, int nLoc) throws EntityLoad
}
}
if (etype.isVariableSize()) {
if (size == 0) {
size = MtfFile.extractLegacySize(equipName);
}
mount.setSize(size);
}
} catch (LocationFullException ex) {
Expand Down
3 changes: 3 additions & 0 deletions megamek/src/megamek/common/loaders/BLKWarshipFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ protected void loadEquipment(Entity en, String sName, int nLoc) throws EntityLoa
}
}
if (etype.isVariableSize()) {
if (size == 0) {
size = MtfFile.extractLegacySize(equipName);
}
newmount.setSize(size);
}
} else if (!equipName.isBlank()) {
Expand Down
23 changes: 23 additions & 0 deletions megamek/src/megamek/common/loaders/MtfFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import megamek.codeUtilities.StringUtility;
import megamek.common.*;
Expand Down Expand Up @@ -156,6 +157,24 @@
public static final String FLUFF_IMAGE = "fluffimage:";
public static final String ICON = "icon:";

private static final Pattern LEGACY_SIZE_PATTERN = Pattern.compile("\\((\\d+(?:\\.\\d+)?)\\s*(?:ton|tons|m|kg)\\)", Pattern.CASE_INSENSITIVE);

/**
* Modern unit files store resizable equipment like Cargo:SIZE:4.0
* Old equipment stores it like Cargo (4 tons)
* This is a helper method to parse old-style resizable equipment
* @param eqName The name of the equipment (including the size designator)
* @return The parsed size of the equipment
*/
public static double extractLegacySize(String eqName) {
var m = LEGACY_SIZE_PATTERN.matcher(eqName);
if (m.find()) {
return Double.parseDouble(m.group(1));
pavelbraginskiy marked this conversation as resolved.
Show resolved Hide resolved
} else {
return 0;
}
}

public MtfFile(InputStream is) throws EntityLoadingException {
try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader r = new BufferedReader(isr)) {
Expand Down Expand Up @@ -906,6 +925,10 @@
mount = mek.addEquipment(etype, etype2, loc, isOmniPod, isArmored);
}
if (etype.isVariableSize()) {
if (size == 0) {
size = extractLegacySize(critName);
}

mount.setSize(size);
// The size may require additional critical slots
// Account for loading Superheavy oversized Variable Size components
Expand Down