Files

56 lines
2.0 KiB
GDScript

class_name ContentPaths
extends RefCounted
## Canonical project-managed content directories and legacy path migration.
const STICKMEN_DIR: String = "res://content/stickmen"
const PROPS_DIR: String = "res://content/props"
const STAGES_DIR: String = "res://content/stages"
const LEGACY_DIRS: Dictionary = {
"stickmen": "content/stickmen",
"props": "content/props",
"game_scenes": "content/stages",
}
static func canonicalize(path: String) -> String:
if path.is_empty():
return path
var normalized: String = path.replace("\\", "/")
if normalized.begins_with("res://"):
var relative_path: String = normalized.substr("res://".length())
var migrated: String = _canonicalize_relative(relative_path)
return "res://" + migrated if migrated != relative_path else path
if normalized.begins_with("user://"):
return path
if normalized.is_absolute_path():
return _canonicalize_absolute(normalized, path)
var migrated_relative: String = _canonicalize_relative(normalized)
return migrated_relative if migrated_relative != normalized else path
static func _canonicalize_relative(path: String) -> String:
var normalized: String = path.simplify_path()
for legacy_dir: String in LEGACY_DIRS:
var canonical_dir: String = String(LEGACY_DIRS[legacy_dir])
if normalized == legacy_dir:
return canonical_dir
var prefix: String = legacy_dir + "/"
if normalized.begins_with(prefix):
return canonical_dir + "/" + normalized.substr(prefix.length())
return path
static func _canonicalize_absolute(path: String, original_path: String) -> String:
var project_root: String = ProjectSettings.globalize_path("res://").replace("\\", "/").trim_suffix("/")
if path == project_root:
return original_path
var project_prefix: String = project_root + "/"
if not path.begins_with(project_prefix):
return original_path
var relative_path: String = path.substr(project_prefix.length())
var migrated: String = _canonicalize_relative(relative_path)
if migrated == relative_path:
return original_path
return project_root.path_join(migrated)