From bafee8a3b1708cd3d858f5da5e2c718c88f10a1b Mon Sep 17 00:00:00 2001 From: Robin Date: Fri, 22 Oct 2021 12:02:13 +0200 Subject: [PATCH] Cleanup --- .../romraider/editor/ecu/OpenImageWorker.java | 36 ++-- src/main/java/com/romraider/maps/RomID.java | 3 + .../BMWCodingConversionLayer.java | 185 ++---------------- .../BMWCodingConversion/BMWRomManager.java | 169 ++++++++++++++++ .../com/romraider/xml/ConversionLayer.java | 1 + .../com/romraider/xml/DOMRomUnmarshaller.java | 22 +++ 6 files changed, 227 insertions(+), 189 deletions(-) rename src/main/java/com/romraider/xml/{ => BMWCodingConversion}/BMWCodingConversionLayer.java (77%) create mode 100644 src/main/java/com/romraider/xml/BMWCodingConversion/BMWRomManager.java diff --git a/src/main/java/com/romraider/editor/ecu/OpenImageWorker.java b/src/main/java/com/romraider/editor/ecu/OpenImageWorker.java index aef77abb..0802e9f0 100644 --- a/src/main/java/com/romraider/editor/ecu/OpenImageWorker.java +++ b/src/main/java/com/romraider/editor/ecu/OpenImageWorker.java @@ -18,7 +18,7 @@ import org.xml.sax.SAXParseException; import com.romraider.Settings; import com.romraider.maps.Rom; import com.romraider.util.SettingsManager; -import com.romraider.xml.BMWCodingConversionLayer; +import com.romraider.xml.BMWCodingConversion.BMWCodingConversionLayer; import com.romraider.xml.ConversionLayer; import com.romraider.xml.DOMRomUnmarshaller; import com.romraider.xml.RomNotFoundException; @@ -79,23 +79,27 @@ public class OpenImageWorker extends SwingWorker { //Check if definition is standard or //if it has to be converted first boolean found = false; - try { - for(ConversionLayer l: convLayers) { - if(l.isFileSupported(f)) { - doc = l.convertToDocumentTree(f); - - if(doc!=null) { - found = true; - break; - } - } + + //Check if xml file + if(!f.getName().matches("^.*\\.(xml|XML)$")) { + try { + for(ConversionLayer l: convLayers) { + if(l.isFileSupported(f)) { + doc = l.convertToDocumentTree(f); + + if(doc!=null) { + found = true; + break; + } + } + } } + catch(Exception e) { + e.printStackTrace(); + continue; + } } - catch(Exception e) { - e.printStackTrace(); - continue; - } - + //Default case if(!found) doc = docBuilder.parse(fileStream, f.getAbsolutePath()); diff --git a/src/main/java/com/romraider/maps/RomID.java b/src/main/java/com/romraider/maps/RomID.java index deffc9cc..de101ede 100644 --- a/src/main/java/com/romraider/maps/RomID.java +++ b/src/main/java/com/romraider/maps/RomID.java @@ -55,6 +55,9 @@ public class RomID implements Serializable { try { if(internalIdString == null || internalIdString.length() == 0) return false; + //If both fields are set to force, use this definition no matter what + if(internalIdAddress == -1 && internalIdString.equalsIgnoreCase("force")) return true; + // romid is hex string if (internalIdString.length() > 2 && internalIdString.substring(0, 2).equalsIgnoreCase("0x")) { diff --git a/src/main/java/com/romraider/xml/BMWCodingConversionLayer.java b/src/main/java/com/romraider/xml/BMWCodingConversion/BMWCodingConversionLayer.java similarity index 77% rename from src/main/java/com/romraider/xml/BMWCodingConversionLayer.java rename to src/main/java/com/romraider/xml/BMWCodingConversion/BMWCodingConversionLayer.java index 651b794c..3920e0ec 100644 --- a/src/main/java/com/romraider/xml/BMWCodingConversionLayer.java +++ b/src/main/java/com/romraider/xml/BMWCodingConversion/BMWCodingConversionLayer.java @@ -17,7 +17,7 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -package com.romraider.xml; +package com.romraider.xml.BMWCodingConversion; import static com.romraider.editor.ecu.ECUEditorManager.getECUEditor; import static com.romraider.swing.LookAndFeelManager.initLookAndFeel; @@ -38,12 +38,6 @@ import java.util.Set; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerConfigurationException; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; import org.apache.log4j.Logger; import org.w3c.dom.Document; @@ -53,8 +47,8 @@ import org.w3c.dom.Node; import com.romraider.Settings; import com.romraider.editor.ecu.ECUEditor; import com.romraider.editor.ecu.OpenImageWorker; -import com.romraider.maps.Rom; import com.romraider.util.SettingsManager; +import com.romraider.xml.ConversionLayer; public class BMWCodingConversionLayer implements ConversionLayer { private static final Logger LOGGER = Logger.getLogger(BMWCodingConversionLayer.class); @@ -112,14 +106,9 @@ public class BMWCodingConversionLayer implements ConversionLayer { this.splitAddress = splitAddress; this.guessChecksums = guessChecksums; } - - //TODO: DELETE - public static boolean isFileSupportedS(File f) { - return f.getName().matches("^[\\w,\\s-]+\\.C\\d\\d"); - } public boolean isFileSupported(File f) { - return f.getName().matches("^[\\w,\\s-]+\\.C\\d\\d"); + return f.getName().matches("^.*\\.C\\d\\d$"); } //Reads a string in an array until zero byte @@ -431,21 +420,14 @@ public class BMWCodingConversionLayer implements ConversionLayer { for(BMWRomManager man: romManagers){ man.calculateRomID(f); } - - /* - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer trans = tf.newTransformer(); - StringWriter sw = new StringWriter(); - trans.transform(new DOMSource(doc), new StreamResult(sw)); - System.out.println(sw.toString());*/ - - return doc; - + + return doc; } private void parseMemoryOrg() { String layout = readString(dataBuffer.array(), dataIndex+1); + /* if(layout.equalsIgnoreCase("byte")) memoryLayout = "uint8"; else if(layout.equalsIgnoreCase("wordmsb")) { memoryLayout = "uint16"; @@ -455,7 +437,7 @@ public class BMWCodingConversionLayer implements ConversionLayer { memoryLayout = "uint16"; endian = "little"; } - + */ dataIndex+= layout.length() + 2; //String blockType = readString(input, i); //? What does it do? //isBlock = blockType.equals("BLOCK"); @@ -609,148 +591,6 @@ public class BMWCodingConversionLayer implements ConversionLayer { } } - - class BMWRomManager{ - int offsetAddress = 0; - - Document doc; - Element romNode; - - Element lastTable; - int lastStorageAddress; - int maxStorageAddress; - - //Variables to find the best ID settings - int bestIDFitAddress; - String bestIDFitData = ""; - int bestIDFitAddressTemp; - String bestIDFitDataTemp = ""; - int lastPresetCount = 0; - - BMWRomManager(int offsetAddress, Document doc, Node root){ - this.doc = doc; - this.offsetAddress = offsetAddress; - this.romNode = doc.createElement("rom"); - root.appendChild(romNode); - } - - public Element getRomNode() { - return romNode; - } - - - public Element createTable(String name, String category, String storageType, String endian, - int storageAddress, int byteCount, byte[] mask) { - - //This handles the correct rom splitting - if(offsetAddress == 0 || storageAddress >= offsetAddress) { - - if(!bestIDFitDataTemp.equals("") && lastPresetCount == 1) { - bestIDFitData = bestIDFitDataTemp; - bestIDFitAddress = bestIDFitAddressTemp; - } - - lastPresetCount = 0; - - if(byteCount == 1) storageType="uint8"; - int sizey = storageType.equalsIgnoreCase("uint8") ? byteCount : byteCount/2; - - Element table = doc.createElement("table"); - table.setAttribute("name", name); - table.setAttribute("category", category); - table.setAttribute("storagetype", storageType); - table.setAttribute("storageaddress", "0x" + - Integer.toHexString(storageAddress - offsetAddress)); - table.setAttribute("endian", endian); - table.setAttribute("sizey", Integer.toString(sizey)); - table.setAttribute("type", "1D"); - table.setAttribute("mask", Integer.toHexString(Byte.toUnsignedInt(mask[0]))); - - romNode.appendChild(table); - lastTable = table; - - lastStorageAddress = storageAddress - offsetAddress; - if(lastStorageAddress > maxStorageAddress) maxStorageAddress = lastStorageAddress; - - return table; - } - else { - return null; - } - } - - public Element addPreset(String data, String name, Node table) { - if(offsetAddress == 0 || this.lastTable == table) { - Element statePSW1 = doc.createElement("state"); - statePSW1.setAttribute("data", data); - statePSW1.setAttribute("name", name); - lastTable.appendChild(statePSW1); - - data = data.replace(" ", ""); - - if(lastPresetCount >= 1) { - bestIDFitDataTemp = ""; - } - else if(data.length() >= 2 && bestIDFitData.length() <= data.length() && - !data.replace("0", "").equals("") && !data.replace("F", "").equals("")){ - bestIDFitDataTemp = data; - bestIDFitAddressTemp = lastStorageAddress; - } - - lastPresetCount++; - - return statePSW1; - } - else { - return null; - } - } - - public void calculateRomID(File f) { - Element romIDNode = doc.createElement("romid"); - - //romNode.setTextContent("Test"); - Node idAddress = doc.createElement("internalidaddress"); - idAddress.setTextContent("0x" + Integer.toHexString(bestIDFitAddress)); - Node idString = doc.createElement("internalidstring"); - idString.setTextContent("0x" + bestIDFitData.replace(" ", "")); - Node ramoffset = doc.createElement("noramoffset"); - - //Set filesize based on largest address and round up to a power of 2 - Node fileSize = doc.createElement("filesize"); - String fileS = ((int)Math.pow(2, 32 - Integer.numberOfLeadingZeros(lastStorageAddress - 1)) + "b"); - fileSize.setTextContent(fileS); - - Node make = doc.createElement("make"); - make.setTextContent("BMW"); - - Node model = doc.createElement("model"); - model.setTextContent(f.getParentFile().getName()); - - Node ecuID = doc.createElement("ecuid"); - String[] nameSplit = f.getName().split("\\."); - ecuID.setTextContent(nameSplit[1]); - - Node subModel = doc.createElement("submodel"); - subModel.setTextContent(f.getName()); - - romIDNode.appendChild(make); - romIDNode.appendChild(ecuID); - romIDNode.appendChild(model); - romIDNode.appendChild(subModel); - romIDNode.appendChild(ramoffset); - romIDNode.appendChild(fileSize); - romIDNode.appendChild(idAddress); - romIDNode.appendChild(idString); - romIDNode.appendChild(doc.createElement("year")); - romIDNode.appendChild(doc.createElement("market")); - romIDNode.appendChild(doc.createElement("transmission")); - romIDNode.appendChild(doc.createElement("xmlid")); - romNode.appendChild(romIDNode); - - } - } - public static Collection listFileTree(File dir) { Set fileTree = new HashSet(); if(dir==null||dir.listFiles()==null){ @@ -775,15 +615,14 @@ public class BMWCodingConversionLayer implements ConversionLayer { Settings settings = SettingsManager.getSettings(); settings.getEcuDefinitionFiles().clear(); + settings.getEcuDefinitionFiles().add(new File("C:\\NCSEXPER\\DATEN\\E46\\KMB_E46.C08")); + //settings.getEcuDefinitionFiles().add(new File("C:\\NCSEXPER\\DATEN\\E46\\IHK_E46.C17")); + //settings.getEcuDefinitionFiles().add(new File("C:\\NCSEXPER\\DATEN\\E46\\GM5.C04")); + + /* File folder = new File("C:\\NCSEXPER\\DATEN\\"); Collection listOfFiles = listFileTree(folder); - settings.getEcuDefinitionFiles().add(new File("C:\\NCSEXPER\\DATEN\\E46\\KMB_E46.C08")); - settings.getEcuDefinitionFiles().add(new File("C:\\NCSEXPER\\DATEN\\E46\\IHK_E46.C17")); - settings.getEcuDefinitionFiles().add(new File("C:\\NCSEXPER\\DATEN\\E46\\GM5.C04")); - - - /* for(File f: listOfFiles) { if (isFileSupportedS(f)) { settings.getEcuDefinitionFiles().add(f); diff --git a/src/main/java/com/romraider/xml/BMWCodingConversion/BMWRomManager.java b/src/main/java/com/romraider/xml/BMWCodingConversion/BMWRomManager.java new file mode 100644 index 00000000..b0950e5d --- /dev/null +++ b/src/main/java/com/romraider/xml/BMWCodingConversion/BMWRomManager.java @@ -0,0 +1,169 @@ +/* + * RomRaider Open-Source Tuning, Logging and Reflashing + * Copyright (C) 2006-2021 RomRaider.com + * + * 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 Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package com.romraider.xml.BMWCodingConversion; + +import java.io.File; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +class BMWRomManager{ + int offsetAddress = 0; + + Document doc; + Element romNode; + + Element lastTable; + int lastStorageAddress; + int maxStorageAddress; + + //Variables to find the best ID settings + int bestIDFitAddress; + String bestIDFitData = ""; + int bestIDFitAddressTemp; + String bestIDFitDataTemp = ""; + int lastPresetCount = 0; + + BMWRomManager(int offsetAddress, Document doc, Node root){ + this.doc = doc; + this.offsetAddress = offsetAddress; + this.romNode = doc.createElement("rom"); + root.appendChild(romNode); + } + + public Element getRomNode() { + return romNode; + } + + + public Element createTable(String name, String category, String storageType, String endian, + int storageAddress, int byteCount, byte[] mask) { + + //This handles the correct rom splitting + if(offsetAddress == 0 || storageAddress >= offsetAddress) { + + if(!bestIDFitDataTemp.equals("") && lastPresetCount == 1) { + bestIDFitData = bestIDFitDataTemp; + bestIDFitAddress = bestIDFitAddressTemp; + } + + lastPresetCount = 0; + + if(byteCount == 1) storageType="uint8"; + int sizey = storageType.equalsIgnoreCase("uint8") ? byteCount : byteCount/2; + + Element table = doc.createElement("table"); + table.setAttribute("name", name); + table.setAttribute("category", category); + table.setAttribute("storagetype", storageType); + table.setAttribute("storageaddress", "0x" + + Integer.toHexString(storageAddress - offsetAddress)); + table.setAttribute("endian", endian); + table.setAttribute("sizey", Integer.toString(sizey)); + table.setAttribute("type", "1D"); + table.setAttribute("mask", Integer.toHexString(Byte.toUnsignedInt(mask[0]))); + + romNode.appendChild(table); + lastTable = table; + + lastStorageAddress = storageAddress - offsetAddress; + if(lastStorageAddress > maxStorageAddress) maxStorageAddress = lastStorageAddress; + + return table; + } + else { + return null; + } + } + + public Element addPreset(String data, String name, Node table) { + if(offsetAddress == 0 || this.lastTable == table) { + Element statePSW1 = doc.createElement("state"); + statePSW1.setAttribute("data", data); + statePSW1.setAttribute("name", name); + lastTable.appendChild(statePSW1); + + data = data.replace(" ", ""); + + if(lastPresetCount >= 1) { + bestIDFitDataTemp = ""; + } + else if(data.length() >= 2 && bestIDFitData.length() <= data.length() && + !data.replace("0", "").equals("") && !data.replace("F", "").equals("")){ + bestIDFitDataTemp = data; + bestIDFitAddressTemp = lastStorageAddress; + } + + lastPresetCount++; + + return statePSW1; + } + else { + return null; + } + } + + public void calculateRomID(File f) { + Element romIDNode = doc.createElement("romid"); + + //romNode.setTextContent("Test"); + Node idAddress = doc.createElement("internalidaddress"); + //idAddress.setTextContent("0x" + Integer.toHexString(bestIDFitAddress)); + idAddress.setTextContent("-1"); + Node idString = doc.createElement("internalidstring"); + //idString.setTextContent("0x" + bestIDFitData.replace(" ", "")); + idString.setTextContent("force"); + Node ramoffset = doc.createElement("noramoffset"); + + //Set filesize based on largest address and round up to a power of 2 + Node fileSize = doc.createElement("filesize"); + String fileS = ((int)Math.pow(2, 32 - Integer.numberOfLeadingZeros(lastStorageAddress - 1)) + "b"); + fileSize.setTextContent(fileS); + + Node make = doc.createElement("make"); + make.setTextContent("BMW"); + + Node model = doc.createElement("model"); + model.setTextContent(f.getParentFile().getName()); + + Node ecuID = doc.createElement("ecuid"); + String[] nameSplit = f.getName().split("\\."); + ecuID.setTextContent(nameSplit[1]); + + Node subModel = doc.createElement("submodel"); + subModel.setTextContent(f.getName()); + + romIDNode.appendChild(make); + romIDNode.appendChild(ecuID); + romIDNode.appendChild(model); + romIDNode.appendChild(subModel); + romIDNode.appendChild(ramoffset); + romIDNode.appendChild(fileSize); + romIDNode.appendChild(idAddress); + romIDNode.appendChild(idString); + romIDNode.appendChild(doc.createElement("year")); + romIDNode.appendChild(doc.createElement("market")); + romIDNode.appendChild(doc.createElement("transmission")); + romIDNode.appendChild(doc.createElement("xmlid")); + romNode.appendChild(romIDNode); + + } +} \ No newline at end of file diff --git a/src/main/java/com/romraider/xml/ConversionLayer.java b/src/main/java/com/romraider/xml/ConversionLayer.java index 6136bcd6..cf900f11 100644 --- a/src/main/java/com/romraider/xml/ConversionLayer.java +++ b/src/main/java/com/romraider/xml/ConversionLayer.java @@ -31,6 +31,7 @@ public interface ConversionLayer { /* * This method receives a file and checks if this converter supports this file * extension. + * TODO: This should be static, but not supported in Java 6 */ public abstract boolean isFileSupported(File f); } diff --git a/src/main/java/com/romraider/xml/DOMRomUnmarshaller.java b/src/main/java/com/romraider/xml/DOMRomUnmarshaller.java index f408db35..ac681197 100644 --- a/src/main/java/com/romraider/xml/DOMRomUnmarshaller.java +++ b/src/main/java/com/romraider/xml/DOMRomUnmarshaller.java @@ -23,12 +23,18 @@ import static com.romraider.xml.DOMHelper.unmarshallAttribute; import static com.romraider.xml.DOMHelper.unmarshallText; import static org.w3c.dom.Node.ELEMENT_NODE; +import java.io.StringWriter; import java.util.HashMap; import java.util.Map; import javax.management.modelmbean.XMLParseException; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; import org.apache.log4j.Logger; +import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -280,4 +286,20 @@ public final class DOMRomUnmarshaller { } return ChecksumFactory.getManager(attrs); } + + private static String convertDocumentToString(Document doc) { + StringWriter sw = new StringWriter(); + + try { + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer trans = tf.newTransformer(); + trans.transform(new DOMSource(doc), new StreamResult(sw)); + } + catch(Exception e) { + e.printStackTrace(); + return null; + } + + return sw.toString(); + } }