Skip to content

Commit f0acd72

Browse files
committed
Add Config Editor and JSON Visual Editor with configuration handling
1 parent 99fa999 commit f0acd72

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

ui/chrome/config/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<header>
2+
<h1>Config Editor</h1>
3+
<nav>
4+
<a href="launcherconfig.html">Launcher Config</a>
5+
<a href="userconfig.html">User Config</a>
6+
</nav>
7+
</header>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>JSON Visual Editor</title>
7+
<style>
8+
body {
9+
font-family: Arial, sans-serif;
10+
padding: 20px;
11+
}
12+
.json-editor {
13+
margin-top: 20px;
14+
}
15+
.json-entry {
16+
display: flex;
17+
align-items: center;
18+
margin-bottom: 10px;
19+
}
20+
.json-entry input {
21+
margin-left: 10px;
22+
padding: 5px;
23+
}
24+
</style>
25+
</head>
26+
<body>
27+
<h1>JSON Visual Editor</h1>
28+
<div id="json-editor" class="json-editor"></div>
29+
<button onclick="addEntry()">Add Entry</button>
30+
<button onclick="saveJson()">Save JSON</button>
31+
32+
<script type="module">
33+
const jsonEditor = document.getElementById('json-editor');
34+
console.log(typeof mcAPI.getConfig('launcher'))
35+
const jsonData = await mcAPI.getConfig('launcher') || {};
36+
console.log(jsonData)
37+
38+
window.addEntry = function(key = '', value = '') {
39+
const entryDiv = document.createElement('div');
40+
entryDiv.className = 'json-entry';
41+
entryDiv.innerHTML = `
42+
<input type="text" placeholder="Key" value="${key}" oninput="updateJson(this, 'key')">
43+
<input type="text" placeholder="Value" value="${value}" oninput="updateJson(this, 'value')">
44+
<button onclick="removeEntry(this)">Remove</button>
45+
`;
46+
jsonEditor.appendChild(entryDiv);
47+
}
48+
49+
window.updateJson = function (input, type) {
50+
const entryDiv = input.parentElement;
51+
const keyInput = entryDiv.children[0];
52+
const valueInput = entryDiv.children[1];
53+
const key = keyInput.value;
54+
const value = valueInput.value;
55+
56+
if (type === 'key') {
57+
delete jsonData[keyInput.dataset.oldKey];
58+
keyInput.dataset.oldKey = key;
59+
}
60+
61+
if (key) {
62+
jsonData[key] = value;
63+
}
64+
}
65+
66+
window.removeEntry = function (button) {
67+
const entryDiv = button.parentElement;
68+
const keyInput = entryDiv.children[0];
69+
delete jsonData[keyInput.value];
70+
jsonEditor.removeChild(entryDiv);
71+
}
72+
73+
window.saveJson = function saveJson() {
74+
const jsonString = JSON.stringify(jsonData, null, 4);
75+
console.log(jsonString);
76+
mcAPI.saveConfig('launcher', jsonString)
77+
.then(() => {
78+
alert('JSON saved successfully!');
79+
})
80+
.catch((error) => {
81+
console.error('Error saving JSON:', error);
82+
alert('Failed to save JSON.');
83+
});
84+
}
85+
86+
// Populate the editor with existing JSON data
87+
for (const [key, value] of Object.entries(jsonData)) {
88+
addEntry(key, value);
89+
}
90+
</script>
91+
</body>
92+
</html>
93+

ui/chrome/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ <h1>MC Launcher</h1>
1313
<main>
1414
<button id="execute-button">Run Minecraft</button>
1515
<a href="moddashboard/index.html">Mod Dashboard</a>
16+
<a href="config/index.html">Config Editor</a>
1617
</main>
1718
<script>
1819
document.getElementById('execute-button').addEventListener('click', function() {

ui/externalScripts/main_preload.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,17 @@ contextBridge.exposeInMainWorld('mcAPI', {
1313
deleteFromModsFolder: (path) => {
1414
ipcRenderer.send('deleteFromModsFolder', path);
1515
},
16+
/**
17+
* Retrieves the configuration for the client or launcher.
18+
*
19+
* @param {"client" || "launcher"} clientOrLauncher - 'client' or 'launcher'
20+
* @returns {Promise<Object>} - The configuration object
21+
*/
22+
getConfig: async (clientOrLauncher) => {
23+
return await ipcRenderer.invoke('getConfig', clientOrLauncher);
24+
},
25+
26+
saveConfig: async (clientOrLauncher, config) => {
27+
return await ipcRenderer.invoke('saveConfig', clientOrLauncher, config);
28+
}
1629
})

ui_index.cjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,21 @@ app.whenReady().then(() => {
189189
fs.rmSync(path, { force: true });
190190
});
191191

192+
ipcMain.handle('getConfig', async (event, clientOrLauncher) => {
193+
const configPath = path.join(__dirname, `${clientOrLauncher == "client" ? "config" : "launcher_config"}.json`);
194+
if (!fs.existsSync(configPath)) {
195+
throw new Error(`Config file not found: ${configPath}`);
196+
}
197+
const configData = fs.readFileSync(configPath);
198+
console.log(`Config data: ${configData}`);
199+
return JSON.parse(configData);
200+
});
201+
202+
ipcMain.handle('saveConfig', async (event, clientOrLauncher, config) => {
203+
const configPath = path.join(__dirname, `${clientOrLauncher == "client" ? "config" : "launcher_config"}.json`);
204+
fs.writeFileSync(configPath, config);
205+
});
206+
192207
// Open main window
193208
createWindow();
194209
});

0 commit comments

Comments
 (0)