فهرست منبع

PO loader: Fix unclosed files and error messages

Fixes #40324.

(cherry picked from commit 47cc2029721735be841291d81bef4d0aea634055)

Also removes empty `p_path` as done in 4857648a16585bbd0fb2fbc33d3d0f768b8223b5.
Rémi Verschelde 5 سال پیش
والد
کامیت
a06ee5e763
3فایلهای تغییر یافته به همراه16 افزوده شده و 15 حذف شده
  1. 14 13
      core/io/translation_loader_po.cpp
  2. 1 1
      core/io/translation_loader_po.h
  3. 1 1
      editor/editor_settings.cpp

+ 14 - 13
core/io/translation_loader_po.cpp

@@ -33,10 +33,8 @@
 #include "core/os/file_access.h"
 #include "core/translation.h"
 
-RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const String &p_path) {
-
+RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
 	enum Status {
-
 		STATUS_NONE,
 		STATUS_READING_ID,
 		STATUS_READING_STRING,
@@ -56,6 +54,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
 	bool skip_this = false;
 	bool skip_next = false;
 	bool is_eof = false;
+	const String path = f->get_path();
 
 	while (!is_eof) {
 
@@ -67,7 +66,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
 
 			if (status == STATUS_READING_ID) {
 				memdelete(f);
-				ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected EOF while reading 'msgid' at file: ");
+				ERR_FAIL_V_MSG(RES(), "Unexpected EOF while reading 'msgid' at: " + path + ":" + itos(line));
 			} else {
 				break;
 			}
@@ -76,9 +75,8 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
 		if (l.begins_with("msgid")) {
 
 			if (status == STATUS_READING_ID) {
-
 				memdelete(f);
-				ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected 'msgid', was expecting 'msgstr' while parsing: ");
+				ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line));
 			}
 
 			if (msg_id != "") {
@@ -98,9 +96,8 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
 		if (l.begins_with("msgstr")) {
 
 			if (status != STATUS_READING_ID) {
-
 				memdelete(f);
-				ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected 'msgstr', was expecting 'msgid' while parsing: ");
+				ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr', was expecting 'msgid' while parsing: " + path + ":" + itos(line));
 			}
 
 			l = l.substr(6, l.length()).strip_edges();
@@ -115,7 +112,10 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
 			continue; //nothing to read or comment
 		}
 
-		ERR_FAIL_COND_V_MSG(!l.begins_with("\"") || status == STATUS_NONE, RES(), p_path + ":" + itos(line) + " Invalid line '" + l + "' while parsing: ");
+		if (!l.begins_with("\"") || status == STATUS_NONE) {
+			memdelete(f);
+			ERR_FAIL_V_MSG(RES(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line));
+		}
 
 		l = l.substr(1, l.length());
 		//find final quote
@@ -128,7 +128,10 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
 			}
 		}
 
-		ERR_FAIL_COND_V_MSG(end_pos == -1, RES(), p_path + ":" + itos(line) + " Expected '\"' at end of message while parsing file: ");
+		if (end_pos == -1) {
+			memdelete(f);
+			ERR_FAIL_V_MSG(RES(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line));
+		}
 
 		l = l.substr(0, end_pos);
 		l = l.c_unescape();
@@ -141,7 +144,6 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
 		line++;
 	}
 
-	f->close();
 	memdelete(f);
 
 	if (status == STATUS_READING_STRING) {
@@ -153,7 +155,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
 			config = msg_str;
 	}
 
-	ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + p_path + ".");
+	ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + path + ".");
 
 	Vector<String> configs = config.split("\n");
 	for (int i = 0; i < configs.size(); i++) {
@@ -190,7 +192,6 @@ RES TranslationLoaderPO::load(const String &p_path, const String &p_original_pat
 void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const {
 
 	p_extensions->push_back("po");
-	//p_extensions->push_back("mo"); //mo in the future...
 }
 bool TranslationLoaderPO::handles_type(const String &p_type) const {
 

+ 1 - 1
core/io/translation_loader_po.h

@@ -37,7 +37,7 @@
 
 class TranslationLoaderPO : public ResourceFormatLoader {
 public:
-	static RES load_translation(FileAccess *f, Error *r_error, const String &p_path = String());
+	static RES load_translation(FileAccess *f, Error *r_error);
 	virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
 	virtual void get_recognized_extensions(List<String> *p_extensions) const;
 	virtual bool handles_type(const String &p_type) const;

+ 1 - 1
editor/editor_settings.cpp

@@ -997,7 +997,7 @@ void EditorSettings::setup_language() {
 			FileAccessMemory *fa = memnew(FileAccessMemory);
 			fa->open_custom(data.ptr(), data.size());
 
-			Ref<Translation> tr = TranslationLoaderPO::load_translation(fa, NULL, "translation_" + String(etl->lang));
+			Ref<Translation> tr = TranslationLoaderPO::load_translation(fa, NULL);
 
 			if (tr.is_valid()) {
 				tr->set_locale(etl->lang);