-
Notifications
You must be signed in to change notification settings - Fork 0
/
deflate.py
executable file
·106 lines (89 loc) · 3.35 KB
/
deflate.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
#!/usr/bin/env python3
import glob
import os.path
import zipfile
import shutil
import math
dir_name = ""
inlcuded_files = []
logfile = "./log/live.log"
current_dir = ""
def log(str):
return 0
with open(logfile, 'a') as f:
f.write("[Save deflation] " + str + "\n")
def parse_URI(line, cdir):
line = line.replace("{","").replace("}","").replace(",","").replace("(", "").replace(")"," ").replace("\"","").replace(".","/").replace("'", "")
line = line[(line.find("require") + 7):].strip()
end_of_file_name = line.find(" ")
if end_of_file_name > -1:
line = line[:end_of_file_name]
line = line + ".lua"
#relative path
URI = cdir + line
if os.path.exists(URI) and URI not in inlcuded_files:
inlcuded_files.append(URI)
parse_file(URI)
#absolute path
URI = dir_name + line
if os.path.exists(URI) and URI not in inlcuded_files:
inlcuded_files.append(URI)
parse_file(URI)
def parse_line(line, cdir):
if "require" in line:
if not "--" in line or line.find("--") > line.find("require"):
parse_URI(line, cdir)
def parse_file(file_name):
cdir = os.path.dirname(file_name) + "/"
try:
f = open(file_name, 'r')
lines = f.readlines()
f.close()
except KeyboardInterrupt as e:
sys.exit(0)
except Exception as e:
print(e)
for line in lines:
parse_line(line.rstrip(), cdir)
def unzip(save_name):
global dir_name
dir_name = os.path.join(os.path.dirname(save_name),"temp")
if os.path.isdir(dir_name):
log("Deleting directory " + dir_name)
shutil.rmtree(dir_name)
zip_ref = zipfile.ZipFile(save_name, 'r')
zip_ref.extractall(os.path.join(os.path.dirname(save_name), "temp"))
zip_ref.close()
dir_name = os.path.join(dir_name, next(os.walk(dir_name))[1][0]) + "/"
def zip(dir_name, save_name):
to_zip = os.path.join(os.path.dirname(save_name), "to_zip")
if not os.path.exists(to_zip):
os.makedirs(to_zip)
shutil.move(dir_name, to_zip)
shutil.make_archive(save_name[:-4], 'zip', to_zip)
if os.path.isdir(dir_name):
shutil.rmtree(dir_name)
if os.path.isdir(to_zip):
shutil.rmtree(to_zip)
def remove_unwanted_files():
for filename in glob.iglob(dir_name + '**/*', recursive=True):
if filename not in inlcuded_files and os.path.isfile(filename) and filename.find("locale/") == -1:
log("Removing " + filename)
with open(filename, "w") as f:
f.write("This file was removed to reduce save size. If you downloaded this scenario and are interested in downloading the full version, visit github.com/valansch/redmew\n")
def add_excemptions():
global inlcuded_files
inlcuded_files.append(dir_name + "control.lua")
inlcuded_files.append(dir_name + "level-init.dat")
inlcuded_files.append(dir_name + "level.dat")
inlcuded_files.append(dir_name + "script.dat")
inlcuded_files.append(dir_name + "info.json")
def clean_save(save_name, output_name):
file_size_before = os.path.getsize(save_name)
unzip(save_name)
add_excemptions()
parse_file(dir_name + "control.lua")
remove_unwanted_files()
zip(dir_name, output_name)
file_size_after = os.path.getsize(save_name)
log("File size reduced by " + str(100 - math.floor(1000 * file_size_after / file_size_before)/10) + "%")