Cleanup and generalization fixes

This commit is contained in:
Robin 2021-11-04 19:38:52 +01:00
parent bafee8a3b1
commit b9e5762d5a
6 changed files with 135 additions and 103 deletions

View File

@ -18,15 +18,14 @@ import org.xml.sax.SAXParseException;
import com.romraider.Settings;
import com.romraider.maps.Rom;
import com.romraider.util.SettingsManager;
import com.romraider.xml.BMWCodingConversion.BMWCodingConversionLayer;
import com.romraider.xml.ConversionLayer;
import com.romraider.xml.DOMRomUnmarshaller;
import com.romraider.xml.RomNotFoundException;
import com.romraider.xml.ConversionLayer.ConversionLayer;
import com.romraider.xml.ConversionLayer.ConversionLayerFactory;
public class OpenImageWorker extends SwingWorker<Void, Void> {
private final File inputFile;
private final ConversionLayer[] convLayers = {new BMWCodingConversionLayer()};
public OpenImageWorker(File inputFile) {
this.inputFile = inputFile;
}
@ -78,31 +77,23 @@ public class OpenImageWorker extends SwingWorker<Void, Void> {
//Check if definition is standard or
//if it has to be converted first
boolean found = false;
//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;
}
}
if(ConversionLayerFactory.requiresConversionLayer(f)) {
ConversionLayer l = ConversionLayerFactory.getConversionLayerForFile(f);
if(l != null) {
doc = l.convertToDocumentTree(f);
if(doc == null) {
fileStream.close();
throw new SAXParseException("Unknown file format!", null);
}
}
}
//Default case
if(!found) doc = docBuilder.parse(fileStream, f.getAbsolutePath());
else {
doc = docBuilder.parse(fileStream, f.getAbsolutePath());
}
try {
rom = new DOMRomUnmarshaller().unmarshallXMLDefinition(doc.getDocumentElement(), input, editor.getStatusPanel());
} catch (RomNotFoundException rex) {

View File

@ -17,7 +17,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.romraider.xml.BMWCodingConversion;
package com.romraider.xml.ConversionLayer;
import static com.romraider.editor.ecu.ECUEditorManager.getECUEditor;
import static com.romraider.swing.LookAndFeelManager.initLookAndFeel;
@ -48,9 +48,9 @@ import com.romraider.Settings;
import com.romraider.editor.ecu.ECUEditor;
import com.romraider.editor.ecu.OpenImageWorker;
import com.romraider.util.SettingsManager;
import com.romraider.xml.ConversionLayer;
import com.romraider.xml.ConversionLayer.ConversionLayer;;
public class BMWCodingConversionLayer implements ConversionLayer {
public class BMWCodingConversionLayer extends ConversionLayer {
private static final Logger LOGGER = Logger.getLogger(BMWCodingConversionLayer.class);
private int splitAddress = 0;
@ -62,7 +62,7 @@ public class BMWCodingConversionLayer implements ConversionLayer {
HashMap <Integer, String> csvMap;
HashMap <String, String> transMap;
BMWRomManager[] romManagers;
ConversionRomNodeManager[] romManagers;
ByteBuffer dataBuffer;
int dataIndex;
@ -158,7 +158,7 @@ public class BMWCodingConversionLayer implements ConversionLayer {
ByteBuffer fswBuffer = ByteBuffer.wrap(fswInput);
fswBuffer = fswBuffer.order(ByteOrder.LITTLE_ENDIAN);
for(int i=0x5E; i < fswInput.length;) {
for(int i = getStartOfFile(fswBuffer); i < fswInput.length;) {
int oldIndex = i;
int length = fswBuffer.get(i);
@ -207,7 +207,7 @@ public class BMWCodingConversionLayer implements ConversionLayer {
*/
String currentOption = "";
for(int i=0xEA; i < fswInput.length;) {
for(int i = getStartOfFile(cvtBuffer); i < fswInput.length;) {
int oldIndex = i;
int length = cvtBuffer.get(i);
@ -253,7 +253,19 @@ public class BMWCodingConversionLayer implements ConversionLayer {
return map;
}
//Look for 0xFFFF in the file to skip the header
private static int getStartOfFile(ByteBuffer b) {
for(int i=0; i < b.capacity();i++) {
int value = b.getShort(i);
if(value == -1) {
return i+2;
}
}
return -1;
}
public Document convertToDocumentTree(File f) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
@ -326,11 +338,11 @@ public class BMWCodingConversionLayer implements ConversionLayer {
//Create one manager if no splitting
//Create two otherwise
if(splitAddress == 0) romManagers= new BMWRomManager[] {new BMWRomManager(0, doc, roms)};
if(splitAddress == 0) romManagers= new ConversionRomNodeManager[] {new ConversionRomNodeManager(0, doc, roms)};
else
romManagers= new BMWRomManager[] {
new BMWRomManager(0, doc, roms),
new BMWRomManager(splitAddress, doc, roms)};
romManagers= new ConversionRomNodeManager[] {
new ConversionRomNodeManager(0, doc, roms),
new ConversionRomNodeManager(splitAddress, doc, roms)};
/*
* 0000 - DATEINAME - S - NAME
@ -355,13 +367,7 @@ public class BMWCodingConversionLayer implements ConversionLayer {
*/
//Look for 0xFFFF in the file to skip the header
for(int i=0; i < input.length;i++) {
int value = dataBuffer.getShort(i);
if(value == -1) {
dataIndex = i+2;
break;
}
}
dataIndex = getStartOfFile(dataBuffer);
if(dataIndex == 0) {
LOGGER.error("Failed to find start of file for " + f.toString());
@ -417,8 +423,8 @@ public class BMWCodingConversionLayer implements ConversionLayer {
dataIndex = oldIndex+length + 4;
}
for(BMWRomManager man: romManagers){
man.calculateRomID(f);
for(ConversionRomNodeManager man: romManagers){
man.calculateRomID(f, "BMW");
}
return doc;
@ -469,7 +475,7 @@ public class BMWCodingConversionLayer implements ConversionLayer {
//Create actual node in rom
for(BMWRomManager man: romManagers){
for(ConversionRomNodeManager man: romManagers){
Element table = man.createTable("UNUSED_" + unusedCounter,
currentCategory, memoryLayout, endian,storageAddressU, byteCountU, maskU);
@ -542,7 +548,7 @@ public class BMWCodingConversionLayer implements ConversionLayer {
}
//Create actual node in rom
for(BMWRomManager man: romManagers){
for(ConversionRomNodeManager man: romManagers){
Element table = man.createTable(nameFSWD,
currentCategory, memoryLayout, endian,storageAddressD, byteCountD, maskD);
@ -586,12 +592,14 @@ public class BMWCodingConversionLayer implements ConversionLayer {
if(csvMap.containsKey(key))namePSW += " | " + csvMap.get(key);
}
for(BMWRomManager man: romManagers){
for(ConversionRomNodeManager man: romManagers){
man.addPreset(PSW1_s.trim(), namePSW.trim(), currentTable);
}
}
public static Collection<File> listFileTree(File dir) {
//Used for test code only
//Gets all files within folder
private static Collection<File> listFileTree(File dir) {
Set<File> fileTree = new HashSet<File>();
if(dir==null||dir.listFiles()==null){
return fileTree;
@ -603,6 +611,7 @@ public class BMWCodingConversionLayer implements ConversionLayer {
return fileTree;
}
//Test Code
public static void main(String args[]) {
initDebugLogging();
initLookAndFeel();
@ -615,27 +624,28 @@ 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\\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<File> listOfFiles = listFileTree(folder);
ConversionLayer l = new BMWCodingConversionLayer();
for(File f: listOfFiles) {
if (isFileSupportedS(f)) {
if (l.isFileSupported(f)) {
settings.getEcuDefinitionFiles().add(f);
}
}*/
}
//settings.getEcuDefinitionFiles().add(new File("C:\\NCSEXPER\\DATEN\\E36\\KMB_E36.C25"));
//OpenImageWorker w = new OpenImageWorker(new File("E:\\google_drive\\ECU_Tuning\\maps\\Tacho\\Tacho Grau\\C25_352k_248_oil_6Cyl.hex"));
OpenImageWorker w = new OpenImageWorker(new File("E:\\google_drive\\ECU_Tuning\\maps\\Tacho\\Tacho Grau\\C25_352k_248_oil_6Cyl.hex"));
//OpenImageWorker w = new OpenImageWorker(new File("E:\\Downloads\\ZKE_eep.bin"));
//OpenImageWorker w = new OpenImageWorker(new File("E:\\Downloads\\A-C_eep.bin"));
//OpenImageWorker w = new OpenImageWorker(new File("E:\\Downloads\\MFL_0000-1000.bin"));
OpenImageWorker w = new OpenImageWorker(new File("E:\\Downloads\\IKE_eep.bin"));
//OpenImageWorker w = new OpenImageWorker(new File("E:\\Downloads\\IKE_eep.bin"));
w.execute();
}

View File

@ -17,11 +17,18 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.romraider.xml;
package com.romraider.xml.ConversionLayer;
import java.io.File;
import java.io.StringWriter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
public interface ConversionLayer {
public abstract class ConversionLayer {
/*
* The actual conversion method. It receives the file and creates a DOM structure
* compatible to the default RR structure.
@ -31,7 +38,23 @@ 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);
//Can be used in the future to export a .xml file from a Document
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();
}
}

View File

@ -0,0 +1,22 @@
package com.romraider.xml.ConversionLayer;
import java.io.File;
public class ConversionLayerFactory {
private final static ConversionLayer[] convLayers = {new BMWCodingConversionLayer()};
//Check if its an .xml file
public static boolean requiresConversionLayer(File f) {
return !f.getName().matches("^.*\\.(xml|XML)$");
}
public static ConversionLayer getConversionLayerForFile(File f) {
for(ConversionLayer l: convLayers) {
if(l.isFileSupported(f)) {
return l;
}
}
return null;
}
}

View File

@ -17,7 +17,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.romraider.xml.BMWCodingConversion;
package com.romraider.xml.ConversionLayer;
import java.io.File;
@ -25,7 +25,7 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
class BMWRomManager{
class ConversionRomNodeManager{
int offsetAddress = 0;
Document doc;
@ -42,7 +42,7 @@ class BMWRomManager{
String bestIDFitDataTemp = "";
int lastPresetCount = 0;
BMWRomManager(int offsetAddress, Document doc, Node root){
ConversionRomNodeManager(int offsetAddress, Document doc, Node root){
this.doc = doc;
this.offsetAddress = offsetAddress;
this.romNode = doc.createElement("rom");
@ -52,8 +52,7 @@ class BMWRomManager{
public Element getRomNode() {
return romNode;
}
public Element createTable(String name, String category, String storageType, String endian,
int storageAddress, int byteCount, byte[] mask) {
@ -66,9 +65,12 @@ class BMWRomManager{
}
lastPresetCount = 0;
int divisor = 1;
if(storageType.contains("int16")) divisor = 2;
else if (storageType.contains("int32")) divisor = 4;
if(byteCount == 1) storageType="uint8";
int sizey = storageType.equalsIgnoreCase("uint8") ? byteCount : byteCount/2;
int sizey = byteCount / divisor;
Element table = doc.createElement("table");
table.setAttribute("name", name);
@ -103,6 +105,9 @@ class BMWRomManager{
data = data.replace(" ", "");
//Try to find the best fitting preset that only appears once
//and has the longest memory footprint for best identification
if(lastPresetCount >= 1) {
bestIDFitDataTemp = "";
}
@ -121,16 +126,19 @@ class BMWRomManager{
}
}
public void calculateRomID(File f) {
public void calculateRomID(File f, String make) {
Element romIDNode = doc.createElement("romid");
//romNode.setTextContent("Test");
Node idAddress = doc.createElement("internalidaddress");
//idAddress.setTextContent("0x" + Integer.toHexString(bestIDFitAddress));
idAddress.setTextContent("-1");
idAddress.setTextContent("0x" + Integer.toHexString(bestIDFitAddress));
Node idString = doc.createElement("internalidstring");
//idString.setTextContent("0x" + bestIDFitData.replace(" ", ""));
idString.setTextContent("force");
idString.setTextContent("0x" + bestIDFitData.replace(" ", ""));
//This can be used to force a definition file for a bin
//idString.setTextContent("force");
//idAddress.setTextContent("-1");
Node ramoffset = doc.createElement("noramoffset");
//Set filesize based on largest address and round up to a power of 2
@ -138,8 +146,8 @@ class BMWRomManager{
String fileS = ((int)Math.pow(2, 32 - Integer.numberOfLeadingZeros(lastStorageAddress - 1)) + "b");
fileSize.setTextContent(fileS);
Node make = doc.createElement("make");
make.setTextContent("BMW");
Node makeN = doc.createElement("make");
makeN.setTextContent(make);
Node model = doc.createElement("model");
model.setTextContent(f.getParentFile().getName());
@ -151,7 +159,7 @@ class BMWRomManager{
Node subModel = doc.createElement("submodel");
subModel.setTextContent(f.getName());
romIDNode.appendChild(make);
romIDNode.appendChild(makeN);
romIDNode.appendChild(ecuID);
romIDNode.appendChild(model);
romIDNode.appendChild(subModel);
@ -163,7 +171,6 @@ class BMWRomManager{
romIDNode.appendChild(doc.createElement("market"));
romIDNode.appendChild(doc.createElement("transmission"));
romIDNode.appendChild(doc.createElement("xmlid"));
romNode.appendChild(romIDNode);
romNode.appendChild(romIDNode);
}
}

View File

@ -23,18 +23,12 @@ 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;
@ -286,20 +280,5 @@ 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();
}
}