Skip to content

Commit

Permalink
Made it work for multiple instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Variable-ind committed Feb 15, 2025
1 parent f81648d commit d3575c4
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions src/HandleExtensions.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var damaged_extensions := PackedStringArray()
var prev_damaged_extensions := PackedStringArray()
## Extensions built using the versions in this array are considered compatible with the current Api
var legacy_api_versions = [5, 4]
var sane_timer := Timer.new() # Used to ping that at least one session is alive during Timer's run.


class Extension:
Expand Down Expand Up @@ -52,7 +53,7 @@ class Extension:

func _ready() -> void:
_add_internal_extensions()
prev_damaged_extensions = remove_damaged()
prev_damaged_extensions = initialize_extension_monitor()
if !prev_damaged_extensions.is_empty():
if prev_damaged_extensions.size() == 1:
# gdlint: ignore=max-line-length
Expand Down Expand Up @@ -263,25 +264,34 @@ func uninstall_extension(file_name := "", remove_mode := UninstallMode.REMOVE_PE
extension_uninstalled.emit(file_name)


func remove_damaged() -> PackedStringArray:
func initialize_extension_monitor() -> PackedStringArray:
var tester_file: FileAccess # For testing and deleting damaged extensions
# Remove any extension that was proven guilty before this extension is loaded
if FileAccess.file_exists(EXTENSIONS_PATH.path_join("Faulty.txt")):
# This code will only run if pixelorama crashed
var faulty_path := EXTENSIONS_PATH.path_join("Faulty.txt")
sane_timer.wait_time = 10 # Ping that at least one session is alive during this time
add_child(sane_timer)
sane_timer.timeout.connect(update_monitoring_time)
sane_timer.start()
if FileAccess.file_exists(EXTENSIONS_PATH.path_join("Monitoring.ini")):
# This code will decide if pixelorama crashed or not
var faulty_path := EXTENSIONS_PATH.path_join("Monitoring.ini")
tester_file = FileAccess.open(faulty_path, FileAccess.READ)
var damaged_extension_names = str_to_var(tester_file.get_as_text())
var last_update_time = str_to_var(tester_file.get_line())
var damaged_extension_names = str_to_var(tester_file.get_line())
tester_file.close()
DirAccess.remove_absolute(EXTENSIONS_PATH.path_join("Faulty.txt"))
if typeof(last_update_time) == TYPE_INT:
if int(Time.get_unix_time_from_system()) - last_update_time <= sane_timer.wait_time:
return PackedStringArray() # Assume the file is still in use (session didn't crash)
# If this line is reached then it's likely that the app crashed last session
DirAccess.remove_absolute(EXTENSIONS_PATH.path_join("Monitoring.ini"))
if typeof(damaged_extension_names) == TYPE_PACKED_STRING_ARRAY:
if damaged_extension_names.size() == 1: # We are certain which extension crashed
# don't delete the extension permanently
# (so that it may be given to the developer in the bug report)
# NOTE: get_file() is used as a counermeasure towards possible malicius tampering
# with Faulty.txt file (to inject paths leading outside EXTENSIONS_PATH using "../")
# with Monitoring.ini file (to inject paths leading outside EXTENSIONS_PATH using "../")
var extension_name = damaged_extension_names[0].get_file()
DirAccess.make_dir_recursive_absolute(BUG_EXTENSIONS_PATH)
if FileAccess.file_exists(EXTENSIONS_PATH.path_join(extension_name)):
# don't delete the extension permanently
# (so that it may be given to the developer in the bug report)
DirAccess.rename_absolute(
EXTENSIONS_PATH.path_join(extension_name),
BUG_EXTENSIONS_PATH.path_join(extension_name)
Expand All @@ -294,10 +304,11 @@ func add_suspicion(extension_name: StringName):
# The new (about to load) extension will be considered guilty till it's proven innocent
if not extension_name in damaged_extensions:
var tester_file := FileAccess.open(
EXTENSIONS_PATH.path_join("Faulty.txt"), FileAccess.WRITE
EXTENSIONS_PATH.path_join("Monitoring.ini"), FileAccess.WRITE
)
damaged_extensions.append(extension_name)
tester_file.store_string(var_to_str(damaged_extensions))
tester_file.store_line(var_to_str(int(Time.get_unix_time_from_system())))
tester_file.store_line(var_to_str(damaged_extensions))
tester_file.close()


Expand All @@ -307,9 +318,23 @@ func clear_suspicion(extension_name: StringName):
# Delete the faulty.txt, if there are no more damaged extensions, else update it
if !damaged_extensions.is_empty():
var tester_file := FileAccess.open(
EXTENSIONS_PATH.path_join("Faulty.txt"), FileAccess.WRITE
EXTENSIONS_PATH.path_join("Monitoring.ini"), FileAccess.WRITE
)
tester_file.store_string(var_to_str(damaged_extensions))
tester_file.store_line(var_to_str(int(Time.get_unix_time_from_system())))
tester_file.store_line(var_to_str(damaged_extensions))
tester_file.close()
else:
DirAccess.remove_absolute(EXTENSIONS_PATH.path_join("Faulty.txt"))
DirAccess.remove_absolute(EXTENSIONS_PATH.path_join("Monitoring.ini"))


func update_monitoring_time():
var tester_file := FileAccess.open(EXTENSIONS_PATH.path_join("Monitoring.ini"), FileAccess.READ)
var active_extensions_str: String
if FileAccess.get_open_error() == OK:
tester_file.get_line() # Ignore first line
active_extensions_str = tester_file.get_line()
tester_file.close()
tester_file = FileAccess.open(EXTENSIONS_PATH.path_join("Monitoring.ini"), FileAccess.WRITE)
tester_file.store_line(var_to_str(int(Time.get_unix_time_from_system())))
tester_file.store_line(active_extensions_str)
tester_file.close()

0 comments on commit d3575c4

Please sign in to comment.