CSV translation import plugin

This commit is contained in:
Juan Linietsky 2017-02-01 20:41:05 -03:00
parent 2cd2ca7bbc
commit b3aebcf6df
11 changed files with 292 additions and 15 deletions

View file

@ -109,7 +109,11 @@ void ResourceFormatImporter::get_recognized_extensions_for_type(const String& p_
Set<String> found;
for (Set< Ref<ResourceImporter> >::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<String> 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<ResourceImporter> >::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;
}

View file

@ -69,7 +69,7 @@ public:
virtual bool get_option_visibility(const String& p_option,const Map<StringName,Variant>& p_options) const=0;
virtual Error import(const String& p_source_file,const String& p_save_path,const Map<StringName,Variant>& p_options,List<String>* r_platform_variants)=0;
virtual Error import(const String& p_source_file,const String& p_save_path,const Map<StringName,Variant>& p_options,List<String>* r_platform_variants,List<String>* r_gen_files=NULL)=0;
};

View file

@ -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<String> TranslationServer::get_all_locales() {

View file

@ -102,6 +102,7 @@ public:
static Vector<String> get_all_locales();
static Vector<String> get_all_locale_names();
static bool is_locale_valid(const String& p_locale);
void set_tool_translation(const Ref<Translation>& p_translation);
StringName tool_translate(const StringName& p_message) const;

View file

@ -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<String> 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<fa.size();i++) {
to_check.push_back(fa[i]);
}
}
} else if (next_tag.name!="remap" && next_tag.name!="deps") {
break;
}
}
memdelete(f);
for (List<String>::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<String> 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<String> import_variants;
List<String> 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<String>::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<String>::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

View file

@ -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);

View file

@ -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<Ref<Resource> >::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<ResourceImporterTexture> import_texture;
import_texture.instance();
ResourceFormatImporter::get_singleton()->add_importer(import_texture);
Ref<ResourceImporterCSVTranslation> import_csv_translation;
import_csv_translation.instance();
ResourceFormatImporter::get_singleton()->add_importer(import_csv_translation);
}
_pvrtc_register_compressors();

View file

@ -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<StringName,Variant>& p_options,List<String>* r_platform_variants) {
Error ResourceImporterTexture::import(const String& p_source_file, const String& p_save_path, const Map<StringName,Variant>& p_options, List<String>* r_platform_variants, List<String> *r_gen_files) {
int compress_mode = p_options["compress/mode"];
float lossy= p_options["compress/lossy_quality"];

View file

@ -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<StringName,Variant>& p_options,List<String>* r_platform_variants);
virtual Error import(const String& p_source_file,const String& p_save_path,const Map<StringName,Variant>& p_options,List<String>* r_platform_variants,List<String>* r_gen_files=NULL);
ResourceImporterTexture();
};

View file

@ -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<String> *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<StringName,Variant>& 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<ImportOption> *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<StringName,Variant>& p_options, List<String>* r_platform_variants, List<String> *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<String> line = f->get_csv_line();
if (line.size()<=1) {
return ERR_PARSE_ERROR;
}
Vector<String> locales;
Vector<Ref<Translation> > translations;
for(int i=1;i<line.size();i++) {
String locale = line[i];
if (!TranslationServer::is_locale_valid(locale)) {
return ERR_PARSE_ERROR;
}
locales.push_back(locale);
Ref<Translation> 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;i<line.size();i++) {
translations[i-1]->add_message(key,line[i]);
}
}
line = f->get_csv_line();
}
for(int i=0;i<translations.size();i++) {
Ref<Translation> xlt = translations[i];
if (compress) {
Ref<PHashTranslation> 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()
{
}

View file

@ -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<String> *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<ImportOption> *r_options,int p_preset=0) const;
virtual bool get_option_visibility(const String& p_option,const Map<StringName,Variant>& p_options) const;
virtual Error import(const String& p_source_file,const String& p_save_path,const Map<StringName,Variant>& p_options,List<String>* r_platform_variants,List<String>* r_gen_files=NULL);
ResourceImporterCSVTranslation();
};
#endif // RESOURCEIMPORTERCSVTRANSLATION_H