diff --git a/dfhack/CMakeLists.txt b/dfhack/CMakeLists.txt index e7c446561..366b80d6a 100644 --- a/dfhack/CMakeLists.txt +++ b/dfhack/CMakeLists.txt @@ -13,6 +13,8 @@ include/DFTypes.h include/DFVector.h include/DFWindow.h #include/DFHackAPI_C.h +include/DFTypes_C.h +include/DFContext_C.h include/integers.h shm/shms.h ) @@ -26,7 +28,8 @@ APIPrivate.cpp DFTileTypes.cpp DFVector.cpp #DFHackAPI_C.cpp -#DFTypes_C.cpp +DFContext_C.cpp +DFTypes_C.cpp depends/md5/md5.cpp depends/md5/md5wrapper.cpp @@ -48,16 +51,16 @@ modules/Vegetation.cpp modules/Buildings.cpp modules/Constructions.cpp -#modules/Position_C.cpp -#modules/Gui_C.cpp -#modules/Materials_C.cpp -#modules/Buildings_C.cpp -#modules/Constructions_C.cpp -#modules/Maps_C.cpp -#modules/Vegetation_C.cpp -#modules/Creatures_C.cpp -#modules/Translation_C.cpp -#modules/Items_C.cpp +modules/Position_C.cpp +modules/Gui_C.cpp +modules/Materials_C.cpp +modules/Buildings_C.cpp +modules/Constructions_C.cpp +modules/Maps_C.cpp +modules/Vegetation_C.cpp +modules/Creatures_C.cpp +modules/Translation_C.cpp +modules/Items_C.cpp ) SET(PROJECT_HDRS_LINUX diff --git a/dfhack/DFContext_C.cpp b/dfhack/DFContext_C.cpp new file mode 100644 index 000000000..855bda6f6 --- /dev/null +++ b/dfhack/DFContext_C.cpp @@ -0,0 +1,367 @@ +/* +www.sourceforge.net/projects/dfhack +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 +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 "Tranquility.h" +#include "Export.h" +#include +#include +#include +#include "integers.h" +#include "DFTileTypes.h" +#include "DFTypes.h" +#include "DFWindow.h" +#include "DFContextManager.h" +#include "DFContext.h" + +using namespace std; +using namespace DFHack; + +#include "DFContext_C.h" + +#ifdef __cplusplus +extern "C" { +#endif + +DFHackObject* ContextManager_Alloc(const char* path_to_xml) +{ + DFHack::ContextManager* contextMgr = new DFHack::ContextManager(std::string(path_to_xml)); + return (DFHackObject*)contextMgr; +} + +//FIXME: X:\dfhack\DFHackContext_C.cpp:56: warning: deleting `DFHackObject* ' is undefined +//DC: Yeah, I forgot that trying to delete a void pointer might be a bad idea. This works now. +void ContextManager_Free(DFHackObject* contextMgr) +{ + if(contextMgr != NULL) + { + DFHack::ContextManager* a = (DFHack::ContextManager*)contextMgr; + delete a; + + contextMgr = NULL; + } +} + +int ContextManager_Refresh(DFHackObject* contextMgr) +{ + if(contextMgr != NULL) + { + return ((DFHack::ContextManager*)contextMgr)->Refresh(); + } + + return -1; +} + +int ContextManager_size(DFHackObject* contextMgr, uint32_t* size) +{ + if(contextMgr != NULL) + { + uint32_t result = ((DFHack::ContextManager*)contextMgr)->size(); + + *size = result; + + return 1; + } + + return -1; +} + +int ContextManager_purge(DFHackObject* contextMgr) +{ + if(contextMgr != NULL) + { + ((DFHack::ContextManager*)contextMgr)->purge(); + + return 1; + } + + return -1; +} + +DFHackObject* ContextManager_getContext(DFHackObject* contextMgr, uint32_t index) +{ + if(contextMgr != NULL) + { + DFHack::ContextManager* mgr = ((DFHack::ContextManager*)contextMgr); + + if(index >= mgr->size()) + return NULL; + + return (DFHackObject*)((DFHack::Context*)((*mgr)[index])); + } + + return NULL; +} + +DFHackObject* ContextManager_getSingleContext(DFHackObject* contextMgr) +{ + if(contextMgr != NULL) + { + return (DFHackObject*)((DFHack::ContextManager*)contextMgr)->getSingleContext(); + } + + return NULL; +} + +void Context_Free(DFHackObject* context) +{ + if(context != NULL) + { + DFHack::Context* c = (DFHack::Context*)context; + delete c; + + context = NULL; + } +} + +int Context_Attach(DFHackObject* context) +{ + if(context != NULL) + { + return ((DFHack::Context*)context)->Attach(); + } + + return -1; +} + +int Context_Detach(DFHackObject* context) +{ + if(context != NULL) + { + return ((DFHack::Context*)context)->Detach(); + } + + return -1; +} + +int Context_isAttached(DFHackObject* context) +{ + if(context != NULL) + { + return ((DFHack::Context*)context)->isAttached(); + } + + return -1; +} + +int Context_Suspend(DFHackObject* context) +{ + if(context != NULL) + { + return ((DFHack::Context*)context)->Suspend(); + } + + return -1; +} + +int Context_Resume(DFHackObject* context) +{ + if(context != NULL) + { + return ((DFHack::Context*)context)->Resume(); + } + + return -1; +} + +int Context_isSuspended(DFHackObject* context) +{ + if(context != NULL) + { + return ((DFHack::Context*)context)->isSuspended(); + } + + return -1; +} + +int Context_ForceResume(DFHackObject* context) +{ + if(context != NULL) + { + return ((DFHack::Context*)context)->ForceResume(); + } + + return -1; +} + +int Context_AsyncSuspend(DFHackObject* context) +{ + if(context != NULL) + { + return ((DFHack::Context*)context)->AsyncSuspend(); + } + + return -1; +} + +//module getters + +DFHackObject* Context_getMemoryInfo(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getMemoryInfo(); + } + + return NULL; +} + +DFHackObject* Context_getProcess(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getProcess(); + } + + return NULL; +} + +DFHackObject* Context_getWindow(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getWindow(); + } + + return NULL; +} + +DFHackObject* Context_getCreatures(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getCreatures(); + } + + return NULL; +} + +DFHackObject* Context_getMaps(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getMaps(); + } + + return NULL; +} + +DFHackObject* Context_getGui(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getGui(); + } + + return NULL; +} + +DFHackObject* Context_getPosition(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getPosition(); + } + + return NULL; +} + +DFHackObject* Context_getMaterials(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getMaterials(); + } + + return NULL; +} + +DFHackObject* Context_getTranslation(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getTranslation(); + } + + return NULL; +} + +DFHackObject* Context_getVegetation(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getVegetation(); + } + + return NULL; +} + +DFHackObject* Context_getBuildings(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getBuildings(); + } + + return NULL; +} + +DFHackObject* Context_getConstructions(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getConstructions(); + } + + return NULL; +} + +DFHackObject* Context_getItems(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getItems(); + } + + return NULL; +} + +void Context_ReadRaw(DFHackObject* context, const uint32_t offset, const uint32_t size, uint8_t* target) +{ + if(context != NULL) + { + ((DFHack::Context*)context)->ReadRaw(offset, size, target); + } +} + +void Context_WriteRaw(DFHackObject* context, const uint32_t offset, const uint32_t size, uint8_t* source) +{ + if(context != NULL) + { + ((DFHack::Context*)context)->WriteRaw(offset, size, source); + } +} + +#ifdef __cplusplus +} +#endif diff --git a/dfhack/DFTypes_C.cpp b/dfhack/DFTypes_C.cpp index c5f39908f..e969ba8ec 100644 --- a/dfhack/DFTypes_C.cpp +++ b/dfhack/DFTypes_C.cpp @@ -32,8 +32,8 @@ using namespace std; #include "DFCommonInternal.h" #include "DFTypes.h" -#include "modules/Materials.h" #include "DFTypes_C.h" +#include "modules/Materials.h" using namespace DFHack; @@ -41,119 +41,31 @@ using namespace DFHack; extern "C" { #endif -c_colormodifier* ColorModifier_New() -{ - c_colormodifier* temp; - - temp = (c_colormodifier*)malloc(sizeof(c_colormodifier)); - - if(temp == NULL) - return NULL; - - temp->part[0] = '\0'; - temp->colorlist = NULL; - temp->colorlistLength = 0; - - return temp; -} +int8_t* (*alloc_byte_buffer_callback)(uint32_t) = NULL; +int16_t* (*alloc_short_buffer_callback)(uint32_t) = NULL; +int32_t* (*alloc_int_buffer_callback)(uint32_t) = NULL; -void ColorModifier_Free(c_colormodifier* src) -{ - if(src != NULL) - { - if(src->colorlist != NULL) - free(src->colorlist); - - free(src); - } -} +uint8_t* (*alloc_ubyte_buffer_callback)(uint32_t) = NULL; +uint16_t* (*alloc_ushort_buffer_callback)(uint32_t) = NULL; +uint32_t* (*alloc_uint_buffer_callback)(uint32_t) = NULL; -c_creaturecaste* CreatureCaste_New() -{ - c_creaturecaste* temp; - - temp = (c_creaturecaste*)malloc(sizeof(c_creaturecaste)); - - if(temp == NULL) - return NULL; - - temp->rawname[0] = '\0'; - temp->singular[0] = '\0'; - temp->plural[0] = '\0'; - temp->adjective[0] = '\0'; - - temp->ColorModifier = NULL; - temp->colorModifierLength = 0; - - temp->bodypart = NULL; - temp->bodypartLength = 0; - - return temp; -} +char* (*alloc_char_buffer_callback)(uint32_t) = NULL; -void CreatureCaste_Free(c_creaturecaste* src) -{ - if(src != NULL) - { - if(src->bodypart != NULL) - free(src->bodypart); - - if(src->ColorModifier != NULL) - { - for(int i = 0; i < src->colorModifierLength; i++) - ColorModifier_Free(&src->ColorModifier[i]); - - free(src->ColorModifier); - } - - free(src); - } -} +t_matgloss* (*alloc_matgloss_buffer_callback)(int) = NULL; +t_descriptor_color* (*alloc_descriptor_buffer_callback)(int) = NULL; +t_matglossOther* (*alloc_matgloss_other_buffer_callback)(int) = NULL; -c_creaturetype* CreatureType_New() -{ - c_creaturetype* temp; - - temp = (c_creaturetype*)malloc(sizeof(c_creaturetype)); - - if(temp == NULL) - return NULL; - - temp->rawname[0] = '\0'; - - temp->castes = NULL; - temp->castesCount = 0; - - temp->extract = NULL; - temp->extractCount = 0; - - temp->tile_character = 0; - - temp->tilecolor.fore = 0; - temp->tilecolor.back = 0; - temp->tilecolor.bright = 0; - - return temp; -} +c_colormodifier* (*alloc_empty_colormodifier_callback)(void) = NULL; +c_colormodifier* (*alloc_colormodifier_callback)(const char*, uint32_t) = NULL; +c_colormodifier* (*alloc_colormodifier_buffer_callback)(uint32_t) = NULL; -void CreatureType_Free(c_creaturetype* src) -{ - if(src != NULL) - { - if(src->castes != NULL) - { - for(int i = 0; i < src->castesCount; i++) - CreatureCaste_Free(&src->castes[i]); - - free(src->castes); - } - - if(src->extract != NULL) - free(src->extract); - - free(src); - } -} +c_creaturecaste* (*alloc_empty_creaturecaste_callback)(void) = NULL; +c_creaturecaste* (*alloc_creaturecaste_callback)(const char*, const char*, const char*, const char*, uint32_t, uint32_t) = NULL; +c_creaturecaste* (*alloc_creaturecaste_buffer_callback)(uint32_t) = NULL; + +c_creaturetype* (*alloc_empty_creaturetype_callback)(void) = NULL; +c_creaturetype* (*alloc_creaturetype_callback)(const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t) = NULL; +c_creaturetype* (*alloc_creaturetype_buffer_callback)(uint32_t) = NULL; #ifdef __cplusplus } @@ -161,13 +73,10 @@ void CreatureType_Free(c_creaturetype* src) int ColorListConvert(t_colormodifier* src, c_colormodifier* dest) { - if(src == NULL || dest == NULL) + if(src == NULL) return -1; - strcpy(dest->part, src->part); - - dest->colorlistLength = src->colorlist.size(); - dest->colorlist = (uint32_t*)malloc(sizeof(uint32_t) * dest->colorlistLength); + dest = ((*alloc_colormodifier_callback)(src->part, src->colorlist.size())); copy(src->colorlist.begin(), src->colorlist.end(), dest->colorlist); @@ -176,23 +85,14 @@ int ColorListConvert(t_colormodifier* src, c_colormodifier* dest) int CreatureCasteConvert(t_creaturecaste* src, c_creaturecaste* dest) { - if(src == NULL || dest == NULL) + if(src == NULL) return -1; - strcpy(dest->rawname, src->rawname); - strcpy(dest->singular, src->singular); - strcpy(dest->plural, src->plural); - strcpy(dest->adjective, src->adjective); - - dest->colorModifierLength = src->ColorModifier.size(); - dest->ColorModifier = (c_colormodifier*)malloc(sizeof(c_colormodifier) * dest->colorModifierLength); + dest = ((*alloc_creaturecaste_callback)(src->rawname, src->singular, src->plural, src->adjective, src->ColorModifier.size(), src->bodypart.size())); for(int i = 0; i < dest->colorModifierLength; i++) ColorListConvert(&src->ColorModifier[i], &dest->ColorModifier[i]); - dest->bodypartLength = src->bodypart.size(); - dest->bodypart = (t_bodypart*)malloc(sizeof(t_bodypart) * dest->bodypartLength); - copy(src->bodypart.begin(), src->bodypart.end(), dest->bodypart); return 1; @@ -200,27 +100,15 @@ int CreatureCasteConvert(t_creaturecaste* src, c_creaturecaste* dest) int CreatureTypeConvert(t_creaturetype* src, c_creaturetype* dest) { - if(src == NULL || dest == NULL) + if(src == NULL) return -1; - strcpy(dest->rawname, src->rawname); - - dest->tilecolor.fore = src->tilecolor.fore; - dest->tilecolor.back = src->tilecolor.back; - dest->tilecolor.bright = src->tilecolor.bright; - - dest->tile_character = src->tile_character; - - dest->castesCount = src->castes.size(); - dest->castes = (c_creaturecaste*)malloc(sizeof(c_creaturecaste) * dest->castesCount); + dest = ((*alloc_creaturetype_callback)(src->rawname, src->castes.size(), src->extract.size(), src->tile_character, src->tilecolor.fore, src->tilecolor.back, src->tilecolor.bright)); for(int i = 0; i < dest->castesCount; i++) CreatureCasteConvert(&src->castes[i], &dest->castes[i]); - dest->extractCount = src->extract.size(); - dest->extract = (t_creatureextract*)malloc(sizeof(t_creatureextract) * dest->extractCount); - copy(src->extract.begin(), src->extract.end(), dest->extract); return 1; -} \ No newline at end of file +} diff --git a/dfhack/include/DFContext_C.h b/dfhack/include/DFContext_C.h new file mode 100644 index 000000000..d1aac9161 --- /dev/null +++ b/dfhack/include/DFContext_C.h @@ -0,0 +1,82 @@ +/* +www.sourceforge.net/projects/dfhack +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 +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. +*/ + +#ifndef DFHACK_C_CONTEXT +#define DFHACK_C_CONTEXT + +#include "Export.h" +#include "integers.h" + +typedef void DFHackObject; + +#ifdef __cplusplus +extern "C" { +#endif + +DFHACK_EXPORT DFHackObject* ContextManager_Alloc(const char* path_to_xml); +DFHACK_EXPORT void ContextManager_Free(DFHackObject* contextMgr); + +DFHACK_EXPORT int ContextManager_Refresh(DFHackObject* contextMgr); +DFHACK_EXPORT int ContextManager_size(DFHackObject* contextMgr, uint32_t* size); +DFHACK_EXPORT int ContextManager_purge(DFHackObject* contextMgr); + +DFHACK_EXPORT DFHackObject* ContextManager_getContext(DFHackObject* contextMgr, uint32_t index); +DFHACK_EXPORT DFHackObject* ContextManager_getSingleContext(DFHackObject* contextMgr); + +DFHACK_EXPORT void Context_Free(DFHackObject* context); + +DFHACK_EXPORT int Context_Attach(DFHackObject* context); +DFHACK_EXPORT int Context_Detach(DFHackObject* context); +DFHACK_EXPORT int Context_isAttached(DFHackObject* context); + +DFHACK_EXPORT int Context_Suspend(DFHackObject* context); +DFHACK_EXPORT int Context_Resume(DFHackObject* context); +DFHACK_EXPORT int Context_isSuspended(DFHackObject* context); +DFHACK_EXPORT int Context_ForceResume(DFHackObject* context); +DFHACK_EXPORT int Context_AsyncSuspend(DFHackObject* context); + +DFHACK_EXPORT DFHackObject* Context_getMemoryInfo(DFHackObject* context); +DFHACK_EXPORT DFHackObject* Context_getProcess(DFHackObject* context); +DFHACK_EXPORT DFHackObject* Context_getWindow(DFHackObject* context); + +DFHACK_EXPORT DFHackObject* Context_getCreatures(DFHackObject* context); +DFHACK_EXPORT DFHackObject* Context_getMaps(DFHackObject* context); +DFHACK_EXPORT DFHackObject* Context_getGui(DFHackObject* context); +DFHACK_EXPORT DFHackObject* Context_getPosition(DFHackObject* context); +DFHACK_EXPORT DFHackObject* Context_getMaterials(DFHackObject* context); +DFHACK_EXPORT DFHackObject* Context_getTranslation(DFHackObject* context); +DFHACK_EXPORT DFHackObject* Context_getVegetation(DFHackObject* context); +DFHACK_EXPORT DFHackObject* Context_getBuildings(DFHackObject* context); +DFHACK_EXPORT DFHackObject* Context_getConstructions(DFHackObject* context); +DFHACK_EXPORT DFHackObject* Context_getItems(DFHackObject* context); + +//these are DANGEROUS...can crash/segfault DF, turn the seas to blood, call up the Antichrist, etc +DFHACK_EXPORT void Context_ReadRaw(DFHackObject* context, const uint32_t offset, const uint32_t size, uint8_t* target); +DFHACK_EXPORT void Context_WriteRaw(DFHackObject* context, const uint32_t offset, const uint32_t size, uint8_t* source); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/dfhack/include/DFTypes_C.h b/dfhack/include/DFTypes_C.h index db078de44..6052555b0 100644 --- a/dfhack/include/DFTypes_C.h +++ b/dfhack/include/DFTypes_C.h @@ -29,7 +29,6 @@ distribution. #include "integers.h" #include "DFTypes.h" #include "modules/Materials.h" -#include "DFHackAPI_C.h" using namespace DFHack; @@ -37,6 +36,20 @@ using namespace DFHack; extern "C" { #endif +DFHACK_EXPORT extern int8_t* (*alloc_byte_buffer_callback)(uint32_t); +DFHACK_EXPORT extern int16_t* (*alloc_short_buffer_callback)(uint32_t); +DFHACK_EXPORT extern int32_t* (*alloc_int_buffer_callback)(uint32_t); + +DFHACK_EXPORT extern uint8_t* (*alloc_ubyte_buffer_callback)(uint32_t); +DFHACK_EXPORT extern uint16_t* (*alloc_ushort_buffer_callback)(uint32_t); +DFHACK_EXPORT extern uint32_t* (*alloc_uint_buffer_callback)(uint32_t); + +DFHACK_EXPORT extern char* (*alloc_char_buffer_callback)(uint32_t); + +DFHACK_EXPORT extern t_matgloss* (*alloc_matgloss_buffer_callback)(int); +DFHACK_EXPORT extern t_descriptor_color* (*alloc_descriptor_buffer_callback)(int); +DFHACK_EXPORT extern t_matglossOther* (*alloc_matgloss_other_buffer_callback)(int); + struct c_colormodifier { char part[128]; @@ -44,8 +57,9 @@ struct c_colormodifier uint32_t colorlistLength; }; -c_colormodifier* ColorModifier_New(); -void ColorModifier_Free(c_colormodifier* src); +DFHACK_EXPORT extern c_colormodifier* (*alloc_empty_colormodifier_callback)(void); +DFHACK_EXPORT extern c_colormodifier* (*alloc_colormodifier_callback)(const char*, uint32_t); +DFHACK_EXPORT extern c_colormodifier* (*alloc_colormodifier_buffer_callback)(uint32_t); struct c_creaturecaste { @@ -61,8 +75,9 @@ struct c_creaturecaste uint32_t bodypartLength; }; -c_creaturecaste* CreatureCaste_New(); -void CreatureCaste_Free(c_creaturecaste* src); +DFHACK_EXPORT extern c_creaturecaste* (*alloc_empty_creaturecaste_callback)(void); +DFHACK_EXPORT extern c_creaturecaste* (*alloc_creaturecaste_callback)(const char*, const char*, const char*, const char*, uint32_t, uint32_t); +DFHACK_EXPORT extern c_creaturecaste* (*alloc_creaturecaste_buffer_callback)(uint32_t); struct c_creaturetype { @@ -84,8 +99,9 @@ struct c_creaturetype } tilecolor; }; -c_creaturetype* CreatureType_New(); -void CreatureType_Free(c_creaturetype* src); +DFHACK_EXPORT extern c_creaturetype* (*alloc_empty_creaturetype_callback)(void); +DFHACK_EXPORT extern c_creaturetype* (*alloc_creaturetype_callback)(const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t); +DFHACK_EXPORT extern c_creaturetype* (*alloc_creaturetype_buffer_callback)(uint32_t); #ifdef __cplusplus } diff --git a/dfhack/include/modules/Items_C.h b/dfhack/include/modules/Items_C.h index 201e7142a..da84b19a5 100644 --- a/dfhack/include/modules/Items_C.h +++ b/dfhack/include/modules/Items_C.h @@ -27,18 +27,20 @@ distribution. #include "Export.h" #include "integers.h" +#include "DFCommonInternal.h" + +using namespace DFHack; + #include "DFTypes.h" #include "modules/Items.h" #include "DFHackAPI_C.h" -using namespace DFHack; - #ifdef __cplusplus extern "C" { #endif -DFHACK_EXPORT char* Items_getItemDescription(DFHackObject* items, uint32_t itemptr, DFHackObject* mats, char* (*char_buffer_create)(int)); -DFHACK_EXPORT char* Items_getItemClass(DFHackObject* items, int32_t index, char* (*char_buffer_create)(int)); +DFHACK_EXPORT char* Items_getItemDescription(DFHackObject* items, uint32_t itemptr, DFHackObject* mats); +DFHACK_EXPORT char* Items_getItemClass(DFHackObject* items, int32_t index); DFHACK_EXPORT int Items_getItemData(DFHackObject* items, uint32_t itemptr, t_item* item); #ifdef __cplusplus diff --git a/dfhack/include/modules/Materials_C.h b/dfhack/include/modules/Materials_C.h index 448f045de..03ebbf532 100644 --- a/dfhack/include/modules/Materials_C.h +++ b/dfhack/include/modules/Materials_C.h @@ -38,10 +38,6 @@ using namespace DFHack; extern "C" { #endif -typedef t_matgloss* (*MatglossBufferFunc)(int); -typedef t_descriptor_color* (*DescriptorColorBufferFunc)(int); -typedef t_matglossOther* (*MatglossOtherBufferFunc)(int); - DFHACK_EXPORT int Materials_ReadInorganicMaterials(DFHackObject* mat); DFHACK_EXPORT int Materials_ReadOrganicMaterials(DFHackObject* mat); DFHACK_EXPORT int Materials_ReadWoodMaterials(DFHackObject* mat); @@ -64,20 +60,16 @@ DFHACK_EXPORT int Materials_getRaceExSize(DFHackObject* mat); DFHACK_EXPORT int Materials_getColorSize(DFHackObject* mat); DFHACK_EXPORT int Materials_getOtherSize(DFHackObject* mat); -DFHACK_EXPORT int Materials_getInorganic(DFHackObject* mat, MatglossBufferFunc callback); -DFHACK_EXPORT int Materials_getOrganic(DFHackObject* mat, MatglossBufferFunc callback); -DFHACK_EXPORT int Materials_getTree(DFHackObject* mat, MatglossBufferFunc callback); -DFHACK_EXPORT int Materials_getPlant(DFHackObject* mat, MatglossBufferFunc callback); -DFHACK_EXPORT int Materials_getRace(DFHackObject* mat, MatglossBufferFunc callback); +DFHACK_EXPORT t_matgloss* Materials_getInorganic(DFHackObject* mat); +DFHACK_EXPORT t_matgloss* Materials_getOrganic(DFHackObject* mat); +DFHACK_EXPORT t_matgloss* Materials_getTree(DFHackObject* mat); +DFHACK_EXPORT t_matgloss* Materials_getPlant(DFHackObject* mat); +DFHACK_EXPORT t_matgloss* Materials_getRace(DFHackObject* mat); -/*doomchild: - I haven't done getRaceEx yet, because I'm not sure about the best way to make the t_creaturetype struct - accessible from C. -*/ -//DFHACK_EXPORT int Materials_getRaceEx(DFHackObject* mat, c_creaturetype* (*c_creaturetype_buffer_create)(c_creaturetype_descriptor*, int)); +DFHACK_EXPORT c_creaturetype* Materials_getRaceEx(DFHackObject* mat); -DFHACK_EXPORT int Materials_getColor(DFHackObject* mat, DescriptorColorBufferFunc callback); -DFHACK_EXPORT int Materials_getOther(DFHackObject* mat, MatglossOtherBufferFunc callback); +DFHACK_EXPORT t_descriptor_color* Materials_getColor(DFHackObject* mat); +DFHACK_EXPORT t_matglossOther* Materials_getOther(DFHackObject* mat); #ifdef __cplusplus } diff --git a/dfhack/modules/Items_C.cpp b/dfhack/modules/Items_C.cpp index a28b3f86a..ee652af13 100644 --- a/dfhack/modules/Items_C.cpp +++ b/dfhack/modules/Items_C.cpp @@ -22,25 +22,27 @@ must not be misrepresented as being the original software. distribution. */ +#include "Export.h" #include "integers.h" -#include +#include +#include +#include "DFTypes.h" using namespace std; +using namespace DFHack; -#include "DFCommonInternal.h" -#include "DFTypes.h" -#include "DFHackAPI.h" +#include "DFProcess.h" #include "modules/Materials.h" #include "modules/Items.h" +#include "DFTypes_C.h" #include "modules/Items_C.h" -using namespace DFHack; #ifdef __cplusplus extern "C" { #endif -char* Items_getItemDescription(DFHackObject* items, uint32_t itemptr, DFHackObject* mats, char* (*char_buffer_create)(int)) +char* Items_getItemDescription(DFHackObject* items, uint32_t itemptr, DFHackObject* mats) { if(items != NULL && mats != NULL) { @@ -48,7 +50,7 @@ char* Items_getItemDescription(DFHackObject* items, uint32_t itemptr, DFHackObje if(desc.size() > 0) { - char* buf = (*char_buffer_create)(desc.size()); + char* buf = (*alloc_char_buffer_callback)(desc.size()); if(buf != NULL) { @@ -67,7 +69,7 @@ char* Items_getItemDescription(DFHackObject* items, uint32_t itemptr, DFHackObje return NULL; } -char* Items_getItemClass(DFHackObject* items, int32_t index, char* (*char_buffer_create)(int)) +char* Items_getItemClass(DFHackObject* items, int32_t index) { if(items != NULL) { @@ -75,7 +77,7 @@ char* Items_getItemClass(DFHackObject* items, int32_t index, char* (*char_buffer if(iclass.size() > 0) { - char* buf = (*char_buffer_create)(iclass.size()); + char* buf = (*alloc_char_buffer_callback)(iclass.size()); if(buf != NULL) { diff --git a/dfhack/modules/Materials_C.cpp b/dfhack/modules/Materials_C.cpp index a78a8dab4..12f2adc5e 100644 --- a/dfhack/modules/Materials_C.cpp +++ b/dfhack/modules/Materials_C.cpp @@ -225,7 +225,7 @@ int Materials_getOtherSize(DFHackObject* mat) //vector getters -int Materials_getInorganic(DFHackObject* mat, MatglossBufferFunc callback) +t_matgloss* Materials_getInorganic(DFHackObject* mat) { if(mat != NULL) { @@ -233,25 +233,21 @@ int Materials_getInorganic(DFHackObject* mat, MatglossBufferFunc callback) if(materials->inorganic.size() > 0) { - t_matgloss* buf = ((*callback)(materials->inorganic.size())); + t_matgloss* buf = ((*alloc_matgloss_buffer_callback)(materials->inorganic.size())); if(buf != NULL) { copy(materials->inorganic.begin(), materials->inorganic.end(), buf); - return 1; + return buf; } - else - return -1; } - else - return 0; } - return -1; + return NULL; } -int Materials_getOrganic(DFHackObject* mat, MatglossBufferFunc callback) +t_matgloss* Materials_getOrganic(DFHackObject* mat) { if(mat != NULL) { @@ -259,25 +255,21 @@ int Materials_getOrganic(DFHackObject* mat, MatglossBufferFunc callback) if(materials->organic.size() > 0) { - t_matgloss* buf = ((*callback)(materials->organic.size())); + t_matgloss* buf = ((*alloc_matgloss_buffer_callback)(materials->organic.size())); if(buf != NULL) { copy(materials->organic.begin(), materials->organic.end(), buf); - return 1; + return buf; } - else - return -1; } - else - return 0; } - return -1; + return NULL; } -int Materials_getTree(DFHackObject* mat, MatglossBufferFunc callback) +t_matgloss* Materials_getTree(DFHackObject* mat) { if(mat != NULL) { @@ -285,25 +277,21 @@ int Materials_getTree(DFHackObject* mat, MatglossBufferFunc callback) if(materials->tree.size() > 0) { - t_matgloss* buf = ((*callback)(materials->tree.size())); + t_matgloss* buf = ((*alloc_matgloss_buffer_callback)(materials->tree.size())); if(buf != NULL) { copy(materials->tree.begin(), materials->tree.end(), buf); - return 1; + return buf; } - else - return -1; } - else - return 0; } - return -1; + return NULL; } -int Materials_getPlant(DFHackObject* mat, MatglossBufferFunc callback) +t_matgloss* Materials_getPlant(DFHackObject* mat) { if(mat != NULL) { @@ -311,25 +299,21 @@ int Materials_getPlant(DFHackObject* mat, MatglossBufferFunc callback) if(materials->plant.size() > 0) { - t_matgloss* buf = ((*callback)(materials->plant.size())); + t_matgloss* buf = ((*alloc_matgloss_buffer_callback)(materials->plant.size())); if(buf != NULL) { copy(materials->plant.begin(), materials->plant.end(), buf); - return 1; + return buf; } - else - return -1; } - else - return 0; } - return -1; + return NULL; } -int Materials_getRace(DFHackObject* mat, MatglossBufferFunc callback) +t_matgloss* Materials_getRace(DFHackObject* mat) { if(mat != NULL) { @@ -337,67 +321,46 @@ int Materials_getRace(DFHackObject* mat, MatglossBufferFunc callback) if(materials->race.size() > 0) { - t_matgloss* buf = ((*callback)(materials->race.size())); + t_matgloss* buf = ((*alloc_matgloss_buffer_callback)(materials->race.size())); if(buf != NULL) { copy(materials->race.begin(), materials->race.end(), buf); - return 1; + return buf; } - else - return -1; } - else - return 0; } - return -1; + return NULL; } //race_ex getter goes here... -// int Materials_getRaceEx(DFHackObject* mat, c_creaturetype* (*c_creaturetype_buffer_create)(c_creaturetype_descriptor*, int)) -// { - // if(mat != NULL) - // { - // DFHack::Materials* materials = (DFHack::Materials*)mat; +c_creaturetype* Materials_getRaceEx(DFHackObject* mat) +{ + if(mat != NULL) + { + DFHack::Materials* materials = (DFHack::Materials*)mat; + int matSize = materials->raceEx.size(); - // if(materials->raceEx.size() > 0) - // { - // std::vector types = materials->raceEx; - // int typessize = types.size(); - - // c_creaturetype_descriptor* descriptors = (c_creaturetype_descriptor*)malloc(sizeof(c_creaturetype_descriptor) * typessize); - - // for(int i = 0; i < typessize; i++) - // { - // descriptors[i].castesCount = types[i].castes.size(); - // descriptors[i].extractCount = types[i].extract.size(); - // } - - // c_creaturetype* buf = ((*c_creaturetype_buffer_create)(descriptors, typessize)); + if(matSize > 0) + { + c_creaturetype* buf = ((*alloc_creaturetype_buffer_callback)(matSize)); - // for(int i = 0; i < typessize; i++) - // { - // t_creaturetype current = types[i]; - - // strncpy(buf[i].rawname, current.rawname, 128); - // buf[i].rawname[127] = '\0'; - - // buf[i].tile_character = current.tile_character; - // buf[i].tilecolor = current.tilecolor; + if(buf != NULL) + { + for(int i = 0; i < matSize; i++) + CreatureTypeConvert(&materials->raceEx[i], &buf[i]); - // current.extract.copy(buf[i].extract, current.extract.size()); - // } - - // free(descriptors); - // } - // } + return buf; + } + } + } - // return -1; -// } + return NULL; +} -int Materials_getColor(DFHackObject* mat, DescriptorColorBufferFunc callback) +t_descriptor_color* Materials_getColor(DFHackObject* mat) { if(mat != NULL) { @@ -405,25 +368,21 @@ int Materials_getColor(DFHackObject* mat, DescriptorColorBufferFunc callback) if(materials->color.size() > 0) { - t_descriptor_color* buf = ((*callback)(materials->color.size())); + t_descriptor_color* buf = ((*alloc_descriptor_buffer_callback)(materials->color.size())); if(buf != NULL) { copy(materials->color.begin(), materials->color.end(), buf); - return 1; + return buf; } - else - return -1; } - else - return 0; } - return -1; + return NULL; } -int Materials_getOther(DFHackObject* mat, MatglossOtherBufferFunc callback) +t_matglossOther* Materials_getOther(DFHackObject* mat) { if(mat != NULL) { @@ -431,22 +390,18 @@ int Materials_getOther(DFHackObject* mat, MatglossOtherBufferFunc callback) if(materials->other.size() > 0) { - t_matglossOther* buf = ((*callback)(materials->other.size())); + t_matglossOther* buf = ((*alloc_matgloss_other_buffer_callback)(materials->other.size())); if(buf != NULL) { copy(materials->other.begin(), materials->other.end(), buf); - return 1; + return buf; } - else - return -1; } - else - return 0; } - return -1; + return NULL; } #ifdef __cplusplus diff --git a/dfhack/python/c api/context.py b/dfhack/python/c api/context.py new file mode 100644 index 000000000..592359f57 --- /dev/null +++ b/dfhack/python/c api/context.py @@ -0,0 +1,195 @@ +from ctypes import * +from pydftypes import * + +libdfhack.ContextManager_Alloc.restype = c_void_p +libdfhack.ContextManager_Free.argtypes = [ c_void_p ] + +libdfhack.ContextManager_getContext.restype = c_void_p +libdfhack.ContextManager_getSingleContext.restype = c_void_p + +libdfhack.Context_Free.argtypes = [ c_void_p ] + +libdfhack.Context_getMemoryInfo.restype = c_void_p +libdfhack.Context_getProcess.restype = c_void_p +libdfhack.Context_getWindow.restype = c_void_p + +libdfhack.Context_getCreatures.restype = c_void_p +libdfhack.Context_getMaps.restype = c_void_p +libdfhack.Context_getGui.restype = c_void_p +libdfhack.Context_getPosition.restype = c_void_p +libdfhack.Context_getMaterials.restype = c_void_p +libdfhack.Context_getTranslation.restype = c_void_p +libdfhack.Context_getVegetation.restype = c_void_p +libdfhack.Context_getBuildings.restype = c_void_p +libdfhack.Context_getConstructions.restype = c_void_p +libdfhack.Context_getItems.restype = c_void_p + +class ContextManager(object): + def __init__(self, memory_path): + self._cm_ptr = libdfhack.ContextManager_Alloc(create_string_buffer(memory_path)) + + def __del__(self): + libdfhack.ContextManager_Free(self._cm_ptr) + + def refresh(self): + return libdfhack.ContextManager_Refresh(self._cm_ptr) > 0 + + def purge(self): + libdfhack.ContextManager_purge(self._cm_ptr) + + def get_context(self, index): + p = libdfhack.ContextManager_getContext(self._cm_ptr, index) + + if p: + return Context(p) + else: + return None + + def get_single_context(self): + p = libdfhack.ContextManager_getSingleContext(self._cm_ptr) + + if p: + return Context(p) + else: + return None + +class Context(object): + def __init__(self, ptr): + self._c_ptr = ptr + + self._pos_obj = None + self._mat_obj = None + self._map_obj = None + self._veg_obj = None + self._build_obj = None + self._con_obj = None + self._gui_obj = None + self._tran_obj = None + self._item_obj = None + self._creature_obj = None + + def __del__(self): + libdfhack.Context_Free(self._c_ptr) + + def attach(self): + return libdfhack.Context_Attach(self._c_ptr) > 0 + + def detach(self): + return libdfhack.Context_Detach(self._c_ptr) > 0 + + def suspend(self): + return libdfhack.Context_Suspend(self._c_ptr) > 0 + + def resume(self): + return libdfhack.Context_Resume(self._c_ptr) > 0 + + def force_resume(self): + return libdfhack.Context_ForceResume(self._c_ptr) > 0 + + def async_suspend(self): + return libdfhack.Context_AsyncSuspend(self._c_ptr) > 0 + + @property + def is_attached(self): + return libdfhack.Context_isAttached(self._c_ptr) > 0 + + @property + def is_suspended(self): + return libdfhack.Context_isSuspended(self._c_ptr) > 0 + + @property + def position(self): + import position + if self._pos_obj is None: + self._pos_obj = position.Position(libdfhack.Context_getPosition(self._c_ptr)) + + return self._pos_obj + + @property + def materials(self): + import materials + if self._mat_obj is None: + self._mat_obj = materials.Materials(libdfhack.Context_getMaterials(self._c_ptr)) + + return self._mat_obj + + @property + def maps(self): + import maps + if self._map_obj is None: + self._map_obj = maps.Maps(libdfhack.Context_getMaps(self._c_ptr)) + + return self._map_obj + + @property + def vegetation(self): + import vegetation + if self._veg_obj is None: + self._veg_obj = vegetation.Vegetation(libdfhack.Context_getVegetation(self._c_ptr)) + + return self._veg_obj + + @property + def buildings(self): + import buildings + if self._build_obj is None: + self._build_obj = buildings.Buildings(libdfhack.Context_getBuildings(self._c_ptr)) + + return self._build_obj + + @property + def creatures(self): + import creatures + if self._creature_obj is None: + self._creature_obj = creatures.Creatures(libdfhack.Context_getCreatures(self._c_ptr)) + + return self._creature_obj + + @property + def gui(self): + import gui + if self._gui_obj is None: + self._gui_obj = gui.Gui(libdfhack.Context_getGui(self._c_ptr)) + + return self._gui_obj + + @property + def items(self): + import items + if self._item_obj is None: + self._item_obj = items.Items(libdfhack.Context_getItems(self._c_ptr)) + + return self._item_obj + + @property + def translation(self): + import translation + if self._tran_obj is None: + self._tran_obj = translation.Translation(libdfhack.Context_getTranslation(self._c_ptr)) + + return self._tran_obj + +def reveal(): + df = API("Memory.xml") + df.attach() + + m = df.maps + + m.start() + + m_x, m_y, m_z = m.size + + for x in xrange(m_x): + for y in xrange(m_y): + for z in xrange(m_z): + if m.is_valid_block(x, y, z): + d = m.read_designations(x, y, z) + + for i in d: + for j in i: + j.bits.hidden = 0 + + m.write_designations(x, y, z, d) + + m.finish() + df.detach() diff --git a/dfhack/python/c api/pydftypes.py b/dfhack/python/c api/pydftypes.py index 479dafe1a..78a90e57f 100644 --- a/dfhack/python/c api/pydftypes.py +++ b/dfhack/python/c api/pydftypes.py @@ -1,10 +1,18 @@ from ctypes import * -from collections import namedtuple from pydfhackflags import * from enum import * +from util import * libdfhack = cdll.libdfhack +libdfhack.alloc_byte_buffer_callback = alloc_byte_buffer +libdfhack.alloc_ubyte_buffer_callback = alloc_ubyte_buffer +libdfhack.alloc_short_buffer_callback = alloc_short_buffer +libdfhack.alloc_ushort_buffer_callback = alloc_ushort_buffer +libdfhack.alloc_int_buffer_callback = alloc_int_buffer +libdfhack.alloc_uint_buffer_callback = alloc_uint_buffer +libdfhack.alloc_char_buffer_callback = alloc_char_buffer + int_ptr = POINTER(c_int) uint_ptr = POINTER(c_uint) @@ -79,6 +87,13 @@ class Matgloss(Structure): ("bright", c_byte), ("name", c_char * 128)] +def _alloc_matgloss_buffer_callback(count): + allocated = _allocate_array(Matgloss, count) + + return allocated[1] + +libdfhack.alloc_matgloss_buffer_callback = CFUNCTYPE(POINTER(Matgloss), c_int)(_alloc_matgloss_buffer_callback) + class MatglossPair(Structure): _fields_ = [("type", c_short), ("index", c_int)] @@ -90,9 +105,23 @@ class DescriptorColor(Structure): ("b", c_float), ("name", c_char * 128)] +def _alloc_descriptor_buffer_callback(count): + allocated = _allocate_array(DescriptorColor, count) + + return allocated[1] + +libdfhack.alloc_descriptor_buffer_callback = CFUNCTYPE(POINTER(DescriptorColor), c_int)(_alloc_descriptor_buffer_callback) + class MatglossOther(Structure): _fields_ = [("rawname", c_char * 128)] +def _alloc_matgloss_other_buffer_callback(count): + allocated = _allocate_array(MatglossOther, count) + + return allocated[1] + +libdfhack.alloc_matgloss_other_buffer_callback = CFUNCTYPE(POINTER(MatglossOther), c_int)(_alloc_matgloss_other_buffer_callback) + class Building(Structure): _fields_ = [("origin", c_uint), ("vtable", c_uint), @@ -256,3 +285,19 @@ class BodyPart(Structure): ("category", (c_char * 128)), ("single", (c_char * 128)), ("plural", (c_char * 128))] + +class ColorModifier(Structure): + _fields_ = [("part", (c_char * 128)), + ("colorlist", POINTER(c_uint)), + ("colorlistLength", c_uint)] + + def __init__(self): + self.part[0] = '\0' + self.colorlistLength = 0 + +ColorModifierPtr = POINTER(ColorModifier) + +def _alloc_empty_colormodifier_callback(): + return ColorModifierPtr(ColorModifier()) + +libdfhack.alloc_empty_colormodifier_callback = CFUNCTYPE(ColorModifierPtr)(_alloc_empty_colormodifier_callback) diff --git a/dfhack/python/c api/util.py b/dfhack/python/c api/util.py index d81b01ede..4f3f06958 100644 --- a/dfhack/python/c api/util.py +++ b/dfhack/python/c api/util.py @@ -12,3 +12,55 @@ def _allocate_array(t_type, count): ptr = addressof(arr) return (arr, ptr) + +def _alloc_int_buffer(count): + a = _allocate_array(c_int, count) + + return a[1] + +alloc_int_buffer = CFUNCTYPE(POINTER(c_int), c_uint)(_alloc_int_buffer) + +def _alloc_uint_buffer(count): + a = _allocate_array(c_uint, count) + + return a[1] + +alloc_uint_buffer = CFUNCTYPE(POINTER(c_uint), c_uint)(_alloc_uint_buffer) + +def _alloc_short_buffer(count): + a = _allocate_array(c_short, count) + + return a[1] + +alloc_short_buffer = CFUNCTYPE(POINTER(c_short), c_uint)(_alloc_short_buffer) + +def _alloc_ushort_buffer(count): + a = _allocate_array(c_ushort, count) + + return a[1] + +alloc_ushort_buffer = CFUNCTYPE(POINTER(c_ushort), c_uint)(_alloc_ushort_buffer) + +def _alloc_byte_buffer(count): + a = _allocate_array(c_byte, count) + + return a[1] + +alloc_byte_buffer = CFUNCTYPE(POINTER(c_byte), c_uint)(_alloc_byte_buffer) + +def _alloc_ubyte_buffer(count): + a = _allocate_array(c_ubyte, count) + + return a[1] + +alloc_ubyte_buffer = CFUNCTYPE(POINTER(c_ubyte), c_uint)(_alloc_ubyte_buffer) + +def _alloc_char_buffer(count): + c = create_string_buffer(count) + + ptr = c_void_p() + ptr = addressof(c) + + return ptr + +alloc_char_buffer = CFUNCTYPE(POINTER(c_char), c_uint)(_alloc_char_buffer)