|
| 1 | +class_name _ModLoaderModHookPacker |
| 2 | +extends RefCounted |
| 3 | + |
| 4 | + |
| 5 | +# This class is used to generate mod hooks on demand and pack them into a zip file. |
| 6 | +# Currently all of the included functions are internal and should only be used by the mod loader itself. |
| 7 | + |
| 8 | +const LOG_NAME := "ModLoader:ModHookPacker" |
| 9 | + |
| 10 | + |
| 11 | +static func start() -> void: |
| 12 | + var hook_pre_processor = _ModLoaderModHookPreProcessor.new() |
| 13 | + hook_pre_processor.process_begin() |
| 14 | + |
| 15 | + var mod_hook_pack_path := _ModLoaderPath.get_path_to_hook_pack() |
| 16 | + |
| 17 | + # Create mod hook pack path if necessary |
| 18 | + if not DirAccess.dir_exists_absolute(mod_hook_pack_path.get_base_dir()): |
| 19 | + var error := DirAccess.make_dir_recursive_absolute(mod_hook_pack_path.get_base_dir()) |
| 20 | + if not error == OK: |
| 21 | + ModLoaderLog.error("Error creating the mod hook directory at %s" % mod_hook_pack_path, LOG_NAME) |
| 22 | + return |
| 23 | + ModLoaderLog.debug("Created dir at: %s" % mod_hook_pack_path, LOG_NAME) |
| 24 | + |
| 25 | + # Create mod hook zip |
| 26 | + var zip_writer := ZIPPacker.new() |
| 27 | + var error: Error |
| 28 | + |
| 29 | + if not FileAccess.file_exists(mod_hook_pack_path): |
| 30 | + # Clear cache if the hook pack does not exist |
| 31 | + _ModLoaderCache.remove_data("hooks") |
| 32 | + error = zip_writer.open(mod_hook_pack_path) |
| 33 | + else: |
| 34 | + # If there is a pack already append to it |
| 35 | + error = zip_writer.open(mod_hook_pack_path, ZIPPacker.APPEND_ADDINZIP) |
| 36 | + if not error == OK: |
| 37 | + ModLoaderLog.error("Error(%s) writing to zip file at path: %s" % [error, mod_hook_pack_path], LOG_NAME) |
| 38 | + return |
| 39 | + |
| 40 | + var cache := _ModLoaderCache.get_data("hooks") |
| 41 | + var script_paths_with_hook: Array = [] if cache.is_empty() else cache.script_paths |
| 42 | + var new_hooks_created := false |
| 43 | + |
| 44 | + # Get all scripts that need processing |
| 45 | + ModLoaderLog.debug("Scripts requiring hooks: %s" % [ModLoaderStore.hooked_script_paths.keys()], LOG_NAME) |
| 46 | + for path in ModLoaderStore.hooked_script_paths.keys(): |
| 47 | + if path in script_paths_with_hook: |
| 48 | + continue |
| 49 | + |
| 50 | + var processed_source_code := hook_pre_processor.process_script(path) |
| 51 | + |
| 52 | + zip_writer.start_file(path.trim_prefix("res://")) |
| 53 | + zip_writer.write_file(processed_source_code.to_utf8_buffer()) |
| 54 | + zip_writer.close_file() |
| 55 | + |
| 56 | + ModLoaderLog.debug("Hooks created for script: %s" % path, LOG_NAME) |
| 57 | + new_hooks_created = true |
| 58 | + script_paths_with_hook.push_back(path) |
| 59 | + |
| 60 | + if new_hooks_created: |
| 61 | + _ModLoaderCache.update_data("hooks", {"script_paths": script_paths_with_hook}) |
| 62 | + _ModLoaderCache.save_to_file() |
| 63 | + ModLoader.new_hooks_created.emit() |
| 64 | + |
| 65 | + zip_writer.close() |
0 commit comments