-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
190 lines (152 loc) · 6.43 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
function listGames() {
const gamesDir = path.resolve(__dirname, 'games');
const imgDir = path.resolve(__dirname, 'img');
if (!fs.existsSync(imgDir)) {
fs.mkdirSync(imgDir);
}
fs.readdir(gamesDir, (err, files) => {
if(err) {
console.error('Erro ao ler a pasta de jogos:', err);
return;
}
const games = files.filter(file => file.endsWith('.swf'));
const gamesListElement = document.getElementById('games-list');
gamesListElement.innerHTML = '';
games.forEach(game => {
const gameBaseName = game.replace('.swf', '');
const coverPathPng = path.join(imgDir, `${gameBaseName}.png`);
const coverPathJpg = path.join(imgDir, `${gameBaseName}.jpg`);
let coverPath = null;
if (fs.existsSync(coverPathPng)) {
coverPath = coverPathPng;
} else if (fs.existsSync(coverPathJpg)) {
coverPath = coverPathJpg;
}
const cardHTML = `
<div class="col-md-3 col-sm-4 col-6 game-card">
<div class="card shadow-sm border-0 m-1">
<div class="game-image-container">
<img src="${coverPath ? 'img/' + path.basename(coverPath) : 'assets/img/sem-jogo.png'}"
class="card-img-top game-image rounded" alt="${gameBaseName} cover" data-game="${game}">
<div class="game-name-container">
${gameBaseName}
</div>
</div>
<div class="position-absolute bottom-0 left-0 m-3 bg-dark text-white px-2 py-1 rounded change-cover" data-game="${game}">
<img src="assets/img/engrenagem.png" alt="Engrenagem" style="width: 20px; height: 20px;">
</div>
</div>
</div>
`;
gamesListElement.innerHTML += cardHTML;
});
const gameImages = document.querySelectorAll('.game-image');
gameImages.forEach(image => {
image.addEventListener('click', function() {
const gameName = image.getAttribute('data-game');
playGame(gameName);
});
});
const changeCoverButtons = document.querySelectorAll('.change-cover');
changeCoverButtons.forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
const gameName = button.getAttribute('data-game');
uploadImage(gameName);
});
});
});
}
function playGame(gameName) {
ipcRenderer.send('minimize');
const flashPlayerPath = path.resolve(__dirname, 'flashplayer.exe');
const gamePath = path.resolve(__dirname, 'games', gameName);
const windowsGamePath = gamePath.replace(/^\/mnt\/c\//, 'C:\\').replace(/\//g, '\\');
const command = `"${flashPlayerPath}" "${windowsGamePath}"`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error('Erro ao abrir o jogo:', error);
alert('Erro ao abrir o jogo. Verifique o Flash Player e o arquivo .swf.');
} else {
console.log('Jogo iniciado com sucesso!');
}
});
}
function uploadImage(gameName) {
const input = document.createElement('input');
input.type = 'file';
input.accept = 'image/png, image/jpeg';
input.onchange = () => {
const file = input.files[0];
if (!file) return;
const imgDir = path.resolve(__dirname, 'img');
const ext = path.extname(file.name);
const newFileName = gameName.replace('.swf', ext);
const newFilePath = path.join(imgDir, newFileName);
const existingPng = path.join(imgDir, gameName.replace('.swf', '.png'));
const existingJpg = path.join(imgDir, gameName.replace('.swf', '.jpg'));
[existingPng, existingJpg].forEach(existingPath => {
if(fs.existsSync(existingPath)) {
fs.unlinkSync(existingPath);
}
});
const reader = new FileReader();
reader.onnload = () => {
const buffer = Buffer.from(reader.result);
fs.writeFile(newFilePath, buffer, err => {
if(err) {
console.error('Erro ao salvar a nova capa do jogo:', err);
} else {
console.log('Nova capa salva com sucesso:', newFileName);
listGames();
}
});
};
reader.onerror = err => {
console.error('Erro ao ler o arquivo:', err);
alert('Erro ao processar o arquivo. Tente novamente.');
};
reader.readAsArrayBuffer(file);
};
input.click();
}
document.getElementById('add-game-btn').addEventListener('click', () => {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.swf';
input.onchange = () => {
const file = input.files[0];
if (!file) return;
const gamesDir = path.resolve(__dirname, 'games');
if(!fs.existsSync(gamesDir)) {
fs.mkdirSync(gamesDir);
}
const newGamePath = path.join(gamesDir, file.name);
const reader = new FileReader();
reader.onnload = () => {
const buffer = Buffer.from(reader.result);
fs.writeFile(newGamePath, buffer, (err) => {
if(err) {
console.error('Erro ao mover o arquivo:', err);
} else {
console.log('Jogo adicionado com sucesso!');
listGames();
}
});
};
reader.onerror = err => {
console.error('Erro ao ler o arquivo:', err);
alert('Erro ao processar o arquivo. Tente novamente.');
};
reader.readAsArrayBuffer(file);
};
input.click();
});
listGames();
const { ipcRenderer } = require('electron');
document.getElementById('minimize-btn').addEventListener('click', () => {ipcRenderer.send('minimize');});
document.getElementById('maximize-btn').addEventListener('click', () => {ipcRenderer.send('maximize');});
document.getElementById('close-btn').addEventListener('click', () => {ipcRenderer.send('close');});