diff --git a/CMakeLists.txt b/CMakeLists.txt index 84930eaca..f7a50432b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ include_directories (${CMAKE_SOURCE_DIR}/dfhack/depends/tinyxml/) include_directories (${CMAKE_SOURCE_DIR}/dfhack/depends/argstream/) add_subdirectory (dfhack) +add_subdirectory (dfhack/python) #add_subdirectory (dfhack/shm) #FIXME: add exports for MSVC #add_subdirectory (dfhack/depends/md5) diff --git a/COMPILE b/COMPILE index 70e319d8f..a4da697ba 100644 --- a/COMPILE +++ b/COMPILE @@ -1,20 +1,10 @@ Here's how you build dfhack! ---------------------------- -First, there is one dependency, regardless of the OS you use: - cmake - it's the build system - - Dependencies ============ You'll need cmake and 'a' compiler for building the main lib and the various tools. -To build the libdfconnect on Linux, you'll have to make a 32bit build. -To build the fake SDL.dll on Windows, you'll have to use MSVC 2008 Express and make -a Release or RelWithDebInfo build. - -The python bindings require SWIG and the python devel libs for building. - (Linux only) Veinlook requires the wide-character ncurses library (libncursesw) Building on Linux: @@ -86,8 +76,33 @@ MSVC generates. For example from 'output/' to 'output/Release/' I'm afraid you are on your own. dfhack wasn't tested with any other compiler. Try using a different cmake generator that's intended for your tools. +Build targets +------------- + +dfhack has a few build targets. +If you're only after the library run 'make dfhack'. +'make' will build everything. +'make expbench' will build the expbench testing program and the library. + +Build types +----------- + +cmake allows you to pick a build type by changing this variable: CMAKE_BUILD_TYPE + +cmake .. -DCMAKE_BUILD_TYPE:string=BUILD_TYPE + +Without specifying a build type or 'None', cmake uses the CMAKE_CXX_FLAGS +variable for building. +Valid an useful build types include 'Release', 'Debug' and 'RelWithDebInfo'. +There are others, but they aren't really that useful. + +Have fun. -Building the shared memory hook library (SHM) +-------------------------------------------------------------------------------- +**Deprecated*Deprecated*Deprecated*Deprecated*Deprecated*Deprecated*Deprecated** +-------------------------------------------------------------------------------- + +Building the shared memory hook library (SHM) --------------------------------------------- Unlike the rest of DFHack, The SHM needs special treatment when it comes to @@ -142,26 +157,3 @@ String writing is best tested with a fresh throw-away fort and dfrenamer. Embark, give one dwarf a very long name using dfrenamer and save/exit. If DF crashes during the save sequence, your SHM is not compatible with DF and the throw-away fort is most probably lost. - - -Build targets -------------- - -dfhack has a few build targets. -If you're only after the library run 'make dfhack'. -'make' will build everything. -'make expbench' will build the expbench testing program and the library. - -Build types ------------ - -cmake allows you to pick a build type by changing this variable: CMAKE_BUILD_TYPE - -cmake .. -DCMAKE_BUILD_TYPE:string=BUILD_TYPE - -Without specifying a build type or 'None', cmake uses the CMAKE_CXX_FLAGS -variable for building. -Valid an useful build types include 'Release', 'Debug' and 'RelWithDebInfo'. -There are others, but they aren't really that useful. - -Have fun. diff --git a/README b/README index 3260242d1..6f38709ef 100644 --- a/README +++ b/README @@ -20,20 +20,6 @@ and the arch-games repository. The package name is dfhack-git :) -Clarification -------------- - -'fake SDL.dll', 'SHM', 'libdfconnect' and 'shim library' are basically the same -thing. A library that sits between DFHack and DF, provides some extra features, -synchronisation and access control. You should use it for everything that writes -data back to DF. - -You'll need the SHM to attach more than one tool to DF on Linux. -The DFHack version of Dwarf Therapist requires it for writing creature names -and professions. - -See 'Using DFHack Tools' for instructions on installing it. - Compatibility ------------- @@ -46,17 +32,10 @@ OSX is also not supported due to lack of developers with a Mac. Currently supported Dwarf Fortress versions: * Windows - 38a - 40d19_2 + 0.31.01 - 0.31.03 * Linux - 40d2 - 40d19 - -Currently supported DF versions *by the SHM*: -* Windows - 40d15 - 40d19 - -* Linux - 40d9 - 40d19 (40d2 - 40d8 might work, but are untested) + wine together with the Windows versions Using the library as a developer -------------------------------- @@ -72,112 +51,34 @@ At the time of writing there's no API reference or documentation. The code does have a lot of comments though (and getting better all the time). -Using DFHack Tools ------------------- - -The project comes with a special extra library you should add to your DF -installation. It's used to boost the transfer speed between DF and DFHack, and -provide data consistency and synchronization. DFHack will work without the -library, but at suboptimal speeds and the consistency of data written back -to DF is questionable. - -!!! on Windows this currently only works with DF 40d15 - 40d19_2 !!! - On Linux, it works with DF 40d9 - 40d19 - -!!! use the pre-compiled library intended for your OS and version of DF !!! - You can find them in the 'precompiled' folder. - - - ** Installing on Windows - - Open your DF folder, locate SDL.dll and rename it to SDLreal.dll (making - a backup of it is a good idea) - - Copy the right DFHack SDL.dll into your DF folder. - - Restart DF if it is running - - ** Unistalling on Windows - - Open your DF folder, locate SDL.dll and delete it - - Rename SDLreal.dll to SDL.dll - - Restart DF if it is running - - - ** Installing on Linux - - Open your DF folder and the libs folder within it - - copy DFHack libdfconnect.so to the libs folder - - copy the df-hacked script to your DF folder - - Use this new startup script to start DF - - ** Replacing libdfconnect.so - - Make sure you close DF before you replace this file! DF crashes whn you don't - close it first! - - ** Uninstalling on Linux - - Open your DF and DF/libs folders - - Delete libdfconnect.so and the dfhacked startup script - - Go back to using the df startup script - - Tools ----- - All the DFHack tools are terminal programs. This might seem strange to Windows users, but these are meant mostly as examples for developers. Still, they can be useful and are cross-platform just like the library itself. -If the tool writes back to DF's memory, make sure you are using the shared -memory interface mentioned in the previous section! - -* catsplosion- Makes all cats pregnant and due in 100 game steps. Use with care. - -* magma_create - creates 7/7 magma at the DF's cursor - -* reveal - plain old reveal tool. It reveals all the map blocks already - initialized by DF. - -* veinlook - a silly map viewer. It can mass-dig veins in a map square. - (Linux only) - -* prospector - scans the map for minerals. by default it only scans only visible - veins. You can make it show hidden things with '-a' and base rock - and soil layers with '-b'. These can be combined ('-ab') - -* cleanmap - cleans mud, vomit, snow and all kinds of mess from the map. - It will clean your irrigated farms too, so consider yourself - warned. - -* incrementalsearch - incremental search utility (Linux only). - -* bauxite - converts all mechanisms into bauxite mechanisms. - -* itemdesignator - Allows mass-designating items by type and material - dump, - forbid, melt and set on fire ;) -* digger - allows designating tiles for digging/cutting/ramp removal - - A list of accepted tile classes: - 1 = WALL - 2 = PILLAR - 3 = FORTIFICATION - - 4 = STAIR_UP - 5 = STAIR_DOWN - 6 = STAIR_UPDOWN - - 7 = RAMP - 8 = RAMP_TOP - - 9 = FLOOR - 10 = TREE_DEAD - 11 = TREE_OK - 12 = SAPLING_DEAD - 13 = SAPLING_OK - 14 = SHRUB_DEAD - 15 = SHRUB_OK - 16 = BOULDER - 17 = PEBBLES - - Example : dfdigger -o 100,100,15 -t 9,10 -m 10 - This will start looking for trees at coords 100,100,15 and designate ten of them for cutting. - - + - dfcleanmap : Cleans all the splatter that get scattered all over the map. + Only exception is mud. It leaves mud alone. + - dfexpbench : Just a simple benchmark of the data export speed. + - dfliquids : A command prompt for liquid creation and manipulation + (the Moses effect included!) + Also allows painting obsidian walls directly. + Note: + Spawning and deleting liquids can F up pathing data and + temperatures (creating heat traps). You've been warned. + - dfposition : Prints the current DF window properties and cursor position. + - dfprospector: Lists all available minerals on the map and how much + of them there is. + - dfreveal : Reveals the whole map, waits for input and hides it again. + If you close the tool while it waits, the map remains revealed. + - dfsuspend : Test of the process suspend/resume mechanism. + - dfunstuck : Use if you prematurely close any of the tools and DF + appears to be stuck. + - dfvdig : Designates a whole vein for digging. Point the cursor at a vein + and run this thing :) + + - Your tool here: Write one ;) + Memory offset definitions ------------------------- diff --git a/dfhack/include/config.h b/dfhack/include/config.h deleted file mode 100644 index a7de5f1c3..000000000 --- a/dfhack/include/config.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef CONFIG_H -#define CONFIG_H - -#define MEMXML_DATA_PATH . -#define HAVE_64_BIT - -#endif // CONFIG_H diff --git a/dfhack/include/modules/Creatures.h b/dfhack/include/modules/Creatures.h index c5e8d8640..af0014b8b 100644 --- a/dfhack/include/modules/Creatures.h +++ b/dfhack/include/modules/Creatures.h @@ -375,7 +375,7 @@ namespace DFHack bool WriteLabors(const uint32_t index, uint8_t labors[NUM_CREATURE_LABORS]); uint32_t GetDwarfRaceIndex ( void ); int32_t GetDwarfCivId ( void ); - bool ReadJob(const t_creature * furball, vector & mat); + bool ReadJob(const t_creature * furball, std::vector & mat); private: struct Private; Private *d; diff --git a/dfhack/include/modules/Maps.h b/dfhack/include/modules/Maps.h index ca0c052bc..ee4b1984f 100644 --- a/dfhack/include/modules/Maps.h +++ b/dfhack/include/modules/Maps.h @@ -19,7 +19,7 @@ namespace DFHack feature_Underworld, feature_Hell_Temple }; - static char * sa_feature[]= + static const char * sa_feature[]= { (char*)"Other", (char*)"Adamantine Tube", diff --git a/dfhack/include/modules/Materials.h b/dfhack/include/modules/Materials.h index 794d175ad..d5e52dd16 100644 --- a/dfhack/include/modules/Materials.h +++ b/dfhack/include/modules/Materials.h @@ -19,11 +19,11 @@ namespace DFHack struct t_descriptor_color { - char id[128]; // id in the raws - float r; - float v; - float b; - char name[128]; //displayed name + char id[128]; // id in the raws + float r; + float v; + float b; + char name[128]; //displayed name }; struct t_matglossPlant diff --git a/dfhack/modules/Materials.cpp b/dfhack/modules/Materials.cpp index ae65946ec..b32d4f234 100644 --- a/dfhack/modules/Materials.cpp +++ b/dfhack/modules/Materials.cpp @@ -199,7 +199,24 @@ inline bool ReadNamesOnly(Process* p, uint32_t address, vector & nam bool Materials::ReadInorganicMaterials (void) { - return ReadNamesOnly(d->owner, d->owner->getDescriptor()->getAddress ("mat_inorganics"), inorganic ); + Process * p = d->owner; + DfVector p_matgloss (p, d->owner->getDescriptor()->getAddress ("mat_inorganics")); + uint32_t size = p_matgloss.size(); + inorganic.clear(); + inorganic.reserve (size); + for (uint32_t i = 0; i < size;i++) + { + t_matgloss mat; + + p->readSTLString (p_matgloss[i], mat.id, 128); + //p->readSTLString (p_matgloss[i] + mat_name, mat.name, 128); + mat.name[0] = 0; + mat.fore = 0; + mat.back = 0; + mat.bright = 0; + inorganic.push_back(mat); + } + return true; } bool Materials::ReadOrganicMaterials (void) diff --git a/dfhack/python/CMakeLists.txt b/dfhack/python/CMakeLists.txt new file mode 100644 index 000000000..a0ee7d5d7 --- /dev/null +++ b/dfhack/python/CMakeLists.txt @@ -0,0 +1,42 @@ +PROJECT (pydfhack) +FIND_PACKAGE(PythonLibs) + +SET(PYTHON_MODULE_PREFIX "") + +SET(PROJECT_LIBS ${PYTHON_LIBRARIES} dfhack ) + +IF(UNIX) + add_definitions(-DLINUX_BUILD) + add_definitions(-DUSE_CONFIG_H) + SET(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -pedantic") + SET(PYTHON_MODULE_SUFFIX ".so") +ENDIF(UNIX) +IF(WIN32) + #windows + SET(PYTHON_MODULE_SUFFIX ".pyd") +ENDIF(WIN32) + +IF(PYTHONLIBS_FOUND) + INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH}) + PYTHON_ADD_MODULE(pydfhack + DF_API.cpp + DF_Buildings.cpp + DF_Constructions.cpp + DF_CreatureManager.cpp + DF_GUI.cpp + DF_Maps.cpp + DF_Material.cpp + DF_Position.cpp + DF_Translate.cpp + DF_Vegetation.cpp + pydfhack.cpp + ) + SET_TARGET_PROPERTIES(pydfhack PROPERTIES PREFIX "") + # fix suffix on windows + SET_TARGET_PROPERTIES(pydfhack PROPERTIES SUFFIX ${PYTHON_MODULE_SUFFIX}) + + TARGET_LINK_LIBRARIES(pydfhack ${PROJECT_LIBS}) + +ELSE(PYTHONLIBS_FOUND) + MESSAGE("UNABLE TO BUILD PYTHON BINDINGS!") +ENDIF(PYTHONLIBS_FOUND) diff --git a/dfhack/python/DF_API.cpp b/dfhack/python/DF_API.cpp index f80534353..b705ef627 100644 --- a/dfhack/python/DF_API.cpp +++ b/dfhack/python/DF_API.cpp @@ -27,6 +27,8 @@ distribution. #include "Python.h" #include +#include "integers.h" + #include "DFTypes.h" #include "DFHackAPI.h" #include "DF_Imports.cpp" @@ -144,7 +146,7 @@ static void DF_API_dealloc(DF_API* self) if(self->api_Ptr != NULL) { - PySys_WriteStdout("api_Ptr = %i\n", (int)self->api_Ptr); + PySys_WriteStdout("api_Ptr = 0x%x\n", self->api_Ptr); delete self->api_Ptr; @@ -692,4 +694,4 @@ static PyTypeObject DF_API_type = DF_API_new, /* tp_new */ }; -#endif \ No newline at end of file +#endif diff --git a/dfhack/python/DF_Buildings.cpp b/dfhack/python/DF_Buildings.cpp index 00bfe146e..3bfaadd7a 100644 --- a/dfhack/python/DF_Buildings.cpp +++ b/dfhack/python/DF_Buildings.cpp @@ -1,6 +1,6 @@ /* www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -28,6 +28,7 @@ distribution. #include "Python.h" #include #include +#include "integers.h" using namespace std; @@ -113,7 +114,7 @@ static void DF_Building_dealloc(DF_Building* self) if(self->b_Ptr != NULL) { - PySys_WriteStdout("b_Ptr = %i\n", (int)self->b_Ptr); + PySys_WriteStdout("b_Ptr = 0x%x\n", self->b_Ptr); delete self->b_Ptr; @@ -268,4 +269,4 @@ static PyTypeObject DF_Building_type = DF_Building_new, /* tp_new */ }; -#endif \ No newline at end of file +#endif diff --git a/dfhack/python/DF_Constructions.cpp b/dfhack/python/DF_Constructions.cpp index 0bfe99320..f54cb5e65 100644 --- a/dfhack/python/DF_Constructions.cpp +++ b/dfhack/python/DF_Constructions.cpp @@ -1,6 +1,6 @@ /* www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -26,6 +26,7 @@ distribution. #define __DFCONSTRUCTIONS__ #include "Python.h" +#include "integers.h" #include "modules/Constructions.h" #include "DF_Helpers.cpp" @@ -104,7 +105,7 @@ static void DF_Construction_dealloc(DF_Construction* self) if(self->c_Ptr != NULL) { - PySys_WriteStdout("c_Ptr = %i\n", (int)self->c_Ptr); + PySys_WriteStdout("c_Ptr = 0x%x\n", self->c_Ptr); delete self->c_Ptr; @@ -217,4 +218,4 @@ static PyTypeObject DF_Construction_type = DF_Construction_new, /* tp_new */ }; -#endif \ No newline at end of file +#endif diff --git a/dfhack/python/DF_CreatureManager.cpp b/dfhack/python/DF_CreatureManager.cpp index c9b4136c8..166e67e2e 100644 --- a/dfhack/python/DF_CreatureManager.cpp +++ b/dfhack/python/DF_CreatureManager.cpp @@ -1,6 +1,6 @@ /* www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -27,6 +27,8 @@ distribution. #include "Python.h" #include "stdio.h" +#include +#include "integers.h" #include "DFTypes.h" #include "modules/Creatures.h" #include "DF_CreatureType.cpp" @@ -68,7 +70,7 @@ static void DF_CreatureManager_dealloc(DF_CreatureManager* self) if(self->creature_Ptr != NULL) { - PySys_WriteStdout("creature_Ptr = %i\n", (int)self->creature_Ptr); + PySys_WriteStdout("creature_Ptr = 0x%x\n", self->creature_Ptr); delete self->creature_Ptr; @@ -272,4 +274,4 @@ static PyTypeObject DF_CreatureManager_type = DF_CreatureManager_new, /* tp_new */ }; -#endif \ No newline at end of file +#endif diff --git a/dfhack/python/DF_CreatureType.cpp b/dfhack/python/DF_CreatureType.cpp index 99a4e081a..5872913f5 100644 --- a/dfhack/python/DF_CreatureType.cpp +++ b/dfhack/python/DF_CreatureType.cpp @@ -1,6 +1,6 @@ /* www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -253,4 +253,4 @@ static PyObject* BuildCreature(DFHack::t_creature& creature) Py_RETURN_NONE; } -#endif \ No newline at end of file +#endif diff --git a/dfhack/python/DF_GUI.cpp b/dfhack/python/DF_GUI.cpp index 5e9d064de..94bd3c5c8 100644 --- a/dfhack/python/DF_GUI.cpp +++ b/dfhack/python/DF_GUI.cpp @@ -1,6 +1,6 @@ /* www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -26,6 +26,7 @@ distribution. #define __DFGUI__ #include "Python.h" +#include "integers.h" #include "DFTypes.h" #include "modules/Gui.h" @@ -64,7 +65,7 @@ static void DF_GUI_dealloc(DF_GUI* self) if(self->g_Ptr != NULL) { - PySys_WriteStdout("g_Ptr = %i\n", (int)self->g_Ptr); + PySys_WriteStdout("g_Ptr = 0x%x\n", self->g_Ptr); delete self->g_Ptr; @@ -203,4 +204,4 @@ static PyTypeObject DF_GUI_type = DF_GUI_new, /* tp_new */ }; -#endif \ No newline at end of file +#endif diff --git a/dfhack/python/DF_Helpers.cpp b/dfhack/python/DF_Helpers.cpp index 6c4b580cd..5cbe166b1 100644 --- a/dfhack/python/DF_Helpers.cpp +++ b/dfhack/python/DF_Helpers.cpp @@ -1,6 +1,6 @@ /* www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -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); @@ -376,4 +365,4 @@ static PyObject* BuildSoul(DFHack::t_soul& soul) return soulObj; } -#endif \ No newline at end of file +#endif diff --git a/dfhack/python/DF_Imports.cpp b/dfhack/python/DF_Imports.cpp index b163a8822..6bc07e546 100644 --- a/dfhack/python/DF_Imports.cpp +++ b/dfhack/python/DF_Imports.cpp @@ -1,6 +1,6 @@ /* www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -97,4 +97,4 @@ static void DoImports() } } -#endif \ No newline at end of file +#endif diff --git a/dfhack/python/DF_Maps.cpp b/dfhack/python/DF_Maps.cpp index e7d4297e6..03971c2ed 100644 --- a/dfhack/python/DF_Maps.cpp +++ b/dfhack/python/DF_Maps.cpp @@ -1,6 +1,6 @@ /* www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -28,10 +28,12 @@ distribution. #include "Python.h" #include #include +#include "integers.h" using namespace std; #include "DFTypes.h" +#include #include "modules/Maps.h" #include "DF_Imports.cpp" #include "DF_Helpers.cpp" @@ -391,7 +393,7 @@ static void DF_Map_dealloc(DF_Map* self) if(self->m_Ptr != NULL) { - PySys_WriteStdout("m_Ptr = %i\n", (int)self->m_Ptr); + PySys_WriteStdout("m_Ptr = 0x%x\n", self->m_Ptr); delete self->m_Ptr; @@ -857,4 +859,4 @@ static PyTypeObject DF_Map_type = DF_Map_new, /* tp_new */ }; -#endif \ No newline at end of file +#endif diff --git a/dfhack/python/DF_Material.cpp b/dfhack/python/DF_Material.cpp index 56813c5d9..33ecf5b6a 100644 --- a/dfhack/python/DF_Material.cpp +++ b/dfhack/python/DF_Material.cpp @@ -1,6 +1,6 @@ /* www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -27,6 +27,7 @@ distribution. #include "Python.h" #include +#include "integers.h" using namespace std; @@ -244,7 +245,7 @@ static void DF_Material_dealloc(DF_Material* self) if(self->mat_Ptr != NULL) { - PySys_WriteStdout("mat_Ptr = %i\n", (int)self->mat_Ptr); + PySys_WriteStdout("mat_Ptr = 0x%x\n", self->mat_Ptr); delete self->mat_Ptr; @@ -421,4 +422,4 @@ static PyTypeObject DF_Material_type = DF_Material_new, /* tp_new */ }; -#endif \ No newline at end of file +#endif diff --git a/dfhack/python/DF_MemInfo.cpp b/dfhack/python/DF_MemInfo.cpp index 4680b502e..c55cf4bbb 100644 --- a/dfhack/python/DF_MemInfo.cpp +++ b/dfhack/python/DF_MemInfo.cpp @@ -1,6 +1,6 @@ /* www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -71,7 +71,7 @@ static void DF_MemInfo_dealloc(DF_MemInfo* self) if(self->mem_Ptr != NULL) { - PySys_WriteStdout("mem_Ptr = %i\n", (int)self->mem_Ptr); + PySys_WriteStdout("mem_Ptr = 0x%x\n", self->mem_Ptr); delete self->mem_Ptr; @@ -681,4 +681,4 @@ static PyTypeObject DF_MemInfo_type = DF_MemInfo_new, /* tp_new */ }; -#endif \ No newline at end of file +#endif diff --git a/dfhack/python/DF_Position.cpp b/dfhack/python/DF_Position.cpp index cc251b185..64f70065d 100644 --- a/dfhack/python/DF_Position.cpp +++ b/dfhack/python/DF_Position.cpp @@ -1,6 +1,6 @@ /* www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -26,6 +26,7 @@ distribution. #define __DFPOSITION__ #include "Python.h" +#include "integers.h" #include "modules/Position.h" using namespace DFHack; @@ -198,4 +199,4 @@ static PyTypeObject DF_Position_type = DF_Position_new, /* tp_new */ }; -#endif \ No newline at end of file +#endif diff --git a/dfhack/python/DF_Translate.cpp b/dfhack/python/DF_Translate.cpp index 1ce635f5c..ca570182b 100644 --- a/dfhack/python/DF_Translate.cpp +++ b/dfhack/python/DF_Translate.cpp @@ -1,6 +1,6 @@ /* www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -28,6 +28,7 @@ distribution. #include "Python.h" #include #include +#include "integers.h" using namespace std; @@ -75,7 +76,7 @@ static void DF_Translate_dealloc(DF_Translate* self) { Py_XDECREF(self->dict); - PySys_WriteStdout("tran_Ptr = %i\n", (int)self->tran_Ptr); + PySys_WriteStdout("tran_Ptr = 0x%x\n", self->tran_Ptr); delete self->tran_Ptr; @@ -196,6 +197,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); @@ -278,4 +285,4 @@ static PyTypeObject DF_Translate_type = DF_Translate_new, /* tp_new */ }; -#endif \ No newline at end of file +#endif diff --git a/dfhack/python/DF_Vegetation.cpp b/dfhack/python/DF_Vegetation.cpp index f549b55c0..ae8b6a016 100644 --- a/dfhack/python/DF_Vegetation.cpp +++ b/dfhack/python/DF_Vegetation.cpp @@ -1,6 +1,6 @@ /* www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -26,6 +26,7 @@ distribution. #define __DFVEGETATION__ #include "Python.h" +#include "integers.h" #include "modules/Vegetation.h" #include "DF_Helpers.cpp" @@ -78,7 +79,7 @@ static void DF_Vegetation_dealloc(DF_Vegetation* self) if(self->veg_Ptr != NULL) { - PySys_WriteStdout("veg_Ptr = %i\n", (int)self->veg_Ptr); + PySys_WriteStdout("veg_Ptr = 0x%x\n", self->veg_Ptr); delete self->veg_Ptr; @@ -191,4 +192,4 @@ static PyTypeObject DF_Vegetation_type = DF_Vegetation_new, /* tp_new */ }; -#endif \ No newline at end of file +#endif diff --git a/dfhack/python/linsetup.py b/dfhack/python/linsetup.py new file mode 100644 index 000000000..356300304 --- /dev/null +++ b/dfhack/python/linsetup.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +from distutils.core import setup, Extension + +e = Extension("pydfhack", + sources=["DF_API.cpp", "DF_Buildings.cpp", "DF_Constructions.cpp", "DF_CreatureManager.cpp", "DF_GUI.cpp", "DF_Maps.cpp", "DF_Material.cpp", "DF_Position.cpp", "DF_Translate.cpp", "DF_Vegetation.cpp", "pydfhack.cpp"], + include_dirs=["../", "../include", "../depends/md5", "../depends/tinyxml"], + library_dirs=["../../output"], + extra_compile_args=["-DLINUX_BUILD"], + libraries=["dfhack-debug"], + export_symbols=["initpydfhack", "ReadRaw", "WriteRaw"]) + +setup(name="PyDFHack", version="1.0", ext_modules=[e]) diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index b01adad1c..3ce0634c2 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -1,6 +1,6 @@ /* www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -132,4 +132,4 @@ PyMODINIT_FUNC initpydfhack(void) DoImports(); -} \ No newline at end of file +} diff --git a/dfhack/python/pydftypes.py b/dfhack/python/pydftypes.py index 5ff79d7ea..972729bdd 100644 --- a/dfhack/python/pydftypes.py +++ b/dfhack/python/pydftypes.py @@ -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): diff --git a/dfhack/python/setup.py b/dfhack/python/setup.py index a115d1003..5c60e8379 100644 --- a/dfhack/python/setup.py +++ b/dfhack/python/setup.py @@ -3,7 +3,7 @@ from distutils.core import setup, Extension e = Extension("pydfhack", sources=["DF_API.cpp", "DF_Buildings.cpp", "DF_Constructions.cpp", "DF_CreatureManager.cpp", "DF_GUI.cpp", "DF_Maps.cpp", "DF_Material.cpp", "DF_Position.cpp", "DF_Translate.cpp", "DF_Vegetation.cpp", "pydfhack.cpp"], - include_dirs=["..\\", "..\\include", "..\\depends\\md5", "..\\depends\\tinyxml"], + include_dirs=["../", "../include", "../depends/md5", "../depends/tinyxml"], library_dirs=["..\\..\\output"], #extra_compile_args=["-w"], libraries=["libdfhack"], diff --git a/output/stones.py b/output/stones.py index de90f8e7e..4db1b3361 100644 --- a/output/stones.py +++ b/output/stones.py @@ -1,11 +1,11 @@ # -*- coding: utf-8 -*- import pydfhack -DF = pydfhack.API("Memory.xml") +DF = pydfhack._API("Memory.xml") if DF.Attach(): - success,stones = DF.ReadStoneMatgloss() - if success: - print "Dumping all stone" - for matgloss in stones: - print "ID %s, name %s" % (matgloss.id, matgloss.name) + Mats = DF.materials + stones = Mats.Read_Inorganic_Materials() + print "Dumping all stone" + for matgloss in stones: + print "ID %s, name %s" % (matgloss.id, matgloss.name) DF.Detach()