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;
|
2023-06-16 13:20:19 -07:00
|
|
|
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;
|
2022-12-02 20:48:21 -08:00
|
|
|
import java.util.ArrayList;
|
2022-12-03 21:05:20 -08:00
|
|
|
import java.util.HashMap;
|
2022-12-02 20:48:21 -08:00
|
|
|
import java.util.List;
|
2022-12-03 21:05:20 -08:00
|
|
|
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";
|
|
|
|
}
|
2022-05-15 14:56:39 -07:00
|
|
|
|
|
|
|
private static final String GET_METHOD_HEADER =
|
2022-12-03 21:46:07 -08:00
|
|
|
"float getConfigValueByName(const char *name) {\n";
|
2022-05-15 14:56:39 -07:00
|
|
|
|
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;
|
2022-12-20 08:42:34 -08:00
|
|
|
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
|
|
|
|
2022-12-20 08:42:34 -08:00
|
|
|
private final StringBuilder mdContent = new StringBuilder();
|
|
|
|
|
|
|
|
public GetConfigValueConsumer() {
|
2023-07-02 21:56:19 -07:00
|
|
|
this(null, null, LazyFile.REAL);
|
2022-12-20 08:42:34 -08:00
|
|
|
}
|
|
|
|
|
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;
|
2022-12-20 08:42:34 -08:00
|
|
|
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()) {
|
2023-10-07 08:50:45 -07:00
|
|
|
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(), "",
|
2023-10-07 08:50:45 -07:00
|
|
|
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 "";
|
2024-04-20 11:06:26 -07:00
|
|
|
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());
|
|
|
|
|
2024-04-20 11:06:26 -07:00
|
|
|
variables.add(new VariableRecord(userName, javaName + cf.getName(), cf.getTypeName(), null));
|
2021-12-14 13:04:18 -08:00
|
|
|
|
2022-12-20 08:42:34 -08:00
|
|
|
mdContent.append("### " + userName + "\n");
|
2023-01-02 09:52:33 -08:00
|
|
|
mdContent.append(cf.getComment() + "\n\n");
|
2022-12-20 08:42:34 -08:00
|
|
|
|
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
|
2022-12-03 21:05:20 -08:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2022-05-15 14:56:39 -07:00
|
|
|
public String getHeaderAndGetter() {
|
2023-06-10 17:12:45 -07:00
|
|
|
return GetConfigValueConsumer.getHeader(getClass()) +
|
2022-12-03 21:01:34 -08:00
|
|
|
getCompleteGetterBody();
|
2022-05-15 14:56:39 -07:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:42:34 -08:00
|
|
|
public String getMdContent() {
|
|
|
|
return mdContent.toString();
|
|
|
|
}
|
|
|
|
|
2022-05-15 14:56:39 -07:00
|
|
|
@NotNull
|
2022-12-03 21:01:34 -08:00
|
|
|
public String getCompleteGetterBody() {
|
2022-12-02 22:06:41 -08:00
|
|
|
StringBuilder switchBody = new StringBuilder();
|
|
|
|
|
2023-01-16 19:47:06 -08:00
|
|
|
StringBuilder getterBody = GetOutputValueConsumer.getGetters(switchBody, variables);
|
2022-12-02 22:06:41 -08:00
|
|
|
|
2023-06-17 19:16:33 -07:00
|
|
|
String fullSwitch = GetOutputValueConsumer.wrapSwitchStatement(switchBody);
|
2022-12-02 22:06:41 -08:00
|
|
|
|
|
|
|
return GET_METHOD_HEADER +
|
|
|
|
fullSwitch +
|
|
|
|
getterBody + GET_METHOD_FOOTER;
|
2022-05-15 14:56:39 -07:00
|
|
|
}
|
|
|
|
|
2021-12-14 13:04:18 -08:00
|
|
|
public String getSetterBody() {
|
2022-12-03 21:09:14 -08:00
|
|
|
StringBuilder switchBody = new StringBuilder();
|
|
|
|
|
2022-12-03 21:05:20 -08:00
|
|
|
StringBuilder setterBody = new StringBuilder();
|
2023-06-17 19:16:33 -07:00
|
|
|
HashMap<Integer, AtomicInteger> hashConflicts = GetOutputValueConsumer.getHashConflicts(variables);
|
2022-12-03 21:21:17 -08:00
|
|
|
|
2023-01-16 19:47:06 -08:00
|
|
|
for (VariableRecord pair : variables) {
|
2022-12-03 21:21:17 -08:00
|
|
|
|
2023-01-16 19:47:06 -08:00
|
|
|
String cast = TypesHelper.isFloat(pair.type) ? "" : "(int)";
|
2022-12-03 21:05:20 -08:00
|
|
|
|
|
|
|
|
2023-01-16 19:47:06 -08:00
|
|
|
int hash = HashUtil.hash(pair.getUserName());
|
|
|
|
String str = getAssignment(cast, pair.getFullName());
|
2022-12-03 21:09:14 -08:00
|
|
|
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()));
|
2022-12-03 21:09:14 -08:00
|
|
|
setterBody.append(str);
|
|
|
|
}
|
2022-12-03 21:05:20 -08:00
|
|
|
}
|
2022-12-03 21:01:34 -08:00
|
|
|
|
2023-06-17 19:16:33 -07:00
|
|
|
String fullSwitch = GetOutputValueConsumer.wrapSwitchStatement(switchBody);
|
2022-12-03 21:01:34 -08:00
|
|
|
|
2023-06-27 12:05:15 -07:00
|
|
|
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() {
|
2022-05-15 14:56:39 -07:00
|
|
|
return getHeaderAndGetter()
|
2021-12-14 13:04:18 -08:00
|
|
|
+
|
2022-12-03 21:01:34 -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
|
|
|
}
|
|
|
|
}
|