-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
1184 lines (909 loc) · 42.2 KB
/
__init__.py
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#The MIT License (MIT)
#
#Copyright (c) 2015 Robert Planas Jimenez
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
#NOTE FROM THE AUTHOR:
# Your game is not a "substantial portion" of the Software.
# The Simple Launcher is Public Domain
# Blenderplayer is GPL 3, if you are going to use it, copy its License file.
# Don't worry too much about how easily your game can be modified, the better
# games are those who have mods and thousands of pirated copies.
import bpy
from bpy.props import *
import shutil, re
import os, sys, stat
import platform as libplatform
import threading
import urllib.request
import zipfile
bl_info = {
"name": "Build Game",
"author": "Robert Planas (elmeunick9)",
"location": "Render Properties > Build Game // Platforms // Tools",
"category": "Game Engine",
"wiki_url": "http://blenderartists.org/forum/showthread.php?340504-New-BGE-Addon-%28Build-Game%29",
"blender": (2, 73, 0),
"description": "Make your game ready for share in multiple platforms."
}
############################ GLOBALS ################################
class BuildGameAddon:
version = 150205 #Version of the addon, change only when there are important changes.
project_root = "" #Root directory of the project, none for nodir, ex: "../../"
player_root = "engine/" #Root of the player
############################ UTILS ##################################
def is_default_player (os_t, arch, player):
if libplatform.system() == 'Linux': default_os = 'lin'
if libplatform.system() == 'Windows': default_os = 'win'
if libplatform.system() == 'Mac': default_os = 'mac'
narch = bpy.app.build_platform.decode("utf-8")
narch = narch[narch.find(':')+1:narch.rfind('bit')]
default_arch = 'x' + narch
if os_t == default_os and arch == default_arch and player == 'blenderplayer': return True
else: return False
def get_magic_paths(platform):
magic_path = platform.type_build
if not magic_path == 'custom':
magic_path = magic_path + platform.default_architecture[1:]
magic_path = bpy.utils.user_resource('DATAFILES', 'platform' + os.sep + magic_path, True) + os.sep
player = magic_path + platform.default_player + os.sep
launcher = magic_path + platform.default_launcher + os.sep
else:
player = bpy.path.abspath(platform.custom_player_path)
launcher = bpy.path.abspath(platform.custom_launcher_path)
playerd = ""; launcherd = ""
if os.path.isdir(player): playerd = os.sep
if os.path.isdir(launcher): launcherd = os.sep
player = os.path.abspath(player) + playerd
launcher = os.path.abspath(launcher) + launcherd
return [magic_path, player, launcher]
def new_install(version_filepath): #Of the addon, not blender
#Delete old data
platform_folder = os.path.dirname(version_filepath) + os.sep + "platform"
try:
shutil.rmtree(platform_folder)
except: pass
#Update the version file
file = open(version_filepath, "w")
file.write(str(BuildGameAddon.version))
file.close()
#Install simple_launcher.zip
try:
addon_folder = bpy.utils.user_resource('SCRIPTS', 'addons' + os.sep + 'game_engine_build' + os.sep)
filepath = addon_folder + 'simple_launcher.zip'
zip = zipfile.ZipFile(filepath)
zip.extractall(platform_folder)
zip.close()
except Exception as ex:
print(ex)
return
#Greets the user
print("BUILD GAME ADDON: Hello, I've been installed!")
print("Check how to use me on my thread --> http://blenderartists.org/forum/showthread.php?340504-Build-Game-Addon")
def check_new_install():
verfile = bpy.utils.user_resource('DATAFILES') + "build_game_addon_version.txt"
if(os.path.isfile(verfile)):
file = open(verfile, "r")
file_version = file.read()
if(file_version != str(BuildGameAddon.version)):
print("BUILD GAME #001: Versions don't match, I'll proced to reinstall myself.")
new_install(verfile)
else:
new_install(verfile)
def in_any (elements, iterable):
for i in elements:
if i in iterable:
return True
return False
###################################### EVENTS #########################################
# Check if the current configuration need to be installed or not.
# Disable/Enable Active and Overwrite buttons if it's nesscesary.
def active_click (self, context):
if RENDER_PT_Platforms.enabled == True: self.user_active = self.active
def check_add (self, context):
bs = context.scene.ge_build_settings
bs.platforms_active = len(bs.platforms)-1
platform = bs.platforms[bs.platforms_active]
RENDER_PT_Platforms.enabled = True
check(platform, context)
# Update from: check_add, platforms_active, type_build, architecture, default_player, default_launcher.
def check(self, context):
bs = context.scene.ge_build_settings
platform = bs.platforms[bs.platforms_active]
magic_path, player, launcher = get_magic_paths(platform)
# Check if installed
if os.path.exists(player): player_exist = True
else: player_exist = os.path.isdir(player)
if os.path.exists(launcher): launcher_exist = True
else: launcher_exist = os.path.isdir(launcher)
installed = (player_exist and launcher_exist and not DownloadPlatformResource.thread.isAlive())
RENDER_PT_Platforms.player_exist = player_exist
RENDER_PT_Platforms.launcher_exist = launcher_exist
# Check for included blenderplayer
if is_default_player(platform.type_build, platform.default_architecture, platform.default_player):
if not magic_path == 'custom':
if launcher_exist: installed = True
if platform.build_merge: installed = True
RENDER_PT_Platforms.player_exist = True
# Update interface
if installed:
if not RENDER_PT_Platforms.enabled: platform.active = platform.user_active
else: platform.user_active = platform.active
else:
if RENDER_PT_Platforms.enabled: platform.user_active = platform.active
RENDER_PT_Platforms.enabled = False #Quick hack
platform.active = False
RENDER_PT_Platforms.enabled = installed
# Update platform.name for standard configurations
platformX = {"Linux_32bit", "Linux_64bit", "Windows_32bit", "Windows_64bit",
"Mac_32bit", "Mac_64bit", "PlatformX"}
if platform.name in platformX:
name = "PlatformX"
if platform.type_build == "lin": name = "Linux_"
elif platform.type_build == "win": name = "Windows_"
elif platform.type_build == "mac": name = "Mac_"
if name != "PlatformX": name += platform.default_architecture[1:] + "bit"
platform.name = name
#Check and update BuildGameAddon.project_root if nescesary
name = bpy.path.basename(bpy.data.filepath).replace('.blend','')
source_name = bpy.path.abspath('//')
source_name = source_name[:len(source_name)-1]
source_name = bpy.path.basename(source_name)
if source_name and (name == source_name or name == bs.game_name):
BuildGameAddon.project_root = "./"
##################################### BUILD GAME RUTINE #########################################
#TODO: Once the addon is splited in files, use the SuperCopy class also in the download class.
import time
class SuperCopy ():
def __init__ (self, ref):
self.stop = False
self.ref = ref
self.progress = 0
self.overwritte = False
self.size = 0
def update_progress(self, progress, state):
if progress >= 1: progress = 1
if progress <= 0: progress = 0
state = state + ' ' * (28 - len(state))
print(state + '[{0:39s}] {1:.1f}%'.format('=' * int(progress * 39), int(progress*100)), end = '\r')
def copyFile (self, source, dest_file, max, message):
# Copy only one file, update the "max" whiletime.
# Open src and dest files, get src file size
min = self.progress
if not self.overwritte:
if os.path.isfile(dest_file): return
if not os.path.exists(os.path.dirname(dest_file)):
os.makedirs(os.path.dirname(dest_file))
src = open(source, "rb")
dest = open(dest_file, "wb")
src_size = os.stat(source).st_size
block_size = 16384
cur_block_pos = 0
while True:
if self.stop: raise Exception ("Building stoped by the user.")
cur_block = src.read(block_size)
if not cur_block:
break
else:
cur_block_pos += block_size
dest.write(cur_block)
if cur_block_pos/block_size >= 1:
self.update_progress(min + max/100, message)
else:
self.update_progress(min + (cur_block_pos/src_size)*(max/100), message)
src.close()
dest.close()
# Check output file is same size as input one!
dest_size = os.stat(dest_file).st_size
if dest_size != src_size:
raise IOError(
"New file-size does not match original (src: %s, dest: %s)" % (
src_size, dest_size)
)
def copy(self, source, max, subject, dest="", exclude=[]):
# Check for overwritte
# Update the progress bar, where max is the maxium progress.
if not dest: dest = self.ref.game_directory
plat = "(" + str(self.ref.platform_index) + "/" + str(len(self.ref.platform_list)) + ") "
# Check if source is a file and exist
if os.path.isfile(source):
self.copyFile(source, dest + os.path.basename(source), max, plat + "Coping " + subject + "...")
else:
if os.path.isdir(source):
#Check the total size to copy, recusively, only one time/platform.
if not self.size:
for root, dirs, files in os.walk(source):
for file in files:
self.size += os.stat(os.path.join(root, file)).st_size
dirlist = os.listdir(source)
# If it's an empty directory, make an empty one.
if dirlist == []:
try:
os.makedirs(dest)
return
except FileExistsError: return
# If it has contents, check the size and copy.
for name in dirlist:
if os.path.isfile(source + name):
filesize = os.stat(source + name).st_size
self.progress = self.progress + (filesize/self.size) * (max/100)
if source+name not in exclude:
self.copyFile(
source + name, dest + name,
self.progress,
plat + "Coping " + subject + "...")
else:
if source+name not in exclude:
self.copy(source + name + os.sep, max, subject, dest + name + os.sep)
else:
print("BUILD GAME #002: (" + subject + ") File or directory not found")
print(source)
class PlatformInstall(threading.Thread):
game_name = "Unknow"
game_directory = "//lib/"
context = None
status = ""
overwritte = True
def run(self):
bs = self.context.scene.ge_build_settings
self.status = "Working, look in the console for now..."
self.size = 0
self.scp = SuperCopy(self)
self.scp.overwritte = self.overwritte
#Check for startup blend
#Warning, doing ops in this thread crashes blender.
#So, this is done on the buid operator.
#if not bpy.data.filepath:
# bpy.ops.wm.save_mainfile()
print("------------ BUILD GAME ------------")
self.platform_list = []
for platform in bs.platforms:
if platform.active == False: continue
self.platform_list.append(platform)
self.platform_index = 0
for platform in self.platform_list:
self.scp.progress = 0.00
self.platform_index += 1
try: self.install(platform)
except:
print()
print("Stop.")
break
self.status = ""
del self.scp
print("------------------------------------")
def install(self, platform):
bs = self.context.scene.ge_build_settings
magic_path, player_path, launcher_path = get_magic_paths(platform)
self.overwritte = platform.overwritte
dest = self.game_directory + platform.name + os.sep
# Install PLAYER
if is_default_player(platform.type_build, platform.default_architecture, platform.default_player):
self.copyDefaultPlayer(dest + BuildGameAddon.player_root)
else:
self.scp.copy(player_path, 50, "player", dest + BuildGameAddon.player_root)
# Install LAUNCHER
if platform.build_merge:
print("WARNING: Merge is strongly discoraged. See the details form:")
print("http://blenderartists.org/forum/showthread.php?259752-The-GPL-Answer-Thread")
print()
print("Not even implemented yet!")
else:
self.scp.copy(launcher_path, 70, "launcher", dest)
#Install SOURCE
name = bpy.path.basename(bpy.data.filepath).replace('.blend','')
source = bpy.data.filepath
source_name = bpy.path.abspath('//')
source_name = source_name[:len(source_name)-1]
source_name = bpy.path.basename(source_name)
source_dir = bpy.path.abspath("//" + BuildGameAddon.project_root)
source_dir = os.path.abspath(source_dir) + os.sep
if BuildGameAddon.project_root:
self.scp.copy(source_dir, 95, "your directory", dest)
else:
self.scp.copy(source, 95, "your file", dest)
print(source)
print("Dir: " + source_dir)
#Configure SOURCE
import runpy
filelist = os.listdir(dest)
for file in filelist:
if file == '__launcher__.py' \
or file == '__player__.py':
try:
runpy.run_path(dest + file, init_globals = {
'blend': source[len(source_dir):],
'player': BuildGameAddon.player_root + platform.default_player,
'launcher': platform.default_launcher,
'arch': platform.default_architecture,
'game': bs.game_name,
'__file__': dest + file,
'__path__': dest,
}, run_name='__main__')
except Exception as e:
print(e)
os.remove(dest + file)
self.scp.update_progress(1, platform.name)
print() #New Line, end of progress bar.
def stopMe(self):
self.scp.stop = True
def copyDefaultPlayer(self, dest):
ext = ''
platform = self.platform_list[self.platform_index-1]
if platform.type_build == 'win': ext = '.exe'
player = os.path.join(os.path.dirname(bpy.app.binary_path), "blenderplayer" + ext)
#Welcome to: Find the Python lib quest!
blender_version = str(bpy.app.version[0]) + '.' + str(bpy.app.version[1])
python_version = 'python' + str(sys.version_info[0]) + '.' + str(sys.version_info[1])
pylib_root = os.path.join(os.path.dirname(bpy.app.binary_path), blender_version) + os.sep
if platform.default_architecture == 'lin':
test = 'python' + os.sep + 'lib' + os.sep + python_version + os.sep
else:
test = 'python' + os.sep + 'lib' + os.sep
pylib = ''
if os.path.exists(pylib_root + test): pylib = pylib_root + test
else:
for path in sys.path:
index = path.rfind(python_version)
if index > 0: pylib = path[0:index+len(python_version)]
if not pylib:
print("Error on platform '" + platform.name + \
"': Path to the default Python library not found.")
return
#Copy Files
self.scp.copy(player, 50, 'player', dest)
self.scp.copy(pylib + os.sep, 80, 'python', dest + os.sep + blender_version + os.sep + test)
if platform.type_build == 'win':
exclude = []
src = os.path.dirname(bpy.app.binary_path) + os.sep
for file in os.listdir(src):
if not file.endswith('.dll'):
exclude.append(src + file)
self.scp.copy(src, 85, 'DLLs', dest, exclude)
game_player = dest + os.path.basename(player)
st = os.stat(game_player)
os.chmod(game_player, st.st_mode | stat.S_IEXEC)
################################### DOWNLOAD RESOURCES ###################################
import time
import shutil
class DownloadProcess(threading.Thread):
system = None
architecture = None
item = None
type = None
official = False
magicURL = "http://pastebin.com/raw.php?i=KkNUhUGy"
message = None
def run(self):
print("BUILD GAME ADDON: Starting download of " + self.item + "...")
if self.official and self.type == "player":
self.download_from_blender_directory()
else: self.download_from_addon_directory()
self.message = None
check(self, self.context)
def download_from_blender_directory(self):
self.message = "Checking repository..."
version = bpy.app.version_string.split()[0]
baseurl = "http://download.blender.org/release/Blender" + version + '/'
#Conversions
if self.system == "win":
realname = "windows"
arch = {self.architecture[1:] + '.'}
else:
if self.system == "lin": realname = "linux"
if self.system == "mac": realname = "OSX"
if self.architecture == "x32": arch = {"i386", "i686"}
else: arch = {"x86_64"}
data = str(urllib.request.urlopen(baseurl).read())
links = re.findall('"(?<=href=")(.*?)(?=">)"', data)
for link in links:
if realname in link and version + bpy.app.version_char in link and in_any(arch, link):
if in_any(link.split('.'), {'tar','zip','bz2'}):
xname = link
if not xname:
print("BUILD GAME #003: Release not found")
return
#Download
print("Downloading " + baseurl + xname + "...")
archive = self.downloadSource(baseurl + xname)
#archive = bpy.utils.user_resource('DATAFILES', 'platform') + os.sep + xname
#Install
print("Unpacking...")
self.message = "Installing..."
basepath = bpy.utils.user_resource('DATAFILES', 'platform') + os.sep
tmppath = basepath + "temp" + os.sep
os.rename(archive, basepath + xname)
archive = basepath + xname
shutil.unpack_archive(archive, tmppath)
#Copy the standard files
dest = basepath + self.system + self.architecture[1:] + os.sep + self.item + os.sep
#NOTE: MacOS uses a complete diferent file structure
os.rename(tmppath + os.listdir(tmppath)[0], dest)
#Clean this mess.
print("Cleaning...")
os.rmdir(tmppath)
try:
shutil.rmtree(dest + version + os.sep + "datafiles")
shutil.rmtree(dest + version + os.sep + "scripts")
shutil.rmtree(dest + "icons", True)
shutil.rmtree(dest + "lib", True)
for filename in os.listdir(dest):
if not in_any({".dll",".txt","blenderplayer", version}, filename):
filepath = dest + filename
if os.path.isfile(filepath): os.remove(filepath)
os.rename(dest + "copyright.txt", dest + "blender-copyright.txt")
print("Done.")
except Exception as ex:
print(ex)
def download_from_addon_directory(self):
self.message = "Getting file links..."
response = urllib.request.urlopen(self.magicURL)
try:
link = self.getLink(response)
if not link: raise Exception ("BUILD GAME #004: Aborted! No links found for this configuration.")
print(link)
self.message = "Starting download..."
filepath = self.downloadSource(link)
zip = zipfile.ZipFile(filepath)
zip.extractall(os.path.dirname(filepath))
zip.close()
print("Done.")
except Exception as ex:
print(ex)
self.message = None
return
def getLink(self, response):
state_platform = False
state_item = False
while True:
line = response.readline()
if not line: break
line = line[:len(line)-2]
plat = str.encode(self.system + self.architecture)
if line[0] == 64 and line[1:] == plat:
state_platform = True
if state_platform == True:
if state_item == True:
return line.decode()
item = str.encode(self.item)
if line[0] == 58 and line[1:] == item:
state_item = True
def downloadSource(self, link):
filepath = bpy.utils.user_resource('DATAFILES', "platform") + os.sep + "temp.zip"
source = urllib.request.urlopen(link)
file = open(filepath, 'wb+')
filesize = source.info()['Content-Length']
size = 0
block = 4096
start_time = time.time()
while True:
buffer = source.read(block)
if not buffer:
break
file.write(buffer)
size += len(buffer)
percent = size/int(filesize)
time_diff = time.time() - start_time
if (percent > 0.01):
estimated_time = int(time_diff / percent - time_diff)
self.message = str(int(percent*100)) + "% - " + str(estimated_time) + " sec"
else: self.message = str(int(percent*100)) + '%'
file.close()
return filepath
###################################### INTERFACE #########################################
class RENDER_PT_BuildGame(bpy.types.Panel):
bl_label = "Build Game"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "render"
@classmethod
def poll(cls, context):
scene = context.scene
return scene and (scene.render.engine == "BLENDER_GAME")
def draw(self, context):
bs = context.scene.ge_build_settings
layout = self.layout
layout.prop(bs, 'game_name')
layout.prop(bs, 'build_filepath')
if BuildGame.thread.status == "":
layout.operator("export.build_game")
else:
layout.operator("export.build_game_cancel")
class RENDER_PT_Platforms(bpy.types.Panel):
bl_label = "Platforms"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "render"
enabled = True
player_exist = True
launcher_exist = True
@classmethod
def poll(cls, context):
scene = context.scene
return scene and (scene.render.engine == "BLENDER_GAME")
def draw(self, context):
bs = context.scene.ge_build_settings
layout = self.layout
row = layout.row()
if bpy.app.version[1] >= 66: row.template_list("UI_UL_list", "platforms_list", bs, 'platforms', bs, 'platforms_active')
else: row.template_list(bs, 'platforms', bs, 'platforms_active')
col = row.column(align=True)
col.operator(BuildAddPlatform.bl_idname, icon='ZOOMIN', text="")
col.operator(BuildRemovePlatform.bl_idname, icon='ZOOMOUT', text="")
if len(bs.platforms) > bs.platforms_active >= 0:
platform = bs.platforms[bs.platforms_active]
row = layout.row()
row.prop(platform, 'active')
row.prop(platform, 'overwritte')
row.enabled = self.enabled
layout.prop(platform, 'type_build', expand=True)
if (platform.type_build == 'custom'):
layout.prop(platform, 'name')
layout.prop(platform, 'custom_player_path')
layout.prop(platform, 'custom_launcher_path')
else:
layout.prop(platform, 'default_architecture', expand=True, event=True)
layout.prop(platform, 'name')
download = DownloadPlatformResource.thread
display = (
download.message and
download.system == platform.type_build and
download.architecture == platform.default_architecture
)
layout.prop(platform, 'default_player')
if display and download.type == 'player':
split = layout.split(0.33)
split.separator()
split.label(download.message)
elif not self.player_exist:
split = layout.split(0.33)
split.separator()
split.operator('scene.build_game_download_platform_resource').type="player"
split.active = not download.isAlive()
layout.prop(platform, 'default_launcher')
split = layout.split(0.33)
split.separator()
if display and download.type == 'launcher':
split.label(download.message)
elif not self.launcher_exist:
split.operator('scene.build_game_download_platform_resource').type="launcher"
split.active = not DownloadPlatformResource.thread.isAlive()
if platform.type_build == 'mac':
layout.label("Warning! MacOS not tested.")
row = layout.row()
# Not implemented yet!
#row.prop(platform, 'build_unpack')
#row.prop(platform, 'build_merge')
class RENDER_PT_Tools(bpy.types.Panel):
bl_label = "Build Game Tools"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "render"
@classmethod
def poll(cls, context):
scene = context.scene
return scene and (scene.render.engine == "BLENDER_GAME")
def draw(self, context):
ts = context.scene.ge_tools_settings
self.layout.prop(ts, 'extract_executable_path')
self.layout.operator("export.build_extract_blend")
###################################### OPERATORS #########################################
#
#class BuildGameOp(bpy.types.Operator):
# bl_idname = "export.build_game"
# bl_label = "Build Game"
#
# def execute(self, context):
#
# print("Coping files...")
#
# original = bpy.data.filepath
# blend_name = bpy.path.basename(original)
#
# filepath = bpy.path.abspath(context.scene.build_filepath)
# if filepath == "": print("You need to select a file path")
#
# else:
# if context.scene.game_name == "": context.scene.game_name = blend_name
# if context.scene.game_name == blend_name: context.scene.game_name = blend_name.replace(".blend", "")
# if bpy.path.basename(filepath) != "": filepath = filepath.replace(bpy.path.basename(filepath), "")
#
# shutil.copy2(bpy.path.abspath(context.scene.build_player_path), filepath)
# shutil.copy2(os.path.dirname(os.path.abspath(__file__))+"/game_engine_build/config", filepath)
# shutil.copy2(os.path.dirname(os.path.abspath(__file__))+"/game_engine_build/mygame", filepath+context.scene.game_name)
# if os.path.exists(filepath+"/lib/") == False: shutil.copytree(os.path.dirname(os.path.abspath(__file__))+"/../../python/lib/",filepath+"lib/")
#
# #Copy .blend and resources
# print("Coping resources...")
# if (original!=""): bpy.ops.wm.save_mainfile() #Save changes before do anything.
# bpy.ops.file.pack_all() #Pack all (don't worry, this won't be saved in the original file)
# bpy.ops.wm.save_mainfile(filepath=filepath+blend_name) #Copy the file packed and open it.
# if context.scene.build_unpack == True:
# bpy.ops.file.unpack_all(method="USE_LOCAL") #Unpack the file
# bpy.ops.wm.save_mainfile(check_existing=False) #Save the changes to the copy
# if os.path.isfile(filepath+blend_name + "1"): #Remove the security copy if exists. (Say "remove security" and everybody loses their minds)
# os.remove(filepath+blend_name + "1")
# if (original!=""): bpy.ops.wm.open_mainfile(filepath=original) #Reopen the original file.
#
# #Configure the config file.
# print("Configuring...")
# f = open(os.path.dirname(os.path.abspath(__file__))+"/game_engine_build/config","r")
# conf = f.read(); f.close()
# conf = conf.replace("player:", "player: " + bpy.path.basename(context.scene.build_player_path))
# conf = conf.replace("blend:", "blend: " + blend_name)
# m = open(filepath+"/config", "w")
# m.write(conf); m.close()
#
# print("Done.")
# return {'FINISHED'}
class BuildGame(bpy.types.Operator):
bl_idname = "export.build_game"
bl_label = "Build"
thread = PlatformInstall()
def execute(self, context):
bs = context.scene.ge_build_settings
#If .blend not exist, save it.
if not bpy.data.filepath:
bpy.ops.wm.save_mainfile()
# Fill properties
blend_name = bpy.path.basename(bpy.data.filepath).replace(".blend", "")
if not blend_name: blend_name = "mygame"
self.thread.game_name = bs.game_name
if self.thread.game_name == "": self.thread.game_name = blend_name
self.thread.game_directory = bpy.path.abspath(bs.build_filepath)
if self.thread.game_directory == "":
print("BUILD GAME #005: You must select a directory first.")
return {'CANCELLED'}
filepath = self.thread.game_directory
if bpy.path.basename(filepath):
self.thread.game_directory = filepath.replace(bpy.path.basename(filepath), "")
# Run thread
if not self.thread.isAlive():
self.thread.context = context
self.thread.__init__()
self.thread.start()
#self.report({'INFO'}, message)
return {'FINISHED'}
class BuildGameCancelButton(bpy.types.Operator):
bl_idname = "export.build_game_cancel"
bl_label = "Cancel"
def execute(self, context):
BuildGame.thread.stopMe()
BuildGame.thread.status = ""
return {'FINISHED'}
class DownloadPlatformResource(bpy.types.Operator):
bl_idname = "scene.build_game_download_platform_resource"
bl_label = "Download"
type = bpy.props.StringProperty()
thread = DownloadProcess()
def execute(self, context):
bs = context.scene.ge_build_settings
platform = bs.platforms[bs.platforms_active]
if not self.thread.isAlive():
self.thread.system = platform.type_build
self.thread.architecture = platform.default_architecture
self.thread.type = self.type
self.thread.context = context
if self.type == 'player':
self.thread.item = platform.default_player
if platform.default_player == 'blenderplayer':
self.thread.official = True
if self.type == 'launcher':
self.thread.item = platform.default_launcher
self.thread.__init__()
self.thread.start()
return {'FINISHED'}
class BuildAddDefaultPlatform(bpy.types.Operator):
bl_idname = "scene.build_add_default_platform"
bl_label = "Add Platform Default"
def execute(self, context):
platform = context.scene.ge_build_settings.platforms.add()
blender_bin_path = bpy.app.binary_path
blender_bin_dir = os.path.dirname(blender_bin_path)
ext = os.path.splitext(blender_bin_path)[-1].lower()
platform.name = bpy.app.build_platform.decode("utf-8").replace(":","_")
if platform.name == 'Linux_32bit':
platform.type_build = 'lin'
platform.default_architecture = 'x32'
if platform.name == 'Linux_64bit':
platform.type_build = 'lin'
platform.default_architecture = 'x64'
if platform.name == 'Windows_32bit':
platform.type_build = 'win'
platform.default_architecture = 'x32'
if platform.name == 'Windows_64bit':
platform.type_build = 'win'
platform.default_architecture = 'x64'
if platform.name == 'Mac_32bit':
platform.type_build = 'mac'
platform.default_architecture = 'x32'
if platform.name == 'Mac_64bit':
platform.type_build = 'mac'
platform.default_architecture = 'x64'
check_add(self, context)
return {'FINISHED'}
class BuildAddPlatform(bpy.types.Operator):
bl_idname = "scene.build_add_platform"
bl_label = "Add Platform"
def execute(self, context):
platforms = context.scene.ge_build_settings.platforms
if len(platforms) > 0:
a = platforms.add()
a.name = a.name
else:
BuildAddDefaultPlatform.execute(self, context)
check_add(self, context)
return {'FINISHED'}
class BuildRemovePlatform(bpy.types.Operator):
bl_idname = "scene.build_remove_platform"
bl_label = "Remove Platform"
def execute(self, context):
bs = context.scene.ge_build_settings
if bs.platforms_active < len(bs.platforms):
bs.platforms.remove(bs.platforms_active)
RENDER_PT_Platforms.enabled = True
if bs.platforms_active == len(bs.platforms) > 0:
bs.platforms_active -= 1
if len(bs.platforms): check(bs.platforms[bs.platforms_active], context)
return {'FINISHED'}