diff options
Diffstat (limited to 'editor')
-rw-r--r-- | editor/SCsub | 20 | ||||
-rw-r--r-- | editor/editor_builders.py | 185 | ||||
-rw-r--r-- | editor/icons/editor_icons_builders.py | 133 | ||||
-rw-r--r-- | editor/template_builders.py | 62 | ||||
-rw-r--r-- | editor/themes/editor_theme_builders.py | 33 |
5 files changed, 210 insertions, 223 deletions
diff --git a/editor/SCsub b/editor/SCsub index 67ded244cf..442d0a3b75 100644 --- a/editor/SCsub +++ b/editor/SCsub @@ -11,17 +11,15 @@ import editor_builders def _make_doc_data_class_path(to_path): # NOTE: It is safe to generate this file here, since this is still executed serially - g = open(os.path.join(to_path, "doc_data_class_path.gen.h"), "w", encoding="utf-8", newline="\n") - g.write("static const int _doc_data_class_path_count = " + str(len(env.doc_class_path)) + ";\n") - g.write("struct _DocDataClassPath { const char* name; const char* path; };\n") - - g.write("static const _DocDataClassPath _doc_data_class_paths[" + str(len(env.doc_class_path) + 1) + "] = {\n") - for c in sorted(env.doc_class_path): - g.write('\t{"' + c + '", "' + env.doc_class_path[c] + '"},\n') - g.write("\t{nullptr, nullptr}\n") - g.write("};\n") - - g.close() + with open(os.path.join(to_path, "doc_data_class_path.gen.h"), "w", encoding="utf-8", newline="\n") as g: + g.write("static const int _doc_data_class_path_count = " + str(len(env.doc_class_path)) + ";\n") + g.write("struct _DocDataClassPath { const char* name; const char* path; };\n") + + g.write("static const _DocDataClassPath _doc_data_class_paths[" + str(len(env.doc_class_path) + 1) + "] = {\n") + for c in sorted(env.doc_class_path): + g.write('\t{"' + c + '", "' + env.doc_class_path[c] + '"},\n') + g.write("\t{nullptr, nullptr}\n") + g.write("};\n") if env.editor_build: diff --git a/editor/editor_builders.py b/editor/editor_builders.py index 7cac984129..0189d7e9d4 100644 --- a/editor/editor_builders.py +++ b/editor/editor_builders.py @@ -16,116 +16,113 @@ from platform_methods import subprocess_main def make_doc_header(target, source, env): dst = target[0] - g = open(dst, "w", encoding="utf-8", newline="\n") - buf = "" - docbegin = "" - docend = "" - for src in source: - if not src.endswith(".xml"): - continue - with open(src, "r", encoding="utf-8") as f: - content = f.read() - buf += content - - buf = (docbegin + buf + docend).encode("utf-8") - decomp_size = len(buf) - - # Use maximum zlib compression level to further reduce file size - # (at the cost of initial build times). - buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION) - - g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") - g.write("#ifndef _DOC_DATA_RAW_H\n") - g.write("#define _DOC_DATA_RAW_H\n") - g.write('static const char *_doc_data_hash = "' + str(hash(buf)) + '";\n') - g.write("static const int _doc_data_compressed_size = " + str(len(buf)) + ";\n") - g.write("static const int _doc_data_uncompressed_size = " + str(decomp_size) + ";\n") - g.write("static const unsigned char _doc_data_compressed[] = {\n") - for i in range(len(buf)): - g.write("\t" + str(buf[i]) + ",\n") - g.write("};\n") - - g.write("#endif") - - g.close() + with open(dst, "w", encoding="utf-8", newline="\n") as g: + buf = "" + docbegin = "" + docend = "" + for src in source: + if not src.endswith(".xml"): + continue + with open(src, "r", encoding="utf-8") as f: + content = f.read() + buf += content + + buf = (docbegin + buf + docend).encode("utf-8") + decomp_size = len(buf) + + # Use maximum zlib compression level to further reduce file size + # (at the cost of initial build times). + buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION) + + g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") + g.write("#ifndef _DOC_DATA_RAW_H\n") + g.write("#define _DOC_DATA_RAW_H\n") + g.write('static const char *_doc_data_hash = "' + str(hash(buf)) + '";\n') + g.write("static const int _doc_data_compressed_size = " + str(len(buf)) + ";\n") + g.write("static const int _doc_data_uncompressed_size = " + str(decomp_size) + ";\n") + g.write("static const unsigned char _doc_data_compressed[] = {\n") + for i in range(len(buf)): + g.write("\t" + str(buf[i]) + ",\n") + g.write("};\n") + + g.write("#endif") def make_translations_header(target, source, env, category): dst = target[0] - g = open(dst, "w", encoding="utf-8", newline="\n") - - g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") - g.write("#ifndef _{}_TRANSLATIONS_H\n".format(category.upper())) - g.write("#define _{}_TRANSLATIONS_H\n".format(category.upper())) + with open(dst, "w", encoding="utf-8", newline="\n") as g: + g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") + g.write("#ifndef _{}_TRANSLATIONS_H\n".format(category.upper())) + g.write("#define _{}_TRANSLATIONS_H\n".format(category.upper())) - sorted_paths = sorted(source, key=lambda path: os.path.splitext(os.path.basename(path))[0]) + sorted_paths = sorted(source, key=lambda path: os.path.splitext(os.path.basename(path))[0]) - msgfmt_available = shutil.which("msgfmt") is not None + msgfmt_available = shutil.which("msgfmt") is not None - if not msgfmt_available: - print("WARNING: msgfmt is not found, using .po files instead of .mo") + if not msgfmt_available: + print("WARNING: msgfmt is not found, using .po files instead of .mo") - xl_names = [] - for i in range(len(sorted_paths)): - if msgfmt_available: - mo_path = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex + ".mo") - cmd = "msgfmt " + sorted_paths[i] + " --no-hash -o " + mo_path - try: - subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate() - with open(mo_path, "rb") as f: - buf = f.read() - except OSError as e: - print( - "WARNING: msgfmt execution failed, using .po file instead of .mo: path=%r; [%s] %s" - % (sorted_paths[i], e.__class__.__name__, e) - ) - with open(sorted_paths[i], "rb") as f: - buf = f.read() - finally: + xl_names = [] + for i in range(len(sorted_paths)): + if msgfmt_available: + mo_path = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex + ".mo") + cmd = "msgfmt " + sorted_paths[i] + " --no-hash -o " + mo_path try: - os.remove(mo_path) + subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate() + with open(mo_path, "rb") as f: + buf = f.read() except OSError as e: - # Do not fail the entire build if it cannot delete a temporary file print( - "WARNING: Could not delete temporary .mo file: path=%r; [%s] %s" - % (mo_path, e.__class__.__name__, e) + "WARNING: msgfmt execution failed, using .po file instead of .mo: path=%r; [%s] %s" + % (sorted_paths[i], e.__class__.__name__, e) ) - else: - with open(sorted_paths[i], "rb") as f: - buf = f.read() - - decomp_size = len(buf) - # Use maximum zlib compression level to further reduce file size - # (at the cost of initial build times). - buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION) - name = os.path.splitext(os.path.basename(sorted_paths[i]))[0] - - g.write("static const unsigned char _{}_translation_{}_compressed[] = {{\n".format(category, name)) - for j in range(len(buf)): - g.write("\t" + str(buf[j]) + ",\n") + with open(sorted_paths[i], "rb") as f: + buf = f.read() + finally: + try: + os.remove(mo_path) + except OSError as e: + # Do not fail the entire build if it cannot delete a temporary file + print( + "WARNING: Could not delete temporary .mo file: path=%r; [%s] %s" + % (mo_path, e.__class__.__name__, e) + ) + else: + with open(sorted_paths[i], "rb") as f: + buf = f.read() + decomp_size = len(buf) + # Use maximum zlib compression level to further reduce file size + # (at the cost of initial build times). + buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION) + name = os.path.splitext(os.path.basename(sorted_paths[i]))[0] + + g.write("static const unsigned char _{}_translation_{}_compressed[] = {{\n".format(category, name)) + for j in range(len(buf)): + g.write("\t" + str(buf[j]) + ",\n") + + g.write("};\n") + + xl_names.append([name, len(buf), str(decomp_size)]) + + g.write("struct {}TranslationList {{\n".format(category.capitalize())) + g.write("\tconst char* lang;\n") + g.write("\tint comp_size;\n") + g.write("\tint uncomp_size;\n") + g.write("\tconst unsigned char* data;\n") + g.write("};\n\n") + g.write("static {}TranslationList _{}_translations[] = {{\n".format(category.capitalize(), category)) + for x in xl_names: + g.write( + '\t{{ "{}", {}, {}, _{}_translation_{}_compressed }},\n'.format( + x[0], str(x[1]), str(x[2]), category, x[0] + ) + ) + g.write("\t{nullptr, 0, 0, nullptr}\n") g.write("};\n") - xl_names.append([name, len(buf), str(decomp_size)]) - - g.write("struct {}TranslationList {{\n".format(category.capitalize())) - g.write("\tconst char* lang;\n") - g.write("\tint comp_size;\n") - g.write("\tint uncomp_size;\n") - g.write("\tconst unsigned char* data;\n") - g.write("};\n\n") - g.write("static {}TranslationList _{}_translations[] = {{\n".format(category.capitalize(), category)) - for x in xl_names: - g.write( - '\t{{ "{}", {}, {}, _{}_translation_{}_compressed }},\n'.format(x[0], str(x[1]), str(x[2]), category, x[0]) - ) - g.write("\t{nullptr, 0, 0, nullptr}\n") - g.write("};\n") - - g.write("#endif") - - g.close() + g.write("#endif") def make_editor_translations_header(target, source, env): diff --git a/editor/icons/editor_icons_builders.py b/editor/icons/editor_icons_builders.py index 4fe74881ed..3b2d8714d8 100644 --- a/editor/icons/editor_icons_builders.py +++ b/editor/icons/editor_icons_builders.py @@ -15,81 +15,76 @@ def make_editor_icons_action(target, source, env): dst = target[0] svg_icons = source - icons_string = StringIO() + with StringIO() as icons_string, StringIO() as s: + for f in svg_icons: + fname = str(f) - for f in svg_icons: - fname = str(f) + icons_string.write('\t"') - icons_string.write('\t"') - - with open(fname, "rb") as svgf: - b = svgf.read(1) - while len(b) == 1: - icons_string.write("\\" + str(hex(ord(b)))[1:]) + with open(fname, "rb") as svgf: b = svgf.read(1) + while len(b) == 1: + icons_string.write("\\" + str(hex(ord(b)))[1:]) + b = svgf.read(1) + + icons_string.write('"') + if fname != svg_icons[-1]: + icons_string.write(",") + icons_string.write("\n") + + s.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") + s.write("#ifndef _EDITOR_ICONS_H\n") + s.write("#define _EDITOR_ICONS_H\n") + s.write("static const int editor_icons_count = {};\n".format(len(svg_icons))) + s.write("static const char *editor_icons_sources[] = {\n") + s.write(icons_string.getvalue()) + s.write("};\n\n") + s.write("static const char *editor_icons_names[] = {\n") + + # this is used to store the indices of thumbnail icons + thumb_medium_indices = [] + thumb_big_indices = [] + index = 0 + for f in svg_icons: + fname = str(f) + + # Trim the `.svg` extension from the string. + icon_name = os.path.basename(fname)[:-4] + # some special cases + if icon_name.endswith("MediumThumb"): # don't know a better way to handle this + thumb_medium_indices.append(str(index)) + if icon_name.endswith("BigThumb"): # don't know a better way to handle this + thumb_big_indices.append(str(index)) + if icon_name.endswith("GodotFile"): # don't know a better way to handle this + thumb_big_indices.append(str(index)) + + s.write('\t"{0}"'.format(icon_name)) + + if fname != svg_icons[-1]: + s.write(",") + s.write("\n") + + index += 1 - icons_string.write('"') - if fname != svg_icons[-1]: - icons_string.write(",") - icons_string.write("\n") - - s = StringIO() - s.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") - s.write("#ifndef _EDITOR_ICONS_H\n") - s.write("#define _EDITOR_ICONS_H\n") - s.write("static const int editor_icons_count = {};\n".format(len(svg_icons))) - s.write("static const char *editor_icons_sources[] = {\n") - s.write(icons_string.getvalue()) - s.write("};\n\n") - s.write("static const char *editor_icons_names[] = {\n") - - # this is used to store the indices of thumbnail icons - thumb_medium_indices = [] - thumb_big_indices = [] - index = 0 - for f in svg_icons: - fname = str(f) - - # Trim the `.svg` extension from the string. - icon_name = os.path.basename(fname)[:-4] - # some special cases - if icon_name.endswith("MediumThumb"): # don't know a better way to handle this - thumb_medium_indices.append(str(index)) - if icon_name.endswith("BigThumb"): # don't know a better way to handle this - thumb_big_indices.append(str(index)) - if icon_name.endswith("GodotFile"): # don't know a better way to handle this - thumb_big_indices.append(str(index)) - - s.write('\t"{0}"'.format(icon_name)) - - if fname != svg_icons[-1]: - s.write(",") - s.write("\n") - - index += 1 - - s.write("};\n") - - if thumb_medium_indices: - s.write("\n\n") - s.write("static const int editor_md_thumbs_count = {};\n".format(len(thumb_medium_indices))) - s.write("static const int editor_md_thumbs_indices[] = {") - s.write(", ".join(thumb_medium_indices)) - s.write("};\n") - if thumb_big_indices: - s.write("\n\n") - s.write("static const int editor_bg_thumbs_count = {};\n".format(len(thumb_big_indices))) - s.write("static const int editor_bg_thumbs_indices[] = {") - s.write(", ".join(thumb_big_indices)) s.write("};\n") - s.write("#endif\n") - - with open(dst, "w", encoding="utf-8", newline="\n") as f: - f.write(s.getvalue()) - - s.close() - icons_string.close() + if thumb_medium_indices: + s.write("\n\n") + s.write("static const int editor_md_thumbs_count = {};\n".format(len(thumb_medium_indices))) + s.write("static const int editor_md_thumbs_indices[] = {") + s.write(", ".join(thumb_medium_indices)) + s.write("};\n") + if thumb_big_indices: + s.write("\n\n") + s.write("static const int editor_bg_thumbs_count = {};\n".format(len(thumb_big_indices))) + s.write("static const int editor_bg_thumbs_indices[] = {") + s.write(", ".join(thumb_big_indices)) + s.write("};\n") + + s.write("#endif\n") + + with open(dst, "w", encoding="utf-8", newline="\n") as f: + f.write(s.getvalue()) if __name__ == "__main__": diff --git a/editor/template_builders.py b/editor/template_builders.py index d5932a08fe..c79c9bd8af 100644 --- a/editor/template_builders.py +++ b/editor/template_builders.py @@ -54,41 +54,41 @@ def parse_template(inherits, source, delimiter): def make_templates(target, source, env): dst = target[0] - s = StringIO() - s.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n\n") - s.write("#ifndef _CODE_TEMPLATES_H\n") - s.write("#define _CODE_TEMPLATES_H\n\n") - s.write('#include "core/object/object.h"\n') - s.write('#include "core/object/script_language.h"\n') - - delimiter = "#" # GDScript single line comment delimiter by default. - if source: - ext = os.path.splitext(source[0])[1] - if ext == ".cs": - delimiter = "//" - - parsed_template_string = "" - number_of_templates = 0 - - for filepath in source: - node_name = os.path.basename(os.path.dirname(filepath)) - parsed_template = parse_template(node_name, filepath, delimiter) - parsed_template_string += "\t" + parsed_template - number_of_templates += 1 - - s.write("\nstatic const int TEMPLATES_ARRAY_SIZE = " + str(number_of_templates) + ";\n") - s.write("\nstatic const struct ScriptLanguage::ScriptTemplate TEMPLATES[" + str(number_of_templates) + "] = {\n") - - s.write(parsed_template_string) + with StringIO() as s: + s.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n\n") + s.write("#ifndef _CODE_TEMPLATES_H\n") + s.write("#define _CODE_TEMPLATES_H\n\n") + s.write('#include "core/object/object.h"\n') + s.write('#include "core/object/script_language.h"\n') + + delimiter = "#" # GDScript single line comment delimiter by default. + if source: + ext = os.path.splitext(source[0])[1] + if ext == ".cs": + delimiter = "//" + + parsed_template_string = "" + number_of_templates = 0 + + for filepath in source: + node_name = os.path.basename(os.path.dirname(filepath)) + parsed_template = parse_template(node_name, filepath, delimiter) + parsed_template_string += "\t" + parsed_template + number_of_templates += 1 + + s.write("\nstatic const int TEMPLATES_ARRAY_SIZE = " + str(number_of_templates) + ";\n") + s.write( + "\nstatic const struct ScriptLanguage::ScriptTemplate TEMPLATES[" + str(number_of_templates) + "] = {\n" + ) - s.write("};\n") + s.write(parsed_template_string) - s.write("\n#endif\n") + s.write("};\n") - with open(dst, "w", encoding="utf-8", newline="\n") as f: - f.write(s.getvalue()) + s.write("\n#endif\n") - s.close() + with open(dst, "w", encoding="utf-8", newline="\n") as f: + f.write(s.getvalue()) if __name__ == "__main__": diff --git a/editor/themes/editor_theme_builders.py b/editor/themes/editor_theme_builders.py index b503c37c4b..399ff16a1d 100644 --- a/editor/themes/editor_theme_builders.py +++ b/editor/themes/editor_theme_builders.py @@ -12,29 +12,26 @@ from platform_methods import subprocess_main def make_fonts_header(target, source, env): dst = target[0] - g = open(dst, "w", encoding="utf-8", newline="\n") + with open(dst, "w", encoding="utf-8", newline="\n") as g: + g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") + g.write("#ifndef _EDITOR_FONTS_H\n") + g.write("#define _EDITOR_FONTS_H\n") - g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") - g.write("#ifndef _EDITOR_FONTS_H\n") - g.write("#define _EDITOR_FONTS_H\n") + # Saving uncompressed, since FreeType will reference from memory pointer. + for i in range(len(source)): + with open(source[i], "rb") as f: + buf = f.read() - # Saving uncompressed, since FreeType will reference from memory pointer. - for i in range(len(source)): - with open(source[i], "rb") as f: - buf = f.read() + name = os.path.splitext(os.path.basename(source[i]))[0] - name = os.path.splitext(os.path.basename(source[i]))[0] + g.write("static const int _font_" + name + "_size = " + str(len(buf)) + ";\n") + g.write("static const unsigned char _font_" + name + "[] = {\n") + for j in range(len(buf)): + g.write("\t" + str(buf[j]) + ",\n") - g.write("static const int _font_" + name + "_size = " + str(len(buf)) + ";\n") - g.write("static const unsigned char _font_" + name + "[] = {\n") - for j in range(len(buf)): - g.write("\t" + str(buf[j]) + ",\n") + g.write("};\n") - g.write("};\n") - - g.write("#endif") - - g.close() + g.write("#endif") if __name__ == "__main__": |