-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathopenjk
590 lines (500 loc) · 18.3 KB
/
openjk
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from os import system
from distutils.dir_util import copy_tree
from subprocess import call
import os
import sys
import gi
import pygame
import fileinput
import re
import urllib.request
import urllib.error
import urllib.parse
import tarfile
import shutil
import requests
import configparser
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
# Check operating system
if sys.platform.startswith('linux'):
homedir = os.path.expanduser('~')
# gamedir = homedir + '/Games/OpenJk'
gamedir = ""
configdir = homedir + '/.local/share/openjk/base'
configfile = configdir + '/autoexec.cfg'
openjk_config = configdir + '/ojkl.cfg'
tmpdir = '/tmp'
path = configdir + '/ojkl.cfg'
resourcedir = '/usr/share/openjk/resources'
exe = gamedir + '/openjk.x86_64'
print('Running on Linux')
cancel = 0
# TODO add support for Windows and OSX
# Load the css file for glade
# cssProvider = Gtk.CssProvider()
# cssProvider.load_from_path('/usr/share/openjk/resources/style.css')
# screen = Gdk.Screen.get_default()
# styleContext = Gtk.StyleContext()
# styleContext.add_provider_for_screen(screen, cssProvider,
# Gtk.STYLE_PROVIDER_PRIORITY_USER)
# setup audio
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.mixer.init()
# setup net
sess = requests.Session()
adapter = requests.adapters.HTTPAdapter(max_retries=2)
sess.mount('http://', adapter)
class launcher:
def __init__(self):
self.builder = Gtk.Builder()
self.builder.add_from_file(resourcedir+'/openjk.glade')
self.builder.connect_signals(self)
self.single = self.builder.get_object('single')
self.multi = self.builder.get_object('multi')
self.overlay = self.builder.get_object('overlay')
self.image = self.builder.get_object('image2')
self.window2 = self.builder.get_object('window2')
self.window3 = self.builder.get_object('window3')
self.hd_res1 = self.builder.get_object('hd_res1')
self.window = self.builder.get_object('window')
self.comboboxtext = self.builder.get_object('comboboxtext')
self.modlist = self.builder.get_object('modlist')
self.install = self.builder.get_object('install')
self.messagedialog = self.builder.get_object('messagedialog')
self.errordialog = self.builder.get_object('errordialog')
self.ok = self.builder.get_object('ok')
self.ok3 = self.builder.get_object('ok3')
self.progress = self.builder.get_object('progress')
self.install_label = self.builder.get_object('install_label')
self.checkbutton = self.builder.get_object('checkbutton')
self.checkbutton2 = self.builder.get_object('checkbutton2')
self.window.connect('destroy', lambda w: Gtk.main_quit())
self.window.show_all()
self.messagedialog.hide()
self.link = self.builder.get_object('link')
self.link.set_label("Openjk @ Github.com")
self.folder = self.builder.get_object("folder")
self.ok_folder = self.builder.get_object("ok_folder")
self.cancel_folder = self.builder.get_object("cancel_folder")
check_directory()
create_file()
if not os.path.exists(path):
create_openjk_config(path)
check_settings(self)
def on_ok_folder_clicked(self, widget):
self.progress.set_fraction(.05)
print("Open clicked")
global gamedir
gamedir = (self.folder.get_filename())
config = configparser.ConfigParser()
config.read(path)
config.set("Path", "folder", gamedir)
with open(path, "w") as config_file:
config.write(config_file)
print(("Gamedir is now set to" + gamedir))
self.folder.hide()
while Gtk.events_pending():
Gtk.main_iteration()
print(gamedir)
def show_message():
self.ok.set_visible(False)
while Gtk.events_pending():
Gtk.main_iteration()
self.messagedialog.show()
# self.ok.set_opacity(.25)
self.ok.set_visible(False)
self.progress.set_fraction(.25)
while Gtk.events_pending():
Gtk.main_iteration()
self.messagedialog.show()
while Gtk.events_pending():
Gtk.main_iteration()
# self.install_label.set_text('Please Wait...')
while Gtk.events_pending():
Gtk.main_iteration()
if check_internet(self) is False:
return
Gtk.main_iteration()
site = 'https://builds.openjk.org/'
hdr = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11\
(KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,\
application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive',
}
req = urllib.request.Request(site, headers=hdr)
Gtk.main_iteration()
try:
page = urllib.request.urlopen(req)
except urllib.error.HTTPError as e:
self.errordialog.show()
while Gtk.events_pending():
Gtk.main_iteration()
print(e.fp.read())
Gtk.main_iteration()
content = page.read()
Gtk.main_iteration()
file = open('/tmp/tmp.html', 'wb')
file.write(content)
file.close()
print('Checking openjk Builds for newest version')
file = open('/tmp/tmp.html', 'r')
lines = file.read()
answer = re.findall(r'openjk[^<>]*?linux-64.tar.gz', lines)[-1]
print(answer)
base_url = 'https://builds.openjk.org/'
file = base_url + answer
print(file)
self.progress.set_fraction(.50)
Gtk.main_iteration()
self.ok.set_visible(False)
self.ok.set_sensitive(False)
while Gtk.events_pending():
Gtk.main_iteration()
self.messagedialog.show()
self.ok.set_opacity(.25)
while Gtk.events_pending():
Gtk.main_iteration()
self.messagedialog.show()
while Gtk.events_pending():
Gtk.main_iteration()
self.install_label.set_text('Please Wait...')
while Gtk.events_pending():
Gtk.main_iteration()
print('Downloading file now')
for i in range(1):
try:
print(('Downloading %s' % answer))
r = sess.get(file, stream=True, verify=False,
allow_redirects=True)
with open('/tmp/' + answer, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
except requests.exceptions.RequestException as e:
print(e)
sys.exit(1)
self.progress.set_fraction(.75)
# Gtk.main_iteration()
print('File finished downloading')
print('Extracting tarball')
tar = tarfile.open('/tmp/' + answer)
tar.extractall('/tmp/')
tar.close()
print('Installing files')
root_src_dir = tmpdir + '/install/JediAcademy/'
root_dst_dir = gamedir
copy_tree(root_src_dir, root_dst_dir)
print('Cleaning up....')
print('Done.\n')
os.remove(tmpdir + '/' + answer)
os.remove(tmpdir + '/tmp.html')
shutil.rmtree(tmpdir + '/install')
# self.messagedialog.show()
self.progress.set_fraction(1)
self.install_label.set_text('Done, Please press ok')
self.ok.set_opacity(1)
self.ok.set_visible(True)
self.ok.set_sensitive(True)
self.folder.hide()
def on_cancel_folder_clicked(self, widget):
self.folder.hide()
cancel == 1
return cancel
def on_ok_clicked(self, widget):
self.install_label.set_text('Please Wait...')
self.ok.set_visible(False)
self.messagedialog.hide()
def on_multi_enter_notify_event(self, widget, hover):
sound_start = pygame.mixer.Sound(resourcedir + '/hover.WAV')
sound_start.play()
def on_single_enter_notify_event(self, widget, hover):
sound_start = pygame.mixer.Sound(resourcedir + '/hover.WAV')
sound_start.play()
def on_exit_enter_notify_event(self, widget, hover):
sound_start = pygame.mixer.Sound(resourcedir + '/hover.WAV')
sound_start.play()
def on_options_enter_notify_event(self, widget, hover):
sound_start = pygame.mixer.Sound(resourcedir + '/hover.WAV')
sound_start.play()
def on_install_enter_notify_event(self, widget, hover):
sound_start = pygame.mixer.Sound(resourcedir + '/hover.WAV')
sound_start.play()
def on_single_clicked(self, widget):
sound_start = pygame.mixer.Sound(resourcedir + '/click.WAV')
sound_start.play()
self.window.destroy()
print(gamedir)
exe = gamedir + '/openjk_sp.x86_64 +fs_game OpenJk'
print(exe)
# p=subprocess.Popen([exe])
call(exe, shell=True)
def on_multi_clicked(self, widget):
sound_start = pygame.mixer.Sound(resourcedir + '/click.WAV')
sound_start.play()
sound_open = pygame.mixer.Sound(resourcedir + '/start.WAV')
sound_open.play()
if '#mod "1"' in open(configfile).read():
exe = gamedir + '/openjk.x86_64 +set fs_game japlus'
elif '#mod "0"' in open(configfile).read():
exe = gamedir + '/openjk.x86_64'
elif '#mod "2"' in open(configfile).read():
exe = gamedir + '/openjk.i386 +set fs_game MBII'
else:
exe = gamedir + '/openjk.x86_64'
self.window.destroy()
call(exe, shell=True)
def on_back_clicked(self, widget):
sound_start = pygame.mixer.Sound(resourcedir + '/back.WAV')
sound_start.play()
self.window2.hide()
self.window.show_all()
def on_options_clicked(self, widget):
sound_start = pygame.mixer.Sound(resourcedir + '/click.WAV')
sound_start.play()
self.window.hide()
self.window2.show_all()
self.window2.connect('delete-event', lambda w, e: w.hide() or True)
def on_exit_clicked(self, widget):
sound_start = pygame.mixer.Sound(resourcedir + '/click.WAV')
sound_start.play()
system('sleep .25')
Gtk.main_quit()
def on_comboboxtext_changed(self, widget):
sound_start = pygame.mixer.Sound(resourcedir + '/click.WAV')
sound_start.play()
active = widget.get_active()
if active == 0:
for line in fileinput.FileInput(configfile, inplace=True):
if line.strip().startswith('seta r_mode "'):
line = 'seta r_mode "-2"\n'
sys.stdout.write(line)
shutil.copy(configfile, configdir + '/autoexec_sp.cfg')
self.comboboxtext.set_active(0)
def on_checkbutton_toggled(self, widget):
sound_start = pygame.mixer.Sound(resourcedir + '/click.WAV')
sound_start.play()
if self.checkbutton.get_active() is True:
for line in fileinput.FileInput(configfile, inplace=True):
if line.strip().startswith('seta r_fullscreen "'):
line = 'seta r_fullscreen "1"\n'
sys.stdout.write(line)
shutil.copy(configfile, configdir + '/autoexec_sp.cfg')
else:
for line in fileinput.FileInput(configfile, inplace=True):
if line.strip().startswith('seta r_fullscreen "'):
line = 'seta r_fullscreen "0"\n'
sys.stdout.write(line)
shutil.copy(configfile, configdir + '/autoexec_sp.cfg')
def on_checkbutton2_toggled(self, widget):
sound_start = pygame.mixer.Sound(resourcedir + '/click.WAV')
sound_start.play()
if self.checkbutton2.get_active() is True:
for line in fileinput.FileInput(configfile, inplace=True):
if line.strip().startswith('seta r_noborder "'):
line = 'seta r_noborder "1"\n'
sys.stdout.write(line)
shutil.copy(configfile, configdir + '/autoexec_sp.cfg')
else:
for line in fileinput.FileInput(configfile, inplace=True):
if line.strip().startswith('seta r_noborder "'):
line = 'seta r_noborder "0"\n'
sys.stdout.write(line)
shutil.copy(configfile, configdir + '/autoexec_sp.cfg')
def on_modlist_changed(self, widget):
sound_start = pygame.mixer.Sound(resourcedir + '/click.WAV')
sound_start.play()
# model = widget.get_model()
active = widget.get_active_id()
if active is None:
for line in fileinput.FileInput(configfile, inplace=True):
if line.strip().startswith('#mod "'):
line = '#mod "0"\n'
sys.stdout.write(line)
if active == 'jp':
for line in fileinput.FileInput(configfile, inplace=True):
if line.strip().startswith('#mod "'):
line = '#mod "1"\n'
sys.stdout.write(line)
self.modlist.set_active_id('jp')
if active == 'mb':
for line in fileinput.FileInput(configfile, inplace=True):
if line.strip().startswith('#mod "'):
line = '#mod "2"\n'
sys.stdout.write(line)
self.modlist.set_active_id('mb')
def on_install_clicked(self, widget):
install(self)
def on_ok2_clicked(self, widget):
self.errordialog.hide()
# check directory
def check_directory():
if not os.path.isdir(configdir):
os.makedirs(configdir)
# Create Config file
def create_file():
if not os.path.isfile(configfile):
file = open(configfile, 'w+')
file.write('#mod "0"\n')
file.write('seta r_mode "4"\n')
file.write('seta r_fullscreen "0"\n')
file.write('seta r_noborder "0"\n')
file.close
shutil.copy(configfile, configdir + '/autoexec_sp.cfg')
def create_openjk_config(path):
if not os.path.isfile(path):
config_file = open(path, 'w+')
config = configparser.ConfigParser()
config.add_section("Path")
config.set("Path", "folder", "null")
with open(path, "r+") as config_file:
config.write(config_file)
def check_settings(self):
# Check for fullscreen
global gamedir
config = configparser.ConfigParser()
config.read(path)
gamedir = config.get("Path", "Folder")
print(("Game dir is : " + gamedir))
# Check for mod folders, add to list if they exist
if os.path.isdir(gamedir + '/japlus'):
self.modlist.insert(1, 'jp', "JA++")
if os.path.isdir(gamedir + '/MBII'):
self.modlist.insert(2, 'mb', "MBII")
if os.path.isfile(configfile):
fd = open(configfile)
fbuf = fd.read()
fd.close()
if 'seta r_fullscreen "1"' in fbuf:
print("Fullscreen is on")
self.checkbutton.set_active(True)
else:
print("Fullscreen is off")
self.checkbutton.set_active(False)
# Check for borderless window
if 'seta r_noborder "1"' in fbuf:
print("Bordless window is on")
self.checkbutton2.set_active(True)
else:
print("Bordless window is off")
self.checkbutton2.set_active(False)
# check for native resolution
if 'seta r_mode "-2"' in fbuf:
print("Resolution is set to native")
self.comboboxtext.set_active(0)
else:
print("Resolution is not set to native")
self.comboboxtext.set_active(1)
# check what mod is enabled
if '#mod "1"' in fbuf:
print("Mod is set to Ja++")
self.modlist.set_active_id('jp')
elif '#mod "0"' in fbuf:
print("Mod is not set")
self.modlist.set_active(0)
elif '#mod "2"' in fbuf:
print("Mod is set to MBII")
self.modlist.set_active_id('mb')
# Install script #
def install(self):
cancel = 0
sound_start = pygame.mixer.Sound(resourcedir + '/click.WAV')
sound_start.play()
# global gamedir
# self.window.hide()
# self.folder.show()
self.folder.run()
if cancel == 1:
return
# End of Install script #
def test(self):
while Gtk.events_pending():
Gtk.main_iteration()
def check_internet(self):
try:
urllib.request.urlopen('https://google.com', timeout=2)
print('you are connected')
return True
except urllib.error.URLError as err:
print(err)
print('you are disconnected')
self.errordialog.show()
self.messagedialog.hide()
return False
# print 'Your game directory is: ' + gamedir
print('Your config directory is : ' + configdir)
# Setting up the windows and widgets
def main():
def gtk_style():
css = b"""
button:hover {
background-image: none;
background-color: transparent;
transition: 400ms linear;
border-right-color: #91a8d0;
border-left-color: #91a8d0;
border-top-color: #91a8d0;
border-bottom-color: #91a8d0;
border-radius: 10px;
box-shadow: 0 0 10px #333 inset;
}
window button{
background-image: none;
background-color: transparent;
font-size: 11px;
color: white;
}
label {
font-size: 11px;
color: white;
}
comboboxtext{
background-image: none;
background-color: transparent;
border-color: transparent;
color:black
}
combotextbox:active{
background-image: none;
background-color: transparent;
border-color: transparent;
}
messagedialog {
background-color: #696969;
color:black
}
messagedialog button {
background-image: none;
background-color: transparent;
font-size: 11px;
color: white;
}
dialog label{
color:black
}
dialog button{
background-color: #696969;
color:black
}
"""
# Load the css file for glade
cssProvider = Gtk.CssProvider()
cssProvider.load_from_path(resourcedir + "/style.css")
screen = Gdk.Screen.get_default()
styleContext = Gtk.StyleContext()
styleContext.add_provider_for_screen(screen, cssProvider,
Gtk.STYLE_PROVIDER_PRIORITY_USER)
launcher()
gtk_style()
sound_launch = pygame.mixer.Sound(resourcedir + '/open.WAV')
sound_launch.play()
Gtk.main()
if __name__ == '__main__':
main()