Skip to content

Commit

Permalink
Make changes based in PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mapedorr committed Jul 23, 2024
1 parent 07fd055 commit 7446676
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 93 deletions.
53 changes: 53 additions & 0 deletions addons/popochiu/editor/helpers/popochiu_editor_helper.gd
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ static var ei := EditorInterface
static var undo_redo: EditorUndoRedoManager = null
static var dock: Panel = null

static var _room_scene_path_template := PopochiuResources.ROOMS_PATH.path_join("%s/room_%s.tscn")


#region Public #####################################################################################
static func select_node(node: Node) -> void:
Expand Down Expand Up @@ -261,4 +263,55 @@ static func pack_scene(node: Node, path := "") -> int:
return ResourceSaver.save(packed_scene, path)


## Helper function to recursively remove all folders and files inside [param folder_path].
static func remove_recursive(folder_path: String) -> bool:
if DirAccess.dir_exists_absolute(folder_path):
# Delete subfolders and their contents recursively in folder_path
for subfolder_path: String in get_absolute_directory_paths_at(folder_path):
remove_recursive(subfolder_path)

# Delete all files in folder_path
for file_path: String in get_absolute_file_paths_at(folder_path):
if DirAccess.remove_absolute(file_path) != OK:
return false

# Once all files are deleted in folder_path, remove folder_path
if DirAccess.remove_absolute(folder_path) != OK:
return false
return true


## Helper function to get the absolute directory paths for all folders under [param folder_path].
static func get_absolute_directory_paths_at(folder_path: String) -> Array:
var dir_array : PackedStringArray = []

if DirAccess.dir_exists_absolute(folder_path):
for folder in DirAccess.get_directories_at(folder_path):
dir_array.append(folder_path.path_join(folder))

return Array(dir_array)


## Helper function to get the absolute file paths for all files under [param folder_path].
static func get_absolute_file_paths_at(folder_path: String) -> PackedStringArray:
var file_array : PackedStringArray = []

if DirAccess.dir_exists_absolute(folder_path):
for file in DirAccess.get_files_at(folder_path):
file_array.append(folder_path.path_join(file))

return file_array


## Returns an array of [PopochiuRoom] (instances) for all the rooms in the project.
static func get_rooms() -> Array[PopochiuRoom]:
var rooms: Array[PopochiuRoom] = []
rooms.assign(PopochiuResources.get_section_keys("rooms").map(
func (room_name: String) -> PopochiuRoom:
var scene_path := _room_scene_path_template.replace("%s", room_name.to_snake_case())
return (load(scene_path) as PackedScene).instantiate(PackedScene.GEN_EDIT_STATE_INSTANCE)
))
return rooms


#endregion
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func start() -> void:
progress.visible = true
while progress.visible:
progress.texture = get_theme_icon("Progress%d" % idx, "EditorIcons")
await PopochiuEditorHelper.wait(0.1)
await PopochiuEditorHelper.secs_passed(0.1)

idx = wrapi(idx + 1, 1, 9)

Expand Down
6 changes: 3 additions & 3 deletions addons/popochiu/engine/others/popochiu_utils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ static func get_click_or_touch_index(event: InputEvent) -> int:
## For each element in [param array] calls [param callback] passing the element as a parameter. If
## any of the calls returns [code]true[/code], then this function returns [code]true[/code],
## otherwise [code]false[/code] is returned.[br][br]
## This is an alternate version for [method Array.any] which doesn't stops the execution if one
## of the results is [code]true[/code] in a large array.
static func any(array: Array, callback: Callable) -> bool:
## This is an alternate version for [method Array.any] that doesn't stops execution even when one
## of the results is [code]true[/code].
static func any_exhaustive(array: Array, callback: Callable) -> bool:
var any_updated := false
for element in array:
var updated: bool = callback.call(element)
Expand Down
69 changes: 9 additions & 60 deletions addons/popochiu/migration/helpers/popochiu_migration_helper.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ class_name PopochiuMigrationHelper
extends Node
## Helper class to assist migrating Popochiu Projects to newer versions

const MIGRATIONS_PATH = "res://addons/popochiu/migration/migrations/"
const MIGRATION_SECTION = "last_migration"
const MIGRATION_KEY = "version"
const POPOCHIU_PATH = "res://popochiu/"

static var is_reload_required := false
static var old_settings_file := PopochiuResources.GAME_PATH.path_join("popochiu_settings.tres")

static var _room_scene_path_template := PopochiuResources.ROOMS_PATH.path_join("%s/room_%s.tscn")


#region Public #####################################################################################
static func get_migrations_count() -> int:
return DirAccess.get_files_at("res://addons/popochiu/migration/migration_files/").size()
return DirAccess.get_files_at(MIGRATIONS_PATH).size()


## Returns the game folder path. If this returns [member POPOCHIU_PATH], then the project is from
Expand All @@ -40,9 +40,9 @@ static func get_user_migration_version() -> int:
PopochiuUtils.print_error("Can't load [code]popochiu_data.cfg[/code] file.")
return -1

if PopochiuResources.has_data_value("migration", "version"):
if PopochiuResources.has_data_value(MIGRATION_SECTION, MIGRATION_KEY):
# Return the migration version in the popochiu_data.cfg file
return PopochiuResources.get_data_value("migration", "version", 1)
return PopochiuResources.get_data_value(MIGRATION_SECTION, MIGRATION_KEY, 1)
else:
# Run Migration 1 and so on
return 0
Expand All @@ -69,7 +69,7 @@ static func is_migration_needed() -> bool:

## Updates [code]res://game/popochiu_data.cfg[/code] migration version to [param version].
static func update_user_migration_version(new_version: int) -> void:
if PopochiuResources.set_data_value("migration", "version", new_version) != OK:
if PopochiuResources.set_data_value(MIGRATION_SECTION, MIGRATION_KEY, new_version) != OK:
PopochiuUtils.print_error(
"Couldn't update the Migration version from [b]%d[/b] to [b]%d[/b] in Data file." % [
get_migrations_count(), new_version
Expand All @@ -84,7 +84,7 @@ static func execute_migration_steps(migration: PopochiuMigration, steps: Array)
PopochiuUtils.print_error(
"No steps to execute for Migration %d" % migration.get_version()
)
await PopochiuEditorHelper.wait_process_frame()
await PopochiuEditorHelper.frame_processed()
return false

var idx := 0
Expand All @@ -103,46 +103,6 @@ static func execute_migration_steps(migration: PopochiuMigration, steps: Array)
return true


## Helper function to delete a folders and files inside [param folder_path].
static func delete_folder_and_contents(folder_path: String) -> bool:
if DirAccess.dir_exists_absolute(folder_path):
# Delete subfolders and their contents recursively in folder_path
for subfolder_path: String in get_absolute_directory_paths_at(folder_path):
delete_folder_and_contents(subfolder_path)

# Delete all files in folder_path
for file_path: String in get_absolute_file_paths_at(folder_path):
if DirAccess.remove_absolute(file_path) != OK:
return false

# Once all files are deleted in folder_path, remove folder_path
if DirAccess.remove_absolute(folder_path) != OK:
return false
return true


## Helper function to get the absolute directory paths for all folders under [param folder_path].
static func get_absolute_directory_paths_at(folder_path: String) -> Array:
var dir_array : PackedStringArray = []

if DirAccess.dir_exists_absolute(folder_path):
for folder in DirAccess.get_directories_at(folder_path):
dir_array.append(folder_path.path_join(folder))

return Array(dir_array)


## Helper function to get the absolute file paths for all files under [param folder_path].
static func get_absolute_file_paths_at(folder_path: String) -> PackedStringArray:
var file_array : PackedStringArray = []

if DirAccess.dir_exists_absolute(folder_path):
for file in DirAccess.get_files_at(folder_path):
file_array.append(folder_path.path_join(file))

return file_array


## Helper function to recursively scan the directory in [param path] and return an [Array] of
## absolute file paths with the specified extension.
static func get_absolute_file_paths_for_file_extensions(
Expand Down Expand Up @@ -180,7 +140,7 @@ static func get_absolute_file_paths_for_file_extensions(
## Looks in the text of each file in [param file_paths] for coincidencies of [param from], and
## replace them by [param to]. If any replacement was done, returns [code]true[/code].
static func replace_text_in_files(from: String, to: String, file_paths: Array) -> bool:
return PopochiuUtils.any(
return PopochiuUtils.any_exhaustive(
file_paths,
func (file_path: String) -> bool:
if not FileAccess.file_exists(file_path):
Expand Down Expand Up @@ -239,15 +199,4 @@ static func replace_in_scripts(
return replaced_matches > 0


## TODO: Document this function.
static func get_rooms() -> Array[PopochiuRoom]:
var rooms: Array[PopochiuRoom] = []
rooms.assign(PopochiuResources.get_section_keys("rooms").map(
func (room_name: String) -> PopochiuRoom:
var scene_path := _room_scene_path_template.replace("%s", room_name.to_snake_case())
return (load(scene_path) as PackedScene).instantiate(PackedScene.GEN_EDIT_STATE_INSTANCE)
))
return rooms


#endregion
13 changes: 11 additions & 2 deletions addons/popochiu/migration/migration/popochiu_migration.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class_name PopochiuMigration
extends Node
## This provides the core features needed to do a migration.
##
## Migration files in [code]res://addons/popochiu/migration/migration_files/*.gd[/code] should
## Migration files in [code]res://addons/popochiu/migration/migrations/*.gd[/code] should
## extend this class.

enum Completion {
Expand Down Expand Up @@ -35,6 +35,10 @@ func _do_migration() -> bool:
return false


func _is_reload_required() -> bool:
return false


#endregion

#region Public #####################################################################################
Expand All @@ -61,7 +65,7 @@ func is_migration_needed() -> bool:
## A helper function to display an error message in the [b]Output[/b] if there is an error doing
## the migration, or a message if it is successful. This updates the [code]popochiu_data.cfg[/code]
## file to have a new migration version if successful. [param migration] is an instansiated
## [PopochiuMigration] from [code]res://addons/popochiu/migration/migration_files/*.gd[/code].
## [PopochiuMigration] from [code]res://addons/popochiu/migration/migrations/*.gd[/code].
## [param version] is an integer for the migration version being run. This is intended to be called
## [DoMigration].
static func run_migration(migration: PopochiuMigration, version: int) -> bool:
Expand Down Expand Up @@ -115,4 +119,9 @@ func step_finished(idx: int, type: Completion) -> void:
step_completed.emit(self)


## Returns [code]true[/code] if this migration needs an Engine restart once applied.
func is_reload_required() -> bool:
return _is_reload_required()


#endregion
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ func _do_migration() -> bool:
]
)


func _is_reload_required() -> bool:
return false


#endregion

#region Private ####################################################################################
#func _step1() -> Completion:
#return Completion.DONE

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var _snake_renamed := []
## is successful. This is called from [method do_migration] which checks to make sure the migration
## should be done before calling this.
func _do_migration() -> bool:
PopochiuMigrationHelper.is_reload_required = true
return await PopochiuMigrationHelper.execute_migration_steps(
self,
[
Expand All @@ -49,6 +48,10 @@ func _do_migration() -> bool:
)


func _is_reload_required() -> bool:
return true


#endregion

#region Private ####################################################################################
Expand All @@ -69,9 +72,9 @@ func _delete_popochiu_folder_autoloads() -> Completion:
var autoloads_path := PopochiuMigrationHelper.POPOCHIU_PATH.path_join("Autoloads")

if DirAccess.dir_exists_absolute(autoloads_path):
all_done = PopochiuMigrationHelper.delete_folder_and_contents(autoloads_path)
all_done = PopochiuEditorHelper.remove_recursive(autoloads_path)
elif DirAccess.dir_exists_absolute(autoloads_path.to_lower()):
all_done = PopochiuMigrationHelper.delete_folder_and_contents(autoloads_path.to_lower())
all_done = PopochiuEditorHelper.remove_recursive(autoloads_path.to_lower())

return Completion.DONE if all_done else Completion.FAILED

Expand Down Expand Up @@ -116,8 +119,8 @@ func _move_game_data() -> Completion:

## Rename PopochiuResources.GAME_PATH files and folders to snake case
func _rename_files_and_folders_to_snake_case() -> Completion:
var any_renamed := PopochiuUtils.any(
PopochiuMigrationHelper.get_absolute_directory_paths_at(PopochiuResources.GAME_PATH),
var any_renamed := PopochiuUtils.any_exhaustive(
PopochiuEditorHelper.get_absolute_directory_paths_at(PopochiuResources.GAME_PATH),
func (folder: String) -> bool:
var any_file_renamed := _rename_files_to_snake_case(folder)
var any_folder_renamed := _rename_folders_to_snake_case(folder)
Expand All @@ -139,7 +142,7 @@ func _rename_files_and_folders_to_snake_case() -> Completion:

## Rename [param folder_path] files to snake_case
func _rename_files_to_snake_case(folder_path: String) -> bool:
return PopochiuUtils.any(
return PopochiuUtils.any_exhaustive(
Array(DirAccess.get_files_at(folder_path)),
func (file: String) -> bool:
var src := folder_path.path_join(file)
Expand All @@ -159,8 +162,8 @@ func _rename_files_to_snake_case(folder_path: String) -> bool:

## Rename [param path] folders and the content in the folders recursively to snake_case
func _rename_folders_to_snake_case(path: String) -> bool:
return PopochiuUtils.any(
PopochiuMigrationHelper.get_absolute_directory_paths_at(path),
return PopochiuUtils.any_exhaustive(
PopochiuEditorHelper.get_absolute_directory_paths_at(path),
func (sub_folder: String) -> bool:
# recursively rename files/folders to snake_case
var any_subfolder_renamed = _rename_folders_to_snake_case(sub_folder)
Expand Down Expand Up @@ -194,7 +197,7 @@ func _select_gui_template() -> Completion:
)
return Completion.DONE
else:
await PopochiuEditorHelper.wait_process_frame()
await PopochiuEditorHelper.frame_processed()
return Completion.IGNORED


Expand Down Expand Up @@ -338,7 +341,7 @@ func _update_characters() -> Completion:
PopochiuResources.CHARACTERS_PATH,
["tscn"]
)
var any_character_updated := PopochiuUtils.any(file_paths, _update_character)
var any_character_updated := PopochiuUtils.any_exhaustive(file_paths, _update_character)
return Completion.DONE if any_character_updated else Completion.IGNORED


Expand Down Expand Up @@ -415,7 +418,9 @@ func _map_voices(emotion_dic: Dictionary, voices: Array) -> Dictionary:
## Update external prop scenes and assign missing scripts for each prop, hotspot, region, and
## walkable area that didn't exist prior [i]beta 1[/i].
func _update_objects_in_rooms() -> Completion:
var any_room_updated := PopochiuUtils.any(PopochiuMigrationHelper.get_rooms(), _update_room)
var any_room_updated := PopochiuUtils.any_exhaustive(
PopochiuEditorHelper.get_rooms(), _update_room
)
return Completion.DONE if any_room_updated else Completion.IGNORED


Expand All @@ -424,7 +429,7 @@ func _update_objects_in_rooms() -> Completion:
func _update_room(popochiu_room: PopochiuRoom) -> bool:
var room_objects_to_add := []
var room_objects_to_check := []
PopochiuUtils.any([
PopochiuUtils.any_exhaustive([
PopochiuPropFactory.new(),
PopochiuHotspotFactory.new(),
PopochiuRegionFactory.new(),
Expand Down
Loading

0 comments on commit 7446676

Please sign in to comment.