mirror of
https://github.com/JHDev2006/Super-Mario-Bros.-Remastered-Public.git
synced 2025-10-23 16:00:54 +00:00
Add true portable mode (#259)
This commit is contained in:
@@ -26,7 +26,7 @@ func get_resource(resource: Resource) -> Resource:
|
||||
|
||||
if original_resource is Texture:
|
||||
var new_resource = null
|
||||
if path.contains("user://"):
|
||||
if path.contains(Global.config_path):
|
||||
new_resource = ImageTexture.create_from_image(Image.load_from_file(path))
|
||||
else:
|
||||
new_resource = load(path)
|
||||
@@ -64,7 +64,7 @@ func send_to_cache(resource_path := "", resource_to_cache: Resource = null) -> v
|
||||
|
||||
func get_resource_path(resource_path := "") -> String:
|
||||
for i in Settings.file.visuals.resource_packs:
|
||||
var test = resource_path.replace("res://Assets/", "user://resource_packs/" + i + "/")
|
||||
var test = resource_path.replace("res://Assets/", Global.config_path.path_join("resource_packs/" + i + "/"))
|
||||
if FileAccess.file_exists(test):
|
||||
return test
|
||||
return resource_path
|
||||
|
@@ -134,13 +134,13 @@ static func get_pure_resource_path(resource_path := "") -> String:
|
||||
if Settings.file.visuals.resource_packs.is_empty() == false:
|
||||
for i in Settings.file.visuals.resource_packs:
|
||||
var new_path = get_override_resource_path(resource_path, i)
|
||||
new_path = new_path.replace("user://custom_characters/", "user://resource_packs/" + new_path + "/Sprites/Players/CustomCharacters/")
|
||||
new_path = new_path.replace(Global.config_path.path_join("custom_characters"), Global.config_path.path_join("resource_packs/" + new_path + "/Sprites/Players/CustomCharacters/"))
|
||||
if FileAccess.file_exists(new_path):
|
||||
return new_path
|
||||
return resource_path
|
||||
|
||||
static func get_override_resource_path(resource_path := "", resource_pack := "") -> String:
|
||||
if resource_pack != "":
|
||||
return resource_path.replace("res://Assets", "user://resource_packs/" + resource_pack)
|
||||
return resource_path.replace("res://Assets", Global.config_path.path_join("resource_packs/" + resource_pack))
|
||||
else:
|
||||
return resource_path
|
||||
|
@@ -276,8 +276,8 @@ func get_config_file(resource_pack := "") -> void:
|
||||
print("resource pack to use: " + resource_pack)
|
||||
|
||||
func get_resource_pack_path(res_path := "", resource_pack := "") -> String:
|
||||
var user_path := res_path.replace("res://Assets", "user://resource_packs/" + resource_pack)
|
||||
user_path = user_path.replace("user://custom_characters/", "user://resource_packs/" + resource_pack + "/Sprites/Players/CustomCharacters/")
|
||||
var user_path := res_path.replace("res://Assets", Global.config_path.path_join("resource_packs/" + resource_pack))
|
||||
user_path = user_path.replace(Global.config_path.path_join("custom_characters"), Global.config_path.path_join("resource_packs/" + resource_pack + "/Sprites/Players/CustomCharacters/"))
|
||||
if FileAccess.file_exists(user_path):
|
||||
return user_path
|
||||
else:
|
||||
|
@@ -366,8 +366,6 @@ func close_save_menu() -> void:
|
||||
menu_open = false
|
||||
current_state = EditorState.TILE_MENU
|
||||
|
||||
const CUSTOM_LEVEL_DIR := "user://custom_levels/"
|
||||
|
||||
func handle_tile_cursor() -> void:
|
||||
Input.set_custom_mouse_cursor(null)
|
||||
var snapped_position = ((%TileCursor.get_global_mouse_position() - CURSOR_OFFSET).snapped(Vector2(16, 16))) + CURSOR_OFFSET
|
||||
|
@@ -18,7 +18,7 @@ func get_character_sprite_path(player_id := 0) -> String:
|
||||
var character = Player.CHARACTERS[int(Global.player_characters[player_id])]
|
||||
var path = "res://Assets/Sprites/Players/" + character + "/CheckpointFlag.json"
|
||||
if int(Global.player_characters[player_id]) > 3:
|
||||
path = path.replace("res://Assets/Sprites/Players", "user://custom_characters")
|
||||
path = path.replace("res://Assets/Sprites/Players", Global.config_path.path_join("custom_characters"))
|
||||
return path
|
||||
|
||||
func activate(player: Player) -> void:
|
||||
|
@@ -194,7 +194,7 @@ func _ready() -> void:
|
||||
func apply_character_physics() -> void:
|
||||
var path = "res://Assets/Sprites/Players/" + character + "/CharacterInfo.json"
|
||||
if int(Global.player_characters[player_id]) > 3:
|
||||
path = path.replace("res://Assets/Sprites/Players", "user://custom_characters")
|
||||
path = path.replace("res://Assets/Sprites/Players", Global.config_path.path_join("custom_characters/"))
|
||||
path = ResourceSetter.get_pure_resource_path(path)
|
||||
var json = JSON.parse_string(FileAccess.open(path, FileAccess.READ).get_as_text())
|
||||
for i in json.physics:
|
||||
@@ -323,7 +323,7 @@ func apply_character_sfx_map() -> void:
|
||||
var custom_character := false
|
||||
if int(Global.player_characters[player_id]) > 3:
|
||||
custom_character = true
|
||||
path = path.replace("res://Assets/Sprites/Players", "user://custom_characters")
|
||||
path = path.replace("res://Assets/Sprites/Players", Global.config_path.path_join("custom_characters/"))
|
||||
path = ResourceSetter.get_pure_resource_path(path)
|
||||
var json = JSON.parse_string(FileAccess.open(path, FileAccess.READ).get_as_text())
|
||||
|
||||
@@ -333,7 +333,7 @@ func apply_character_sfx_map() -> void:
|
||||
if FileAccess.file_exists(res_path) == false or custom_character:
|
||||
var directory = "res://Assets/Sprites/Players/" + character + "/" + json[i]
|
||||
if int(Global.player_characters[player_id]) > 3:
|
||||
directory = directory.replace("res://Assets/Sprites/Players", "user://custom_characters")
|
||||
directory = directory.replace("res://Assets/Sprites/Players", Global.config_path.path_join("custom_characters/"))
|
||||
directory = ResourceSetter.get_pure_resource_path(directory)
|
||||
if FileAccess.file_exists(directory):
|
||||
json[i] = directory
|
||||
@@ -699,7 +699,7 @@ func dispense_stored_item() -> void:
|
||||
func get_character_sprite_path(power_stateto_use := power_state.state_name) -> String:
|
||||
var path = "res://Assets/Sprites/Players/" + character + "/" + power_stateto_use + ".json"
|
||||
if int(Global.player_characters[player_id]) > 3:
|
||||
path = path.replace("res://Assets/Sprites/Players", "user://custom_characters")
|
||||
path = path.replace("res://Assets/Sprites/Players", Global.config_path.path_join("custom_characters/"))
|
||||
return path
|
||||
|
||||
func enter_pipe(pipe: PipeArea, warp_to_level := true) -> void:
|
||||
|
@@ -113,7 +113,7 @@ func play_sfx(stream_name = "", position := Vector2.ZERO, pitch := 1.0) -> void:
|
||||
var stream = stream_name
|
||||
var is_custom = false
|
||||
if stream_name is String:
|
||||
is_custom = sfx_library[stream_name].contains("user://custom_characters")
|
||||
is_custom = sfx_library[stream_name].contains(Global.config_path.path_join("custom_characters"))
|
||||
stream = import_stream(sfx_library[stream_name])
|
||||
if is_custom == false:
|
||||
player.stream = ResourceSetter.get_resource(stream, player)
|
||||
@@ -236,7 +236,7 @@ func handle_music_override() -> void:
|
||||
func create_stream_from_json(json_path := "") -> AudioStream:
|
||||
if json_path.contains(".json") == false:
|
||||
var path = ResourceSetter.get_pure_resource_path(json_path)
|
||||
if path.contains("user://"):
|
||||
if path.contains(Global.config_path):
|
||||
match json_path.get_slice(".", 1):
|
||||
"wav":
|
||||
return AudioStreamWAV.load_from_file(ResourceSetter.get_pure_resource_path(json_path))
|
||||
|
@@ -26,11 +26,13 @@ var second_quest := false
|
||||
var extra_worlds_win := false
|
||||
const lang_codes := ["en", "fr", "es", "de", "it", "pt", "pl", "tr", "ru", "jp", "fil", "id", "ga"]
|
||||
|
||||
var config_path : String = get_config_path()
|
||||
|
||||
var rom_path := ""
|
||||
var rom_assets_exist := false
|
||||
const ROM_POINTER_PATH := "user://rom_pointer.smb"
|
||||
const ROM_PATH := "user://baserom.nes"
|
||||
const ROM_ASSETS_PATH := "user://resource_packs/BaseAssets"
|
||||
var ROM_POINTER_PATH = config_path.path_join("rom_pointer.smb")
|
||||
var ROM_PATH = config_path.path_join("baserom.nes")
|
||||
var ROM_ASSETS_PATH = config_path.path_join("resource_packs/BaseAssets")
|
||||
const ROM_PACK_NAME := "BaseAssets"
|
||||
const ROM_ASSETS_VERSION := 0
|
||||
|
||||
@@ -169,8 +171,46 @@ func _ready() -> void:
|
||||
get_server_version()
|
||||
if OS.is_debug_build():
|
||||
debug_mode = false
|
||||
setup_config_dirs()
|
||||
check_for_rom()
|
||||
|
||||
func setup_config_dirs() -> void:
|
||||
var dirs = [
|
||||
"custom_characters",
|
||||
"custom_levels",
|
||||
"logs",
|
||||
"marathon_recordings",
|
||||
"resource_packs",
|
||||
"saves"
|
||||
]
|
||||
|
||||
for d in dirs:
|
||||
var full_path = Global.config_path.path_join(d)
|
||||
if not DirAccess.dir_exists_absolute(full_path):
|
||||
DirAccess.make_dir_recursive_absolute(full_path)
|
||||
|
||||
func get_config_path() -> String:
|
||||
var exe_path := OS.get_executable_path()
|
||||
var exe_dir := exe_path.get_base_dir()
|
||||
var portable_flag := exe_dir.path_join("portable.txt")
|
||||
|
||||
# Test that exe dir is writeable, if not fallback to user://
|
||||
if FileAccess.file_exists(portable_flag):
|
||||
var test_file = exe_dir.path_join("test.txt")
|
||||
var f = FileAccess.open(test_file, FileAccess.WRITE)
|
||||
if f:
|
||||
f.close()
|
||||
var dir = DirAccess.open(exe_dir)
|
||||
if dir:
|
||||
dir.remove(test_file.get_file())
|
||||
var local_dir = exe_dir.path_join("config")
|
||||
if not DirAccess.dir_exists_absolute(local_dir):
|
||||
DirAccess.make_dir_recursive_absolute(local_dir)
|
||||
return local_dir
|
||||
else:
|
||||
push_warning("Portable flag found but exe directory is not writeable. Falling back to user://")
|
||||
return "user://"
|
||||
|
||||
func check_for_rom() -> void:
|
||||
rom_path = ""
|
||||
rom_assets_exist = false
|
||||
@@ -330,7 +370,7 @@ func close_freeze() -> void:
|
||||
$Transition/Freeze.hide()
|
||||
$Transition.hide()
|
||||
|
||||
var recording_dir = "user://marathon_recordings/"
|
||||
var recording_dir = config_path.path_join("marathon_recordings")
|
||||
|
||||
func update_game_status() -> void:
|
||||
var lives_str := str(Global.lives)
|
||||
|
@@ -1,6 +1,6 @@
|
||||
extends Node
|
||||
|
||||
const SAVE_DIR := "user://saves/CAMPAIGN.sav"
|
||||
var SAVE_DIR : String = Global.config_path.path_join("saves/CAMPAIGN.sav")
|
||||
|
||||
var visited_levels := "1000000000000000000000000000000010000000000000000000"
|
||||
|
||||
@@ -71,14 +71,10 @@ func write_save(campaign: String = Global.current_campaign, force := false) -> v
|
||||
if Global.debugged_in and not force:
|
||||
return
|
||||
var save = null
|
||||
DirAccess.make_dir_recursive_absolute("user://saves")
|
||||
DirAccess.make_dir_recursive_absolute("user://resource_packs")
|
||||
DirAccess.make_dir_recursive_absolute("user://custom_characters")
|
||||
DirAccess.make_dir_recursive_absolute("user://custom_levels")
|
||||
var save_json = {}
|
||||
var path = "user://saves/" + campaign + ".sav"
|
||||
var path = Global.config_path.path_join("saves/" + campaign + ".sav")
|
||||
if FileAccess.file_exists(path):
|
||||
save = FileAccess.open("user://saves/" + campaign + ".sav", FileAccess.READ)
|
||||
save = FileAccess.open(path, FileAccess.READ)
|
||||
save_json = JSON.parse_string(save.get_as_text())
|
||||
save.close()
|
||||
else:
|
||||
@@ -150,7 +146,8 @@ func clear_save() -> void:
|
||||
visited_levels[0][0] = "1"
|
||||
var save = SAVE_TEMPLATE.duplicate(true)
|
||||
apply_save(save)
|
||||
DirAccess.remove_absolute("user://saves/" + Global.current_campaign + ".sav")
|
||||
var save_path = Global.config_path.path_join("saves" + Global.current_campaign + ".sav")
|
||||
DirAccess.remove_absolute(save_path)
|
||||
write_save(Global.current_campaign)
|
||||
|
||||
func clear_array(arr := []) -> void:
|
||||
@@ -172,9 +169,10 @@ func get_level_idx(world_num := 1, level_num := 1) -> int:
|
||||
return ((world_num - 1) * 4) + (level_num - 1)
|
||||
|
||||
func load_achievements() -> void:
|
||||
if FileAccess.file_exists("user://achievements.sav") == false:
|
||||
var path = Global.config_path.path_join("achievements.sav")
|
||||
if FileAccess.file_exists(path) == false:
|
||||
write_achievements()
|
||||
var file = FileAccess.open("user://achievements.sav", FileAccess.READ)
|
||||
var file = FileAccess.open(path, FileAccess.READ)
|
||||
var idx := 0
|
||||
for i in file.get_as_text():
|
||||
Global.achievements[idx] = i
|
||||
@@ -183,6 +181,7 @@ func load_achievements() -> void:
|
||||
file.close()
|
||||
|
||||
func write_achievements() -> void:
|
||||
var file = FileAccess.open("user://achievements.sav", FileAccess.WRITE)
|
||||
var path = Global.config_path.path_join("achievements.sav")
|
||||
var file = FileAccess.open(path, FileAccess.WRITE)
|
||||
file.store_string(Global.achievements)
|
||||
file.close()
|
||||
|
@@ -79,10 +79,10 @@ var file := {
|
||||
}
|
||||
}
|
||||
|
||||
const SETTINGS_DIR := "user://settings.cfg"
|
||||
static var SETTINGS_DIR := Global.config_path.path_join("settings.cfg")
|
||||
|
||||
func _enter_tree() -> void:
|
||||
DirAccess.make_dir_absolute("user://resource_packs")
|
||||
DirAccess.make_dir_absolute(Global.config_path.path_join("resource_packs"))
|
||||
load_settings()
|
||||
await get_tree().physics_frame
|
||||
apply_settings()
|
||||
|
@@ -201,7 +201,7 @@ func gen_time_string(timer_dict := {}) -> String:
|
||||
|
||||
func save_recording() -> void:
|
||||
var recording := [timer, current_recording, levels, str(["Mario", "Luigi", "Toad", "Toadette"].find(get_tree().get_first_node_in_group("Players").character)), anim_list]
|
||||
var recording_dir = "user://marathon_recordings/" + Global.current_campaign
|
||||
var recording_dir = Global.config_path.path_join("marathon_recordings/" + Global.current_campaign)
|
||||
DirAccess.make_dir_recursive_absolute(recording_dir)
|
||||
var file = FileAccess.open(recording_dir + "/" + str(Global.world_num) + "-" + str(Global.level_num) + ("warp" if is_warp_run else "") + ".json", FileAccess.WRITE)
|
||||
file.store_string(compress_recording(JSON.stringify(recording, "", false, true)))
|
||||
@@ -240,7 +240,7 @@ func load_best_marathon() -> void:
|
||||
anim_list = recording[4].duplicate()
|
||||
|
||||
func load_recording(world_num := 0, level_num := 0, is_warpless := true, campaign := "SMB1") -> Array:
|
||||
var recording_dir = "user://marathon_recordings/" + campaign
|
||||
var recording_dir = Global.config_path.path_join("marathon_recordings/" + campaign)
|
||||
var path = recording_dir + "/" + str(world_num) + "-" + str(level_num) + ("" if is_warpless else "warp") + ".json"
|
||||
print(path)
|
||||
if FileAccess.file_exists(path) == false:
|
||||
@@ -257,12 +257,12 @@ func load_best_times(campaign = Global.current_campaign) -> void:
|
||||
best_level_any_times.clear()
|
||||
for world_num in 8:
|
||||
for level_num in 4:
|
||||
var path = "user://marathon_recordings/" + campaign + "/" + str(world_num + 1) + "-" + str(level_num + 1) + ".json"
|
||||
var path = Global.config_path.path_join("marathon_recordings/" + campaign + "/" + str(world_num + 1) + "-" + str(level_num + 1) + ".json")
|
||||
if FileAccess.file_exists(path):
|
||||
best_level_warpless_times[world_num][level_num] = load_recording(world_num + 1, level_num + 1, true, campaign)[0]
|
||||
else:
|
||||
best_level_warpless_times[world_num][level_num] = -1
|
||||
path = "user://marathon_recordings/" + campaign + "/" + str(world_num + 1) + "-" + str(level_num + 1) +"warp" + ".json"
|
||||
path = Global.config_path.path_join("marathon_recordings/" + campaign + "/" + str(world_num + 1) + "-" + str(level_num + 1) +"warp" + ".json")
|
||||
if FileAccess.file_exists(path):
|
||||
best_level_any_times[str(world_num + 1) + "-" + str(level_num + 1)] = load_recording(world_num + 1, level_num + 1, false, campaign)[0]
|
||||
check_for_medal_achievement()
|
||||
|
Reference in New Issue
Block a user