From 1327b7236ba6e7b8d15ef65061b9b251443c0782 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 25 Aug 2010 09:20:00 -0500 Subject: [PATCH 1/8] changed extension to "rst" --- library/include/dfhack-c/{README_C.txt => README_C.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename library/include/dfhack-c/{README_C.txt => README_C.rst} (100%) diff --git a/library/include/dfhack-c/README_C.txt b/library/include/dfhack-c/README_C.rst similarity index 100% rename from library/include/dfhack-c/README_C.txt rename to library/include/dfhack-c/README_C.rst From fba11842bc393f8819068e16874f2b793f515612 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 26 Aug 2010 10:27:41 -0500 Subject: [PATCH 2/8] add allocator callbacks for t_feature, t_hotkey, and t_screen --- library/DFTypes_C.cpp | 4 ++++ library/include/dfhack-c/DFTypes_C.h | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/library/DFTypes_C.cpp b/library/DFTypes_C.cpp index 9957ef0a1..9677ae9f1 100644 --- a/library/DFTypes_C.cpp +++ b/library/DFTypes_C.cpp @@ -57,6 +57,10 @@ 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; diff --git a/library/include/dfhack-c/DFTypes_C.h b/library/include/dfhack-c/DFTypes_C.h index 112e195d4..4bb9676bf 100644 --- a/library/include/dfhack-c/DFTypes_C.h +++ b/library/include/dfhack-c/DFTypes_C.h @@ -49,6 +49,10 @@ 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); + struct t_customWorkshop { uint32_t index; From b78b0ad2e6a0d6400a6a3ea85816242da01935f7 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 26 Aug 2010 10:28:22 -0500 Subject: [PATCH 3/8] wrapped ReadMenuState --- library/include/dfhack-c/modules/Gui_C.h | 1 + library/modules/Gui_C.cpp | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/library/include/dfhack-c/modules/Gui_C.h b/library/include/dfhack-c/modules/Gui_C.h index 2373d458d..8cc737f78 100644 --- a/library/include/dfhack-c/modules/Gui_C.h +++ b/library/include/dfhack-c/modules/Gui_C.h @@ -38,6 +38,7 @@ DFHACK_EXPORT int Gui_Finish(DFHackObject* gui); DFHACK_EXPORT int Gui_ReadPauseState(DFHackObject* gui); DFHACK_EXPORT int Gui_ReadViewScreen(DFHackObject* gui, t_viewscreen* viewscreen); +DFHACK_EXPORT int Gui_ReadMenuState(DFHackObject* gui, uint32_t* menuState); #ifdef __cplusplus } diff --git a/library/modules/Gui_C.cpp b/library/modules/Gui_C.cpp index da17d2a18..8870b4e87 100644 --- a/library/modules/Gui_C.cpp +++ b/library/modules/Gui_C.cpp @@ -60,8 +60,6 @@ int Gui_ReadPauseState(DFHackObject* gui) int Gui_ReadViewScreen(DFHackObject* gui, t_viewscreen* viewscreen) { - //int result; - if(gui != NULL) { return ((DFHack::Gui*)gui)->ReadViewScreen(*viewscreen); @@ -70,6 +68,18 @@ int Gui_ReadViewScreen(DFHackObject* gui, t_viewscreen* viewscreen) return -1; } +int Gui_ReadMenuState(DFHackObject* gui, uint32_t* menuState) +{ + if(gui != NULL) + { + *menuState = ((DFHack::Gui*)gui)->ReadMenuState(); + + return 1; + } + + return -1; +} + #ifdef __cplusplus } #endif From cfa39dde7fddaf28b7b9c4b19f5597093c4f1307 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 26 Aug 2010 10:28:54 -0500 Subject: [PATCH 4/8] wrapped ReadGlobalFeatures --- library/include/dfhack-c/modules/Maps_C.h | 2 ++ library/modules/Maps_C.cpp | 31 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/library/include/dfhack-c/modules/Maps_C.h b/library/include/dfhack-c/modules/Maps_C.h index d1bad823e..fdbfdf1be 100644 --- a/library/include/dfhack-c/modules/Maps_C.h +++ b/library/include/dfhack-c/modules/Maps_C.h @@ -36,6 +36,8 @@ extern "C" { DFHACK_EXPORT int Maps_Start(DFHackObject* maps); DFHACK_EXPORT int Maps_Finish(DFHackObject* maps); +DFHACK_EXPORT t_feature* Maps_ReadGlobalFeatures(DFHackObject* maps); + DFHACK_EXPORT void Maps_getSize(DFHackObject* maps, uint32_t* x, uint32_t* y, uint32_t* z); DFHACK_EXPORT int Maps_isValidBlock(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z); diff --git a/library/modules/Maps_C.cpp b/library/modules/Maps_C.cpp index 09a7dc065..911f4e0f9 100644 --- a/library/modules/Maps_C.cpp +++ b/library/modules/Maps_C.cpp @@ -54,6 +54,37 @@ int Maps_Finish(DFHackObject* maps) return -1; } +t_feature* Maps_ReadGlobalFeatures(DFHackObject* maps) +{ + if(maps != NULL) + { + std::vector featureVec; + + if(((DFHack::Maps*)maps)->ReadGlobalFeatures(featureVec)) + { + if(featureVec.size() <= 0) + return NULL; + + t_feature* buf; + + (*alloc_t_feature_buffer_callback)(buf, featureVec.size()); + + if(buf != NULL) + { + copy(featureVec.begin(), featureVec.end(), buf); + + return buf; + } + else + return NULL; + } + else + return NULL; + } + + return NULL; +} + void Maps_getSize(DFHackObject* maps, uint32_t* x, uint32_t* y, uint32_t* z) { if(maps != NULL) From fb7cfc45c820b4d34f1837aff9abbdfbbc671568 Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 27 Aug 2010 09:19:23 -0500 Subject: [PATCH 5/8] added getType --- library/include/dfhack-c/modules/Materials_C.h | 1 + library/modules/Materials_C.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/library/include/dfhack-c/modules/Materials_C.h b/library/include/dfhack-c/modules/Materials_C.h index 888978a50..82f426ca7 100644 --- a/library/include/dfhack-c/modules/Materials_C.h +++ b/library/include/dfhack-c/modules/Materials_C.h @@ -44,6 +44,7 @@ DFHACK_EXPORT int Materials_ReadOthers(DFHackObject* mat); DFHACK_EXPORT void Materials_ReadAllMaterials(DFHackObject* mat); +DFHACK_EXPORT const char* Materials_getType(DFHackObject* mat, t_material* material); DFHACK_EXPORT const char* Materials_getDescription(DFHackObject* mat, t_material* material); DFHACK_EXPORT int Materials_getInorganicSize(DFHackObject* mat); diff --git a/library/modules/Materials_C.cpp b/library/modules/Materials_C.cpp index f95c4e76c..4fa8b7705 100644 --- a/library/modules/Materials_C.cpp +++ b/library/modules/Materials_C.cpp @@ -116,6 +116,18 @@ void Materials_ReadAllMaterials(DFHackObject* mat) } } +const char* Materials_getType(DFHackObject* mat, t_material* material) +{ + if(mat != NULL) + { + std::string type = ((DFHack::Materials*)mat)->getType(*material); + + return type.c_str(); + } + + return "\0"; +} + const char* Materials_getDescription(DFHackObject* mat, t_material* material) { if(mat != NULL) From 33cd5fb1b47a6b8a53c10fb293b984879a9d3a57 Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 27 Aug 2010 09:20:16 -0500 Subject: [PATCH 6/8] added ReadHotkeys and getScreenTiles --- library/include/dfhack-c/modules/Position_C.h | 4 ++ library/modules/Position_C.cpp | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/library/include/dfhack-c/modules/Position_C.h b/library/include/dfhack-c/modules/Position_C.h index 665e400bf..2b60fa2e2 100644 --- a/library/include/dfhack-c/modules/Position_C.h +++ b/library/include/dfhack-c/modules/Position_C.h @@ -38,8 +38,12 @@ DFHACK_EXPORT int Position_setViewCoords(DFHackObject* pos, const int32_t x, con DFHACK_EXPORT int Position_getCursorCoords(DFHackObject* pos, int32_t* x, int32_t* y, int32_t* z); DFHACK_EXPORT int Position_setCursorCoords(DFHackObject* pos, const int32_t x, const int32_t y, const int32_t z); +DFHACK_EXPORT t_hotkey* Position_ReadHotkeys(DFHackObject* pos); + DFHACK_EXPORT int Position_getWindowSize(DFHackObject* pos, int32_t* width, int32_t* height); +DFHACK_EXPORT t_screen* Position_getScreenTiles(DFHackObject* pos, int32_t width, int32_t height); + #ifdef __cplusplus } #endif diff --git a/library/modules/Position_C.cpp b/library/modules/Position_C.cpp index 0144abdb3..a97a52a56 100644 --- a/library/modules/Position_C.cpp +++ b/library/modules/Position_C.cpp @@ -22,6 +22,7 @@ must not be misrepresented as being the original software. distribution. */ +#include "dfhack-c/DFTypes_C.h" #include "dfhack-c/modules/Position_C.h" #ifdef __cplusplus @@ -97,6 +98,28 @@ int Position_setCursorCoords(DFHackObject* pos, int32_t x, int32_t y, int32_t z) return -1; } +t_hotkey* Position_ReadHotkeys(DFHackObject* pos) +{ + if(pos != NULL) + { + t_hotkey* buf; + + (*alloc_t_hotkey_buffer_callback)(buf, NUM_HOTKEYS); + + if(buf != NULL) + { + if(((DFHack::Position*)pos)->ReadHotkeys(buf)) + return buf; + else + return NULL; + } + else + return NULL; + } + + return NULL; +} + int Position_getWindowSize(DFHackObject* pos, int32_t* width, int32_t* height) { if(pos != NULL) @@ -117,6 +140,26 @@ int Position_getWindowSize(DFHackObject* pos, int32_t* width, int32_t* height) return -1; } +t_screen* Position_getScreenTiles(DFHackObject* pos, int32_t width, int32_t height) +{ + if(pos != NULL) + { + t_screen* buf; + + (*alloc_t_screen_buffer_callback)(buf, width * height); + + if(buf == NULL) + return NULL; + + if(((DFHack::Position*)pos)->getScreenTiles(width, height, buf)) + return buf; + else + return NULL; + } + + return NULL; +} + #ifdef __cplusplus } #endif From c7f37b697c486290958dc95e97e95bb2c996fecd Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 27 Aug 2010 09:30:15 -0500 Subject: [PATCH 7/8] added include for Position.h --- library/include/dfhack-c/DFTypes_C.h | 1 + 1 file changed, 1 insertion(+) diff --git a/library/include/dfhack-c/DFTypes_C.h b/library/include/dfhack-c/DFTypes_C.h index 4bb9676bf..49bd54dc9 100644 --- a/library/include/dfhack-c/DFTypes_C.h +++ b/library/include/dfhack-c/DFTypes_C.h @@ -29,6 +29,7 @@ distribution. #include "dfhack/DFTypes.h" #include "dfhack/modules/Maps.h" #include "dfhack/modules/Materials.h" +#include "dfhack/modules/Position.h" #include "dfhack/DFTileTypes.h" #ifdef __cplusplus From 60bfc1d420dd5e3e149f4b82d675da1c305f461c Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 27 Aug 2010 09:31:08 -0500 Subject: [PATCH 8/8] added the new callbacks to the list --- library/include/dfhack-c/README_C.rst | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/library/include/dfhack-c/README_C.rst b/library/include/dfhack-c/README_C.rst index 3b785cf54..7a5b69a54 100644 --- a/library/include/dfhack-c/README_C.rst +++ b/library/include/dfhack-c/README_C.rst @@ -54,17 +54,36 @@ Buffer Callback List - 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) Templates Make My Life Harder ------------------------------- -Three dfhack structures (t_colormodifier, t_creaturecaste, and t_creaturetype) contain vectors, which (obviously) don't work in C. Therefore, these structures have C versions that replace the vector with a buffer and length, but are otherwise identical to their C++ counterparts. For each structure, there are three associated callbacks. One initializes an empty instance of the structure, one initializes an instance with values passed in, and one allocates a buffer in the same manner as the other allocators. - +Several dfhack structures contain vectors, which (obviously) don't work in C. Therefore, these structures have C versions that replace the vector with a buffer and length, but are otherwise identical to their C++ counterparts. For each structure, there are three associated callbacks. One initializes an empty instance of the structure (*alloc_empty_colormodifier_callback*, for instance), one initializes an instance with values passed in (*alloc_colormodifier_callback*), and one allocates a buffer in the same manner as the other allocators (*alloc_colormodifier_buffer_callback*). + +The replaced structures and their callbacks are as follows. + +- c_colormodifier + * alloc_empty_colormodifier_callback(c_colormodifier*) + * alloc_colormodifier_callback(c_colormodifier*, const char*, uint32_t) + * alloc_colormodifier_buffer_callback(c_colormodifier*, uint32_t) +- c_creaturecaste + * alloc_empty_creaturecaste_callback(c_creaturecaste*) + * alloc_creaturecaste_callback(c_creaturecaste*, const char*, const char*, const char*, const char*, uint32_t, uint32_t) + * alloc_creaturecaste_buffer_callback(c_creaturecaste*, uint32_t) +- c_creaturetype + * alloc_empty_creaturetype_callback(c_creaturetype*) + * alloc_creaturetype_callback(c_creaturetype*, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t) + * alloc_creaturetype_buffer_callback(c_creaturetype*, uint32_t) + A Small Callback Example In Python ------------------------------------- The Python bindings for dfhack implement the unsigned integer allocator callback like this: