rusefi/java_tools/configuration_definition_base/src/main/java/com/rusefi/output/GetConfigValueConsumer.java

190 lines
6.7 KiB
Java
Raw Normal View History

2021-12-11 17:53:51 -08:00
package com.rusefi.output;
2023-01-06 09:09:11 -08:00
import com.rusefi.ConfigField;
2023-01-06 08:55:59 -08:00
import com.rusefi.ReaderState;
import com.rusefi.parse.TypesHelper;
2023-01-16 19:47:06 -08:00
import com.rusefi.output.variables.VariableRecord;
2023-07-02 21:56:19 -07:00
import com.rusefi.util.LazyFile;
2021-12-14 13:04:18 -08:00
import org.jetbrains.annotations.NotNull;
2021-12-11 17:53:51 -08:00
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
2021-12-11 17:53:51 -08:00
2023-01-06 08:01:32 -08:00
import static com.rusefi.output.ConfigStructureImpl.ALIGNMENT_FILL_AT;
2021-12-11 17:53:51 -08:00
import static com.rusefi.output.DataLogConsumer.UNUSED;
2023-01-02 09:10:09 -08:00
/**
* Here we generate C++ code for https://github.com/rusefi/rusefi/wiki/Lua-Scripting#getcalibrationname
* @see GetOutputValueConsumer
2023-06-10 17:27:33 -07:00
* @see GetConfigValueConsumerTest
2023-01-02 09:10:09 -08:00
*/
2021-12-11 17:53:51 -08:00
@SuppressWarnings("StringConcatenationInsideStringBufferAppend")
2022-04-15 10:47:14 -07:00
public class GetConfigValueConsumer implements ConfigurationConsumer {
2021-12-14 15:25:48 -08:00
private static final String CONFIG_ENGINE_CONFIGURATION = "config->engineConfiguration.";
private static final String ENGINE_CONFIGURATION = "engineConfiguration.";
2023-06-10 17:12:45 -07:00
public static String getHeader(Class clazz) {
return "// generated by " + clazz.getSimpleName() + ".java\n" +
"#include \"pch.h\"\n" +
"#include \"value_lookup.h\"\n";
}
private static final String GET_METHOD_HEADER =
"float getConfigValueByName(const char *name) {\n";
2021-12-15 16:43:31 -08:00
static final String GET_METHOD_FOOTER = "\treturn EFI_ERROR_CODE;\n" + "}\n";
2023-06-27 11:49:21 -07:00
private static final String SET_METHOD_HEADER = "bool setConfigValueByName(const char *name, float value) {\n";
2021-12-14 15:25:48 -08:00
private static final String SET_METHOD_FOOTER = "}\n";
2023-01-16 19:47:06 -08:00
private final List<VariableRecord> variables = new ArrayList<>();
2021-12-15 16:43:31 -08:00
private final String outputFileName;
private final String mdOutputFileName;
2023-07-02 21:56:19 -07:00
private final LazyFile.LazyFileFactory lazyFileFactory;
2021-12-11 17:53:51 -08:00
private final StringBuilder mdContent = new StringBuilder();
public GetConfigValueConsumer() {
2023-07-02 21:56:19 -07:00
this(null, null, LazyFile.REAL);
}
2023-07-02 21:56:19 -07:00
public GetConfigValueConsumer(String outputFileName, String mdOutputFileName, LazyFile.LazyFileFactory lazyFileFactory) {
2021-12-15 16:43:31 -08:00
this.outputFileName = outputFileName;
this.mdOutputFileName = mdOutputFileName;
2023-07-02 21:56:19 -07:00
this.lazyFileFactory = lazyFileFactory;
2021-12-11 17:53:51 -08:00
}
2023-07-02 21:56:19 -07:00
public static void writeStringToFile(@Nullable String fileName, String content, LazyFile.LazyFileFactory lazyFileFactory) throws IOException {
2021-12-11 17:53:51 -08:00
if (fileName != null) {
2023-07-02 21:56:19 -07:00
LazyFile fw = lazyFileFactory.create(fileName);
2021-12-11 17:53:51 -08:00
fw.write(content);
fw.close();
}
}
@Override
2023-01-06 08:55:59 -08:00
public void handleEndStruct(ReaderState state, ConfigStructure structure) throws IOException {
2023-01-06 07:52:09 -08:00
if (state.isStackEmpty()) {
PerFieldWithStructuresIterator.Strategy strategy = new PerFieldWithStructuresIterator.Strategy() {
@Override
public String process(ReaderState state, ConfigField cf, String prefix) {
return processConfig(cf, prefix);
}
@Override
public String getArrayElementName(ConfigField cf) {
return cf.getOriginalArrayName();
}
};
2023-01-06 07:54:29 -08:00
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(state, structure.getTsFields(), "",
strategy, ".");
2021-12-12 19:42:46 -08:00
iterator.loop();
2021-12-11 17:53:51 -08:00
}
}
@Override
public void endFile() throws IOException {
2023-07-02 21:56:19 -07:00
writeStringToFile(outputFileName, getContent(), lazyFileFactory);
writeStringToFile(mdOutputFileName, getMdContent(), lazyFileFactory);
2021-12-11 17:53:51 -08:00
}
2023-01-06 09:09:11 -08:00
private String processConfig(ConfigField cf, String prefix) {
2021-12-11 17:53:51 -08:00
if (cf.getName().contains(UNUSED) || cf.getName().contains(ALIGNMENT_FILL_AT))
2021-12-12 19:42:46 -08:00
return "";
2021-12-12 20:17:38 -08:00
if (cf.isArray() || cf.isFromIterate() || cf.isDirective())
2021-12-12 19:42:46 -08:00
return "";
if (!TypesHelper.isPrimitive(cf.getTypeName()) && !TypesHelper.isBoolean(cf.getTypeName())) {
2021-12-13 15:57:22 -08:00
return "";
}
2021-12-11 17:53:51 -08:00
2021-12-13 16:12:54 -08:00
String userName = prefix + cf.getName();
if (userName.startsWith(ENGINE_CONFIGURATION))
userName = userName.substring(ENGINE_CONFIGURATION.length());
String javaName = "config->" + prefix;
if (javaName.startsWith(CONFIG_ENGINE_CONFIGURATION))
javaName = "engineConfiguration->" + javaName.substring(CONFIG_ENGINE_CONFIGURATION.length());
variables.add(new VariableRecord(userName, javaName + cf.getName(), cf.getTypeName(), null));
2021-12-14 13:04:18 -08:00
mdContent.append("### " + userName + "\n");
mdContent.append(cf.getComment() + "\n\n");
2021-12-14 13:04:18 -08:00
2021-12-12 19:42:46 -08:00
return "";
2021-12-11 17:53:51 -08:00
}
2021-12-14 15:25:48 -08:00
@NotNull
private String getAssignment(String cast, String value) {
return "\t{\n" + "\t\t" + value + " = " + cast +
2021-12-14 15:25:48 -08:00
"value;\n" +
2023-06-27 11:49:21 -07:00
"\t\treturn 1;\n\t}\n";
2021-12-14 15:25:48 -08:00
}
2021-12-14 13:04:18 -08:00
@NotNull
2021-12-15 16:43:31 -08:00
static String getCompareName(String userName) {
2021-12-14 13:04:18 -08:00
return "\tif (strEqualCaseInsensitive(name, \"" + userName + "\"))\n";
}
public String getHeaderAndGetter() {
2023-06-10 17:12:45 -07:00
return GetConfigValueConsumer.getHeader(getClass()) +
getCompleteGetterBody();
}
public String getMdContent() {
return mdContent.toString();
}
@NotNull
public String getCompleteGetterBody() {
StringBuilder switchBody = new StringBuilder();
2023-01-16 19:47:06 -08:00
StringBuilder getterBody = GetOutputValueConsumer.getGetters(switchBody, variables);
String fullSwitch = GetOutputValueConsumer.wrapSwitchStatement(switchBody);
return GET_METHOD_HEADER +
fullSwitch +
getterBody + GET_METHOD_FOOTER;
}
2021-12-14 13:04:18 -08:00
public String getSetterBody() {
StringBuilder switchBody = new StringBuilder();
StringBuilder setterBody = new StringBuilder();
HashMap<Integer, AtomicInteger> hashConflicts = GetOutputValueConsumer.getHashConflicts(variables);
2023-01-16 19:47:06 -08:00
for (VariableRecord pair : variables) {
2023-01-16 19:47:06 -08:00
String cast = TypesHelper.isFloat(pair.type) ? "" : "(int)";
2023-01-16 19:47:06 -08:00
int hash = HashUtil.hash(pair.getUserName());
String str = getAssignment(cast, pair.getFullName());
if (hashConflicts.get(hash).get() == 1) {
switchBody.append("\t\tcase " + hash + ":\n");
switchBody.append(str);
} else {
2023-01-16 19:47:06 -08:00
setterBody.append(getCompareName(pair.getUserName()));
setterBody.append(str);
}
}
String fullSwitch = GetOutputValueConsumer.wrapSwitchStatement(switchBody);
return fullSwitch + "\treturn 0;\n" + setterBody;
2021-12-14 13:04:18 -08:00
}
2021-12-11 17:53:51 -08:00
public String getContent() {
return getHeaderAndGetter()
2021-12-14 13:04:18 -08:00
+
SET_METHOD_HEADER + getSetterBody() + SET_METHOD_FOOTER
2021-12-14 13:04:18 -08:00
;
2021-12-11 17:53:51 -08:00
}
}