2023-06-18 16:35:46 -07:00
|
|
|
package com.rusefi.tools.tune;
|
|
|
|
|
|
|
|
import com.opensr5.ini.DialogModel;
|
|
|
|
import com.opensr5.ini.IniFileModel;
|
|
|
|
import com.rusefi.*;
|
|
|
|
import com.rusefi.core.preferences.storage.Node;
|
|
|
|
import com.rusefi.output.ConfigStructure;
|
|
|
|
import com.rusefi.tune.xml.Constant;
|
|
|
|
import com.rusefi.tune.xml.Msq;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.util.List;
|
2023-06-18 17:01:56 -07:00
|
|
|
import java.util.Objects;
|
2023-06-18 16:35:46 -07:00
|
|
|
|
|
|
|
import static com.rusefi.ConfigFieldImpl.unquote;
|
|
|
|
import static com.rusefi.tools.tune.WriteSimulatorConfiguration.INI_FILE_FOR_SIMULATOR;
|
|
|
|
|
|
|
|
public class TuneCanTool {
|
|
|
|
public static final String SRC_TEST_RESOURCES = "src/test/resources/";
|
|
|
|
private static final String FOLDER = "generated";
|
|
|
|
public static final String DEFAULT_TUNE = FOLDER + File.separator + "simulator_tune.xml";
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
IniFileModel ini = new IniFileModel().readIniFile(INI_FILE_FOR_SIMULATOR);
|
|
|
|
if (ini == null)
|
|
|
|
throw new IllegalStateException("Not found " + INI_FILE_FOR_SIMULATOR);
|
|
|
|
Msq simulatorDefaultTune = Msq.readTune(TuneCanTool.DEFAULT_TUNE);
|
|
|
|
|
|
|
|
RootHolder.ROOT = "../firmware/";
|
|
|
|
|
|
|
|
|
2023-06-18 18:21:36 -07:00
|
|
|
// 3A92 "La Mitsubicha"
|
|
|
|
// String fileName = "C:\\Users\\Dell2019\\Downloads\\1258.msq";
|
|
|
|
// Scion 1NZ-FE "xB Chinita auto"
|
|
|
|
// String fileName = "C:\\Users\\Dell2019\\Downloads\\1448.msq";
|
|
|
|
// 4g93
|
2023-06-18 20:02:38 -07:00
|
|
|
//String fileName = "C:\\Users\\Dell2019\\Downloads\\1425.msq";
|
|
|
|
// mtmotorsport BMW https://rusefi.com/online/view.php?msq=1479
|
|
|
|
String fileName = "C:\\Users\\Dell2019\\Downloads\\1479.msq";
|
|
|
|
|
2023-06-18 16:35:46 -07:00
|
|
|
|
|
|
|
Msq custom = Msq.readTune(fileName);
|
|
|
|
|
|
|
|
StringBuilder sb = TuneCanTool.getTunePatch2(simulatorDefaultTune, custom, ini);
|
|
|
|
|
|
|
|
System.out.println(sb);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isHardwareEnum(String type) {
|
|
|
|
switch (type) {
|
|
|
|
case "output_pin_e":
|
|
|
|
case "brain_input_pin_e":
|
|
|
|
case "adc_channel_e":
|
|
|
|
case "Gpio":
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Object simplerSpaces(String value) {
|
|
|
|
if (value == null)
|
|
|
|
return value;
|
|
|
|
return value.replaceAll("\\s+", " ").trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
@NotNull
|
2023-06-18 17:01:56 -07:00
|
|
|
public static StringBuilder getTunePatch2(Msq defaultTune, Msq customTune, IniFileModel ini) throws IOException {
|
2023-06-18 16:35:46 -07:00
|
|
|
List<String> options = Files.readAllLines(Paths.get(RootHolder.ROOT + "../" + ConfigDefinition.CONFIG_PATH));
|
|
|
|
String[] totalArgs = options.toArray(new String[0]);
|
|
|
|
|
|
|
|
ReaderStateImpl state = new ReaderStateImpl();
|
|
|
|
ConfigDefinition.doJob(totalArgs, state);
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (DialogModel.Field f : ini.fieldsInUiOrder) {
|
|
|
|
String name = f.getKey();
|
2023-06-18 17:01:56 -07:00
|
|
|
Constant customValue = customTune.getConstantsAsMap().get(name);
|
|
|
|
Constant defaultValue = defaultTune.getConstantsAsMap().get(name);
|
2023-06-18 16:35:46 -07:00
|
|
|
if (defaultValue == null) {
|
|
|
|
// no longer present
|
|
|
|
continue;
|
|
|
|
}
|
2023-06-18 17:01:56 -07:00
|
|
|
Objects.requireNonNull(defaultValue.getValue(), "d value");
|
|
|
|
if (customValue == null) {
|
|
|
|
System.out.println("Skipping " + name + " TODO");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Objects.requireNonNull(customValue.getValue(), "c value");
|
2023-06-18 16:35:46 -07:00
|
|
|
|
|
|
|
boolean isSameValue = simplerSpaces(defaultValue.getValue()).equals(simplerSpaces(customValue.getValue()));
|
|
|
|
if (!isSameValue) {
|
|
|
|
ConfigStructure s = state.getStructures().get("engine_configuration_s");
|
|
|
|
System.out.println(s);
|
|
|
|
ConfigField cf = s.getTsFieldByName(name);
|
|
|
|
if (cf == null) {
|
|
|
|
System.out.println("Not found " + name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cf.getType().equals("boolean")) {
|
2023-06-18 18:21:36 -07:00
|
|
|
sb.append(TuneTools.getAssignmentCode(defaultValue, customValue.getName(), unquote(customValue.getValue())));
|
2023-06-18 16:35:46 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!Node.isNumeric(customValue.getValue())) {
|
|
|
|
// todo: smarter logic for enums
|
|
|
|
|
|
|
|
String type = cf.getType();
|
|
|
|
if (isHardwareEnum(type)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
EnumsReader.EnumState sourceCodeEnum = state.getEnumsReader().getEnums().get(type);
|
|
|
|
if (sourceCodeEnum == null) {
|
|
|
|
System.out.println("No info for " + type);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
String customEnum = state.getTsCustomLine().get(type);
|
|
|
|
|
|
|
|
int ordinal;
|
|
|
|
try {
|
|
|
|
ordinal = TuneTools.resolveEnumByName(customEnum, unquote(customValue.getValue()));
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
System.out.println("Looks like things were renamed: " + customValue.getValue() + " not found in " + customEnum);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println(cf + " " + sourceCodeEnum + " " + customEnum + " " + ordinal);
|
|
|
|
|
|
|
|
String sourceCodeValue = sourceCodeEnum.findByValue(ordinal);
|
2023-06-18 18:21:36 -07:00
|
|
|
sb.append(TuneTools.getAssignmentCode(defaultValue, customValue.getName(), sourceCodeValue));
|
2023-06-18 16:35:46 -07:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2023-06-18 18:21:36 -07:00
|
|
|
sb.append(TuneTools.getAssignmentCode(defaultValue, customValue.getName(), customValue.getValue()));
|
2023-06-18 16:35:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
}
|