develop
Petr Mrázek 2010-04-28 13:29:36 +02:00
commit 87a2741a09
3 changed files with 45 additions and 52 deletions

@ -90,9 +90,19 @@ static PyObject* BuildMatglossPair(DFHack::t_matglossPair& matgloss)
static DFHack::t_matglossPair ReverseBuildMatglossPair(PyObject* mObj)
{
DFHack::t_matglossPair mPair;
PyObject* temp;
temp = PyTuple_GetItem(mObj, 0);
mPair.type = (int16_t)PyInt_AsLong(temp);
Py_DECREF(temp);
mPair.type = (int16_t)PyInt_AsLong(PyTuple_GetItem(mObj, 0));
mPair.index = (int32_t)PyInt_AsLong(PyTuple_GetItem(mObj, 1));
temp = PyTuple_GetItem(mObj, 1);
mPair.index = (int32_t)PyInt_AsLong(temp);
Py_DECREF(temp);
return mPair;
}
@ -172,46 +182,28 @@ static PyObject* BuildNote(DFHack::t_note& note)
return noteObj;
}
static int NAME_WORD_COUNT = 7;
static PyObject* BuildName(DFHack::t_name& name)
{
PyObject* nameObj;
PyObject *wordList, *speechList;
PyObject* temp;
int wordCount = 7;
nameObj = PyObject_CallObject(Name_type, NULL);
if(name.first_name[0])
temp = PyString_FromString(name.first_name);
else
temp = PyString_FromString("");
OBJSET(nameObj, "first_name", temp);
if(name.nickname[0])
temp = PyString_FromString(name.nickname);
else
temp = PyString_FromString("");
OBJSET(nameObj, "nickname", temp);
PyObject *wordList, *speechList, *args;
temp = PyInt_FromLong(name.language);
OBJSET(nameObj, "language", temp);
wordList = PyList_New(NAME_WORD_COUNT);
speechList = PyList_New(NAME_WORD_COUNT);
temp = PyBool_FromLong((int)name.has_name);
OBJSET(nameObj, "has_name", temp);
wordList = PyList_New(wordCount);
speechList = PyList_New(wordCount);
for(int i = 0; i < wordCount; i++)
for(int i = 0; i < NAME_WORD_COUNT; i++)
{
PyList_SET_ITEM(wordList, i, PyInt_FromLong(name.words[i]));
PyList_SET_ITEM(speechList, i, PyInt_FromLong(name.parts_of_speech[i]));
}
OBJSET(nameObj, "words", wordList);
OBJSET(nameObj, "parts_of_speech", speechList);
args = Py_BuildValue("ssiOOO", name.first_name, name.nickname, name.language, \
PyBool_FromLong((int)name.has_name), wordList, speechList);
nameObj = PyObject_CallObject(Name_type, args);
Py_DECREF(args);
return nameObj;
}
@ -219,8 +211,8 @@ static PyObject* BuildName(DFHack::t_name& name)
static DFHack::t_name ReverseBuildName(PyObject* nameObj)
{
PyObject *temp, *listTemp;
int boolTemp, arrLength;
Py_ssize_t listLength, strLength;
int boolTemp;
Py_ssize_t strLength;
char* strTemp;
DFHack::t_name name;
@ -231,38 +223,33 @@ static DFHack::t_name ReverseBuildName(PyObject* nameObj)
boolTemp = (int)PyInt_AsLong(temp);
Py_DECREF(temp);
if(boolTemp != 0)
name.has_name = true;
else
name.has_name = false;
//I seriously doubt the name arrays will change length, but why take chances?
listTemp = PyObject_GetAttrString(nameObj, "words");
arrLength = sizeof(name.words) / sizeof(uint32_t);
listLength = PyList_Size(listTemp);
if(listLength < arrLength)
arrLength = listLength;
for(int i = 0; i < arrLength; i++)
for(int i = 0; i < NAME_WORD_COUNT; i++)
name.words[i] = (uint32_t)PyInt_AsLong(PyList_GetItem(listTemp, i));
listTemp = PyObject_GetAttrString(nameObj, "parts_of_speech");
arrLength = sizeof(name.parts_of_speech) / sizeof(uint16_t);
listLength = PyList_Size(listTemp);
Py_DECREF(listTemp);
if(listLength < arrLength)
arrLength = listLength;
listTemp = PyObject_GetAttrString(nameObj, "parts_of_speech");
for(int i = 0; i < arrLength; i++)
for(int i = 0; i < NAME_WORD_COUNT; i++)
name.parts_of_speech[i] = (uint16_t)PyInt_AsLong(PyList_GetItem(listTemp, i));
Py_DECREF(listTemp);
temp = PyObject_GetAttrString(nameObj, "first_name");
strLength = PyString_Size(temp);
strTemp = PyString_AsString(temp);
Py_DECREF(temp);
if(strLength > 128)
{
strncpy(name.first_name, strTemp, 127);
@ -278,6 +265,8 @@ static DFHack::t_name ReverseBuildName(PyObject* nameObj)
strLength = PyString_Size(temp);
strTemp = PyString_AsString(temp);
Py_DECREF(temp);
if(strLength > 128)
{
strncpy(name.nickname, strTemp, 127);

@ -196,6 +196,12 @@ static PyObject* DF_Translate_TranslateName(DF_Translate* self, PyObject* args)
if(PyArg_ParseTuple(args, "O|i", &nameObj, &inEnglish))
return NULL;
if(PyObject_IsInstance(nameObj, Name_type) != 1)
{
PyErr_SetString(PyExc_TypeError, "argument 1 must be a Name object");
return NULL;
}
name = ReverseBuildName(nameObj);
std::string nameStr = self->tran_Ptr->TranslateName(name, (bool)inEnglish);

@ -17,9 +17,7 @@ Matgloss = namedtuple("Matgloss", "id, fore, back, bright, name")
DescriptorColor = namedtuple("DescriptorColor", "id, r, v, b, name")
CreatureTypeEx = namedtuple("CreatureTypeEx", "rawname, castes, tile_character, tilecolor")
TileColor = namedtuple("TileColor", "fore, back, bright")
class Name(object):
__slots__ = ["first_name", "nickname", "language", "has_name", "words", "parts_of_speech"]
Name = namedtuple("Name", "first_name, nickname, language, has_name, words, parts_of_speech")
class Soul(object):
def __init__(self, *args, **kwds):