summaryrefslogtreecommitdiff
path: root/src/flashrunner.js
blob: 8f95449573582cb85e9b5e5b9622c493f31e6b5b (about) (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const { mainWindow, ipcRenderer, ipcMain, dialog } = require( 'electron' );

const r_fsys = require( 'fs' );

const version_str = '2.0.7';

var open_menu_name = '';

const flash_container = document.getElementById( 'flash_container' );

/*
 * window_resize is used to resize
 * the container element of flash content
 * container to fit under the menu bar.
*/
function window_resize(){
    flash_container.style.height = window.innerHeight - 35;
}

/*
 * set_container_content function
 * is used to set the flash container
 * element's flash content
*/
function set_container_content( flash_url ){
    if ( flash_url[flash_url.indexOf('.swf') + 4] === undefined ){
        document.getElementById( 'flash_container' ).style.height = window.innerHeight - 35;
        document.getElementById( 'flash_container' ).innerHTML = '<embed src="' + flash_url + '" type="application/x-shockwave-flash" width="100%" height="100%" style="position: relative; top: 0px; left: 0px;"></embed>';
    }
    else
    {
        alert( 'Error: That doesn\'t look like a swf link because it doesn\'t end in .swf' );
    }
}

/*
 * open_url is used to open file/url
*/
function open_url( runner_url ){
    try {
        if ( runner_url === undefined || runner_url === null || runner_url === '' )
            throw new Error( 'Link must not be empty' );
        if ( !runner_url.startsWith('http://') && !runner_url.startsWith('https://') && !runner_url.startsWith('file:///') )
            throw new Error( 'Invalid link' );
        if ( !runner_url.endsWith('.swf') )
            throw new Error( 'Invalid swf file link or location' );
        if ( runner_url.startsWith('file:///') ){
            if ( !r_fsys.existsSync(runner_url.replace('file:///', '')) )
                throw new Error( 'File doesn\'t exist' );
        }
        if ( runner_url[runner_url.indexOf('.swf') + 4] === undefined ){
            set_container_content( runner_url );
            if ( document.getElementById( 'file_opener' ).style.display === 'block' )
                document.getElementById( 'file_opener' ).style.display = 'none';
            if ( document.getElementById( 'file_opener_bg' ).style.display === 'block' );
                document.getElementById( 'file_opener_bg' ).style.display = 'none';
            if ( document.getElementById( 'game_selector' ).style.display === 'block' )
                document.getElementById( 'game_selector' ).style.display = 'none';
            if ( document.getElementById( 'games_list' ).style.display === 'block' )
                document.getElementById( 'games_list' ).style.display = 'none';
        }
        else
        {
            alert( 'Error: that doesn\'t look like a swf link because it doesn\'t end in .swf' );
        }
        runner_url = undefined;
    } catch( err_c ){
        alert( 'Error:\n' + err_c );
    }
}

/*
 * open_modal opens the file/url opener dialog
*/
function open_modal(){
    open_menu_name = 'url_opener';
    document.getElementById( 'file_opener_bg' ).style.display = 'block';
    document.getElementById( 'file_opener' ).style.display = 'block';
}

/*
 * close_modal closes the file/url opener dialog
*/
function close_modal(){
    open_menu_name = '';
    document.getElementById( 'file_opener' ).style.display = 'none';
    document.getElementById( 'file_opener_bg' ).style.display = 'none';
}

/*
 * open_game_selector_menu opens the easy to use
 * game selection menu
*/
function open_game_selector_menu(){
    open_menu_name = 'game_selector';
    document.getElementById( 'file_opener_bg' ).style.display = 'block';
    document.getElementById( 'game_selector' ).style.display = 'block';
    document.getElementById( 'games_list' ).style.display = 'block';
}

/*
 * close_game_selector_menu closes the easy to use
 * game selection menu
*/
function close_game_selector_menu(){
    open_menu_name = '';
    document.getElementById( 'file_opener_bg' ).style.display = 'none';
    document.getElementById( 'game_selector' ).style.display = 'none';
    document.getElementById( 'games_list' ).style.display = 'none';
}

/*
 * create_games_list creates the easy to use
 * list of games received from JSON file on server
*/
function create_games_list( parsed_json ){
    var g_list = document.getElementById( 'games_list' );
    g_list.innerHTML = '';
    var game_index = null;
    for ( game_index in parsed_json.games_menu )
        if ( parsed_json.games_menu[game_index].game_name !== undefined && parsed_json.games_menu[game_index].game_name !== null && parsed_json.games_menu[game_index].game_name !== '' && typeof parsed_json.games_menu[game_index].game_name === 'string' && parsed_json.games_menu[game_index].game_swf !== undefined && parsed_json.games_menu[game_index].game_swf !== null && parsed_json.games_menu[game_index].game_swf !== '' && typeof parsed_json.games_menu[game_index].game_swf === 'string' )
            g_list.innerHTML += '<a href="#" onclick="open_url(\'' + parsed_json.games_menu[game_index].game_swf + '\');" class="modal_game_selector_menu_item">' + parsed_json.games_menu[game_index].game_name + '</a>';
    game_index = undefined;
    g_list = undefined;
}

/*
 * flashrunner_get_game_list gets the list of games
 * from JSON file for the easy to use game selection menu
*/
function flashrunner_get_game_list(){
    try {
        if ( r_fsys.existsSync('./flashrunner_game_list.json') ){
            var game_list_json = r_fsys.readFileSync( './flashrunner_game_list.json', {
                encoding: 'utf8',
                flag: 'r'
            } );
            var games_list_parsed = JSON.parse( game_list_json );
            create_games_list( games_list_parsed );
            games_list_parsed = undefined;
            game_list_json = undefined;
        }
        else
        {
            document.getElementById( 'game_list' ).innerHTML = '<span class="wtext">Error: could not find games list file</span>';
        }
    } catch ( err_m ){
        document.getElementById( 'game_list' ).innerHTML = '<span class="wtext">Error loading games list</span>';
    }
}

/*
 * flashrunner_about is the about dialog, simple
*/
function flashrunner_about(){
    alert( 'FlashRunner by VoidNet\nVersion: ' + version_str + '\n\nFlash copyright:\n\u00A9 Adobe Systems Incorporated and its licensors. All rights reserved.' );
}

document.getElementById( 'menu_open' ).addEventListener( 'click', function(){
    open_modal();
} );

document.getElementById( 'menu_about' ).addEventListener( 'click', function(){
    flashrunner_about();
} );

document.getElementById( 'modal_run_btn' ).addEventListener( 'click', function(){
    if ( document.getElementById('flash_file').value !== undefined && document.getElementById('flash_file').value !== null && document.getElementById('flash_file').value !== '' ){
        if ( document.getElementById('flash_file').value.startsWith('http://') || document.getElementById('flash_file').value.startsWith('https://') )
            open_url( document.getElementById('flash_file').value );
        else
            open_url( 'file:///' + document.getElementById('flash_file').value );
	}
    else
    {
        alert( 'Flash file URL or path can\'t be empty' );
	}
} );

document.getElementById( 'menu_game_selector' ).addEventListener( 'click', function(){
    open_game_selector_menu();
} );

document.getElementById( 'menu_steamgroup' ).addEventListener( 'click', function(){
    ipcRenderer.send( 'asyncmsg', 'OPEN_URL https://steamcommunity.com/groups/VoidNet_' );
} );

window.addEventListener( 'resize', window_resize );

document.getElementById( 'menu_exit' ).addEventListener( 'click', function(){
    ipcRenderer.send( 'asyncmsg', 'APP_SHUTDOWN' );
} );

document.getElementById( 'close_flash_opener' ).addEventListener( 'click', function(){
    switch ( open_menu_name ){
        case 'url_opener':
            close_modal();
        break;
        case 'game_selector':
            close_game_selector_menu();
        break;
        default:
        break;
    }
} );

document.addEventListener( 'contextmenu', function(ctx_event){
    ctx_event.preventDefault();
} );

flashrunner_get_game_list();