-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathyaz0.py
159 lines (113 loc) · 4.01 KB
/
yaz0.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Miyamoto! Level Editor - New Super Mario Bros. U Level Editor
# Copyright (C) 2009-2021 Treeki, Tempus, angelsl, JasonP27, Kinnay,
# MalStar1000, RoadrunnerWMC, MrRean, Grop, AboodXD, Gota7, John10v10,
# mrbengtsson
# This file is part of Miyamoto!.
# Miyamoto! is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Miyamoto! is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Miyamoto!. If not, see <http://www.gnu.org/licenses/>.
# yaz0.py
# Multiple methods for Yaz0 (de)compression
################################################################
################################################################
import os
import platform
import subprocess
import globals
if globals.cython_available:
try:
from libyaz0 import compress
from libyaz0 import decompress
except:
pass
else:
globals.libyaz0_available = True
def determineCompressionMethod():
if globals.libyaz0_available:
return compressLIBYAZ0, decompressLIBYAZ0
else:
return compressWSZST, decompressWSZST
def compressWSZST(inb, outf, level=9):
"""
Compress the file using WSZST
"""
inf = os.path.join(globals.miyamoto_path, 'tmp.tmp')
with open(inf, "wb+") as out:
out.write(inb)
if os.path.isfile(outf):
os.remove(outf)
if platform.system() == 'Windows':
os.chdir(globals.miyamoto_path + '/Tools')
subprocess.call('wszst.exe COMPRESS "' + inf + '" --dest "' + outf + '"', creationflags=0x8)
elif platform.system() == 'Linux':
os.chdir(globals.miyamoto_path + '/linuxTools')
os.system('chmod +x ./wszst_linux.elf')
os.system('./wszst_linux.elf COMPRESS "' + inf + '" --dest "' + outf + '"')
else:
os.chdir(globals.miyamoto_path + '/macTools')
os.system('chmod +x ./wszst_mac')
os.system('./wszst_mac COMPRESS "' + inf + '" --dest "' + outf + '"')
os.chdir(globals.miyamoto_path)
if not os.path.isfile(outf):
return False
return True
def decompressWSZST(inb):
"""
Deompress the data using WSZST
"""
inf = os.path.join(globals.miyamoto_path, 'tmp.tmp')
outf = os.path.join(globals.miyamoto_path, 'tmp2.tmp')
with open(inf, "wb+") as out:
out.write(inb)
if os.path.isfile(outf):
os.remove(outf)
if platform.system() == 'Windows':
os.chdir(globals.miyamoto_path + '/Tools')
subprocess.call('wszst.exe DECOMPRESS "' + inf + '" --dest "' + outf + '"', creationflags=0x8)
elif platform.system() == 'Linux':
os.chdir(globals.miyamoto_path + '/linuxTools')
os.system('chmod +x ./wszst_linux.elf')
os.system('./wszst_linux.elf DECOMPRESS "' + inf + '" --dest "' + outf + '"')
else:
os.chdir(globals.miyamoto_path + '/macTools')
os.system('chmod +x ./wszst_mac')
os.system('./wszst_mac DECOMPRESS "' + inf + '" --dest "' + outf + '"')
os.remove(inf)
os.chdir(globals.miyamoto_path)
if not os.path.isfile(outf):
return None
with open(outf, "rb") as inf_:
data = inf_.read()
os.remove(outf)
return data
def compressLIBYAZ0(inb, outf, level=1):
"""
Compress the file using libyaz0
"""
try:
data = compress(inb, 0, level)
with open(outf, "wb+") as out:
out.write(data)
except:
return False
else:
return True
def decompressLIBYAZ0(inb):
"""
Decompress the file using libyaz0
"""
try:
data = decompress(inb)
except:
return None
else:
return data