Skip to content

Commit 6601004

Browse files
Implement mod downloading functionality and enhance dependency handling in mod dashboard
1 parent 9d0881e commit 6601004

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

ui/chrome/moddashboard/index.html

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,39 @@
88
<link rel="stylesheet" href="../css/main.css">
99
<script>
1010
async function installMod(modId) {
11-
const modrinthUrl = "https://api.modrinth.com/v2/project/" + modId + "/version?loaders=[\"neoforge\"]&game_versions=[\"1.21.1\"]"
12-
const modrinthResponse = await fetch(modrinthUrl)
13-
const modrinthData = await modrinthResponse.json()
14-
let latestversion = modrinthData[0]
15-
const versionData = await fetch("https://api.modrinth.com/v2/version/" + latestversion.id)
16-
const versionJson = await versionData.json()
17-
console.log(versionJson)
11+
let depsdownloaded = []
12+
async function downloadMod(id) {
13+
const modrinthUrl = "https://api.modrinth.com/v2/project/" + id + "/version?loaders=[\"neoforge\"]&game_versions=[\"1.21.1\"]"
14+
const modrinthResponse = await fetch(modrinthUrl)
15+
const modrinthData = await modrinthResponse.json()
16+
let latestversion = modrinthData[0]
17+
const versionData = await fetch("https://api.modrinth.com/v2/version/" + latestversion.id)
18+
const versionJson = await versionData.json()
19+
const primaryFile = versionJson.files.find(file => file.primary)
20+
const downloadUrl = primaryFile.url
21+
window.mcAPI.downloadToModsFolder(downloadUrl)
22+
return versionJson
23+
}
24+
async function loopThroughDependencies(mod) {
25+
const dependencies = mod.dependencies
26+
for (const dependency of dependencies) {
27+
console.log(dependency)
28+
if (!depsdownloaded.includes(dependency.project_id)) {
29+
depsdownloaded.push(dependency.project_id)
30+
const depMod = await downloadMod(dependency.project_id)
31+
await loopThroughDependencies(depMod)
32+
}
33+
}
34+
}
35+
const mainMod = await downloadMod(modId)
36+
loopThroughDependencies(mainMod)
1837
}
1938
</script>
2039
</head>
2140

2241
<body>
2342
<header>
43+
<a href="../index.html">Back</a>
2444
<h1>Mod Dashboard</h1>
2545
</header>
2646
<main>

ui/externalScripts/main_preload.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ const {ipcRenderer,contextBridge} = require('electron');
33
contextBridge.exposeInMainWorld('mcAPI', {
44
launch: () => {
55
ipcRenderer.send('launch');
6+
},
7+
downloadToModsFolder: (url) => {
8+
ipcRenderer.send('downloadToModsFolder', url);
69
}
710
})

ui_index.cjs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,49 @@ app.whenReady().then(() => {
3333
ipcMain.on('launch', (event, arg) => {
3434
let consoleWin = createConsole();
3535
const process = spawn('node', [mainIndex, "--ui"]);
36-
36+
3737
process.stdout.on('data', (data) => {
3838
consoleWin.webContents.send('msg', String(data)); // Send to renderer
3939
console.log(`stdout: ${data}`);
4040
});
41-
41+
4242
process.stderr.on('data', (data) => {
4343
consoleWin.webContents.send('error', String(data)); // Send to renderer
4444
console.error(`stderr: ${data}`);
4545
});
46-
46+
4747
process.on('close', (code) => {
4848
console.log(`child process exited with code ${code}`);
4949
});
50-
50+
5151
process.on('error', (error) => {
5252
console.error(`Error: ${error}`);
5353
});
54-
54+
5555
process.on('exit', (code) => {
5656
console.log(`Process exited with code: ${code}`);
5757
event.reply('process-exit', code);
5858
consoleWin.close();
5959
consoleWin = null;
6060
});
6161
});
62+
ipcMain.on("downloadToModsFolder", (event, url) => {
63+
fetch(url)
64+
.then(response => {
65+
if (!response.ok) {
66+
throw new Error('Network response was not ok');
67+
}
68+
return response.arrayBuffer();
69+
})
70+
.then(buffer => {
71+
const modsFolder = path.join(__dirname, ".minecraft", 'mods');
72+
if (!fs.existsSync(modsFolder)) {
73+
fs.mkdirSync(modsFolder);
74+
}
75+
const filePath = path.join(modsFolder, decodeURIComponent(path.basename(url)));
76+
fs.writeFileSync(filePath, Buffer.from(buffer));
77+
event.reply('download-complete', filePath);
78+
})
79+
});
6280
createWindow();
6381
});

0 commit comments

Comments
 (0)