electric-games/renderer.js

169 lines
6.2 KiB
JavaScript
Raw Permalink Normal View History

2024-12-29 22:49:41 +00:00
const { ipcRenderer } = require('electron');
const path = require('path');
// Declare variables for selected Wine binary and game executable
let winePath = null;
let selectedGameExe = null;
// Tab switching logic
document.getElementById('downloadsTab').addEventListener('click', () => {
switchTab('downloads');
});
document.getElementById('libraryTab').addEventListener('click', () => {
switchTab('library');
});
function switchTab(tabName) {
const tabs = document.querySelectorAll('.sidebar-button');
const sections = document.querySelectorAll('.section');
// Reset all tabs and sections
tabs.forEach(tab => tab.classList.remove('active'));
sections.forEach(section => section.classList.remove('active'));
// Activate the selected tab and section
if (tabName === 'downloads') {
2024-12-29 22:56:38 +00:00
document.getElementById('libraryTab').classList.remove('active');
document.getElementById('librarySection').classList.remove('active');
2024-12-29 22:49:41 +00:00
document.getElementById('downloadsTab').classList.add('active');
document.getElementById('downloadsSection').classList.add('active');
} else if (tabName === 'library') {
2024-12-29 22:56:38 +00:00
document.getElementById('downloadsTab').classList.remove('active');
document.getElementById('downloadsSection').classList.remove('active');
2024-12-29 22:49:41 +00:00
document.getElementById('libraryTab').classList.add('active');
document.getElementById('librarySection').classList.add('active');
loadLibrary(); // Load the library when switching to this tab
}
}
// Default to Downloads tab active
switchTab('downloads');
// Back and Forward buttons for webview navigation
const gameBrowser = document.getElementById('gameBrowser');
document.getElementById('backButton').addEventListener('click', () => {
gameBrowser.goBack();
});
document.getElementById('forwardButton').addEventListener('click', () => {
gameBrowser.goForward();
});
// Listen for a "download" request from the webview
gameBrowser.addEventListener('did-start-navigation', (event) => {
const downloadUrl = event.url;
if (downloadUrl && downloadUrl.endsWith('.zip')) {
// Trigger download automatically when a .zip file is detected
handleDownload(downloadUrl);
}
});
async function handleDownload(downloadUrl) {
try {
// Prompt the user to choose a library folder
const libraryFolder = await ipcRenderer.invoke('select-library-folder');
if (!libraryFolder) return;
const fileName = path.basename(downloadUrl); // Use the URL to get the file name
// Download and extract the ZIP file
const result = await ipcRenderer.invoke('download-and-extract', downloadUrl, libraryFolder, fileName);
if (result.success) {
console.log('Game downloaded and extracted successfully!');
} else {
console.error('Error downloading or extracting the game:', result.error);
}
} catch (error) {
console.error('Error handling download:', error);
}
}
// Load the Library content (list of .exe files)
async function loadLibrary() {
const libraryFolder = await ipcRenderer.invoke('select-library-folder');
if (libraryFolder) {
const gameList = await ipcRenderer.invoke('scan-library', libraryFolder);
// Show the list of executables in the library
const gameListDiv = document.getElementById('gameList');
gameListDiv.innerHTML = ''; // Clear previous content
if (gameList.length === 0) {
gameListDiv.innerHTML = 'No games found in the library.';
} else {
gameList.forEach(game => {
const gameItem = document.createElement('div');
gameItem.className = 'game-item';
gameItem.textContent = game; // Show the game path or name
gameListDiv.appendChild(gameItem);
});
}
}
}
// Select Wine binary
document.getElementById('selectWineButton').addEventListener('click', async () => {
winePath = await ipcRenderer.invoke('select-wine-binary');
if (winePath) {
document.getElementById('wineStatus').textContent = `Wine Binary Selected: ${winePath}`;
} else {
document.getElementById('wineStatus').textContent = 'No Wine Binary Selected';
}
});
// Select Library Folder and list `.exe` files
document.getElementById('selectLibraryFolder').addEventListener('click', async () => {
const result = await ipcRenderer.invoke('select-library-folder');
if (result && result.libraryFolderPath) {
document.getElementById('wineStatus').textContent = `Library Folder Selected: ${result.libraryFolderPath}`;
displayGameList(result.exeFiles);
} else {
document.getElementById('wineStatus').textContent = 'No Library Folder Selected';
}
});
// Display the list of `.exe` files
function displayGameList(gameList) {
const gameListContainer = document.getElementById('gameList');
gameListContainer.innerHTML = ''; // Clear the existing list
if (gameList.length === 0) {
gameListContainer.innerHTML = 'No games found in the selected library.';
return;
}
gameList.forEach((gameExe) => {
const gameButton = document.createElement('button');
gameButton.textContent = path.basename(gameExe);
gameButton.addEventListener('click', () => {
selectedGameExe = gameExe;
document.getElementById('wineStatus').textContent = `Selected Game: ${path.basename(gameExe)}`;
});
gameListContainer.appendChild(gameButton);
});
}
// Run game using Wine
document.getElementById('runGameButton').addEventListener('click', async () => {
if (!winePath) {
document.getElementById('wineStatus').textContent = 'Please select a Wine binary first.';
return;
}
if (!selectedGameExe) {
document.getElementById('wineStatus').textContent = 'Please select a game to run.';
return;
}
// Run the game via Wine
const result = await ipcRenderer.invoke('run-game-with-wine', selectedGameExe);
if (result.success) {
document.getElementById('wineStatus').textContent = 'Game is running!';
} else {
document.getElementById('wineStatus').textContent = `Error: ${result.message}`;
}
});