Name translation module - untested

develop
Petr Mrázek 2010-04-06 17:11:58 +02:00
parent d5aa1488a6
commit 7b1edd5122
6 changed files with 252 additions and 135 deletions

@ -37,6 +37,7 @@ modules/Gui.cpp
modules/Maps.cpp
modules/Materials.cpp
modules/Position.cpp
modules/Translation.cpp
)
SET(PROJECT_HDRS_LINUX

@ -588,129 +588,6 @@ bool API::getItemIndexesInBox(vector<uint32_t> &indexes,
}
*/
/*
bool API::InitReadNameTables(vector<vector<string> > & translations , vector<vector<string> > & foreign_languages) //(map< string, vector<string> > & nameTable)
{
try
{
int genericAddress = d->offset_descriptor->getAddress ("language_vector");
int transAddress = d->offset_descriptor->getAddress ("translation_vector");
int word_table_offset = d->offset_descriptor->getOffset ("word_table");
int sizeof_string = d->offset_descriptor->getHexValue ("sizeof_string");
DfVector genericVec (d->p, genericAddress, 4);
DfVector transVec (d->p, transAddress, 4);
translations.resize(10);
for (uint32_t i = 0;i < genericVec.getSize();i++)
{
uint32_t genericNamePtr = * (uint32_t *) genericVec.at (i);
for(int i=0; i<10;i++)
{
string word = d->p->readSTLString (genericNamePtr + i * sizeof_string);
translations[i].push_back (word);
}
}
foreign_languages.resize(transVec.getSize());
for (uint32_t i = 0; i < transVec.getSize();i++)
{
uint32_t transPtr = * (uint32_t *) transVec.at (i);
//string transName = d->p->readSTLString (transPtr);
DfVector trans_names_vec (d->p, transPtr + word_table_offset, 4);
for (uint32_t j = 0;j < trans_names_vec.getSize();j++)
{
uint32_t transNamePtr = * (uint32_t *) trans_names_vec.at (j);
string name = d->p->readSTLString (transNamePtr);
foreign_languages[i].push_back (name);
}
}
d->nameTablesInited = true;
return true;
}
catch (Error::MissingMemoryDefinition&)
{
d->nameTablesInited = false;
throw;
}
}
string API::TranslateName(const DFHack::t_name &name,const std::vector< std::vector<std::string> > & translations ,const std::vector< std::vector<std::string> > & foreign_languages, bool inEnglish)
{
string out;
assert (d->nameTablesInited);
map<string, vector<string> >::const_iterator it;
if(!inEnglish)
{
if(name.words[0] >=0 || name.words[1] >=0)
{
if(name.words[0]>=0) out.append(foreign_languages[name.language][name.words[0]]);
if(name.words[1]>=0) out.append(foreign_languages[name.language][name.words[1]]);
out[0] = toupper(out[0]);
}
if(name.words[5] >=0)
{
string word;
for(int i=2;i<=5;i++)
if(name.words[i]>=0) word.append(foreign_languages[name.language][name.words[i]]);
word[0] = toupper(word[0]);
if(out.length() > 0) out.append(" ");
out.append(word);
}
if(name.words[6] >=0)
{
string word;
word.append(foreign_languages[name.language][name.words[6]]);
word[0] = toupper(word[0]);
if(out.length() > 0) out.append(" ");
out.append(word);
}
}
else
{
if(name.words[0] >=0 || name.words[1] >=0)
{
if(name.words[0]>=0) out.append(translations[name.parts_of_speech[0]+1][name.words[0]]);
if(name.words[1]>=0) out.append(translations[name.parts_of_speech[1]+1][name.words[1]]);
out[0] = toupper(out[0]);
}
if(name.words[5] >=0)
{
if(out.length() > 0)
out.append(" the");
else
out.append("The");
string word;
for(int i=2;i<=5;i++)
{
if(name.words[i]>=0)
{
word = translations[name.parts_of_speech[i]+1][name.words[i]];
word[0] = toupper(word[0]);
out.append(" " + word);
}
}
}
if(name.words[6] >=0)
{
if(out.length() > 0)
out.append(" of");
else
out.append("Of");
string word;
word.append(translations[name.parts_of_speech[6]+1][name.words[6]]);
word[0] = toupper(word[0]);
out.append(" " + word);
}
}
return out;
}
void API::FinishReadNameTables()
{
d->nameTablesInited = false;
}
void API::FinishReadNotes()
{
if(d->p_notes)

@ -154,14 +154,6 @@ namespace DFHack
bool InitReadHotkeys( );
bool ReadHotkeys(t_hotkey hotkeys[]);
*/
/*
* DF translation tables and name translation
*/
/*
bool InitReadNameTables (std::vector< std::vector<std::string> > & translations , std::vector< std::vector<std::string> > & foreign_languages);
void FinishReadNameTables();
std::string TranslateName(const t_name & name,const std::vector< std::vector<std::string> > & translations ,const std::vector< std::vector<std::string> > & foreign_languages, bool inEnglish=true);
*/
/*
* Item reading

@ -0,0 +1,37 @@
#ifndef CL_MOD_POSITION
#define CL_MOD_POSITION
/*
* Translation of names
*/
#include "Export.h"
namespace DFHack
{
struct APIPrivate;
typedef std::vector< std::vector<std::string> > DFDict;
typedef struct
{
DFDict translations;
DFDict foreign_languages;
} Dicts;
class DFHACK_EXPORT Translation
{
public:
Translation(APIPrivate * d);
~Translation();
bool Start();
bool Finish();
/*
* DF translation tables and name translation
*/
// Get pointer to the two dictionary structures
Dicts * getDicts();
// translate a name using the loaded dictionaries
std::string TranslateName(const DFHack::t_name& name, bool inEnglish = true);
private:
struct Private;
Private *d;
};
}
#endif

@ -0,0 +1,200 @@
/*
www.sourceforge.net/projects/dfhack
Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf
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 "DFCommonInternal.h"
#include "../private/APIPrivate.h"
#include "modules/Translation.h"
#include "DFMemInfo.h"
#include "DFProcess.h"
#include "DFVector.h"
#include "DFTypes.h"
#include "modules/Translation.h"
using namespace DFHack;
struct Translation::Private
{
uint32_t genericAddress;
uint32_t transAddress;
uint32_t word_table_offset;
uint32_t sizeof_string;
// translation
Dicts dicts;
APIPrivate *d;
bool Inited;
bool Started;
//uint32_t biome_stuffs;
//vector<uint16_t> v_geology[eBiomeCount];
};
Translation::Translation(APIPrivate * d_)
{
d = new Private;
d->d = d_;
d->Inited = d->Started = false;
memory_info * mem = d->d->offset_descriptor;
int genericAddress = mem->getAddress ("language_vector");
int transAddress = mem->getAddress ("translation_vector");
int word_table_offset = mem->getOffset ("word_table");
int sizeof_string = mem->getHexValue ("sizeof_string");
d->Inited = true;
}
Translation::~Translation()
{
if(d->Started)
Finish();
delete d;
}
bool Translation::Start()
{
if(!d->Inited)
return false;
Process * p = d->d->p;
DfVector genericVec (p, d->genericAddress, 4);
DfVector transVec (p, d->transAddress, 4);
DFDict translations = d->dicts.translations;
DFDict foreign_languages = d->dicts.foreign_languages;
translations.resize(10);
for (uint32_t i = 0;i < genericVec.getSize();i++)
{
uint32_t genericNamePtr = * (uint32_t *) genericVec.at (i);
for(int i=0; i<10;i++)
{
string word = p->readSTLString (genericNamePtr + i * d->sizeof_string);
translations[i].push_back (word);
}
}
foreign_languages.resize(transVec.getSize());
for (uint32_t i = 0; i < transVec.getSize();i++)
{
uint32_t transPtr = * (uint32_t *) transVec.at (i);
//string transName = d->p->readSTLString (transPtr);
DfVector trans_names_vec (p, transPtr + d->word_table_offset, 4);
for (uint32_t j = 0;j < trans_names_vec.getSize();j++)
{
uint32_t transNamePtr = * (uint32_t *) trans_names_vec.at (j);
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);
if(d->Started)
return &d->dicts;
return 0;
}
string Translation::TranslateName(const t_name &name, bool inEnglish)
{
string out;
assert (d->Started);
map<string, vector<string> >::const_iterator it;
if(!inEnglish)
{
if(name.words[0] >=0 || name.words[1] >=0)
{
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]]);
out[0] = toupper(out[0]);
}
if(name.words[5] >=0)
{
string word;
for(int i=2;i<=5;i++)
if(name.words[i]>=0) word.append(d->dicts.foreign_languages[name.language][name.words[i]]);
word[0] = toupper(word[0]);
if(out.length() > 0) out.append(" ");
out.append(word);
}
if(name.words[6] >=0)
{
string word;
word.append(d->dicts.foreign_languages[name.language][name.words[6]]);
word[0] = toupper(word[0]);
if(out.length() > 0) out.append(" ");
out.append(word);
}
}
else
{
if(name.words[0] >=0 || name.words[1] >=0)
{
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]]);
out[0] = toupper(out[0]);
}
if(name.words[5] >=0)
{
if(out.length() > 0)
out.append(" the");
else
out.append("The");
string word;
for(int i=2;i<=5;i++)
{
if(name.words[i]>=0)
{
word = d->dicts.translations[name.parts_of_speech[i]+1][name.words[i]];
word[0] = toupper(word[0]);
out.append(" " + word);
}
}
}
if(name.words[6] >=0)
{
if(out.length() > 0)
out.append(" of");
else
out.append("Of");
string word;
word.append(d->dicts.translations[name.parts_of_speech[6]+1][name.words[6]]);
word[0] = toupper(word[0]);
out.append(" " + word);
}
}
return out;
}

@ -12,6 +12,9 @@ using namespace std;
#include <DFHackAPI.h>
#include <DFMemInfo.h>
#include <DFProcess.h>
#include <modules/Materials.h>
#include <modules/Creatures.h>
#include <modules/NameTables.h>
template <typename T>
void print_bits ( T val, std::ostream& out )
@ -33,10 +36,11 @@ vector<DFHack::t_matgloss> creaturestypes;
void printDwarves(DFHack::API & DF)
{
int dwarfCounter = 0;
DFHack::Creatures * c = DF.getCreatures();
for (uint32_t i = 0; i < numCreatures; i++)
{
DFHack::t_creature temp;
DF.ReadCreature(i, temp);
c->ReadCreature(i, temp);
string type = creaturestypes[temp.type].id;
if (type == "DWARF" && !temp.flags1.bits.dead && !temp.flags2.bits.killed)
{
@ -148,6 +152,7 @@ bool getDwarfSelection(DFHack::API & DF, DFHack::t_creature & toChange,string &
changeString = lastText;
return true;
}
bool waitTillChanged(DFHack::API &DF, int creatureToCheck, string changeValue, bool isName)
{
DFHack::DFWindow * w = DF.getWindow();
@ -313,9 +318,13 @@ bool setCursorToCreature(DFHack::API &DF)
int main (void)
{
DFHack::API DF("Memory.xml");
DFHack::Creatures *c;
DFHack::Materials *m;
try
{
DF.Attach();
c = DF.getCreatures();
}
catch (exception& e)
{
@ -328,15 +337,16 @@ int main (void)
DFHack::memory_info * mem = DF.getMemoryInfo();
if (!DF.ReadCreatureMatgloss(creaturestypes))
if (!m->ReadCreatureTypes(creaturestypes))
{
cerr << "Can't get the creature types." << endl;
return 1;
}
DF.InitReadNameTables(englishWords,foreignWords);
DF.InitReadCreatures(numCreatures);
DF.InitViewAndCursor();
c->Start(numCreatures);
// DF.InitViewAndCursor();
DFHack::Process * p = DF.getProcess();
DFHack::DFWindow * w = DF.getWindow();