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

New names cleanup and potential bugfixes #5043

Merged
merged 5 commits into from
Oct 13, 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
18 changes: 2 additions & 16 deletions MekHQ/src/mekhq/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -1322,23 +1322,9 @@ public static int selectBestBayFor(Entity cargo, Entity transport) {
}

/**
* Testable function to get the original unit based on information from a new
* unit
*
* @param newE new Entity we want to read information from
* @return MekSummary that most closely represents the original of the new
* Entity
* @param shortNameRaw complete Entity name as returned by getShortNameRaw()
* @throws EntityLoadingException
*/
public static MekSummary retrieveOriginalUnit(Entity newE) throws EntityLoadingException {
// this might be better with refreshUnitData() and/or elsewhere
MekSummaryCache.getInstance().loadMekData();

// I need to change the new entity to the one from the mtf file now, so that
// equipment numbers will match
return retrieveUnit(newE.getShortNameRaw());
}

*/
public static MekSummary retrieveUnit(String shortNameRaw) throws EntityLoadingException {
MekSummary summary = MekSummaryCache.getInstance().getMek(shortNameRaw);

Expand Down
3 changes: 1 addition & 2 deletions MekHQ/src/mekhq/campaign/Campaign.java
Original file line number Diff line number Diff line change
Expand Up @@ -6880,8 +6880,7 @@ public void addCustom(String name) {
}

public boolean isCustom(Unit u) {
return customs.contains(u.getEntity().getChassis() + ' '
+ u.getEntity().getModel());
return customs.contains(u.getEntity().getShortNameRaw());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion MekHQ/src/mekhq/campaign/mission/Loot.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public void writeToXML(final PrintWriter pw, int indent) {
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "name", name);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "cash", getCash());
for (Entity e : units) {
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "entityName", e.getChassis() + ' ' + e.getModel());
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "entityName", e.getShortNameRaw());
}

for (Part p : parts) {
Expand Down
45 changes: 19 additions & 26 deletions MekHQ/src/mekhq/campaign/parts/Refit.java
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,8 @@ private void changeAmmoBinMunitions(final Unit unit) {
public void saveCustomization() throws EntityLoadingException {
UnitUtil.compactCriticals(newEntity);

String fileName = MHQXMLUtility.escape(newEntity.getChassis() + ' ' + newEntity.getModel());
String unitName = newEntity.getShortNameRaw();
String fileName = MHQXMLUtility.escape(unitName);
String sCustomsDir = String.join(File.separator, "data", "mekfiles", "customs"); // TODO : Remove inline file
// path
String sCustomsDirCampaign = sCustomsDir + File.separator + getCampaign().getName();
Expand All @@ -1632,46 +1633,39 @@ public void saveCustomization() throws EntityLoadingException {

String fileNameCampaign;
try {
String fileExtension = newEntity instanceof Mek ? ".mtf" : ".blk";
String fileOutName = sCustomsDir + File.separator + fileName + fileExtension;
fileNameCampaign = sCustomsDirCampaign + File.separator + fileName + fileExtension;

// if this file already exists then don't overwrite it or we will end up with a bunch of copies
if ((new File(fileOutName)).exists() || (new File(fileNameCampaign)).exists()) {
throw new IOException("A file already exists with the custom name " + fileNameCampaign
+ ". Please choose a different name. (Unit name and/or model)");
}

if (newEntity instanceof Mek) {
// if this file already exists then don't overwrite it or we will end up with a
// bunch of copies
String fileOutName = sCustomsDir + File.separator + fileName + ".mtf";
fileNameCampaign = sCustomsDirCampaign + File.separator + fileName + ".mtf";
if ((new File(fileOutName)).exists() || (new File(fileNameCampaign)).exists()) {
throw new IOException("A file already exists with the custom name " + fileNameCampaign
+ ". Please choose a different name. (Unit name and/or model)");
}
try (FileOutputStream out = new FileOutputStream(fileNameCampaign);
PrintStream p = new PrintStream(out)) {
p.println(((Mek) newEntity).getMtf());
}
} else {
// if this file already exists then don't overwrite it or we will end up with a
// bunch of copies
String fileOutName = sCustomsDir + File.separator + fileName + ".blk";
fileNameCampaign = sCustomsDirCampaign + File.separator + fileName + ".blk";
if ((new File(fileOutName)).exists() || (new File(fileNameCampaign)).exists()) {
throw new IOException("A file already exists with the custom name " + fileNameCampaign
+ ". Please choose a different name. (Unit name and/or model)");
}
BLKFile.encode(fileNameCampaign, newEntity);
}
} catch (Exception ex) {
logger.error("", ex);
fileNameCampaign = null;
}

getCampaign().addCustom(newEntity.getChassis() + " " + newEntity.getModel());
getCampaign().addCustom(unitName);
MekSummaryCache.refreshUnitData(false);

try {
MekSummary summary = Utilities.retrieveOriginalUnit(newEntity);
MekSummary summary = Utilities.retrieveUnit(newEntity.getShortNameRaw());

newEntity = new MekFileParser(summary.getSourceFile(), summary.getEntryName()).getEntity();
logger.info(String.format("Saved %s %s to %s",
newEntity.getChassis(), newEntity.getModel(), summary.getSourceFile()));
logger.info(String.format("Saved %s to %s", unitName, summary.getSourceFile()));
} catch (EntityLoadingException ex) {
logger.error(String.format("Could not read back refit entity %s %s",
newEntity.getChassis(), newEntity.getModel()), ex);
logger.error(String.format("Could not read back refit entity %s", unitName), ex);

if (fileNameCampaign != null) {
logger.warn("Deleting invalid refit file " + fileNameCampaign);
Expand All @@ -1680,11 +1674,10 @@ public void saveCustomization() throws EntityLoadingException {
} catch (SecurityException ex2) {
logger.warn("Could not clean up bad refit file " + fileNameCampaign, ex2);
}
// Reload the mek cache if we had to delete the file
MekSummaryCache.refreshUnitData(false);
}

// Reload the mek cache if we had to delete the file
MekSummaryCache.getInstance().loadMekData();

throw ex;
}
}
Expand Down
3 changes: 1 addition & 2 deletions MekHQ/src/mekhq/campaign/unit/UnitOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ public String getQuantityName(int quantity) {

@Override
public Object getNewEquipment() {
String name = getEntity().getFullChassis() + " " + getEntity().getModel();
name = name.trim();
String name = getEntity().getShortNameRaw();
MekSummary summary = MekSummaryCache.getInstance().getMek(name);
if (null == summary) {
logger.error("Could not find a mek summary for " + name);
Expand Down
49 changes: 20 additions & 29 deletions MekHQ/src/mekhq/gui/adapter/UnitTableMouseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1093,48 +1093,39 @@ private void addCustomUnitTag(Unit... units) {
}
}
for (Unit unit : units) {
String fileName = unit.getEntity().getChassis() + ' ' + unit.getEntity().getModel();
if (unit.getEntity() instanceof Mek) {
// if this file already exists then don't overwrite
// it or we will end up with a bunch of copies
String fileOutName = MHQConstants.CUSTOM_MEKFILES_DIRECTORY_PATH + File.separator
+ fileName + ".mtf";
String fileNameCampaign = sCustomsDirCampaign + File.separator + fileName + ".mtf";
if ((new File(fileOutName)).exists() || (new File(fileNameCampaign)).exists()) {
JOptionPane.showMessageDialog(null,
"A file already exists for this unit, cannot tag as custom. (Unit name and model)",
"File Already Exists", JOptionPane.ERROR_MESSAGE);
return;
}
Entity entity = unit.getEntity();
String unitName = entity.getShortNameRaw();
String fileExtension = entity instanceof Mek ? ".mtf" : ".blk";
String fileOutName = MHQConstants.CUSTOM_MEKFILES_DIRECTORY_PATH + File.separator
+ unitName + fileExtension;
String fileNameCampaign = sCustomsDirCampaign + File.separator + unitName + fileExtension;

// if this file already exists then don't overwrite it or we will end up with a bunch of copies
if ((new File(fileOutName)).exists() || (new File(fileNameCampaign)).exists()) {
JOptionPane.showMessageDialog(null,
"A file already exists for this unit, cannot tag as custom. (Unit name and model)",
"File Already Exists", JOptionPane.ERROR_MESSAGE);
return;
}

if (entity instanceof Mek) {
try (OutputStream os = new FileOutputStream(fileNameCampaign);
PrintStream p = new PrintStream(os)) {

p.println(((Mek) unit.getEntity()).getMtf());
p.println(((Mek) entity).getMtf());
} catch (Exception e) {
logger.error("", e);
}
} else {
// if this file already exists then don't overwrite
// it or we will end up with a bunch of copies
String fileOutName = MHQConstants.CUSTOM_MEKFILES_DIRECTORY_PATH + File.separator
+ fileName + ".blk";
String fileNameCampaign = sCustomsDirCampaign + File.separator + fileName + ".blk";
if ((new File(fileOutName)).exists() || (new File(fileNameCampaign)).exists()) {
JOptionPane.showMessageDialog(null,
"A file already exists for this unit, cannot tag as custom. (Unit name and model)",
"File Already Exists", JOptionPane.ERROR_MESSAGE);
return;
}
try {
BLKFile.encode(fileNameCampaign, unit.getEntity());
BLKFile.encode(fileNameCampaign, entity);
} catch (EntitySavingException e) {
logger.error("Error encoding unit {}", unit.getName(), e);
return;
}
}
gui.getCampaign().addCustom(unit.getEntity().getChassis() + ' '
+ unit.getEntity().getModel());
gui.getCampaign().addCustom(unitName);
}
MekSummaryCache.getInstance().loadMekData();
MekSummaryCache.refreshUnitData(false);
}
}
7 changes: 4 additions & 3 deletions MekHQ/src/mekhq/gui/dialog/RefitNameDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,17 @@ private void btnOKActionPerformed(ActionEvent evt) {
}
if (model.isEmpty()) {
model = refit.getOriginalEntity().getModel() + " Mk II";
model = model.trim(); // remove leading space if original model name was blank
}
if (null != MekSummaryCache.getInstance().getMek(chassis + " " + model)) {
refit.getNewEntity().setChassis(chassis);
refit.getNewEntity().setModel(model);
if (null != MekSummaryCache.getInstance().getMek(refit.getNewEntity().getShortNameRaw())) {
JOptionPane.showMessageDialog(null,
"There is already a unit in the database with this name.\nPlease select a different name.",
"Name already in use",
JOptionPane.ERROR_MESSAGE);
return;
}
refit.getNewEntity().setChassis(chassis);
refit.getNewEntity().setModel(model);
this.setVisible(false);
}

Expand Down