Skip to content

Commit

Permalink
Merge pull request #6180 from Scoppio/fix/blkfile-parser-encoding
Browse files Browse the repository at this point in the history
Fix/blkfile parser encoding
  • Loading branch information
HammerGS authored Nov 9, 2024
2 parents ef8d32c + ce28838 commit 582de3c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
29 changes: 19 additions & 10 deletions megamek/src/megamek/common/loaders/BLKFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package megamek.common.loaders;

import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
Expand Down Expand Up @@ -288,16 +289,18 @@ protected void loadSVArmor(Entity sv) throws EntityLoadingException {
sv.setBARRating(armor.getBAR(), i);
sv.setArmorTechLevel(armor.getStaticTechLevel().getCompoundTechLevel(false), i);
}
} else if (dataFile.exists("barrating")) {
megamek.common.equipment.ArmorType armor = ArmorType.svArmor(dataFile.getDataAsInt("barrating")[0]);
sv.setArmorType(armor.getArmorType());
sv.setBARRating(armor.getBAR());
sv.setArmorTechLevel(armor.getStaticTechLevel().getCompoundTechLevel(false));
} else {
if (dataFile.exists("armor_type")) {
sv.setArmorType(dataFile.getDataAsInt("armor_type")[0]);
if (dataFile.exists("barrating")) {
megamek.common.equipment.ArmorType armor = ArmorType.svArmor(dataFile.getDataAsInt("barrating")[0]);
sv.setArmorType(armor.getArmorType());
sv.setBARRating(armor.getBAR());
sv.setArmorTechLevel(armor.getStaticTechLevel().getCompoundTechLevel(false));
} else {
throw new EntityLoadingException("could not find armor_type block.");
if (dataFile.exists("armor_type")) {
sv.setArmorType(dataFile.getDataAsInt("armor_type")[0]);
} else {
throw new EntityLoadingException("could not find armor_type block.");
}
}
if (dataFile.exists("armor_tech")) {
sv.setArmorTechRating(dataFile.getDataAsInt("armor_tech")[0]);
Expand Down Expand Up @@ -746,7 +749,7 @@ public static BuildingBlock getBlock(Entity t) throws EntitySavingException {
// 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));
blk.writeBlockData("armor_tech_rating", t.getArmorTechRating());
} else if (t.hasPatchworkArmor()) {
blk.writeBlockData("armor_type",
EquipmentType.T_ARMOR_PATCHWORK);
Expand Down Expand Up @@ -868,6 +871,7 @@ public static BuildingBlock getBlock(Entity t) throws EntitySavingException {
if (t.isSupportVehicle()) {
blk.writeBlockData("structural_tech_rating", t.getStructuralTechRating());
blk.writeBlockData("engine_tech_rating", t.getEngineTechRating());

}

if (t.hasETypeFlag(Entity.ETYPE_SMALL_CRAFT) || t.hasETypeFlag(Entity.ETYPE_JUMPSHIP)) {
Expand Down Expand Up @@ -1163,8 +1167,13 @@ private static String encodeEquipmentLine(Mounted<?> m) {
}

public static void encode(String fileName, Entity t) throws EntitySavingException {
File file = new File(fileName);
encode(file, t);
}

public static void encode(File file, Entity t) throws EntitySavingException {
BuildingBlock blk = BLKFile.getBlock(t);
blk.writeBlockFile(fileName);
blk.writeBlockFile(file);
}

protected void addTransports(Entity e) throws EntityLoadingException {
Expand Down
45 changes: 20 additions & 25 deletions megamek/src/megamek/common/util/BuildingBlock.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* MegaMek - Copyright (C) 2000-2002 Ben Mazur ([email protected])
* MegaMek - Copyright (C) 2000-2024 Ben Mazur ([email protected])
*
* This program 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
Expand Down Expand Up @@ -385,17 +385,14 @@ public List<String> getDataAsVector(String blockName) {
/**
* Clears the <CODE>rawData</CODE> Vector and inserts a default comment and
* <I>BlockVersion</I> information.
*
* @return Returns true on success.
*/
public boolean createNewBlock() {
public void createNewBlock() {
rawData.clear();
writeBlockComment("Saved from version " + SuiteConstants.VERSION + " on " + LocalDate.now());
return true;
}

/**
* @see writeBlockData (String, Vector)
* @see #writeBlockData (String, Vector)
*/
public boolean writeBlockData(String blockName, String blockData) {
String[] temp = new String[1];
Expand All @@ -405,7 +402,7 @@ public boolean writeBlockData(String blockName, String blockData) {
}

/**
* @see writeBlockData (String, Vector)
* @see #writeBlockData (String, Vector)
*/
public boolean writeBlockData(String blockName, int blockData) {
String[] temp = new String[1];
Expand All @@ -414,7 +411,7 @@ public boolean writeBlockData(String blockName, int blockData) {
}

/**
* @see writeBlockData (String, Vector)
* @see #writeBlockData (String, Vector)
*/
public boolean writeBlockData(String blockName, int[] blockData) {
String[] temp = new String[blockData.length];
Expand All @@ -426,7 +423,7 @@ public boolean writeBlockData(String blockName, int[] blockData) {
}

/**
* @see writeBlockData (String, Vector)
* @see #writeBlockData (String, Vector)
*/
public boolean writeBlockData(String blockName, float blockData) {
String[] temp = new String[1];
Expand All @@ -435,7 +432,7 @@ public boolean writeBlockData(String blockName, float blockData) {
}

/**
* @see writeBlockData (String, Vector)
* @see #writeBlockData (String, Vector)
*/
public boolean writeBlockData(String blockName, double blockData) {
String[] temp = new String[1];
Expand All @@ -444,7 +441,7 @@ public boolean writeBlockData(String blockName, double blockData) {
}

/**
* @see writeBlockData (String, Vector)
* @see #writeBlockData (String, Vector)
*/
public boolean writeBlockData(String blockName, float[] blockData) {
String[] temp = new String[blockData.length];
Expand All @@ -455,7 +452,7 @@ public boolean writeBlockData(String blockName, float[] blockData) {
}

/**
* @see writeBlockData (String, Vector)
* @see #writeBlockData (String, Vector)
*/
public boolean writeBlockData(String blockName, String[] blockData) {
return writeBlockData(blockName, makeVector(blockData));
Expand Down Expand Up @@ -497,15 +494,14 @@ public boolean writeBlockComment(String theComment) {
/**
* Writes the buildingBlock data to a file.
*
* @param fileName File to write. Overwrites existing files.
* @param file File to write. Overwrites existing files.
* @return true on success.
*/
public boolean writeBlockFile(String fileName) {
File file = new File(fileName);
public boolean writeBlockFile(File file) {

if (file.exists()) {
if (!file.delete()) {
logger.error("Unable to delete file with name " + fileName);
logger.error("Unable to delete file with name " + file);
return false;
}
}
Expand All @@ -520,7 +516,7 @@ public boolean writeBlockFile(String fileName) {

bw.flush();
} catch (Exception e) {
logger.error("Unable to save block file " + fileName, e);
logger.error("Unable to save block file " + file.getPath(), e);
return false;
}

Expand Down Expand Up @@ -614,7 +610,7 @@ public String[] getAllDataAsString() {
/**
* Just about the same as the <CODE>getVector()</CODE> command.
*
* @see getVector ()
* @see #getVector()
* @return Returns the <CODE>rawData</CODE> Vector.
*/
public Vector<String> getAllDataAsVector() {
Expand Down Expand Up @@ -646,29 +642,28 @@ public int getReturnedArraySize(String[] array) {

// for those of us who like doing things indirectly ; -?
/**
* @see getReturnedArraySize (String[])
* @see #getReturnedArraySize (String[])
*/
public int getReturnedArraySize(int[] array) {
return array[0];
}

/**
* @see getReturnedArraySize (String[])
* @see #getReturnedArraySize (String[])
* @return Returns <CODE>array.size()</CODE>
*/
public int getReturnedArraySize(Vector<Object> array) {
return array.size();
}

/**
* @see getReturnedArraySize (String[])
* @see #getReturnedArraySize (String[])
*/
public int getReturnedArraySize(float[] array) {
try {
return Integer.parseInt("" + array[0]);
} catch (Exception ignored) {
logger.error(
"Couldn't find array size at [0]...is this an array I returned...? Trying to find the size anyway...");
logger.error("Couldn't find array size at [0], is this an array I returned? Trying to find the size anyway");
return this.countArray(array);
}
}
Expand All @@ -685,15 +680,15 @@ public int countArray(String[] array) {
}

/**
* @see countArray( String[] )
* @see #countArray( String[] )
*/
public int countArray(float[] array) {

return array.length;
}

/**
* @see countArray( String[] )
* @see #countArray( String[] )
*/
public int countArray(int[] array) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ void testBAR10ArmorCorrectSlots() {
// Rating E should return CV slots for IS FF
st.setArmorTechRating(ITechnology.RATING_E);
assertEquals(
2,
ArmorType.of(T_ARMOR_FERRO_FIBROUS, false).getSupportVeeSlots(st));
2,
ArmorType.of(T_ARMOR_FERRO_FIBROUS, false).getSupportVeeSlots(st));

// Rating F should return CV slots for Clan FF
st.setArmorTechRating(ITechnology.RATING_F);
assertEquals(
1,
ArmorType.of(T_ARMOR_FERRO_FIBROUS, true).getSupportVeeSlots(st));
1,
ArmorType.of(T_ARMOR_FERRO_FIBROUS, true).getSupportVeeSlots(st));
}
}

0 comments on commit 582de3c

Please sign in to comment.