Enable auto cfg loading and running from arguments.

This commit is contained in:
2026-06-13 16:37:16 +02:00
parent 5f2f76a7d3
commit dd7100d1d5
4 changed files with 74 additions and 20 deletions
+53 -2
View File
@@ -120,13 +120,51 @@ function fallbackBrowseForFolder() {
picker.click();
}
function getConfigValue(content, key) {
const lines = content.toString().split('\n');
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#') || trimmed.startsWith('[')) {
continue;
}
const separator = trimmed.indexOf('=');
if (separator === -1) {
continue;
}
const param = trimmed.slice(0, separator).trim();
if (param === key) {
return trimmed.slice(separator + 1).trim();
}
}
return null;
}
function resolveConfigWorkPath(filename, content) {
const configuredWorkPath = getConfigValue(content, 'workPath');
if (!configuredWorkPath) {
return null;
}
if (configuredWorkPath.startsWith('./')) {
return path.resolve(path.dirname(filename), configuredWorkPath);
}
if (path.isAbsolute(configuredWorkPath)) {
return configuredWorkPath;
}
return null;
}
function loadConfigFile(filename, autoRun = false) {
console.log('Open file ' + filename);
fs.readFile(filename.toString(), function read(err, data) {
const configPath = Array.isArray(filename) ? filename[0] : filename.toString();
console.log('Open file ' + configPath);
fs.readFile(configPath, function read(err, data) {
if (err) {
throw err;
}
setValues(data);
const resolvedWorkPath = resolveConfigWorkPath(configPath, data);
if (resolvedWorkPath) {
setWorkFolder([resolvedWorkPath]);
}
if (autoRun) {
setTimeout(() => startSim(), 0);
}
@@ -215,3 +253,16 @@ function firstLoad() {
fallbackBrowseForFolder();
});
}
window.addEventListener('load', async function() {
try {
const startupOptions = await ipcRenderer.invoke('getStartupOptions');
if (startupOptions && startupOptions.configPath) {
setTimeout(() => {
loadConfigFile(startupOptions.configPath, !!startupOptions.autoRun);
}, 0);
}
} catch (err) {
console.log('Failed to read startup options:', err);
}
});