extends Node2D

# Code for DLCs and separating levels in multiple files.

var loaded_dlc = []

var has_content = false

func dir_contents(path):
	var dir = Directory.new()
	if dir.open(path) == OK:
		dir.list_dir_begin()
		var file_name = dir.get_next()
		while file_name != "":
			if dir.current_is_dir() and file_name != ".." and file_name != "." and not loaded_dlc.has(file_name):
				has_content = true
				loaded_dlc.append(file_name)
			elif file_name != ".." and file_name != ".":
				has_content = true
			file_name = dir.get_next()
	else:
		print("ERROR! No content loaded; the game won't work!")


func list_files_in_directory(path, import_verify = true):
	var files = []
	var dir = Directory.new()
	dir.open(path)
	dir.list_dir_begin()
	while true:
		var file = dir.get_next()
		if file == "":
			break
		elif not file.begins_with(".") and not file.ends_with(".import"):
			files.append(file)
		elif not file.begins_with(".") and file.ends_with(".import") and import_verify:
			files.append(file)
	dir.list_dir_end()
	return files

func _ready():
	load_dlc()

func load_dlc():
	# warning-ignore:unused_variable
	loaded_dlc = []
	has_content = false
	var dir = Directory.new()
	var path = OS.get_executable_path().get_base_dir() + "/packs/"
	var dlc_files = list_files_in_directory(path)
	for dlc in dlc_files:
		if dlc.ends_with(".gts") or dlc.ends_with(".patreon"):
			# warning-ignore:return_value_discarded
			ProjectSettings.load_resource_pack(OS.get_executable_path().get_base_dir() + "/packs/" + dlc, false)
			if dlc.ends_with(".patreon"):
				var name_execute = dlc.split(".")
				if (ResourceLoader.exists("res://@content/"+name_execute[0]+"/Execute.tscn")): 
					var run_once = Utils.load_file("res://@content/"+name_execute[0]+"/Execute.tscn").instance()
					run_once.execute_code()
					run_once.queue_free()
					run_once = null
				var dir_remove = Directory.new()
				dir_remove.remove("res://@content/"+name_execute[0])
				
				#print(dlc)
	path = OS.get_executable_path().get_base_dir() + "/"
	dlc_files = list_files_in_directory(path)
	for dlc in dlc_files:
		if dlc.ends_with(".gts") or dlc.ends_with(".patreon"):
			# warning-ignore:return_value_discarded
			ProjectSettings.load_resource_pack(OS.get_executable_path().get_base_dir() + "/" + dlc, false)
			if dlc.ends_with(".patreon"):
				var name_execute = dlc.split(".")
				if (ResourceLoader.exists("res://@content/"+name_execute[0]+"/Execute.tscn")): 
					var run_once = Utils.load_file("res://@content/"+name_execute[0]+"/Execute.tscn").instance()
					run_once.execute_code()
					run_once.queue_free()
					run_once = null
				var dir_remove = Directory.new()
				dir_remove.remove("res://@content/"+name_execute[0])
	
	dir_contents("res://@content/")

func get_content_list():
	return loaded_dlc

func get_content_treasure_mini(dlc):
	if not (ResourceLoader.exists("res://@content/"+dlc+"/setup.tscn")): return []
	return list_files_in_directory("res://@content/" + dlc + "/treasures/mini/", true)
	
func get_content_treasure_original(dlc):
	if not (ResourceLoader.exists("res://@content/"+dlc+"/setup.tscn")): return []
	return list_files_in_directory("res://@content/" + dlc + "/treasures/original/", true)
	
func get_content_settings(dlc):
	if not (ResourceLoader.exists("res://@content/"+dlc+"/setup.tscn")): return []
	return Utils.load_file("res://@content/"+dlc+"/setup.tscn").instance()

func get_content_treasure_prices(dlc):
	var content = self.get_content_settings(dlc)
	var treasure = content.treasure_prices
	content.queue_free()
	return treasure

func get_content_character_price(dlc):
	var content = self.get_content_settings(dlc)
	var treasure = content.character_price
	content.queue_free()
	return treasure
	
func get_content_character(dlc):
	var content = self.get_content_settings(dlc)
	var character = content.character
	content.queue_free()
	return character

func get_content_platform_count(dlc):
	var content = self.get_content_settings(dlc)
	var counter = content.platform_blocks
	content.queue_free()
	return int(counter)

func get_content_talk(dlc):
	var content = self.get_content_settings(dlc)
	var counter = content.talks
	content.queue_free()
	return counter

func is_raining(dlc):
	var content = self.get_content_settings(dlc)
	var booleanval = content.raining
	content.queue_free()
	return booleanval

func is_vertical_camera_limited(dlc):
	var content = self.get_content_settings(dlc)
	var booleanval = content.limit_vertical_camera
	content.queue_free()
	return booleanval

func is_snowing(dlc):
	var content = self.get_content_settings(dlc)
	var booleanval = content.snowing
	content.queue_free()
	return booleanval

func is_fog(dlc):
	var content = self.get_content_settings(dlc)
	var booleanval = content.fog
	content.queue_free()
	return booleanval

func is_lavarock(dlc):
	var content = self.get_content_settings(dlc)
	var booleanval = content.lavarock
	content.queue_free()
	return booleanval


func get_trauma_strength(dlc):
	var content = self.get_content_settings(dlc)
	var strength = content.camera_trauma
	content.queue_free()
	return strength
	
func get_content_difficult(dlc):
	var content = self.get_content_settings(dlc)
	var difficult = content.difficult
	content.queue_free()
	return difficult

func get_content_author(dlc):
	var content = self.get_content_settings(dlc)
	var author = content.author
	content.queue_free()
	return author

func has_file(dlc, dir):
	var file2Check = File.new()
	var doFileExists = file2Check.file_exists("res://@content/" + dlc + dir)
	if not doFileExists:
		 doFileExists = file2Check.file_exists("res://@content/" + dlc + dir + ".import")
	return doFileExists
	
func load_texture(dlc, dir):
	if not has_file(dlc, dir): return Texture.new()
	var texture = Utils.load_file("res://@content/" + dlc + dir)
	if not texture:
		texture = Utils.load_file("res://@content/" + dlc + dir + "import")
	return texture
	


