9
9
* `./src/main.js` using webpack. This gives us some performance wins.
10
10
*/
11
11
import path from 'path' ;
12
- import { app , BrowserWindow , shell , ipcMain } from 'electron' ;
12
+ import fs from 'fs' ;
13
+ import { app , BrowserWindow , dialog , shell , ipcMain } from 'electron' ;
13
14
import { autoUpdater } from 'electron-updater' ;
14
15
import log from 'electron-log' ;
15
16
import MenuBuilder from './menu' ;
16
17
import { resolveHtmlPath } from './util' ;
17
18
19
+ import defaultConfig from './defaultConfig' ;
20
+
21
+ const HOME : string = process . env . HOME || "/" ;
22
+ const CONFIG_FILE : string = `${ HOME } /.dub-editor-config.json` ;
23
+
18
24
export default class AppUpdater {
19
25
constructor ( ) {
20
26
log . transports . file . level = 'info' ;
@@ -25,11 +31,18 @@ export default class AppUpdater {
25
31
26
32
let mainWindow : BrowserWindow | null = null ;
27
33
28
- ipcMain . on ( 'ipc-example' , async ( event , arg ) => {
29
- const msgTemplate = ( pingPong : string ) => `IPC test: ${ pingPong } ` ;
30
- console . log ( msgTemplate ( arg ) ) ;
31
- event . reply ( 'ipc-example' , msgTemplate ( 'pong' ) ) ;
32
- } ) ;
34
+ let config = defaultConfig ;
35
+ if ( process . platform === 'darwin' ) {
36
+ config . whatTheDubDirectory = "~/Library/Application Support/Steam/steamapps/common/WhatTheDub/WhatTheDub.app/Contents/Resources/Data" ;
37
+ config . rifftraxDirectory = "~/Library/Application Support/Steam/steamapps/common/RiffTraxTheGame/RiffTraxTheGame.app/Contents/Resources/Data" ;
38
+ config . isMac = true ;
39
+ }
40
+
41
+ if ( ! fs . existsSync ( CONFIG_FILE ) ) {
42
+ fs . writeFileSync ( CONFIG_FILE , Buffer . from ( JSON . stringify ( config , null , 5 ) ) ) ;
43
+ } else {
44
+ config = JSON . parse ( fs . readFileSync ( CONFIG_FILE , { } ) . toString ( ) ) ;
45
+ }
33
46
34
47
if ( process . env . NODE_ENV === 'production' ) {
35
48
const sourceMapSupport = require ( 'source-map-support' ) ;
@@ -71,8 +84,8 @@ const createWindow = async () => {
71
84
72
85
mainWindow = new BrowserWindow ( {
73
86
show : false ,
74
- width : 1024 ,
75
- height : 728 ,
87
+ width : 1920 ,
88
+ height : 1080 ,
76
89
icon : getAssetPath ( 'icon.png' ) ,
77
90
webPreferences : {
78
91
preload : app . isPackaged
@@ -135,3 +148,140 @@ app
135
148
} ) ;
136
149
} )
137
150
. catch ( console . log ) ;
151
+
152
+ // Bridged functionality
153
+
154
+ ipcMain . handle ( 'updateConfig' , ( event , newConfig ) => {
155
+ console . log ( "CONFIG: " + JSON . stringify ( newConfig ) ) ;
156
+ config = newConfig ;
157
+ fs . writeFileSync ( CONFIG_FILE , Buffer . from ( JSON . stringify ( config , null , 5 ) ) ) ;
158
+ return ;
159
+ } ) ;
160
+
161
+ ipcMain . handle ( 'getConfig' , ( ) => {
162
+ return config ;
163
+ } ) ;
164
+
165
+ ipcMain . handle ( 'getVideos' , ( event , game ) => {
166
+ let clipsDirectory = null ;
167
+ if ( game === "rifftrax" ) {
168
+ clipsDirectory = `${ config . rifftraxDirectory } /StreamingAssets/VideoClips` . replace ( "~" , HOME ) ;
169
+ } else if ( game === "whatthedub" ) {
170
+ clipsDirectory = `${ config . whatTheDubDirectory } /StreamingAssets/VideoClips` . replace ( "~" , HOME ) ;
171
+ } else {
172
+ return [ ] ;
173
+ }
174
+
175
+ const files = fs . readdirSync ( clipsDirectory ) ;
176
+ const fileObjects = files . filter ( file => file . endsWith ( ".mp4" ) || file . endsWith ( ".mp4.disabled" ) ) . map ( ( file ) => { return { _id : file . substring ( 0 , file . lastIndexOf ( ".mp4" ) ) , name : file . replace ( / _ / g, " " ) . substring ( 0 , file . lastIndexOf ( ".mp4" ) ) , game, disabled : file . endsWith ( ".disabled" ) } } ) ;
177
+ return fileObjects ;
178
+ } ) ;
179
+
180
+ ipcMain . handle ( 'getVideo' , ( event , { id, game} ) => {
181
+ console . log ( "Opening: " + id + " from game " + game ) ;
182
+
183
+ let directory = null ;
184
+ if ( game === "rifftrax" ) {
185
+ directory = config . rifftraxDirectory ;
186
+ } else if ( game === "whatthedub" ) {
187
+ directory = config . whatTheDubDirectory ;
188
+ } else {
189
+ return [ ] ;
190
+ }
191
+
192
+ const clipsDirectory = `${ directory } /StreamingAssets/VideoClips` . replace ( "~" , HOME ) ;
193
+ const subsDirectory = `${ directory } /StreamingAssets/Subtitles` . replace ( "~" , HOME ) ;
194
+ const videoFilePath = `${ clipsDirectory } /${ id } .mp4` ;
195
+ const subFilePath = `${ subsDirectory } /${ id } .srt` ;
196
+
197
+ const videoBase64 = fs . readFileSync ( videoFilePath , { encoding : 'base64' } ) ;
198
+ const subtitles = fs . readFileSync ( subFilePath , { encoding : 'base64' } ) ;
199
+
200
+ return {
201
+ name : id . replace ( / _ / g, " " ) ,
202
+ videoUrl : `data:video/mp4;base64,${ videoBase64 } ` ,
203
+ subtitles : [ ] ,
204
+ srtBase64 : subtitles
205
+ }
206
+ } ) ;
207
+
208
+ ipcMain . handle ( 'storeVideo' , ( event , { base64ByteStream, subtitles, title, clipNumber, game} ) => {
209
+ console . log ( `STORING ${ title } -${ clipNumber } for game ${ game } with subtitles ${ subtitles } ` ) ;
210
+
211
+ let directory = null ;
212
+ if ( game === "rifftrax" ) {
213
+ directory = config . rifftraxDirectory ;
214
+ } else if ( game === "whatthedub" ) {
215
+ directory = config . whatTheDubDirectory ;
216
+ } else {
217
+ return ;
218
+ }
219
+
220
+ let baseFileName = title . replace ( " " , "_" ) + `-Clip${ `${ clipNumber } ` . padStart ( 3 , "0" ) } ` ;
221
+
222
+ const clipsDirectory = `${ directory } /StreamingAssets/VideoClips` . replace ( "~" , HOME ) ;
223
+ const subsDirectory = `${ directory } /StreamingAssets/Subtitles` . replace ( "~" , HOME ) ;
224
+ const videoFilePath = `${ clipsDirectory } /_${ baseFileName } .mp4` ;
225
+ const subFilePath = `${ subsDirectory } /_${ baseFileName } .srt` ;
226
+
227
+ console . log ( "SAVING TO " + videoFilePath + "\n" + subFilePath ) ;
228
+
229
+ fs . writeFileSync ( videoFilePath , Buffer . from ( base64ByteStream , "base64" ) ) ;
230
+ fs . writeFileSync ( subFilePath , subtitles ) ;
231
+ } ) ;
232
+
233
+ ipcMain . handle ( 'deleteVideo' , ( event , { id, game} ) => {
234
+ console . log ( "DELETING " + id + " FOR GAME " + game ) ;
235
+
236
+ let directory = null ;
237
+ if ( game === "rifftrax" ) {
238
+ directory = config . rifftraxDirectory ;
239
+ } else if ( game === "whatthedub" ) {
240
+ directory = config . whatTheDubDirectory ;
241
+ } else {
242
+ return ;
243
+ }
244
+ const clipsDirectory = `${ directory } /StreamingAssets/VideoClips` . replace ( "~" , HOME ) ;
245
+ const subsDirectory = `${ directory } /StreamingAssets/Subtitles` . replace ( "~" , HOME ) ;
246
+ const videoFilePath = `${ clipsDirectory } /${ id } .mp4` ;
247
+ const subFilePath = `${ subsDirectory } /${ id } .srt` ;
248
+
249
+ console . log ( "DELETING " + videoFilePath + "\n" + subFilePath ) ;
250
+
251
+ fs . unlinkSync ( videoFilePath ) ;
252
+ fs . unlinkSync ( subFilePath ) ;
253
+ } ) ;
254
+
255
+ ipcMain . handle ( 'openDialog' , async ( ) => {
256
+ const response = await dialog . showOpenDialog ( { properties : [ 'openDirectory' , 'createDirectory' ] } ) ;
257
+ if ( ! response . canceled ) {
258
+ return response . filePaths [ 0 ] ;
259
+ } else {
260
+ return null ;
261
+ }
262
+ } ) ;
263
+
264
+ ipcMain . handle ( 'setActive' , async ( event , { id, game, isActive} ) => {
265
+ console . log ( "TOGGLING " + id + " in game " + game + " to " + isActive ) ;
266
+
267
+ let directory = null ;
268
+ if ( game === "rifftrax" ) {
269
+ directory = config . rifftraxDirectory ;
270
+ } else if ( game === "whatthedub" ) {
271
+ directory = config . whatTheDubDirectory ;
272
+ } else {
273
+ return ;
274
+ }
275
+ const clipsDirectory = `${ directory } /StreamingAssets/VideoClips` . replace ( "~" , HOME ) ;
276
+ const subsDirectory = `${ directory } /StreamingAssets/Subtitles` . replace ( "~" , HOME ) ;
277
+ const videoFilePath = `${ clipsDirectory } /${ id } .mp4` ;
278
+ const subFilePath = `${ subsDirectory } /${ id } .srt` ;
279
+
280
+ if ( isActive ) {
281
+ fs . renameSync ( `${ videoFilePath } .disabled` , videoFilePath ) ;
282
+ fs . renameSync ( `${ subFilePath } .disabled` , subFilePath ) ;
283
+ } else {
284
+ fs . renameSync ( videoFilePath , `${ videoFilePath } .disabled` ) ;
285
+ fs . renameSync ( subFilePath , `${ subFilePath } .disabled` ) ;
286
+ }
287
+ } ) ;
0 commit comments