|
| 1 | +import os.path |
| 2 | + |
| 3 | + |
| 4 | +## See https://github.com/godotengine/godot/blob/master/glsl_builders.py |
| 5 | +def build_raw_header(source_filename: str, constant_name: str) -> None: |
| 6 | + # Read the source file content. |
| 7 | + with open(source_filename, "r") as source_file: |
| 8 | + source_content = source_file.read() |
| 9 | + constant_name = constant_name.replace(".", "_") |
| 10 | + # Build header content using a C raw string literal. |
| 11 | + header_content = ( |
| 12 | + "/* THIS FILE IS GENERATED. EDITS WILL BE LOST. */\n\n" |
| 13 | + "#pragma once\n\n" |
| 14 | + f"inline constexpr const char *{constant_name}" " = " |
| 15 | + f'R"<!>({source_content})<!>"' |
| 16 | + ";\n" |
| 17 | + ) |
| 18 | + # Write the header to the provided file name with a ".gen.h" suffix. |
| 19 | + header_filename = f"{source_filename}.gen.h" |
| 20 | + with open(header_filename, "w") as header_file: |
| 21 | + header_file.write(header_content) |
| 22 | + |
| 23 | + |
| 24 | +def build_raw_headers_action(target, source, env): |
| 25 | + env.NoCache(target) |
| 26 | + for src in source: |
| 27 | + source_filename = str(src) |
| 28 | + # To match Godot, replace ".glsl" with "_shader_glsl". Does nothing for non-GLSL files. |
| 29 | + constant_name = os.path.basename(source_filename).replace(".glsl", "_shader_glsl") |
| 30 | + build_raw_header(source_filename, constant_name) |
| 31 | + |
| 32 | + |
| 33 | +def escape_svg(filename: str) -> str: |
| 34 | + with open(filename, encoding="utf-8", newline="\n") as svg_file: |
| 35 | + svg_content = svg_file.read() |
| 36 | + return f'R"<!>({svg_content})<!>"' |
| 37 | + |
| 38 | + |
| 39 | +## See https://github.com/godotengine/godot/blob/master/editor/icons/editor_icons_builders.py |
| 40 | +## See https://github.com/godotengine/godot/blob/master/scene/theme/icons/default_theme_icons_builders.py |
| 41 | +def make_svg_icons_action(target, source, env): |
| 42 | + destination = str(target[0]) |
| 43 | + constant_prefix = os.path.basename(destination).replace(".gen.h", "") |
| 44 | + svg_icons = [str(x) for x in source] |
| 45 | + # Convert the SVG icons to escaped strings and convert their names to C strings. |
| 46 | + icon_names = [f'"{os.path.basename(fname)[:-4]}"' for fname in svg_icons] |
| 47 | + icon_sources = [escape_svg(fname) for fname in svg_icons] |
| 48 | + # Join them as indented comma-separated items for use in an array initializer. |
| 49 | + icon_names_str = ",\n\t".join(icon_names) |
| 50 | + icon_sources_str = ",\n\t".join(icon_sources) |
| 51 | + # Write the file to disk. |
| 52 | + with open(destination, "w", encoding="utf-8", newline="\n") as destination_file: |
| 53 | + destination_file.write(f"""\ |
| 54 | +/* THIS FILE IS GENERATED. EDITS WILL BE LOST. */ |
| 55 | +
|
| 56 | +#pragma once |
| 57 | +
|
| 58 | +inline constexpr int {constant_prefix}_count = {len(icon_names)}; |
| 59 | +inline constexpr const char *{constant_prefix}_sources[] = {{ |
| 60 | + {icon_sources_str} |
| 61 | +}}; |
| 62 | +
|
| 63 | +inline constexpr const char *{constant_prefix}_names[] = {{ |
| 64 | + {icon_names_str} |
| 65 | +}}; |
| 66 | +""") |
0 commit comments