28
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
29
/* *************************************************************************/
30
30
31
+ /* This is implementation is for loading/saving JS files. */
32
+ /* After loading it contains the code/bytecode of the loaded file. */
33
+
31
34
#include " javascript.h"
32
35
#include " core/config/engine.h"
33
36
#include " core/io/file_access_encrypted.h"
@@ -110,8 +113,17 @@ Error JavaScript::reload(bool p_keep_state) {
110
113
javascript_class = NULL ;
111
114
Error err = OK;
112
115
JavaScriptBinder *binder = JavaScriptLanguage::get_thread_binder (Thread::get_caller_id ());
116
+ #ifdef TOOLS_ENABLED
117
+ // This is a workaround for files generated outside of the godot editor
118
+ if (binder == NULL ) {
119
+ binder = JavaScriptLanguage::get_thread_binder (Thread::MAIN_ID);
120
+ }
121
+ #endif
113
122
ERR_FAIL_COND_V_MSG (binder == NULL , ERR_INVALID_DATA, " Cannot load script in this thread" );
114
123
JavaScriptError js_err;
124
+
125
+ // TODO: We should have a setting/option to skip parsing or reading .mjs files which aren't Godot classes for example "chunk-xxx.mjs" build from esbuild
126
+
115
127
if (!bytecode.is_empty ()) {
116
128
javascript_class = binder->parse_javascript_class (bytecode, script_path, true , &js_err);
117
129
} else {
@@ -268,6 +280,11 @@ Ref<Resource> ResourceFormatLoaderJavaScript::load(const String &p_path, const S
268
280
Ref<JavaScriptModule> module = ResourceFormatLoaderJavaScriptModule::load_static (p_path, p_original_path, &err);
269
281
if (r_error)
270
282
*r_error = err;
283
+
284
+ if (err == ERR_FILE_NOT_FOUND) {
285
+ return module ;
286
+ }
287
+
271
288
ERR_FAIL_COND_V_MSG (err != OK, Ref<Resource>(), " Cannot load script file '" + p_path + " '." );
272
289
Ref<JavaScript> javaScript;
273
290
javaScript.instantiate ();
@@ -377,9 +394,17 @@ String ResourceFormatLoaderJavaScriptModule::get_resource_type(const String &p_p
377
394
378
395
Ref<Resource> ResourceFormatLoaderJavaScriptModule::load_static (const String &p_path, const String &p_original_path, Error *r_error) {
379
396
Error err = ERR_FILE_CANT_OPEN;
397
+ bool fileExists = FileAccess::exists (p_path);
398
+ if (!fileExists) {
399
+ *r_error = ERR_FILE_NOT_FOUND;
400
+ WARN_PRINT (" Cannot find file '" + p_path + " '. Maybe you deleted the file." );
401
+ return Ref<Resource>();
402
+ }
403
+
380
404
Ref<JavaScriptModule> module ;
381
405
module .instantiate ();
382
406
module ->set_script_path (p_path);
407
+
383
408
if (p_path.ends_with (" ." EXT_JSMODULE) || p_path.ends_with (" ." EXT_JSCLASS) || p_path.ends_with (" ." EXT_JSON)) {
384
409
String code = FileAccess::get_file_as_string (p_path, &err);
385
410
if (r_error)
@@ -388,6 +413,7 @@ Ref<Resource> ResourceFormatLoaderJavaScriptModule::load_static(const String &p_
388
413
module ->set_source_code (code);
389
414
}
390
415
416
+ // TODO: Check what this block is for
391
417
#if 0
392
418
else if (p_path.ends_with("." EXT_JSMODULE_BYTECODE) || p_path.ends_with("." EXT_JSCLASS_BYTECODE)) {
393
419
module->set_bytecode(FileAccess::get_file_as_array(p_path, &err));
0 commit comments