Skip to content

Commit

Permalink
Uniformize path concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
rsubtil committed Apr 7, 2024
1 parent 667ad7f commit 6177c27
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 40 deletions.
2 changes: 1 addition & 1 deletion scenes/config/settings/GeneralSettings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func set_themes():
if RetroHubConfig._get_default_themes_dir() in RetroHubConfig.theme_path and \
theme_pck in RetroHubConfig.theme_path:
n_themes.selected = id
theme_id_map[id] = "res://default_themes/" + theme_pck
theme_id_map[id] = "res://default_themes".path_join(theme_pck)
id += 1
n_themes.add_separator()
id += 1
Expand Down
40 changes: 20 additions & 20 deletions source/Config.gd
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,15 @@ func _load_config_file():
# TODO: behavior for when file is corrupt

func _get_credential(key: String) -> String:
var json : Dictionary = JSONUtils.load_json_file(_get_config_dir() + "/rh_credentials.json")
var json : Dictionary = JSONUtils.load_json_file(_get_config_dir().path_join("rh_credentials.json"))
if not json.is_empty() and json.has(key):
return json[key]
return ""

func _set_credential(key: String, value: String):
var json : Dictionary = JSONUtils.load_json_file(_get_config_dir() + "/rh_credentials.json")
var json : Dictionary = JSONUtils.load_json_file(_get_config_dir().path_join("rh_credentials.json"))
json[key] = value
JSONUtils.save_json_file(json, _get_config_dir() + "/rh_credentials.json")
JSONUtils.save_json_file(json, _get_config_dir().path_join("rh_credentials.json"))

func _on_config_updated(key, old_value, new_value):
match key:
Expand Down Expand Up @@ -552,7 +552,7 @@ func set_theme_config(key, value):
func _load_theme_config():
_theme_config = {}
_theme_config_changed = false
var theme_config_path := _get_theme_config_dir() + "/config.json"
var theme_config_path := _get_theme_config_dir().path_join("config.json")
# If the config file doesn't exist, don't try reading it
if not FileAccess.file_exists(theme_config_path):
return
Expand All @@ -566,7 +566,7 @@ func _load_theme_config():

func save_theme_config():
if _theme_config_changed:
var theme_config_path := _get_theme_config_dir() + "/config.json"
var theme_config_path := _get_theme_config_dir().path_join("config.json")
FileUtils.ensure_path(theme_config_path)
var file := FileAccess.open(theme_config_path, FileAccess.WRITE)
if not file:
Expand Down Expand Up @@ -597,12 +597,12 @@ func _bootstrap_config_dir():
push_error("Error when creating directory " + path)

# Bootstrap system specific configs
for filename in [
for filename: String in [
"rh_emulators.json",
"rh_systems.json",
"_emulator_paths.json"
]:
var filepath_out := _get_config_dir() + "/" + (filename as String)
var filepath_out := _get_config_dir().path_join(filename)
var file := FileAccess.open(filepath_out, FileAccess.WRITE)
if not file:
push_error("Error when opening file " + filepath_out + " for saving")
Expand All @@ -611,7 +611,7 @@ func _bootstrap_config_dir():
file.close()

# Bootstrap credentials file
JSONUtils.save_json_file({}, _get_config_dir() + "/rh_credentials.json")
JSONUtils.save_json_file({}, _get_config_dir().path_join("rh_credentials.json"))

func _save_config():
if config.save_config_to_path(_get_config_file()):
Expand Down Expand Up @@ -734,7 +734,7 @@ func _determine_sc_mode():
if exe_path.ends_with("MacOS") and exe_path.path_join("..").simplify_path().ends_with("Contents"):
exe_path = exe_path.path_join("../../..").simplify_path()

if FileAccess.file_exists(exe_path + "/._sc_") or FileAccess.file_exists(exe_path + "/_sc_"):
if FileAccess.file_exists(exe_path.path_join("._sc_")) or FileAccess.file_exists(exe_path.path_join("_sc_")):
_is_sc = true

func _get_emulator_path(emulator_name: String, key: String) -> String:
Expand All @@ -759,22 +759,22 @@ func _set_emulator_path(emulator_name: String, key: String, value: String) -> vo
func _get_config_dir() -> String:
var path : String
if _is_sc:
return OS.get_executable_path().get_base_dir() + "/config"
return OS.get_executable_path().get_base_dir().path_join("config")

match FileUtils.get_os_id():
FileUtils.OS_ID.WINDOWS:
path = FileUtils.get_home_dir() + "/RetroHub"
path = FileUtils.get_home_dir().path_join("RetroHub")
if RetroHub._is_dev_env():
path += "-Dev"
return path
_:
path = FileUtils.get_home_dir() + "/.retrohub"
path = FileUtils.get_home_dir().path_join(".retrohub")
if RetroHub._is_dev_env():
path += "-dev"
return path

func _get_config_file() -> String:
return _get_config_dir() + "/rh_config.json"
return _get_config_dir().path_join("rh_config.json")

func _get_systems_file() -> String:
return "res://base_config/systems.json"
Expand All @@ -783,27 +783,27 @@ func _get_emulators_file() -> String:
return "res://base_config/emulators.json"

func _get_emulator_paths_file() -> String:
return _get_config_dir() + "/_emulator_paths.json"
return _get_config_dir().path_join("_emulator_paths.json")

func _get_custom_systems_file() -> String:
return _get_config_dir() + "/rh_systems.json"
return _get_config_dir().path_join("rh_systems.json")

func _get_custom_emulators_file() -> String:
return _get_config_dir() + "/rh_emulators.json"
return _get_config_dir().path_join("rh_emulators.json")

func _get_default_themes_dir() -> String:
return "res://default_themes"

func _get_themes_dir() -> String:
return _get_config_dir() + "/themes"
return _get_config_dir().path_join("themes")

func _get_theme_config_dir() -> String:
return _get_themes_dir() + "/config/" + theme_data.id if theme_data else ""
return _get_themes_dir().path_join("config").path_join(theme_data.id if theme_data else "")

func _get_gamelists_dir() -> String:
return _get_config_dir() + "/gamelists"
return _get_config_dir().path_join("gamelists")

func _get_gamemedia_dir() -> String:
if not config.custom_gamemedia_dir.is_empty():
return config.custom_gamemedia_dir
return _get_config_dir() + "/gamemedia"
return _get_config_dir().path_join("gamemedia")
2 changes: 1 addition & 1 deletion source/data/ConfigData.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var _old_config : Dictionary
# Games directory
var config_version : int = 2
var is_first_time : bool = true: set = _set_is_first_time
var games_dir : String = FileUtils.get_home_dir() + "/ROMS": set = _set_games_dir
var games_dir : String = FileUtils.get_home_dir().path_join("ROMS"): set = _set_games_dir
var current_theme : String = "default": set = _set_current_theme
var lang : String = "en": set = _set_lang
var fullscreen : bool = true: set = _set_fullscreen
Expand Down
6 changes: 3 additions & 3 deletions source/emulators/RetroarchEmulator.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ static func get_custom_core_path() -> String:

var config_file := get_config_path()
if config_file:
var file := FileAccess.open(config_file + "/retroarch.cfg", FileAccess.READ)
var file := FileAccess.open(config_file.path_join("retroarch.cfg"), FileAccess.READ)
if file:
while file.get_position() < file.get_length():
var line := file.get_line()
Expand All @@ -32,12 +32,12 @@ static func get_config_path() -> String:
# RetroArch uses either XDG_CONFIG_HOME or HOME.
var xdg := OS.get_environment("XDG_CONFIG_HOME")
if not xdg.is_empty():
var path := xdg + "/retroarch"
var path := xdg.path_join("retroarch")
if DirAccess.dir_exists_absolute(path):
return path
else:
# Default to HOME
var path := FileUtils.get_home_dir() + "/.config/retroarch"
var path := FileUtils.get_home_dir().path_join(".config/retroarch")
if DirAccess.dir_exists_absolute(path):
return path
return ""
Expand Down
8 changes: 4 additions & 4 deletions source/importers/ESDEImporter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ extends EmulationStationImporter
class_name ESDEImporter

func _init():
config_path = FileUtils.get_home_dir() + "/ES-DE"
media_path = config_path + "/downloaded_media"
gamelists_path = config_path + "/gamelists"
config_file_path = config_path + "/settings/es_settings.xml"
config_path = FileUtils.get_home_dir().path_join("ES-DE")
media_path = config_path.path_join("downloaded_media")
gamelists_path = config_path.path_join("gamelists")
config_file_path = config_path.path_join("settings/es_settings.xml")

# Returns this importer name
func get_importer_name() -> String:
Expand Down
12 changes: 6 additions & 6 deletions source/importers/EmulationStationImporter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ var theme_support : int
# Config level 7 is already ES-DE.
const MAXIMUM_SUPPORTED_CONFIG_LEVEL = 7

var config_path := FileUtils.get_home_dir() + "/.emulationstation"
var media_path := config_path + "/downloaded_media"
var gamelists_path := config_path + "/gamelists"
var config_file_path := config_path + "/es_settings.xml"
var config_path := FileUtils.get_home_dir().path_join(".emulationstation")
var media_path := config_path.path_join("downloaded_media")
var gamelists_path := config_path.path_join("gamelists")
var config_file_path := config_path.path_join("es_settings.xml")
var folder_size : int = -1

var game_datas := {}
Expand Down Expand Up @@ -95,7 +95,7 @@ func is_available() -> bool:
return false

# Are the theme's config version too recent?
var theme_path := config_path + "/themes"
var theme_path := config_path.path_join("themes")
var config_level : int = -1
var dir := DirAccess.open(theme_path)
if dir and not dir.list_dir_begin() :# TODOGODOT4 fill missing arguments https://github.com/godotengine/godot/pull/40547
Expand All @@ -114,7 +114,7 @@ func is_available() -> bool:
func check_theme_config_level(base_path: String) -> int:

# Query first at root, and only move to folders if it doesn't exist
var root_file := base_path + "/theme.xml"
var root_file := base_path.path_join("theme.xml")
if FileAccess.file_exists(root_file):
var config_level := inspect_theme_xml(root_file)
if config_level != -1:
Expand Down
4 changes: 2 additions & 2 deletions source/importers/RetroArchImporter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func is_available() -> bool:

func set_paths(root: String):
config_path = root
thumbnails_path = config_path + "/thumbnails"
playlists_path = config_path + "/playlists"
thumbnails_path = config_path.path_join("thumbnails")
playlists_path = config_path.path_join("playlists")

# Begins the import process. `copy` determines if the user wants
# to copy previous data and, therefore, not affect the other game library
Expand Down
2 changes: 1 addition & 1 deletion source/integrations/RetroAchievementsIntegration.gd
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ func _ready() -> void:
Raw.http.timeout = 10
add_child(Raw.http)

FileUtils.ensure_path(get_cheevos_dir() + "/")
FileUtils.ensure_dir(get_cheevos_dir())

func _download_hash_cache() -> int:
var path := get_cheevos_hash_cache_path()
Expand Down
7 changes: 5 additions & 2 deletions source/utils/FileUtils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ func test_for_valid_path(paths) -> String:
return expanded_path
return ""

func ensure_dir(dir: String):
if DirAccess.make_dir_recursive_absolute(dir):
push_error("Failed to create directory %s" % dir)

func ensure_path(path: String):
if DirAccess.make_dir_recursive_absolute(path.get_base_dir()):
push_error("Failed to create directory %s" % path.get_base_dir())
ensure_dir(path.get_base_dir())

func expand_path(path: String) -> String:
# Replace ~ by home
Expand Down

0 comments on commit 6177c27

Please sign in to comment.