From 187a8a0578d429232e6a24ff1ec9799634f1aece Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 9 Mar 2011 12:21:06 -0600 Subject: [PATCH 01/13] added a function to read all the veins at once updated vein reading functions to use the allocator callbacks correctly --- library/include/dfhack-c/modules/Maps_C.h | 11 ++ library/modules/Maps_C.cpp | 131 ++++++++++++++++++++-- 2 files changed, 131 insertions(+), 11 deletions(-) diff --git a/library/include/dfhack-c/modules/Maps_C.h b/library/include/dfhack-c/modules/Maps_C.h index 088e530f6..78db697cb 100644 --- a/library/include/dfhack-c/modules/Maps_C.h +++ b/library/include/dfhack-c/modules/Maps_C.h @@ -79,6 +79,17 @@ DFHACK_EXPORT t_spattervein* Maps_ReadSpatterVeins(DFHackObject* maps, uint32_t DFHACK_EXPORT t_grassvein* Maps_ReadGrassVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z); DFHACK_EXPORT t_worldconstruction* Maps_ReadWorldConstructions(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z); +typedef struct +{ + t_vein* veins; + t_frozenliquidvein* frozen_veins; + t_spattervein* spatter_veins; + t_grassvein* grass_veins; + t_worldconstruction* world_constructions; +} c_allveins; + +DFHACK_EXPORT int Maps_ReadAllVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, c_allveins* vein_struct); + #ifdef __cplusplus } #endif diff --git a/library/modules/Maps_C.cpp b/library/modules/Maps_C.cpp index b1ded2dcc..059f69503 100644 --- a/library/modules/Maps_C.cpp +++ b/library/modules/Maps_C.cpp @@ -63,7 +63,7 @@ uint16_t* Maps_ReadGeology(DFHackObject* maps) if(((DFHack::Maps*)maps)->ReadGeology(geology)) { - uint16_t* buf = NULL; + uint16_t** buf = NULL; uint32_t geoLength = 0; for(unsigned int i = 0; i < geology.size(); i++) @@ -78,7 +78,7 @@ uint16_t* Maps_ReadGeology(DFHackObject* maps) if(buf != NULL) { - uint16_t* bufCopyPtr = buf; + uint16_t* bufCopyPtr = *buf; for(unsigned int i = 0; i < geology.size(); i++) { @@ -87,7 +87,7 @@ uint16_t* Maps_ReadGeology(DFHackObject* maps) bufCopyPtr += geology[i].size(); } - return buf; + return *buf; } else return NULL; @@ -108,15 +108,15 @@ t_feature* Maps_ReadGlobalFeatures(DFHackObject* maps) if(featureVec.size() <= 0) return NULL; - t_feature* buf = NULL; + t_feature** buf = NULL; (*alloc_t_feature_buffer_callback)(buf, featureVec.size()); if(buf != NULL) { - copy(featureVec.begin(), featureVec.end(), buf); + copy(featureVec.begin(), featureVec.end(), *buf); - return buf; + return *buf; } else return NULL; @@ -368,7 +368,10 @@ t_vein* Maps_ReadStandardVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint3 if(veins.size() > 0) { - ((*alloc_vein_buffer_callback)(v_buf, veins.size())); + ((*alloc_vein_buffer_callback)(&v_buf, veins.size())); + + if(v_buf == NULL) + return NULL; copy(veins.begin(), veins.end(), v_buf); } @@ -399,7 +402,10 @@ t_frozenliquidvein* Maps_ReadFrozenVeins(DFHackObject* maps, uint32_t x, uint32_ if(frozen_veins.size() > 0) { - ((*alloc_frozenliquidvein_buffer_callback)(fv_buf, frozen_veins.size())); + ((*alloc_frozenliquidvein_buffer_callback)(&fv_buf, frozen_veins.size())); + + if(fv_buf == NULL) + return NULL; copy(frozen_veins.begin(), frozen_veins.end(), fv_buf); } @@ -430,7 +436,10 @@ t_spattervein* Maps_ReadSpatterVeins(DFHackObject* maps, uint32_t x, uint32_t y, if(spatter_veins.size() > 0) { - ((*alloc_spattervein_buffer_callback)(sv_buf, spatter_veins.size())); + ((*alloc_spattervein_buffer_callback)(&sv_buf, spatter_veins.size())); + + if(sv_buf == NULL) + return NULL; copy(spatter_veins.begin(), spatter_veins.end(), sv_buf); } @@ -461,7 +470,10 @@ t_grassvein* Maps_ReadGrassVeins(DFHackObject* maps, uint32_t x, uint32_t y, uin if(grass_veins.size() > 0) { - ((*alloc_grassvein_buffer_callback)(gs_buf, grass_veins.size())); + ((*alloc_grassvein_buffer_callback)(&gs_buf, grass_veins.size())); + + if(gs_buf == NULL) + return NULL; copy(grass_veins.begin(), grass_veins.end(), gs_buf); } @@ -492,7 +504,10 @@ t_worldconstruction* Maps_ReadWorldConstructions(DFHackObject* maps, uint32_t x, if(constructions.size() > 0) { - ((*alloc_worldconstruction_buffer_callback)(ct_buf, constructions.size())); + ((*alloc_worldconstruction_buffer_callback)(&ct_buf, constructions.size())); + + if(ct_buf == NULL) + return NULL; copy(constructions.begin(), constructions.end(), ct_buf); } @@ -506,6 +521,100 @@ t_worldconstruction* Maps_ReadWorldConstructions(DFHackObject* maps, uint32_t x, return NULL; } +int Maps_ReadAllVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, c_allveins* vein_struct) +{ + if(maps != NULL) + { + if(alloc_vein_buffer_callback == NULL || alloc_frozenliquidvein_buffer_callback == NULL || + alloc_spattervein_buffer_callback == NULL || alloc_grassvein_buffer_callback == NULL || + alloc_worldconstruction_buffer_callback == NULL) + return -1; + + vector veins; + vector frozen_veins; + vector spatter_veins; + vector grass_veins; + vector constructions; + + bool result = ((DFHack::Maps*)maps)->ReadVeins(x, y, z, &veins, &frozen_veins, &spatter_veins, &grass_veins, &constructions); + + if(result) + { + t_vein* v_buf = NULL; + t_frozenliquidvein* fv_buf = NULL; + t_spattervein* sv_buf = NULL; + t_grassvein* gs_buf = NULL; + t_worldconstruction* ct_buf = NULL; + + if(veins.size() > 0) + { + ((*alloc_vein_buffer_callback)(&v_buf, veins.size())); + + if(v_buf == NULL) + return -1; + + copy(veins.begin(), veins.end(), v_buf); + + vein_struct->veins = v_buf; + } + + if(frozen_veins.size() > 0) + { + ((*alloc_frozenliquidvein_buffer_callback)(&fv_buf, frozen_veins.size())); + + if(fv_buf == NULL) + return -1; + + copy(frozen_veins.begin(), frozen_veins.end(), fv_buf); + + vein_struct->frozen_veins = fv_buf; + } + + if(spatter_veins.size() > 0) + { + ((*alloc_spattervein_buffer_callback)(&sv_buf, spatter_veins.size())); + + if(sv_buf == NULL) + return -1; + + copy(spatter_veins.begin(), spatter_veins.end(), sv_buf); + + vein_struct->spatter_veins = sv_buf; + } + + if(grass_veins.size() > 0) + { + ((*alloc_grassvein_buffer_callback)(&gs_buf, grass_veins.size())); + + if(gs_buf == NULL) + return NULL; + + copy(grass_veins.begin(), grass_veins.end(), gs_buf); + + vein_struct->grass_veins = gs_buf; + } + + if(constructions.size() > 0) + { + ((*alloc_worldconstruction_buffer_callback)(&ct_buf, constructions.size())); + + if(ct_buf == NULL) + return NULL; + + copy(constructions.begin(), constructions.end(), ct_buf); + + vein_struct->world_constructions = ct_buf; + } + + return 1; + } + else + return 0; + } + + return -1; +} + #ifdef __cplusplus } #endif From 44cbc3fd5ce1e665ea1494f70b60015c644bb231 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 9 Mar 2011 12:22:48 -0600 Subject: [PATCH 02/13] fixed allocator callbacks to take pointers to pointers so they'll actually, you know, WORK --- library/DFTypes_C.cpp | 144 +++++++++++++-------------- library/include/dfhack-c/DFTypes_C.h | 124 +++++++++++------------ 2 files changed, 134 insertions(+), 134 deletions(-) diff --git a/library/DFTypes_C.cpp b/library/DFTypes_C.cpp index 4bd1a676b..ce2a00448 100644 --- a/library/DFTypes_C.cpp +++ b/library/DFTypes_C.cpp @@ -68,60 +68,60 @@ void UnregisterByteBufferCallback() extern "C" { #endif -int (*alloc_byte_buffer_callback)(int8_t*, uint32_t) = NULL; -int (*alloc_short_buffer_callback)(int16_t*, uint32_t) = NULL; -int (*alloc_int_buffer_callback)(int32_t*, uint32_t) = NULL; - -int (*alloc_ubyte_buffer_callback)(uint8_t*, uint32_t) = NULL; -int (*alloc_ushort_buffer_callback)(uint16_t*, uint32_t) = NULL; -int (*alloc_uint_buffer_callback)(uint32_t*, uint32_t) = NULL; - -int (*alloc_char_buffer_callback)(char*, uint32_t) = NULL; - -int (*alloc_matgloss_buffer_callback)(t_matgloss*, uint32_t) = NULL; -int (*alloc_descriptor_buffer_callback)(t_descriptor_color*, uint32_t) = NULL; -int (*alloc_matgloss_other_buffer_callback)(t_matglossOther*, uint32_t) = NULL; - -int (*alloc_t_feature_buffer_callback)(t_feature*, uint32_t) = NULL; -int (*alloc_t_hotkey_buffer_callback)(t_hotkey*, uint32_t) = NULL; -int (*alloc_t_screen_buffer_callback)(t_screen*, uint32_t) = NULL; - -int (*alloc_t_customWorkshop_buffer_callback)(t_customWorkshop*, uint32_t) = NULL; -int (*alloc_t_material_buffer_callback)(t_material*, uint32_t) = NULL; - -int (*alloc_empty_colormodifier_callback)(c_colormodifier*) = NULL; -int (*alloc_colormodifier_callback)(c_colormodifier*, const char*, uint32_t) = NULL; -int (*alloc_colormodifier_buffer_callback)(c_colormodifier*, uint32_t) = NULL; - -int (*alloc_empty_creaturecaste_callback)(c_creaturecaste*)= NULL; -int (*alloc_creaturecaste_callback)(c_creaturecaste*, const char*, const char*, const char*, const char*, uint32_t, uint32_t) = NULL; -int (*alloc_creaturecaste_buffer_callback)(c_creaturecaste*, uint32_t) = NULL; - -int (*alloc_empty_creaturetype_callback)(c_creaturetype*) = NULL; -int (*alloc_creaturetype_callback)(c_creaturetype*, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t) = NULL; -int (*alloc_creaturetype_buffer_callback)(c_creaturetype*, uint32_t) = NULL; - -int (*alloc_vein_buffer_callback)(t_vein*, uint32_t) = NULL; -int (*alloc_frozenliquidvein_buffer_callback)(t_frozenliquidvein*, uint32_t) = NULL; -int (*alloc_spattervein_buffer_callback)(t_spattervein*, uint32_t) = NULL; -int (*alloc_grassvein_buffer_callback)(t_grassvein*, uint32_t) = NULL; -int (*alloc_worldconstruction_buffer_callback)(t_worldconstruction*, uint32_t) = NULL; - -REG_MACRO(Byte, int8_t*, alloc_byte_buffer_callback) -REG_MACRO(Short, int16_t*, alloc_short_buffer_callback) -REG_MACRO(Int, int32_t*, alloc_int_buffer_callback) -REG_MACRO(UByte, uint8_t*, alloc_ubyte_buffer_callback) -REG_MACRO(UShort, uint16_t*, alloc_ushort_buffer_callback) -REG_MACRO(UInt, uint32_t*, alloc_uint_buffer_callback) -REG_MACRO(Char, char*, alloc_char_buffer_callback) -REG_MACRO(Matgloss, t_matgloss*, alloc_matgloss_buffer_callback) -REG_MACRO(DescriptorColor, t_descriptor_color*, alloc_descriptor_buffer_callback) -REG_MACRO(MatglossOther, t_matglossOther*, alloc_matgloss_other_buffer_callback) -REG_MACRO(Feature, t_feature*, alloc_t_feature_buffer_callback) -REG_MACRO(Hotkey, t_hotkey*, alloc_t_hotkey_buffer_callback) -REG_MACRO(Screen, t_screen*, alloc_t_screen_buffer_callback) -REG_MACRO(CustomWorkshop, t_customWorkshop*, alloc_t_customWorkshop_buffer_callback) -REG_MACRO(Material, t_material*, alloc_t_material_buffer_callback) +int (*alloc_byte_buffer_callback)(int8_t**, uint32_t) = NULL; +int (*alloc_short_buffer_callback)(int16_t**, uint32_t) = NULL; +int (*alloc_int_buffer_callback)(int32_t**, uint32_t) = NULL; + +int (*alloc_ubyte_buffer_callback)(uint8_t**, uint32_t) = NULL; +int (*alloc_ushort_buffer_callback)(uint16_t**, uint32_t) = NULL; +int (*alloc_uint_buffer_callback)(uint32_t**, uint32_t) = NULL; + +int (*alloc_char_buffer_callback)(char**, uint32_t) = NULL; + +int (*alloc_matgloss_buffer_callback)(t_matgloss**, uint32_t) = NULL; +int (*alloc_descriptor_buffer_callback)(t_descriptor_color**, uint32_t) = NULL; +int (*alloc_matgloss_other_buffer_callback)(t_matglossOther**, uint32_t) = NULL; + +int (*alloc_t_feature_buffer_callback)(t_feature**, uint32_t) = NULL; +int (*alloc_t_hotkey_buffer_callback)(t_hotkey**, uint32_t) = NULL; +int (*alloc_t_screen_buffer_callback)(t_screen**, uint32_t) = NULL; + +int (*alloc_t_customWorkshop_buffer_callback)(t_customWorkshop**, uint32_t) = NULL; +int (*alloc_t_material_buffer_callback)(t_material**, uint32_t) = NULL; + +int (*alloc_empty_colormodifier_callback)(c_colormodifier**) = NULL; +int (*alloc_colormodifier_callback)(c_colormodifier**, const char*, uint32_t) = NULL; +int (*alloc_colormodifier_buffer_callback)(c_colormodifier**, uint32_t) = NULL; + +int (*alloc_empty_creaturecaste_callback)(c_creaturecaste**)= NULL; +int (*alloc_creaturecaste_callback)(c_creaturecaste**, const char*, const char*, const char*, const char*, uint32_t, uint32_t) = NULL; +int (*alloc_creaturecaste_buffer_callback)(c_creaturecaste**, uint32_t) = NULL; + +int (*alloc_empty_creaturetype_callback)(c_creaturetype**) = NULL; +int (*alloc_creaturetype_callback)(c_creaturetype**, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t) = NULL; +int (*alloc_creaturetype_buffer_callback)(c_creaturetype**, uint32_t) = NULL; + +int (*alloc_vein_buffer_callback)(t_vein**, uint32_t) = NULL; +int (*alloc_frozenliquidvein_buffer_callback)(t_frozenliquidvein**, uint32_t) = NULL; +int (*alloc_spattervein_buffer_callback)(t_spattervein**, uint32_t) = NULL; +int (*alloc_grassvein_buffer_callback)(t_grassvein**, uint32_t) = NULL; +int (*alloc_worldconstruction_buffer_callback)(t_worldconstruction**, uint32_t) = NULL; + +REG_MACRO(Byte, int8_t**, alloc_byte_buffer_callback) +REG_MACRO(Short, int16_t**, alloc_short_buffer_callback) +REG_MACRO(Int, int32_t**, alloc_int_buffer_callback) +REG_MACRO(UByte, uint8_t**, alloc_ubyte_buffer_callback) +REG_MACRO(UShort, uint16_t**, alloc_ushort_buffer_callback) +REG_MACRO(UInt, uint32_t**, alloc_uint_buffer_callback) +REG_MACRO(Char, char**, alloc_char_buffer_callback) +REG_MACRO(Matgloss, t_matgloss**, alloc_matgloss_buffer_callback) +REG_MACRO(DescriptorColor, t_descriptor_color**, alloc_descriptor_buffer_callback) +REG_MACRO(MatglossOther, t_matglossOther**, alloc_matgloss_other_buffer_callback) +REG_MACRO(Feature, t_feature**, alloc_t_feature_buffer_callback) +REG_MACRO(Hotkey, t_hotkey**, alloc_t_hotkey_buffer_callback) +REG_MACRO(Screen, t_screen**, alloc_t_screen_buffer_callback) +REG_MACRO(CustomWorkshop, t_customWorkshop**, alloc_t_customWorkshop_buffer_callback) +REG_MACRO(Material, t_material**, alloc_t_material_buffer_callback) UNREG_MACRO(Byte, alloc_byte_buffer_callback) UNREG_MACRO(Short, alloc_short_buffer_callback) @@ -139,19 +139,19 @@ UNREG_MACRO(Screen, alloc_t_screen_buffer_callback) UNREG_MACRO(CustomWorkshop, alloc_t_customWorkshop_buffer_callback) UNREG_MACRO(Material, alloc_t_material_buffer_callback) -void RegisterEmptyColorModifierCallback(int (*funcptr)(c_colormodifier*)) +void RegisterEmptyColorModifierCallback(int (*funcptr)(c_colormodifier**)) { alloc_empty_colormodifier_callback = funcptr; } -void RegisterNewColorModifierCallback(int (*funcptr)(c_colormodifier*, const char*, uint32_t)) +void RegisterNewColorModifierCallback(int (*funcptr)(c_colormodifier**, const char*, uint32_t)) { alloc_colormodifier_callback = funcptr; } -REG_MACRO(ColorModifier, c_colormodifier*, alloc_colormodifier_buffer_callback) +REG_MACRO(ColorModifier, c_colormodifier**, alloc_colormodifier_buffer_callback) -void RegisterEmptyCreatureCasteCallback(int (*funcptr)(c_creaturecaste*)) +void RegisterEmptyCreatureCasteCallback(int (*funcptr)(c_creaturecaste**)) { alloc_empty_creaturecaste_callback = funcptr; } @@ -166,12 +166,12 @@ void UnregisterNewColorModifierCallback() alloc_colormodifier_callback = NULL; } -void RegisterNewCreatureCasteCallback(int (*funcptr)(c_creaturecaste*, const char*, const char*, const char*, const char*, uint32_t, uint32_t)) +void RegisterNewCreatureCasteCallback(int (*funcptr)(c_creaturecaste**, const char*, const char*, const char*, const char*, uint32_t, uint32_t)) { alloc_creaturecaste_callback = funcptr; } -REG_MACRO(CreatureCaste, c_creaturecaste*, alloc_creaturecaste_buffer_callback) +REG_MACRO(CreatureCaste, c_creaturecaste**, alloc_creaturecaste_buffer_callback) UNREG_MACRO(CreatureCaste, alloc_creaturecaste_buffer_callback) void UnregisterEmptyCreatureCasteCallback() @@ -184,17 +184,17 @@ void UnregisterNewCreatureCasteCallback() alloc_creaturecaste_callback = NULL; } -void RegisterEmptyCreatureTypeCallback(int (*funcptr)(c_creaturetype*)) +void RegisterEmptyCreatureTypeCallback(int (*funcptr)(c_creaturetype**)) { alloc_empty_creaturetype_callback = funcptr; } -void RegisterNewCreatureTypeCallback(int (*funcptr)(c_creaturetype*, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t)) +void RegisterNewCreatureTypeCallback(int (*funcptr)(c_creaturetype**, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t)) { alloc_creaturetype_callback = funcptr; } -REG_MACRO(CreatureType, c_creaturetype*, alloc_creaturetype_buffer_callback) +REG_MACRO(CreatureType, c_creaturetype**, alloc_creaturetype_buffer_callback) UNREG_MACRO(CreatureType, alloc_creaturetype_buffer_callback) void UnregisterEmptyCreatureTypeCallback() @@ -207,11 +207,11 @@ void UnregisterNewCreatureTypeCallback() alloc_creaturetype_callback = NULL; } -REG_MACRO(Vein, t_vein*, alloc_vein_buffer_callback) -REG_MACRO(FrozenLiquidVein, t_frozenliquidvein*, alloc_frozenliquidvein_buffer_callback) -REG_MACRO(SpatterVein, t_spattervein*, alloc_spattervein_buffer_callback) -REG_MACRO(GrassVein, t_grassvein*, alloc_grassvein_buffer_callback) -REG_MACRO(WorldConstruction, t_worldconstruction*, alloc_worldconstruction_buffer_callback) +REG_MACRO(Vein, t_vein**, alloc_vein_buffer_callback) +REG_MACRO(FrozenLiquidVein, t_frozenliquidvein**, alloc_frozenliquidvein_buffer_callback) +REG_MACRO(SpatterVein, t_spattervein**, alloc_spattervein_buffer_callback) +REG_MACRO(GrassVein, t_grassvein**, alloc_grassvein_buffer_callback) +REG_MACRO(WorldConstruction, t_worldconstruction**, alloc_worldconstruction_buffer_callback) UNREG_MACRO(Vein, alloc_vein_buffer_callback) UNREG_MACRO(FrozenLiquidVein, alloc_frozenliquidvein_buffer_callback) @@ -268,7 +268,7 @@ int ColorListConvert(t_colormodifier* src, c_colormodifier* dest) if(src == NULL) return -1; - ((*alloc_colormodifier_callback)(dest, src->part, src->colorlist.size())); + ((*alloc_colormodifier_callback)(&dest, src->part, src->colorlist.size())); copy(src->colorlist.begin(), src->colorlist.end(), dest->colorlist); @@ -280,10 +280,10 @@ int CreatureCasteConvert(t_creaturecaste* src, c_creaturecaste* dest) if(src == NULL) return -1; - ((*alloc_creaturecaste_callback)(dest, src->rawname, src->singular, src->plural, src->adjective, src->ColorModifier.size(), src->bodypart.size())); + ((*alloc_creaturecaste_callback)(&dest, src->rawname, src->singular, src->plural, src->adjective, src->colorModifier.size(), src->bodypart.size())); for(unsigned int i = 0; i < dest->colorModifierLength; i++) - ColorListConvert(&src->ColorModifier[i], &dest->ColorModifier[i]); + ColorListConvert(&src->colorModifier[i], &(dest->colorModifier[i])); copy(src->bodypart.begin(), src->bodypart.end(), dest->bodypart); @@ -295,7 +295,7 @@ int CreatureTypeConvert(t_creaturetype* src, c_creaturetype* dest) if(src == NULL) return -1; - ((*alloc_creaturetype_callback)(dest, src->rawname, src->castes.size(), src->extract.size(), src->tile_character, src->tilecolor.fore, src->tilecolor.back, src->tilecolor.bright)); + ((*alloc_creaturetype_callback)(&dest, src->rawname, src->castes.size(), src->extract.size(), src->tile_character, src->tilecolor.fore, src->tilecolor.back, src->tilecolor.bright)); for(unsigned int i = 0; i < dest->castesCount; i++) CreatureCasteConvert(&src->castes[i], &dest->castes[i]); diff --git a/library/include/dfhack-c/DFTypes_C.h b/library/include/dfhack-c/DFTypes_C.h index f0c3dcb4c..8832ea3d8 100644 --- a/library/include/dfhack-c/DFTypes_C.h +++ b/library/include/dfhack-c/DFTypes_C.h @@ -41,41 +41,41 @@ distribution. extern "C" { #endif -DFHACK_EXPORT extern int (*alloc_byte_buffer_callback)(int8_t*, uint32_t); -DFHACK_EXPORT extern int (*alloc_short_buffer_callback)(int16_t*, uint32_t); -DFHACK_EXPORT extern int (*alloc_int_buffer_callback)(int32_t*, uint32_t); +DFHACK_EXPORT extern int (*alloc_byte_buffer_callback)(int8_t**, uint32_t); +DFHACK_EXPORT extern int (*alloc_short_buffer_callback)(int16_t**, uint32_t); +DFHACK_EXPORT extern int (*alloc_int_buffer_callback)(int32_t**, uint32_t); -DFHACK_EXPORT extern int (*alloc_ubyte_buffer_callback)(uint8_t*, uint32_t); -DFHACK_EXPORT extern int (*alloc_ushort_buffer_callback)(uint16_t*, uint32_t); -DFHACK_EXPORT extern int (*alloc_uint_buffer_callback)(uint32_t*, uint32_t); +DFHACK_EXPORT extern int (*alloc_ubyte_buffer_callback)(uint8_t**, uint32_t); +DFHACK_EXPORT extern int (*alloc_ushort_buffer_callback)(uint16_t**, uint32_t); +DFHACK_EXPORT extern int (*alloc_uint_buffer_callback)(uint32_t**, uint32_t); -DFHACK_EXPORT extern int (*alloc_char_buffer_callback)(char*, uint32_t); +DFHACK_EXPORT extern int (*alloc_char_buffer_callback)(char**, uint32_t); -DFHACK_EXPORT extern int (*alloc_matgloss_buffer_callback)(t_matgloss*, uint32_t); -DFHACK_EXPORT extern int (*alloc_descriptor_buffer_callback)(t_descriptor_color*, uint32_t); -DFHACK_EXPORT extern int (*alloc_matgloss_other_buffer_callback)(t_matglossOther*, uint32_t); +DFHACK_EXPORT extern int (*alloc_matgloss_buffer_callback)(t_matgloss**, uint32_t); +DFHACK_EXPORT extern int (*alloc_descriptor_buffer_callback)(t_descriptor_color**, uint32_t); +DFHACK_EXPORT extern int (*alloc_matgloss_other_buffer_callback)(t_matglossOther**, uint32_t); -DFHACK_EXPORT extern int (*alloc_t_feature_buffer_callback)(t_feature*, uint32_t); -DFHACK_EXPORT extern int (*alloc_t_hotkey_buffer_callback)(t_hotkey*, uint32_t); -DFHACK_EXPORT extern int (*alloc_t_screen_buffer_callback)(t_screen*, uint32_t); +DFHACK_EXPORT extern int (*alloc_t_feature_buffer_callback)(t_feature**, uint32_t); +DFHACK_EXPORT extern int (*alloc_t_hotkey_buffer_callback)(t_hotkey**, uint32_t); +DFHACK_EXPORT extern int (*alloc_t_screen_buffer_callback)(t_screen**, uint32_t); -DFHACK_EXPORT void RegisterByteBufferCallback(int (*funcptr)(int8_t*, uint32_t)); -DFHACK_EXPORT void RegisterShortBufferCallback(int (*funcptr)(int16_t*, uint32_t)); -DFHACK_EXPORT void RegisterIntBufferCallback(int (*funcptr)(int32_t*, uint32_t)); +DFHACK_EXPORT void RegisterByteBufferCallback(int (*funcptr)(int8_t**, uint32_t)); +DFHACK_EXPORT void RegisterShortBufferCallback(int (*funcptr)(int16_t**, uint32_t)); +DFHACK_EXPORT void RegisterIntBufferCallback(int (*funcptr)(int32_t**, uint32_t)); -DFHACK_EXPORT void RegisterUByteBufferCallback(int (*funcptr)(uint8_t*, uint32_t)); -DFHACK_EXPORT void RegisterUShortBufferCallback(int (*funcptr)(uint16_t*, uint32_t)); -DFHACK_EXPORT void RegisterUIntBufferCallback(int (*funcptr)(uint32_t*, uint32_t)); +DFHACK_EXPORT void RegisterUByteBufferCallback(int (*funcptr)(uint8_t**, uint32_t)); +DFHACK_EXPORT void RegisterUShortBufferCallback(int (*funcptr)(uint16_t**, uint32_t)); +DFHACK_EXPORT void RegisterUIntBufferCallback(int (*funcptr)(uint32_t**, uint32_t)); -DFHACK_EXPORT void RegisterCharBufferCallback(int (*funcptr)(char*, uint32_t)); +DFHACK_EXPORT void RegisterCharBufferCallback(int (*funcptr)(char**, uint32_t)); -DFHACK_EXPORT void RegisterMatglossBufferCallback(int (*funcptr)(t_matgloss*, uint32_t)); -DFHACK_EXPORT void RegisterDescriptorColorBufferCallback(int (*funcptr)(t_descriptor_color*, uint32_t)); -DFHACK_EXPORT void RegisterMatglossOtherBufferCallback(int (*funcptr)(t_matglossOther*, uint32_t)); +DFHACK_EXPORT void RegisterMatglossBufferCallback(int (*funcptr)(t_matgloss**, uint32_t)); +DFHACK_EXPORT void RegisterDescriptorColorBufferCallback(int (*funcptr)(t_descriptor_color**, uint32_t)); +DFHACK_EXPORT void RegisterMatglossOtherBufferCallback(int (*funcptr)(t_matglossOther**, uint32_t)); -DFHACK_EXPORT void RegisterFeatureBufferCallback(int (*funcptr)(t_feature*, uint32_t)); -DFHACK_EXPORT void RegisterHotkeyBufferCallback(int (*funcptr)(t_hotkey*, uint32_t)); -DFHACK_EXPORT void RegisterScreenBufferCallback(int (*funcptr)(t_screen*, uint32_t)); +DFHACK_EXPORT void RegisterFeatureBufferCallback(int (*funcptr)(t_feature**, uint32_t)); +DFHACK_EXPORT void RegisterHotkeyBufferCallback(int (*funcptr)(t_hotkey**, uint32_t)); +DFHACK_EXPORT void RegisterScreenBufferCallback(int (*funcptr)(t_screen**, uint32_t)); HUNREG_MACRO(Byte) HUNREG_MACRO(Short) @@ -100,11 +100,11 @@ struct t_customWorkshop char name[256]; }; -DFHACK_EXPORT extern int (*alloc_t_customWorkshop_buffer_callback)(t_customWorkshop*, uint32_t); -DFHACK_EXPORT extern int (*alloc_t_material_buffer_callback)(t_material*, uint32_t); +DFHACK_EXPORT extern int (*alloc_t_customWorkshop_buffer_callback)(t_customWorkshop**, uint32_t); +DFHACK_EXPORT extern int (*alloc_t_material_buffer_callback)(t_material**, uint32_t); -DFHACK_EXPORT void RegisterCustomWorkshopBufferCallback(int (*funcptr)(t_customWorkshop*, uint32_t)); -DFHACK_EXPORT void RegisterMaterialBufferCallback(int (*funcptr)(t_material*, uint32_t)); +DFHACK_EXPORT void RegisterCustomWorkshopBufferCallback(int (*funcptr)(t_customWorkshop**, uint32_t)); +DFHACK_EXPORT void RegisterMaterialBufferCallback(int (*funcptr)(t_material**, uint32_t)); HUNREG_MACRO(CustomWorkshop) HUNREG_MACRO(Material) @@ -116,13 +116,13 @@ struct c_colormodifier uint32_t colorlistLength; }; -DFHACK_EXPORT extern int (*alloc_empty_colormodifier_callback)(c_colormodifier*); -DFHACK_EXPORT extern int (*alloc_colormodifier_callback)(c_colormodifier*, const char*, uint32_t); -DFHACK_EXPORT extern int (*alloc_colormodifier_buffer_callback)(c_colormodifier*, uint32_t); +DFHACK_EXPORT extern int (*alloc_empty_colormodifier_callback)(c_colormodifier**); +DFHACK_EXPORT extern int (*alloc_colormodifier_callback)(c_colormodifier**, const char*, uint32_t); +DFHACK_EXPORT extern int (*alloc_colormodifier_buffer_callback)(c_colormodifier**, uint32_t); -DFHACK_EXPORT void RegisterEmptyColorModifierCallback(int (*funcptr)(c_colormodifier*)); -DFHACK_EXPORT void RegisterNewColorModifierCallback(int (*funcptr)(c_colormodifier*, const char*, uint32_t)); -DFHACK_EXPORT void RegisterColorModifierBufferCallback(int (*funcptr)(c_colormodifier*, uint32_t)); +DFHACK_EXPORT void RegisterEmptyColorModifierCallback(int (*funcptr)(c_colormodifier**)); +DFHACK_EXPORT void RegisterNewColorModifierCallback(int (*funcptr)(c_colormodifier**, const char*, uint32_t)); +DFHACK_EXPORT void RegisterColorModifierBufferCallback(int (*funcptr)(c_colormodifier**, uint32_t)); DFHACK_EXPORT void UnregisterEmptyColorModifierCallback(); DFHACK_EXPORT void UnregisterNewColorModifierCallback(); @@ -135,20 +135,20 @@ struct c_creaturecaste char plural[128]; char adjective[128]; - c_colormodifier* ColorModifier; + c_colormodifier* colorModifier; uint32_t colorModifierLength; t_bodypart* bodypart; uint32_t bodypartLength; }; -DFHACK_EXPORT extern int (*alloc_empty_creaturecaste_callback)(c_creaturecaste*); -DFHACK_EXPORT extern int (*alloc_creaturecaste_callback)(c_creaturecaste*, const char*, const char*, const char*, const char*, uint32_t, uint32_t); -DFHACK_EXPORT extern int (*alloc_creaturecaste_buffer_callback)(c_creaturecaste*, uint32_t); +DFHACK_EXPORT extern int (*alloc_empty_creaturecaste_callback)(c_creaturecaste**); +DFHACK_EXPORT extern int (*alloc_creaturecaste_callback)(c_creaturecaste**, const char*, const char*, const char*, const char*, uint32_t, uint32_t); +DFHACK_EXPORT extern int (*alloc_creaturecaste_buffer_callback)(c_creaturecaste**, uint32_t); -DFHACK_EXPORT void RegisterEmptyCreatureCasteCallback(int (*funcptr)(c_creaturecaste*)); -DFHACK_EXPORT void RegisterNewCreatureCasteCallback(int (*funcptr)(c_creaturecaste*, const char*, const char*, const char*, const char*, uint32_t, uint32_t)); -DFHACK_EXPORT void RegisterCreatureCasteBufferCallback(int (*funcptr)(c_creaturecaste*, uint32_t)); +DFHACK_EXPORT void RegisterEmptyCreatureCasteCallback(int (*funcptr)(c_creaturecaste**)); +DFHACK_EXPORT void RegisterNewCreatureCasteCallback(int (*funcptr)(c_creaturecaste**, const char*, const char*, const char*, const char*, uint32_t, uint32_t)); +DFHACK_EXPORT void RegisterCreatureCasteBufferCallback(int (*funcptr)(c_creaturecaste**, uint32_t)); DFHACK_EXPORT void UnregisterEmptyCreatureCasteCallback(); DFHACK_EXPORT void UnregisterNewCreatureCasteCallback(); @@ -174,25 +174,25 @@ struct c_creaturetype } tilecolor; }; -DFHACK_EXPORT extern int (*alloc_empty_creaturetype_callback)(c_creaturetype*); -DFHACK_EXPORT extern int (*alloc_creaturetype_callback)(c_creaturetype*, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t); -DFHACK_EXPORT extern int (*alloc_creaturetype_buffer_callback)(c_creaturetype*, uint32_t); - -DFHACK_EXPORT extern int (*alloc_vein_buffer_callback)(t_vein*, uint32_t); -DFHACK_EXPORT extern int (*alloc_frozenliquidvein_buffer_callback)(t_frozenliquidvein*, uint32_t); -DFHACK_EXPORT extern int (*alloc_spattervein_buffer_callback)(t_spattervein*, uint32_t); -DFHACK_EXPORT extern int (*alloc_grassvein_buffer_callback)(t_grassvein*, uint32_t); -DFHACK_EXPORT extern int (*alloc_worldconstruction_buffer_callback)(t_worldconstruction*, uint32_t); - -DFHACK_EXPORT void RegisterEmptyCreatureTypeCallback(int (*funcptr)(c_creaturetype*)); -DFHACK_EXPORT void RegisterNewCreatureTypeCallback(int (*funcptr)(c_creaturetype*, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t)); -DFHACK_EXPORT void RegisterCreatureTypeBufferCallback(int (*funcptr)(c_creaturetype*, uint32_t)); - -DFHACK_EXPORT void RegisterVeinBufferCallback(int (*funcptr)(t_vein*, uint32_t)); -DFHACK_EXPORT void RegisterFrozenLiquidVeinBufferCallback(int (*funcptr)(t_frozenliquidvein*, uint32_t)); -DFHACK_EXPORT void RegisterSpatterVeinBufferCallback(int (*funcptr)(t_spattervein*, uint32_t)); -DFHACK_EXPORT void RegisterGrassVeinBufferCallback(int (*funcptr)(t_grassvein*, uint32_t)); -DFHACK_EXPORT void RegisterWorldConstructionBufferCallback(int (*funcptr)(t_worldconstruction*, uint32_t)); +DFHACK_EXPORT extern int (*alloc_empty_creaturetype_callback)(c_creaturetype**); +DFHACK_EXPORT extern int (*alloc_creaturetype_callback)(c_creaturetype**, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t); +DFHACK_EXPORT extern int (*alloc_creaturetype_buffer_callback)(c_creaturetype**, uint32_t); + +DFHACK_EXPORT extern int (*alloc_vein_buffer_callback)(t_vein**, uint32_t); +DFHACK_EXPORT extern int (*alloc_frozenliquidvein_buffer_callback)(t_frozenliquidvein**, uint32_t); +DFHACK_EXPORT extern int (*alloc_spattervein_buffer_callback)(t_spattervein**, uint32_t); +DFHACK_EXPORT extern int (*alloc_grassvein_buffer_callback)(t_grassvein**, uint32_t); +DFHACK_EXPORT extern int (*alloc_worldconstruction_buffer_callback)(t_worldconstruction**, uint32_t); + +DFHACK_EXPORT void RegisterEmptyCreatureTypeCallback(int (*funcptr)(c_creaturetype**)); +DFHACK_EXPORT void RegisterNewCreatureTypeCallback(int (*funcptr)(c_creaturetype**, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t)); +DFHACK_EXPORT void RegisterCreatureTypeBufferCallback(int (*funcptr)(c_creaturetype**, uint32_t)); + +DFHACK_EXPORT void RegisterVeinBufferCallback(int (*funcptr)(t_vein**, uint32_t)); +DFHACK_EXPORT void RegisterFrozenLiquidVeinBufferCallback(int (*funcptr)(t_frozenliquidvein**, uint32_t)); +DFHACK_EXPORT void RegisterSpatterVeinBufferCallback(int (*funcptr)(t_spattervein**, uint32_t)); +DFHACK_EXPORT void RegisterGrassVeinBufferCallback(int (*funcptr)(t_grassvein**, uint32_t)); +DFHACK_EXPORT void RegisterWorldConstructionBufferCallback(int (*funcptr)(t_worldconstruction**, uint32_t)); DFHACK_EXPORT void UnregisterEmptyCreatureTypeCallback(); DFHACK_EXPORT void UnregisterNewCreatureTypeCallback(); From 5bd51c2e08c3a7779751d2a442605b141d98ee39 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 9 Mar 2011 12:23:48 -0600 Subject: [PATCH 03/13] updated to use the allocator callbacks correctly --- library/modules/Buildings_C.cpp | 2 +- library/modules/Creatures_C.cpp | 6 +-- library/modules/Items_C.cpp | 7 +++- library/modules/Materials_C.cpp | 60 +++++++++++++++++++++--------- library/modules/Position_C.cpp | 4 +- library/modules/Translation_C.cpp | 62 +------------------------------ 6 files changed, 55 insertions(+), 86 deletions(-) diff --git a/library/modules/Buildings_C.cpp b/library/modules/Buildings_C.cpp index ed3e3e74e..5fee4677a 100644 --- a/library/modules/Buildings_C.cpp +++ b/library/modules/Buildings_C.cpp @@ -81,7 +81,7 @@ t_customWorkshop* Buildings_ReadCustomWorkshopTypes(DFHackObject* b_Ptr) if(!((DFHack::Buildings*)b_Ptr)->ReadCustomWorkshopTypes(bTypes)) return NULL; - (*alloc_t_customWorkshop_buffer_callback)(cw_Ptr, bTypes.size()); + (*alloc_t_customWorkshop_buffer_callback)(&cw_Ptr, bTypes.size()); if(cw_Ptr == NULL) return NULL; diff --git a/library/modules/Creatures_C.cpp b/library/modules/Creatures_C.cpp index 297a15460..b34c6e8d9 100644 --- a/library/modules/Creatures_C.cpp +++ b/library/modules/Creatures_C.cpp @@ -86,7 +86,7 @@ t_material* Creatures_ReadJob(DFHackObject* cPtr, const t_creature* furball) t_material* buf = NULL; - (*alloc_t_material_buffer_callback)(buf, mat.size()); + (*alloc_t_material_buffer_callback)(&buf, mat.size()); if(buf != NULL) { @@ -117,7 +117,7 @@ uint32_t* Creatures_ReadInventoryIdx(DFHackObject* cPtr, const uint32_t index) uint32_t* buf = NULL; - (*alloc_uint_buffer_callback)(buf, item.size()); + (*alloc_uint_buffer_callback)(&buf, item.size()); if(buf != NULL) { @@ -146,7 +146,7 @@ uint32_t* Creatures_ReadInventoryPtr(DFHackObject* cPtr, const uint32_t index) uint32_t* buf = NULL; - (*alloc_uint_buffer_callback)(buf, item.size()); + (*alloc_uint_buffer_callback)(&buf, item.size()); if(buf != NULL) { diff --git a/library/modules/Items_C.cpp b/library/modules/Items_C.cpp index bfc31f2f4..7b366aece 100644 --- a/library/modules/Items_C.cpp +++ b/library/modules/Items_C.cpp @@ -59,7 +59,8 @@ char* Items_getItemDescription(DFHackObject* items, uint32_t itemptr, DFHackObje if(desc.size() > 0) { char* buf = NULL; - (*alloc_char_buffer_callback)(buf,desc.size()); + + (*alloc_char_buffer_callback)(&buf, desc.size()); if(buf != NULL) { @@ -87,7 +88,9 @@ char* Items_getItemClass(DFHackObject* items, int32_t index) if(iclass.size() > 0) { char* buf = NULL; - (*alloc_char_buffer_callback)(buf, iclass.size()); + + (*alloc_char_buffer_callback)(&buf, iclass.size()); + if(buf != NULL) { size_t len = iclass.copy(buf, iclass.size()); diff --git a/library/modules/Materials_C.cpp b/library/modules/Materials_C.cpp index e8e42af76..c334e5595 100644 --- a/library/modules/Materials_C.cpp +++ b/library/modules/Materials_C.cpp @@ -32,7 +32,10 @@ int Materials_ReadInorganicMaterials(DFHackObject* mat) { if(mat != NULL) { - return ((DFHack::Materials*)mat)->ReadInorganicMaterials(); + if(((DFHack::Materials*)mat)->ReadInorganicMaterials() == true) + return 1; + else + return 0; } return -1; @@ -42,7 +45,10 @@ int Materials_ReadOrganicMaterials(DFHackObject* mat) { if(mat != NULL) { - return ((DFHack::Materials*)mat)->ReadOrganicMaterials(); + if(((DFHack::Materials*)mat)->ReadOrganicMaterials() == true) + return 1; + else + return 0; } return -1; @@ -52,7 +58,10 @@ int Materials_ReadWoodMaterials(DFHackObject* mat) { if(mat != NULL) { - return ((DFHack::Materials*)mat)->ReadWoodMaterials(); + if(((DFHack::Materials*)mat)->ReadWoodMaterials() == true) + return 1; + else + return 0; } return -1; @@ -62,7 +71,10 @@ int Materials_ReadPlantMaterials(DFHackObject* mat) { if(mat != NULL) { - return ((DFHack::Materials*)mat)->ReadPlantMaterials(); + if(((DFHack::Materials*)mat)->ReadPlantMaterials() == true) + return 1; + else + return 0; } return -1; @@ -72,7 +84,10 @@ int Materials_ReadCreatureTypes(DFHackObject* mat) { if(mat != NULL) { - return ((DFHack::Materials*)mat)->ReadCreatureTypes(); + if(((DFHack::Materials*)mat)->ReadCreatureTypes() == true) + return 1; + else + return 0; } return -1; @@ -82,7 +97,10 @@ int Materials_ReadCreatureTypesEx(DFHackObject* mat) { if(mat != NULL) { - return ((DFHack::Materials*)mat)->ReadCreatureTypesEx(); + if(((DFHack::Materials*)mat)->ReadCreatureTypesEx() == true) + return 1; + else + return 0; } return -1; @@ -92,7 +110,10 @@ int Materials_ReadDescriptorColors(DFHackObject* mat) { if(mat != NULL) { - return ((DFHack::Materials*)mat)->ReadDescriptorColors(); + if(((DFHack::Materials*)mat)->ReadDescriptorColors() == true) + return 1; + else + return 0; } return -1; @@ -102,7 +123,10 @@ int Materials_ReadOthers(DFHackObject* mat) { if(mat != NULL) { - return ((DFHack::Materials*)mat)->ReadOthers(); + if(((DFHack::Materials*)mat)->ReadOthers() == true) + return 1; + else + return 0; } return -1; @@ -237,7 +261,7 @@ t_matgloss* Materials_getInorganic(DFHackObject* mat) if(alloc_matgloss_buffer_callback == NULL) return NULL; - ((*alloc_matgloss_buffer_callback)(buf, materials->inorganic.size())); + ((*alloc_matgloss_buffer_callback)(&buf, materials->inorganic.size())); if(buf != NULL) { @@ -264,7 +288,7 @@ t_matgloss* Materials_getOrganic(DFHackObject* mat) if(alloc_matgloss_buffer_callback == NULL) return NULL; - ((*alloc_matgloss_buffer_callback)(buf, materials->organic.size())); + ((*alloc_matgloss_buffer_callback)(&buf, materials->organic.size())); if(buf != NULL) { @@ -291,7 +315,7 @@ t_matgloss* Materials_getTree(DFHackObject* mat) if(alloc_matgloss_buffer_callback == NULL) return NULL; - ((*alloc_matgloss_buffer_callback)(buf, materials->tree.size())); + ((*alloc_matgloss_buffer_callback)(&buf, materials->tree.size())); if(buf != NULL) { @@ -318,7 +342,7 @@ t_matgloss* Materials_getPlant(DFHackObject* mat) if(alloc_matgloss_buffer_callback == NULL) return NULL; - ((*alloc_matgloss_buffer_callback)(buf, materials->plant.size())); + ((*alloc_matgloss_buffer_callback)(&buf, materials->plant.size())); if(buf != NULL) { @@ -345,7 +369,7 @@ t_matgloss* Materials_getRace(DFHackObject* mat) if(alloc_matgloss_buffer_callback == NULL) return NULL; - ((*alloc_matgloss_buffer_callback)(buf, materials->race.size())); + ((*alloc_matgloss_buffer_callback)(&buf, materials->race.size())); if(buf != NULL) { @@ -374,12 +398,12 @@ c_creaturetype* Materials_getRaceEx(DFHackObject* mat) if(alloc_creaturetype_buffer_callback == NULL) return NULL; - ((*alloc_creaturetype_buffer_callback)(buf, matSize)); + ((*alloc_creaturetype_buffer_callback)(&buf, matSize)); if(buf != NULL) { for(int i = 0; i < matSize; i++) - CreatureTypeConvert(&materials->raceEx[i], &buf[i]); + CreatureTypeConvert(&materials->raceEx[i], &(buf[i])); return buf; } @@ -399,7 +423,7 @@ t_descriptor_color* Materials_getColor(DFHackObject* mat) { t_descriptor_color* buf = NULL; - ((*alloc_descriptor_buffer_callback)(buf, materials->color.size())); + ((*alloc_descriptor_buffer_callback)(&buf, materials->color.size())); if(buf != NULL) { @@ -423,7 +447,7 @@ t_matglossOther* Materials_getOther(DFHackObject* mat) { t_matglossOther* buf = NULL; - ((*alloc_matgloss_other_buffer_callback)(buf, materials->other.size())); + ((*alloc_matgloss_other_buffer_callback)(&buf, materials->other.size())); if(buf != NULL) { @@ -447,7 +471,7 @@ t_matgloss* Materials_getAllDesc(DFHackObject* mat) { t_matgloss* buf = NULL; - ((*alloc_matgloss_buffer_callback)(buf, materials->alldesc.size())); + ((*alloc_matgloss_buffer_callback)(&buf, materials->alldesc.size())); if(buf != NULL) { diff --git a/library/modules/Position_C.cpp b/library/modules/Position_C.cpp index 386bd1c5b..e3e4af199 100644 --- a/library/modules/Position_C.cpp +++ b/library/modules/Position_C.cpp @@ -104,7 +104,7 @@ t_hotkey* Position_ReadHotkeys(DFHackObject* pos) { t_hotkey* buf = NULL; - (*alloc_t_hotkey_buffer_callback)(buf, NUM_HOTKEYS); + (*alloc_t_hotkey_buffer_callback)(&buf, NUM_HOTKEYS); if(buf != NULL) { @@ -146,7 +146,7 @@ t_screen* Position_getScreenTiles(DFHackObject* pos, int32_t width, int32_t heig { t_screen* buf = NULL; - (*alloc_t_screen_buffer_callback)(buf, width * height); + (*alloc_t_screen_buffer_callback)(&buf, width * height); if(buf == NULL) return NULL; diff --git a/library/modules/Translation_C.cpp b/library/modules/Translation_C.cpp index 7032f26c5..64420ae33 100644 --- a/library/modules/Translation_C.cpp +++ b/library/modules/Translation_C.cpp @@ -58,64 +58,6 @@ int Translation_Finish(DFHackObject* trans) return -1; } -// char* Translation_TranslateNameEnglish(DFHackObject* trans, const DFHack::t_name* name, char* (*char_buffer_create)(int)) -// { - // if(trans != NULL) - // { - // std::string nameTrans = ((DFHack::Translation*)trans)->TranslateName(*name, true); - - // if(nameTrans.size() > 0) - // { - // char* buf = (*char_buffer_create)(nameTrans.size()); - - // if(buf != NULL) - // { - // size_t len = nameTrans.copy(buf, nameTrans.size()); - - // if(len > 0) - // buf[len] = '\0'; - // else - // buf[0] = '\0'; - // } - - // return buf; - // } - // else - // return NULL; - // } - - // return NULL; -// } - -// char* Translation_TranslateNameNonEnglish(DFHackObject* trans, const DFHack::t_name* name, char* (*char_buffer_create)(int)) -// { - // if(trans != NULL) - // { - // std::string nameTrans = ((DFHack::Translation*)trans)->TranslateName(*name, false); - - // if(nameTrans.size() > 0) - // { - // char* buf = (*char_buffer_create)(nameTrans.size()); - - // if(buf != NULL) - // { - // size_t len = nameTrans.copy(buf, nameTrans.size()); - - // if(len > 0) - // buf[len] = '\0'; - // else - // buf[0] = '\0'; - // } - - // return buf; - // } - // else - // return NULL; - // } - - // return NULL; -// } - char* Translation_TranslateNameEnglish(DFHackObject* trans, const DFHack::t_name* name) { if(trans != NULL) @@ -126,7 +68,7 @@ char* Translation_TranslateNameEnglish(DFHackObject* trans, const DFHack::t_name { char* buf = NULL; - (*alloc_char_buffer_callback)(buf, nameTrans.size()); + (*alloc_char_buffer_callback)(&buf, nameTrans.size()); if(buf != NULL) { @@ -157,7 +99,7 @@ char* Translation_TranslateNameNonEnglish(DFHackObject* trans, const DFHack::t_n { char* buf = NULL; - (*alloc_char_buffer_callback)(buf, nameTrans.size()); + (*alloc_char_buffer_callback)(&buf, nameTrans.size()); if(buf != NULL) { From 50af9e2d7e3bb704ba545bd7a5195e18fefbfa45 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 9 Mar 2011 12:27:07 -0600 Subject: [PATCH 04/13] updated to use the pointer caching callbacks --- library/python/pydfhack/creatures.py | 13 +++++- library/python/pydfhack/maps.py | 51 ++++++++++++++++++-- library/python/pydfhack/materials.py | 69 ++++++++++++++++++++++++---- library/python/pydfhack/util.py | 64 +++++++++++++++++--------- 4 files changed, 159 insertions(+), 38 deletions(-) diff --git a/library/python/pydfhack/creatures.py b/library/python/pydfhack/creatures.py index 06bc52c36..b4a51a4b5 100644 --- a/library/python/pydfhack/creatures.py +++ b/library/python/pydfhack/creatures.py @@ -4,6 +4,10 @@ import util libdfhack.Creatures_WriteLabors.argtypes = [ c_void_p, c_uint, POINTER(c_ubyte) ] +libdfhack.Creatures_ReadJob.restype = POINTER(Material) +libdfhack.Creatures_ReadInventoryIdx.restype = POINTER(c_uint) +libdfhack.Creatures_ReadInventoryPtr.restype = POINTER(c_uint) + class Creatures(object): def __init__(self, ptr): print ptr @@ -45,7 +49,14 @@ class Creatures(object): return libdfhack.Creatures_WriteLabors(self._c_ptr, c_uint(index), labors) > 0 def read_job(self, creature): - return libdfhack.Creatures_ReadJob(self._c_ptr, byref(creature)) + job_ptr = libdfhack.Creatures_ReadJob(self._c_ptr, byref(creature)) + jobs = None + + if id(job_ptr) in dftypes.pointer_dict: + jobs = dftypes.pointer_dict[id(job_ptr)][1] + del dftypes.pointer_dict[id(job_ptr)] + + return jobs @property def dwarf_race_index(self): diff --git a/library/python/pydfhack/maps.py b/library/python/pydfhack/maps.py index ea2175b6a..8fa92abe9 100644 --- a/library/python/pydfhack/maps.py +++ b/library/python/pydfhack/maps.py @@ -23,6 +23,12 @@ libdfhack.Maps_ReadSpatterVeins.argtypes = _default_argtypes libdfhack.Maps_ReadGrassVeins.argtypes = _default_argtypes libdfhack.Maps_ReadWorldConstructions.argtypes = _default_argtypes +libdfhack.Maps_ReadStandardVeins.restype = POINTER(Vein) +libdfhack.Maps_ReadFrozenVeins.restype = POINTER(FrozenLiquidVein) +libdfhack.Maps_ReadSpatterVeins.restype = POINTER(SpatterVein) +libdfhack.Maps_ReadGrassVeins.restype = POINTER(GrassVein) +libdfhack.Maps_ReadWorldConstructions.restype = POINTER(WorldConstruction) + class Maps(object): def __init__(self, ptr): self._map_ptr = ptr @@ -162,27 +168,62 @@ class Maps(object): def read_veins(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - return libdfhack.Maps_ReadStandardVeins(self._map_ptr, ux, uy, uz) + veins_ptr = libdfhack.Maps_ReadStandardVeins(self._map_ptr, ux, uy, uz) + veins = None + + if id(veins_ptr) in dftypes.pointer_dict: + veins = dftypes.pointer_dict[id(veins_ptr)][1] + del dftypes.pointer_dict[id(veins_ptr)] + + return veins def read_frozen_veins(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - return libdfhack.Maps_ReadFrozenVeins(self._map_ptr, ux, uy, uz) + veins_ptr = libdfhack.Maps_ReadFrozenVeins(self._map_ptr, ux, uy, uz) + veins = None + + if id(veins_ptr) in dftypes.pointer_dict: + veins = dftypes.pointer_dict[id(veins_ptr)][1] + del dftypes.pointer_dict[id(veins_ptr)] + + return veins def read_spatter_veins(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - return libdfhack.Maps_ReadSpatterVeins(self._map_ptr, ux, uy, uz) + veins_ptr = libdfhack.Maps_ReadSpatterVeins(self._map_ptr, ux, uy, uz) + veins = None + + if id(veins_ptr) in dftypes.pointer_dict: + veins = dftypes.pointer_dict[id(veins_ptr)][1] + del dftypes.pointer_dict[id(veins_ptr)] + + return veins def read_grass_veins(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - return libdfhack.Maps_ReadGrassVeins(self._map_ptr, ux, uy, uz) + veins_ptr = libdfhack.Maps_ReadGrassVeins(self._map_ptr, ux, uy, uz) + veins = None + + if id(veins_ptr) in dftypes.pointer_dict: + veins = dftypes.pointer_dict[id(veins_ptr)][1] + del dftypes.pointer_dict[id(veins_ptr)] + + return veins def read_world_constructions(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - return libdfhack.Maps_ReadWorldConstructions(self._map_ptr, ux, uy, uz) + veins_ptr = libdfhack.Maps_ReadWorldConstructions(self._map_ptr, ux, uy, uz) + veins = None + + if id(veins_ptr) in dftypes.pointer_dict: + veins = dftypes.pointer_dict[id(veins_ptr)][1] + del dftypes.pointer_dict[id(veins_ptr)] + + return veins @property def size(self): diff --git a/library/python/pydfhack/materials.py b/library/python/pydfhack/materials.py index 4a135f9a1..466bb68ae 100644 --- a/library/python/pydfhack/materials.py +++ b/library/python/pydfhack/materials.py @@ -1,6 +1,16 @@ from ctypes import * -from dftypes import libdfhack -from util import * +import dftypes +from dftypes import libdfhack, Matgloss, CreatureType, DescriptorColor, MatglossOther + +libdfhack.Materials_getInorganic.restype = POINTER(Matgloss) +libdfhack.Materials_getOrganic.restype = POINTER(Matgloss) +libdfhack.Materials_getTree.restype = POINTER(Matgloss) +libdfhack.Materials_getPlant.restype = POINTER(Matgloss) +libdfhack.Materials_getRace.restype = POINTER(Matgloss) +libdfhack.Materials_getRaceEx.restype = POINTER(CreatureType) +libdfhack.Materials_getColor.restype = POINTER(DescriptorColor) +libdfhack.Materials_getOther.restype = POINTER(MatglossOther) +libdfhack.Materials_getAllDesc.restype = POINTER(Matgloss) class Materials(object): def __init__(self, ptr): @@ -16,21 +26,60 @@ class Materials(object): self.other = None def _get_inorganic(self): - self.inorganic = libdfhack.Materials_getInorganic(self._mat_ptr) + inorganic = libdfhack.Materials_getInorganic(self._mat_ptr) + + if id(inorganic) in dftypes.pointer_dict: + self.inorganic = dftypes.pointer_dict[id(inorganic)][1] + del dftypes.pointer_dict[id(inorganic)] + def _get_organic(self): - self.organic = libdfhack.Materials_getOrganic(self._mat_ptr) + organic = libdfhack.Materials_getOrganic(self._mat_ptr) + + if id(organic) in dftypes.pointer_dict: + self.organic = dftypes.pointer_dict[id(organic)][1] + del dftypes.pointer_dict[id(inorganic)] + def _get_tree(self): - self.tree = libdfhack.Materials_getTree(self._mat_ptr) + tree = libdfhack.Materials_getTree(self._mat_ptr) + + if id(tree) in dftypes.pointer_dict: + self.tree = dftypes.pointer_dict[id(tree)][1] + del dftypes.pointer_dict[id(tree)] + def _get_plant(self): - self.plant = libdfhack.Materials_getPlant(self._mat_ptr) + plant = libdfhack.Materials_getPlant(self._mat_ptr) + + if id(plant) in dftypes.pointer_dict: + self.tree = dftypes.pointer_dict[id(tree)][1] + del dftypes.pointer_dict[id(tree)] + def _get_race(self): - self.race = libdfhack.Materials_getRace(self._mat_ptr) + race = libdfhack.Materials_getRace(self._mat_ptr) + + if id(race) in dftypes.pointer_dict: + self.race = dftypes.pointer_dict[id(race)][1] + del dftypes.pointer_dict[id(race)] + def _get_race_ex(self): - self.race_ex = libdfhack.Materials_getRaceEx(self._mat_ptr) + race_ex = libdfhack.Materials_getRaceEx(self._mat_ptr) + + if id(race_ex) in dftypes.pointer_dict: + self.race_ex = dftypes.pointer_dict[id(race_ex)][1] + del dftypes.pointer_dict[id(race_ex)] + def _get_color(self): - self.color = libdfhack.Materials_getColor(self._mat_ptr) + color = libdfhack.Materials_getColor(self._mat_ptr) + + if id(color) in dftypes.pointer_dict: + self.color = dftypes.pointer_dict[id(color)][1] + del dftypes.pointer_dict[id(color)] + def _get_other(self): - self.other = libdfhack.Materials_getOther(self._mat_ptr) + other = libdfhack.Materials_getOther(self._mat_ptr) + + if id(other) in dftypes.pointer_dict: + self.other = dftypes.pointer_dict[id(other)][1] + del dftypes.pointer_dict[id(other)] def _get_all(self): self._get_inorganic() diff --git a/library/python/pydfhack/util.py b/library/python/pydfhack/util.py index d0849b1d8..38365cbf5 100644 --- a/library/python/pydfhack/util.py +++ b/library/python/pydfhack/util.py @@ -1,7 +1,15 @@ from ctypes import * -uint_ptr = POINTER(c_uint) int_ptr = POINTER(c_int) +uint_ptr = POINTER(c_uint) + +short_ptr = POINTER(c_short) +ushort_ptr = POINTER(c_ushort) + +byte_ptr = POINTER(c_byte) +ubyte_ptr = POINTER(c_ubyte) + +pointer_dict = {} def _uintify(x, y, z): return (c_uint(x), c_uint(y), c_uint(z)) @@ -11,80 +19,92 @@ def _allocate_array(t_type, count): arr = arr_type() - ptr = c_void_p() - ptr = addressof(arr) - - return (arr, ptr) + return arr def _alloc_int_buffer(ptr, count): a = _allocate_array(c_int, count) + + p = cast(a, int_ptr) - ptr = addressof(a[0]) + ptr[0] = p + + pointer_dict[id(ptr[0])] = (ptr, a, p) return 1 -_int_functype = CFUNCTYPE(c_int, POINTER(c_int), c_uint) +_int_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_int)), c_uint) alloc_int_buffer = _int_functype(_alloc_int_buffer) def _alloc_uint_buffer(ptr, count): a = _allocate_array(c_uint, count) - ptr = addressof(a[0]) + ptr[0] = cast(a, uint_ptr) + + pointer_dict[id(ptr[0])] = (ptr, a, p) return 1 -_uint_functype = CFUNCTYPE(c_int, POINTER(c_uint), c_uint) +_uint_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_uint)), c_uint) alloc_uint_buffer = _uint_functype(_alloc_uint_buffer) def _alloc_short_buffer(ptr, count): a = _allocate_array(c_short, count) - ptr = addressof(a[0]) + ptr[0] = cast(a, short_ptr) + + pointer_dict[id(ptr[0])] = (ptr, a, p) return 1 -_short_functype = CFUNCTYPE(c_int, POINTER(c_short), c_uint) +_short_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_short)), c_uint) alloc_short_buffer = _short_functype(_alloc_short_buffer) def _alloc_ushort_buffer(ptr, count): a = _allocate_array(c_ushort, count) - ptr = addressof(a[0]) + ptr[0] = cast(a, ushort_ptr) + + pointer_dict[id(ptr[0])] = (ptr, a, p) return 1 -_ushort_functype = CFUNCTYPE(c_int, POINTER(c_ushort), c_uint) +_ushort_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_ushort)), c_uint) alloc_ushort_buffer = _ushort_functype(_alloc_ushort_buffer) def _alloc_byte_buffer(ptr, count): a = _allocate_array(c_byte, count) - ptr = addressof(a[0]) + ptr[0] = cast(a, byte_ptr) + + pointer_dict[id(ptr[0])] = (ptr, a, p) return 1 -_byte_functype = CFUNCTYPE(c_int, POINTER(c_byte), c_uint) +_byte_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_byte)), c_uint) alloc_byte_buffer = _byte_functype(_alloc_byte_buffer) def _alloc_ubyte_buffer(ptr, count): a = _allocate_array(c_ubyte, count) - ptr = addressof(a[0]) + ptr[0] = cast(a, ubyte_ptr) + + pointer_dict[id(ptr[0])] = (ptr, a, p) return 1 -_ubyte_functype = CFUNCTYPE(c_int, POINTER(c_ubyte), c_uint) +_ubyte_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_ubyte)), c_uint) alloc_ubyte_buffer = _ubyte_functype(_alloc_ubyte_buffer) def _alloc_char_buffer(ptr, count): c = create_string_buffer(count) + + p = cast(c, POINTER(c_char)) - if ptr is None: - ptr = c_void_p - - ptr = addressof(c) + ptr[0] = p + + pointer_dict[id(ptr[0])] = (ptr, c, p) return 1 -_char_functype = CFUNCTYPE(c_int, POINTER(c_char), c_uint) +_char_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_char)), c_uint) alloc_char_buffer = _char_functype(_alloc_char_buffer) From 1839c6b843edc49f2c4b7786c13fece346ffc7c5 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 9 Mar 2011 12:29:29 -0600 Subject: [PATCH 05/13] fixed callback registration allocator callbacks now cache the buffers and associated pointers so they don't get collected too early added CreatureCaste, TileColor, and CreatureType classes --- library/python/pydfhack/dftypes.py | 187 +++++++++++++++++++++-------- 1 file changed, 134 insertions(+), 53 deletions(-) diff --git a/library/python/pydfhack/dftypes.py b/library/python/pydfhack/dftypes.py index d7dddd96f..7752f17fd 100644 --- a/library/python/pydfhack/dftypes.py +++ b/library/python/pydfhack/dftypes.py @@ -1,20 +1,22 @@ from ctypes import * from flags import * from enum import * +import util 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 +def _register_callback(name, func): + ptr = c_void_p.in_dll(libdfhack, name) + ptr.value = cast(func, c_void_p).value -int_ptr = POINTER(c_int) -uint_ptr = POINTER(c_uint) +_register_callback("alloc_byte_buffer_callback", alloc_byte_buffer) +_register_callback("alloc_ubyte_buffer_callback", alloc_ubyte_buffer) +_register_callback("alloc_short_buffer_callback", alloc_short_buffer) +_register_callback("alloc_ushort_buffer_callback", alloc_ushort_buffer) +_register_callback("alloc_int_buffer_callback", alloc_int_buffer) +_register_callback("alloc_uint_buffer_callback", alloc_uint_buffer) +_register_callback("alloc_char_buffer_callback", alloc_char_buffer) _arr_create_func = CFUNCTYPE(c_void_p, c_int) @@ -70,30 +72,40 @@ class Vein(Structure): ("flags", c_uint), ("address_of", c_uint)] +_vein_ptr = POINTER(Vein) + def _alloc_vein_buffer_callback(ptr, count): - allocated = _allocate_array(Vein, count) + allocated = util._allocate_array(Vein, count) - ptr = addressof(allocated[0]) + ptr.contents = cast(addressof(allocated), _vein_ptr) + + pointer_dict[id(ptr)] = allocated return 1 -_vein_functype = CFUNCTYPE(c_int, POINTER(Vein), c_uint) -libdfhack.alloc_vein_buffer_callback = _vein_functype(_alloc_vein_buffer_callback) +_vein_functype = CFUNCTYPE(c_int, POINTER(_vein_ptr), c_uint) +_vein_func = _vein_functype(_alloc_vein_buffer_callback) +_register_callback("alloc_vein_buffer_callback", _vein_func) class FrozenLiquidVein(Structure): _fields_ = [("vtable", c_uint), ("tiles", TileTypes40d), ("address_of", c_uint)] +_frozenvein_ptr = POINTER(FrozenLiquidVein) + def _alloc_frozenliquidvein_buffer_callback(ptr, count): - allocated = _allocate_array(FrozenLiquidVein, count) + allocated = util._allocate_array(FrozenLiquidVein, count) - ptr = addressof(allocated[0]) + ptr.contents = cast(addressofallocated, _frozenvein_ptr) + + pointer_dict[id(ptr)] = allocated return 1 -_frozenliquidvein_functype = CFUNCTYPE(c_int, POINTER(FrozenLiquidVein), c_uint) -libdfhack.alloc_frozenliquidvein_buffer_callback = _frozenliquidvein_functype(_alloc_frozenliquidvein_buffer_callback) +_frozenliquidvein_functype = CFUNCTYPE(c_int, POINTER(_frozenvein_ptr), c_uint) +_frozenliquidvein_func = _frozenliquidvein_functype(_alloc_frozenliquidvein_buffer_callback) +_register_callback("alloc_frozenliquidvein_buffer_callback", _frozenliquidvein_func) class SpatterVein(Structure): _fields_ = [("vtable", c_uint), @@ -104,15 +116,20 @@ class SpatterVein(Structure): ("intensity", ((c_ubyte * 16) * 16)), ("address_of", c_uint)] +_spattervein_ptr = POINTER(SpatterVein) + def _alloc_spattervein_buffer_callback(ptr, count): - allocated = _allocate_array(SpatterVein, count) + allocated = util._allocate_array(SpatterVein, count) - ptr = addressof(allocated[0]) + ptr.contents = cast(addressof(allocated), _spattervein_ptr) + + pointer_dict[id(ptr)] = allocated return 1 -_spattervein_functype = CFUNCTYPE(c_int, POINTER(SpatterVein), c_uint) -libdfhack.alloc_spatter_buffer_callback = _spattervein_functype(_alloc_spattervein_buffer_callback) +_spattervein_functype = CFUNCTYPE(c_int, POINTER(_spattervein_ptr), c_uint) +_spattervein_func = _spattervein_functype(_alloc_spattervein_buffer_callback) +_register_callback("alloc_spattervein_buffer_callback", _spattervein_func) class GrassVein(Structure): _fields_ = [("vtable", c_uint), @@ -120,15 +137,20 @@ class GrassVein(Structure): ("intensity", ((c_ubyte * 16) * 16)), ("address_of", c_uint)] +_grassvein_ptr = POINTER(GrassVein) + def _alloc_grassvein_buffer_callback(ptr, count): - allocated = _allocate_array(GrassVein, count) + allocated = util._allocate_array(GrassVein, count) + + ptr.contents = cast(addressof(allocated), _grassvein_ptr) - ptr = addressof(allocated[0]) + pointer_dict[id(ptr)] = allocated return 1 -_grassvein_functype = CFUNCTYPE(c_int, POINTER(GrassVein), c_uint) -libdfhack.alloc_grassvein_buffer_callback = _grassvein_functype(_alloc_grassvein_buffer_callback) +_grassvein_functype = CFUNCTYPE(c_int, POINTER(_grassvein_ptr), c_uint) +_grassvein_func = _grassvein_functype(_alloc_grassvein_buffer_callback) +_register_callback("alloc_grassvein_buffer_callback", _grassvein_func) class WorldConstruction(Structure): _fields_ = [("vtable", c_uint), @@ -136,15 +158,20 @@ class WorldConstruction(Structure): ("assignment", c_ushort * 16), ("address_of", c_uint)] +_worldconstruction_ptr = POINTER(WorldConstruction) + def _alloc_worldconstruction_buffer_callback(ptr, count): - allocated = _allocate_array(WorldConstruction, count) + allocated = util._allocate_array(WorldConstruction, count) + + ptr.contents = cast(addressof(allocated), _worldconstruction_ptr) - ptr = addressof(allocated[0]) + pointer_dict[id(ptr)] = allocated return 1 -_worldconstruction_functype = CFUNCTYPE(c_int, POINTER(WorldConstruction), c_uint) -libdfhack.alloc_worldconstruction_buffer_callback = _worldconstruction_functype(_alloc_worldconstructrion_buffer_callback) +_worldconstruction_functype = CFUNCTYPE(c_int, POINTER(_worldconstruction_ptr), c_uint) +_worldconstruction_func = _worldconstruction_functype(_alloc_worldconstruction_buffer_callback) +_register_callback("alloc_worldconstruction_buffer_callback", _worldconstruction_func) class MapBlock40d(Structure): _fields_ = [("tiletypes", TileTypes40d), @@ -167,15 +194,22 @@ class Matgloss(Structure): ("bright", c_byte), ("name", c_char * 128)] -def _alloc_matgloss_buffer_callback(ptr, count): - allocated = _allocate_array(Matgloss, count) - - ptr = addressof(allocated[0]) +_matgloss_ptr = POINTER(Matgloss) +def _alloc_matgloss_buffer_callback(ptr, count): + allocated = util._allocate_array(Matgloss, count) + + p = cast(allocated, _matgloss_ptr) + + ptr[0] = p + + pointer_dict[id(ptr[0])] = (ptr, allocated, p) + return 1 -_matgloss_functype = CFUNCTYPE(c_int, POINTER(Matgloss), c_uint) -libdfhack.alloc_matgloss_buffer_callback = _matgloss_functype(_alloc_matgloss_buffer_callback) +_matgloss_functype = CFUNCTYPE(c_int, POINTER(_matgloss_ptr), c_uint) +_matgloss_func = _matgloss_functype(_alloc_matgloss_buffer_callback) +_register_callback("alloc_matgloss_buffer_callback", _matgloss_func) class MatglossPair(Structure): _fields_ = [("type", c_short), @@ -189,27 +223,37 @@ class DescriptorColor(Structure): ("name", c_char * 128)] def _alloc_descriptor_buffer_callback(ptr, count): - allocated = _allocate_array(DescriptorColor, count) + allocated = util._allocate_array(DescriptorColor, count) + + p = cast(allocated, POINTER(DescriptorColor)) - ptr = addressof(allocated[0]) + ptr[0] = p + + pointer_dict[id(ptr[0])] = (ptr, allocated, p) return 1 _descriptor_functype = CFUNCTYPE(c_int, POINTER(DescriptorColor), c_uint) -libdfhack.alloc_descriptor_buffer_callback = _descriptor_functype(_alloc_descriptor_buffer_callback) +_descriptor_func = _descriptor_functype(_alloc_descriptor_buffer_callback) +_register_callback("alloc_descriptor_buffer_callback", _descriptor_func) class MatglossOther(Structure): _fields_ = [("rawname", c_char * 128)] def _alloc_matgloss_other_buffer_callback(count): - allocated = _allocate_array(MatglossOther, count) + allocated = util._allocate_array(MatglossOther, count) + + p = cast(allocated, POINTER(MatglossOther)) - ptr = addressof(allocated[0]) + ptr[0] = p + + pointer_dict[id(ptr[0])] = (ptr, allocated, p) return 1 _matgloss_other_functype = CFUNCTYPE(c_int, POINTER(MatglossOther), c_uint) -libdfhack.alloc_matgloss_other_buffer_callback = _matgloss_other_functype(_alloc_matgloss_other_buffer_callback) +_matgloss_other_func = _matgloss_other_functype(_alloc_matgloss_other_buffer_callback) +_register_callback("alloc_matgloss_other_buffer_callback", _matgloss_other_func) class Building(Structure): _fields_ = [("origin", c_uint), @@ -227,14 +271,19 @@ class CustomWorkshop(Structure): ("name", c_char * 256)] def _alloc_custom_workshop_buffer_callback(count): - allocated = _allocate_array(CustomWorkshop, count) + allocated = util._allocate_array(CustomWorkshop, count) + + p = cast(allocated, POINTER(CustomWorkshop)) - ptr = addressof(allocated[0]) + ptr[0] = p + + pointer_dict[id(ptr[0])] = (ptr, allocated, p) return 1 _custom_workshop_functype = CFUNCTYPE(c_int, POINTER(CustomWorkshop), c_uint) -libdfhack.alloc_t_customWorkshop_buffer_callback = _custom_workshop_functype(_alloc_custom_workshop_buffer_callback) +_custom_workshop_func = _custom_workshop_functype(_alloc_custom_workshop_buffer_callback) +_register_callback("alloc_t_customWorkshop_buffer_callback", _custom_workshop_func) class Construction(Structure): _fields_ = [("x", c_ushort), @@ -266,14 +315,19 @@ class Material(Structure): ("flags", c_uint)] def _alloc_material_buffer_callback(count): - allocated = _allocate_array(Material, count) + allocated = util._allocate_array(Material, count) + + p = cast(allocated, POINTER(Material)) - ptr = addressof(allocated[0]) + ptr[0] = p + + pointer_dict[id(ptr[0])] = (ptr, allocated, p) return 1 _material_functype = CFUNCTYPE(c_int, POINTER(Material), c_uint) -libdfhack.alloc_t_material_buffer_callback = _material_functype(_alloc_material_buffer_callback) +_material_func = _material_functype(_alloc_material_buffer_callback) +_register_callback("alloc_t_material_buffer_callback", _material_func) class Skill(Structure): _fields_ = [("id", c_uint), @@ -385,7 +439,7 @@ class Creature(Structure): ("defaultSoul", Soul), ("nbcolors", c_uint), ("color", (c_uint * _MAX_COLORS)), - ("birth_year", c_uint), + ("birth_year", c_int), ("birth_time", c_uint)] class CreatureExtract(Structure): @@ -406,15 +460,42 @@ class ColorModifier(Structure): self.part[0] = '\0' self.colorlistLength = 0 -ColorModifierPtr = POINTER(ColorModifier) - def _alloc_empty_colormodifier_callback(ptr): - ptr = ColorModifierPtr(ColorModifier()) + allocated = ColorModifier() + + p = cast(allocated, POINTER(ColorModifier)) + + ptr[0] = p + + pointer_dict[id(ptr[0])] = (ptr, allocated, p) return 1 -_empty_colormodifier_functype = CFUNCTYPE(c_int, ColorModifierPtr) -libdfhack.alloc_empty_colormodifier_callback = _empty_colormodifier_functype(_alloc_empty_colormodifier_callback) +_empty_colormodifier_functype = CFUNCTYPE(c_int, POINTER(ColorModifier)) +_empty_colormodifier_func = _empty_colormodifier_functype(_alloc_empty_colormodifier_callback) +_register_callback("alloc_empty_colormodifier_callback", _empty_colormodifier_func) + +class CreatureCaste(Structure): + _fields_ = [("rawname", (c_char * 128)), + ("singular", (c_char * 128)), + ("plural", (c_char * 128)), + ("adjective", (c_char * 128)), + ("color_modifier", POINTER(ColorModifier)), + ("color_modifier_length", c_uint), + ("bodypart", POINTER(BodyPart)), + ("bodypart_length", c_uint)] + +class TileColor(Structure): + _fields_ = [("fore", c_ushort), + ("back", c_ushort), + ("bright", c_ushort)] + +class CreatureType(Structure): + _fields_ = [("rawname", (c_char * 128)), + ("castes", POINTER(CreatureCaste)), + ("castes_count", c_uint), + ("tile_character", c_ubyte), + ("tilecolor", TileColor)] class GameModes(Structure): _fields_ = [("control_mode", c_ubyte), From 828df5fa8045a61c10a8fefec1b3a2235b66c31d Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 9 Mar 2011 14:20:34 -0600 Subject: [PATCH 06/13] consolidated buffer stuff, so that the callbacks are a lot shorter --- library/python/pydfhack/dftypes.py | 90 ++++-------------------------- library/python/pydfhack/util.py | 60 +++++--------------- 2 files changed, 23 insertions(+), 127 deletions(-) diff --git a/library/python/pydfhack/dftypes.py b/library/python/pydfhack/dftypes.py index 7752f17fd..838bb357b 100644 --- a/library/python/pydfhack/dftypes.py +++ b/library/python/pydfhack/dftypes.py @@ -75,13 +75,7 @@ class Vein(Structure): _vein_ptr = POINTER(Vein) def _alloc_vein_buffer_callback(ptr, count): - allocated = util._allocate_array(Vein, count) - - ptr.contents = cast(addressof(allocated), _vein_ptr) - - pointer_dict[id(ptr)] = allocated - - return 1 + return util._allocate_array(ptr, Vein, count) _vein_functype = CFUNCTYPE(c_int, POINTER(_vein_ptr), c_uint) _vein_func = _vein_functype(_alloc_vein_buffer_callback) @@ -95,13 +89,7 @@ class FrozenLiquidVein(Structure): _frozenvein_ptr = POINTER(FrozenLiquidVein) def _alloc_frozenliquidvein_buffer_callback(ptr, count): - allocated = util._allocate_array(FrozenLiquidVein, count) - - ptr.contents = cast(addressofallocated, _frozenvein_ptr) - - pointer_dict[id(ptr)] = allocated - - return 1 + return util._allocate_array(ptr, FrozenLiquidVein, count) _frozenliquidvein_functype = CFUNCTYPE(c_int, POINTER(_frozenvein_ptr), c_uint) _frozenliquidvein_func = _frozenliquidvein_functype(_alloc_frozenliquidvein_buffer_callback) @@ -119,13 +107,7 @@ class SpatterVein(Structure): _spattervein_ptr = POINTER(SpatterVein) def _alloc_spattervein_buffer_callback(ptr, count): - allocated = util._allocate_array(SpatterVein, count) - - ptr.contents = cast(addressof(allocated), _spattervein_ptr) - - pointer_dict[id(ptr)] = allocated - - return 1 + return util._allocate_array(ptr, SpatterVein, count) _spattervein_functype = CFUNCTYPE(c_int, POINTER(_spattervein_ptr), c_uint) _spattervein_func = _spattervein_functype(_alloc_spattervein_buffer_callback) @@ -140,13 +122,7 @@ class GrassVein(Structure): _grassvein_ptr = POINTER(GrassVein) def _alloc_grassvein_buffer_callback(ptr, count): - allocated = util._allocate_array(GrassVein, count) - - ptr.contents = cast(addressof(allocated), _grassvein_ptr) - - pointer_dict[id(ptr)] = allocated - - return 1 + return util._allocate_array(ptr, GrassVein, count) _grassvein_functype = CFUNCTYPE(c_int, POINTER(_grassvein_ptr), c_uint) _grassvein_func = _grassvein_functype(_alloc_grassvein_buffer_callback) @@ -161,13 +137,7 @@ class WorldConstruction(Structure): _worldconstruction_ptr = POINTER(WorldConstruction) def _alloc_worldconstruction_buffer_callback(ptr, count): - allocated = util._allocate_array(WorldConstruction, count) - - ptr.contents = cast(addressof(allocated), _worldconstruction_ptr) - - pointer_dict[id(ptr)] = allocated - - return 1 + return util._allocate_array(ptr, WorldConstruction, count) _worldconstruction_functype = CFUNCTYPE(c_int, POINTER(_worldconstruction_ptr), c_uint) _worldconstruction_func = _worldconstruction_functype(_alloc_worldconstruction_buffer_callback) @@ -197,15 +167,7 @@ class Matgloss(Structure): _matgloss_ptr = POINTER(Matgloss) def _alloc_matgloss_buffer_callback(ptr, count): - allocated = util._allocate_array(Matgloss, count) - - p = cast(allocated, _matgloss_ptr) - - ptr[0] = p - - pointer_dict[id(ptr[0])] = (ptr, allocated, p) - - return 1 + return util._allocate_array(ptr, Matgloss, count) _matgloss_functype = CFUNCTYPE(c_int, POINTER(_matgloss_ptr), c_uint) _matgloss_func = _matgloss_functype(_alloc_matgloss_buffer_callback) @@ -223,15 +185,7 @@ class DescriptorColor(Structure): ("name", c_char * 128)] def _alloc_descriptor_buffer_callback(ptr, count): - allocated = util._allocate_array(DescriptorColor, count) - - p = cast(allocated, POINTER(DescriptorColor)) - - ptr[0] = p - - pointer_dict[id(ptr[0])] = (ptr, allocated, p) - - return 1 + return util._allocate_array(ptr, DescriptorColor, count) _descriptor_functype = CFUNCTYPE(c_int, POINTER(DescriptorColor), c_uint) _descriptor_func = _descriptor_functype(_alloc_descriptor_buffer_callback) @@ -241,15 +195,7 @@ class MatglossOther(Structure): _fields_ = [("rawname", c_char * 128)] def _alloc_matgloss_other_buffer_callback(count): - allocated = util._allocate_array(MatglossOther, count) - - p = cast(allocated, POINTER(MatglossOther)) - - ptr[0] = p - - pointer_dict[id(ptr[0])] = (ptr, allocated, p) - - return 1 + return util._allocate_array(ptr, MatglossOther, count) _matgloss_other_functype = CFUNCTYPE(c_int, POINTER(MatglossOther), c_uint) _matgloss_other_func = _matgloss_other_functype(_alloc_matgloss_other_buffer_callback) @@ -271,15 +217,7 @@ class CustomWorkshop(Structure): ("name", c_char * 256)] def _alloc_custom_workshop_buffer_callback(count): - allocated = util._allocate_array(CustomWorkshop, count) - - p = cast(allocated, POINTER(CustomWorkshop)) - - ptr[0] = p - - pointer_dict[id(ptr[0])] = (ptr, allocated, p) - - return 1 + return util._allocate_array(ptr, CustomWorkshop, count) _custom_workshop_functype = CFUNCTYPE(c_int, POINTER(CustomWorkshop), c_uint) _custom_workshop_func = _custom_workshop_functype(_alloc_custom_workshop_buffer_callback) @@ -315,15 +253,7 @@ class Material(Structure): ("flags", c_uint)] def _alloc_material_buffer_callback(count): - allocated = util._allocate_array(Material, count) - - p = cast(allocated, POINTER(Material)) - - ptr[0] = p - - pointer_dict[id(ptr[0])] = (ptr, allocated, p) - - return 1 + return util._allocate_array(ptr, Material, count) _material_functype = CFUNCTYPE(c_int, POINTER(Material), c_uint) _material_func = _material_functype(_alloc_material_buffer_callback) diff --git a/library/python/pydfhack/util.py b/library/python/pydfhack/util.py index 38365cbf5..6e7c1a331 100644 --- a/library/python/pydfhack/util.py +++ b/library/python/pydfhack/util.py @@ -14,83 +14,49 @@ pointer_dict = {} def _uintify(x, y, z): return (c_uint(x), c_uint(y), c_uint(z)) -def _allocate_array(t_type, count): - arr_type = t_type * count - - arr = arr_type() - - return arr - -def _alloc_int_buffer(ptr, count): - a = _allocate_array(c_int, count) +def _allocate_array(ptr, t_type, count): + arr = (t_type * count)() + + p = cast(arr, POINTER(t_type)) - p = cast(a, int_ptr) - ptr[0] = p - pointer_dict[id(ptr[0])] = (ptr, a, p) + pointer_dict[id(ptr[0])] = (ptr, arr, p) return 1 +def _alloc_int_buffer(ptr, count): + return _allocate_array(ptr, c_int, count) + _int_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_int)), c_uint) alloc_int_buffer = _int_functype(_alloc_int_buffer) def _alloc_uint_buffer(ptr, count): - a = _allocate_array(c_uint, count) - - ptr[0] = cast(a, uint_ptr) - - pointer_dict[id(ptr[0])] = (ptr, a, p) - - return 1 + return _allocate_array(ptr, c_uint, count) _uint_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_uint)), c_uint) alloc_uint_buffer = _uint_functype(_alloc_uint_buffer) def _alloc_short_buffer(ptr, count): - a = _allocate_array(c_short, count) - - ptr[0] = cast(a, short_ptr) - - pointer_dict[id(ptr[0])] = (ptr, a, p) - - return 1 + return _allocate_array(ptr, c_short, count) _short_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_short)), c_uint) alloc_short_buffer = _short_functype(_alloc_short_buffer) def _alloc_ushort_buffer(ptr, count): - a = _allocate_array(c_ushort, count) - - ptr[0] = cast(a, ushort_ptr) - - pointer_dict[id(ptr[0])] = (ptr, a, p) - - return 1 + return _allocate_array(ptr, c_ushort, count) _ushort_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_ushort)), c_uint) alloc_ushort_buffer = _ushort_functype(_alloc_ushort_buffer) def _alloc_byte_buffer(ptr, count): - a = _allocate_array(c_byte, count) - - ptr[0] = cast(a, byte_ptr) - - pointer_dict[id(ptr[0])] = (ptr, a, p) - - return 1 + return _allocate_array(ptr, c_byte, count) _byte_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_byte)), c_uint) alloc_byte_buffer = _byte_functype(_alloc_byte_buffer) def _alloc_ubyte_buffer(ptr, count): - a = _allocate_array(c_ubyte, count) - - ptr[0] = cast(a, ubyte_ptr) - - pointer_dict[id(ptr[0])] = (ptr, a, p) - - return 1 + return _allocate_array(ptr, c_ubyte, count) _ubyte_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_ubyte)), c_uint) alloc_ubyte_buffer = _ubyte_functype(_alloc_ubyte_buffer) From 1de2efb1620af4106884c94fabd215da8c869b5b Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 9 Mar 2011 14:25:32 -0600 Subject: [PATCH 07/13] updated documentation --- library/include/dfhack-c/README_C.rst | 52 +++++++++++++++------------ 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/library/include/dfhack-c/README_C.rst b/library/include/dfhack-c/README_C.rst index 7a5b69a54..875903357 100644 --- a/library/include/dfhack-c/README_C.rst +++ b/library/include/dfhack-c/README_C.rst @@ -48,22 +48,27 @@ All of the allocators are defined in dfhack/library/include/dfhack-c/DFTypes_C.h Buffer Callback List --------------------- -- alloc_byte_buffer_callback(int8_t*, uint32_t) -- alloc_short_buffer_callback(int16_t*, uint32_t) -- alloc_int_buffer_callback(int32_t*, uint32_t) -- alloc_ubyte_buffer_callback(uint8_t*, uint32_t) -- alloc_ushort_buffer_callback(uint16_t*, uint32_t) -- alloc_uint_buffer_callback(uint32_t*, uint32_t) -- alloc_char_buffer_callback(char* uint32_t) -- alloc_matgloss_buffer_callback(t_matgloss*, uint32_t) -- alloc_descriptor_buffer_callback(t_descriptor_color*, uint32_t) -- alloc_matgloss_other_buffer_callback(t_matglossOther*, uint32_t) -- alloc_vein_buffer_callback(t_vein*, uint32_t) -- alloc_t_feature_buffer_callback(t_feature*, uint32_t) -- alloc_t_hotkey_buffer_callback(t_hotkey*, uint32_t) -- alloc_t_screen_buffer_callback(t_screen*, uint32_t) -- alloc_frozenliquidvein_buffer_callback(t_frozenliquidvein*, uint32_t) -- alloc_spattervein_buffer_callback(t_spattervein*, uint32_t) +- alloc_byte_buffer_callback(int8_t**, uint32_t) +- alloc_short_buffer_callback(int16_t**, uint32_t) +- alloc_int_buffer_callback(int32_t**, uint32_t) +- alloc_ubyte_buffer_callback(uint8_t**, uint32_t) +- alloc_ushort_buffer_callback(uint16_t**, uint32_t) +- alloc_uint_buffer_callback(uint32_t**, uint32_t) +- alloc_char_buffer_callback(char** uint32_t) +- alloc_matgloss_buffer_callback(t_matgloss**, uint32_t) +- alloc_descriptor_buffer_callback(t_descriptor_color**, uint32_t) +- alloc_matgloss_other_buffer_callback(t_matglossOther**, uint32_t) +- alloc_t_feature_buffer_callback(t_feature**, uint32_t) +- alloc_t_hotkey_buffer_callback(t_hotkey**, uint32_t) +- alloc_t_screen_buffer_callback(t_screen**, uint32_t) +- alloc_t_customWorkshop_buffer_callback(t_customWorkshop**, uint32_t) +- alloc_t_material_buffer_callback(t_material**, uint32_t) +- alloc_vein_buffer_callback(t_vein**, uint32_t) +- alloc_frozenliquidvein_buffer_callback(t_frozenliquidvein**, uint32_t) +- alloc_spattervein_buffer_callback(t_spattervein**, uint32_t) +- alloc_grassvein_buffer_callback(t_grassvein**, uint32_t) +- alloc_worldconstruction_buffer_callback(t_worldconstruction**, uint32_t) + Templates Make My Life Harder ------------------------------- @@ -96,15 +101,14 @@ The Python bindings for dfhack implement the unsigned integer allocator callback | arr_type = t_type * count | arr = arr_type() | - | ptr = c_void_p() - | ptr = addressof(arr) - | - | return (arr, ptr) + | return arr | | def _alloc_uint_buffer(ptr, count): | a = _allocate_array(c_uint, count) + | + | p = cast(a, POINTER(c_uint)) | - | ptr = addressof(a[0]) + | ptr[0] = p | | return 1 | @@ -118,7 +122,11 @@ The Python bindings for dfhack implement the unsigned integer allocator callback | | libdfhack = cdll.libdfhack | - | libdfhack.alloc_uint_buffer_callback = alloc_uint_buffer + | def _register_callback(name, func): + | ptr = c_void_p.in_dll(libdfhack, name) + | ptr.value = cast(func, c_void_p).value + | + | _register_callback("alloc_uint_buffer_callback", alloc_uint_buffer) Modules ======== From b75f5132381cfe78c4d0be5465c75e194a43e018 Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 11 Mar 2011 14:08:53 -0600 Subject: [PATCH 08/13] updated creature caste and type structs removed some (now) unneeded callbacks made a better t_creaturetype -> c_creaturetype converter --- library/DFTypes_C.cpp | 243 +++++++++++++++------------ library/include/dfhack-c/DFTypes_C.h | 76 +++++---- 2 files changed, 183 insertions(+), 136 deletions(-) diff --git a/library/DFTypes_C.cpp b/library/DFTypes_C.cpp index ce2a00448..427859cf8 100644 --- a/library/DFTypes_C.cpp +++ b/library/DFTypes_C.cpp @@ -89,17 +89,7 @@ int (*alloc_t_screen_buffer_callback)(t_screen**, uint32_t) = NULL; int (*alloc_t_customWorkshop_buffer_callback)(t_customWorkshop**, uint32_t) = NULL; int (*alloc_t_material_buffer_callback)(t_material**, uint32_t) = NULL; -int (*alloc_empty_colormodifier_callback)(c_colormodifier**) = NULL; -int (*alloc_colormodifier_callback)(c_colormodifier**, const char*, uint32_t) = NULL; -int (*alloc_colormodifier_buffer_callback)(c_colormodifier**, uint32_t) = NULL; - -int (*alloc_empty_creaturecaste_callback)(c_creaturecaste**)= NULL; -int (*alloc_creaturecaste_callback)(c_creaturecaste**, const char*, const char*, const char*, const char*, uint32_t, uint32_t) = NULL; -int (*alloc_creaturecaste_buffer_callback)(c_creaturecaste**, uint32_t) = NULL; - -int (*alloc_empty_creaturetype_callback)(c_creaturetype**) = NULL; -int (*alloc_creaturetype_callback)(c_creaturetype**, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t) = NULL; -int (*alloc_creaturetype_buffer_callback)(c_creaturetype**, uint32_t) = NULL; +int (*alloc_creaturetype_buffer_callback)(c_creaturetype**, c_creaturetype_descriptor*, uint32_t) = NULL; int (*alloc_vein_buffer_callback)(t_vein**, uint32_t) = NULL; int (*alloc_frozenliquidvein_buffer_callback)(t_frozenliquidvein**, uint32_t) = NULL; @@ -107,6 +97,7 @@ int (*alloc_spattervein_buffer_callback)(t_spattervein**, uint32_t) = NULL; int (*alloc_grassvein_buffer_callback)(t_grassvein**, uint32_t) = NULL; int (*alloc_worldconstruction_buffer_callback)(t_worldconstruction**, uint32_t) = NULL; +//int (*alloc_bodypart_buffer_callback)(t_bodypart**, uint32_t) = NULL; REG_MACRO(Byte, int8_t**, alloc_byte_buffer_callback) REG_MACRO(Short, int16_t**, alloc_short_buffer_callback) REG_MACRO(Int, int32_t**, alloc_int_buffer_callback) @@ -139,74 +130,13 @@ UNREG_MACRO(Screen, alloc_t_screen_buffer_callback) UNREG_MACRO(CustomWorkshop, alloc_t_customWorkshop_buffer_callback) UNREG_MACRO(Material, alloc_t_material_buffer_callback) -void RegisterEmptyColorModifierCallback(int (*funcptr)(c_colormodifier**)) -{ - alloc_empty_colormodifier_callback = funcptr; -} - -void RegisterNewColorModifierCallback(int (*funcptr)(c_colormodifier**, const char*, uint32_t)) -{ - alloc_colormodifier_callback = funcptr; -} - -REG_MACRO(ColorModifier, c_colormodifier**, alloc_colormodifier_buffer_callback) - -void RegisterEmptyCreatureCasteCallback(int (*funcptr)(c_creaturecaste**)) -{ - alloc_empty_creaturecaste_callback = funcptr; -} - -void UnregisterEmptyColorModifierCallback() -{ - alloc_empty_colormodifier_callback = NULL; -} - -void UnregisterNewColorModifierCallback() +void RegisterCreatureTypeBufferCallback(int (*funcptr)(c_creaturetype**, c_creaturetype_descriptor*, uint32_t)) { - alloc_colormodifier_callback = NULL; + alloc_creaturetype_buffer_callback = funcptr; } -void RegisterNewCreatureCasteCallback(int (*funcptr)(c_creaturecaste**, const char*, const char*, const char*, const char*, uint32_t, uint32_t)) -{ - alloc_creaturecaste_callback = funcptr; -} - -REG_MACRO(CreatureCaste, c_creaturecaste**, alloc_creaturecaste_buffer_callback) -UNREG_MACRO(CreatureCaste, alloc_creaturecaste_buffer_callback) - -void UnregisterEmptyCreatureCasteCallback() -{ - alloc_empty_creaturecaste_callback = NULL; -} - -void UnregisterNewCreatureCasteCallback() -{ - alloc_creaturecaste_callback = NULL; -} - -void RegisterEmptyCreatureTypeCallback(int (*funcptr)(c_creaturetype**)) -{ - alloc_empty_creaturetype_callback = funcptr; -} - -void RegisterNewCreatureTypeCallback(int (*funcptr)(c_creaturetype**, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t)) -{ - alloc_creaturetype_callback = funcptr; -} - -REG_MACRO(CreatureType, c_creaturetype**, alloc_creaturetype_buffer_callback) UNREG_MACRO(CreatureType, alloc_creaturetype_buffer_callback) -void UnregisterEmptyCreatureTypeCallback() -{ - alloc_empty_creaturetype_callback = NULL; -} - -void UnregisterNewCreatureTypeCallback() -{ - alloc_creaturetype_callback = NULL; -} - REG_MACRO(Vein, t_vein**, alloc_vein_buffer_callback) REG_MACRO(FrozenLiquidVein, t_frozenliquidvein**, alloc_frozenliquidvein_buffer_callback) REG_MACRO(SpatterVein, t_spattervein**, alloc_spattervein_buffer_callback) @@ -263,44 +193,151 @@ int DFHack_getTileType(int index, TileRow* tPtr) } #endif -int ColorListConvert(t_colormodifier* src, c_colormodifier* dest) +void BuildDescriptorList(std::vector & src, c_creaturetype_descriptor** dest) { - if(src == NULL) - return -1; - - ((*alloc_colormodifier_callback)(&dest, src->part, src->colorlist.size())); + c_creaturetype_descriptor* descriptor = NULL; + + descriptor = (c_creaturetype_descriptor*)calloc(src.size(), sizeof(c_creaturetype_descriptor)); - copy(src->colorlist.begin(), src->colorlist.end(), dest->colorlist); + for(uint32_t i = 0; i < src.size(); i++) + { + uint32_t castes_size = src[i].castes.size(); + c_creaturetype_descriptor* current = &descriptor[i]; + + current->castesCount = castes_size; + current->caste_descriptors = (c_creaturecaste_descriptor*)calloc(castes_size, sizeof(c_creaturecaste_descriptor)); + + for(uint32_t j = 0; j < castes_size; j++) + { + uint32_t color_size = src[i].castes[j].ColorModifier.size(); + c_creaturecaste_descriptor* current_caste = ¤t->caste_descriptors[j]; + + current_caste->colorModifierLength = color_size; + current_caste->color_descriptors = (c_colormodifier_descriptor*)calloc(color_size, sizeof(c_colormodifier_descriptor)); + + for(uint32_t k = 0; k < color_size; k++) + { + c_colormodifier_descriptor* current_color = ¤t_caste->color_descriptors[k]; + + current_color->colorlistLength = src[i].castes[j].ColorModifier[k].colorlist.size(); + } + + current_caste->bodypartLength = src[i].castes[j].bodypart.size(); + } + + current->extractCount = src[i].extract.size(); + } - return 1; + *dest = descriptor; } -int CreatureCasteConvert(t_creaturecaste* src, c_creaturecaste* dest) +void FreeDescriptorList(c_creaturetype_descriptor* d, uint32_t length) { - if(src == NULL) - return -1; - - ((*alloc_creaturecaste_callback)(&dest, src->rawname, src->singular, src->plural, src->adjective, src->colorModifier.size(), src->bodypart.size())); + for(uint32_t i = 0; i < length; i++) + { + c_creaturetype_descriptor* desc = &d[i]; + + for(uint32_t j = 0; j < desc->castesCount; j++) + free(desc->caste_descriptors[j].color_descriptors); + + free(desc->caste_descriptors); + } - for(unsigned int i = 0; i < dest->colorModifierLength; i++) - ColorListConvert(&src->colorModifier[i], &(dest->colorModifier[i])); - - copy(src->bodypart.begin(), src->bodypart.end(), dest->bodypart); - - return 1; + free(d); } -int CreatureTypeConvert(t_creaturetype* src, c_creaturetype* dest) +int CreatureTypeConvert(std::vector & src, c_creaturetype** out_buf) { - if(src == NULL) + if(src.size() <= 0) + return 0; + else if(alloc_creaturetype_buffer_callback == NULL) return -1; - - ((*alloc_creaturetype_callback)(&dest, src->rawname, src->castes.size(), src->extract.size(), src->tile_character, src->tilecolor.fore, src->tilecolor.back, src->tilecolor.bright)); - - for(unsigned int i = 0; i < dest->castesCount; i++) - CreatureCasteConvert(&src->castes[i], &dest->castes[i]); - - copy(src->extract.begin(), src->extract.end(), dest->extract); - - return 1; + else + { + c_creaturetype_descriptor* descriptor; + c_creaturetype* buf; + + BuildDescriptorList(src, &descriptor); + + ((*alloc_creaturetype_buffer_callback)(out_buf, descriptor, src.size())); + + FreeDescriptorList(descriptor, src.size()); + + if(out_buf == NULL) + return -1; + + buf = out_buf[0]; + + for(uint32_t i = 0; i < src.size(); i++) + { + c_creaturetype* current = &(buf[i]); + + memset(current->rawname, '\0', 128); + strncpy(current->rawname, src[i].rawname, 128); + + for(uint32_t j = 0; j < current->castesCount; j++) + { + c_creaturecaste* current_caste = ¤t->castes[j]; + t_creaturecaste* src_caste = &src[i].castes[j]; + + memset(current_caste->rawname, '\0', 128); + memset(current_caste->singular, '\0', 128); + memset(current_caste->plural, '\0', 128); + memset(current_caste->adjective, '\0', 128); + + strncpy(current_caste->rawname, src_caste->rawname, 128); + strncpy(current_caste->singular, src_caste->singular, 128); + strncpy(current_caste->plural, src_caste->plural, 128); + strncpy(current_caste->adjective, src_caste->adjective, 128); + + for(uint32_t k = 0; k < src[i].castes[j].ColorModifier.size(); k++) + { + c_colormodifier* current_color = ¤t_caste->colorModifier[k]; + + memset(current_color->part, '\0', 128); + strncpy(current_color->part, src_caste->ColorModifier[k].part, 128); + + copy(src_caste->ColorModifier[k].colorlist.begin(), src_caste->ColorModifier[k].colorlist.end(), current_color->colorlist); + current_color->colorlistLength = src_caste->ColorModifier[k].colorlist.size(); + + current_color->startdate = src_caste->ColorModifier[k].startdate; + current_color->enddate = src_caste->ColorModifier[k].enddate; + } + + current_caste->colorModifierLength = src_caste->ColorModifier.size(); + + copy(src_caste->bodypart.begin(), src_caste->bodypart.end(), current_caste->bodypart); + current_caste->bodypartLength = src_caste->bodypart.size(); + + current_caste->strength = src_caste->strength; + current_caste->agility = src_caste->agility; + current_caste->toughness = src_caste->toughness; + current_caste->endurance = src_caste->endurance; + current_caste->recuperation = src_caste->recuperation; + current_caste->disease_resistance = src_caste->disease_resistance; + current_caste->analytical_ability = src_caste->analytical_ability; + current_caste->focus = src_caste->focus; + current_caste->willpower = src_caste->willpower; + current_caste->creativity = src_caste->creativity; + current_caste->intuition = src_caste->intuition; + current_caste->patience = src_caste->patience; + current_caste->memory = src_caste->memory; + current_caste->linguistic_ability = src_caste->linguistic_ability; + current_caste->spatial_sense = src_caste->spatial_sense; + current_caste->musicality = src_caste->musicality; + current_caste->kinesthetic_sense = src_caste->kinesthetic_sense; + } + + copy(src[i].extract.begin(), src[i].extract.end(), current->extract); + current->extractCount = src[i].extract.size(); + + current->tile_character = src[i].tile_character; + + current->tilecolor.fore = src[i].tilecolor.fore; + current->tilecolor.back = src[i].tilecolor.back; + current->tilecolor.bright = src[i].tilecolor.bright; + } + + return 1; + } } diff --git a/library/include/dfhack-c/DFTypes_C.h b/library/include/dfhack-c/DFTypes_C.h index 8832ea3d8..67aebf96d 100644 --- a/library/include/dfhack-c/DFTypes_C.h +++ b/library/include/dfhack-c/DFTypes_C.h @@ -114,19 +114,14 @@ struct c_colormodifier char part[128]; uint32_t* colorlist; uint32_t colorlistLength; + uint32_t startdate; + uint32_t enddate; }; -DFHACK_EXPORT extern int (*alloc_empty_colormodifier_callback)(c_colormodifier**); -DFHACK_EXPORT extern int (*alloc_colormodifier_callback)(c_colormodifier**, const char*, uint32_t); -DFHACK_EXPORT extern int (*alloc_colormodifier_buffer_callback)(c_colormodifier**, uint32_t); - -DFHACK_EXPORT void RegisterEmptyColorModifierCallback(int (*funcptr)(c_colormodifier**)); -DFHACK_EXPORT void RegisterNewColorModifierCallback(int (*funcptr)(c_colormodifier**, const char*, uint32_t)); -DFHACK_EXPORT void RegisterColorModifierBufferCallback(int (*funcptr)(c_colormodifier**, uint32_t)); - -DFHACK_EXPORT void UnregisterEmptyColorModifierCallback(); -DFHACK_EXPORT void UnregisterNewColorModifierCallback(); -HUNREG_MACRO(ColorModifier) +struct c_colormodifier_descriptor +{ + uint32_t colorlistLength; +}; struct c_creaturecaste { @@ -140,19 +135,32 @@ struct c_creaturecaste t_bodypart* bodypart; uint32_t bodypartLength; + + t_attrib strength; + t_attrib agility; + t_attrib toughness; + t_attrib endurance; + t_attrib recuperation; + t_attrib disease_resistance; + t_attrib analytical_ability; + t_attrib focus; + t_attrib willpower; + t_attrib creativity; + t_attrib intuition; + t_attrib patience; + t_attrib memory; + t_attrib linguistic_ability; + t_attrib spatial_sense; + t_attrib musicality; + t_attrib kinesthetic_sense; }; -DFHACK_EXPORT extern int (*alloc_empty_creaturecaste_callback)(c_creaturecaste**); -DFHACK_EXPORT extern int (*alloc_creaturecaste_callback)(c_creaturecaste**, const char*, const char*, const char*, const char*, uint32_t, uint32_t); -DFHACK_EXPORT extern int (*alloc_creaturecaste_buffer_callback)(c_creaturecaste**, uint32_t); - -DFHACK_EXPORT void RegisterEmptyCreatureCasteCallback(int (*funcptr)(c_creaturecaste**)); -DFHACK_EXPORT void RegisterNewCreatureCasteCallback(int (*funcptr)(c_creaturecaste**, const char*, const char*, const char*, const char*, uint32_t, uint32_t)); -DFHACK_EXPORT void RegisterCreatureCasteBufferCallback(int (*funcptr)(c_creaturecaste**, uint32_t)); - -DFHACK_EXPORT void UnregisterEmptyCreatureCasteCallback(); -DFHACK_EXPORT void UnregisterNewCreatureCasteCallback(); -HUNREG_MACRO(CreatureCaste) +struct c_creaturecaste_descriptor +{ + c_colormodifier_descriptor* color_descriptors; + uint32_t colorModifierLength; + uint32_t bodypartLength; +}; struct c_creaturetype { @@ -174,9 +182,14 @@ struct c_creaturetype } tilecolor; }; -DFHACK_EXPORT extern int (*alloc_empty_creaturetype_callback)(c_creaturetype**); -DFHACK_EXPORT extern int (*alloc_creaturetype_callback)(c_creaturetype**, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t); -DFHACK_EXPORT extern int (*alloc_creaturetype_buffer_callback)(c_creaturetype**, uint32_t); +struct c_creaturetype_descriptor +{ + c_creaturecaste_descriptor* caste_descriptors; + uint32_t castesCount; + uint32_t extractCount; +}; + +DFHACK_EXPORT extern int (*alloc_creaturetype_buffer_callback)(c_creaturetype**, c_creaturetype_descriptor*, uint32_t); DFHACK_EXPORT extern int (*alloc_vein_buffer_callback)(t_vein**, uint32_t); DFHACK_EXPORT extern int (*alloc_frozenliquidvein_buffer_callback)(t_frozenliquidvein**, uint32_t); @@ -184,9 +197,7 @@ DFHACK_EXPORT extern int (*alloc_spattervein_buffer_callback)(t_spattervein**, u DFHACK_EXPORT extern int (*alloc_grassvein_buffer_callback)(t_grassvein**, uint32_t); DFHACK_EXPORT extern int (*alloc_worldconstruction_buffer_callback)(t_worldconstruction**, uint32_t); -DFHACK_EXPORT void RegisterEmptyCreatureTypeCallback(int (*funcptr)(c_creaturetype**)); -DFHACK_EXPORT void RegisterNewCreatureTypeCallback(int (*funcptr)(c_creaturetype**, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t)); -DFHACK_EXPORT void RegisterCreatureTypeBufferCallback(int (*funcptr)(c_creaturetype**, uint32_t)); +DFHACK_EXPORT void RegisterCreatureTypeBufferCallback(int (*funcptr)(c_creaturetype**, c_creaturetype_descriptor*, uint32_t)); DFHACK_EXPORT void RegisterVeinBufferCallback(int (*funcptr)(t_vein**, uint32_t)); DFHACK_EXPORT void RegisterFrozenLiquidVeinBufferCallback(int (*funcptr)(t_frozenliquidvein**, uint32_t)); @@ -194,8 +205,6 @@ DFHACK_EXPORT void RegisterSpatterVeinBufferCallback(int (*funcptr)(t_spattervei DFHACK_EXPORT void RegisterGrassVeinBufferCallback(int (*funcptr)(t_grassvein**, uint32_t)); DFHACK_EXPORT void RegisterWorldConstructionBufferCallback(int (*funcptr)(t_worldconstruction**, uint32_t)); -DFHACK_EXPORT void UnregisterEmptyCreatureTypeCallback(); -DFHACK_EXPORT void UnregisterNewCreatureTypeCallback(); HUNREG_MACRO(CreatureType) HUNREG_MACRO(Vein) @@ -217,8 +226,9 @@ DFHACK_EXPORT int DFHack_getTileType(int index, TileRow* tPtr); } #endif -int CreatureTypeConvert(t_creaturetype* src, c_creaturetype* dest); -int CreatureCasteConvert(t_creaturecaste* src, c_creaturecaste* dest); -int ColorListConvert(t_colormodifier* src, c_colormodifier* dest); +void BuildDescriptorList(std::vector & src, c_creaturetype_descriptor** dest); +void FreeDescriptorList(c_creaturetype_descriptor* d, uint32_t length); + +int CreatureTypeConvert(std::vector &, c_creaturetype**); #endif From 49b7f8acb5989e05c7e85bfa015982d59a0dc2ff Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 11 Mar 2011 14:10:22 -0600 Subject: [PATCH 09/13] fixed a couple of bad return values --- library/modules/Maps_C.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/modules/Maps_C.cpp b/library/modules/Maps_C.cpp index 059f69503..09260de17 100644 --- a/library/modules/Maps_C.cpp +++ b/library/modules/Maps_C.cpp @@ -587,7 +587,7 @@ int Maps_ReadAllVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, c_ ((*alloc_grassvein_buffer_callback)(&gs_buf, grass_veins.size())); if(gs_buf == NULL) - return NULL; + return -1; copy(grass_veins.begin(), grass_veins.end(), gs_buf); @@ -599,7 +599,7 @@ int Maps_ReadAllVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, c_ ((*alloc_worldconstruction_buffer_callback)(&ct_buf, constructions.size())); if(ct_buf == NULL) - return NULL; + return -1; copy(constructions.begin(), constructions.end(), ct_buf); From b9e8aa8952d13596191a3294423b99065c404a38 Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 11 Mar 2011 14:10:51 -0600 Subject: [PATCH 10/13] Materials_getRaceEx finally works as expected --- library/modules/Materials_C.cpp | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/library/modules/Materials_C.cpp b/library/modules/Materials_C.cpp index c334e5595..20642888f 100644 --- a/library/modules/Materials_C.cpp +++ b/library/modules/Materials_C.cpp @@ -395,18 +395,9 @@ c_creaturetype* Materials_getRaceEx(DFHackObject* mat) { c_creaturetype* buf = NULL; - if(alloc_creaturetype_buffer_callback == NULL) - return NULL; - - ((*alloc_creaturetype_buffer_callback)(&buf, matSize)); + CreatureTypeConvert(((DFHack::Materials*)mat)->raceEx, &buf); - if(buf != NULL) - { - for(int i = 0; i < matSize; i++) - CreatureTypeConvert(&materials->raceEx[i], &(buf[i])); - - return buf; - } + return buf; } } From 5b29bf52800e07d99aefd3be278e3812bd9ce911 Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 11 Mar 2011 14:11:36 -0600 Subject: [PATCH 11/13] changed pointer cache to use array addresses instead of pointer ids --- library/python/pydfhack/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/python/pydfhack/util.py b/library/python/pydfhack/util.py index 6e7c1a331..e9485052d 100644 --- a/library/python/pydfhack/util.py +++ b/library/python/pydfhack/util.py @@ -21,7 +21,7 @@ def _allocate_array(ptr, t_type, count): ptr[0] = p - pointer_dict[id(ptr[0])] = (ptr, arr, p) + pointer_dict[addressof(arr)] = (ptr, arr, p) return 1 From f6a7de9ba61f9a5dbde2318449b307a4576f7191 Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 11 Mar 2011 14:12:07 -0600 Subject: [PATCH 12/13] updated getters to use the changed pointer caching stuff --- library/python/pydfhack/materials.py | 66 ++++++++++++++-------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/library/python/pydfhack/materials.py b/library/python/pydfhack/materials.py index 466bb68ae..d0bfc3ddd 100644 --- a/library/python/pydfhack/materials.py +++ b/library/python/pydfhack/materials.py @@ -2,15 +2,15 @@ from ctypes import * import dftypes from dftypes import libdfhack, Matgloss, CreatureType, DescriptorColor, MatglossOther -libdfhack.Materials_getInorganic.restype = POINTER(Matgloss) -libdfhack.Materials_getOrganic.restype = POINTER(Matgloss) -libdfhack.Materials_getTree.restype = POINTER(Matgloss) -libdfhack.Materials_getPlant.restype = POINTER(Matgloss) -libdfhack.Materials_getRace.restype = POINTER(Matgloss) -libdfhack.Materials_getRaceEx.restype = POINTER(CreatureType) -libdfhack.Materials_getColor.restype = POINTER(DescriptorColor) -libdfhack.Materials_getOther.restype = POINTER(MatglossOther) -libdfhack.Materials_getAllDesc.restype = POINTER(Matgloss) +libdfhack.Materials_getInorganic.restype = c_void_p +libdfhack.Materials_getOrganic.restype = c_void_p +libdfhack.Materials_getTree.restype = c_void_p +libdfhack.Materials_getPlant.restype = c_void_p +libdfhack.Materials_getRace.restype = c_void_p +libdfhack.Materials_getRaceEx.restype = c_void_p +libdfhack.Materials_getColor.restype = c_void_p +libdfhack.Materials_getOther.restype = c_void_p +libdfhack.Materials_getAllDesc.restype = c_void_p class Materials(object): def __init__(self, ptr): @@ -28,58 +28,58 @@ class Materials(object): def _get_inorganic(self): inorganic = libdfhack.Materials_getInorganic(self._mat_ptr) - if id(inorganic) in dftypes.pointer_dict: - self.inorganic = dftypes.pointer_dict[id(inorganic)][1] - del dftypes.pointer_dict[id(inorganic)] + if inorganic in dftypes.pointer_dict: + self.inorganic = [i for i in dftypes.pointer_dict[inorganic][1]] + del dftypes.pointer_dict[inorganic] def _get_organic(self): organic = libdfhack.Materials_getOrganic(self._mat_ptr) - if id(organic) in dftypes.pointer_dict: - self.organic = dftypes.pointer_dict[id(organic)][1] - del dftypes.pointer_dict[id(inorganic)] + if organic in dftypes.pointer_dict: + self.organic = [i for i in dftypes.pointer_dict[organic][1]] + del dftypes.pointer_dict[organic] def _get_tree(self): tree = libdfhack.Materials_getTree(self._mat_ptr) - if id(tree) in dftypes.pointer_dict: - self.tree = dftypes.pointer_dict[id(tree)][1] - del dftypes.pointer_dict[id(tree)] + if tree in dftypes.pointer_dict: + self.tree = [i for i in dftypes.pointer_dict[tree][1]] + del dftypes.pointer_dict[tree] def _get_plant(self): plant = libdfhack.Materials_getPlant(self._mat_ptr) - if id(plant) in dftypes.pointer_dict: - self.tree = dftypes.pointer_dict[id(tree)][1] - del dftypes.pointer_dict[id(tree)] + if plant in dftypes.pointer_dict: + self.plant = [i for i in dftypes.pointer_dict[plant][1]] + del dftypes.pointer_dict[plant] def _get_race(self): race = libdfhack.Materials_getRace(self._mat_ptr) - if id(race) in dftypes.pointer_dict: - self.race = dftypes.pointer_dict[id(race)][1] - del dftypes.pointer_dict[id(race)] + if race in dftypes.pointer_dict: + self.race = [i for i in dftypes.pointer_dict[race][1]] + del dftypes.pointer_dict[race] def _get_race_ex(self): race_ex = libdfhack.Materials_getRaceEx(self._mat_ptr) - if id(race_ex) in dftypes.pointer_dict: - self.race_ex = dftypes.pointer_dict[id(race_ex)][1] - del dftypes.pointer_dict[id(race_ex)] + if race_ex in dftypes.pointer_dict: + self.race_ex = [i for i in dftypes.pointer_dict[race_ex][1]] + del dftypes.pointer_dict[race_ex] def _get_color(self): color = libdfhack.Materials_getColor(self._mat_ptr) - if id(color) in dftypes.pointer_dict: - self.color = dftypes.pointer_dict[id(color)][1] - del dftypes.pointer_dict[id(color)] + if color in dftypes.pointer_dict: + self.color = [i for i in dftypes.pointer_dict[color][1]] + del dftypes.pointer_dict[color] def _get_other(self): other = libdfhack.Materials_getOther(self._mat_ptr) - if id(other) in dftypes.pointer_dict: - self.other = dftypes.pointer_dict[id(other)][1] - del dftypes.pointer_dict[id(other)] + if other in dftypes.pointer_dict: + self.other = [i for i in dftypes.pointer_dict[other][1]] + del dftypes.pointer_dict[other] def _get_all(self): self._get_inorganic() From 5aeac3d43dde39973186c327a1bb30baa6c35e67 Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 11 Mar 2011 14:13:52 -0600 Subject: [PATCH 13/13] updated CreatureCaste, and CreatureType structs added MatglossPlant and creature type descriptor structs got the creature type allocator callback working --- library/python/pydfhack/dftypes.py | 172 ++++++++++++++++++++++------- 1 file changed, 134 insertions(+), 38 deletions(-) diff --git a/library/python/pydfhack/dftypes.py b/library/python/pydfhack/dftypes.py index 838bb357b..326d3690e 100644 --- a/library/python/pydfhack/dftypes.py +++ b/library/python/pydfhack/dftypes.py @@ -19,6 +19,7 @@ _register_callback("alloc_uint_buffer_callback", alloc_uint_buffer) _register_callback("alloc_char_buffer_callback", alloc_char_buffer) _arr_create_func = CFUNCTYPE(c_void_p, c_int) +_dfhack_string = (c_char * 128) TileTypes40d = ((c_int * 16) * 16) BiomeIndices40d = c_ubyte * 16 @@ -158,11 +159,11 @@ class ViewScreen(Structure): _fields_ = [("type", c_int)] class Matgloss(Structure): - _fields_ = [("id", c_char * 128), + _fields_ = [("id", _dfhack_string), ("fore", c_byte), ("back", c_byte), ("bright", c_byte), - ("name", c_char * 128)] + ("name", _dfhack_string)] _matgloss_ptr = POINTER(Matgloss) @@ -178,11 +179,11 @@ class MatglossPair(Structure): ("index", c_int)] class DescriptorColor(Structure): - _fields_ = [("id", c_char * 128), + _fields_ = [("id", _dfhack_string), ("r", c_float), ("v", c_float), ("b", c_float), - ("name", c_char * 128)] + ("name", _dfhack_string)] def _alloc_descriptor_buffer_callback(ptr, count): return util._allocate_array(ptr, DescriptorColor, count) @@ -191,6 +192,16 @@ _descriptor_functype = CFUNCTYPE(c_int, POINTER(DescriptorColor), c_uint) _descriptor_func = _descriptor_functype(_alloc_descriptor_buffer_callback) _register_callback("alloc_descriptor_buffer_callback", _descriptor_func) +class MatglossPlant(Structure): + _fields_ = [("id", _dfhack_string), + ("fore", c_ubyte), + ("back", c_ubyte), + ("bright", c_ubyte), + ("name", _dfhack_string), + ("drink_name", _dfhack_string), + ("food_name", _dfhack_string), + ("extract_name", _dfhack_string)] + class MatglossOther(Structure): _fields_ = [("rawname", c_char * 128)] @@ -287,8 +298,8 @@ class Attribute(Structure): ("field_18", c_uint)] class Name(Structure): - _fields_ = [("first_name", (c_char * 128)), - ("nickname", (c_char * 128)), + _fields_ = [("first_name", _dfhack_string), + ("nickname", _dfhack_string), ("words", (c_int * 7)), ("parts_of_speech", (c_ushort * 7)), ("language", c_uint), @@ -298,7 +309,7 @@ class Note(Structure): _fields_ = [("symbol", c_char), ("foreground", c_ushort), ("background", c_ushort), - ("name", (c_char * 128)), + ("name", _dfhack_string), ("x", c_ushort), ("y", c_ushort), ("z", c_ushort)] @@ -350,7 +361,7 @@ class Creature(Structure): ("mood_skill", c_short), ("artifact_name", Name), ("profession", c_ubyte), - ("custom_profession", (c_char * 128)), + ("custom_profession", _dfhack_string), ("labors", (c_ubyte * _NUM_CREATURE_LABORS)), ("current_job", Job), ("happiness", c_uint), @@ -373,60 +384,145 @@ class Creature(Structure): ("birth_time", c_uint)] class CreatureExtract(Structure): - _fields_ = [("rawname", (c_char * 128))] + _fields_ = [("rawname", _dfhack_string)] class BodyPart(Structure): - _fields_ = [("id", (c_char * 128)), - ("category", (c_char * 128)), - ("single", (c_char * 128)), - ("plural", (c_char * 128))] + _fields_ = [("id", _dfhack_string), + ("category", _dfhack_string), + ("single", _dfhack_string), + ("plural", _dfhack_string)] class ColorModifier(Structure): - _fields_ = [("part", (c_char * 128)), + _fields_ = [("part", _dfhack_string), ("colorlist", POINTER(c_uint)), - ("colorlistLength", c_uint)] + ("colorlistLength", c_uint), + ("startdate", c_uint), + ("enddate", c_uint)] def __init__(self): - self.part[0] = '\0' self.colorlistLength = 0 -def _alloc_empty_colormodifier_callback(ptr): - allocated = ColorModifier() - - p = cast(allocated, POINTER(ColorModifier)) - - ptr[0] = p - - pointer_dict[id(ptr[0])] = (ptr, allocated, p) - - return 1 - -_empty_colormodifier_functype = CFUNCTYPE(c_int, POINTER(ColorModifier)) -_empty_colormodifier_func = _empty_colormodifier_functype(_alloc_empty_colormodifier_callback) -_register_callback("alloc_empty_colormodifier_callback", _empty_colormodifier_func) +_colormodifier_ptr = POINTER(ColorModifier) class CreatureCaste(Structure): - _fields_ = [("rawname", (c_char * 128)), - ("singular", (c_char * 128)), - ("plural", (c_char * 128)), - ("adjective", (c_char * 128)), - ("color_modifier", POINTER(ColorModifier)), + _fields_ = [("rawname", _dfhack_string), + ("singular", _dfhack_string), + ("plural", _dfhack_string), + ("adjective", _dfhack_string), + ("color_modifier", _colormodifier_ptr), ("color_modifier_length", c_uint), ("bodypart", POINTER(BodyPart)), - ("bodypart_length", c_uint)] + ("bodypart_length", c_uint), + ("strength", Attribute), + ("agility", Attribute), + ("toughness", Attribute), + ("endurance", Attribute), + ("recuperation", Attribute), + ("disease_resistance", Attribute), + ("analytical_ability", Attribute), + ("focus", Attribute), + ("willpower", Attribute), + ("creativity", Attribute), + ("intuition", Attribute), + ("patience", Attribute), + ("memory", Attribute), + ("linguistic_ability", Attribute), + ("spatial_sense", Attribute), + ("musicality", Attribute), + ("kinesthetic_sense", Attribute)] + +_creaturecaste_ptr = POINTER(CreatureCaste) class TileColor(Structure): _fields_ = [("fore", c_ushort), ("back", c_ushort), ("bright", c_ushort)] +class ColorDescriptor(Structure): + _fields_ = [("colorlistLength", c_uint)] + +class CasteDescriptor(Structure): + _fields_ = [("color_descriptors", POINTER(ColorDescriptor)), + ("colorModifierLength", c_uint), + ("bodypartLength", c_uint)] + +class CreatureTypeDescriptor(Structure): + _fields_ = [("caste_descriptors", POINTER(CasteDescriptor)), + ("castesCount", c_uint), + ("extractCount", c_uint)] + class CreatureType(Structure): - _fields_ = [("rawname", (c_char * 128)), - ("castes", POINTER(CreatureCaste)), + _fields_ = [("rawname", _dfhack_string), + ("castes", _creaturecaste_ptr), ("castes_count", c_uint), + ("extract", POINTER(CreatureExtract)), + ("extract_count", c_uint), ("tile_character", c_ubyte), ("tilecolor", TileColor)] +_creaturetype_ptr = POINTER(CreatureType) + +def _alloc_creaturetype_buffer(ptr, descriptors, count): + arr_list = [] + c_arr = (CreatureType * count)() + + for i, v in enumerate(c_arr): + v.castesCount = descriptors[i].castesCount + v_caste_arr = (CreatureCaste * v.castesCount)() + + for j, v_c in enumerate(v_caste_arr): + caste_descriptor = descriptors[i].caste_descriptors[j] + + v_c.colorModifierLength = caste_descriptor.colorModifierLength + c_color_arr = (ColorModifier * v_c.colorModifierLength)() + + for k, c_c in enumerate(c_color_arr): + color_descriptor = caste_descriptor.color_descriptors[k] + + c_c.colorlistLength = color_descriptor.colorlistLength + + c_color_list_arr = (c_uint * c_c.colorlistLength)() + + p = cast(c_color_list_arr, POINTER(c_uint)) + + c_c.colorlist = p + + arr_list.extend((c_color_list_arr, p)) + + c_p = cast(c_color_arr, POINTER(ColorModifier)) + v_c.colorModifier = c_p + + v_c.bodypartLength = caste_descriptor.bodypartLength + c_bodypart_arr = (BodyPart * v_c.bodypartLength)() + + b_p = cast(c_bodypart_arr, POINTER(BodyPart)) + v_c.bodypart = b_p + + arr_list.extend((c_color_arr, c_p, c_bodypart_arr, b_p)) + + v.extractCount = descriptors[i].extractCount + v_extract_arr = (CreatureExtract * v.extractCount)() + + caste_p = cast(v_caste_arr, POINTER(CreatureCaste)) + v.castes = caste_p + + extract_p = cast(v_extract_arr, POINTER(CreatureExtract)) + v.extract = extract_p + + arr_list.extend((v_caste_arr, caste_p, v_extract_arr, extract_p)) + + p = cast(c_arr, _creaturetype_ptr) + ptr[0] = p + print "" + + pointer_dict[addressof(c_arr)] = (ptr, c_arr, p, arr_list) + + return 1 + +_alloc_creaturetype_buffer_functype = CFUNCTYPE(c_int, POINTER(_creaturetype_ptr), POINTER(CreatureTypeDescriptor), c_uint) +_alloc_creaturetype_buffer_func = _alloc_creaturetype_buffer_functype(_alloc_creaturetype_buffer) +_register_callback("alloc_creaturetype_buffer_callback", _alloc_creaturetype_buffer_func) + class GameModes(Structure): _fields_ = [("control_mode", c_ubyte), ("game_mode", c_ubyte)]