forked from Thalixte/Google-Earth-Decoder-Optimization-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress_built_package.py
281 lines (218 loc) · 10.9 KB
/
compress_built_package.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
######################################################
# script settings
######################################################
# constants
CEND = "\033[0m"
BOLD = "\033[01m"
CRED = "\033[31m"
CGREEN = "\033[32m"
CORANGE = "\033[38;5;214m"
CREDBG = "\033[41m"
CGREENBG = "\033[6;30;42m"
OK = "OK"
KO = "KO"
EOL = "\n"
COMPRESSONATOR_EXE_FILE = "compressonatorcli.exe"
PACKAGES_TEXTURES_SUBFOLDERS = "\\scenery\\global\\scenery\\TEXTURE"
BMP_FORMAT = "BMP"
DDS_FORMAT = "DDS"
BMP_EXTENSION = "." + BMP_FORMAT
DDS_EXTENSION = "." + DDS_FORMAT
BMP_PATTERN = "*." + BMP_FORMAT
DDS_PATTERN = "*." + DDS_FORMAT
DDS_CONVERSION_FORMAT = "DXT1"
BASE_COLOR_INDEX = 0
NB_PARALLEL_TASKS = 20
# reduce number of texture files (Lily Texture Packer addon is necessary https://gumroad.com/l/DFExj)
bake_textures_enabled = True
# folder where the scenery projects are placed
projects_folder = "E:\\MSFSProjects"
# folder of the scenery project you want to optimize
project_name = "My_project"
# folder that contains the compressonator exe that converts dds files
compressonatortool_folder = "compressonator_folder"
# author name
author_name = "author_name"
#######################****************###########################
import sys, bpy, glob, os, shutil, json, uuid, mathutils, math, subprocess
from math import radians, cos, sin, asin, sqrt
from xml.dom.minidom import *
from mathutils import Vector
from xml.dom.minidom import *
from os.path import dirname, join, normpath
from bpy.app import binary_path_python
import xml.etree.ElementTree as ET
class ScriptError(Exception):
def __init__(self, value):
self.value = CREDBG + value + CEND + EOL
def __str__(self):
return repr(CREDBG + self.value + CEND + EOL)
# clear the system console
os.system("cls")
# initial directory
cwd = os.path.dirname(__file__)
project_folder = projects_folder + "\\" + project_name
# built packages folder
built_packages_folder = project_folder + "\\Packages\\"
# built packages texture folder
built_packages_textures_folder = built_packages_folder + project_name.lower() + PACKAGES_TEXTURES_SUBFOLDERS
# backup folder
backup_folder = project_folder + "\\backup"
# backup packages texture folder
backup_packages_textures_folder = backup_folder + "\\packages_textures"
######################################################
# colored print methods
######################################################
def pr_red(skk): print(CRED, format(skk), CEND)
def pr_green(skk): print(CGREEN, format(skk), CEND)
def pr_ko_red(skk): print("-", format(skk), BOLD + CRED, KO, CEND)
def pr_ko_orange(skk): print("-", format(skk), BOLD + CORANGE, KO, CEND)
def pr_ok_green(skk): print("-", format(skk), BOLD + CGREEN, OK, CEND)
def pr_bg_red(skk): print(CREDBG, format(skk), CEND)
def pr_bg_green(skk): print(CGREENBG, format(skk), CEND)
######################################################
# check configuration methods
######################################################
def check_configuration():
error_msg = "Configuration error found ! "
# check if the projects folder exists
if not os.path.isdir(projects_folder):
pr_ko_red ("projects_folder value ")
raise ScriptError(error_msg + "The folder containing your projects (" + projects_folder + ") was not found. Please check the projects_folder value")
pr_ok_green ("projects_folder value ")
# check the projects name
if not os.path.isdir(project_folder):
pr_ko_red ("project_name value ")
raise ScriptError(error_msg + "Project folder " + project_folder + " not found. Please check the project_name value")
pr_ok_green ("project_name value ")
# check if the built packages folder exists
if not os.path.isdir(built_packages_folder):
pr_ko_red ("built_packages folder value ")
raise ScriptError(error_msg + "The folder containing the built packages of the project (" + built_packages_folder + ") was not found. Please check the built_packages_folder value")
pr_ok_green ("built_packages folder value ")
# check if the compressonatorcli.exe file is reachable
if not os.path.isfile(compressonatortool_folder + "\\" + COMPRESSONATOR_EXE_FILE):
pr_ko_red("compressonatortool_folder value ")
build_package_enabled = False
raise ScriptError(error_msg + COMPRESSONATOR_EXE_FILE + " bin file not found. DDS conversion disabled" + CEND + EOL)
pr_ok_green ("compressonatortool_folder value ")
######################################################
# check packages folder configuration methods
######################################################
def check_packages_folder_configuration():
error_msg = "Configuration error found ! "
# check if the built packages textures folder exists
if not os.path.isdir(built_packages_textures_folder):
pr_ko_red ("built_packages_textures folder value")
raise ScriptError(error_msg + "The folder containing the built packages textures of the project (" + built_packages_textures_folder + ") was not found. Please check the built_packages_textures_folder value")
pr_ok_green ("built_packages_textures folder value")
##################################################################
# Backup the packages textures files before the conversion process
##################################################################
def backup_files():
os.chdir(built_packages_textures_folder)
for file in glob.glob(DDS_PATTERN):
file_name = os.path.basename(file)
if not os.path.isfile(backup_packages_textures_folder + "\\" + file_name):
print("backup file ", file_name)
shutil.copyfile(file, backup_packages_textures_folder + "\\" + file_name)
######################################################
# dds texture files conversion methods
######################################################
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
def convert_dds_texture_files():
os.chdir(built_packages_textures_folder)
data = retrieve_texture_files_to_treat(DDS_PATTERN, DDS_EXTENSION, BMP_EXTENSION)
do_convert_dds_texture_files(data)
data = retrieve_texture_files_to_treat(BMP_PATTERN, BMP_EXTENSION, DDS_EXTENSION)
compress_dds_texture_files(data)
# clean bmp files
for bmp_texture_file in glob.glob(BMP_PATTERN):
file_path = os.path.basename(bmp_texture_file)
try:
os.remove(file_path)
except OSError as e:
error_msg = "Error: " + file_path + " : " + e.strerror
raise ScriptError(error_msg)
print("bmp temp texture file:", file_path, "removed")
def retrieve_texture_files_to_treat(texture_files_pattern, texture_files_extension, dest_texture_files_extension):
data = []
for texture_file in glob.glob(texture_files_pattern):
print("-------------------------------------------------------------------------------")
print("texture file: ", os.path.basename(texture_file))
file_name = os.path.splitext(texture_file)[0]
data.append({'file_name': file_name + texture_files_extension, 'dest_file_name': file_name + dest_texture_files_extension})
return chunks(data, NB_PARALLEL_TASKS)
def do_convert_dds_texture_files(data):
ON_POSIX = 'posix' in sys.builtin_module_names
for chunck in data:
# create a pipe to get data
input_fd, output_fd = os.pipe()
processes = [subprocess.Popen([compressonatortool_folder + "\\" + COMPRESSONATOR_EXE_FILE, obj['file_name'], obj['dest_file_name']], stdout=output_fd,
close_fds=ON_POSIX) # close input_fd in children
for obj in chunck]
os.close(output_fd) # close unused end of the pipe
# read output line by line as soon as it is available
with io.open(input_fd, 'r', buffering=1) as file:
for line in file:
print(line, end='')
for p in processes:
p.wait()
def compress_dds_texture_files(data):
ON_POSIX = 'posix' in sys.builtin_module_names
for chunck in data:
# create a pipe to get data
input_fd, output_fd = os.pipe()
processes = [subprocess.Popen([compressonatortool_folder + "\\" + COMPRESSONATOR_EXE_FILE, "-fd", DDS_CONVERSION_FORMAT, obj['file_name'], obj['dest_file_name']], stdout=output_fd,
close_fds=ON_POSIX) # close input_fd in children
for obj in chunck]
os.close(output_fd) # close unused end of the pipe
# read output line by line as soon as it is available
with io.open(input_fd, 'r', buffering=1) as file:
for line in file:
print(line, end='')
for p in processes:
p.wait()
#######################****************###########################
##################################################################
# Main process
##################################################################
def main():
print("-------------------------------------------------------------------------------")
print("----------------------- DDS TEXTURE FILES CONVERSION --------------------------")
print("-------------------------------------------------------------------------------")
convert_dds_texture_files()
#######################****************###########################
##################################################################
# Script
##################################################################
try:
check_configuration()
if not os.path.isdir(built_packages_textures_folder):
built_packages_textures_folder = built_packages_folder + author_name.lower() + "-" + project_name.lower() + PACKAGES_TEXTURES_SUBFOLDERS
check_packages_folder_configuration()
os.chdir(built_packages_folder)
# create the backup folders
if not os.path.isdir(backup_folder):
os.mkdir(backup_folder)
if not os.path.isdir(backup_packages_textures_folder):
os.mkdir(backup_packages_textures_folder)
print("-------------------------------------------------------------------------------")
print("--------------------------------- BACKUP FILES --------------------------------")
print("-------------------------------------------------------------------------------")
# backup_files()
main()
print(EOL)
pr_bg_green("Script correctly applied" + CEND)
except ScriptError as ex:
error_report = "".join(ex.value)
print(EOL + error_report)
pr_bg_red("Script aborted" + CEND)
except RuntimeError as ex:
print(EOL + ex)
pr_bg_red("Script aborted" + CEND)
finally:
os.chdir(cwd)