- added the beginning work to support translatable techtrees

This commit is contained in:
Mark Vejvoda 2012-05-02 20:46:47 +00:00
parent 5c0db25c86
commit 897346d1f5
21 changed files with 141 additions and 47 deletions

View File

@ -63,7 +63,7 @@ ProduceTask::ProduceTask(const ResourceType *resourceType) : Task() {
string ProduceTask::toString() const{ string ProduceTask::toString() const{
string str= "Produce "; string str= "Produce ";
if(unitType!=NULL){ if(unitType!=NULL){
str+= unitType->getName(); str+= unitType->getName(true);
} }
return str; return str;
} }
@ -141,7 +141,7 @@ BuildTask::BuildTask(const UnitType *unitType, const Vec2i &pos){
string BuildTask::toString() const{ string BuildTask::toString() const{
string str= "Build "; string str= "Build ";
if(unitType!=NULL){ if(unitType!=NULL){
str+= unitType->getName(); str+= unitType->getName(true);
} }
return str; return str;
} }

View File

@ -17,10 +17,13 @@
#include <algorithm> #include <algorithm>
#include "xml_parser.h" #include "xml_parser.h"
#include "config.h" #include "config.h"
#include "util.h" //#include "util.h"
#include "platform_common.h"
#include "conversion.h"
#include "leak_dumper.h" #include "leak_dumper.h"
using namespace Shared::Util; using namespace Shared::Util;
using namespace Shared::PlatformCommon;
using Shared::Xml::XmlNode; using Shared::Xml::XmlNode;
namespace Glest{ namespace Game{ namespace Glest{ namespace Game{

View File

@ -24,6 +24,7 @@
#include "core_data.h" #include "core_data.h"
#include "renderer.h" #include "renderer.h"
#include <algorithm> #include <algorithm>
#include "config.h"
#include "leak_dumper.h" #include "leak_dumper.h"
using namespace std; using namespace std;
@ -238,6 +239,48 @@ void Lang::loadScenarioStrings(string scenarioDir, string scenarioName){
} }
} }
void Lang::loadTechTreeStrings(string techTree) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] techTree = [%s]\n",__FILE__,__FUNCTION__,__LINE__,techTree.c_str());
string currentPath = "";
Config &config = Config::getInstance();
vector<string> techPaths = config.getPathListForType(ptTechs);
for(int idx = 0; idx < techPaths.size(); idx++) {
string &techPath = techPaths[idx];
endPathWithSlash(techPath);
//printf("techPath [%s]\n",techPath.c_str());
if(folderExists(techPath + techTree) == true) {
currentPath = techPath;
endPathWithSlash(currentPath);
break;
}
}
string techTreeFolder = currentPath + techTree + "/";
string path = techTreeFolder + "lang/" + techTree + "_" + language + ".lng";
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path = [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str());
techTreeStrings.clear();
//try to load the current language first
if(fileExists(path)) {
techTreeStrings.load(path);
}
else {
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path not found [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str());
//try english otherwise
path = techTreeFolder + "lang/" + techTree + "_english.lng";
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path = [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str());
if(fileExists(path)) {
techTreeStrings.load(path);
}
}
}
bool Lang::hasString(const string &s, string uselanguage, bool fallbackToDefault) { bool Lang::hasString(const string &s, string uselanguage, bool fallbackToDefault) {
bool result = false; bool result = false;
try { try {
@ -333,6 +376,27 @@ string Lang::getScenarioString(const string &s) {
} }
} }
string Lang::getTechTreeString(const string &s,const char *defaultValue) {
try{
string result = "";
if(techTreeStrings.hasString(s) == true || defaultValue == NULL) {
result = techTreeStrings.getString(s);
}
else if(defaultValue != NULL) {
result = defaultValue;
}
replaceAll(result, "\\n", "\n");
return result;
}
catch(exception &ex) {
if(techTreeStrings.getpath() != "") {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
}
return "???" + s + "???";
}
}
bool Lang::fileMatchesISO630Code(string uselanguage, string testLanguageFile) { bool Lang::fileMatchesISO630Code(string uselanguage, string testLanguageFile) {
bool result = false; bool result = false;
Properties stringsTest; Properties stringsTest;

View File

@ -37,6 +37,7 @@ private:
Properties strings; Properties strings;
Properties scenarioStrings; Properties scenarioStrings;
Properties techTreeStrings;
std::map<string,Properties> otherLanguageStrings; std::map<string,Properties> otherLanguageStrings;
@ -54,10 +55,12 @@ public:
void loadStrings(string uselanguage, bool loadFonts=true, bool fallbackToDefault=false); void loadStrings(string uselanguage, bool loadFonts=true, bool fallbackToDefault=false);
void loadScenarioStrings(string scenarioDir, string scenarioName); void loadScenarioStrings(string scenarioDir, string scenarioName);
void loadTechTreeStrings(string techTree);
string get(const string &s,string uselanguage="", bool fallbackToDefault=false); string get(const string &s,string uselanguage="", bool fallbackToDefault=false);
bool hasString(const string &s, string uselanguage="", bool fallbackToDefault=false); bool hasString(const string &s, string uselanguage="", bool fallbackToDefault=false);
string getScenarioString(const string &s); string getScenarioString(const string &s);
string getTechTreeString(const string &s, const char *defaultValue=NULL);
string getLanguage() const { return language; } string getLanguage() const { return language; }
bool isLanguageLocal(string compareLanguage) const; bool isLanguageLocal(string compareLanguage) const;

View File

@ -769,7 +769,7 @@ void Gui::computeDisplay(){
const Object *selectedResourceObject =getSelectedResourceObject(); const Object *selectedResourceObject =getSelectedResourceObject();
if(selection.isEmpty() && selectedResourceObject != NULL) { if(selection.isEmpty() && selectedResourceObject != NULL) {
Resource *r = selectedResourceObject->getResource(); Resource *r = selectedResourceObject->getResource();
display.setTitle(r->getType()->getName()); display.setTitle(r->getType()->getName(true));
display.setText(lang.get("Amount")+ ": "+intToStr(r->getAmount())+" / "+intToStr(r->getType()->getDefResPerPatch())); display.setText(lang.get("Amount")+ ": "+intToStr(r->getAmount())+" / "+intToStr(r->getType()->getDefResPerPatch()));
//display.setProgressBar(r->); //display.setProgressBar(r->);
display.setUpImage(0, r->getType()->getImage()); display.setUpImage(0, r->getType()->getImage());

View File

@ -60,7 +60,7 @@ void Resource::init(const ResourceType *rt, const Vec2i &pos) {
string Resource::getDescription() const { string Resource::getDescription() const {
string str; string str;
str+= type->getName(); str+= type->getName(true);
str+="\n"; str+="\n";
str+= intToStr(amount); str+= intToStr(amount);
str+="/"; str+="/";

View File

@ -798,7 +798,7 @@ string Unit::getFullName() const{
if(type == NULL) { if(type == NULL) {
throw megaglest_runtime_error("type == NULL in Unit::getFullName()!"); throw megaglest_runtime_error("type == NULL in Unit::getFullName()!");
} }
str += type->getName(); str += type->getName(true);
return str; return str;
} }
@ -2501,7 +2501,7 @@ string Unit::getDescExtension() const{
if(i == 0){ if(i == 0){
str+= "\n" + lang.get("OrdersOnQueue") + ": "; str+= "\n" + lang.get("OrdersOnQueue") + ": ";
} }
str+= "\n#" + intToStr(i + 1) + " " + ct->getName(); str+= "\n#" + intToStr(i + 1) + " " + ct->getName(true);
++it; ++it;
} }
} }
@ -2572,7 +2572,7 @@ string Unit::getDesc() const {
//load //load
if(loadCount!=0){ if(loadCount!=0){
str+= "\n" + lang.get("Load")+ ": " + intToStr(loadCount) +" " + loadType->getName(); str+= "\n" + lang.get("Load")+ ": " + intToStr(loadCount) +" " + loadType->getName(true);
} }
//consumable production //consumable production
@ -2581,13 +2581,13 @@ string Unit::getDesc() const {
if(r->getType()->getClass() == rcConsumable) { if(r->getType()->getClass() == rcConsumable) {
str+= "\n"; str+= "\n";
str+= r->getAmount() < 0 ? lang.get("Produce")+": ": lang.get("Consume")+": "; str+= r->getAmount() < 0 ? lang.get("Produce")+": ": lang.get("Consume")+": ";
str+= intToStr(abs(r->getAmount())) + " " + r->getType()->getName(); str+= intToStr(abs(r->getAmount())) + " " + r->getType()->getName(true);
} }
} }
//command info //command info
if(commands.empty() == false) { if(commands.empty() == false) {
str+= "\n" + commands.front()->getCommandType()->getName(); str+= "\n" + commands.front()->getCommandType()->getName(true);
if(commands.size() > 1) { if(commands.size() > 1) {
str+= "\n" + lang.get("OrdersOnQueue") + ": " + intToStr(commands.size()); str+= "\n" + lang.get("OrdersOnQueue") + ": " + intToStr(commands.size());
} }
@ -2598,7 +2598,7 @@ string Unit::getDesc() const {
for(int i = 0; i < getType()->getStoredResourceCount(); ++i) { for(int i = 0; i < getType()->getStoredResourceCount(); ++i) {
const Resource *r= getType()->getStoredResource(i); const Resource *r= getType()->getStoredResource(i);
str+= "\n" + lang.get("Store") + ": "; str+= "\n" + lang.get("Store") + ": ";
str+= intToStr(r->getAmount()) + " " + r->getType()->getName(); str+= intToStr(r->getAmount()) + " " + r->getType()->getName(true);
} }
} }
} }

View File

@ -527,7 +527,7 @@ string HarvestCommandType::getDesc(const TotalUpgrade *totalUpgrade) const{
} }
str+=lang.get("Resources")+":\n"; str+=lang.get("Resources")+":\n";
for(int i=0; i<getHarvestedResourceCount(); ++i){ for(int i=0; i<getHarvestedResourceCount(); ++i){
str+= getHarvestedResource(i)->getName()+"\n"; str+= getHarvestedResource(i)->getName(true)+"\n";
} }
str+=harvestSkillType->getBoostDesc(); str+=harvestSkillType->getBoostDesc();
return str; return str;
@ -601,7 +601,7 @@ string RepairCommandType::getDesc(const TotalUpgrade *totalUpgrade) const{
} }
str+="\n"+lang.get("CanRepair")+":\n"; str+="\n"+lang.get("CanRepair")+":\n";
for(int i=0; i<repairableUnits.size(); ++i){ for(int i=0; i<repairableUnits.size(); ++i){
str+= (static_cast<const UnitType*>(repairableUnits[i]))->getName()+"\n"; str+= (static_cast<const UnitType*>(repairableUnits[i]))->getName(true)+"\n";
} }
str+=repairSkillType->getBoostDesc(); str+=repairSkillType->getBoostDesc();
return str; return str;

View File

@ -54,7 +54,7 @@ string RequirableType::getReqDesc() const{
if(getUnitReq(i) == NULL) { if(getUnitReq(i) == NULL) {
throw megaglest_runtime_error("getUnitReq(i) == NULL"); throw megaglest_runtime_error("getUnitReq(i) == NULL");
} }
reqString+= getUnitReq(i)->getName(); reqString+= getUnitReq(i)->getName(true);
reqString+= "\n"; reqString+= "\n";
anyReqs= true; anyReqs= true;
} }
@ -64,12 +64,12 @@ string RequirableType::getReqDesc() const{
throw megaglest_runtime_error("getUpgradeReq(i) == NULL"); throw megaglest_runtime_error("getUpgradeReq(i) == NULL");
} }
reqString+= getUpgradeReq(i)->getName(); reqString+= getUpgradeReq(i)->getName(true);
reqString+= "\n"; reqString+= "\n";
anyReqs= true; anyReqs= true;
} }
string str= getName(); string str= getName(true);
if(anyReqs){ if(anyReqs){
return str + " " + Lang::getInstance().get("Reqs") + ":\n" + reqString; return str + " " + Lang::getInstance().get("Reqs") + ":\n" + reqString;
} }
@ -125,11 +125,11 @@ string ProducibleType::getReqDesc() const {
return getReqDesc(false); return getReqDesc(false);
} }
string ProducibleType::getReqDesc(bool ignoreResourceRequirements) const { string ProducibleType::getReqDesc(bool ignoreResourceRequirements) const {
string str= getName()+" "+Lang::getInstance().get("Reqs")+":\n"; string str= getName(true) + " " + Lang::getInstance().get("Reqs") + ":\n";
if(ignoreResourceRequirements == false) { if(ignoreResourceRequirements == false) {
for(int i=0; i<getCostCount(); ++i){ for(int i=0; i<getCostCount(); ++i){
if(getCost(i)->getAmount()!=0){ if(getCost(i)->getAmount()!=0){
str+= getCost(i)->getType()->getName(); str+= getCost(i)->getType()->getName(true);
str+= ": "+ intToStr(getCost(i)->getAmount()); str+= ": "+ intToStr(getCost(i)->getAmount());
str+= "\n"; str+= "\n";
} }
@ -137,12 +137,12 @@ string ProducibleType::getReqDesc(bool ignoreResourceRequirements) const {
} }
for(int i=0; i<getUnitReqCount(); ++i){ for(int i=0; i<getUnitReqCount(); ++i){
str+= getUnitReq(i)->getName(); str+= getUnitReq(i)->getName(true);
str+= "\n"; str+= "\n";
} }
for(int i=0; i<getUpgradeReqCount(); ++i){ for(int i=0; i<getUpgradeReqCount(); ++i){
str+= getUpgradeReq(i)->getName(); str+= getUpgradeReq(i)->getName(true);
str+= "\n"; str+= "\n";
} }

View File

@ -53,8 +53,8 @@ public:
virtual ~DisplayableType(){}; virtual ~DisplayableType(){};
//get //get
string getName() const {return name;} virtual string getName(bool translatedValue=false) const { return name; }
const Texture2D *getImage() const {return image;} virtual const Texture2D *getImage() const { return image; }
//virtual void saveGame(XmlNode *rootNode) const; //virtual void saveGame(XmlNode *rootNode) const;
}; };

View File

@ -185,6 +185,13 @@ void ResourceType::load(const string &dir, Checksum* checksum, Checksum *techtre
} }
} }
string ResourceType::getName(bool translatedValue) const {
if(translatedValue == false) return name;
Lang &lang = Lang::getInstance();
return lang.getTechTreeString("ResourceTypeName_" + name,name.c_str());
}
// ==================== misc ==================== // ==================== misc ====================
ResourceClass ResourceType::strToRc(const string &s){ ResourceClass ResourceType::strToRc(const string &s){

View File

@ -63,6 +63,7 @@ public:
std::map<string,vector<pair<string, string> > > &loadedFileList, std::map<string,vector<pair<string, string> > > &loadedFileList,
string techtreePath); string techtreePath);
virtual string getName(bool translatedValue=false) const;
//get //get
int getClass() const {return resourceClass;} int getClass() const {return resourceClass;}
int getTilesetObject() const {return tilesetObject;} int getTilesetObject() const {return tilesetObject;}

View File

@ -191,7 +191,7 @@ string AttackBoost::getDesc() const{
if(boostUnitList.empty() == false) { if(boostUnitList.empty() == false) {
for(int i=0; i<boostUnitList.size(); ++i){ for(int i=0; i<boostUnitList.size(); ++i){
str+= " "+boostUnitList[i]->getName()+"\n"; str+= " "+boostUnitList[i]->getName(true)+"\n";
} }
} }
else else

View File

@ -225,6 +225,9 @@ void TechTree::load(const string &dir, set<string> &factions, Checksum* checksum
*techtreeChecksum = checksumValue; *techtreeChecksum = checksumValue;
} }
Lang &lang = Lang::getInstance();
lang.loadTechTreeStrings(name);
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
} }

View File

@ -487,7 +487,7 @@ string UpgradeType::getReqDesc() const{
if(getEffectCount()>0){ if(getEffectCount()>0){
str+= lang.get("AffectedUnits")+"\n"; str+= lang.get("AffectedUnits")+"\n";
for(int i=0; i<getEffectCount(); ++i){ for(int i=0; i<getEffectCount(); ++i){
str+= indent+getEffect(i)->getName()+"\n"; str+= indent+getEffect(i)->getName(true)+"\n";
} }
} }
return str; return str;

View File

@ -168,6 +168,9 @@ public:
// ===================================================== // =====================================================
void Tokenize(const string& str,vector<string>& tokens,const string& delimiters = " "); void Tokenize(const string& str,vector<string>& tokens,const string& delimiters = " ");
bool isdir(const char *path); bool isdir(const char *path);
bool fileExists(const string &path);
inline bool folderExists(const string &path) { return isdir(path.c_str()); }
void findDirs(string path, vector<string> &results, bool errorOnNotFound,bool keepDuplicates); void findDirs(string path, vector<string> &results, bool errorOnNotFound,bool keepDuplicates);
void findDirs(const vector<string> &paths, vector<string> &results, bool errorOnNotFound=false,bool keepDuplicates=false); void findDirs(const vector<string> &paths, vector<string> &results, bool errorOnNotFound=false,bool keepDuplicates=false);
void findAll(const vector<string> &paths, const string &fileFilter, vector<string> &results, bool cutExtension=false, bool errorOnNotFound=true,bool keepDuplicates=false); void findAll(const vector<string> &paths, const string &fileFilter, vector<string> &results, bool cutExtension=false, bool errorOnNotFound=true,bool keepDuplicates=false);

View File

@ -79,6 +79,8 @@ public:
void setFloat(const string &key, float value); void setFloat(const string &key, float value);
void setString(const string &key, const string &value); void setString(const string &key, const string &value);
bool hasString(const string &key) const;
static bool applyTagsToValue(string &value, std::map<string,string> *mapTagReplacementValues=NULL); static bool applyTagsToValue(string &value, std::map<string,string> *mapTagReplacementValues=NULL);
static std::map<string,string> getTagReplacementValues(std::map<string,string> *mapExtraTagReplacementValues=NULL); static std::map<string,string> getTagReplacementValues(std::map<string,string> *mapExtraTagReplacementValues=NULL);

View File

@ -203,7 +203,6 @@ float saturate(float value);
int round(float f); int round(float f);
//misc //misc
bool fileExists(const string &path);
bool checkVersionComptability(string clientVersionString, string serverVersionString); bool checkVersionComptability(string clientVersionString, string serverVersionString);
template<typename T> template<typename T>

View File

@ -421,6 +421,28 @@ bool isdir(const char *path)
return ret; return ret;
} }
bool fileExists(const string &path) {
#ifdef WIN32
wstring wstr = utf8_decode(path);
FILE* file= _wfopen(wstr.c_str(), L"rb");
#else
FILE* file= fopen(path.c_str(), "rb");
#endif
if(file != NULL) {
fclose(file);
return true;
}
else {
//int fileErrno = errno;
#ifdef WIN32
int fileErrno = errno;
DWORD error = GetLastError();
string strError = "[#6] Could not open file, result: " + intToStr(error) + " - " + intToStr(fileErrno) + " " + strerror(fileErrno) + " [" + path + "]";
#endif
}
return false;
}
void removeFolder(const string path) { void removeFolder(const string path) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str()); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str());

View File

@ -416,6 +416,15 @@ const string Properties::getString(const string &key, const char *defaultValueIf
} }
} }
bool Properties::hasString(const string &key) const {
PropertyMap::const_iterator it;
it= propertyMap.find(key);
if(it == propertyMap.end()) {
return false;
}
return true;
}
void Properties::setInt(const string &key, int value){ void Properties::setInt(const string &key, int value){
setString(key, intToStr(value)); setString(key, intToStr(value));
} }

View File

@ -736,28 +736,6 @@ int round(float f){
// ==================== misc ==================== // ==================== misc ====================
bool fileExists(const string &path) {
#ifdef WIN32
wstring wstr = utf8_decode(path);
FILE* file= _wfopen(wstr.c_str(), L"rb");
#else
FILE* file= fopen(path.c_str(), "rb");
#endif
if(file != NULL) {
fclose(file);
return true;
}
else {
//int fileErrno = errno;
#ifdef WIN32
int fileErrno = errno;
DWORD error = GetLastError();
string strError = "[#6] Could not open file, result: " + intToStr(error) + " - " + intToStr(fileErrno) + " " + strerror(fileErrno) + " [" + path + "]";
#endif
}
return false;
}
bool checkVersionComptability(string clientVersionString, string serverVersionString) { bool checkVersionComptability(string clientVersionString, string serverVersionString) {
bool compatible = (clientVersionString == serverVersionString); bool compatible = (clientVersionString == serverVersionString);
if(compatible == false) { if(compatible == false) {