forked from Podshot/MCEdit-Unified
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcplatform.py
427 lines (361 loc) · 13.8 KB
/
mcplatform.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
"""Copyright (c) 2010-2012 David Rio Vierra
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""
#-# Modified by D.C.-G. for translation purpose
#!# Tests for file chooser
"""
mcplatform.py
Platform-specific functions, folder paths, and the whole fixed/portable nonsense.
"""
import directories
import os
from os.path import dirname, exists, join
import sys
import platform
enc = sys.getfilesystemencoding()
if sys.platform == "win32":
if platform.architecture()[0] == "32bit":
plat = "win32"
if platform.architecture()[0] == "64bit":
plat = "win-amd64"
sys.path.append(join(directories.getDataDir(), "pymclevel", "build", "lib." + plat + "-2.6").encode(enc))
elif sys.platform == 'linux2':
try:
import Xlib.display
import Xlib.X
hasXlibDisplay = True
except ImportError:
hasXlibDisplay = None
os.environ["YAML_ROOT"] = join(directories.getDataDir(), "pymclevel").encode(enc)
from pygame import display
from albow import request_new_filename, request_old_filename
from albow.translate import _
from pymclevel import minecraftSaveFileDir, getMinecraftProfileDirectory, getSelectedProfile
from datetime import datetime
try:
import gtk
if gtk.pygtk_version < (2,3,90):
raise ImportError
hasGtk = True
except ImportError:
hasGtk = False #Using old method as fallback
texturePacksDir = os.path.join(getMinecraftProfileDirectory(getSelectedProfile()), "texturepacks")
#Compatibility layer for filters:
filtersDir = directories.filtersDir
schematicsDir = directories.schematicsDir
#!# Disabling platform specific file chooser:
#!# Please, don't touch these two lines and the 'platChooser' stuff. -- D.C.-G.
platChooser = sys.platform in ('linux2', 'darwin')
#platChooser = sys.platform == 'darwin'
def getTexturePacks():
try:
return os.listdir(texturePacksDir)
except:
return []
# for k,v in os.environ.iteritems():
# try:
# os.environ[k] = v.decode(sys.getfilesystemencoding())
# except:
# continue
if sys.platform == "win32":
try:
from win32 import win32gui
from win32 import win32api
from win32.lib import win32con
except ImportError:
import win32gui
import win32api
import win32con
try:
import win32com.client
from win32com.shell import shell, shellcon # @UnresolvedImport
except:
pass
if sys.platform == 'darwin':
cmd_name = "Cmd"
option_name = "Opt"
else:
cmd_name = "Ctrl"
option_name = "Alt"
def OSXVersionChecker(name,compare):
"""Rediculously complicated function to compare current System version to inputted version."""
if compare != 'gt' and compare != 'lt' and compare != 'eq' and compare != 'gteq' and compare != 'lteq':
print "Invalid version check {}".format(compare)
return False
if sys.platform == 'darwin':
try:
systemVersion = platform.mac_ver()[0].split('.')
if len(systemVersion) == 2:
systemVersion.append('0')
major, minor, patch = 10, 0, 0
if name.lower() == 'cheetah':
minor = 0
patch = 4
elif name.lower() == 'puma':
minor = 1
patch = 5
elif name.lower() == 'jaguar':
minor = 2
patch = 8
elif name.lower() == 'panther':
minor = 3
patch = 9
elif name.lower() == 'tiger':
minor = 4
patch = 11
elif name.lower() == 'snow_leopard':
minor = 5
patch = 8
elif name.lower() == 'snow_leopard':
minor = 6
patch = 8
elif name.lower() == 'lion':
minor = 7
patch = 5
elif name.lower() == 'mountain_lion':
minor = 8
patch = 5
elif name.lower() == 'mavericks':
minor = 9
patch = 5
elif name.lower() == 'yosemite':
minor = 10
patch = 0
else:
major = 0
if int(systemVersion[0]) > int(major):
ret_val = 1
elif int(systemVersion[0]) < int(major):
ret_val = -1
else:
if int(systemVersion[1]) > int(minor):
ret_val = 1
elif int(systemVersion[1]) < int(minor):
ret_val = -1
else:
if int(systemVersion[2]) > int(patch):
ret_val = 1
elif int(systemVersion[2]) < int(patch):
ret_val = -1
else:
ret_val = 0
if ret_val == 0 and (compare == 'eq' or compare == 'gteq' or compare == 'lteq'):
return True
elif ret_val == -1 and (compare == 'lt' or compare == 'lteq'):
return True
elif ret_val == 1 and (compare == 'gt' or compare == 'gteq'):
return True
except:
print "An error occured determining the system version"
return False
else:
return False
lastSchematicsDir = None
lastSaveDir = None
def askOpenFile(title='Select a Minecraft level....', schematics=False, suffixes=["mclevel", "dat", "mine", "mine.gz"]):
global lastSchematicsDir, lastSaveDir
initialDir = lastSaveDir or minecraftSaveFileDir
if schematics:
initialDir = lastSchematicsDir or directories.schematicsDir
def _askOpen(_suffixes):
if schematics:
_suffixes.append("schematic")
_suffixes.append("schematic.gz")
_suffixes.append("zip")
_suffixes.append("inv")
if sys.platform == "win32": #!#
return askOpenFileWin32(title, schematics, initialDir)
elif hasGtk and not platChooser: #!# #Linux (When GTK 2.4 or newer is installed)
return askOpenFileGtk(title, _suffixes, initialDir)
else:
return request_old_filename(suffixes=_suffixes, directory=initialDir)
filename = _askOpen(suffixes)
if filename:
if schematics:
lastSchematicsDir = dirname(filename)
else:
lastSaveDir = dirname(filename)
return filename
def askOpenFileWin32(title, schematics, initialDir):
try:
# if schematics:
f = ('Levels and Schematics\0*.mclevel;*.dat;*.mine;*.mine.gz;*.schematic;*.zip;*.schematic.gz;*.inv\0' +
'*.*\0*.*\0\0')
# else:
# f = ('Levels (*.mclevel, *.dat;*.mine;*.mine.gz;)\0' +
# '*.mclevel;*.dat;*.mine;*.mine.gz;*.zip;*.lvl\0' +
# '*.*\0*.*\0\0')
(filename, customfilter, flags) = win32gui.GetOpenFileNameW(
hwndOwner=display.get_wm_info()['window'],
InitialDir=initialDir,
Flags=(win32con.OFN_EXPLORER
| win32con.OFN_NOCHANGEDIR
| win32con.OFN_FILEMUSTEXIST
| win32con.OFN_LONGNAMES
# |win32con.OFN_EXTENSIONDIFFERENT
),
Title=title,
Filter=f,
)
except Exception:
#print "Open File: ", e
pass
else:
return filename
def askOpenFileGtk(title, suffixes, initialDir):
chooser = gtk.FileChooserDialog(title,
None, gtk.FILE_CHOOSER_ACTION_SAVE,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
chooser.set_default_response(gtk.RESPONSE_OK)
chooser.set_current_folder(initialDir)
chooser.set_current_name("world") #For some reason the Windows isn't closing if this line ins missing or the parameter is ""
#Add custom Filter
filter = gtk.FileFilter()
filter.set_name("Levels and Schematics")
for suffix in suffixes:
filter.add_pattern("*."+suffix)
chooser.add_filter(filter)
#Add "All files" Filter
filter = gtk.FileFilter()
filter.set_name("All files")
filter.add_pattern("*")
chooser.add_filter(filter)
response = chooser.run()
if response == gtk.RESPONSE_OK:
filename = chooser.get_filename()
else:
chooser.destroy()
return # pressed cancel
chooser.destroy()
return filename
def askSaveSchematic(initialDir, displayName, fileFormat):
dt = datetime.now().strftime("%Y-%m-%d--%H-%M-%S")
return askSaveFile(initialDir,
title=_('Save this schematic...'),
defaultName=displayName + "_" + dt + "." + fileFormat,
filetype=_('Minecraft Schematics (*.{0})\0*.{0}\0\0').format(fileFormat),
suffix=fileFormat,
)
def askCreateWorld(initialDir):
defaultName = name = _("Untitled World")
i = 0
while exists(join(initialDir, name)):
i += 1
name = defaultName + " " + str(i)
return askSaveFile(initialDir,
title=_('Name this new world.'),
defaultName=name,
filetype=_('Minecraft World\0*.*\0\0'),
suffix="",
)
def askSaveFile(initialDir, title, defaultName, filetype, suffix):
if sys.platform == "win32": #!#
try:
(filename, customfilter, flags) = win32gui.GetSaveFileNameW(
hwndOwner=display.get_wm_info()['window'],
InitialDir=initialDir,
Flags=win32con.OFN_EXPLORER | win32con.OFN_NOCHANGEDIR | win32con.OFN_OVERWRITEPROMPT,
File=defaultName,
DefExt=suffix,
Title=title,
Filter=filetype,
)
except Exception, e:
print "Error getting file name: ", e
return
try:
filename = filename[:filename.index('\0')]
filename = filename.decode(sys.getfilesystemencoding())
except:
pass
elif hasGtk and not platChooser: #!# #Linux (When GTK 2.4 or newer is installed)
chooser = gtk.FileChooserDialog(title,
None, gtk.FILE_CHOOSER_ACTION_SAVE,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_SAVE, gtk.RESPONSE_OK))
chooser.set_default_response(gtk.RESPONSE_OK)
chooser.set_current_folder(initialDir)
chooser.set_current_name(defaultName)
#Add custom Filter
filter = gtk.FileFilter()
filter.set_name(filetype[:filetype.index("\0")])
filter.add_pattern("*." + suffix)
chooser.add_filter(filter)
#Add "All files" Filter
filter = gtk.FileFilter()
filter.set_name("All files")
filter.add_pattern("*")
chooser.add_filter(filter)
response = chooser.run()
if response == gtk.RESPONSE_OK:
filename = chooser.get_filename()
else:
chooser.destroy()
return # pressed cancel
chooser.destroy()
else: #Fallback
filename = request_new_filename(prompt=title,
suffix=("." + suffix) if suffix else "",
directory=initialDir,
filename=defaultName,
pathname=None)
return filename
# if sys.platform == "win32":
# try:
#
# (filename, customfilter, flags) = win32gui.GetSaveFileNameW(
# hwndOwner = display.get_wm_info()['window'],
# # InitialDir=minecraftSaveFileDir,
# Flags=win32con.OFN_EXPLORER | win32con.OFN_NOCHANGEDIR | win32con.OFN_OVERWRITEPROMPT,
# File=initialDir + os.sep + displayName,
# DefExt=fileFormat,
# Title=,
# Filter=,
# )
# except Exception, e:
# print "Error getting filename: {0!r}".format(e)
# return
#
# elif sys.platform == "darwin" and AppKit is not None:
# sp = AppKit.NSSavePanel.savePanel()
# sp.setDirectory_(initialDir)
# sp.setAllowedFileTypes_([fileFormat])
# # sp.setFilename_(self.editor.level.displayName)
#
# if sp.runModal() == 0:
# return; # pressed cancel
#
# filename = sp.filename()
# AppKit.NSApp.mainWindow().makeKeyWindow()
#
# else:
#
# filename = request_new_filename(prompt = "Save this schematic...",
# suffix = ".{0}".format(fileFormat),
# directory = initialDir,
# filename = displayName,
# pathname = None)
#
# return filename
def platform_open(path):
try:
if sys.platform == "win32":
os.startfile(path)
# os.system('start ' + path + '\'')
elif sys.platform == "darwin":
# os.startfile(path)
os.system('open "' + path + '"')
else:
os.system('xdg-open "' + path + '"')
except Exception, e:
print "platform_open failed on {0}: {1}".format(sys.platform, e)
win32_window_size = True