@@ -207,22 +207,125 @@ void ECMAScript::_bind_methods() {
207207}
208208
209209RES ResourceFormatLoaderECMAScript::load (const String &p_path, const String &p_original_path, Error *r_error) {
210- Error err;
211-
212- if (r_error)
213- *r_error = ERR_FILE_CANT_OPEN;
214-
210+ Error err = OK;
211+ Ref<ECMAScriptModule> module = ResourceFormatLoaderECMAScriptModule::load_static (p_path, p_original_path, &err);
212+ if (r_error) *r_error = err;
213+ ERR_FAIL_COND_V_MSG (err != OK, RES (), " Cannot load script file '" + p_path + " '." );
215214 Ref<ECMAScript> script;
216215 script.instance ();
217216 script->set_script_path (p_path);
217+ script->bytecode = module ->get_bytecode ();
218+ script->set_source_code (module ->get_source_code ());
219+ err = script->reload ();
220+ if (r_error) *r_error = err;
221+ ERR_FAIL_COND_V_MSG (err != OK, RES (), " Parse source code from file '" + p_path + " ' failed." );
222+ return script;
223+ }
224+
225+ void ResourceFormatLoaderECMAScript::get_recognized_extensions (List<String> *p_extensions) const {
226+ p_extensions->push_front (EXT_JSCLASS);
227+ p_extensions->push_front (EXT_JSCLASS_BYTECODE);
228+ p_extensions->push_front (EXT_JSCLASS_ENCRYPTED);
229+ }
230+
231+ void ResourceFormatLoaderECMAScript::get_recognized_extensions_for_type (const String &p_type, List<String> *p_extensions) const {
232+ get_recognized_extensions (p_extensions);
233+ }
234+
235+ bool ResourceFormatLoaderECMAScript::handles_type (const String &p_type) const {
236+ return p_type == ECMAScript::get_class_static ();
237+ }
238+
239+ String ResourceFormatLoaderECMAScript::get_resource_type (const String &p_path) const {
240+ String el = p_path.get_extension ().to_lower ();
241+ if (el == EXT_JSCLASS || el == EXT_JSCLASS_BYTECODE || el == EXT_JSCLASS_ENCRYPTED) return ECMAScript::get_class_static ();
242+ return " " ;
243+ }
244+
245+ Error ResourceFormatSaverECMAScript::save (const String &p_path, const RES &p_resource, uint32_t p_flags) {
246+
247+ Ref<ECMAScript> script = p_resource;
248+ ERR_FAIL_COND_V (script.is_null (), ERR_INVALID_PARAMETER);
249+
250+ String source = script->get_source_code ();
251+
252+ Error err;
253+ FileAccessRef file = FileAccess::open (p_path, FileAccess::WRITE, &err);
254+ ERR_FAIL_COND_V_MSG (err, err, " Cannot save file '" + p_path + " '." );
255+ file->store_string (source);
256+ if (file->get_error () != OK && file->get_error () != ERR_FILE_EOF) {
257+ return ERR_CANT_CREATE;
258+ }
259+ file->close ();
260+
261+ if (ScriptServer::is_reload_scripts_on_save_enabled ()) {
262+ script->reload ();
263+ }
264+
265+ return OK;
266+ }
267+
268+ void ResourceFormatSaverECMAScript::get_recognized_extensions (const RES &p_resource, List<String> *p_extensions) const {
269+ if (Object::cast_to<ECMAScript>(*p_resource)) {
270+ p_extensions->push_back (EXT_JSCLASS);
271+ }
272+ }
273+
274+ bool ResourceFormatSaverECMAScript::recognize (const RES &p_resource) const {
275+ return Object::cast_to<ECMAScript>(*p_resource) != NULL ;
276+ }
277+
278+ void ECMAScriptModule::_bind_methods () {
279+ ClassDB::bind_method (D_METHOD (" set_script_path" , " script_path" ), &ECMAScriptModule::set_script_path);
280+ ClassDB::bind_method (D_METHOD (" get_script_path" ), &ECMAScriptModule::get_script_path);
281+ ClassDB::bind_method (D_METHOD (" set_source_code" , " source_code" ), &ECMAScriptModule::set_source_code);
282+ ClassDB::bind_method (D_METHOD (" get_source_code" ), &ECMAScriptModule::get_source_code);
283+ ClassDB::bind_method (D_METHOD (" set_bytecode" , " bytecode" ), &ECMAScriptModule::set_bytecode);
284+ ClassDB::bind_method (D_METHOD (" get_bytecode" ), &ECMAScriptModule::get_bytecode);
285+ ADD_PROPERTY (PropertyInfo (Variant::STRING, " script_path" ), " set_script_path" , " get_script_path" );
286+ }
218287
219- if (p_path.ends_with (" ." EXT_JSCLASS)) {
288+ ECMAScriptModule::ECMAScriptModule () {
289+ set_source_code (" module.exports = {};" ENDL);
290+ }
291+
292+ RES ResourceFormatLoaderECMAScriptModule::load (const String &p_path, const String &p_original_path, Error *r_error) {
293+ return load_static (p_path, p_original_path, r_error);
294+ }
295+
296+ void ResourceFormatLoaderECMAScriptModule::get_recognized_extensions (List<String> *p_extensions) const {
297+ p_extensions->push_front (EXT_JSMODULE);
298+ p_extensions->push_front (EXT_JSMODULE_BYTECODE);
299+ p_extensions->push_front (EXT_JSMODULE_ENCRYPTED);
300+ }
301+
302+ void ResourceFormatLoaderECMAScriptModule::get_recognized_extensions_for_type (const String &p_type, List<String> *p_extensions) const {
303+ get_recognized_extensions (p_extensions);
304+ }
305+
306+ bool ResourceFormatLoaderECMAScriptModule::handles_type (const String &p_type) const {
307+ return p_type == ECMAScriptModule::get_class_static ();
308+ }
309+
310+ String ResourceFormatLoaderECMAScriptModule::get_resource_type (const String &p_path) const {
311+ String el = p_path.get_extension ().to_lower ();
312+ if (el == EXT_JSMODULE || el == EXT_JSMODULE_BYTECODE || el == EXT_JSMODULE_ENCRYPTED) return ECMAScriptModule::get_class_static ();
313+ return " " ;
314+ }
315+
316+ RES ResourceFormatLoaderECMAScriptModule::load_static (const String &p_path, const String &p_original_path, Error *r_error) {
317+ Error err = ERR_FILE_CANT_OPEN;
318+ Ref<ECMAScriptModule> module ;
319+ module .instance ();
320+ module ->set_script_path (p_path);
321+ if (p_path.ends_with (" ." EXT_JSMODULE) || p_path.ends_with (" ." EXT_JSCLASS)) {
220322 String code = FileAccess::get_file_as_string (p_path, &err);
323+ if (r_error) *r_error = err;
221324 ERR_FAIL_COND_V_MSG (err != OK, RES (), " Cannot load source code from file '" + p_path + " '." );
222- script ->set_source_code (code);
223- } else if (p_path.ends_with (" ." EXT_JSCLASS_BYTECODE)) {
224- Error err;
225- script-> bytecode = FileAccess::get_file_as_array (p_path, & err) ;
325+ module ->set_source_code (code);
326+ } else if (p_path.ends_with (" ." EXT_JSMODULE_BYTECODE) || p_path. ends_with ( " . " EXT_JSCLASS_BYTECODE)) {
327+ module -> set_bytecode ( FileAccess::get_file_as_array (p_path, & err)) ;
328+ if (r_error) *r_error = err;
226329 ERR_FAIL_COND_V_MSG (err != OK, RES (), " Cannot load bytecode from file '" + p_path + " '." );
227330 } else if (p_path.ends_with (" ." EXT_JSCLASS_ENCRYPTED)) {
228331 FileAccess *fa = FileAccess::open (p_path, FileAccess::READ);
@@ -243,7 +346,7 @@ RES ResourceFormatLoaderECMAScript::load(const String &p_path, const String &p_o
243346 if (code.parse_utf8 ((const char *)encrypted_code.ptr (), encrypted_code.size ())) {
244347 err = ERR_PARSE_ERROR;
245348 } else {
246- script ->set_source_code (code);
349+ module ->set_source_code (code);
247350 }
248351 fa->close ();
249352 fae->close ();
@@ -258,71 +361,32 @@ RES ResourceFormatLoaderECMAScript::load(const String &p_path, const String &p_o
258361 err = ERR_CANT_OPEN;
259362 }
260363 }
261-
262- err = script->reload ();
263- if (OK != err) {
264- ERR_PRINTS (" Cannot parse source code from file '" + p_path + " '." );
265- }
266- if (r_error)
267- *r_error = err;
268-
269- return script;
270- }
271-
272- void ResourceFormatLoaderECMAScript::get_recognized_extensions (List<String> *p_extensions) const {
273- p_extensions->push_front (EXT_JSCLASS);
274- p_extensions->push_front (EXT_JSCLASS_BYTECODE);
275- p_extensions->push_front (EXT_JSCLASS_ENCRYPTED);
364+ if (r_error) *r_error = err;
365+ ERR_FAIL_COND_V (err != OK, RES ());
366+ return module ;
276367}
277368
278- void ResourceFormatLoaderECMAScript::get_recognized_extensions_for_type (const String &p_type, List<String> *p_extensions) const {
279- get_recognized_extensions (p_extensions);
280- }
281-
282- bool ResourceFormatLoaderECMAScript::handles_type (const String &p_type) const {
283- return p_type == " ECMAScript" ;
284- }
285-
286- String ResourceFormatLoaderECMAScript::get_resource_type (const String &p_path) const {
287- String el = p_path.get_extension ().to_lower ();
288- if (el == EXT_JSCLASS || el == EXT_JSCLASS_BYTECODE || el == EXT_JSCLASS_ENCRYPTED) return " ECMAScript" ;
289- return " " ;
290- }
291-
292- Error ResourceFormatSaverECMAScript::save (const String &p_path, const RES &p_resource, uint32_t p_flags) {
293-
294- Ref<ECMAScript> script = p_resource;
295- ERR_FAIL_COND_V (script.is_null (), ERR_INVALID_PARAMETER);
296-
297- String source = script->get_source_code ();
298-
369+ Error ResourceFormatSaverECMAScriptModule::save (const String &p_path, const RES &p_resource, uint32_t p_flags) {
370+ Ref<ECMAScriptModule> module = p_resource;
371+ ERR_FAIL_COND_V (module .is_null (), ERR_INVALID_PARAMETER);
372+ String source = module ->get_source_code ();
299373 Error err;
300- FileAccess *file = FileAccess::open (p_path, FileAccess::WRITE, &err);
301-
302- ERR_FAIL_COND_V_MSG (err, err, " Cannot save ECMAScript file '" + p_path + " '." );
303-
374+ FileAccessRef file = FileAccess::open (p_path, FileAccess::WRITE, &err);
375+ ERR_FAIL_COND_V_MSG (err, err, " Cannot save file '" + p_path + " '." );
304376 file->store_string (source);
305377 if (file->get_error () != OK && file->get_error () != ERR_FILE_EOF) {
306- memdelete (file);
307378 return ERR_CANT_CREATE;
308379 }
309380 file->close ();
310- memdelete (file);
311-
312- if (ScriptServer::is_reload_scripts_on_save_enabled ()) {
313- script->reload ();
314- }
315-
316381 return OK;
317382}
318383
319- void ResourceFormatSaverECMAScript::get_recognized_extensions (const RES &p_resource, List<String> *p_extensions) const {
320-
321- if (Object::cast_to<ECMAScript>(*p_resource)) {
322- p_extensions->push_back (" jsx" );
384+ void ResourceFormatSaverECMAScriptModule::get_recognized_extensions (const RES &p_resource, List<String> *p_extensions) const {
385+ if (Object::cast_to<ECMAScriptModule>(*p_resource)) {
386+ p_extensions->push_back (EXT_JSMODULE);
323387 }
324388}
325389
326- bool ResourceFormatSaverECMAScript ::recognize (const RES &p_resource) const {
327- return Object::cast_to<ECMAScript >(*p_resource) != NULL ;
390+ bool ResourceFormatSaverECMAScriptModule ::recognize (const RES &p_resource) const {
391+ return Object::cast_to<ECMAScriptModule >(*p_resource) != NULL ;
328392}
0 commit comments