@@ -119,32 +119,30 @@ cdef class PythonResourceFormatLoader:
119119 cdef PythonScript script
120120 cdef gd_string_t gd_source
121121 cdef gd_string_t gd_script_path
122- cdef object source_code
122+ cdef GDString source_code
123123
124124 # Create a new PythonScript instance
125125 script = PythonScript()
126126
127127 # Try to load the source code from file
128- try :
129- with open (py_original_path, ' r' , encoding = ' utf-8' ) as f:
130- source_code = f.read()
131-
132- # Set the source code on the script
133- gd_source = gd_string_from_unchecked_pystr(source_code)
134- script._set_source_code(gd_source)
135- gd_string_del(& gd_source)
128+ from godot.classes import FileAccess
129+ # TODO: use `path` directly !
130+ cdef object file = FileAccess.open(GDString(py_path), FileAccess.ModeFlags.READ.value)
131+ if file is None :
132+ spy_log(f" Failed to load Python script {py_original_path}: cannot open file {path}" )
133+ # If file loading fails, return the nil variant (already initialized)
134+ return ret
135+ # TODO: what happen if the text is not UTF8 ?
136+ source_code = file .get_as_text()
136137
137- # Set the script path
138- gd_script_path = gd_string_from_unchecked_pystr(py_original_path)
139- script._set_path(gd_script_path)
140- gd_string_del(& gd_script_path)
138+ # Set the source code on the script
139+ script._set_source_code(source_code._gd_data)
141140
142- # Return the script as a variant
143- ret = gd_object_into_variant( script._gd_ptr )
141+ # Set the script path
142+ script._set_path(original_path )
144143
145- except Exception as e:
146- # If file loading fails, return the nil variant (already initialized)
147- spy_log(f" Failed to load Python script {py_original_path}: {e}" )
144+ # Return the script as a variant
145+ ret = gd_object_into_variant(script._gd_ptr)
148146
149147 return ret
150148
0 commit comments