Ensure that a base language is loaded before custom theme

Main goal of this patch is to improve error handling when a custom
theme does not contain a valid theme version. We were getting a
message saying that the theme_ver_err resource did not exist as
well as an error saying that the package had failed to load. Now
we properly get a single error message about the theme version.

Change-Id: I361ef2d139220bbeaf2e856be919dc3a50ae4299
diff --git a/gui/pages.cpp b/gui/pages.cpp
index 250fb4c..932be6b 100644
--- a/gui/pages.cpp
+++ b/gui/pages.cpp
@@ -50,6 +50,7 @@
 #include "blanktimer.hpp"
 
 #define TW_THEME_VERSION 1
+#define TW_THEME_VER_ERR -2
 
 extern int gGuiRunning;
 
@@ -721,7 +722,7 @@
 	return 0;
 }
 
-int PageSet::Load(ZipArchive* package, char* xmlFile, char* languageFile)
+int PageSet::Load(ZipArchive* package, char* xmlFile, char* languageFile, char* baseLanguageFile)
 {
 	xml_document<> mDoc;
 	xml_node<>* parent;
@@ -736,6 +737,9 @@
 
 	set_scale_values(1, 1); // Reset any previous scaling values
 
+	if (baseLanguageFile)
+		LoadLanguage(baseLanguageFile, NULL);
+
 	// Now, let's parse the XML
 	child = parent->first_node("details");
 	if (child) {
@@ -751,7 +755,7 @@
 			if (package) {
 				gui_err("theme_ver_err=Custom theme version does not match TWRP version. Using stock theme.");
 				mDoc.clear();
-				return -1;
+				return TW_THEME_VER_ERR;
 			} else {
 				gui_print_color("warning", "Stock theme version does not match TWRP version.\n");
 			}
@@ -1353,6 +1357,7 @@
 	long len;
 	char* xmlFile = NULL;
 	char* languageFile = NULL;
+	char* baseLanguageFile = NULL;
 	PageSet* pageSet = NULL;
 	int ret;
 	MemMapping map;
@@ -1390,6 +1395,7 @@
 		package = "ui.xml";
 		LoadLanguageList(pZip);
 		languageFile = LoadFileToBuffer("languages/en.xml", pZip);
+		baseLanguageFile = LoadFileToBuffer(TWRES "languages/en.xml", NULL);
 	}
 
 	xmlFile = LoadFileToBuffer(package, pZip);
@@ -1400,19 +1406,17 @@
 	// Before loading, mCurrentSet must be the loading package so we can find resources
 	pageSet = mCurrentSet;
 	mCurrentSet = new PageSet(xmlFile);
-	ret = mCurrentSet->Load(pZip, xmlFile, languageFile);
+	ret = mCurrentSet->Load(pZip, xmlFile, languageFile, baseLanguageFile);
 	if (languageFile) {
 		free(languageFile);
 		languageFile = NULL;
 	}
-	if (ret == 0)
-	{
+	if (ret == 0) {
 		mCurrentSet->SetPage(startpage);
 		mPageSets.insert(std::pair<std::string, PageSet*>(name, mCurrentSet));
-	}
-	else
-	{
-		LOGERR("Package %s failed to load.\n", name.c_str());
+	} else {
+		if (ret != TW_THEME_VER_ERR)
+			LOGERR("Package %s failed to load.\n", name.c_str());
 	}
 
 	// The first successful package we loaded is the base
diff --git a/gui/pages.hpp b/gui/pages.hpp
index 87e1fb5..b2f2580 100644
--- a/gui/pages.hpp
+++ b/gui/pages.hpp
@@ -91,7 +91,7 @@
 
 public:
 	int LoadLanguage(char* languageFile, ZipArchive* package);
-	int Load(ZipArchive* package, char* xmlFile, char* languageFile);
+	int Load(ZipArchive* package, char* xmlFile, char* languageFile, char* baseLanguageFile);
 	int CheckInclude(ZipArchive* package, xml_document<> *parentDoc);
 
 	Page* FindPage(std::string name);