|
| 1 | +extends Node |
| 2 | + |
| 3 | + |
| 4 | +const ModHookPreprocessorScript := preload("res://addons/mod_loader/_export_plugin/mod_hook_preprocessor.gd") |
| 5 | +static var ModHookPreprocessor |
| 6 | + |
| 7 | + |
| 8 | +func _ready() -> void: |
| 9 | + run_script() |
| 10 | + await get_tree().process_frame |
| 11 | + get_tree().quit() |
| 12 | + |
| 13 | + |
| 14 | +func run_script() -> void: |
| 15 | + ModHookPreprocessor = ModHookPreprocessorScript.new() |
| 16 | + ModHookPreprocessor.process_begin() |
| 17 | + |
| 18 | + # TODO: consider mac location |
| 19 | + var res := OS.get_executable_path().get_base_dir() |
| 20 | + if OS.has_feature("editor"): |
| 21 | + res = ProjectSettings.globalize_path("res://").rsplit("/", true, 2)[0] |
| 22 | + |
| 23 | + var save_base_path := res.path_join("godot_mod_loader/temp_test_mod/") |
| 24 | + print(save_base_path) |
| 25 | + DirAccess.make_dir_recursive_absolute(save_base_path) |
| 26 | + |
| 27 | + var zip_writer := ZIPPacker.new() |
| 28 | + var err := zip_writer.open(save_base_path + "temp_test_mod.zip") |
| 29 | + if err != OK: |
| 30 | + printerr(err) |
| 31 | + |
| 32 | + transform_scripts_recursive(ModHookPreprocessor.process_script, zip_writer, save_base_path) |
| 33 | + |
| 34 | + zip_writer.close() |
| 35 | + ZIPPacker |
| 36 | + |
| 37 | + |
| 38 | +func transform_scripts_recursive(callback: Callable, zip_writer: ZIPPacker, save_base: String, path := "res://") -> void: |
| 39 | + var dir := DirAccess.open(path) |
| 40 | + if not dir: |
| 41 | + printt("An error occurred when trying to access the path:", path) |
| 42 | + return |
| 43 | + |
| 44 | + dir.list_dir_begin() |
| 45 | + var file_name = dir.get_next() |
| 46 | + while file_name != "": |
| 47 | + if path.begins_with("res://addons") or path.begins_with("res://mods-unpacked"): |
| 48 | + file_name = dir.get_next() |
| 49 | + continue |
| 50 | + |
| 51 | + if dir.current_is_dir(): |
| 52 | + transform_scripts_recursive(callback, zip_writer, save_base, dir.get_current_dir() + file_name + "/") |
| 53 | + file_name = dir.get_next() |
| 54 | + continue |
| 55 | + |
| 56 | + if file_name.get_extension() != "gd": |
| 57 | + file_name = dir.get_next() |
| 58 | + continue |
| 59 | + |
| 60 | + var processed: String = callback.call(dir.get_current_dir() + file_name) |
| 61 | + zip_writer.start_file(path.trim_prefix("res://").path_join(file_name)) |
| 62 | + zip_writer.write_file(processed.to_utf8_buffer()) |
| 63 | + zip_writer.close_file() |
| 64 | + |
| 65 | + file_name = dir.get_next() |
0 commit comments