Skip to content

Commit

Permalink
Merge pull request #3 from olvior/texture_loading
Browse files Browse the repository at this point in the history
Better map distribution
  • Loading branch information
olvior authored Sep 27, 2024
2 parents 40a490c + eab1322 commit 0a76392
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 55 deletions.
72 changes: 53 additions & 19 deletions addons/ModLoader/mod_loader.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extends Node

func _init():

func _ready():
print("MOD LOADER starting\n\n")
ProjectSettings.set_setting("application/config/version", ProjectSettings.get_setting("application/config/version") + " - Modded")

load_maps()
Expand All @@ -10,8 +12,7 @@ func _init():

load(level_override_path).take_over_path(level_path)

func _ready():
get_tree().get_root().print_tree_pretty()
print("MOD LOADER finished\n\n")

###############
# MAP LOADING #
Expand Down Expand Up @@ -40,19 +41,58 @@ func load_maps():

while file_name != "":
if maps_folder.current_is_dir():
map_folders_list.append(maps_folder_path + '/' + file_name)
elif file_name.get_extension() == "zip":
print("Added res://maps/" + file_name.get_basename())
map_folders_list.append("res://maps/" + file_name.get_basename())
ProjectSettings.load_resource_pack(maps_folder_path + '/' + file_name)

if file_name == "disabled":
file_name = maps_folder.get_next()
continue
map_folders_list.append([maps_folder_path + '/' + file_name, file_name])

file_name = maps_folder.get_next()

for i in map_folders_list:
print(i)
print("Loading folder")
load_map_folder(i)
load_map_folder(i[0])

if DirAccess.dir_exists_absolute(i[0] + '/textures'):
load_textures(i[0] + '/textures', '', i[1])


func load_textures(path_to_textures, extra_path, mod_name):
var folder = DirAccess.open(path_to_textures + extra_path)
print("Opening for textures: " + path_to_textures + extra_path)

if not folder:
print("Failed to open " + path_to_textures + extra_path + " folder")
return

folder.list_dir_begin()
var file_name = folder.get_next()

while file_name != "":
if folder.current_is_dir():
load_textures(path_to_textures, extra_path + '/' + file_name, mod_name)
else:
if file_name.get_extension() == "png":
var file_path = path_to_textures + extra_path + '/' + file_name
var take_over_path = "res://textures/" + mod_name + extra_path + '/' + file_name
var img = Image.new()
var err = img.load(file_path)
if err != OK:
file_name = folder.get_next()
continue


var newTexture = ImageTexture.create_from_image(img)
var thing = newTexture.create_placeholder()

thing.take_over_path(take_over_path)

print(file_path)
print("takes over")
print(take_over_path)

file_name = folder.get_next()


func load_map_folder(path):
var folder = DirAccess.open(path)
Expand All @@ -63,30 +103,24 @@ func load_map_folder(path):
print("Failed to open " + path + " folder")
return


print("Searching through maps/")


folder.list_dir_begin()

var file_name = folder.get_next()
var map_list = []

while file_name != "":
if folder.current_is_dir():
pass
load_map_folder(path + '/' + file_name)
else:
if file_name.get_extension() == "map":
var base_name = file_name.get_basename()
var json_access = FileAccess.open(path + '/' + base_name + ".json", FileAccess.READ)
if json_access:
map_list.append(path + '/' + base_name)
load_map(path + '/' + base_name)
else:
print("Failed to open map " + path + '/' + base_name + ".json")

file_name = folder.get_next()

for i in map_list:
load_map(i)

print("\n")

Expand Down
27 changes: 27 additions & 0 deletions bt_mod_loader_installer/main_tab/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,30 @@ func hash_file(path):
# Print the result as hex string and array.
return res.hex_encode()
printt(res.hex_encode(), Array(res))


func unzip(path_to_zip: String, path_to_unzipped: String) -> void:
print("Unzipping " + path_to_zip + " to " + path_to_unzipped)
var zr : ZIPReader = ZIPReader.new()

if zr.open(path_to_zip) == OK:
for filepath in zr.get_files():
var zip_directory : String = path_to_zip.get_base_dir()

var da : DirAccess = DirAccess.open(zip_directory)

var extract_path : String = path_to_unzipped + '/'

da.make_dir(extract_path)

da = DirAccess.open(extract_path)
print(da)

da.make_dir_recursive(filepath.get_base_dir())

print(extract_path + filepath)
print(filepath, " is the path")

var fa : FileAccess = FileAccess.open("%s/%s" % [extract_path, filepath], FileAccess.WRITE)
if fa:
fa.store_buffer(zr.read_file(filepath))
28 changes: 1 addition & 27 deletions bt_mod_loader_installer/main_tab/manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,7 @@ func _on_install_mod_loader_button_up():


func move_mod_loader():
unzip("user://mod_loader.zip", main.path)

func unzip(path_to_zip: String, path_to_unzipped: String) -> void:
var zr : ZIPReader = ZIPReader.new()

if zr.open(path_to_zip) == OK:
for filepath in zr.get_files():
var zip_directory : String = path_to_zip.get_base_dir()

var da : DirAccess = DirAccess.open(zip_directory)

var extract_path : String = path_to_unzipped + '/'

da.make_dir(extract_path)

da = DirAccess.open(extract_path)
print(da)

da.make_dir_recursive(filepath.get_base_dir())

print(extract_path + filepath)
print(filepath, " is the path")

var fa : FileAccess = FileAccess.open("%s/%s" % [extract_path, filepath], FileAccess.WRITE)
if fa:
fa.store_buffer(zr.read_file(filepath))

main.unzip("user://mod_loader.zip", main.path)

var http_s = []
func download(link, path):
Expand Down
36 changes: 27 additions & 9 deletions bt_mod_loader_installer/maps/map.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ var disabled_list: Array[bool] = [true, false, true, false]
var uninstalled_list: Array[bool] = [false, true, true, true]

func check_if_installed():
my_path = main.path + "/maps/" + id + ".zip"
my_disabled_path = main.path + "/maps/disabled/" + id + ".zip"
my_path = main.path + "/maps/" + id
my_disabled_path = main.path + "/maps/disabled/" + id

if FileAccess.file_exists(my_path):
if DirAccess.dir_exists_absolute(my_path):
is_installed = true
change_buttons(enabled_list)

elif FileAccess.file_exists(my_disabled_path):
elif DirAccess.dir_exists_absolute(my_disabled_path):
is_disabled = true
change_buttons(disabled_list)

Expand Down Expand Up @@ -62,19 +62,21 @@ func _http_request_completed(result, _response_code, _headers, _body):
debug_label.text = "Download failed"
push_error("Download Failed")
else:
var my_hash = main.hash_file(my_path)
var my_hash = main.hash_file(my_path + ".zip")
if my_hash != manifest["SHA-256"]:
print("Hashes do not match")
print("Found: ", my_hash)
print("Was given: ", manifest["SHA-256"])
debug_label.text = "Hash does not match\nCanceled download"
DirAccess.remove_absolute(my_path)
DirAccess.remove_absolute(my_path + ".zip")

return

print("Found: ", my_hash)
print("All good")
debug_label.text = "Downloaded"
main.unzip(my_path + ".zip", main.path + "/maps")
DirAccess.remove_absolute(my_path + ".zip")
change_buttons(enabled_list)


Expand All @@ -85,7 +87,7 @@ func change_buttons(button_list: Array[bool]):
enable_button.disabled = button_list[3]

func _on_install_button_up():
download(manifest["download"], my_path)
download(manifest["download"], my_path + ".zip")

func _on_disable_button_up() -> void:
DirAccess.rename_absolute(my_path, my_disabled_path)
Expand All @@ -101,10 +103,26 @@ func _on_enable_button_up() -> void:

func _on_uninstall_button_up() -> void:
if is_disabled:
DirAccess.remove_absolute(my_disabled_path)
delete_folder_recursive(my_disabled_path)
else:
DirAccess.remove_absolute(my_path)
delete_folder_recursive(my_path)

change_buttons(uninstalled_list)
is_installed = false
debug_label.text = "Uninstalled mod"

func delete_folder_recursive(path):
var dir = DirAccess.open(path)
if dir:
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if dir.current_is_dir():
delete_folder_recursive(path + '/' + file_name)
else:
DirAccess.remove_absolute(path + '/' + file_name)
file_name = dir.get_next()

DirAccess.remove_absolute(path)
else:
print("An error occurred when trying to access the path.")

0 comments on commit 0a76392

Please sign in to comment.