dfhack/library/modules/Translation.cpp

284 lines
8.0 KiB
C++

2010-04-06 09:11:58 -06:00
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
2010-04-06 09:11:58 -06:00
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "Internal.h"
#include <string>
#include <vector>
#include <map>
#include <cassert>
using namespace std;
2011-12-31 04:48:42 -07:00
#include "modules/Translation.h"
#include "VersionInfo.h"
#include "MemAccess.h"
#include "Vector.h"
#include "Types.h"
#include "ModuleFactory.h"
2011-12-31 04:48:42 -07:00
#include "Core.h"
2010-04-06 09:11:58 -06:00
using namespace DFHack;
Module* DFHack::createTranslation()
{
return new Translation();
}
2010-04-06 09:11:58 -06:00
struct Translation::Private
{
uint32_t genericAddress;
uint32_t transAddress;
uint32_t word_table_offset;
uint32_t sizeof_string;
2010-04-06 09:11:58 -06:00
// translation
Dicts dicts;
2010-04-06 09:11:58 -06:00
bool Inited;
bool Started;
// names
uint32_t name_firstname_offset;
uint32_t name_nickname_offset;
uint32_t name_words_offset;
uint32_t name_parts_offset;
uint32_t name_language_offset;
uint32_t name_set_offset;
bool namesInited;
bool namesFailed;
2010-04-06 09:11:58 -06:00
};
Translation::Translation()
2010-04-06 09:11:58 -06:00
{
Core & c = Core::getInstance();
2010-04-06 09:11:58 -06:00
d = new Private;
d->Inited = d->Started = false;
OffsetGroup * OG_Translation = c.vinfo->getGroup("Translations");
OffsetGroup * OG_String = c.vinfo->getGroup("string");
d->genericAddress = OG_Translation->getAddress ("language_vector");
d->transAddress = OG_Translation->getAddress ("translation_vector");
d->word_table_offset = OG_Translation->getOffset ("word_table");
d->sizeof_string = OG_String->getHexValue ("sizeof");
2010-04-06 09:11:58 -06:00
d->Inited = true;
d->namesInited = false;
d->namesFailed = false;
2010-04-06 09:11:58 -06:00
}
Translation::~Translation()
{
if(d->Started)
Finish();
delete d;
}
bool Translation::Start()
{
Core & c = Core::getInstance();
2010-04-06 09:11:58 -06:00
if(!d->Inited)
return false;
Process * p = c.p;
2010-06-05 16:56:09 -06:00
Finish();
DfVector <uint32_t> genericVec (d->genericAddress);
DfVector <uint32_t> transVec (d->transAddress);
2010-04-06 17:17:03 -06:00
DFDict & translations = d->dicts.translations;
DFDict & foreign_languages = d->dicts.foreign_languages;
2010-04-06 09:11:58 -06:00
translations.resize(10);
for (uint32_t i = 0;i < genericVec.size();i++)
2010-04-06 09:11:58 -06:00
{
uint32_t genericNamePtr = genericVec.at(i);
2011-08-04 21:02:36 -06:00
for(int j=0; j<10;j++)
2010-04-06 09:11:58 -06:00
{
2011-07-13 20:05:27 -06:00
string word = p->readSTLString (genericNamePtr + j * d->sizeof_string);
translations[j].push_back (word);
2010-04-06 09:11:58 -06:00
}
}
foreign_languages.resize(transVec.size());
for (uint32_t i = 0; i < transVec.size();i++)
2010-04-06 09:11:58 -06:00
{
uint32_t transPtr = transVec.at(i);
DfVector <uint32_t> trans_names_vec (transPtr + d->word_table_offset);
for (uint32_t j = 0;j < trans_names_vec.size();j++)
2010-04-06 09:11:58 -06:00
{
uint32_t transNamePtr = trans_names_vec.at(j);
2010-04-06 09:11:58 -06:00
string name = p->readSTLString (transNamePtr);
foreign_languages[i].push_back (name);
}
}
d->Started = true;
return true;
}
bool Translation::Finish()
{
d->dicts.foreign_languages.clear();
d->dicts.translations.clear();
d->Started = false;
return true;
}
Dicts * Translation::getDicts()
{
assert(d->Started);
2010-04-06 09:11:58 -06:00
if(d->Started)
return &d->dicts;
return 0;
}
bool Translation::InitReadNames()
{
Core & c = Core::getInstance();
try
{
OffsetGroup * OG = c.vinfo->getGroup("name");
d->name_firstname_offset = OG->getOffset("first");
d->name_nickname_offset = OG->getOffset("nick");
d->name_words_offset = OG->getOffset("second_words");
d->name_parts_offset = OG->getOffset("parts_of_speech");
d->name_language_offset = OG->getOffset("language");
d->name_set_offset = OG->getOffset("has_name");
}
catch(exception &)
{
d->namesFailed = true;
return false;
}
d->namesInited = true;
return true;
}
2011-09-18 05:49:10 -06:00
bool Translation::readName(t_name & name, df_name * source)
{
Core & c = Core::getInstance();
Process * p = c.p;
if(d->namesFailed)
{
return false;
}
if(!d->namesInited)
{
if(!InitReadNames()) return false;
}
2011-09-18 05:49:10 -06:00
strncpy(name.first_name,source->first_name.c_str(),127);
strncpy(name.nickname,source->nick_name.c_str(),127);
memcpy(&name.parts_of_speech, &source->parts_of_speech, sizeof (source->parts_of_speech));
memcpy(&name.words, &source->words, sizeof (source->words));
name.language = source->language;
name.has_name = source->has_name;
return true;
}
2011-09-18 05:49:10 -06:00
bool Translation::copyName(df_name * source, df_name * target)
{
uint8_t buf[28];
2011-09-18 05:49:10 -06:00
if (source == target)
return true;
Core & c = Core::getInstance();
Process * p = c.p;
2011-09-18 05:49:10 -06:00
target->first_name = source->first_name;
target->nick_name = source->nick_name;
target->has_name = source->has_name;
target->language = source->language;
memcpy(&target->parts_of_speech, &source->parts_of_speech, sizeof (source->parts_of_speech));
memcpy(&target->words, &source->words, sizeof (source->words));
target->unknown = source->unknown;
return true;
}
2011-09-18 05:49:10 -06:00
string Translation::TranslateName(const df_name * name, bool inEnglish)
2010-04-06 09:11:58 -06:00
{
string out;
assert (d->Started);
2010-04-06 09:11:58 -06:00
map<string, vector<string> >::const_iterator it;
if(!inEnglish)
2010-04-06 09:11:58 -06:00
{
2011-09-18 05:49:10 -06:00
if(name->words[0] >=0 || name->words[1] >=0)
2010-04-06 09:11:58 -06:00
{
2011-09-18 05:49:10 -06:00
if(name->words[0]>=0) out.append(d->dicts.foreign_languages[name->language][name->words[0]]);
if(name->words[1]>=0) out.append(d->dicts.foreign_languages[name->language][name->words[1]]);
2010-04-06 09:11:58 -06:00
out[0] = toupper(out[0]);
}
2011-09-18 05:49:10 -06:00
if(name->words[5] >=0)
2010-04-06 09:11:58 -06:00
{
string word;
for(int i=2;i<=5;i++)
2011-09-18 05:49:10 -06:00
if(name->words[i]>=0) word.append(d->dicts.foreign_languages[name->language][name->words[i]]);
2010-04-06 09:11:58 -06:00
word[0] = toupper(word[0]);
if(out.length() > 0) out.append(" ");
out.append(word);
}
2011-09-18 05:49:10 -06:00
if(name->words[6] >=0)
2010-04-06 09:11:58 -06:00
{
string word;
2011-09-18 05:49:10 -06:00
word.append(d->dicts.foreign_languages[name->language][name->words[6]]);
2010-04-06 09:11:58 -06:00
word[0] = toupper(word[0]);
if(out.length() > 0) out.append(" ");
out.append(word);
}
}
2010-04-06 09:11:58 -06:00
else
{
2011-09-18 05:49:10 -06:00
if(name->words[0] >=0 || name->words[1] >=0)
2010-04-06 09:11:58 -06:00
{
2011-09-18 05:49:10 -06:00
if(name->words[0]>=0) out.append(d->dicts.translations[name->parts_of_speech[0]+1][name->words[0]]);
if(name->words[1]>=0) out.append(d->dicts.translations[name->parts_of_speech[1]+1][name->words[1]]);
2010-04-06 09:11:58 -06:00
out[0] = toupper(out[0]);
}
2011-09-18 05:49:10 -06:00
if(name->words[5] >=0)
2010-04-06 09:11:58 -06:00
{
if(out.length() > 0)
out.append(" the");
else
out.append("The");
string word;
for(int i=2;i<=5;i++)
{
2011-09-18 05:49:10 -06:00
if(name->words[i]>=0)
2010-04-06 09:11:58 -06:00
{
2011-09-18 05:49:10 -06:00
word = d->dicts.translations[name->parts_of_speech[i]+1][name->words[i]];
2010-04-06 09:11:58 -06:00
word[0] = toupper(word[0]);
out.append(" " + word);
}
}
}
2011-09-18 05:49:10 -06:00
if(name->words[6] >=0)
2010-04-06 09:11:58 -06:00
{
if(out.length() > 0)
out.append(" of");
else
out.append("Of");
string word;
2011-09-18 05:49:10 -06:00
word.append(d->dicts.translations[name->parts_of_speech[6]+1][name->words[6]]);
2010-04-06 09:11:58 -06:00
word[0] = toupper(word[0]);
out.append(" " + word);
}
}
return out;
2010-05-01 18:38:18 -06:00
}