-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
176 lines (161 loc) · 8.03 KB
/
main.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
const { app, BrowserWindow, ipcMain, shell } = require('electron')
if (require('electron-squirrel-startup')) return app.quit();
const fs = require('fs')
const path = require('path')
let userlang = 'en'
const createWindow = () => {
const win = new BrowserWindow({
minWidth: 250,
minHeight: 250,
width: 500,
height: 250,
backgroundColor: '#000',
titleBarStyle: 'hidden', /* hidden = cache la barre des titres */
// transparent: true, /* true = rend le fond transparent */
//movable: true,
//focusable: true,
opacity: 0.7,
icon: './cct2.png',
//alwaysOnTop: true,
fullScreenable: false,
movable: true,
hasShadow: false,
/*
titleBarOverlay: {
color: '#000',
symbolColor: '#95D5A8',
height: 10,
width: 10
}, //true, // true = default
*/
titleBarOverlay: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
sandbox: false,
devTools: true,
preload: path.join(__dirname, 'preload.js'),
nativeWindowOpen: true,
},
})
win.setAlwaysOnTop(true, 'normal')
win.loadFile('index.html')
win.webContents.send('fromMain', `Initialization ...`);
ipcMain.handle('exit', () => {
console.log('received exit from main')
win.close()
})
ipcMain.handle('switchToEn', () => {
userlang = 'en'
})
ipcMain.handle('switchToFr', () => {
userlang = 'fr'
})
ipcMain.handle('switchToEs', () => {
userlang = 'es'
})
ipcMain.handle('switchToIt', () => {
userlang = 'it'
})
ipcMain.handle('switchToRu', () => {
userlang = 'ru'
})
ipcMain.handle('switchToDe', () => {
userlang = 'de'
})
ipcMain.handle('switchToTr', () => {
userlang = 'tr'
})
ipcMain.handle('support', () => {
shell.openExternal('https://discord.gg/cknUdH94Cn', {
activate: true,
})
})
ipcMain.on("toMain", (event, args) => {
console.log(args)
/*
switch () {
case value:
break;
default:
break;
}*/
});
ipcMain.handle('init', () => {
win.webContents.send('fromMain', `Searching for /Logs in your %Document% ...`);
fs.readdir(`C:${process.env.HOMEPATH}/Documents/My games/Crossout/Logs`, (error, folders) => {
if (error) { console.error(error); return win.webContents.send('fromMain', `Error encountered...`); }
win.webContents.send('fromMain', `Found :||green`)
win.webContents.send('fromMain', `C:${process.env.HOMEPATH}/Documents/My games/Crossout/Logs||green`)
win.webContents.send('fromMain', `Searching for the last child in this folder...`)
fs.readdir(`C:${process.env.HOMEPATH}/Documents/My games/Crossout/Logs/${folders[(folders.length - 1)]}`, async (error, list) => {
if (error) { console.error(error); return win.webContents.send('fromMain', `Error encountered !`); }
win.webContents.send('fromMain', `Found :||green`)
win.webContents.send('fromMain', `${folders[(folders.length - 1)]}||green`)
win.webContents.send('fromMain', `Searching for a chat.log file in this folder...`)
if (!list.find(file => file === 'chat.log')) { return win.webContents.send('fromMain', `chat.log not found inside ${folders[(folders.length - 1)]}. Please, write something in the in-game chat then restart the app ^^`) }
win.webContents.send('fromMain', `CLEAN_IT`)
win.webContents.send('fromMain', `This project is open-source: https://github.com/JimmyGaubert/CCT2`)
win.webContents.send('fromMain', `Join the support server to be kept informed of updates :p`)
win.webContents.send('fromMain', `File found: /${folders[(folders.length - 1)]}/chat.log ||green`)
win.webContents.send('fromMain', `Starting to read the changes in chat.log :)||green`)
let linesArray = []
setInterval(() => {
fs.readFile(`C:${process.env.HOMEPATH}/Documents/My games/Crossout/Logs/${folders[(folders.length - 1)]}/chat.log`, (error, filedata) => {
if (error) { return console.error(error); }
});
}, 200);
fs.watch(`C:${process.env.HOMEPATH}/Documents/My games/Crossout/Logs/${folders[(folders.length - 1)]}/chat.log`, (eventType, filename) => {
if (eventType === 'change') {
fs.readFile(`C:${process.env.HOMEPATH}/Documents/My games/Crossout/Logs/${folders[(folders.length - 1)]}/chat.log`, 'utf8', (err, data) => {
const lines = data.split(/\r?\n/);
const lastline = lines[lines.length - 2];
if (!(/\]/).test(lastline)) return;
if (!linesArray.find(e => e == lastline)) {
linesArray.push(lastline);
setTimeout(() => {
for (let i = 0; i < linesArray.length; i++) {
if (linesArray[i] === lastline) { linesArray.splice(i, 1) }
}
}, 500);
const logstimer = lastline.slice(0, 5);
let playername;
((lastline.split('[')[1].split(' #')[0].trim().length) % 2 == 0) ? playername = lastline.split('[')[1].split(' #')[0].padEnd(25, ' ') : playername = lastline.split('[')[1].split(' #')[0].padEnd(16, ' ') + " ";
const translate = require('@iamtraction/google-translate');
translate(lastline.split('] ')[1].replace('#^#^', '^^'), { to: `${userlang}` }).then(res => {
async function Send(time, type, name, text, color) {
win.webContents.send('fromMain', `${time} ${type} ${name}: ${text} ||${color}`);
}
if ((/PRIVATE From/).test(lastline)) {
Send(logstimer, 'PRIVATE FROM', playername, res.text, 'purple')
} else if ((/PRIVATE To/).test(lastline)) {
Send(logstimer, 'PRIVATE TO', playername, res.text, 'purple')
} else if ((/team_/).test(lastline)) {
Send(logstimer, 'TEAM', playername, res.text, 'cyan')
} else if ((/clan_/).test(lastline)) {
Send(logstimer, 'CLAN', playername, res.text, 'green')
} else if ((/party_/).test(lastline) || (/custom_game/).test(lastline)) {
Send(logstimer, 'PARTY', playername, res.text, 'blue')
} else {
Send(logstimer, 'GENERAL', playername, res.text, 'white')
}
}).catch(err => {
console.error(err);
});
}
})
};
});
});
});
})
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})