From b3aebcf6dfa72dcb4a151b7b17c52ecd0ec5d6f7 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Wed, 1 Feb 2017 20:41:05 -0300 Subject: [PATCH] CSV translation import plugin --- core/io/resource_import.cpp | 13 +- core/io/resource_import.h | 2 +- core/translation.cpp | 14 ++ core/translation.h | 1 + tools/editor/editor_file_system.cpp | 106 ++++++++++++++- tools/editor/editor_file_system.h | 4 + tools/editor/editor_node.cpp | 10 +- .../editor/import/resource_import_texture.cpp | 2 +- tools/editor/import/resource_import_texture.h | 2 +- .../resource_importer_csv_translation.cpp | 126 ++++++++++++++++++ .../resource_importer_csv_translation.h | 27 ++++ 11 files changed, 292 insertions(+), 15 deletions(-) create mode 100644 tools/editor/import/resource_importer_csv_translation.cpp create mode 100644 tools/editor/import/resource_importer_csv_translation.h diff --git a/core/io/resource_import.cpp b/core/io/resource_import.cpp index 18fd8d25e7d..d0799cdbe6c 100644 --- a/core/io/resource_import.cpp +++ b/core/io/resource_import.cpp @@ -109,7 +109,11 @@ void ResourceFormatImporter::get_recognized_extensions_for_type(const String& p_ Set found; for (Set< Ref >::Element *E=importers.front();E;E=E->next()) { - if (!ClassDB::is_parent_class(E->get()->get_resource_type(),p_type)) + String res_type = E->get()->get_resource_type(); + if (res_type==String()) + continue; + + if (!ClassDB::is_parent_class(res_type,p_type)) continue; List local_exts; @@ -138,8 +142,11 @@ bool ResourceFormatImporter::can_be_imported(const String& p_path) const { bool ResourceFormatImporter::handles_type(const String& p_type) const { for (Set< Ref >::Element *E=importers.front();E;E=E->next()) { - print_line("handles "+p_type+" base is "+E->get()->get_resource_type()); - if (ClassDB::is_parent_class(E->get()->get_resource_type(),p_type)) + + String res_type = E->get()->get_resource_type(); + if (res_type==String()) + continue; + if (ClassDB::is_parent_class(res_type,p_type)) return true; } diff --git a/core/io/resource_import.h b/core/io/resource_import.h index d54308b6edb..939cecfbd96 100644 --- a/core/io/resource_import.h +++ b/core/io/resource_import.h @@ -69,7 +69,7 @@ public: virtual bool get_option_visibility(const String& p_option,const Map& p_options) const=0; - virtual Error import(const String& p_source_file,const String& p_save_path,const Map& p_options,List* r_platform_variants)=0; + virtual Error import(const String& p_source_file,const String& p_save_path,const Map& p_options,List* r_platform_variants,List* r_gen_files=NULL)=0; }; diff --git a/core/translation.cpp b/core/translation.cpp index d5ec61b8d6c..8835cb133cb 100644 --- a/core/translation.cpp +++ b/core/translation.cpp @@ -751,6 +751,20 @@ static const char* locale_names[]={ 0 }; +bool TranslationServer::is_locale_valid(const String& p_locale) { + + const char **ptr=locale_list; + + while (*ptr) { + + if (*ptr==p_locale) + return true; + ptr++; + } + + return false; + +} Vector TranslationServer::get_all_locales() { diff --git a/core/translation.h b/core/translation.h index 85ab4a229df..feed3525498 100644 --- a/core/translation.h +++ b/core/translation.h @@ -102,6 +102,7 @@ public: static Vector get_all_locales(); static Vector get_all_locale_names(); + static bool is_locale_valid(const String& p_locale); void set_tool_translation(const Ref& p_translation); StringName tool_translate(const StringName& p_message) const; diff --git a/tools/editor/editor_file_system.cpp b/tools/editor/editor_file_system.cpp index f05aaa683c0..1d7d58591c4 100644 --- a/tools/editor/editor_file_system.cpp +++ b/tools/editor/editor_file_system.cpp @@ -451,8 +451,76 @@ EditorFileSystem::ScanProgress EditorFileSystem::ScanProgress::get_sub(int p_cur sp.hi=slice; return sp; - } + +bool EditorFileSystem::_check_missing_imported_files(const String& p_path) { + + if (!reimport_on_missing_imported_files) + return true; + + Error err; + FileAccess *f= FileAccess::open(p_path+".import",FileAccess::READ,&err); + + if (!f) { + print_line("could not open import for "+p_path); + return false; + } + + VariantParser::StreamFile stream; + stream.f=f; + + String assign; + Variant value; + VariantParser::Tag next_tag; + + int lines=0; + String error_text; + + List to_check; + + while(true) { + + assign=Variant(); + next_tag.fields.clear(); + next_tag.name=String(); + + err = VariantParser::parse_tag_assign_eof(&stream,lines,error_text,next_tag,assign,value,NULL,true); + if (err==ERR_FILE_EOF) { + memdelete(f); + return OK; + } + else if (err!=OK) { + ERR_PRINTS("ResourceFormatImporter::load - "+p_path+".import:"+itos(lines)+" error: "+error_text); + memdelete(f); + return false; + } + + if (assign!=String()) { + if (assign.begins_with("path")) { + to_check.push_back(value); + } else if (assign=="files") { + Array fa = value; + for(int i=0;i::Element *E=to_check.front();E;E=E->next()) { + if (!FileAccess::exists(E->get())) { + print_line("missing "+E->get()+", reimport" ); + return false; + } + } + return true; +} + void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir,DirAccess *da,const ScanProgress& p_progress) { List dirs; @@ -562,7 +630,7 @@ void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir,DirAccess import_mt=FileAccess::get_modified_time(path+".import"); } - if (fc && fc->modification_time==mt && fc->import_modification_time==import_mt) { + if (fc && fc->modification_time==mt && fc->import_modification_time==import_mt && _check_missing_imported_files(path)) { fi->type=fc->type; fi->modified_time=fc->modification_time; @@ -761,6 +829,9 @@ void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir,const S if (import_mt!=p_dir->files[i]->import_modified_time) { print_line("REIMPORT: import modified changed, reimport"); reimport=true; + } else if (!_check_missing_imported_files(path)) { + print_line("REIMPORT: imported files removed"); + reimport=true; } } @@ -1277,11 +1348,13 @@ void EditorFileSystem::_reimport_file(const String& p_file) { String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_file); List import_variants; + List gen_files; - Error err = importer->import(p_file,base_path,params,&import_variants); + Error err = importer->import(p_file,base_path,params,&import_variants,&gen_files); - ERR_EXPLAIN("Error importing: "+p_file); - ERR_FAIL_COND(err!=OK); + if (err!=OK) { + ERR_PRINTS("Error importing: "+p_file); + } //as import is complete, save the .import file @@ -1292,9 +1365,13 @@ void EditorFileSystem::_reimport_file(const String& p_file) { f->store_line("[remap]"); f->store_line(""); f->store_line("importer=\""+importer->get_importer_name()+"\""); - f->store_line("type=\""+importer->get_resource_type()+"\""); + if (importer->get_resource_type()!="") { + f->store_line("type=\""+importer->get_resource_type()+"\""); + } - if (import_variants.size()) { + if (importer->get_save_extension()=="") { + //no path + } else if (import_variants.size()) { //import with variants for(List::Element *E=import_variants.front();E;E=E->next()) { @@ -1307,6 +1384,20 @@ void EditorFileSystem::_reimport_file(const String& p_file) { } f->store_line(""); + if (gen_files.size()) { + f->store_line("[gen]"); + Array genf; + for (List::Element *E=gen_files.front();E;E=E->next()) { + genf.push_back(E->get()); + } + + String value; + VariantWriter::write_to_string(genf,value); + f->store_line("files="+value); + f->store_line(""); + } + + f->store_line("[params]"); f->store_line(""); @@ -1380,6 +1471,7 @@ void EditorFileSystem::_update_extensions() { EditorFileSystem::EditorFileSystem() { + reimport_on_missing_imported_files = GLOBAL_DEF("editor/reimport_missing_imported_files",true); singleton=this; filesystem=memnew( EditorFileSystemDirectory ); //like, empty diff --git a/tools/editor/editor_file_system.h b/tools/editor/editor_file_system.h index 3ad739ae9db..92169907a46 100644 --- a/tools/editor/editor_file_system.h +++ b/tools/editor/editor_file_system.h @@ -195,6 +195,10 @@ class EditorFileSystem : public Node { void _reimport_file(const String &p_file); + bool _check_missing_imported_files(const String& p_path); + + bool reimport_on_missing_imported_files; + protected: void _notification(int p_what); diff --git a/tools/editor/editor_node.cpp b/tools/editor/editor_node.cpp index 56384fb32d5..a51507e96ff 100644 --- a/tools/editor/editor_node.cpp +++ b/tools/editor/editor_node.cpp @@ -100,6 +100,7 @@ #include "plugins/collision_shape_2d_editor_plugin.h" #include "plugins/gi_probe_editor_plugin.h" #include "import/resource_import_texture.h" +#include "import/resource_importer_csv_translation.h" // end #include "editor_settings.h" #include "io_plugins/editor_texture_import_plugin.h" @@ -422,11 +423,11 @@ void EditorNode::_fs_changed() { } if (changed.size()) { - EditorProgress ep("reload_res","Reload Modified Resources",changed.size()); + //EditorProgress ep("reload_res","Reload Modified Resources",changed.size()); int idx=0; for(List >::Element *E=changed.front();E;E=E->next()) { - ep.step(E->get()->get_path(),idx++); + //ep.step(E->get()->get_path(),idx++); E->get()->reload_from_file(); } } @@ -5120,6 +5121,11 @@ EditorNode::EditorNode() { Ref import_texture; import_texture.instance(); ResourceFormatImporter::get_singleton()->add_importer(import_texture); + + Ref import_csv_translation; + import_csv_translation.instance(); + ResourceFormatImporter::get_singleton()->add_importer(import_csv_translation); + } _pvrtc_register_compressors(); diff --git a/tools/editor/import/resource_import_texture.cpp b/tools/editor/import/resource_import_texture.cpp index 41b330e12b8..940b932e547 100644 --- a/tools/editor/import/resource_import_texture.cpp +++ b/tools/editor/import/resource_import_texture.cpp @@ -190,7 +190,7 @@ void ResourceImporterTexture::_save_stex(const Image& p_image,const String& p_to memdelete(f); } -Error ResourceImporterTexture::import(const String& p_source_file,const String& p_save_path,const Map& p_options,List* r_platform_variants) { +Error ResourceImporterTexture::import(const String& p_source_file, const String& p_save_path, const Map& p_options, List* r_platform_variants, List *r_gen_files) { int compress_mode = p_options["compress/mode"]; float lossy= p_options["compress/lossy_quality"]; diff --git a/tools/editor/import/resource_import_texture.h b/tools/editor/import/resource_import_texture.h index ac0a3f05751..84f7b77838a 100644 --- a/tools/editor/import/resource_import_texture.h +++ b/tools/editor/import/resource_import_texture.h @@ -35,7 +35,7 @@ public: void _save_stex(const Image& p_image, const String& p_to_path, int p_compress_mode, float p_lossy_quality, Image::CompressMode p_vram_compression, bool p_mipmaps, int p_texture_flags, bool p_streamable); - virtual Error import(const String& p_source_file,const String& p_save_path,const Map& p_options,List* r_platform_variants); + virtual Error import(const String& p_source_file,const String& p_save_path,const Map& p_options,List* r_platform_variants,List* r_gen_files=NULL); ResourceImporterTexture(); }; diff --git a/tools/editor/import/resource_importer_csv_translation.cpp b/tools/editor/import/resource_importer_csv_translation.cpp new file mode 100644 index 00000000000..f14c10fb998 --- /dev/null +++ b/tools/editor/import/resource_importer_csv_translation.cpp @@ -0,0 +1,126 @@ + +#include "resource_importer_csv_translation.h" +#include "os/file_access.h" +#include "translation.h" +#include "io/resource_saver.h" +#include "compressed_translation.h" + +String ResourceImporterCSVTranslation::get_importer_name() const { + + return "csv_translation"; +} + +String ResourceImporterCSVTranslation::get_visible_name() const{ + + return "CSV Translation"; +} +void ResourceImporterCSVTranslation::get_recognized_extensions(List *p_extensions) const{ + + p_extensions->push_back("csv"); +} + +String ResourceImporterCSVTranslation::get_save_extension() const { + return ""; //does not save a single resoure +} + +String ResourceImporterCSVTranslation::get_resource_type() const{ + + return "StreamCSVTranslation"; +} + +bool ResourceImporterCSVTranslation::get_option_visibility(const String& p_option,const Map& p_options) const { + + return true; +} + +int ResourceImporterCSVTranslation::get_preset_count() const { + return 0; +} +String ResourceImporterCSVTranslation::get_preset_name(int p_idx) const { + + return ""; +} + + +void ResourceImporterCSVTranslation::get_import_options(List *r_options,int p_preset) const { + + + r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL,"compress"),true)); + +} + + + +Error ResourceImporterCSVTranslation::import(const String& p_source_file, const String& p_save_path, const Map& p_options, List* r_platform_variants, List *r_gen_files) { + + + bool compress = p_options["compress"]; + FileAccessRef f = FileAccess::open(p_source_file,FileAccess::READ); + + ERR_FAIL_COND_V( !f, ERR_INVALID_PARAMETER ); + + Vector line = f->get_csv_line(); + if (line.size()<=1) { + return ERR_PARSE_ERROR; + } + + Vector locales; + Vector > translations; + + for(int i=1;i translation; + translation.instance(); + translation->set_locale(locale); + translations.push_back(translation); + } + + line = f->get_csv_line(); + + while(line.size()==locales.size()+1) { + + String key = line[0]; + if (key!="") { + + for(int i=1;iadd_message(key,line[i]); + } + } + + line = f->get_csv_line(); + } + + + for(int i=0;i xlt = translations[i]; + + if (compress) { + Ref cxl = memnew( PHashTranslation ); + cxl->generate( xlt ); + xlt=cxl; + } + + String save_path = p_source_file.get_basename()+"."+translations[i]->get_locale()+".xl"; + + ResourceSaver::save(save_path,xlt); + if (r_gen_files) { + r_gen_files->push_back(save_path); + } + } + + + + return OK; + +} + +ResourceImporterCSVTranslation::ResourceImporterCSVTranslation() +{ + +} diff --git a/tools/editor/import/resource_importer_csv_translation.h b/tools/editor/import/resource_importer_csv_translation.h new file mode 100644 index 00000000000..d08218e7d93 --- /dev/null +++ b/tools/editor/import/resource_importer_csv_translation.h @@ -0,0 +1,27 @@ +#ifndef RESOURCEIMPORTERCSVTRANSLATION_H +#define RESOURCEIMPORTERCSVTRANSLATION_H + +#include "io/resource_import.h" + + +class ResourceImporterCSVTranslation : public ResourceImporter { + GDCLASS(ResourceImporterCSVTranslation,ResourceImporter) +public: + virtual String get_importer_name() const; + virtual String get_visible_name() const; + virtual void get_recognized_extensions(List *p_extensions) const; + virtual String get_save_extension() const; + virtual String get_resource_type() const; + + virtual int get_preset_count() const; + virtual String get_preset_name(int p_idx) const; + + virtual void get_import_options(List *r_options,int p_preset=0) const; + virtual bool get_option_visibility(const String& p_option,const Map& p_options) const; + + virtual Error import(const String& p_source_file,const String& p_save_path,const Map& p_options,List* r_platform_variants,List* r_gen_files=NULL); + + ResourceImporterCSVTranslation(); +}; + +#endif // RESOURCEIMPORTERCSVTRANSLATION_H