From 3a40de482f3faec231b99e87970dbdf87f1dc4c5 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Sat, 10 Aug 2024 10:31:36 +0200 Subject: [PATCH] Fix icon *.import files on scons build Enables editor scaling and color conversion in LimboAI icon imports. This won't work until icons are actually imported in the godot editor, but that's okay - it's a convenience feature. --- SConstruct | 17 +++-- gdextension/fix_demo_imports.sh | 51 -------------- gdextension/fix_icon_imports.py | 114 ++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 55 deletions(-) delete mode 100755 gdextension/fix_demo_imports.sh create mode 100755 gdextension/fix_icon_imports.py diff --git a/SConstruct b/SConstruct index c3a9e6c..b4fa6e7 100644 --- a/SConstruct +++ b/SConstruct @@ -15,6 +15,10 @@ import sys import subprocess from limboai_version import generate_module_version_header, godot_cpp_ref +sys.path.append("gdextension") +from update_icon_entries import update_icon_entries +from fix_icon_imports import fix_icon_imports + # Check if godot-cpp/ exists if not os.path.exists("godot-cpp"): print("Directory godot-cpp/ not found. Cloning repository...") @@ -85,12 +89,17 @@ generate_module_version_header() # Update icon entries in limboai.gdextension file. # Note: This will remove everything after [icons] section, and rebuild it with generated icon entries. -sys.path.append("gdextension") -from update_icon_entries import update_icon_entries - print("Updating LimboAI icon entries...") update_icon_entries(silent=True) -sys.path.remove("gdextension") + +# Fix icon imports in the PROJECT/addons/limboai/icons/. +# Enables scaling and color conversion in the editor for imported SVG icons. +try: + fix_icon_imports(project_dir) +except FileNotFoundError as e: + print(e) +except Exception as e: + print("Unknown error: " + str(e)) # Tweak this if you want to use different folders, or more folders, to store your source code in. env.Append(CPPDEFINES=["LIMBOAI_GDEXTENSION"]) diff --git a/gdextension/fix_demo_imports.sh b/gdextension/fix_demo_imports.sh deleted file mode 100755 index 278605e..0000000 --- a/gdextension/fix_demo_imports.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/bash - -## This script fixes icon imports in the demo project. -## It enables scaling and color conversion for SVG icons in the demo project. -## -## Dependencies: bash, sed, find. - -# Colors -HIGHLIGHT_COLOR='\033[1;36m' # Light Cyan -NC='\033[0m' # No Color -ERROR_COLOR="\033[0;31m" # Red - -usage() { echo -e "Usage: $0 [--silent]\nRun from limboai root directory!"; } - -msg () { echo -e "$@"; } -highlight() { echo -e "${HIGHLIGHT_COLOR}$@${NC}"; } -error () { echo -e "${ERROR_COLOR}$@${NC}" >&2; } - -# Exit if a command returns non-zero status. -set -e - -if [ ! -d "${PWD}/demo/" ]; then - error Aborting: \"demo\" subdirectory is not found. - msg Tip: Run this script from the limboai root directory. - msg Command: bash ./gdextension/fix_demo_imports.sh - exit 1 -fi - -if test -z "$(find demo/addons/limboai/icons/ -maxdepth 1 -name '*.svg' -print -quit)"; then - error "No icons found in the demo project!" - msg Make sure to copy/link the icons into the demo project \(icons/ -\> demo/addons/limboai/icons/\). - exit 1 -fi - -if test -z "$(find demo/addons/limboai/icons/ -maxdepth 1 -name '*.import' -print -quit)"; then - error "No icon import files found!" - msg Make sure to open the demo project in Godot Editor before running this script. - exit 1 -fi - -highlight "--- Listing icons dir:" -ls demo/addons/limboai/icons/ -highlight "---" - -highlight Applying scale settings... -sed -i 's|editor/scale_with_editor_scale=false|editor/scale_with_editor_scale=true|' demo/addons/limboai/icons/*.import - -highlight Applying color conversion settings... -sed -i 's|editor/convert_colors_with_editor_theme=false|editor/convert_colors_with_editor_theme=true|' demo/addons/limboai/icons/*.import - -highlight Done! diff --git a/gdextension/fix_icon_imports.py b/gdextension/fix_icon_imports.py new file mode 100755 index 0000000..ecfccb6 --- /dev/null +++ b/gdextension/fix_icon_imports.py @@ -0,0 +1,114 @@ +#!/usr/bin/python +""" +Usage: fix_icon_imports.py [--silent] PROJECT_DIR +Fix icon imports in PROJECT_DIR/addons/limboai/icons/. + +Options: + -s, --silent Don't print anything. + -h, --help Print this message. + +Dependencies: python3. + +Use of this source code is governed by an MIT-style +license that can be found in the LICENSE file or at +https://opensource.org/licenses/MIT. +""" + +import os +import glob +import sys +import getopt + + +def usage(): + print(__doc__.strip()) + + +def get_limboai_icon_import_files(project_path="demo/"): + if not os.path.isdir(project_path): + raise FileNotFoundError("Project directory not found: " + project_path) + + icons_path = os.path.join(project_path, "addons/limboai/icons/") + if not os.path.isdir(icons_path): + raise FileNotFoundError("Icons directory not found: " + icons_path) + + return glob.glob(icons_path + "*.import") + + +def fix_import_file(file_path): + if not os.path.isfile(file_path): + raise FileNotFoundError("File not found: " + file_path) + + old_lines = [] + new_lines = [] + + file = open(file_path, "r") + old_lines = file.readlines() + file.close() + + for line in old_lines: + line = line.replace("editor/scale_with_editor_scale=false", "editor/scale_with_editor_scale=true") + line = line.replace( + "editor/convert_colors_with_editor_theme=false", "editor/convert_colors_with_editor_theme=true" + ) + new_lines.append(line) + + if old_lines != new_lines: + file = open(file_path, "w") + for line in new_lines: + file.write(line) + file.close() + return True + return False + + +def fix_icon_imports(project_path="demo/", silent=False): + if not silent: + print("Checking icon import files...") + + project_import_files = get_limboai_icon_import_files(project_path) + + for import_file in project_import_files: + changed = fix_import_file(import_file) + if changed and not silent: + print("Updated icon import file: " + import_file) + + +if __name__ == "__main__": + silent = False + project_path = "demo/" + + try: + opts, args = getopt.getopt(sys.argv[1:], "s", ["silent"]) + except getopt.GetoptError as e: + print( + "%s: %s!\n" + % ( + os.path.basename(__file__), + e.msg, + ) + ) + usage() + sys.exit(2) + + if len(args) > 1: + usage() + sys.exit(2) + elif len(args) == 1: + project_path = args[0] + + for opt, arg in opts: + if opt in ("-h", "--help"): + usage() + sys.exit(0) + elif opt in ("-s", "--silent"): + silent = True + + try: + fix_icon_imports(project_path, silent) + except FileNotFoundError as e: + print(e) + exit(1) + + if not silent: + print("Done!")