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 MML 1537: cannot load sv blk files missing armor type field #5634

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
14 changes: 10 additions & 4 deletions megamek/src/megamek/common/loaders/BLKFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ protected void loadSVArmor(Entity sv) throws EntityLoadingException {
}
if (dataFile.exists("armor_tech")) {
sv.setArmorTechRating(dataFile.getDataAsInt("armor_tech")[0]);
} else if (dataFile.exists("armor_tech_rating")) {
sv.setArmorTechRating(dataFile.getDataAsInt("armor_tech_rating")[0]);
}
}
}
Expand Down Expand Up @@ -584,7 +586,7 @@ public void setTechLevel(Entity e) throws EntityLoadingException {
}
}

public static BuildingBlock getBlock(Entity t) {
public static BuildingBlock getBlock(Entity t) throws EntitySavingException{
BuildingBlock blk = new BuildingBlock();
blk.createNewBlock();

Expand Down Expand Up @@ -810,7 +812,10 @@ public static BuildingBlock getBlock(Entity t) {
blk.writeBlockData("clan_engine", Boolean.toString(t.getEngine().isClan()));
}
}
if (!t.hasPatchworkArmor() && (t.getArmorType(1) != 0)) {

// Need to make sure that only "Unknown" armor gets skipped
// barRating block written out later in SV-specific section
if (!t.hasPatchworkArmor() && (t.getArmorType(1) != ArmorType.T_ARMOR_UNKNOWN)) {
blk.writeBlockData("armor_type", t.getArmorType(1));
blk.writeBlockData("armor_tech", t.getArmorTechLevel(1));
} else if (t.hasPatchworkArmor()) {
Expand All @@ -825,6 +830,8 @@ public static BuildingBlock getBlock(Entity t) {
blk.writeBlockData(t.getLocationName(i) + "_barrating", armor.getBAR());
}
}
} else {
throw new EntitySavingException("Armor type unknown or not set; aborting save!");
}
if (t.getStructureType() != 0) {
blk.writeBlockData("internal_type", t.getStructureType());
Expand Down Expand Up @@ -932,7 +939,6 @@ public static BuildingBlock getBlock(Entity t) {
if (t.isSupportVehicle()) {
blk.writeBlockData("structural_tech_rating", t.getStructuralTechRating());
blk.writeBlockData("engine_tech_rating", t.getEngineTechRating());
blk.writeBlockData("armor_tech_rating", t.getArmorTechRating());
}

if (t.hasETypeFlag(Entity.ETYPE_SMALL_CRAFT) || t.hasETypeFlag(Entity.ETYPE_JUMPSHIP)) {
Expand Down Expand Up @@ -1227,7 +1233,7 @@ private static String encodeEquipmentLine(Mounted m) {
return name;
}

public static void encode(String fileName, Entity t) {
public static void encode(String fileName, Entity t) throws EntitySavingException{
BuildingBlock blk = BLKFile.getBlock(t);
blk.writeBlockFile(fileName);
}
Expand Down
12 changes: 8 additions & 4 deletions megamek/src/megamek/common/loaders/BLKSupportTankFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected int defaultVGLFacing(int location, boolean rearFacing) {
return 0;
}
}

@Override
public Entity getEntity() throws EntityLoadingException {

Expand Down Expand Up @@ -116,7 +116,7 @@ public Entity getEntity() throws EntityLoadingException {
for (int x = 0; x < fullArmor.length; x++) {
t.initializeArmor(fullArmor[x], x);
}

// Set the structural tech rating
if (!dataFile.exists("structural_tech_rating")) {
throw new EntityLoadingException("Could not find " +
Expand All @@ -125,14 +125,18 @@ public Entity getEntity() throws EntityLoadingException {
t.setStructuralTechRating(dataFile
.getDataAsInt("structural_tech_rating")[0]);
// Set armor tech rating, if it exists (defaults to structural tr)
// Allow use of armor_tech field if provided
if (dataFile.exists("armor_tech_rating")) {
t.setArmorTechRating(dataFile
.getDataAsInt("armor_tech_rating")[0]);
.getDataAsInt("armor_tech_rating")[0]);
} else if (dataFile.exists("armor_tech")) {
t.setArmorTechRating(dataFile
.getDataAsInt("armor_tech")[0]);
}
// Set engine tech rating, if it exists (defaults to structural tr)
if (dataFile.exists("engine_tech_rating")) {
t.setEngineTechRating(dataFile
.getDataAsInt("engine_tech_rating")[0]);
.getDataAsInt("engine_tech_rating")[0]);
}

t.autoSetInternal();
Expand Down
57 changes: 57 additions & 0 deletions megamek/src/megamek/common/loaders/EntitySavingException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MegaMek.
*
* MegaMek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MegaMek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MegaMek. If not, see <http://www.gnu.org/licenses/>.
*/
package megamek.common.loaders;

/**
* This exception is thrown if a mech or other cannot be properly saved from a
* file due to IO errors, file format errors, missing fields, or whatever.
*
* @author sleet01
*/
public class EntitySavingException extends Exception {
private static final long serialVersionUID = -4472736483205970852L;

/**
* Creates new <code>EntityLoadingException</code> without detail message.
*/
public EntitySavingException() {

}

/**
* Constructs an <code>EntityLoadingException</code> with the specified
* detail message.
*
* @param msg the detail message.
*/
public EntitySavingException(String msg) {
super(msg);
}

/**
* Constructs a new exception with the specified detail message and
* cause.
*
* @param message the detail message
* @param cause the cause
*/
public EntitySavingException(final String message, final Throwable cause) {
super(message, cause);
}
}
Loading