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