build_zip.gd 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. extends Node
  2. class_name ModToolZipBuilder
  3. func build_zip(mod_tool_store: ModToolStore) -> void:
  4. var writer := ZIPPacker.new()
  5. var err := writer.open(mod_tool_store.path_global_final_zip)
  6. if not err == OK:
  7. return
  8. # Get all file paths inside the mod folder
  9. mod_tool_store.path_mod_files = ModToolUtils.get_flat_view_dict(mod_tool_store.path_mod_dir)
  10. # Loop over each file path
  11. for i in mod_tool_store.path_mod_files.size():
  12. var path_mod_file := mod_tool_store.path_mod_files[i] as String
  13. # Check for excluded file extensions
  14. if ModToolUtils.is_file_extension(path_mod_file, mod_tool_store.excluded_file_extensions):
  15. # Dont add files with unwanted extensions to the zip
  16. mod_tool_store.path_mod_files.remove_at(i)
  17. continue
  18. # If it's a .import file
  19. if path_mod_file.get_extension() == "import":
  20. # Get the path to the imported file
  21. var path_imported_file := _get_imported_file_path(path_mod_file)
  22. # And add it to the mod file paths
  23. if not path_imported_file == "":
  24. mod_tool_store.path_mod_files.append(path_imported_file)
  25. # Add each file to the mod zip
  26. for i in mod_tool_store.path_mod_files.size():
  27. var path_mod_file: String = mod_tool_store.path_mod_files[i]
  28. var path_mod_file_data := FileAccess.open(path_mod_file, FileAccess.READ)
  29. var path_mod_file_length := path_mod_file_data.get_length()
  30. var path_mod_file_buffer := path_mod_file_data.get_buffer(path_mod_file_length)
  31. var path_zip_file: String = path_mod_file.trim_prefix("res://")
  32. writer.start_file(path_zip_file) # path inside the zip file
  33. writer.write_file(path_mod_file_buffer)
  34. writer.close_file()
  35. writer.close()
  36. # Open the export dir
  37. var file_manager_path: String = mod_tool_store.path_global_export_dir
  38. if OS.has_feature("macos"):
  39. file_manager_path = "file://" + file_manager_path
  40. OS.shell_open(file_manager_path)
  41. func _get_imported_file_path(import_file_path: String) -> String:
  42. var config := ConfigFile.new()
  43. # Open file
  44. var error := config.load(import_file_path)
  45. if error != OK:
  46. ModToolUtils.output_error("Failed to load import file -> " + str(error))
  47. # Get the path to the imported file
  48. # Imported file example path:
  49. # res://.godot/imported/ImportedPNG.png-eddc81c8e2d2fc90950be5862656c2b5.stex
  50. var imported_file_path := config.get_value('remap', 'path', '') as String
  51. if imported_file_path == '':
  52. ModToolUtils.output_error("No remap path found in import file -> " + import_file_path)
  53. return ''
  54. return imported_file_path