Skip to content

Commit df70ec7

Browse files
author
Simeon Kummer
committed
add resourcepack download.
1 parent 757e174 commit df70ec7

File tree

2 files changed

+145
-1
lines changed

2 files changed

+145
-1
lines changed

src/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ ipcMain.handle('launch-minecraft', async (event, { uuid, name }) => {
186186

187187
try {
188188
await minecraftStarter.downloadMods();
189+
await minecraftStarter.downloadResourcepack();
190+
await minecraftStarter.configureResourcepack();
189191
// Clear the status message after mods have been downloaded
190192
mainWindow.webContents.send('minecraft-status', { message: null });
191193
} catch (error) {

src/minecraftStarter.js

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ const { spawn } = require('child_process');
55
const axios = require('axios');
66
const extract = require('extract-zip');
77

8+
async function fileExists(path) {
9+
try {
10+
await access(path, constants.F_OK);
11+
return true; // Datei existiert
12+
} catch {
13+
return false; // Datei existiert nicht
14+
}
15+
}
16+
817
class MinecraftStarter {
918
constructor() {
1019
this.INSTALL_DIR = this.getAppDataDirectory() + path.sep + 'BlitzClient' + path.sep + 'client-installer';
@@ -158,7 +167,140 @@ class MinecraftStarter {
158167
})
159168
}
160169

161-
run(uuid, name) {
170+
async downloadResourcepack() {
171+
return new Promise(async (resolve, reject) => {
172+
const resourcepackUrl = "https://blitzclient.netlify.app/resourcepack.zip";
173+
const parentDir = path.dirname(this.INSTALL_DIR);
174+
console.log('Parent directory:', parentDir);
175+
176+
const resourcepacksDir = path.join(parentDir, 'minecraft/resourcepacks');
177+
const resourcepackPath = path.join(resourcepacksDir, 'resourcepack.zip');
178+
179+
console.log('Download info:', {
180+
url: resourcepackUrl,
181+
parentDir: parentDir,
182+
resourcepacksDir: resourcepacksDir,
183+
resourcepackPath: resourcepackPath
184+
});
185+
186+
try {
187+
// Check if resourcepack already exists
188+
if (fs.existsSync(resourcepackPath)) {
189+
console.log('Resourcepack already exists, skipping download');
190+
resolve(true);
191+
return;
192+
}
193+
194+
fs.mkdirSync(resourcepacksDir, { recursive: true });
195+
console.log('Created resourcepacks directory:', resourcepacksDir);
196+
197+
// Download Resourcepack
198+
console.log('Starting download from:', resourcepackUrl);
199+
const resourcepackResponse = await axios({
200+
method: 'GET',
201+
url: resourcepackUrl,
202+
responseType: 'arraybuffer',
203+
headers: {
204+
'User-Agent': 'Node.js downloader'
205+
}
206+
});
207+
208+
console.log('Download completed, response size:', resourcepackResponse.data.length);
209+
fs.writeFileSync(resourcepackPath, resourcepackResponse.data);
210+
console.log('Resourcepack saved to:', resourcepackPath);
211+
212+
// Verify the file was written
213+
if (fs.existsSync(resourcepackPath)) {
214+
const stats = fs.statSync(resourcepackPath);
215+
console.log('Resourcepack file size:', stats.size, 'bytes');
216+
}
217+
218+
console.log('Resourcepack downloaded successfully');
219+
resolve(true);
220+
} catch (error) {
221+
console.error('Error downloading resourcepack:', error);
222+
reject(error);
223+
}
224+
});
225+
}
226+
227+
async configureResourcepack() {
228+
try {
229+
const parentDir = path.dirname(this.INSTALL_DIR);
230+
const optionsPath = path.join(parentDir, 'minecraft/options.txt');
231+
const resourcepacksDir = path.join(parentDir, 'minecraft/resourcepacks');
232+
const resourcepackPath = path.join(resourcepacksDir, 'resourcepack.zip');
233+
234+
console.log('Configure resourcepack info:', {
235+
parentDir: parentDir,
236+
optionsPath: optionsPath,
237+
resourcepackPath: resourcepackPath,
238+
resourcepackExists: fs.existsSync(resourcepackPath),
239+
optionsExists: fs.existsSync(optionsPath)
240+
});
241+
242+
// Check if resourcepack exists
243+
if (!(await fileExists(resourcepackPath))) {
244+
console.log('Resourcepack not found, skipping configuration');
245+
return false;
246+
}
247+
248+
let optionsContent = '';
249+
let resourcePacksLine = 'resourcePacks:["fabric","file/resourcepack.zip"]';
250+
251+
// Read existing options.txt if it exists
252+
if (await fileExists(optionsPath)) {
253+
optionsContent = fs.readFileSync(optionsPath, 'utf8');
254+
console.log('Existing options.txt content length:', optionsContent.length);
255+
256+
// Check if resourcePacks line already exists
257+
const lines = optionsContent.split('\n');
258+
let resourcePackLineIndex = -1;
259+
260+
for (let i = 0; i < lines.length; i++) {
261+
if (lines[i].startsWith('resourcePacks:')) {
262+
resourcePackLineIndex = i;
263+
console.log('Found existing resourcePacks line at index', i, ':', lines[i]);
264+
break;
265+
}
266+
}
267+
268+
if (resourcePackLineIndex !== -1) {
269+
// Update existing resourcePacks line
270+
const currentLine = lines[resourcePackLineIndex];
271+
// Parse the current resourcePacks array and add our pack if not already present
272+
if (!currentLine.includes('file/resourcepack.zip')) {
273+
// Simple approach: replace the closing bracket with our pack
274+
const updatedLine = currentLine.replace(/]$/, ',"file/resourcepack.zip"]');
275+
lines[resourcePackLineIndex] = updatedLine;
276+
console.log('Updated resourcePacks line to:', updatedLine);
277+
} else {
278+
console.log('Resourcepack already configured in options.txt');
279+
}
280+
optionsContent = lines.join('\n');
281+
} else {
282+
// Add resourcePacks line
283+
console.log('Adding new resourcePacks line');
284+
optionsContent += '\n' + resourcePacksLine + '\n';
285+
}
286+
} else {
287+
// Create new options.txt with resourcepack
288+
console.log('Creating new options.txt with resourcepack');
289+
optionsContent = resourcePacksLine + '\n';
290+
}
291+
292+
// Write the updated options.txt
293+
console.log('Writing options.txt to:', optionsPath);
294+
fs.writeFileSync(optionsPath, optionsContent);
295+
console.log('Resourcepack configured in options.txt successfully');
296+
return true;
297+
} catch (error) {
298+
console.error('Error configuring resourcepack:', error);
299+
return false;
300+
}
301+
}
302+
303+
async run(uuid, name) {
162304
try {
163305
const installDir = path.resolve(this.INSTALL_DIR);
164306
const parentDir = path.dirname(installDir);

0 commit comments

Comments
 (0)