Goodbye, Translation module

develop
Quietust 2012-01-24 10:54:12 -06:00
parent 1ca90de647
commit 466bf89578
8 changed files with 65 additions and 237 deletions

@ -1093,7 +1093,6 @@ MODULE_GETTER(Engravings);
MODULE_GETTER(Gui); MODULE_GETTER(Gui);
MODULE_GETTER(World); MODULE_GETTER(World);
MODULE_GETTER(Materials); MODULE_GETTER(Materials);
MODULE_GETTER(Translation);
MODULE_GETTER(Vegetation); MODULE_GETTER(Vegetation);
MODULE_GETTER(Constructions); MODULE_GETTER(Constructions);
MODULE_GETTER(Notes); MODULE_GETTER(Notes);

@ -57,7 +57,6 @@ namespace DFHack
class Gui; class Gui;
class World; class World;
class Materials; class Materials;
class Translation;
class Vegetation; class Vegetation;
class Constructions; class Constructions;
class Notes; class Notes;
@ -107,8 +106,6 @@ namespace DFHack
World * getWorld(); World * getWorld();
/// get the materials module /// get the materials module
Materials * getMaterials(); Materials * getMaterials();
/// get the translation module
Translation * getTranslation();
/// get the vegetation module /// get the vegetation module
Vegetation * getVegetation(); Vegetation * getVegetation();
/// get the constructions module /// get the constructions module
@ -166,7 +163,6 @@ namespace DFHack
Gui * pGui; Gui * pGui;
World * pWorld; World * pWorld;
Materials * pMaterials; Materials * pMaterials;
Translation * pTranslation;
Vegetation * pVegetation; Vegetation * pVegetation;
Constructions * pConstructions; Constructions * pConstructions;
Notes * pNotes; Notes * pNotes;

@ -35,7 +35,6 @@ namespace DFHack
Module* createGui(); Module* createGui();
Module* createWorld(); Module* createWorld();
Module* createMaterials(); Module* createMaterials();
Module* createTranslation();
Module* createVegetation(); Module* createVegetation();
Module* createBuildings(); Module* createBuildings();
Module* createConstructions(); Module* createConstructions();

@ -33,51 +33,28 @@ distribution.
#include "Export.h" #include "Export.h"
#include "Module.h" #include "Module.h"
#include "Types.h" #include "Types.h"
#include "DataDefs.h" #include "DataDefs.h"
#include "df/language_word.h"
#include "df/language_translation.h"
#include "df/language_name.h" #include "df/language_name.h"
namespace DFHack namespace DFHack
{ {
namespace Simple
{
namespace Translation
{
// simple check to make sure if there's actual language data present
DFHACK_EXPORT bool IsValid ();
class DFContextShared; // names, used by a few other modules.
/** DFHACK_EXPORT bool readName(t_name & name, df::language_name * address);
* \ingroup grp_translation DFHACK_EXPORT bool copyName(df::language_name * address, df::language_name * target);
*/
typedef std::vector< std::vector<std::string> > DFDict;
/**
* \ingroup grp_translation
*/
typedef struct
{
DFDict translations;
DFDict foreign_languages;
} Dicts;
/**
* The Tanslation module
* \ingroup grp_translation
* \ingroup grp_maps
*/
class DFHACK_EXPORT Translation : public Module
{
public:
Translation();
~Translation();
bool Start();
bool Finish();
// Get pointer to the two dictionary structures
Dicts * getDicts();
// names, used by a few other modules.
bool InitReadNames();
bool readName(t_name & name, df::language_name * address);
bool copyName(df::language_name * address, df::language_name * target);
// translate a name using the loaded dictionaries
std::string TranslateName(const df::language_name * name, bool inEnglish = true);
private: // translate a name using the loaded dictionaries
struct Private; DFHACK_EXPORT std::string TranslateName (const df::language_name * name, bool inEnglish = true);
Private *d; }
}; }
} }
#endif #endif

@ -38,148 +38,20 @@ using namespace std;
#include "Core.h" #include "Core.h"
using namespace DFHack; using namespace DFHack;
using namespace DFHack::Simple;
Module* DFHack::createTranslation() #include "DataDefs.h"
{ #include "df/world.h"
return new Translation();
}
struct Translation::Private
{
void * genericAddress;
void * transAddress;
uint32_t word_table_offset;
uint32_t sizeof_string;
// translation using df::global::world;
Dicts dicts;
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;
};
Translation::Translation()
{
Core & c = Core::getInstance();
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");
d->Inited = true;
d->namesInited = false;
d->namesFailed = false;
}
Translation::~Translation() bool Translation::IsValid ()
{ {
if(d->Started) return (world->raws.language.words.size() > 0) && (world->raws.language.translations.size() > 0);
Finish();
delete d;
}
bool Translation::Start()
{
Core & c = Core::getInstance();
if(!d->Inited)
return false;
Process * p = c.p;
Finish();
vector <char *> & genericVec = *(vector <char *> *) d->genericAddress;
vector <char *> & transVec = *(vector <char *> *) d->transAddress;
DFDict & translations = d->dicts.translations;
DFDict & foreign_languages = d->dicts.foreign_languages;
translations.resize(10);
for (uint32_t i = 0;i < genericVec.size();i++)
{
char * genericNamePtr = genericVec[i];
for(int j=0; j<10;j++)
{
string word = p->readSTLString (genericNamePtr + j * d->sizeof_string);
translations[j].push_back (word);
}
}
foreign_languages.resize(transVec.size());
for (uint32_t i = 0; i < transVec.size();i++)
{
char * transPtr = transVec.at(i);
vector <void *> & trans_names_vec = *(vector <void *> *) (transPtr + d->word_table_offset);
for (uint32_t j = 0;j < trans_names_vec.size();j++)
{
void * transNamePtr = trans_names_vec[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;
}
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;
} }
bool Translation::readName(t_name & name, df::language_name * source) bool Translation::readName(t_name & name, df::language_name * source)
{ {
Core & c = Core::getInstance();
Process * p = c.p;
if(d->namesFailed)
{
return false;
}
if(!d->namesInited)
{
if(!InitReadNames()) return false;
}
strncpy(name.first_name,source->first_name.c_str(),127); strncpy(name.first_name,source->first_name.c_str(),127);
strncpy(name.nickname,source->nickname.c_str(),127); strncpy(name.nickname,source->nickname.c_str(),127);
memcpy(&name.parts_of_speech, &source->parts_of_speech, sizeof (source->parts_of_speech)); memcpy(&name.parts_of_speech, &source->parts_of_speech, sizeof (source->parts_of_speech));
@ -193,16 +65,17 @@ bool Translation::copyName(df::language_name * source, df::language_name * targe
{ {
if (source == target) if (source == target)
return true; return true;
Core & c = Core::getInstance();
Process * p = c.p;
target->first_name = source->first_name; target->first_name = source->first_name;
target->nickname = source->nickname; target->nickname = source->nickname;
target->has_name = source->has_name; for (int i = 0; i < 7; i++)
{
target->words[i] = source->words[i];
target->parts_of_speech[i] = source->parts_of_speech[i];
}
target->language = source->language; 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; target->unknown = source->unknown;
target->has_name = source->has_name;
return true; return true;
} }
@ -211,71 +84,74 @@ string Translation::TranslateName(const df::language_name * name, bool inEnglish
string out; string out;
assert (d->Started); assert (d->Started);
map<string, vector<string> >::const_iterator it;
if(!inEnglish) if(!inEnglish)
{ {
if(name->words[0] >=0 || name->words[1] >=0) 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[0] >= 0)
if(name->words[1]>=0) out.append(d->dicts.foreign_languages[name->language][name->words[1]]); out.append(*world->raws.language.translations[name->language]->words[name->words[0]]);
if (name->words[1] >= 0)
out.append(*world->raws.language.translations[name->language]->words[name->words[1]]);
out[0] = toupper(out[0]); out[0] = toupper(out[0]);
} }
if(name->words[5] >=0) if (name->words[5] >= 0)
{ {
string word; string word;
for(int i=2;i<=5;i++) for (int i = 2; i <= 5; i++)
if(name->words[i]>=0) word.append(d->dicts.foreign_languages[name->language][name->words[i]]); if (name->words[i] >= 0)
word.append(*world->raws.language.translations[name->language]->words[name->words[i]]);
word[0] = toupper(word[0]); word[0] = toupper(word[0]);
if(out.length() > 0) out.append(" "); if (out.length() > 0)
out.append(" ");
out.append(word); out.append(word);
} }
if(name->words[6] >=0) if (name->words[6] >= 0)
{ {
string word; string word = *world->raws.language.translations[name->language]->words[name->words[6]];
word.append(d->dicts.foreign_languages[name->language][name->words[6]]);
word[0] = toupper(word[0]); word[0] = toupper(word[0]);
if(out.length() > 0) out.append(" "); if (out.length() > 0)
out.append(" ");
out.append(word); out.append(word);
} }
} }
else else
{ {
if(name->words[0] >=0 || name->words[1] >=0) if (name->words[0] >= 0 || name->words[1] >= 0)
{ {
if(name->words[0]>=0) out.append(d->dicts.translations[name->parts_of_speech[0].value+1][name->words[0]]); if (name->words[0] >= 0)
if(name->words[1]>=0) out.append(d->dicts.translations[name->parts_of_speech[1].value+1][name->words[1]]); out.append(world->raws.language.words[name->words[0]]->forms[name->parts_of_speech[0].value]);
if (name->words[1] >= 0)
out.append(world->raws.language.words[name->words[1]]->forms[name->parts_of_speech[1].value]);
out[0] = toupper(out[0]); out[0] = toupper(out[0]);
} }
if(name->words[5] >=0) if (name->words[5] >= 0)
{ {
if(out.length() > 0) if (out.length() > 0)
out.append(" the"); out.append(" the");
else else
out.append("The"); out.append("The");
string word; for (int i = 2; i <= 5; i++)
for(int i=2;i<=5;i++)
{ {
if(name->words[i]>=0) if (name->words[i] >= 0)
{ {
word = d->dicts.translations[name->parts_of_speech[i].value+1][name->words[i]]; string word = world->raws.language.words[name->words[i]]->forms[name->parts_of_speech[i].value];
word[0] = toupper(word[0]); word[0] = toupper(word[0]);
out.append(" " + word); out.append(" " + word);
} }
} }
} }
if(name->words[6] >=0) if (name->words[6] >= 0)
{ {
if(out.length() > 0) if (out.length() > 0)
out.append(" of"); out.append(" of");
else else
out.append("Of"); out.append("Of");
string word;
word.append(d->dicts.translations[name->parts_of_speech[6].value+1][name->words[6]]); string word = world->raws.language.words[name->words[6]]->forms[name->parts_of_speech[6].value];
word[0] = toupper(word[0]); word[0] = toupper(word[0]);
out.append(" " + word); out.append(" " + word);
} }
} }
return out; return out;
} }

@ -51,6 +51,7 @@ using namespace std;
#include "df/unit_inventory_item.h" #include "df/unit_inventory_item.h"
using namespace DFHack; using namespace DFHack;
using namespace DFHack::Simple;
using df::global::world; using df::global::world;
using df::global::ui; using df::global::ui;
@ -58,12 +59,6 @@ struct Units::Private
{ {
bool Inited; bool Inited;
bool Started; bool Started;
bool IdMapReady;
std::map<int32_t, int32_t> IdMap;
Process *owner;
Translation * trans;
}; };
Module* DFHack::createUnits() Module* DFHack::createUnits()
@ -75,16 +70,9 @@ Units::Units()
{ {
Core & c = Core::getInstance(); Core & c = Core::getInstance();
d = new Private; d = new Private;
d->owner = c.p;
VersionInfo * minfo = c.vinfo; VersionInfo * minfo = c.vinfo;
d->Inited = false; d->Inited = false;
d->Started = false; d->Started = false;
d->IdMapReady = false;
d->trans = c.getTranslation();
d->trans->InitReadNames(); // FIXME: throws on error
OffsetGroup *OG_Creatures = minfo->getGroup("Creatures");
d->Inited = true; d->Inited = true;
} }
@ -98,8 +86,6 @@ bool Units::Start( uint32_t &numcreatures )
{ {
d->Started = true; d->Started = true;
numcreatures = world->units.all.size(); numcreatures = world->units.all.size();
d->IdMap.clear();
d->IdMapReady = false;
return true; return true;
} }
@ -157,7 +143,7 @@ void Units::CopyCreature(df::unit * source, t_unit & furball)
//read creature from memory //read creature from memory
// name // name
d->trans->readName(furball.name, &source->name); Translation::readName(furball.name, &source->name);
// basic stuff // basic stuff
furball.id = source->id; furball.id = source->id;
@ -183,7 +169,7 @@ void Units::CopyCreature(df::unit * source, t_unit & furball)
// mood stuff // mood stuff
furball.mood = source->mood; furball.mood = source->mood;
furball.mood_skill = source->job.unk_2f8; // FIXME: really? More like currently used skill anyway. furball.mood_skill = source->job.unk_2f8; // FIXME: really? More like currently used skill anyway.
d->trans->readName(furball.artifact_name, &source->status.artifact_name); Translation::readName(furball.artifact_name, &source->status.artifact_name);
// labors // labors
memcpy(&furball.labors, &source->status.labors, sizeof(furball.labors)); memcpy(&furball.labors, &source->status.labors, sizeof(furball.labors));
@ -555,7 +541,6 @@ bool Units::RemoveOwnedItemByIdx(const uint32_t index, int32_t id)
bool Units::RemoveOwnedItemByPtr(df::unit * temp, int32_t id) bool Units::RemoveOwnedItemByPtr(df::unit * temp, int32_t id)
{ {
if(!d->Started) return false; if(!d->Started) return false;
Process * p = d->owner;
vector <int32_t> & vec = temp->owned_items; vector <int32_t> & vec = temp->owned_items;
vec.erase(std::remove(vec.begin(), vec.end(), id), vec.end()); vec.erase(std::remove(vec.begin(), vec.end(), id), vec.end());
/* /*
@ -573,6 +558,6 @@ bool Units::RemoveOwnedItemByPtr(df::unit * temp, int32_t id)
void Units::CopyNameTo(df::unit * creature, df::language_name * target) void Units::CopyNameTo(df::unit * creature, df::language_name * target)
{ {
d->trans->copyName(&creature->name, target); Translation::copyName(&creature->name, target);
} }

@ -98,13 +98,11 @@ DFhackCExport command_result df_cleanowned (Core * c, vector <string> & paramete
DFHack::Materials *Materials = c->getMaterials(); DFHack::Materials *Materials = c->getMaterials();
DFHack::Units *Creatures = c->getUnits(); DFHack::Units *Creatures = c->getUnits();
DFHack::Translation *Tran = c->getTranslation();
uint32_t num_creatures; uint32_t num_creatures;
bool ok = true; bool ok = true;
ok &= Materials->ReadAllMaterials(); ok &= Materials->ReadAllMaterials();
ok &= Creatures->Start(num_creatures); ok &= Creatures->Start(num_creatures);
ok &= Tran->Start();
c->con.print("Found total %d items.\n", world->items.all.size()); c->con.print("Found total %d items.\n", world->items.all.size());
@ -197,7 +195,7 @@ DFhackCExport command_result df_cleanowned (Core * c, vector <string> & paramete
if (!temp->name.nickname.empty()) if (!temp->name.nickname.empty())
info += std::string(" '") + temp->name.nickname + "'"; info += std::string(" '") + temp->name.nickname + "'";
info += " "; info += " ";
info += Tran->TranslateName(&temp->name,false); info += Translation::TranslateName(&temp->name,false);
c->con.print(", owner %s", info.c_str()); c->con.print(", owner %s", info.c_str());
} }

@ -22,6 +22,7 @@
using std::string; using std::string;
using std::vector; using std::vector;
using namespace DFHack; using namespace DFHack;
using namespace DFHack::Simple;
using namespace df::enums; using namespace df::enums;
using df::global::world; using df::global::world;
@ -31,8 +32,6 @@ DFhackCExport command_result df_showmood (Core * c, vector <string> & parameters
if (!parameters.empty()) if (!parameters.empty())
return CR_WRONG_USAGE; return CR_WRONG_USAGE;
Translation *trans = c->getTranslation();
trans->Start();
CoreSuspender suspend(c); CoreSuspender suspend(c);
bool found = false; bool found = false;
@ -62,7 +61,7 @@ DFhackCExport command_result df_showmood (Core * c, vector <string> & parameters
c->con.printerr("Dwarf with strange mood does not have a mood type!\n"); c->con.printerr("Dwarf with strange mood does not have a mood type!\n");
continue; continue;
} }
c->con.print("%s %s is currently ", unit->name.first_name.c_str(), trans->TranslateName(&unit->name, false).c_str()); c->con.print("%s %s is currently ", unit->name.first_name.c_str(), Translation::TranslateName(&unit->name, false).c_str());
switch (unit->mood) switch (unit->mood)
{ {
case mood_type::Macabre: case mood_type::Macabre:
@ -255,7 +254,6 @@ DFhackCExport command_result df_showmood (Core * c, vector <string> & parameters
c->con.print(", quantity %i\n", item->quantity); c->con.print(", quantity %i\n", item->quantity);
} }
} }
trans->Finish();
if (!found) if (!found)
c->con.print("No strange moods currently active.\n"); c->con.print("No strange moods currently active.\n");