From 368f3c1b2ef2d927d6c2da43e72a0746f826d9c4 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 3 Mar 2011 14:14:28 -0600 Subject: [PATCH 1/7] added World getter renamed get_Window to get_WindowIO --- library/DFContext_C.cpp | 30 +++++++++++++++++--------- library/include/dfhack-c/DFContext_C.h | 3 ++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/library/DFContext_C.cpp b/library/DFContext_C.cpp index e6406c5be..1afba8fc1 100644 --- a/library/DFContext_C.cpp +++ b/library/DFContext_C.cpp @@ -236,16 +236,6 @@ DFHackObject* Context_getProcess(DFHackObject* context) return NULL; } -DFHackObject* Context_getWindow(DFHackObject* context) -{ - if(context != NULL) - { - return (DFHackObject*)((DFHack::Context*)context)->getWindowIO(); - } - - return NULL; -} - DFHackObject* Context_getCreatures(DFHackObject* context) { if(context != NULL) @@ -346,6 +336,26 @@ DFHackObject* Context_getItems(DFHackObject* context) return NULL; } +DFHackObject* Context_getWorld(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getWorld(); + } + + return NULL; +} + +DFHackObject* Context_getWindowIO(DFHackObject* context) +{ + if(context != NULL) + { + return (DFHackObject*)((DFHack::Context*)context)->getWindowIO(); + } + + return NULL; +} + void Context_ReadRaw(DFHackObject* context, const uint32_t offset, const uint32_t size, uint8_t* target) { if(context != NULL) diff --git a/library/include/dfhack-c/DFContext_C.h b/library/include/dfhack-c/DFContext_C.h index 9ae73e4ef..0dbe85181 100644 --- a/library/include/dfhack-c/DFContext_C.h +++ b/library/include/dfhack-c/DFContext_C.h @@ -133,7 +133,6 @@ DFHACK_EXPORT int Context_AsyncSuspend(DFHackObject* context); DFHACK_EXPORT DFHackObject* Context_getMemoryInfo(DFHackObject* context); DFHACK_EXPORT DFHackObject* Context_getProcess(DFHackObject* context); -DFHACK_EXPORT DFHackObject* Context_getWindow(DFHackObject* context); DFHACK_EXPORT DFHackObject* Context_getCreatures(DFHackObject* context); DFHACK_EXPORT DFHackObject* Context_getMaps(DFHackObject* context); @@ -145,6 +144,8 @@ DFHACK_EXPORT DFHackObject* Context_getVegetation(DFHackObject* context); DFHACK_EXPORT DFHackObject* Context_getBuildings(DFHackObject* context); DFHACK_EXPORT DFHackObject* Context_getConstructions(DFHackObject* context); DFHACK_EXPORT DFHackObject* Context_getItems(DFHackObject* context); +DFHACK_EXPORT DFHackObject* Context_getWorld(DFHackObject* context); +DFHACK_EXPORT DFHackObject* Context_getWindowIO(DFHackObject* context); //these are DANGEROUS...can crash/segfault DF, turn the seas to blood, call up the Antichrist, etc DFHACK_EXPORT void Context_ReadRaw(DFHackObject* context, const uint32_t offset, const uint32_t size, uint8_t* target); From 4cf037d5d3b0d6d5eaa92807aabbe4d3c0f0c0ba Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 3 Mar 2011 14:15:27 -0600 Subject: [PATCH 2/7] changed a couple of argument types to be more explicit --- library/include/dfhack-c/modules/WindowIO_C.h | 4 ++-- library/modules/WindowIO_C.cpp | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/library/include/dfhack-c/modules/WindowIO_C.h b/library/include/dfhack-c/modules/WindowIO_C.h index 072a4f352..2051dee6a 100644 --- a/library/include/dfhack-c/modules/WindowIO_C.h +++ b/library/include/dfhack-c/modules/WindowIO_C.h @@ -32,9 +32,9 @@ distribution. extern "C" { #endif -DFHACK_EXPORT int WindowIO_TypeStr(DFHackObject* window, const char* input, int delay, bool useShift); +DFHACK_EXPORT int WindowIO_TypeStr(DFHackObject* window, const char* input, uint32_t delay, int8_t useShift); -DFHACK_EXPORT int WindowIO_TypeSpecial(DFHackObject* window, t_special command, int count, int delay); +DFHACK_EXPORT int WindowIO_TypeSpecial(DFHackObject* window, t_special command, uint32_t count, uint32_t delay); #ifdef __cplusplus } diff --git a/library/modules/WindowIO_C.cpp b/library/modules/WindowIO_C.cpp index 31c6e4739..73217a7d2 100644 --- a/library/modules/WindowIO_C.cpp +++ b/library/modules/WindowIO_C.cpp @@ -40,18 +40,23 @@ using namespace DFHack; extern "C" { #endif -int WindowIO_TypeStr(DFHackObject* window, const char* input, int delay, bool useShift) +int WindowIO_TypeStr(DFHackObject* window, const char* input, uint32_t delay, int8_t useShift) { if(window != NULL) { - ((DFHack::WindowIO*)window)->TypeStr(input, delay, useShift); + bool shifting = false; + + if(useShift > 0) + shifting = true; + + ((DFHack::WindowIO*)window)->TypeStr(input, delay, shifting); return 1; } return -1; } -int WindowIO_TypeSpecial(DFHackObject* window, t_special command, int count, int delay) +int WindowIO_TypeSpecial(DFHackObject* window, t_special command, uint32_t count, uint32_t delay) { if(window != NULL) { From 2c9016d2f29fda4fcbbb2f4213ed1841a2703a1a Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 3 Mar 2011 14:17:09 -0600 Subject: [PATCH 3/7] added checks for allocator callback being null --- library/modules/Materials_C.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/library/modules/Materials_C.cpp b/library/modules/Materials_C.cpp index 90b16adf9..e8e42af76 100644 --- a/library/modules/Materials_C.cpp +++ b/library/modules/Materials_C.cpp @@ -233,6 +233,9 @@ t_matgloss* Materials_getInorganic(DFHackObject* mat) if(materials->inorganic.size() > 0) { t_matgloss* buf = NULL; + + if(alloc_matgloss_buffer_callback == NULL) + return NULL; ((*alloc_matgloss_buffer_callback)(buf, materials->inorganic.size())); @@ -258,6 +261,9 @@ t_matgloss* Materials_getOrganic(DFHackObject* mat) { t_matgloss* buf = NULL; + if(alloc_matgloss_buffer_callback == NULL) + return NULL; + ((*alloc_matgloss_buffer_callback)(buf, materials->organic.size())); if(buf != NULL) @@ -282,6 +288,9 @@ t_matgloss* Materials_getTree(DFHackObject* mat) { t_matgloss* buf = NULL; + if(alloc_matgloss_buffer_callback == NULL) + return NULL; + ((*alloc_matgloss_buffer_callback)(buf, materials->tree.size())); if(buf != NULL) @@ -306,6 +315,9 @@ t_matgloss* Materials_getPlant(DFHackObject* mat) { t_matgloss* buf = NULL; + if(alloc_matgloss_buffer_callback == NULL) + return NULL; + ((*alloc_matgloss_buffer_callback)(buf, materials->plant.size())); if(buf != NULL) @@ -330,6 +342,9 @@ t_matgloss* Materials_getRace(DFHackObject* mat) { t_matgloss* buf = NULL; + if(alloc_matgloss_buffer_callback == NULL) + return NULL; + ((*alloc_matgloss_buffer_callback)(buf, materials->race.size())); if(buf != NULL) @@ -356,6 +371,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)); if(buf != NULL) From 6fc1f31e314ad8c5eaac56179c52c5efb453815b Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 3 Mar 2011 14:17:31 -0600 Subject: [PATCH 4/7] first commit --- library/python/pydfhack/window_io.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 library/python/pydfhack/window_io.py diff --git a/library/python/pydfhack/window_io.py b/library/python/pydfhack/window_io.py new file mode 100644 index 000000000..8be4ff9c0 --- /dev/null +++ b/library/python/pydfhack/window_io.py @@ -0,0 +1,25 @@ +from ctypes import * + +libdfhack.WindowIO_TypeStr.argtypes = [ c_void_p, c_char_p, c_uint, c_byte ] +libdfhack.WindowIO_TypeSpecial.argtypes = [ c_void_p, c_uint, c_uint, c_uint, c_uint ] + +class WindowIO(object): + def __init__(self, ptr): + self._window_io_ptr = ptr + + def type_str(self, s, delay = 0, use_shift = False): + c_shift = c_byte(0) + c_delay = c_int(delay) + c_s = c_char_p(s) + + if use_shift is True: + c_shift = c_byte(1) + + return libdfhack.WindowIO_TypeStr(self._window_io_ptr, c_s, c_delay, c_shift) > 0 + + def type_special(self, command, count = 1, delay = 0): + c_command = c_uint(command) + c_count = c_uint(count) + c_delay = c_uint(delay) + + return libdfhack.WindwIO_TypeSpecial(self._window_io_ptr, c_command, c_count, c_delay) > 0 \ No newline at end of file From a816b67362c580edc2c5a1c279f06c2fc70af2e7 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 3 Mar 2011 14:17:56 -0600 Subject: [PATCH 5/7] added key types for window_io module --- library/python/pydfhack/enum.py | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/library/python/pydfhack/enum.py b/library/python/pydfhack/enum.py index f9124fe2a..bc123a8c9 100644 --- a/library/python/pydfhack/enum.py +++ b/library/python/pydfhack/enum.py @@ -95,3 +95,62 @@ LiquidType = C_EnumerationType("LiquidType", (c_uint,), {"Water" : 0, "Magma" : 1}) + +#this list must stay in the same order as the one in dfhack/library/include/dfhack/modules/WindowIO.h! +_keys = ["ENTER", + "SPACE", + "BACK_SPACE", + "TAB", + "CAPS_LOCK", + "LEFT_SHIFT", + "RIGHT_SHIFT", + "LEFT_CONTROL", + "RIGHT_CONTROL", + "ALT", + "WAIT", + "ESCAPE", + "UP", + "DOWN", + "LEFT", + "RIGHT", + "F1", + "F2", + "F3", + "F4", + "F5", + "F6", + "F7", + "F8", + "F9", + "F10", + "F11", + "F12", + "PAGE_UP", + "PAGE_DOWN", + "INSERT", + "DFK_DELETE", + "HOME", + "END", + "KEYPAD_DIVIDE", + "KEYPAD_MULTIPLY", + "KEYPAD_SUBTRACT", + "KEYPAD_ADD," + "KEYPAD_ENTER", + "KEYPAD_0", + "KEYPAD_1", + "KEYPAD_2", + "KEYPAD_3", + "KEYPAD_4", + "KEYPAD_5", + "KEYPAD_6", + "KEYPAD_7", + "KEYPAD_8", + "KEYPAD_9", + "KEYPAD_DECIMAL_POINT", + "NUM_SPECIALS"] + +_key_dict = dict([(k, i) for i, k in enumerate(_keys)]) + +KeyType = C_EnumerationType("KeyType", + (c_uint,), + _key_dict) From d314c733b23cb5c94a0555f7cd13f87b026da607 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 3 Mar 2011 14:18:28 -0600 Subject: [PATCH 6/7] added world and window_io getters --- library/python/pydfhack/context.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/library/python/pydfhack/context.py b/library/python/pydfhack/context.py index 1f9f06c16..a09e1ea17 100644 --- a/library/python/pydfhack/context.py +++ b/library/python/pydfhack/context.py @@ -11,7 +11,6 @@ libdfhack.Context_Free.argtypes = [ c_void_p ] libdfhack.Context_getMemoryInfo.restype = c_void_p libdfhack.Context_getProcess.restype = c_void_p -libdfhack.Context_getWindow.restype = c_void_p libdfhack.Context_getCreatures.restype = c_void_p libdfhack.Context_getMaps.restype = c_void_p @@ -23,6 +22,8 @@ libdfhack.Context_getVegetation.restype = c_void_p libdfhack.Context_getBuildings.restype = c_void_p libdfhack.Context_getConstructions.restype = c_void_p libdfhack.Context_getItems.restype = c_void_p +libdfhack.Context_getWorld.restype = c_void_p +libdfhack.Context_getWindowIO.restype = c_void_p class ContextManager(object): def __init__(self, memory_path): @@ -67,6 +68,8 @@ class Context(object): self._tran_obj = None self._item_obj = None self._creature_obj = None + self._world_obj = None + self._window_io_obj = None def __del__(self): libdfhack.Context_Free(self._c_ptr) @@ -167,4 +170,20 @@ class Context(object): if self._tran_obj is None: self._tran_obj = translation.Translation(libdfhack.Context_getTranslation(self._c_ptr)) - return self._tran_obj \ No newline at end of file + return self._tran_obj + + @property + def world(self): + import world + if self._world_obj is None: + self._world_obj = world.World(libdfhack.Context_getWorld(self._c_ptr)) + + return self._world_obj + + @property + def window_io(self): + import window_io + if self._window_io_obj is None: + self._window_io_obj = window_io.WindowIO(libdfhack.Context_getWindowIO(self._c_ptr)) + + return self._window_io_obj \ No newline at end of file From c98bc9da9d47ae3228042e3f3e2595e96c2fda61 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 3 Mar 2011 14:19:20 -0600 Subject: [PATCH 7/7] updated to actually match the C stuff (not passing allocator callbacks every time, etc) --- library/python/pydfhack/materials.py | 221 ++++++++++++++------------- 1 file changed, 114 insertions(+), 107 deletions(-) diff --git a/library/python/pydfhack/materials.py b/library/python/pydfhack/materials.py index 354f4e59a..4a135f9a1 100644 --- a/library/python/pydfhack/materials.py +++ b/library/python/pydfhack/materials.py @@ -2,17 +2,6 @@ from ctypes import * from dftypes import libdfhack from util import * -_get_arg_types = [ c_void_p, _arr_create_func ] - -libdfhack.Materials_getInorganic.argtypes = _get_arg_types -libdfhack.Materials_getOrganic.argtypes = _get_arg_types -libdfhack.Materials_getTree.argtypes = _get_arg_types -libdfhack.Materials_getPlant.argtypes = _get_arg_types -libdfhack.Materials_getRace.argtypes = _get_arg_types -#libdfhack.Materials_getRaceEx.argtypes = _get_arg_types -libdfhack.Materials_getColor.argtypes = _get_arg_types -libdfhack.Materials_getOther.argtypes = _get_arg_types - class Materials(object): def __init__(self, ptr): self._mat_ptr = ptr @@ -25,117 +14,135 @@ class Materials(object): self.race_ex = None self.color = None self.other = None + + def _get_inorganic(self): + self.inorganic = libdfhack.Materials_getInorganic(self._mat_ptr) + def _get_organic(self): + self.organic = libdfhack.Materials_getOrganic(self._mat_ptr) + def _get_tree(self): + self.tree = libdfhack.Materials_getTree(self._mat_ptr) + def _get_plant(self): + self.plant = libdfhack.Materials_getPlant(self._mat_ptr) + def _get_race(self): + self.race = libdfhack.Materials_getRace(self._mat_ptr) + def _get_race_ex(self): + self.race_ex = libdfhack.Materials_getRaceEx(self._mat_ptr) + def _get_color(self): + self.color = libdfhack.Materials_getColor(self._mat_ptr) + def _get_other(self): + self.other = libdfhack.Materials_getOther(self._mat_ptr) + + def _get_all(self): + self._get_inorganic() + self._get_organic() + self._get_tree() + self._get_plant() + self._get_race() + self._get_race_ex() + self._get_color() + self._get_other() + def _clear_all(self): + self.inorganic = None + self.organic = None + self.tree = None + self.plant = None + self.race = None + self.race_ex = None + self.color = None + self.other = None def read_inorganic(self): - return libdfhack.Materials_ReadInorganicMaterials(self._mat_ptr) + result = libdfhack.Materials_ReadInorganicMaterials(self._mat_ptr) > 0 + + if result == True: + self._get_inorganic() + else: + self.inorganic = None + + return result def read_organic(self): - return libdfhack.Materials_ReadOrganicMaterials(self._mat_ptr) - - def read_wood(self): - return libdfhack.Materials_ReadWoodMaterials(self._mat_ptr) + result = libdfhack.Materials_ReadOrganicMaterials(self._mat_ptr) > 0 + + if result == True: + self._get_organic() + else: + self.organic = None + + return result + + def read_tree(self): + result = libdfhack.Materials_ReadWoodMaterials(self._mat_ptr) > 0 + + if result == True: + self._get_tree() + else: + self.tree = None + + return result def read_plant(self): - return libdfhack.Materials_ReadPlantMaterials(self._mat_ptr) + result = libdfhack.Materials_ReadPlantMaterials(self._mat_ptr) > 0 + + if result == True: + self._get_plant() + else: + self.plant = None + + return result def read_creature_types(self): - return libdfhack.Materials_ReadCreatureTypes(self._mat_ptr) + result = libdfhack.Materials_ReadCreatureTypes(self._mat_ptr) > 0 + + if result == True: + self._get_race() + else: + self.race = None + + return result def read_creature_types_ex(self): - return libdfhack.Materials_ReadCreatureTypesEx(self._mat_ptr) + result = libdfhack.Materials_ReadCreatureTypesEx(self._mat_ptr) > 0 + + if result == True: + self._get_race_ex() + else: + self.race_ex = None + + return result def read_descriptor_colors(self): - return libdfhack.Materials_ReadDescriptorColors(self._mat_ptr) + result = libdfhack.Materials_ReadDescriptorColors(self._mat_ptr) > 0 + + if result == True: + self._get_color() + else: + self.color = None + + return result def read_others(self): - return libdfhack.Materials_ReadOthers(self._mat_ptr) + result = libdfhack.Materials_ReadOthers(self._mat_ptr) > 0 + + if result == True: + self._get_other() + else: + self.other = None + + return result def read_all(self): - libdfhack.Materials_ReadAllMaterials(self._mat_ptr) + result = libdfhack.Materials_ReadAllMaterials(self._mat_ptr) > 0 + + if result == True: + self._get_all() + else: + self._clear_all() + + return result + + def get_type(self, material): + return libdfhack.Materials_getType(self._mat_ptr, byref(material)) def get_description(self, material): - return libdfhack.Materials_getDescription(self._mat_ptr, byref(material)) - - def update_inorganic_cache(self): - def update_callback(count): - allocated = _allocate_array(Matgloss, count) - - self.inorganic = allocated[0] - - return allocated[1] - - callback = _arr_create_func(update_callback) - - return libdfhack.Materials_getInorganic(self._mat_ptr, callback) - - def update_organic_cache(self): - def update_callback(count): - allocated = _allocate_array(Matgloss, count) - - self.organic = allocated[0] - - return allocated[1] - - callback = _arr_create_func(update_callback) - - return libdfhack.Materials_getOrganic(self._mat_ptr, callback) - - def update_tree_cache(self): - def update_callback(count): - allocated = _allocate_array(Matgloss, count) - - self.tree = allocated[0] - - return allocated[1] - - callback = _arr_create_func(update_callback) - - return libdfhack.Materials_getTree(self._mat_ptr, callback) - - def update_plant_cache(self): - def update_callback(count): - allocated = _allocate_array(Matgloss, count) - - self.plant = allocated[0] - - return allocated[1] - - callback = _arr_create_func(update_callback) - - return libdfhack.Materials_getPlant(self._mat_ptr, callback) - - def update_race_cache(self): - def update_callback(count): - allocated = _allocate_array(Matgloss, count) - - self.race = allocated[0] - - return allocated[1] - - callback = _arr_create_func(update_callback) - - return libdfhack.Materials_getRace(self._mat_ptr, callback) - - def update_color_cache(self): - def update_callback(count): - allocated = _allocate_array(DescriptorColor, count) - - self.color = allocated[0] - - return allocated[1] - - callback = _arr_create_func(update_callback) - - return libdfhack.Materials_getColor(self._mat_ptr, callback) - - def update_other_cache(self): - def update_callback(count): - allocated = _allocate_array(MatglossOther, count) - - self.other = allocated[0] - - return allocated[1] - - callback = _arr_create_func(update_callback) - - return libdfhack.Materials_getOther(self._mat_ptr, callback) + return libdfhack.Materials_getDescription(self._mat_ptr, byref(material)) \ No newline at end of file