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)