From c579d60c98e8e7806682f2c236dc83ee0e1ee324 Mon Sep 17 00:00:00 2001 From: doomchild Date: Sat, 3 Apr 2010 20:57:30 -0500 Subject: [PATCH 01/37] using ctypes Structure and Union class now --- library/pydfhack/pydftypes.py | 592 +++++++++++----------------------- 1 file changed, 196 insertions(+), 396 deletions(-) diff --git a/library/pydfhack/pydftypes.py b/library/pydfhack/pydftypes.py index 81c0b23dd..998a17bbb 100644 --- a/library/pydfhack/pydftypes.py +++ b/library/pydfhack/pydftypes.py @@ -1,233 +1,212 @@ from pydfhack import * +from ctypes import * class DFAPI(API): def Read_Designations(self, x, y, z): - d_list = [DesignationFlags(d) for d in API.Read_Designations(self, x, y, z)] + temp = API.Read_Designations(self, x, y, z) + + d_list = [] + + for i in temp: + d = [] + + for j in i: + d.append(DesignationFlags(j)) + + d_list.append(d) return d_list + def Write_Designations(self, x, y, z, d_list): + temp = [] + + for i in d_list: + t = [] + + for j in i: + t.append(j.whole) + + temp.append(t) + + API.Write_Designations(self, x, y, z, temp) def Read_Occupancy(self, x, y, z): - o_list = [OccupancyFlags(o) for o in API.Read_Occupancy(self, x, y, z)] + temp = API.Read_Occupancy(self, x, y, z) + + o_list = [] + + for i in temp: + o = [] + + for j in i: + o.append(OccupancyFlags(j)) + + o_list.append(o) return o_list -class CreatureFlags1(_UnionBase): - move_state = property(fget = lambda self: self._get_mask_bit(0), - fset = lambda self, val: self._set_mask_bit(0, val)) - dead = property(fget = lambda self: self._get_mask_bit(1), - fset = lambda self, val: self._set_mask_bit(1, val)) - has_mood = property(fget = lambda self: self._get_mask_bit(2), - fset = lambda self, val: self._set_mask_bit(2, val)) - had_mood = property(fget = lambda self: self._get_mask_bit(3), - fset = lambda self, val: self._set_mask_bit(3, val)) - - marauder = property(fget = lambda self: self._get_mask_bit(4), - fset = lambda self, val: self._set_mask_bit(4, val)) - drowning = property(fget = lambda self: self._get_mask_bit(5), - fset = lambda self, val: self._set_mask_bit(5, val)) - merchant = property(fget = lambda self: self._get_mask_bit(6), - fset = lambda self, val: self._set_mask_bit(6, val)) - forest = property(fget = lambda self: self._get_mask_bit(7), - fset = lambda self, val: self._set_mask_bit(7, val)) - - left = property(fget = lambda self: self._get_mask_bit(8), - fset = lambda self, val: self._set_mask_bit(8, val)) - rider = property(fget = lambda self: self._get_mask_bit(9), - fset = lambda self, val: self._set_mask_bit(9, val)) - incoming = property(fget = lambda self: self._get_mask_bit(10), - fset = lambda self, val: self._set_mask_bit(10, val)) - diplomat = property(fget = lambda self: self._get_mask_bit(11), - fset = lambda self, val: self._set_mask_bit(11, val)) - - zombie = property(fget = lambda self: self._get_mask_bit(12), - fset = lambda self: self._set_mask_bit(12, val)) - skeleton = property(fget = lambda self: self._get_mask_bit(13), - fset = lambda self, val: self._set_mask_bit(13, val)) - can_swap = property(fget = lambda self: self._get_mask_bit(14), - fset = lambda self, val: self._set_mask_bit(14, val)) - on_ground = property(fget = lambda self: self._get_mask_bit(15), - fset = lambda self, val: self._set_mask_bit(15, val)) - - projectile = property(fget = lambda self: self._get_mask_bit(16), - fset = lambda self, val: self._set_mask_bit(16, val)) - active_invader = property(fget = lambda self: self._get_mask_bit(17), - fset = lambda self, val: self._set_mask_bit(17, val)) - hidden_in_ambush = property(fget = lambda self: self._get_mask_bit(18), - fset = lambda self, val: self._set_mask_bit(18, val)) - invader_origin = property(fget = lambda self: self._get_mask_bit(19), - fset = lambda self, val: self._set_mask_bit(19, val)) - - coward = property(fget = lambda self: self._get_mask_bit(20), - fset = lambda self, val: self._set_mask_bit(20, val)) - hidden_ambusher = property(fget = lambda self: self._get_mask_bit(21), - fset = lambda self, val: self._set_mask_bit(21, val)) - invades = property(fget = lambda self: self._get_mask_bit(22), - fset = lambda self, val: self._set_mask_bit(22, val)) - check_flows = property(fget = lambda self: self._get_mask_bit(23), - fset = lambda self, val: self._set_mask_bit(23, val)) - - ridden = property(fget = lambda self: self._get_mask_bit(24), - fset = lambda self, val: self._set_mask_bit(24, val)) - caged = property(fget = lambda self: self._get_mask_bit(25), - fset = lambda self, val: self._set_mask_bit(25, val)) - tame = property(fget = lambda self: self._get_mask_bit(26), - fset = lambda self, val: self._set_mask_bit(26, val)) - chained = property(fget = lambda self: self._get_mask_bit(27), - fset = lambda self, val: self._set_mask_bit(27, val)) - - royal_guard = property(fget = lambda self: self._get_mask_bit(28), - fset = lambda self, val: self._set_mask_bit(28, val)) - fortress_guard = property(fget = lambda self: self._get_mask_bit(29), - fset = lambda self, val: self._set_mask_bit(29, val)) - suppress_wield = property(fget = lambda self: self._get_mask_bit(30), - fset = lambda self, val: self._set_mask_bit(30, val)) - important_historial_figure = property(fget = lambda self: self._get_mask_bit(31), - fset = lambda self, val: self._set_mask_bit(31, val)) +class DesignationStruct(Structure): + _fields_ = [("flow_size", c_uint, 3), + ("pile", c_uint, 1), + ("dig", c_uint, 3), + ("smooth", c_uint, 2), + ("hidden", c_uint, 1), + ("geolayer_index", c_uint, 4), + ("light", c_uint, 1), + ("subterranean", c_uint, 1), + ("skyview", c_uint, 1), + ("biome", c_uint, 4), + ("liquid_type", c_uint, 1), + ("water_table", c_uint, 1), + ("rained", c_uint, 1), + ("traffic", c_uint, 2), + ("flow_forbid", c_uint, 1), + ("liquid_static", c_uint, 1), + ("moss", c_uint, 1), + ("feature_present", c_uint, 1), + ("liquid_character", c_uint, 2)] -class CreatureFlags2(_UnionBase): - swimming = property(fget = lambda self: self._get_mask_bit(0), - fset = lambda self, val: self._set_mask_bit(0, val)) - sparring = property(fget = lambda self: self._get_mask_bit(1), - fset = lambda self, val: self._set_mask_bit(1, val)) - no_notify = property(fget = lambda self: self._get_mask_bit(2), - fset = lambda self, val: self._set_mask_bit(2, val)) - unused = property(fget = lambda self: self._get_mask_bit(3), - fset = lambda self, val: self._set_mask_bit(3, val)) - - calculated_nerves = property(fget = lambda self: self._get_mask_bit(4), - fset = lambda self, val: self._set_mask_bit(4, val)) - calculated_bodyparts = property(fget = lambda self: self._get_mask_bit(5), - fset = lambda self, val: self._set_mask_bit(5, val)) - important_historical_figure = property(fget = lambda self: self._get_mask_bit(6), - fset = lambda self, val: self._set_mask_bit(6, val)) - killed = property(fget = lambda self: self._get_mask_bit(7), - fset = lambda self, val: self._set_mask_bit(7, val)) - - cleanup_1 = property(fget = lambda self: self._get_mask_bit(8), - fset = lambda self, val: self._set_mask_bit(8, val)) - cleanup_2 = property(fget = lambda self: self._get_mask_bit(9), - fset = lambda self, val: self._set_mask_bit(9, val)) - cleanup_3 = property(fget = lambda self: self._get_mask_bit(10), - fset = lambda self, val: self._set_mask_bit(10, val)) - for_trade = property(fget = lambda self: self._get_mask_bit(11), - fset = lambda self, val: self._set_mask_bit(11, val)) - - trade_resolved = property(fget = lambda self: self._get_mask_bit(12), - fset = lambda self: self._set_mask_bit(12, val)) - has_breaks = property(fget = lambda self: self._get_mask_bit(13), - fset = lambda self, val: self._set_mask_bit(13, val)) - gutted = property(fget = lambda self: self._get_mask_bit(14), - fset = lambda self, val: self._set_mask_bit(14, val)) - circulatory_spray = property(fget = lambda self: self._get_mask_bit(15), - fset = lambda self, val: self._set_mask_bit(15, val)) - - locked_in_for_trading = property(fget = lambda self: self._get_mask_bit(16), - fset = lambda self, val: self._set_mask_bit(16, val)) - slaughter = property(fget = lambda self: self._get_mask_bit(17), - fset = lambda self, val: self._set_mask_bit(17, val)) - underworld = property(fget = lambda self: self._get_mask_bit(18), - fset = lambda self, val: self._set_mask_bit(18, val)) - resident = property(fget = lambda self: self._get_mask_bit(19), - fset = lambda self, val: self._set_mask_bit(19, val)) - - cleanup_4 = property(fget = lambda self: self._get_mask_bit(20), - fset = lambda self, val: self._set_mask_bit(20, val)) - calculated_insulation = property(fget = lambda self: self._get_mask_bit(21), - fset = lambda self, val: self._set_mask_bit(21, val)) - visitor_uninvited = property(fget = lambda self: self._get_mask_bit(22), - fset = lambda self, val: self._set_mask_bit(22, val)) - visitor = property(fget = lambda self: self._get_mask_bit(23), - fset = lambda self, val: self._set_mask_bit(23, val)) - - calculated_inventory = property(fget = lambda self: self._get_mask_bit(24), - fset = lambda self, val: self._set_mask_bit(24, val)) - vision_good = property(fget = lambda self: self._get_mask_bit(25), - fset = lambda self, val: self._set_mask_bit(25, val)) - vision_damaged = property(fget = lambda self: self._get_mask_bit(26), - fset = lambda self, val: self._set_mask_bit(26, val)) - vision_missing = property(fget = lambda self: self._get_mask_bit(27), - fset = lambda self, val: self._set_mask_bit(27, val)) +class DesignationFlags(Union): + _fields_ = [("whole", c_uint, 32), + ("bits", DesignationStruct)] - breathing_good = property(fget = lambda self: self._get_mask_bit(28), - fset = lambda self, val: self._set_mask_bit(28, val)) - breathing_problem = property(fget = lambda self: self._get_mask_bit(29), - fset = lambda self, val: self._set_mask_bit(29, val)) - roaming_wilderness_population_source = property(fget = lambda self: self._get_mask_bit(30), - fset = lambda self, val: self._set_mask_bit(30, val)) - roaming_wilderness_population_source_not_a_map_feature = property(fget = lambda self: self._get_mask_bit(31), - fset = lambda self, val: self._set_mask_bit(31, val)) + def __init__(self, initial = 0): + self.whole = initial -class ItemFlags(_UnionBase): - on_ground = property(fget = lambda self: self._get_mask_bit(0), - fset = lambda self, val: self._set_mask_bit(0, val)) - in_job = property(fget = lambda self: self._get_mask_bit(1), - fset = lambda self, val: self._set_mask_bit(1, val)) - in_inventory = property(fget = lambda self: self._get_mask_bit(2), - fset = lambda self, val: self._set_mask_bit(2, val)) - u_ngrd1 = property(fget = lambda self: self._get_mask_bit(3), - fset = lambda self, val: self._set_mask_bit(3, val)) - - in_workshop = property(fget = lambda self: self._get_mask_bit(4), - fset = lambda self, val: self._set_mask_bit(4, val)) - u_ngrd2 = property(fget = lambda self: self._get_mask_bit(5), - fset = lambda self, val: self._set_mask_bit(5, val)) - u_ngrd3 = property(fget = lambda self: self._get_mask_bit(6), - fset = lambda self, val: self._set_mask_bit(6, val)) - rotten = property(fget = lambda self: self._get_mask_bit(7), - fset = lambda self, val: self._set_mask_bit(7, val)) - - unk1 = property(fget = lambda self: self._get_mask_bit(8), - fset = lambda self, val: self._set_mask_bit(8, val)) - u_ngrd4 = property(fget = lambda self: self._get_mask_bit(9), - fset = lambda self, val: self._set_mask_bit(9, val)) - unk2 = property(fget = lambda self: self._get_mask_bit(10), - fset = lambda self, val: self._set_mask_bit(10, val)) - u_ngrd5 = property(fget = lambda self: self._get_mask_bit(11), - fset = lambda self, val: self._set_mask_bit(11, val)) - - unk3 = property(fget = lambda self: self._get_mask_bit(12), - fset = lambda self: self._set_mask_bit(12, val)) - u_ngrd6 = property(fget = lambda self: self._get_mask_bit(13), - fset = lambda self, val: self._set_mask_bit(13, val)) - narrow = property(fget = lambda self: self._get_mask_bit(14), - fset = lambda self, val: self._set_mask_bit(14, val)) - u_ngrd7 = property(fget = lambda self: self._get_mask_bit(15), - fset = lambda self, val: self._set_mask_bit(15, val)) - - worn = property(fget = lambda self: self._get_mask_bit(16), - fset = lambda self, val: self._set_mask_bit(16, val)) - unk4 = property(fget = lambda self: self._get_mask_bit(17), - fset = lambda self, val: self._set_mask_bit(17, val)) - u_ngrd8 = property(fget = lambda self: self._get_mask_bit(18), - fset = lambda self, val: self._set_mask_bit(18, val)) - forbid = property(fget = lambda self: self._get_mask_bit(19), - fset = lambda self, val: self._set_mask_bit(19, val)) - - unk5 = property(fget = lambda self: self._get_mask_bit(20), - fset = lambda self, val: self._set_mask_bit(20, val)) - dump = property(fget = lambda self: self._get_mask_bit(21), - fset = lambda self, val: self._set_mask_bit(21, val)) - on_fire = property(fget = lambda self: self._get_mask_bit(22), - fset = lambda self, val: self._set_mask_bit(22, val)) - melt = property(fget = lambda self: self._get_mask_bit(23), - fset = lambda self, val: self._set_mask_bit(23, val)) - - hidden = property(fget = lambda self: self._get_mask_bit(24), - fset = lambda self, val: self._set_mask_bit(24, val)) - u_ngrd9 = property(fget = lambda self: self._get_mask_bit(25), - fset = lambda self, val: self._set_mask_bit(25, val)) - unk6 = property(fget = lambda self: self._get_mask_bit(26), - fset = lambda self, val: self._set_mask_bit(26, val)) - unk7 = property(fget = lambda self: self._get_mask_bit(27), - fset = lambda self, val: self._set_mask_bit(27, val)) - - unk8 = property(fget = lambda self: self._get_mask_bit(28), - fset = lambda self, val: self._set_mask_bit(28, val)) - unk9 = property(fget = lambda self: self._get_mask_bit(29), - fset = lambda self, val: self._set_mask_bit(29, val)) - unk10 = property(fget = lambda self: self._get_mask_bit(30), - fset = lambda self, val: self._set_mask_bit(30, val)) - unk11 = property(fget = lambda self: self._get_mask_bit(31), - fset = lambda self, val: self._set_mask_bit(31, val)) +class OccupancyStruct(Strucure): + _fields_ = [("building", c_uint, 3), + ("unit", c_uint, 1), + ("unit_grounded", c_uint, 1), + ("item", c_uint, 1), + ("splatter", c_uint, 26)] + +class OccupancyFlags(Union): + _fields_ = [("whole", c_uint, 32), + ("bits", OccupancyStruct)] + + def __init__(self, initial = 0): + self.whole = initial + +class CreatureStruct1(Structure): + _fields_ = [("move_state", c_uint, 1), + ("dead", c_uint, 1), + ("has_mood", c_uint, 1), + ("had_mood", c_uint, 1), + ("marauder", c_uint, 1), + ("drowning", c_uint, 1), + ("merchant", c_uint, 1), + ("forest", c_uint, 1), + ("left", c_uint, 1), + ("rider", c_uint, 1), + ("incoming", c_uint, 1), + ("diplomat", c_uint, 1), + ("zombie", c_uint, 1), + ("skeleton", c_uint, 1), + ("can_swap", c_uint, 1), + ("on_ground", c_uint, 1), + ("projectile", c_uint, 1), + ("active_invader", c_uint, 1), + ("hidden_in_ambush", c_uint, 1), + ("invader_origin", c_uint, 1), + ("coward", c_uint, 1), + ("hidden_ambusher", c_uint, 1), + ("invades", c_uint, 1), + ("check_flows", c_uint, 1), + ("ridden", c_uint, 1), + ("caged", c_uint, 1), + ("tame", c_uint, 1), + ("chained", c_uint, 1), + ("royal_guard", c_uint, 1), + ("fortress_guard", c_uint, 1), + ("suppress_wield", c_uint, 1), + ("important_historical_figure", c_uint, 1)] + +class CreatureFlags1(Union): + _fields_ = [("whole", c_uint, 32), + ("bits", CreatureStruct1)] + + def __init__(self, initial = 0): + self.whole = initial + +class CreatureStruct2(Structure): + _fields_ = [("swimming", c_uint, 1), + ("sparring", c_uint, 1), + ("no_notify", c_uint, 1), + ("unused", c_uint, 1), + ("calculated_nerves", c_uint, 1), + ("calculated_bodyparts", c_uint, 1), + ("important_historical_figure", c_uint, 1), + ("killed", c_uint, 1), + ("cleanup_1", c_uint, 1), + ("cleanup_2", c_uint, 1), + ("cleanup_3", c_uint, 1), + ("for_trade", c_uint, 1), + ("trade_resolved", c_uint, 1), + ("has_breaks", c_uint, 1), + ("gutted", c_uint, 1), + ("circulatory_spray", c_uint, 1), + ("locked_in_for_trading", c_uint, 1), + ("slaughter", c_uint, 1), + ("underworld", c_uint, 1), + ("resident", c_uint, 1), + ("cleanup_4", c_uint, 1), + ("calculated_insulation", c_uint, 1), + ("visitor_uninvited", c_uint, 1), + ("visitor", c_uint, 1), + ("calculated_inventory", c_uint, 1), + ("vision_good", c_uint, 1), + ("vision_damaged", c_uint, 1), + ("vision_missing", c_uint, 1), + ("breathing_good", c_uint, 1), + ("breathing_problem", c_uint, 1), + ("roaming_wilderness_population_source", c_uint, 1), + ("roaming_wilderness_population_source_not_a_map_feature", c_uint, 1)] + +class CreatureFlags2(Union): + _fields_ = [("whole", c_uint, 32), + ("bits", CreatureStruct2)] + + def __init__(self, initial = 0): + self.whole = initial + +class ItemStruct(Structure): + _fields_ = [("on_ground", c_uint, 1), + ("in_job", c_uint, 1), + ("in_inventory", c_uint, 1), + ("u_ngrd1", c_uint, 1), + ("in_workshop", c_uint, 1), + ("u_ngrd2", c_uint, 1), + ("u_ngrd3", c_uint, 1), + ("rotten", c_uint, 1), + ("unk1", c_uint, 1), + ("u_ngrd4", c_uint, 1), + ("unk2", c_uint, 1), + ("u_ngrd5", c_uint, 1), + ("unk3", c_uint, 1), + ("u_ngrd6", c_uint, 1), + ("narrow", c_uint, 1), + ("u_ngrd7", c_uint, 1), + ("worn", c_uint, 1), + ("unk4", c_uint, 1), + ("u_ngrd8", c_uint, 1), + ("forbid", c_uint, 1), + ("unk5", c_uint, 1), + ("dump", c_uint, 1), + ("on_fire", c_uint, 1), + ("melt", c_uint, 1), + ("hidden", c_uint, 1), + ("u_ngrd9", c_uint, 1), + ("unk6", c_uint, 1), + ("unk7", c_uint, 1), + ("unk8", c_uint, 1), + ("unk9", c_uint, 1), + ("unk10", c_uint, 1), + ("unk11", c_uint, 1)] + +class ItemFlags(Union): + _fields_ = [("whole", c_uint, 32), + ("bits", ItemStruct)] + + def __init__(self, initial = 0): + self.whole = initial dig_types = { "no" : 0, "default" : 1, @@ -242,182 +221,3 @@ traffic_types = { "normal" : 0, "low" : 1, "high" : 2, "restricted" : 3 } - -class DesignationFlags(_UnionBase): - def dig(self, dig_type): - if dig_type == "no": - self.dig_1 = False; self.dig_2 = False; self.dig_3 = False - elif dig_type == "default": - self.dig_1 = True; self.dig_2 = False; self.dig_3 = False - elif dig_type == "ud_stair": - self.dig_1 = False; self.dig_2 = True; self.dig_3 = False - elif dig_type == "channel": - self.dig_1 = True; self.dig_2 = True; self.dig_3 = False - elif dig_type == "ramp": - self.dig_1 = False; self.dig_2 = False; self.dig_3 = True - elif dig_type == "d_stair": - self.dig_1 = True; self.dig_2 = False; self.dig_3 = True - elif dig_type == "u_stair": - self.dig_1 = False; self.dig_2 = True; self.dig_3 = True - elif dig_type == "whatever": - self.dig_1 = True; self.dig_2 = True; self.dig_3 = True - - def set_traffic(self, traffic_type): - if traffic_type == "normal": - self.traffic_1 = False; self.traffic_2 = False - elif traffic_type == "low": - self.traffic_1 = True; self.traffic_2 = False - elif traffic_type == "high": - self.traffic_1 = False; self.traffic_2 = True - elif traffic_type == "restricted": - self.traffic_1 = True; self.traffic_2 = True - - flow_size_1 = property(fget = lambda self: self._get_mask_bit(0), - fset = lambda self, val: self._set_mask_bit(0, val)) - flow_size_2 = property(fget = lambda self: self._get_mask_bit(1), - fset = lambda self, val: self._set_mask_bit(1, val)) - flow_size_3 = property(fget = lambda self: self._get_mask_bit(2), - fset = lambda self, val: self._set_mask_bit(2, val)) - pile = property(fget = lambda self: self._get_mask_bit(3), - fset = lambda self, val: self._set_mask_bit(3, val)) - - dig_1 = property(fget = lambda self: self._get_mask_bit(4), - fset = lambda self, val: self._set_mask_bit(4, val)) - dig_2 = property(fget = lambda self: self._get_mask_bit(5), - fset = lambda self, val: self._set_mask_bit(5, val)) - dig_3 = property(fget = lambda self: self._get_mask_bit(6), - fset = lambda self, val: self._set_mask_bit(6, val)) - smooth_1 = property(fget = lambda self: self._get_mask_bit(7), - fset = lambda self, val: self._set_mask_bit(7, val)) - - smooth_2 = property(fget = lambda self: self._get_mask_bit(8), - fset = lambda self, val: self._set_mask_bit(8, val)) - hidden = property(fget = lambda self: self._get_mask_bit(9), - fset = lambda self, val: self._set_mask_bit(9, val)) - geolayer_index_1 = property(fget = lambda self: self._get_mask_bit(10), - fset = lambda self, val: self._set_mask_bit(10, val)) - geolayer_index_2 = property(fget = lambda self: self._get_mask_bit(11), - fset = lambda self, val: self._set_mask_bit(11, val)) - - geolayer_index_3 = property(fget = lambda self: self._get_mask_bit(12), - fset = lambda self, val: self._set_mask_bit(12, val)) - geolayer_index_4 = property(fget = lambda self: self._get_mask_bit(13), - fset = lambda self: self._set_mask_bit(13, val)) - light = property(fget = lambda self: self._get_mask_bit(14), - fset = lambda self, val: self._set_mask_bit(14, val)) - subterranean = property(fget = lambda self: self._get_mask_bit(15), - fset = lambda self, val: self._set_mask_bit(15, val)) - - skyview = property(fget = lambda self: self._get_mask_bit(16), - fset = lambda self, val: self._set_mask_bit(16, val)) - biome_1 = property(fget = lambda self: self._get_mask_bit(17), - fset = lambda self, val: self._set_mask_bit(17, val)) - biome_2 = property(fget = lambda self: self._get_mask_bit(18), - fset = lambda self, val: self._set_mask_bit(18, val)) - biome_3 = property(fget = lambda self: self._get_mask_bit(19), - fset = lambda self, val: self._set_mask_bit(19, val)) - - biome_4 = property(fget = lambda self: self._get_mask_bit(20), - fset = lambda self, val: self._set_mask_bit(20, val)) - liquid_type = property(fget = lambda self: self._get_mask_bit(21), - fset = lambda self, val: self._set_mask_bit(21, val)) - water_table = property(fget = lambda self: self._get_mask_bit(22), - fset = lambda self, val: self._set_mask_bit(22, val)) - rained = property(fget = lambda self: self._get_mask_bit(23), - fset = lambda self, val: self._set_mask_bit(23, val)) - - traffic_1 = property(fget = lambda self: self._get_mask_bit(24), - fset = lambda self, val: self._set_mask_bit(24, val)) - traffic_2 = property(fget = lambda self: self._get_mask_bit(25), - fset = lambda self, val: self._set_mask_bit(25, val)) - flow_forbid = property(fget = lambda self: self._get_mask_bit(26), - fset = lambda self, val: self._set_mask_bit(26, val)) - liquid_static = property(fget = lambda self: self._get_mask_bit(27), - fset = lambda self, val: self._set_mask_bit(27, val)) - - moss = property(fget = lambda self: self._get_mask_bit(28), - fset = lambda self, val: self._set_mask_bit(28, val)) - feature_present = property(fget = lambda self: self._get_mask_bit(29), - fset = lambda self, val: self._set_mask_bit(29, val)) - liquid_character_1 = property(fget = lambda self: self._get_mask_bit(30), - fset = lambda self, val: self._set_mask_bit(30, val)) - liquid_character_2 = property(fget = lambda self: self._get_mask_bit(31), - fset = lambda self, val: self._set_mask_bit(31, val)) - -class OccupancyFlags(_UnionBase): - def set_splatter(self, state): - if state: - self.whole &= 0xFFFFFFC0 - else: - self.whole &= ~0xFFFFFFC0 - building_1 = property(fget = lambda self: self._get_mask_bit(0), - fset = lambda self, val: self._set_mask_bit(0, val)) - building_2 = property(fget = lambda self: self._get_mask_bit(1), - fset = lambda self, val: self._set_mask_bit(1, val)) - building_3 = property(fget = lambda self: self._get_mask_bit(2), - fset = lambda self, val: self._set_mask_bit(2, val)) - unit = property(fget = lambda self: self._get_mask_bit(3), - fset = lambda self, val: self._set_mask_bit(3, val)) - - unit_grounded = property(fget = lambda self: self._get_mask_bit(4), - fset = lambda self, val: self._set_mask_bit(4, val)) - item = property(fget = lambda self: self._get_mask_bit(5), - fset = lambda self, val: self._set_mask_bit(5, val)) - mud = property(fget = lambda self: self._get_mask_bit(6), - fset = lambda self, val: self._set_mask_bit(6, val)) - vomit = property(fget = lambda self: self._get_mask_bit(7), - fset = lambda self, val: self._set_mask_bit(7, val)) - - broken_arrows_color_1 = property(fget = lambda self: self._get_mask_bit(8), - fset = lambda self, val: self._set_mask_bit(8, val)) - broken_arrows_color_2 = property(fget = lambda self: self._get_mask_bit(9), - fset = lambda self, val: self._set_mask_bit(9, val)) - broken_arrows_color_3 = property(fget = lambda self: self._get_mask_bit(10), - fset = lambda self, val: self._set_mask_bit(10, val)) - broken_arrows_color_4 = property(fget = lambda self: self._get_mask_bit(11), - fset = lambda self, val: self._set_mask_bit(11, val)) - - blood_g = property(fget = lambda self: self._get_mask_bit(12), - fset = lambda self: self._set_mask_bit(12, val)) - blood_g2 = property(fget = lambda self: self._get_mask_bit(13), - fset = lambda self, val: self._set_mask_bit(13, val)) - blood_b = property(fget = lambda self: self._get_mask_bit(14), - fset = lambda self, val: self._set_mask_bit(14, val)) - blood_b2 = property(fget = lambda self: self._get_mask_bit(15), - fset = lambda self, val: self._set_mask_bit(15, val)) - - blood_y = property(fget = lambda self: self._get_mask_bit(16), - fset = lambda self, val: self._set_mask_bit(16, val)) - blood_y2 = property(fget = lambda self: self._get_mask_bit(17), - fset = lambda self, val: self._set_mask_bit(17, val)) - blood_m = property(fget = lambda self: self._get_mask_bit(18), - fset = lambda self, val: self._set_mask_bit(18, val)) - blood_m2 = property(fget = lambda self: self._get_mask_bit(19), - fset = lambda self, val: self._set_mask_bit(19, val)) - - blood_c = property(fget = lambda self: self._get_mask_bit(20), - fset = lambda self, val: self._set_mask_bit(20, val)) - blood_c2 = property(fget = lambda self: self._get_mask_bit(21), - fset = lambda self, val: self._set_mask_bit(21, val)) - blood_w = property(fget = lambda self: self._get_mask_bit(22), - fset = lambda self, val: self._set_mask_bit(22, val)) - blood_w2 = property(fget = lambda self: self._get_mask_bit(23), - fset = lambda self, val: self._set_mask_bit(23, val)) - - blood_o = property(fget = lambda self: self._get_mask_bit(24), - fset = lambda self, val: self._set_mask_bit(24, val)) - blood_o2 = property(fget = lambda self: self._get_mask_bit(25), - fset = lambda self, val: self._set_mask_bit(25, val)) - slime = property(fget = lambda self: self._get_mask_bit(26), - fset = lambda self, val: self._set_mask_bit(26, val)) - slime2 = property(fget = lambda self: self._get_mask_bit(27), - fset = lambda self, val: self._set_mask_bit(27, val)) - - blood = property(fget = lambda self: self._get_mask_bit(28), - fset = lambda self, val: self._set_mask_bit(28, val)) - blood2 = property(fget = lambda self: self._get_mask_bit(29), - fset = lambda self, val: self._set_mask_bit(29, val)) - broken_arrows_variant = property(fget = lambda self: self._get_mask_bit(30), - fset = lambda self, val: self._set_mask_bit(30, val)) - snow = property(fget = lambda self: self._get_mask_bit(31), - fset = lambda self, val: self._set_mask_bit(31, val)) From afc051d03214714a3adea4e8ef028abda7bba0d8 Mon Sep 17 00:00:00 2001 From: doomchild Date: Sat, 3 Apr 2010 20:58:06 -0500 Subject: [PATCH 02/37] added getter for window size added ReadHotkeys --- library/pydfhack/DF_API.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/pydfhack/DF_API.cpp b/library/pydfhack/DF_API.cpp index 708e99314..d70b969a5 100644 --- a/library/pydfhack/DF_API.cpp +++ b/library/pydfhack/DF_API.cpp @@ -905,9 +905,9 @@ static PyObject* DF_API_WriteDesignations(DF_API* self, PyObject* args) for(int j = 0; j < 16; j++) { - UnionBase* obj = (UnionBase*)PyList_GetItem(innerList, j); + unsigned int obj = (unsigned int)PyInt_AsUnsignedLongMask(PyList_GetItem(innerList, j)); - designations[i][j].whole = obj->whole; + designations[i][j].whole = obj; } } @@ -967,9 +967,9 @@ static PyObject* DF_API_WriteOccupancy(DF_API* self, PyObject* args) for(int j = 0; j < 16; j++) { - UnionBase* obj = (UnionBase*)PyList_GetItem(innerList, j); + unsigned int obj = PyInt_AsLong(PyList_GetItem(innerList, j)); - occupancies[i][j].whole = obj->whole; + occupancies[i][j].whole = obj; } } From 478974e399a7c5359c828f3df0bbd882f2d7f9a9 Mon Sep 17 00:00:00 2001 From: doomchild Date: Sat, 3 Apr 2010 21:01:11 -0500 Subject: [PATCH 03/37] removed UnionBase.cpp dependency --- library/pydfhack/pydfhack.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/library/pydfhack/pydfhack.cpp b/library/pydfhack/pydfhack.cpp index 23aa3a730..378d885bd 100644 --- a/library/pydfhack/pydfhack.cpp +++ b/library/pydfhack/pydfhack.cpp @@ -23,7 +23,7 @@ distribution. */ #include "Python.h" -#include "UnionBase.cpp" +//#include "UnionBase.cpp" #include "DF_API.cpp" #ifndef PyMODINIT_FUNC @@ -39,17 +39,17 @@ PyMODINIT_FUNC initpydfhack(void) { PyObject* module; - if(PyType_Ready(&UnionBase_type) < 0) - return; + // if(PyType_Ready(&UnionBase_type) < 0) + // return; if(PyType_Ready(&DF_API_type) < 0) return; module = Py_InitModule3("pydfhack", module_methods, "pydfhack extension module"); - Py_INCREF(&UnionBase_type); + //Py_INCREF(&UnionBase_type); Py_INCREF(&DF_API_type); - PyModule_AddObject(module, "UnionBase", (PyObject*)&UnionBase_type); + //PyModule_AddObject(module, "UnionBase", (PyObject*)&UnionBase_type); PyModule_AddObject(module, "API", (PyObject*)&DF_API_type); } \ No newline at end of file From 9a867e44a60b0ddcb5141dfbcac04f7367e5b171 Mon Sep 17 00:00:00 2001 From: doomchild Date: Sat, 3 Apr 2010 21:01:21 -0500 Subject: [PATCH 04/37] removed UnionBase.cpp dependency --- library/pydfhack/DF_API.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/pydfhack/DF_API.cpp b/library/pydfhack/DF_API.cpp index d70b969a5..83dd2e073 100644 --- a/library/pydfhack/DF_API.cpp +++ b/library/pydfhack/DF_API.cpp @@ -30,7 +30,7 @@ distribution. #include #include "DFTypes.h" #include "DFHackAPI.h" -#include "UnionBase.cpp" +//#include "UnionBase.cpp" //#include "MatGloss.cpp" using namespace std; From f0ed3f2098a84b076d47e81cde92ec6f50b6a77a Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 5 Apr 2010 11:08:05 -0500 Subject: [PATCH 05/37] turned off all the stuff that DF2010 moved into modules --- dfhack/python/DF_API.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/dfhack/python/DF_API.cpp b/dfhack/python/DF_API.cpp index 83dd2e073..4236b93c2 100644 --- a/dfhack/python/DF_API.cpp +++ b/dfhack/python/DF_API.cpp @@ -125,6 +125,8 @@ static PyObject* DF_API_getIsSuspended(DF_API* self, void* closure) Py_RETURN_FALSE; } +/* + static PyObject* DF_API_getIsPaused(DF_API* self, void* closure) { try @@ -335,17 +337,19 @@ static PyObject* DF_API_getWindowSize(DF_API* self, void* closure) Py_RETURN_NONE; } +*/ + static PyGetSetDef DF_API_getterSetters[] = { {"is_attached", (getter)DF_API_getIsAttached, NULL, "is_attached", NULL}, {"is_suspended", (getter)DF_API_getIsSuspended, NULL, "is_suspended", NULL}, - {"is_paused", (getter)DF_API_getIsPaused, NULL, "is_paused", NULL}, + /*{"is_paused", (getter)DF_API_getIsPaused, NULL, "is_paused", NULL}, {"menu_state", (getter)DF_API_getMenuState, NULL, "menu_state", NULL}, {"view_coords", (getter)DF_API_getViewCoords, (setter)DF_API_setViewCoords, "view_coords", NULL}, {"map_size", (getter)DF_API_getSize, NULL, "max_size", NULL}, {"cursor_coords", (getter)DF_API_getCursorCoords, (setter)DF_API_setCursorCoords, "cursor_coords", NULL}, {"current_cursor_creature", (getter)DF_API_getCurrentCursorCreature, NULL, "current_cursor_creature", NULL}, - {"window_size", (getter)DF_API_getWindowSize, NULL, "window_size", NULL}, + {"window_size", (getter)DF_API_getWindowSize, NULL, "window_size", NULL},*/ {NULL} // Sentinel }; @@ -437,6 +441,8 @@ static PyObject* DF_API_ForceResume(DF_API* self) Py_RETURN_FALSE; } +/* + static PyObject* DF_API_InitMap(DF_API* self) { try @@ -1183,6 +1189,8 @@ static PyObject* DF_API_InitViewAndCursor(DF_API* self, PyObject* args) } } +*/ + static PyMethodDef DF_API_methods[] = { {"Attach", (PyCFunction)DF_API_Attach, METH_NOARGS, "Attach to the DF process"}, @@ -1191,7 +1199,7 @@ static PyMethodDef DF_API_methods[] = {"Resume", (PyCFunction)DF_API_Resume, METH_NOARGS, "Resume the DF process"}, {"Async_Suspend", (PyCFunction)DF_API_AsyncSuspend, METH_NOARGS, "Asynchronously suspend the DF process"}, {"Force_Resume", (PyCFunction)DF_API_ForceResume, METH_NOARGS, "Force the DF process to resume"}, - {"Init_Map", (PyCFunction)DF_API_InitMap, METH_NOARGS, "Initialize the DFHack map reader"}, + /*{"Init_Map", (PyCFunction)DF_API_InitMap, METH_NOARGS, "Initialize the DFHack map reader"}, {"Destroy_Map", (PyCFunction)DF_API_DestroyMap, METH_NOARGS, "Shut down the DFHack map reader"}, {"Init_Read_Constructions", (PyCFunction)DF_API_InitReadConstructions, METH_NOARGS, "Initialize construction reader"}, {"Finish_Read_Constructions", (PyCFunction)DF_API_FinishReadConstructions, METH_NOARGS, "Shut down construction reader"}, @@ -1224,7 +1232,7 @@ static PyMethodDef DF_API_methods[] = {"Read_Metal_Matgloss", (PyCFunction)DF_API_ReadMetalMatgloss, METH_NOARGS, ""}, {"Read_Plant_Matgloss", (PyCFunction)DF_API_ReadPlantMatgloss, METH_NOARGS, ""}, {"Read_Creature_Matgloss", (PyCFunction)DF_API_ReadCreatureMatgloss, METH_NOARGS, ""}, - {"Init_View_And_Cursor", (PyCFunction)DF_API_InitViewAndCursor, METH_NOARGS, ""}, + {"Init_View_And_Cursor", (PyCFunction)DF_API_InitViewAndCursor, METH_NOARGS, ""},*/ {NULL} // Sentinel }; From 514b0a5e45c72a3a8a2cb60fe605abde30e8fe1b Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 5 Apr 2010 11:08:42 -0500 Subject: [PATCH 06/37] ReadRaw and WriteRaw are straight C functions, so's I can use ctypes to pass pointers in without a lot of list copying overhead --- dfhack/python/pydfhack.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index 378d885bd..cd72dbc17 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -30,6 +30,25 @@ distribution. #define PyMODINIT_FUNC void #endif +extern "C" +{ +void ReadRaw(DF_API* self, const uint32_t offset, const uint32_t size, uint8_t* target) +{ + if(self != NULL && self->api_Ptr != NULL) + { + self->api_Ptr->ReadRaw(offset, size, target); + } +} + +void WriteRaw(DF_API* self, const uint32_t offset, const uint32_t size, uint8_t* source) +{ + if(self != NULL && self->api_Ptr != NULL) + { + self->api_Ptr->WriteRaw(offset, size, source); + } +} +} + static PyMethodDef module_methods[] = { {NULL} //Sentinel From c7217cbdf798503906ae4b08f9e03a1944d72cf4 Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 5 Apr 2010 11:08:55 -0500 Subject: [PATCH 07/37] modified setup script to export ReadRaw and WriteRaw --- dfhack/python/setup.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dfhack/python/setup.py b/dfhack/python/setup.py index ad528a7a8..dda41f241 100644 --- a/dfhack/python/setup.py +++ b/dfhack/python/setup.py @@ -2,9 +2,10 @@ from distutils.core import setup, Extension e = Extension("pydfhack", - sources=["UnionBase.cpp", "pydfhack.cpp", "DF_API.cpp"], + sources=["pydfhack.cpp", "DF_API.cpp"], include_dirs=["..\\include"], library_dirs=["..\\..\\output"], - libraries=["libdfhack"]) + libraries=["libdfhack"], + export_symbols=["initpydfhack", "ReadRaw", "WriteRaw"]) setup(name="PyDFHack", version="1.0", ext_modules=[e]) From ab0b9f48e982ad48497308646e6f74deadac0640 Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 5 Apr 2010 14:58:15 -0500 Subject: [PATCH 08/37] first commit --- dfhack/python/MemInfo.cpp | 688 ++++++++++++++++++++++++++++++++++++ dfhack/python/Process.cpp | 23 ++ dfhack/python/UnionBase.cpp | 215 ----------- dfhack/python/matgloss.cpp | 231 ------------ 4 files changed, 711 insertions(+), 446 deletions(-) create mode 100644 dfhack/python/MemInfo.cpp create mode 100644 dfhack/python/Process.cpp delete mode 100644 dfhack/python/UnionBase.cpp delete mode 100644 dfhack/python/matgloss.cpp diff --git a/dfhack/python/MemInfo.cpp b/dfhack/python/MemInfo.cpp new file mode 100644 index 000000000..fab813db1 --- /dev/null +++ b/dfhack/python/MemInfo.cpp @@ -0,0 +1,688 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DF_MEMINFO__ +#define __DF_MEMINFO__ + +#include "Python.h" +#include + +using namespace std; + +#include "Export.h" +#include "DFCommonInternal.h" +#include "DFMemInfo.h" + +using namespace DFHack; + +struct DF_MemInfo +{ + PyObject_HEAD + DFHack::memory_info* mem_Ptr; +}; + +// MemInfo type Allocation, Deallocation, and Initialization + +static PyObject* DF_MemInfo_new(PyTypeObject* type, PyObject* args, PyObject* kwds) +{ + DF_MemInfo* self; + + self = (DF_MemInfo*)type->tp_alloc(type, 0); + + if(self != NULL) + { + self->mem_Ptr = NULL; + } + + return (PyObject*)self; +} + +static int DF_MemInfo_init(DF_MemInfo* self, PyObject* args, PyObject* kwds) +{ + DF_MemInfo* copy = NULL; + + if(self->mem_Ptr == NULL) + { + PyArg_ParseTuple(args, "|O", ©); + + if(copy != NULL) + self->mem_Ptr = new DFHack::memory_info(*(copy->mem_Ptr)); + else + self->mem_Ptr = new DFHack::memory_info(); + } + + return 0; +} + +static void DF_MemInfo_dealloc(DF_MemInfo* self) +{ + if(self != NULL) + { + if(self->mem_Ptr != NULL) + { + delete self->mem_Ptr; + + self->mem_Ptr = NULL; + } + + self->ob_type->tp_free((PyObject*)self); + } +} + +// Setters/Getters + +static PyObject* DF_MemInfo_getBase(DF_MemInfo* self) +{ + if(self->mem_Ptr != NULL) + { + return PyInt_FromLong(self->mem_Ptr->getBase()); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_setBase(DF_MemInfo* self, PyObject* args) +{ + PyObject* arg = NULL; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "O", arg)) + { + PyErr_SetString(PyExc_ValueError, "Bad argument list"); + return NULL; + } + + if(PyString_Check(arg)) + self->mem_Ptr->setBase(std::string(PyString_AsString(arg))); + else if(PyInt_Check(arg)) + self->mem_Ptr->setBase((uint32_t)PyInt_AsUnsignedLongMask(arg)); + else + { + PyErr_SetString(PyExc_ValueError, "Argument must be string or integer"); + return NULL; + } + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_getVersion(DF_MemInfo* self) +{ + if(self->mem_Ptr != NULL) + { + return PyString_FromString(self->mem_Ptr->getVersion().c_str()); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_setVersion(DF_MemInfo* self, PyObject* args) +{ + const char* v = NULL; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "s", &v)) + return NULL; + + self->mem_Ptr->setVersion(v); + } + + Py_RETURN_NONE; +} + +static PyGetSetDef DF_MemInfo_getterSetters[] = +{ + {"base", (getter)DF_MemInfo_getBase, (setter)DF_MemInfo_setBase, "base", NULL}, + {"version", (getter)DF_MemInfo_getVersion, (setter)DF_MemInfo_setVersion, "version", NULL}, + {NULL} // Sentinel +}; + +// Member methods + +static PyObject* DF_MemInfo_GetOffset(DF_MemInfo* self, PyObject* args) +{ + const char* s = NULL; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "s", &s)) + return NULL; + + return PyInt_FromLong(self->mem_Ptr->getOffset(s)); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_SetOffset(DF_MemInfo* self, PyObject* args) +{ + // const char* s = NULL; + // const char* n = NULL; + // int32_t i; + // PyObject* obj; + + // if(self->mem_Ptr != NULL) + // { + // if(!PyArg_ParseTuple(args, "sO", &s, &obj)) + // return NULL; + + // if(PyString_Check(obj)) + // { + // n = PyString_AsString(obj); + // self->mem_Ptr->setOffset(s, n); + // } + // else if(PyInt_Check(obj)) + // { + // i = (int32_t)PyInt_AsLong(obj); + // self->mem_Ptr->setOffset(s, i); + // } + // } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_GetAddress(DF_MemInfo* self, PyObject* args) +{ + const char* s = NULL; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "s", &s)) + return NULL; + + return PyInt_FromLong(self->mem_Ptr->getAddress(s)); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_SetAddress(DF_MemInfo* self, PyObject* args) +{ + // const char* s = NULL; + // const char* n = NULL; + // int32_t i; + // PyObject* obj; + + // if(self->mem_Ptr != NULL) + // { + // if(!PyArg_ParseTuple(args, "sO", &s, &obj)) + // return NULL; + + // if(PyString_Check(obj)) + // { + // n = PyString_AsString(obj); + // self->mem_Ptr->setAddress(s, n); + // } + // else if(PyInt_Check(obj)) + // { + // i = (int32_t)PyInt_AsLong(obj); + // self->mem_Ptr->setAddress(s, i); + // } + // } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_GetHexValue(DF_MemInfo* self, PyObject* args) +{ + const char* s = NULL; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "s", &s)) + return NULL; + + return PyInt_FromLong(self->mem_Ptr->getHexValue(s)); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_SetHexValue(DF_MemInfo* self, PyObject* args) +{ + // const char* s = NULL; + // const char* n = NULL; + // int32_t i; + // PyObject* obj; + + // if(self->mem_Ptr != NULL) + // { + // if(!PyArg_ParseTuple(args, "sO", &s, &obj)) + // return NULL; + + // if(PyString_Check(obj)) + // { + // n = PyString_AsString(obj); + // self->mem_Ptr->setHexValue(s, n); + // } + // else if(PyInt_Check(obj)) + // { + // i = (int32_t)PyInt_AsLong(obj); + // self->mem_Ptr->setHexValue(s, i); + // } + // } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_GetString(DF_MemInfo* self, PyObject* args) +{ + const char* s = NULL; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "s", &s)) + return NULL; + + return PyString_FromString(self->mem_Ptr->getString(s).c_str()); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_SetString(DF_MemInfo* self, PyObject* args) +{ + // const char* s = NULL; + // const char* n = NULL; + + // if(self->mem_Ptr != NULL) + // { + // if(!PyArg_ParseTuple(args, "ss", &s, &n)) + // return NULL; + + // self->mem_Ptr->setString(s, n); + // } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_GetProfession(DF_MemInfo* self, PyObject* args) +{ + uint32_t s; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "I", &s)) + return NULL; + + return PyString_FromString(self->mem_Ptr->getProfession(s).c_str()); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_SetProfession(DF_MemInfo* self, PyObject* args) +{ + const char* s = NULL; + const char* n = NULL; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "ss", &s, &n)) + return NULL; + + self->mem_Ptr->setProfession(s, n); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_GetJob(DF_MemInfo* self, PyObject* args) +{ + uint32_t s; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "I", &s)) + return NULL; + + return PyString_FromString(self->mem_Ptr->getJob(s).c_str()); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_SetJob(DF_MemInfo* self, PyObject* args) +{ + const char* s = NULL; + const char* n = NULL; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "ss", &s, &n)) + return NULL; + + self->mem_Ptr->setJob(s, n); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_GetSkill(DF_MemInfo* self, PyObject* args) +{ + uint32_t s; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "I", &s)) + return NULL; + + return PyString_FromString(self->mem_Ptr->getSkill(s).c_str()); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_SetSkill(DF_MemInfo* self, PyObject* args) +{ + const char* s = NULL; + const char* n = NULL; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "ss", &s, &n)) + return NULL; + + self->mem_Ptr->setSkill(s, n); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_GetTrait(DF_MemInfo* self, PyObject* args) +{ + uint32_t s, n; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "II", &s, &n)) + return NULL; + + return PyString_FromString(self->mem_Ptr->getTrait(s, n).c_str()); + } + + Py_RETURN_NONE; +} + +// I have no idea what any of the strings are, so I can't really put meaningful names - doomchild +static PyObject* DF_MemInfo_SetTrait(DF_MemInfo* self, PyObject* args) +{ + const char* a = NULL; + const char* b = NULL; + const char* c = NULL; + const char* d = NULL; + const char* e = NULL; + const char* f = NULL; + const char* g = NULL; + const char* h = NULL; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "ssssssss", &a, &b, &c, &d, &e, &f, &g, &h)) + return NULL; + + self->mem_Ptr->setTrait(a, b, c, d, e, f, g, h); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_GetTraitName(DF_MemInfo* self, PyObject* args) +{ + uint32_t s; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "I", &s)) + return NULL; + + return PyString_FromString(self->mem_Ptr->getTraitName(s).c_str()); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_GetLabor(DF_MemInfo* self, PyObject* args) +{ + uint32_t s; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "i", &s)) + return NULL; + + return PyString_FromString(self->mem_Ptr->getLabor(s).c_str()); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_SetLabor(DF_MemInfo* self, PyObject* args) +{ + const char* s = NULL; + const char* n = NULL; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "ss", &s, &n)) + return NULL; + + self->mem_Ptr->setLabor(s, n); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_RebaseAddresses(DF_MemInfo* self, PyObject* args) +{ + int32_t base; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "i", &base)) + return NULL; + + self->mem_Ptr->RebaseAddresses(base); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_RebaseAll(DF_MemInfo* self, PyObject* args) +{ + int32_t base; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "i", &base)) + return NULL; + + self->mem_Ptr->RebaseAll(base); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_RebaseVTable(DF_MemInfo* self, PyObject* args) +{ + int32_t offset; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "i", &offset)) + return NULL; + + self->mem_Ptr->RebaseVTable(offset); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_ResolveObjectToClassID(DF_MemInfo* self, PyObject* args) +{ + uint32_t address; + int32_t classID; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "I", &address)) + return NULL; + + if(self->mem_Ptr->resolveObjectToClassID(address, classID)) + return PyInt_FromLong(classID); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_ResolveClassNameToClassID(DF_MemInfo* self, PyObject* args) +{ + // const char* name; + // int32_t classID; + + // if(self->mem_Ptr != NULL) + // { + // if(!PyArg_ParseTuple(args, "s", &name)) + // return NULL; + + // if(self->mem_Ptr->resolveClassnameToClassID(name, classID)) + // return PyInt_FromLong(classID); + // } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_ResolveClassNameToVPtr(DF_MemInfo* self, PyObject* args) +{ + const char* n; + uint32_t vPtr; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "s", &n)) + return NULL; + + std::string name(n); + + if(self->mem_Ptr->resolveClassnameToVPtr(name, vPtr)) + return PyInt_FromLong(vPtr); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_MemInfo_ResolveClassIDToClassName(DF_MemInfo* self, PyObject* args) +{ + int32_t id; + string name; + + if(self->mem_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "i", &id)) + return NULL; + + if(self->mem_Ptr->resolveClassIDToClassname(id, name)) + return PyString_FromString(name.c_str()); + } + + Py_RETURN_NONE; +} + +static PyMethodDef DF_MemInfo_methods[] = +{ + {"Rebase_Addresses", (PyCFunction)DF_MemInfo_RebaseAddresses, METH_VARARGS, ""}, + {"Rebase_All", (PyCFunction)DF_MemInfo_RebaseAll, METH_VARARGS, ""}, + {"Rebase_VTable", (PyCFunction)DF_MemInfo_RebaseVTable, METH_VARARGS, ""}, + {"Get_Offset", (PyCFunction)DF_MemInfo_GetOffset, METH_VARARGS, ""}, + {"Set_Offset", (PyCFunction)DF_MemInfo_SetOffset, METH_VARARGS, ""}, + {"Get_Address", (PyCFunction)DF_MemInfo_GetAddress, METH_VARARGS, ""}, + {"Set_Address", (PyCFunction)DF_MemInfo_SetAddress, METH_VARARGS, ""}, + {"Get_HexValue", (PyCFunction)DF_MemInfo_GetHexValue, METH_VARARGS, ""}, + {"Set_HexValue", (PyCFunction)DF_MemInfo_SetHexValue, METH_VARARGS, ""}, + {"Get_String", (PyCFunction)DF_MemInfo_GetString, METH_VARARGS, ""}, + {"Set_String", (PyCFunction)DF_MemInfo_SetString, METH_VARARGS, ""}, + {"Get_Profession", (PyCFunction)DF_MemInfo_GetProfession, METH_VARARGS, ""}, + {"Set_Profession", (PyCFunction)DF_MemInfo_SetProfession, METH_VARARGS, ""}, + {"Get_Job", (PyCFunction)DF_MemInfo_GetJob, METH_VARARGS, ""}, + {"Set_Job", (PyCFunction)DF_MemInfo_SetJob, METH_VARARGS, ""}, + {"Get_Skill", (PyCFunction)DF_MemInfo_GetSkill, METH_VARARGS, ""}, + {"Set_Skill", (PyCFunction)DF_MemInfo_SetSkill, METH_VARARGS, ""}, + {"Get_Trait", (PyCFunction)DF_MemInfo_GetTrait, METH_VARARGS, ""}, + {"Set_Trait", (PyCFunction)DF_MemInfo_SetTrait, METH_VARARGS, ""}, + {"Get_Trait_Name", (PyCFunction)DF_MemInfo_GetTraitName, METH_VARARGS, ""}, + {"Get_Labor", (PyCFunction)DF_MemInfo_GetLabor, METH_VARARGS, ""}, + {"Set_Labor", (PyCFunction)DF_MemInfo_SetLabor, METH_VARARGS, ""}, + {"Resolve_Object_To_Class_ID", (PyCFunction)DF_MemInfo_ResolveObjectToClassID, METH_VARARGS, ""}, + {"Resolve_Classname_To_Class_ID", (PyCFunction)DF_MemInfo_ResolveClassNameToClassID, METH_VARARGS, ""}, + {"Resolve_Classname_To_VPtr", (PyCFunction)DF_MemInfo_ResolveClassNameToVPtr, METH_VARARGS, ""}, + {"Resolve_Class_ID_To_Classname", (PyCFunction)DF_MemInfo_ResolveClassIDToClassName, METH_VARARGS, ""}, + {NULL} //Sentinel +}; + +static PyTypeObject DF_MemInfo_type = +{ + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "pydfhack.DF_MemInfo", /*tp_name*/ + sizeof(DF_MemInfo), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)DF_MemInfo_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "pydfhack DF_MemInfo objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DF_MemInfo_methods, /* tp_methods */ + 0, /* tp_members */ + DF_MemInfo_getterSetters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)DF_MemInfo_init, /* tp_init */ + 0, /* tp_alloc */ + DF_MemInfo_new, /* tp_new */ +}; + +#endif \ No newline at end of file diff --git a/dfhack/python/Process.cpp b/dfhack/python/Process.cpp new file mode 100644 index 000000000..6a4e3eb31 --- /dev/null +++ b/dfhack/python/Process.cpp @@ -0,0 +1,23 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ \ No newline at end of file diff --git a/dfhack/python/UnionBase.cpp b/dfhack/python/UnionBase.cpp deleted file mode 100644 index d5c2507a1..000000000 --- a/dfhack/python/UnionBase.cpp +++ /dev/null @@ -1,215 +0,0 @@ -/* -www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any -damages arising from the use of this software. - -Permission is granted to anyone to use this software for any -purpose, including commercial applications, and to alter it and -redistribute it freely, subject to the following restrictions: - -1. The origin of this software must not be misrepresented; you must -not claim that you wrote the original software. If you use this -software in a product, an acknowledgment in the product documentation -would be appreciated but is not required. - -2. Altered source versions must be plainly marked as such, and -must not be misrepresented as being the original software. - -3. This notice may not be removed or altered from any source -distribution. -*/ - -#ifndef __UNIONBASE__ -#define __UNIONBASE__ - -#include - -struct UnionBase -{ - PyObject_HEAD - unsigned int whole; -}; - -static PyObject* UnionBase_new(PyTypeObject* type, PyObject* args, PyObject* kwds) -{ - UnionBase* self; - - self = (UnionBase*)type->tp_alloc(type, 0); - - if(self != NULL) - self->whole = 0; - else - return NULL; - - return (PyObject*)self; -} - -static int UnionBase_init(UnionBase* self, PyObject* args, PyObject* kwds) -{ - if(!PyArg_ParseTuple(args, "|I", &self->whole)) - return -1; - - return 0; -} - -static void UnionBase_dealloc(UnionBase* self) -{ - self->ob_type->tp_free((PyObject*)self); -} - -static PyObject* UnionBase_GetBitMask(UnionBase* self, PyObject* args) -{ - unsigned int mask; - - if(!PyArg_ParseTuple(args, "I", &mask)) - Py_RETURN_NONE; - - if((self->whole & mask) != 0) - Py_RETURN_TRUE; - - Py_RETURN_FALSE; -} - -static PyObject* UnionBase_SetBitMask(UnionBase* self, PyObject* args) -{ - unsigned int mask; - int on; - - if(!PyArg_ParseTuple(args, "Ii", &mask, &on)) - return NULL; - - if(on) - self->whole |= mask; - else - self->whole &= ~mask; - - Py_RETURN_TRUE; -} - -static PyObject* UnionBase_GetSingleBit(UnionBase* self, PyObject* args) -{ - unsigned int mask; - - if(!PyArg_ParseTuple(args, "I", &mask)) - Py_RETURN_NONE; - - if((self->whole & (1 << mask)) != 0) - Py_RETURN_TRUE; - - Py_RETURN_FALSE; -} - -static PyObject* UnionBase_SetSingleBit(UnionBase* self, PyObject* args) -{ - unsigned int mask; - int on; - - if(!PyArg_ParseTuple(args, "Ii", &mask, &on)) - return NULL; - - if(on) - self->whole |= (1 << mask); - else - self->whole &= ~(1 << mask); - - Py_RETURN_TRUE; -} - -static PyObject* UnionBase_int(PyObject* self) -{ - return PyInt_FromLong(((UnionBase*)self)->whole); -} - -static PyObject* UnionBase_long(PyObject* self) -{ - return PyInt_FromLong(((UnionBase*)self)->whole); -} - -static PyObject* UnionBase_hex(PyObject* self) -{ - return PyNumber_ToBase(PyInt_FromLong(((UnionBase*)self)->whole), 16); -} - -static PyMethodDef UnionBase_methods[] = -{ - {"_get_mask_bit", (PyCFunction)UnionBase_GetSingleBit, METH_VARARGS, "Get mask bit"}, - {"_set_mask_bit", (PyCFunction)UnionBase_SetSingleBit, METH_VARARGS, "Set mask bit"}, - {"_get_mask", (PyCFunction)UnionBase_GetBitMask, METH_VARARGS, ""}, - {"_set_mask", (PyCFunction)UnionBase_SetBitMask, METH_VARARGS, ""}, - {NULL} //Sentinel -}; - -static PyNumberMethods UnionBase_as_number[] = -{ - 0, // binaryfunc nb_add; /* __add__ */ - 0, // binaryfunc nb_subtract; /* __sub__ */ - 0, // binaryfunc nb_multiply; /* __mul__ */ - 0, // binaryfunc nb_divide; /* __div__ */ - 0, // binaryfunc nb_remainder; /* __mod__ */ - 0, // binaryfunc nb_divmod; /* __divmod__ */ - 0, // ternaryfunc nb_power; /* __pow__ */ - 0, // unaryfunc nb_negative; /* __neg__ */ - 0, // unaryfunc nb_positive; /* __pos__ */ - 0, // unaryfunc nb_absolute; /* __abs__ */ - 0, // inquiry nb_nonzero; /* __nonzero__ */ - 0, // unaryfunc nb_invert; /* __invert__ */ - 0, // binaryfunc nb_lshift; /* __lshift__ */ - 0, // binaryfunc nb_rshift; /* __rshift__ */ - 0, // binaryfunc nb_and; /* __and__ */ - 0, // binaryfunc nb_xor; /* __xor__ */ - 0, // binaryfunc nb_or; /* __or__ */ - 0, // coercion nb_coerce; /* __coerce__ */ - UnionBase_int, // unaryfunc nb_int; /* __int__ */ - UnionBase_long, // unaryfunc nb_long; /* __long__ */ - 0, // unaryfunc nb_float; /* __float__ */ - 0, // unaryfunc nb_oct; /* __oct__ */ - UnionBase_hex, // unaryfunc nb_hex; /* __hex__ */ -}; - -static PyTypeObject UnionBase_type = -{ - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ - "pydfhack.UnionBase", /*tp_name*/ - sizeof(UnionBase), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)UnionBase_dealloc, /*tp_dealloc*/ - 0, /*tp_print*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_compare*/ - 0, /*tp_repr*/ - UnionBase_as_number, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ - "pydfhack UnionBase objects", /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - UnionBase_methods, /* tp_methods */ - 0, /* tp_members */ - 0, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - (initproc)UnionBase_init, /* tp_init */ - 0, /* tp_alloc */ - UnionBase_new, /* tp_new */ -}; - -#endif \ No newline at end of file diff --git a/dfhack/python/matgloss.cpp b/dfhack/python/matgloss.cpp deleted file mode 100644 index d48c227cc..000000000 --- a/dfhack/python/matgloss.cpp +++ /dev/null @@ -1,231 +0,0 @@ -/* -www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any -damages arising from the use of this software. - -Permission is granted to anyone to use this software for any -purpose, including commercial applications, and to alter it and -redistribute it freely, subject to the following restrictions: - -1. The origin of this software must not be misrepresented; you must -not claim that you wrote the original software. If you use this -software in a product, an acknowledgment in the product documentation -would be appreciated but is not required. - -2. Altered source versions must be plainly marked as such, and -must not be misrepresented as being the original software. - -3. This notice may not be removed or altered from any source -distribution. -*/ - -#ifndef __MATGLOSS__ -#define __MATGLOSS__ - -#include -#include -#include -#include "DFTypes.h" - -using namespace DFHack; - -// MatGloss - -struct MatGloss -{ - PyObject_HEAD - DFHack::t_matgloss native; -}; - -static PyObject* MatGloss_new(PyTypeObject* type, PyObject* arg, PyObject* kwds) -{ - MatGloss* self; - - self = (MatGloss*)type->tp_alloc(type, 0); - - self->native->id[0] = '\0'; - self->native->name[0] = '\0'; - - return (PyObject*)self; -} - -static void MatGloss_dealloc(MatGloss* self) -{ - self->ob_type->tp_free((PyObject*)self); -} - -static PyObject* MatGloss_GetID(MatGloss* self, void* closure) -{ - return PyString_FromString(self->native->id); -} - -static int MatGloss_SetID(MatGloss* self, PyObject* args, void* closure) -{ - char* temp; - - if(!PyArg_ParseTuple(args, "s", &temp)) - return -1; - - strncpy(self->id, temp, 127); - self->native->id[127] = '\0'; - - return 0; -} - -static PyObject* MatGloss_GetName(MatGloss* self, PyObject* args, void* closure) -{ - return PyString_FromString(self->native->name); -} - -static PyObject* MatGloss_SetName(MatGloss* self, PyObject* args, void* closure) -{ - char* temp; - - if(!PyArg_ParseTuple(args, "s", &temp)) - return -1; - - strncpy(self->native->name, temp, 127); - self->native->name[127] = '\0'; - - return 0; -} - -static PyGetSetDef MatGloss_getSetters[] = -{ - {"id", (getter)MatGloss_GetID, (setter)MatGloss_SetID, "id", NULL}, - {"name", (getter)MatGloss_GetName, (setter)MatGloss_SetName, "name", NULL}, - {NULL} -}; - -// MatGlossPlant - -struct MatGlossPlant -{ - PyObject_HEAD - DFHack::t_matglossPlant native; -}; - -static PyObject* MatGlossPlant_new(PyTypeObject* type, PyObject* arg, PyObject* kwds) -{ - MatGlossPlant* self; - - self = (MatGlossPlant*)type->tp_alloc(type, 0); - - self->native->id[0] = '\0'; - self->native->name[0] = '\0'; - self->native->drink_name[127] = '\0'; - self->native->food_name[127] = '\0'; - self->native->extract_name[127] = '\0'; - - return (PyObject*)self; -} - -static void MatGlossPlant_dealloc(MatGlossPlant* self) -{ - self->ob_type->tp_free((PyObject*)self); -} - -static PyObject* MatGlossPlant_GetID(MatGlossPlant* self, void* closure) -{ - return PyString_FromString(self->native->id); -} - -static int MatGlossPlant_SetID(MatGlossPlant* self, PyObject* args, void* closure) -{ - char* temp; - - if(!PyArg_ParseTuple(args, "s", &temp)) - return -1; - - strncpy(self->native->id, temp, 127); - self->native->id[127] = '\0'; - - return 0; -} - -static PyObject* MatGlossPlant_GetName(MatGlossPlant* self, PyObject* args, void* closure) -{ - return PyString_FromString(self->native->name); -} - -static PyObject* MatGlossPlant_SetName(MatGlossPlant* self, PyObject* args, void* closure) -{ - char* temp; - - if(!PyArg_ParseTuple(args, "s", &temp)) - return -1; - - strncpy(self->native->name, temp, 127); - self->native->name[127] = '\0'; - - return 0; -} - -static PyObject* MatGlossPlant_GetDrinkName(MatGlossPlant* self, PyObject* args, void* closure) -{ - return PyString_FromString(self->native->drink_name); -} - -static PyObject* MatGlossPlant_SetDrinkName(MatGlossPlant* self, PyObject* args, void* closure) -{ - char* temp; - - if(!PyArg_ParseTuple(args, "s", &temp)) - return -1; - - strncpy(self->native->drink_name, temp, 127); - self->native->drink_name[127] = '\0'; - - return 0; -} - -static PyObject* MatGlossPlant_GetFoodName(MatGlossPlant* self, PyObject* args, void* closure) -{ - return PyString_FromString(self->native->food_name); -} - -static PyObject* MatGlossPlant_SetFoodName(MatGlossPlant* self, PyObject* args, void* closure) -{ - char* temp; - - if(!PyArg_ParseTuple(args, "s", &temp)) - return -1; - - strncpy(self->native->food_name, temp, 127); - self->native->food_name[127] = '\0'; - - return 0; -} - -static PyObject* MatGlossPlant_GetExtractName(MatGlossPlant* self, PyObject* args, void* closure) -{ - return PyString_FromString(self->native->extract_name); -} - -static PyObject* MatGlossPlant_SetExtractName(MatGlossPlant* self, PyObject* args, void* closure) -{ - char* temp; - - if(!PyArg_ParseTuple(args, "s", &temp)) - return -1; - - strncpy(self->native->extract_name, temp, 127); - self->native->extract_name[127] = '\0'; - - return 0; -} - -static PyGetSetDef MatGlossPlant_getSetters[] = -{ - {"id", (getter)MatGlossPlant_GetID, (setter)MatGlossPlant_SetID, "id", NULL}, - {"name", (getter)MatGlossPlant_GetName, (setter)MatGlossPlant_SetName, "name", NULL}, - {"drink_name", (getter)MatGlossPlant_GetDrinkName, (setter)MatGlossPlant_SetDrinkName, "drink_name", NULL}, - {"food_name", (getter)MatGlossPlant_GetFoodName, (setter)MatGlossPlant_SetFoodName, "food_name", NULL}, - {"extract_name", (getter)MatGlossPlant_GetExtractName, (setter)MatGlossPlant_SetExtractName, "extract_name", NULL}, - {NULL} -}; - -#endif \ No newline at end of file From b79c50d4b779e8e1e58a2a3f162b2a9c38bed9c4 Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 5 Apr 2010 15:01:01 -0500 Subject: [PATCH 09/37] cleaned out the recently deceased code --- dfhack/python/DF_API.cpp | 1008 -------------------------------------- 1 file changed, 1008 deletions(-) diff --git a/dfhack/python/DF_API.cpp b/dfhack/python/DF_API.cpp index 4236b93c2..b84d72804 100644 --- a/dfhack/python/DF_API.cpp +++ b/dfhack/python/DF_API.cpp @@ -27,11 +27,8 @@ distribution. #include "Python.h" #include -#include #include "DFTypes.h" #include "DFHackAPI.h" -//#include "UnionBase.cpp" -//#include "MatGloss.cpp" using namespace std; using namespace DFHack; @@ -125,231 +122,10 @@ static PyObject* DF_API_getIsSuspended(DF_API* self, void* closure) Py_RETURN_FALSE; } -/* - -static PyObject* DF_API_getIsPaused(DF_API* self, void* closure) -{ - try - { - if(self->api_Ptr != NULL) - if(self->api_Ptr->ReadPauseState()) - Py_RETURN_TRUE; - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to read pause state"); - return NULL; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_getMenuState(DF_API* self, void* closure) -{ - uint32_t menuState = 0; - - try - { - if(self->api_Ptr != NULL) - { - menuState = self->api_Ptr->ReadMenuState(); - - return PyInt_FromLong(menuState); - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to read menu state"); - return NULL; - } - - Py_RETURN_NONE; -} - -static PyObject* DF_API_getViewCoords(DF_API* self, void* closure) -{ - int32_t x, y, z; - bool success; - - try - { - if(self->api_Ptr != NULL) - { - success = self->api_Ptr->getViewCoords(x, y, z); - - if(success) - { - return Py_BuildValue("iii", x, y, z); - } - else - Py_RETURN_NONE; - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to get view coordinates"); - return NULL; - } - - Py_RETURN_NONE; -} - -static int DF_API_setViewCoords(DF_API* self, PyObject* args, void* closure) -{ - int32_t x, y, z; - - try - { - if(self->api_Ptr != NULL) - { - if(args == NULL) - { - PyErr_SetString(PyExc_TypeError, "Cannot delete view coordinates"); - return -1; - } - - if(PyArg_ParseTuple(args, "iii", &x, &y, &z)) - { - self->api_Ptr->setViewCoords(x, y, z); - } - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to set view coordinates"); - return -1; - } - - return 0; -} - -static PyObject* DF_API_getSize(DF_API* self, void* closure) -{ - uint32_t x, y, z; - - try - { - if(self->api_Ptr != NULL) - { - self->api_Ptr->getSize(x, y, z); - return Py_BuildValue("III", x, y, z); - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to get view coordinates"); - return NULL; - } - - Py_RETURN_NONE; -} - -static PyObject* DF_API_getCursorCoords(DF_API* self, void* closure) -{ - int32_t x, y, z; - - try - { - if(self->api_Ptr != NULL) - { - self->api_Ptr->getCursorCoords(x, y, z); - return Py_BuildValue("iii", x, y, z); - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to get cursor coordinates"); - return NULL; - } - - Py_RETURN_NONE; -} - -static int DF_API_setCursorCoords(DF_API* self, PyObject* args, void* closure) -{ - int32_t x, y, z; - - try - { - if(self->api_Ptr != NULL) - { - if(args == NULL) - { - PyErr_SetString(PyExc_TypeError, "Cannot delete view coordinates"); - return -1; - } - - if(PyArg_ParseTuple(args, "iii", &x, &y, &z)) - { - self->api_Ptr->setCursorCoords(x, y, z); - } - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to set view coordinates"); - return -1; - } - - return 0; -} - -static PyObject* DF_API_getCurrentCursorCreature(DF_API* self, void* closure) -{ - uint32_t index; - - try - { - if(self->api_Ptr != NULL) - { - self->api_Ptr->getCurrentCursorCreature(index); - - return PyInt_FromLong(index); - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to get current cursor creature"); - return NULL; - } - - Py_RETURN_NONE; -} - -static PyObject* DF_API_getWindowSize(DF_API* self, void* closure) -{ - int32_t width, height; - - try - { - if(self->api_Ptr != NULL) - { - self->api_Ptr->getWindowSize(width, height); - return Py_BuildValue("ii", width, height); - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to get window size"); - return NULL; - } - - Py_RETURN_NONE; -} - -*/ - static PyGetSetDef DF_API_getterSetters[] = { {"is_attached", (getter)DF_API_getIsAttached, NULL, "is_attached", NULL}, {"is_suspended", (getter)DF_API_getIsSuspended, NULL, "is_suspended", NULL}, - /*{"is_paused", (getter)DF_API_getIsPaused, NULL, "is_paused", NULL}, - {"menu_state", (getter)DF_API_getMenuState, NULL, "menu_state", NULL}, - {"view_coords", (getter)DF_API_getViewCoords, (setter)DF_API_setViewCoords, "view_coords", NULL}, - {"map_size", (getter)DF_API_getSize, NULL, "max_size", NULL}, - {"cursor_coords", (getter)DF_API_getCursorCoords, (setter)DF_API_setCursorCoords, "cursor_coords", NULL}, - {"current_cursor_creature", (getter)DF_API_getCurrentCursorCreature, NULL, "current_cursor_creature", NULL}, - {"window_size", (getter)DF_API_getWindowSize, NULL, "window_size", NULL},*/ {NULL} // Sentinel }; @@ -441,756 +217,6 @@ static PyObject* DF_API_ForceResume(DF_API* self) Py_RETURN_FALSE; } -/* - -static PyObject* DF_API_InitMap(DF_API* self) -{ - try - { - if(self->api_Ptr != NULL) - if(self->api_Ptr->InitMap()) - Py_RETURN_TRUE; - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to initialize map"); - return NULL; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_DestroyMap(DF_API* self) -{ - try - { - if(self->api_Ptr != NULL) - if(self->api_Ptr->DestroyMap()) - Py_RETURN_TRUE; - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to destroy map"); - return NULL; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_InitReadConstructions(DF_API* self) -{ - uint32_t numConstructions = 0; - - try - { - if(self->api_Ptr != NULL) - if(self->api_Ptr->InitReadConstructions(numConstructions)) - return PyInt_FromLong(numConstructions); - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to read constructions"); - return NULL; - } - - Py_RETURN_NONE; -} - -static PyObject* DF_API_FinishReadConstructions(DF_API* self) -{ - try - { - if(self->api_Ptr != NULL) - { - self->api_Ptr->FinishReadConstructions(); - Py_RETURN_TRUE; - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to finish reading constructions"); - return NULL; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_InitReadBuildings(DF_API* self) -{ - uint32_t numBuildings = 0; - - try - { - if(self->api_Ptr != NULL) - if(self->api_Ptr->InitReadBuildings(numBuildings)) - return PyInt_FromLong(numBuildings); - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to read buildings"); - return NULL; - } - - Py_RETURN_NONE; -} - -static PyObject* DF_API_FinishReadBuildings(DF_API* self) -{ - try - { - if(self->api_Ptr != NULL) - { - self->api_Ptr->FinishReadBuildings(); - Py_RETURN_TRUE; - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to finish reading buildings"); - return NULL; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_InitReadEffects(DF_API* self) -{ - uint32_t numEffects = 0; - - try - { - if(self->api_Ptr != NULL) - if(self->api_Ptr->InitReadEffects(numEffects)) - return PyInt_FromLong(numEffects); - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to read effects"); - return NULL; - } - - Py_RETURN_NONE; -} - -static PyObject* DF_API_FinishReadEffects(DF_API* self) -{ - try - { - if(self->api_Ptr != NULL) - { - self->api_Ptr->FinishReadEffects(); - Py_RETURN_TRUE; - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to finish reading effects"); - return NULL; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_InitReadVegetation(DF_API* self) -{ - uint32_t numVegetation = 0; - - try - { - if(self->api_Ptr != NULL) - if(self->api_Ptr->InitReadVegetation(numVegetation)) - return PyInt_FromLong(numVegetation); - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to read vegetation"); - return NULL; - } - - Py_RETURN_NONE; -} - -static PyObject* DF_API_FinishReadVegetation(DF_API* self) -{ - try - { - if(self->api_Ptr != NULL) - { - self->api_Ptr->FinishReadVegetation(); - Py_RETURN_TRUE; - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to finish reading vegetation"); - return NULL; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_InitReadCreatures(DF_API* self) -{ - uint32_t numCreatures = 0; - - try - { - if(self->api_Ptr != NULL) - if(self->api_Ptr->InitReadCreatures(numCreatures)) - return PyInt_FromLong(numCreatures); - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to read creatures"); - return NULL; - } - - Py_RETURN_NONE; -} - -static PyObject* DF_API_FinishReadCreatures(DF_API* self) -{ - try - { - if(self->api_Ptr != NULL) - { - self->api_Ptr->FinishReadCreatures(); - Py_RETURN_TRUE; - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to finish reading creatures"); - return NULL; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_InitReadNotes(DF_API* self) -{ - uint32_t numNotes = 0; - - try - { - if(self->api_Ptr != NULL) - if(self->api_Ptr->InitReadNotes(numNotes)) - return PyInt_FromLong(numNotes); - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to read notes"); - return NULL; - } - - Py_RETURN_NONE; -} - -static PyObject* DF_API_FinishReadNotes(DF_API* self) -{ - try - { - if(self->api_Ptr != NULL) - { - self->api_Ptr->FinishReadNotes(); - Py_RETURN_TRUE; - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to finish reading notes"); - return NULL; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_InitReadSettlements(DF_API* self) -{ - uint32_t numSettlements = 0; - - try - { - if(self->api_Ptr != NULL) - if(self->api_Ptr->InitReadSettlements(numSettlements)) - return PyInt_FromLong(numSettlements); - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to read settlements"); - return NULL; - } - - Py_RETURN_NONE; -} - -static PyObject* DF_API_FinishReadSettlements(DF_API* self) -{ - try - { - if(self->api_Ptr != NULL) - { - self->api_Ptr->FinishReadSettlements(); - Py_RETURN_TRUE; - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to finish reading settlements"); - return NULL; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_InitReadItems(DF_API* self) -{ - uint32_t numItems = 0; - - try - { - if(self->api_Ptr != NULL) - if(self->api_Ptr->InitReadItems(numItems)) - return PyInt_FromLong(numItems); - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to read items"); - return NULL; - } - - Py_RETURN_NONE; -} - -static PyObject* DF_API_FinishReadItems(DF_API* self) -{ - try - { - if(self->api_Ptr != NULL) - { - self->api_Ptr->FinishReadItems(); - Py_RETURN_TRUE; - } - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to finish reading items"); - return NULL; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_InitReadHotkeys(DF_API* self) -{ - try - { - if(self->api_Ptr != NULL) - if(self->api_Ptr->InitReadHotkeys()) - Py_RETURN_TRUE; - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to initialize hotkey reader"); - return NULL; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_ReadHotkeys(DF_API* self, PyObject* args) -{ - PyObject* list; - - if(self->api_Ptr == NULL) - return NULL; - else - { - DFHack::t_hotkey hotkeys[NUM_HOTKEYS]; - - self->api_Ptr->ReadHotkeys(hotkeys); - - list = PyList_New(0); - - for(int i = 0; i < NUM_HOTKEYS; i++) - { - DFHack::t_hotkey key = hotkeys[i]; - - PyList_Append(list, Py_BuildValue("siiii", key.name, key.mode, key.x, key.y, key.z)); - } - - return list; - } - - Py_RETURN_NONE; -} - -static PyObject* DF_API_InitViewSize(DF_API* self) -{ - try - { - if(self->api_Ptr != NULL) - if(self->api_Ptr->InitViewSize()) - Py_RETURN_TRUE; - } - catch(...) - { - PyErr_SetString(PyExc_ValueError, "Error trying to initialize view size"); - return NULL; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_IsValidBlock(DF_API* self, PyObject* args) -{ - uint32_t blockx, blocky, blockz; - bool valid; - - if(self->api_Ptr != NULL) - { - if(!PyArg_ParseTuple(args, "III", &blockx, &blocky, &blockz)) - Py_RETURN_NONE; - - valid = self->api_Ptr->isValidBlock(blockx, blocky, blockz); - - if(valid) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_ReadDesignations(DF_API* self, PyObject* args) -{ - uint32_t blockx, blocky, blockz; - DFHack::designations40d designations; - PyObject* list = NULL; - - if(self->api_Ptr != NULL) - { - if(!PyArg_ParseTuple(args, "III", &blockx, &blocky, &blockz)) - Py_RETURN_NONE; - - self->api_Ptr->ReadDesignations(blockx, blocky, blockz, &designations); - - list = PyList_New(16); - - for(int i = 0; i < 16; i++) - { - PyObject* innerList = PyList_New(16); - - for(int j = 0; j < 16; j++) - { - PyList_SetItem(innerList, j, PyInt_FromLong(designations[i][j].whole)); - } - - PyList_SetItem(list, i, innerList); - } - } - - return list; -} - -static PyObject* DF_API_WriteDesignations(DF_API* self, PyObject* args) -{ - uint32_t blockx, blocky, blockz; - DFHack::designations40d designations; - PyObject* list; - - if(self->api_Ptr != NULL) - { - if(!PyArg_ParseTuple(args, "IIIO", &blockx, &blocky, &blockz, &list)) - Py_RETURN_NONE; - - for(int i = 0; i < 16; i++) - { - PyObject* innerList = PyList_GetItem(list, i); - - for(int j = 0; j < 16; j++) - { - unsigned int obj = (unsigned int)PyInt_AsUnsignedLongMask(PyList_GetItem(innerList, j)); - - designations[i][j].whole = obj; - } - } - - self->api_Ptr->WriteDesignations(blockx, blocky, blockz, &designations); - - Py_RETURN_TRUE; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_ReadOccupancy(DF_API* self, PyObject* args) -{ - uint32_t blockx, blocky, blockz; - DFHack::occupancies40d occupancies; - PyObject* list = NULL; - - if(self->api_Ptr != NULL) - { - if(!PyArg_ParseTuple(args, "III", &blockx, &blocky, &blockz)) - Py_RETURN_NONE; - - self->api_Ptr->ReadOccupancy(blockx, blocky, blockz, &occupancies); - - list = PyList_New(16); - - for(int i = 0; i < 16; i++) - { - PyObject* innerList = PyList_New(16); - - for(int j = 0; j < 16; j++) - { - PyList_SetItem(innerList, j, PyInt_FromLong(occupancies[i][j].whole)); - } - - PyList_SetItem(list, i, innerList); - } - } - - return list; -} - -static PyObject* DF_API_WriteOccupancy(DF_API* self, PyObject* args) -{ - uint32_t blockx, blocky, blockz; - DFHack::occupancies40d occupancies; - PyObject* list; - - if(self->api_Ptr != NULL) - { - if(!PyArg_ParseTuple(args, "IIIO", &blockx, &blocky, &blockz, &list)) - Py_RETURN_NONE; - - for(int i = 0; i < 16; i++) - { - PyObject* innerList = PyList_GetItem(list, i); - - for(int j = 0; j < 16; j++) - { - unsigned int obj = PyInt_AsLong(PyList_GetItem(innerList, j)); - - occupancies[i][j].whole = obj; - } - } - - self->api_Ptr->WriteOccupancy(blockx, blocky, blockz, &occupancies); - - Py_RETURN_TRUE; - } - - Py_RETURN_FALSE; -} - -static PyObject* DF_API_ReadDirtyBit(DF_API* self, PyObject* args) -{ - uint32_t blockx, blocky, blockz; - bool dirty; - - if(self->api_Ptr == NULL) - return NULL; - else - { - if(!PyArg_ParseTuple(args, "III", &blockx, &blocky, &blockz)) - return NULL; - - self->api_Ptr->ReadDirtyBit(blockx, blocky, blockz, dirty); - - if(dirty) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; - } -} - -static PyObject* DF_API_WriteDirtyBit(DF_API* self, PyObject* args) -{ - uint32_t blockx, blocky, blockz; - int dirtyFlag; - - if(self->api_Ptr == NULL) - return NULL; - else - { - if(!PyArg_ParseTuple(args, "IIIi", &blockx, &blocky, &blockz, &dirtyFlag)) - return NULL; - - self->api_Ptr->WriteDirtyBit(blockx, blocky, blockz, dirtyFlag); - - Py_RETURN_TRUE; - } -} - -static PyObject* DF_API_ReadStoneMatgloss(DF_API* self, PyObject* args) -{ - PyObject* dict; - - if(self->api_Ptr == NULL) - return NULL; - else - { - std::vector output; - std::vector::iterator iter; - - if(!self->api_Ptr->ReadStoneMatgloss(output)) - { - PyErr_SetString(PyExc_ValueError, "Error reading stone matgloss"); - return NULL; - } - - dict = PyDict_New(); - - for(iter = output.begin(); iter != output.end(); iter++) - { - t_matgloss item = *iter; - - PyDict_SetItemString(dict, item.id, Py_BuildValue("IIIs", item.fore, item.back, item.bright, item.name)); - } - - return dict; - } -} - -static PyObject* DF_API_ReadWoodMatgloss(DF_API* self, PyObject* args) -{ - PyObject* dict; - - if(self->api_Ptr == NULL) - return NULL; - else - { - std::vector output; - std::vector::iterator iter; - - if(!self->api_Ptr->ReadWoodMatgloss(output)) - { - PyErr_SetString(PyExc_ValueError, "Error reading wood matgloss"); - return NULL; - } - - dict = PyDict_New(); - - for(iter = output.begin(); iter != output.end(); iter++) - { - t_matgloss item = *iter; - - PyDict_SetItemString(dict, item.id, Py_BuildValue("IIIs", item.fore, item.back, item.bright, item.name)); - } - - return dict; - } -} - -static PyObject* DF_API_ReadMetalMatgloss(DF_API* self, PyObject* args) -{ - PyObject* dict; - - if(self->api_Ptr == NULL) - return NULL; - else - { - std::vector output; - std::vector::iterator iter; - - if(!self->api_Ptr->ReadMetalMatgloss(output)) - { - PyErr_SetString(PyExc_ValueError, "Error reading metal matgloss"); - return NULL; - } - - dict = PyDict_New(); - - for(iter = output.begin(); iter != output.end(); iter++) - { - t_matgloss item = *iter; - - PyDict_SetItemString(dict, item.id, Py_BuildValue("IIIs", item.fore, item.back, item.bright, item.name)); - } - - return dict; - } -} - -static PyObject* DF_API_ReadPlantMatgloss(DF_API* self, PyObject* args) -{ - PyObject* dict; - - if(self->api_Ptr == NULL) - return NULL; - else - { - std::vector output; - std::vector::iterator iter; - - if(!self->api_Ptr->ReadPlantMatgloss(output)) - { - PyErr_SetString(PyExc_ValueError, "Error reading plant matgloss"); - return NULL; - } - - dict = PyDict_New(); - - for(iter = output.begin(); iter != output.end(); iter++) - { - t_matglossPlant item = *iter; - - PyDict_SetItemString(dict, item.id, Py_BuildValue("IIIssss", item.fore, item.back, item.bright, item.name, item.drink_name, item.food_name, item.extract_name)); - } - - return dict; - } -} - -static PyObject* DF_API_ReadCreatureMatgloss(DF_API* self, PyObject* args) -{ - PyObject* dict; - - if(self->api_Ptr == NULL) - return NULL; - else - { - std::vector output; - std::vector::iterator iter; - - if(!self->api_Ptr->ReadStoneMatgloss(output)) - { - PyErr_SetString(PyExc_ValueError, "Error reading creature matgloss"); - return NULL; - } - - dict = PyDict_New(); - - for(iter = output.begin(); iter != output.end(); iter++) - { - t_matgloss item = *iter; - - PyDict_SetItemString(dict, item.id, Py_BuildValue("IIIs", item.fore, item.back, item.bright, item.name)); - } - - return dict; - } -} - -static PyObject* DF_API_InitViewAndCursor(DF_API* self, PyObject* args) -{ - if(self->api_Ptr == NULL) - return NULL; - else - { - if(self->api_Ptr->InitViewAndCursor()) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; - } -} - -*/ - static PyMethodDef DF_API_methods[] = { {"Attach", (PyCFunction)DF_API_Attach, METH_NOARGS, "Attach to the DF process"}, @@ -1199,40 +225,6 @@ static PyMethodDef DF_API_methods[] = {"Resume", (PyCFunction)DF_API_Resume, METH_NOARGS, "Resume the DF process"}, {"Async_Suspend", (PyCFunction)DF_API_AsyncSuspend, METH_NOARGS, "Asynchronously suspend the DF process"}, {"Force_Resume", (PyCFunction)DF_API_ForceResume, METH_NOARGS, "Force the DF process to resume"}, - /*{"Init_Map", (PyCFunction)DF_API_InitMap, METH_NOARGS, "Initialize the DFHack map reader"}, - {"Destroy_Map", (PyCFunction)DF_API_DestroyMap, METH_NOARGS, "Shut down the DFHack map reader"}, - {"Init_Read_Constructions", (PyCFunction)DF_API_InitReadConstructions, METH_NOARGS, "Initialize construction reader"}, - {"Finish_Read_Constructions", (PyCFunction)DF_API_FinishReadConstructions, METH_NOARGS, "Shut down construction reader"}, - {"Init_Read_Buildings", (PyCFunction)DF_API_InitReadBuildings, METH_NOARGS, "Initialize building reader"}, - {"Finish_Read_Buildings", (PyCFunction)DF_API_FinishReadBuildings, METH_NOARGS, "Shut down building reader"}, - {"Init_Read_Effects", (PyCFunction)DF_API_InitReadEffects, METH_NOARGS, "Initialize effect reader"}, - {"Finish_Read_Effects", (PyCFunction)DF_API_FinishReadEffects, METH_NOARGS, "Shut down effect reader"}, - {"Init_Read_Vegetation", (PyCFunction)DF_API_InitReadVegetation, METH_NOARGS, "Initialize vegetation reader"}, - {"Finish_Read_Vegetation", (PyCFunction)DF_API_FinishReadVegetation, METH_NOARGS, "Shut down vegetation reader"}, - {"Init_Read_Creatures", (PyCFunction)DF_API_InitReadCreatures, METH_NOARGS, "Initialize creature reader"}, - {"Finish_Read_Creatures", (PyCFunction)DF_API_FinishReadCreatures, METH_NOARGS, "Shut down creature reader"}, - {"Init_Read_Notes", (PyCFunction)DF_API_InitReadNotes, METH_NOARGS, "Initialize note reader"}, - {"Finish_Read_Notes", (PyCFunction)DF_API_FinishReadNotes, METH_NOARGS, "Shut down note reader"}, - {"Init_Read_Settlements", (PyCFunction)DF_API_InitReadSettlements, METH_NOARGS, "Initialize settlement reader"}, - {"Finish_Read_Settlements", (PyCFunction)DF_API_FinishReadSettlements, METH_NOARGS, "Shut down settlement reader"}, - {"Init_Read_Items", (PyCFunction)DF_API_InitReadItems, METH_NOARGS, "Initialize item reader"}, - {"Finish_Read_Items", (PyCFunction)DF_API_FinishReadItems, METH_NOARGS, "Shut down item reader"}, - {"Init_Read_Hotkeys", (PyCFunction)DF_API_InitReadHotkeys, METH_NOARGS, "Initialize hotkey reader"}, - {"Read_Hotkeys", (PyCFunction)DF_API_ReadHotkeys, METH_NOARGS, ""}, - {"Init_View_Size", (PyCFunction)DF_API_InitViewSize, METH_NOARGS, "Initialize view size reader"}, - {"Is_Valid_Block", (PyCFunction)DF_API_IsValidBlock, METH_VARARGS, ""}, - {"Read_Designations", (PyCFunction)DF_API_ReadDesignations, METH_VARARGS, "Read a block's designations"}, - {"Write_Designations", (PyCFunction)DF_API_WriteDesignations, METH_VARARGS, ""}, - {"Read_Occupancy", (PyCFunction)DF_API_ReadOccupancy, METH_VARARGS, ""}, - {"Write_Occupancy", (PyCFunction)DF_API_WriteOccupancy, METH_VARARGS, ""}, - {"Read_Dirty_Bit", (PyCFunction)DF_API_ReadDirtyBit, METH_VARARGS, ""}, - {"Write_Dirty_Bit", (PyCFunction)DF_API_WriteDirtyBit, METH_VARARGS, ""}, - {"Read_Stone_Matgloss", (PyCFunction)DF_API_ReadStoneMatgloss, METH_NOARGS, ""}, - {"Read_Wood_Matgloss", (PyCFunction)DF_API_ReadWoodMatgloss, METH_NOARGS, ""}, - {"Read_Metal_Matgloss", (PyCFunction)DF_API_ReadMetalMatgloss, METH_NOARGS, ""}, - {"Read_Plant_Matgloss", (PyCFunction)DF_API_ReadPlantMatgloss, METH_NOARGS, ""}, - {"Read_Creature_Matgloss", (PyCFunction)DF_API_ReadCreatureMatgloss, METH_NOARGS, ""}, - {"Init_View_And_Cursor", (PyCFunction)DF_API_InitViewAndCursor, METH_NOARGS, ""},*/ {NULL} // Sentinel }; From eb17353f3cc83d0bc9c151f918b060eacb0fecac Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 5 Apr 2010 16:12:37 -0500 Subject: [PATCH 10/37] added getter for memory info --- dfhack/python/DF_API.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/dfhack/python/DF_API.cpp b/dfhack/python/DF_API.cpp index b84d72804..f252af5dd 100644 --- a/dfhack/python/DF_API.cpp +++ b/dfhack/python/DF_API.cpp @@ -29,6 +29,7 @@ distribution. #include #include "DFTypes.h" #include "DFHackAPI.h" +#include "MemInfo.cpp" using namespace std; using namespace DFHack; @@ -36,6 +37,7 @@ using namespace DFHack; struct DF_API { PyObject_HEAD + PyObject* mem_info; DFHack::API* api_Ptr; }; @@ -59,6 +61,8 @@ static int DF_API_init(DF_API* self, PyObject* args, PyObject* kwds) if(self->api_Ptr == NULL) { + self->mem_info = NULL; + if(!PyArg_ParseTuple(args, "s", &memFileString)) return -1; @@ -82,6 +86,8 @@ static void DF_API_dealloc(DF_API* self) self->api_Ptr = NULL; } + Py_XDECREF(self->mem_info); + self->ob_type->tp_free((PyObject*)self); } } @@ -122,10 +128,40 @@ static PyObject* DF_API_getIsSuspended(DF_API* self, void* closure) Py_RETURN_FALSE; } +static PyObject* DF_API_getMemoryInfo(DF_API* self, void* closure) +{ + if(self->mem_info != NULL) + return self->mem_info; + + try + { + if(self->api_Ptr != NULL) + { + DFHack::memory_info* mem; + + mem = self->api_Ptr->getMemoryInfo(); + + self->mem_info = _PyObject_New(&DF_MemInfo_type); + delete ((DF_MemInfo*)(self->mem_info))->mem_Ptr; + ((DF_MemInfo*)(self->mem_info))->mem_Ptr = mem; + + return self->mem_info; + } + } + catch(...) + { + PyErr_SetString(PyExc_ValueError, "Error trying to read memory info"); + return NULL; + } + + Py_RETURN_NONE; +} + static PyGetSetDef DF_API_getterSetters[] = { {"is_attached", (getter)DF_API_getIsAttached, NULL, "is_attached", NULL}, {"is_suspended", (getter)DF_API_getIsSuspended, NULL, "is_suspended", NULL}, + {"memory_info", (getter)DF_API_getMemoryInfo, NULL, "memory_info", NULL}, {NULL} // Sentinel }; From b40d0e4a4034e36287e5bab6fb7a830be1b52585 Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 5 Apr 2010 16:43:05 -0500 Subject: [PATCH 11/37] MemInfo class is exposed --- dfhack/python/pydfhack.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index cd72dbc17..f743da267 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -23,7 +23,7 @@ distribution. */ #include "Python.h" -//#include "UnionBase.cpp" +#include "MemInfo.cpp" #include "DF_API.cpp" #ifndef PyMODINIT_FUNC @@ -47,7 +47,7 @@ void WriteRaw(DF_API* self, const uint32_t offset, const uint32_t size, uint8_t* self->api_Ptr->WriteRaw(offset, size, source); } } -} +}; static PyMethodDef module_methods[] = { @@ -58,17 +58,17 @@ PyMODINIT_FUNC initpydfhack(void) { PyObject* module; - // if(PyType_Ready(&UnionBase_type) < 0) - // return; - if(PyType_Ready(&DF_API_type) < 0) return; + if(PyType_Ready(&DF_MemInfo_type) < 0) + return; + module = Py_InitModule3("pydfhack", module_methods, "pydfhack extension module"); - //Py_INCREF(&UnionBase_type); Py_INCREF(&DF_API_type); + Py_INCREF(&DF_MemInfo_type); - //PyModule_AddObject(module, "UnionBase", (PyObject*)&UnionBase_type); PyModule_AddObject(module, "API", (PyObject*)&DF_API_type); + PyModule_AddObject(module, "MemInfo", (PyObject*)&DF_MemInfo_type); } \ No newline at end of file From 884a6144a892dab727c0ed5df0ce25bbb048861f Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 5 Apr 2010 16:45:15 -0500 Subject: [PATCH 12/37] first commit --- dfhack/python/Position.cpp | 201 +++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 dfhack/python/Position.cpp diff --git a/dfhack/python/Position.cpp b/dfhack/python/Position.cpp new file mode 100644 index 000000000..debfa400a --- /dev/null +++ b/dfhack/python/Position.cpp @@ -0,0 +1,201 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DFPOSITION__ +#define __DFPOSITION__ + +#include "Python.h" +#include "Position.h" + +using namespace DFHack; + +struct DF_Position +{ + PyObject_HEAD + DFHack::Position* pos_Ptr; +}; + +// API type Allocation, Deallocation, and Initialization + +static PyObject* DF_Position_new(PyTypeObject* type, PyObject* args, PyObject* kwds) +{ + DF_Position* self; + + self = (DF_Position*)type->tp_alloc(type, 0); + + if(self != NULL) + self->pos_Ptr = NULL; + + return (PyObject*)self; +} + +static int DF_Position_init(DF_Position* self, PyObject* args, PyObject* kwds) +{ + return 0; +} + +static void DF_Position_dealloc(DF_API* self) +{ + if(self != NULL) + { + if(self->pos_Ptr != NULL) + { + delete self->pos_Ptr; + + self->pos_Ptr = NULL; + } + + self->ob_type->tp_free((PyObject*)self); + } +} + +// Getters/Setters + +static PyObject* DF_Position_getViewCoords(DF_Position* self, void* closure) +{ + int32_t x, y, z; + + if(self->pos_Ptr != NULL) + { + if(self->pos_Ptr->getViewCoords(&x, &y, &z)) + return Py_BuildValue("iii", x, y, z); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Position_setViewCoords(DF_Position* self, PyObject* args) +{ + int32_t x, y, z; + + if(self->pos_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "iii", &x, &y, &z)) + return NULL; + + if(self->pos_Ptr->setViewCoords(x, y, z)) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Position_getCursorCoords(DF_Position* self, void* closure) +{ + int32_t x, y, z; + + if(self->pos_Ptr != NULL) + { + if(self->pos_Ptr->getCursorCoords(&x, &y, &z)) + return Py_BuildValue("iii", x, y, z); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Position_setCursorCoords(DF_Position* self, PyObject* args) +{ + int32_t x, y, z; + + if(self->pos_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "iii", &x, &y, &z)) + return NULL; + + if(self->pos_Ptr->setCursorCoords(x, y, z)) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Position_getWindowSize(DF_Position* self, void* closure) +{ + int32_t x, y, z; + + if(self->pos_Ptr != NULL) + { + if(self->pos_Ptr->getWindowSize(&x, &y, &z)) + return Py_BuildValue("iii", x, y, z); + } + + Py_RETURN_NONE; +} + +static PyGetSetDef DF_Position_getterSetters[] = +{ + {"view_coords", (getter)DF_Position_getViewCoords, (setter)DF_Position_setViewCoords, "view_coords", NULL}, + {"cursor_coords", (getter)DF_Position_getCursorCoords, (setter)DF_Position_setCursorCoords, "cursor_coords", NULL}, + {"window_size", (getter)DF_Position_getWindowSize, NULL, "window_size", NULL}, + {NULL} // Sentinel +}; + +static PyTypeObject DF_Position_type = +{ + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "pydfhack.Position", /*tp_name*/ + sizeof(DF_Position), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)DF_Position_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "pydfhack Position objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + 0, /* tp_methods */ + 0, /* tp_members */ + DF_Position_getterSetters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)DF_Position_init, /* tp_init */ + 0, /* tp_alloc */ + DF_Position_new, /* tp_new */ +}; + +#endif \ No newline at end of file From 282421fe5a1efe2faed247c448021226ea744ba9 Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 5 Apr 2010 16:47:38 -0500 Subject: [PATCH 13/37] fixed include path --- dfhack/python/Position.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/dfhack/python/Position.cpp b/dfhack/python/Position.cpp index debfa400a..cc251b185 100644 --- a/dfhack/python/Position.cpp +++ b/dfhack/python/Position.cpp @@ -26,7 +26,7 @@ distribution. #define __DFPOSITION__ #include "Python.h" -#include "Position.h" +#include "modules/Position.h" using namespace DFHack; @@ -55,7 +55,7 @@ static int DF_Position_init(DF_Position* self, PyObject* args, PyObject* kwds) return 0; } -static void DF_Position_dealloc(DF_API* self) +static void DF_Position_dealloc(DF_Position* self) { if(self != NULL) { @@ -78,7 +78,7 @@ static PyObject* DF_Position_getViewCoords(DF_Position* self, void* closure) if(self->pos_Ptr != NULL) { - if(self->pos_Ptr->getViewCoords(&x, &y, &z)) + if(self->pos_Ptr->getViewCoords(x, y, z)) return Py_BuildValue("iii", x, y, z); } @@ -109,7 +109,7 @@ static PyObject* DF_Position_getCursorCoords(DF_Position* self, void* closure) if(self->pos_Ptr != NULL) { - if(self->pos_Ptr->getCursorCoords(&x, &y, &z)) + if(self->pos_Ptr->getCursorCoords(x, y, z)) return Py_BuildValue("iii", x, y, z); } @@ -136,12 +136,12 @@ static PyObject* DF_Position_setCursorCoords(DF_Position* self, PyObject* args) static PyObject* DF_Position_getWindowSize(DF_Position* self, void* closure) { - int32_t x, y, z; + int32_t x, y; if(self->pos_Ptr != NULL) { - if(self->pos_Ptr->getWindowSize(&x, &y, &z)) - return Py_BuildValue("iii", x, y, z); + if(self->pos_Ptr->getWindowSize(x, y)) + return Py_BuildValue("ii", x, y); } Py_RETURN_NONE; From 7b0510f5e129ae4a6eaaff0ac61cc56aef0ff958 Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 5 Apr 2010 16:52:12 -0500 Subject: [PATCH 14/37] added a getter for Position --- dfhack/python/DF_API.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/dfhack/python/DF_API.cpp b/dfhack/python/DF_API.cpp index f252af5dd..d8b59f607 100644 --- a/dfhack/python/DF_API.cpp +++ b/dfhack/python/DF_API.cpp @@ -30,6 +30,7 @@ distribution. #include "DFTypes.h" #include "DFHackAPI.h" #include "MemInfo.cpp" +#include "Position.cpp" using namespace std; using namespace DFHack; @@ -38,6 +39,7 @@ struct DF_API { PyObject_HEAD PyObject* mem_info; + PyObject* position; DFHack::API* api_Ptr; }; @@ -62,6 +64,7 @@ static int DF_API_init(DF_API* self, PyObject* args, PyObject* kwds) if(self->api_Ptr == NULL) { self->mem_info = NULL; + self->position = NULL; if(!PyArg_ParseTuple(args, "s", &memFileString)) return -1; @@ -87,6 +90,7 @@ static void DF_API_dealloc(DF_API* self) } Py_XDECREF(self->mem_info); + Py_XDECREF(self->position); self->ob_type->tp_free((PyObject*)self); } @@ -157,11 +161,41 @@ static PyObject* DF_API_getMemoryInfo(DF_API* self, void* closure) Py_RETURN_NONE; } +static PyObject* DF_API_getPosition(DF_API* self, void* closure) +{ + if(self->position != NULL) + return self->position; + + try + { + if(self->api_Ptr != NULL) + { + DFHack::Position* pos; + + pos = self->api_Ptr->getPosition(); + + self->position = _PyObject_New(&DF_Position_type); + delete ((DF_Position*)(self->position))->pos_Ptr; + ((DF_Position*)(self->position))->pos_Ptr = pos; + + return self->position; + } + } + catch(...) + { + PyErr_SetString(PyExc_ValueError, "Error trying to read position"); + return NULL; + } + + Py_RETURN_NONE; +} + static PyGetSetDef DF_API_getterSetters[] = { {"is_attached", (getter)DF_API_getIsAttached, NULL, "is_attached", NULL}, {"is_suspended", (getter)DF_API_getIsSuspended, NULL, "is_suspended", NULL}, {"memory_info", (getter)DF_API_getMemoryInfo, NULL, "memory_info", NULL}, + {"position", (getter)DF_API_getPosition, NULL, "position", NULL}, {NULL} // Sentinel }; From 2d853daadff27e5b562a9e16e40d429b15e68061 Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 5 Apr 2010 16:52:32 -0500 Subject: [PATCH 15/37] Position type is exposed --- dfhack/python/pydfhack.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index f743da267..4dbfa514a 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -64,11 +64,16 @@ PyMODINIT_FUNC initpydfhack(void) if(PyType_Ready(&DF_MemInfo_type) < 0) return; + if(PyType_Ready(&DF_Position_type) < 0) + return; + module = Py_InitModule3("pydfhack", module_methods, "pydfhack extension module"); Py_INCREF(&DF_API_type); Py_INCREF(&DF_MemInfo_type); + Py_INCREF(&DF_Position_type); PyModule_AddObject(module, "API", (PyObject*)&DF_API_type); PyModule_AddObject(module, "MemInfo", (PyObject*)&DF_MemInfo_type); + PyModule_AddObject(module, "Position", (PyObject*)&DF_Position_type); } \ No newline at end of file From a3942478d9ef7e3ad48f5faeb4a20864544ae1ac Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 5 Apr 2010 16:52:43 -0500 Subject: [PATCH 16/37] setup script updated --- dfhack/python/setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dfhack/python/setup.py b/dfhack/python/setup.py index dda41f241..aeddb72e5 100644 --- a/dfhack/python/setup.py +++ b/dfhack/python/setup.py @@ -2,8 +2,8 @@ from distutils.core import setup, Extension e = Extension("pydfhack", - sources=["pydfhack.cpp", "DF_API.cpp"], - include_dirs=["..\\include"], + sources=["MemInfo.cpp", "DF_API.cpp", "pydfhack.cpp"], + include_dirs=["..\\", "..\\include", "..\\depends\\md5", "..\\depends\\tinyxml"], library_dirs=["..\\..\\output"], libraries=["libdfhack"], export_symbols=["initpydfhack", "ReadRaw", "WriteRaw"]) From dd14c9ec667105afc273a94f421de2b5998b3262 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 6 Apr 2010 10:10:47 -0500 Subject: [PATCH 17/37] updated for renamed sources --- dfhack/python/DF_API.cpp | 4 ++-- dfhack/python/{MemInfo.cpp => DF_MemInfo.cpp} | 0 dfhack/python/{Position.cpp => DF_Position.cpp} | 0 dfhack/python/pydfhack.cpp | 2 +- dfhack/python/setup.py | 2 +- 5 files changed, 4 insertions(+), 4 deletions(-) rename dfhack/python/{MemInfo.cpp => DF_MemInfo.cpp} (100%) rename dfhack/python/{Position.cpp => DF_Position.cpp} (100%) diff --git a/dfhack/python/DF_API.cpp b/dfhack/python/DF_API.cpp index d8b59f607..9e4c7be06 100644 --- a/dfhack/python/DF_API.cpp +++ b/dfhack/python/DF_API.cpp @@ -29,8 +29,8 @@ distribution. #include #include "DFTypes.h" #include "DFHackAPI.h" -#include "MemInfo.cpp" -#include "Position.cpp" +#include "DF_MemInfo.cpp" +#include "DF_Position.cpp" using namespace std; using namespace DFHack; diff --git a/dfhack/python/MemInfo.cpp b/dfhack/python/DF_MemInfo.cpp similarity index 100% rename from dfhack/python/MemInfo.cpp rename to dfhack/python/DF_MemInfo.cpp diff --git a/dfhack/python/Position.cpp b/dfhack/python/DF_Position.cpp similarity index 100% rename from dfhack/python/Position.cpp rename to dfhack/python/DF_Position.cpp diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index 4dbfa514a..7fef20d62 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -23,7 +23,7 @@ distribution. */ #include "Python.h" -#include "MemInfo.cpp" +#include "DF_MemInfo.cpp" #include "DF_API.cpp" #ifndef PyMODINIT_FUNC diff --git a/dfhack/python/setup.py b/dfhack/python/setup.py index aeddb72e5..f42072986 100644 --- a/dfhack/python/setup.py +++ b/dfhack/python/setup.py @@ -2,7 +2,7 @@ from distutils.core import setup, Extension e = Extension("pydfhack", - sources=["MemInfo.cpp", "DF_API.cpp", "pydfhack.cpp"], + sources=["DF_MemInfo.cpp", "DF_API.cpp", "pydfhack.cpp"], include_dirs=["..\\", "..\\include", "..\\depends\\md5", "..\\depends\\tinyxml"], library_dirs=["..\\..\\output"], libraries=["libdfhack"], From 61fb9fb2c650e2bb75429c48e2bf64ce6828d0fb Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 6 Apr 2010 10:39:24 -0500 Subject: [PATCH 18/37] first commit --- dfhack/python/DF_Material.cpp | 259 ++++++++++++++++++++++++++++++++++ dfhack/python/Process.cpp | 23 --- 2 files changed, 259 insertions(+), 23 deletions(-) create mode 100644 dfhack/python/DF_Material.cpp delete mode 100644 dfhack/python/Process.cpp diff --git a/dfhack/python/DF_Material.cpp b/dfhack/python/DF_Material.cpp new file mode 100644 index 000000000..5c655b4af --- /dev/null +++ b/dfhack/python/DF_Material.cpp @@ -0,0 +1,259 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DFMATERIAL__ +#define __DFMATERIAL__ + +#include "Python.h" +#include + +using namespace std; + +#include "modules/Materials.h" + +using namespace DFHack; + +struct DF_Material +{ + PyObject_HEAD + DFHack::Materials* mat_Ptr; +}; + +// Helpers + +static PyObject* BuildMatgloss(t_matgloss matgloss) +{ + PyObject* matDict; + + matDict = PyDict_New(); + + PyDict_SetItemString(matDict, "id", PyString_FromString(matgloss.id)); + PyDict_SetItemString(matDict, "fore", PyInt_FromLong(matgloss.fore)); + PyDict_SetItemString(matDict, "back", PyInt_FromLong(matgloss.back)); + PyDict_SetItemString(matDict, "bright", PyInt_FromLong(matgloss.bright)); + PyDict_SetItemString(matDict, "name", PyString_FromString(matgloss.name)); + + return matDict; +} + +static PyObject* BuildMatglossPlant(t_matglossPlant matgloss) +{ + PyObject* matDict; + + matDict = PyDict_New(); + + PyDict_SetItemString(matDict, "id", PyString_FromString(matgloss.id)); + PyDict_SetItemString(matDict, "fore", PyInt_FromLong(matgloss.fore)); + PyDict_SetItemString(matDict, "back", PyInt_FromLong(matgloss.back)); + PyDict_SetItemString(matDict, "bright", PyInt_FromLong(matgloss.bright)); + PyDict_SetItemString(matDict, "name", PyString_FromString(matgloss.name)); + PyDict_SetItemString(matDict, "drink_name", PyString_FromString(matgloss.drink_name)); + PyDict_SetItemString(matDict, "food_name", PyString_FromString(matgloss.food_name)); + PyDict_SetItemString(matDict, "extract_name", PyString_FromString(matgloss.extract_name)); + + return matDict; +} + +static PyObject* BuildMatglossList(std::vector & matVec) +{ + PyObject* matList; + std::vector::iterator matIter; + + matList = PyList_New(0); + + for(matIter = matVec.begin(); matIter != matVec.end(); matIter++) + { + PyObject* matgloss = BuildMatgloss(*matIter); + + PyList_Append(matList, matgloss); + } + + return matList; +} + +// API type Allocation, Deallocation, and Initialization + +static PyObject* DF_Material_new(PyTypeObject* type, PyObject* args, PyObject* kwds) +{ + DF_Material* self; + + self = (DF_Material*)type->tp_alloc(type, 0); + + if(self != NULL) + self->mat_Ptr = NULL; + + return (PyObject*)self; +} + +static int DF_Material_init(DF_Material* self, PyObject* args, PyObject* kwds) +{ + return 0; +} + +static void DF_Material_dealloc(DF_Material* self) +{ + if(self != NULL) + { + if(self->mat_Ptr != NULL) + { + delete self->mat_Ptr; + + self->mat_Ptr = NULL; + } + + self->ob_type->tp_free((PyObject*)self); + } +} + +// Type methods + +static PyObject* DF_Material_ReadInorganicMaterials(DF_Material* self, PyObject* args) +{ + if(self->mat_Ptr != NULL) + { + std::vector matVec; + + if(self->mat_Ptr->ReadInorganicMaterials(matVec)) + { + return BuildMatglossList(matVec); + } + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Material_ReadOrganicMaterials(DF_Material* self, PyObject* args) +{ + if(self->mat_Ptr != NULL) + { + std::vector matVec; + + if(self->mat_Ptr->ReadOrganicMaterials(matVec)) + { + return BuildMatglossList(matVec); + } + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Material_ReadWoodMaterials(DF_Material* self, PyObject* args) +{ + if(self->mat_Ptr != NULL) + { + std::vector matVec; + + if(self->mat_Ptr->ReadWoodMaterials(matVec)) + { + return BuildMatglossList(matVec); + } + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Material_ReadPlantMaterials(DF_Material* self, PyObject* args) +{ + if(self->mat_Ptr != NULL) + { + std::vector matVec; + + if(self->mat_Ptr->ReadPlantMaterials(matVec)) + { + return BuildMatglossList(matVec); + } + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Material_ReadCreatureTypes(DF_Material* self, PyObject* args) +{ + if(self->mat_Ptr != NULL) + { + std::vector matVec; + + if(self->mat_Ptr->ReadCreatureTypes(matVec)) + { + return BuildMatglossList(matVec); + } + } + + Py_RETURN_NONE; +} + +static PyMethodDef DF_Material_methods[] = +{ + {"Read_Inorganic_Materials", (PyCFunction)DF_Material_ReadInorganicMaterials, METH_NOARGS, ""}, + {"Read_Organic_Materials", (PyCFunction)DF_Material_ReadOrganicMaterials, METH_NOARGS, ""}, + {"Read_Wood_Materials", (PyCFunction)DF_Material_ReadWoodMaterials, METH_NOARGS, ""}, + {"Read_Plant_Materials", (PyCFunction)DF_Material_ReadPlantMaterials, METH_NOARGS, ""}, + {"Read_Creature_Types", (PyCFunction)DF_Material_ReadCreatureTypes, METH_NOARGS, ""}, + {NULL} //Sentinel +}; + +static PyTypeObject DF_Material_type = +{ + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "pydfhack.Material", /*tp_name*/ + sizeof(DF_Material), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)DF_Material_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "pydfhack Material objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DF_Material_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)DF_Material_init, /* tp_init */ + 0, /* tp_alloc */ + DF_Material_new, /* tp_new */ +}; + +#endif \ No newline at end of file diff --git a/dfhack/python/Process.cpp b/dfhack/python/Process.cpp deleted file mode 100644 index 6a4e3eb31..000000000 --- a/dfhack/python/Process.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any -damages arising from the use of this software. - -Permission is granted to anyone to use this software for any -purpose, including commercial applications, and to alter it and -redistribute it freely, subject to the following restrictions: - -1. The origin of this software must not be misrepresented; you must -not claim that you wrote the original software. If you use this -software in a product, an acknowledgment in the product documentation -would be appreciated but is not required. - -2. Altered source versions must be plainly marked as such, and -must not be misrepresented as being the original software. - -3. This notice may not be removed or altered from any source -distribution. -*/ \ No newline at end of file From 3d70dd5221ca951ceffbfb9de8a5f9e1d5714d36 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 6 Apr 2010 10:39:44 -0500 Subject: [PATCH 19/37] updated to include Material type --- dfhack/python/pydfhack.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index 7fef20d62..da5e9690e 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -24,6 +24,7 @@ distribution. #include "Python.h" #include "DF_MemInfo.cpp" +#include "DF_Material.cpp" #include "DF_API.cpp" #ifndef PyMODINIT_FUNC @@ -66,14 +67,19 @@ PyMODINIT_FUNC initpydfhack(void) if(PyType_Ready(&DF_Position_type) < 0) return; + + if(PyType_Ready(&DF_Material_type) < 0) + return; module = Py_InitModule3("pydfhack", module_methods, "pydfhack extension module"); Py_INCREF(&DF_API_type); Py_INCREF(&DF_MemInfo_type); Py_INCREF(&DF_Position_type); + Py_INCREF(&DF_Material_type); PyModule_AddObject(module, "API", (PyObject*)&DF_API_type); PyModule_AddObject(module, "MemInfo", (PyObject*)&DF_MemInfo_type); PyModule_AddObject(module, "Position", (PyObject*)&DF_Position_type); + PyModule_AddObject(module, "Material", (PyObject*)&DF_Material_type); } \ No newline at end of file From e56e7dcbeac4d90f9829f3ef26edf497434f953c Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 6 Apr 2010 10:43:41 -0500 Subject: [PATCH 20/37] added getter for materials --- dfhack/python/DF_API.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/dfhack/python/DF_API.cpp b/dfhack/python/DF_API.cpp index 9e4c7be06..1b43189aa 100644 --- a/dfhack/python/DF_API.cpp +++ b/dfhack/python/DF_API.cpp @@ -31,6 +31,7 @@ distribution. #include "DFHackAPI.h" #include "DF_MemInfo.cpp" #include "DF_Position.cpp" +#include "DF_Material.cpp" using namespace std; using namespace DFHack; @@ -40,6 +41,7 @@ struct DF_API PyObject_HEAD PyObject* mem_info; PyObject* position; + PyObject* material; DFHack::API* api_Ptr; }; @@ -65,6 +67,7 @@ static int DF_API_init(DF_API* self, PyObject* args, PyObject* kwds) { self->mem_info = NULL; self->position = NULL; + self->material = NULL; if(!PyArg_ParseTuple(args, "s", &memFileString)) return -1; @@ -91,6 +94,7 @@ static void DF_API_dealloc(DF_API* self) Py_XDECREF(self->mem_info); Py_XDECREF(self->position); + Py_XDECREF(self->material); self->ob_type->tp_free((PyObject*)self); } @@ -190,12 +194,42 @@ static PyObject* DF_API_getPosition(DF_API* self, void* closure) Py_RETURN_NONE; } +static PyObject* DF_API_getMaterial(DF_API* self, void* closure) +{ + if(self->material != NULL) + return self->material; + + try + { + if(self->api_Ptr != NULL) + { + DFHack::Materials* mat; + + mat = self->api_Ptr->getMaterials(); + + self->material = _PyObject_New(&DF_Material_type); + delete ((DF_Material*)(self->material))->mat_Ptr; + ((DF_Material*)(self->material))->mat_Ptr = mat; + + return self->material; + } + } + catch(...) + { + PyErr_SetString(PyExc_ValueError, "Error trying to read material"); + return NULL; + } + + Py_RETURN_NONE; +} + static PyGetSetDef DF_API_getterSetters[] = { {"is_attached", (getter)DF_API_getIsAttached, NULL, "is_attached", NULL}, {"is_suspended", (getter)DF_API_getIsSuspended, NULL, "is_suspended", NULL}, {"memory_info", (getter)DF_API_getMemoryInfo, NULL, "memory_info", NULL}, {"position", (getter)DF_API_getPosition, NULL, "position", NULL}, + {"material", (getter)DF_API_getMaterial, NULL, "material", NULL}, {NULL} // Sentinel }; From 6bec24d6c84a92718d6c055d810123d3b28f80e8 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 6 Apr 2010 13:46:24 -0500 Subject: [PATCH 21/37] getters for memory info, position, and materials should actually work now --- dfhack/python/DF_API.cpp | 44 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/dfhack/python/DF_API.cpp b/dfhack/python/DF_API.cpp index 1b43189aa..733b8322a 100644 --- a/dfhack/python/DF_API.cpp +++ b/dfhack/python/DF_API.cpp @@ -145,15 +145,15 @@ static PyObject* DF_API_getMemoryInfo(DF_API* self, void* closure) { if(self->api_Ptr != NULL) { - DFHack::memory_info* mem; - - mem = self->api_Ptr->getMemoryInfo(); - self->mem_info = _PyObject_New(&DF_MemInfo_type); - delete ((DF_MemInfo*)(self->mem_info))->mem_Ptr; - ((DF_MemInfo*)(self->mem_info))->mem_Ptr = mem; - return self->mem_info; + if(self->mem_info != NULL) + { + ((DF_MemInfo*)(self->mem_info))->mem_Ptr = self->api_Ptr->getMemoryInfo(); + + if(((DF_MemInfo*)(self->mem_info))->mem_Ptr != NULL) + return self->mem_info; + } } } catch(...) @@ -174,15 +174,15 @@ static PyObject* DF_API_getPosition(DF_API* self, void* closure) { if(self->api_Ptr != NULL) { - DFHack::Position* pos; - - pos = self->api_Ptr->getPosition(); - self->position = _PyObject_New(&DF_Position_type); - delete ((DF_Position*)(self->position))->pos_Ptr; - ((DF_Position*)(self->position))->pos_Ptr = pos; - return self->position; + if(self->position != NULL) + { + ((DF_Position*)(self->position))->pos_Ptr = self->api_Ptr->getPosition(); + + if(((DF_Position*)(self->position))->pos_Ptr != NULL) + return self->position; + } } } catch(...) @@ -203,15 +203,15 @@ static PyObject* DF_API_getMaterial(DF_API* self, void* closure) { if(self->api_Ptr != NULL) { - DFHack::Materials* mat; - - mat = self->api_Ptr->getMaterials(); - self->material = _PyObject_New(&DF_Material_type); - delete ((DF_Material*)(self->material))->mat_Ptr; - ((DF_Material*)(self->material))->mat_Ptr = mat; - return self->material; + if(self->material != NULL) + { + ((DF_Material*)(self->material))->mat_Ptr = self->api_Ptr->getMaterials(); + + if(((DF_Material*)(self->material))->mat_Ptr != NULL) + return self->material; + } } } catch(...) @@ -229,7 +229,7 @@ static PyGetSetDef DF_API_getterSetters[] = {"is_suspended", (getter)DF_API_getIsSuspended, NULL, "is_suspended", NULL}, {"memory_info", (getter)DF_API_getMemoryInfo, NULL, "memory_info", NULL}, {"position", (getter)DF_API_getPosition, NULL, "position", NULL}, - {"material", (getter)DF_API_getMaterial, NULL, "material", NULL}, + {"materials", (getter)DF_API_getMaterial, NULL, "material", NULL}, {NULL} // Sentinel }; From aaa6c711ad22804a1cccc056df333fcdd7ffbc5a Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 6 Apr 2010 16:06:19 -0500 Subject: [PATCH 22/37] first commit --- dfhack/python/DF_Helpers.cpp | 126 +++++++++++++++++++++++++++++++++++ dfhack/python/DF_Imports.cpp | 51 ++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 dfhack/python/DF_Helpers.cpp create mode 100644 dfhack/python/DF_Imports.cpp diff --git a/dfhack/python/DF_Helpers.cpp b/dfhack/python/DF_Helpers.cpp new file mode 100644 index 000000000..8b156e535 --- /dev/null +++ b/dfhack/python/DF_Helpers.cpp @@ -0,0 +1,126 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DFHELPERS__ +#define __DFHELPERS__ + +#include "Python.h" +#include "DF_Imports.cpp" +#include "DFTypes.h" + +using namespace DFHack; + +static PyObject* BuildMatglossPair(DFHack::t_matglossPair& matgloss) +{ + return Py_BuildValue("ii", matgloss.type, matgloss.index); +} + +static PyObject* BuildTreeDesc(DFHack::t_tree_desc& tree) +{ + Py_BuildValue("OO", BuildMatglossPair(tree.material), Py_BuildValue("III", tree.x, tree.y, tree.z)); +} + +static PyObject* BuildSkill(DFHack::t_skill& skill) +{ + return Py_BuildValue("III", skill.id, skill.experience, skill.rating); +} + +static PyObject* BuildJob(DFHack::t_job& job) +{ + return Py_BuildValue("Oi", PyBool_FromLong((int)job.active), job.jobId); +} + +static PyObject* BuildItemType(DFHack::t_itemType& item) +{ + return Py_BuildValue("ss", PyString_FromString(item.id), PyString_FromString(item.name)); +} + +static PyObject* BuildLike(DFHack::t_like& like) +{ + PyObject* item; + + item = Py_BuildValue("iii", like.type, like.itemClass, like.itemIndex); + + return Py_BuildValue("OOO", item, BuildMatglossPair(item.material), PyBool_FromLong((int)item.active)); +} + +static PyObject* BuildNote(DFHack::t_note& note) +{ + PyObject* noteDict = PyDict_New(); + + PyDict_SetItemString(noteDict, "symbol", PyString_FromFormat("%c", note.symbol)); + PyDict_SetItemString(noteDict, "fore_back", Py_BuildValue("II", note.foreground, note.background)); + PyDict_SetItemString(noteDict, "name", PyString_FromString(note.name)); + PyDict_SetItemString(noteDict, "position", Py_BuildValue("III", note.x, note.y, note.z)); + + return noteDict; +} + +static PyObject* BuildName(DFHack::t_name& name) +{ + PyObject* nameDict; + PyObject *wordList, *speechList; + + nameDict = PyDict_New(); + + PyDict_SetItemString(nameDict, "first_name", PyString_FromString(name.first_name)); + PyDict_SetItemString(nameDict, "nickname", PyString_FromString(name.nickname)); + PyDict_SetItemString(nameDict, "language", PyInt_FromLong(name.language)); + PyDict_SetItemString(nameDict, "has_name", PyBool_FromLong((int)name.has_name)); + + wordList = PyList_New(7); + speechList = PyList_New(7); + + for(int i = 0; i < 7; i++) + { + PyList_SetItem(wordList, i, PyInt_FromLong(name.words[i])); + PyList_SetItem(wordList, i, PyInt_FromLong(name.parts_of_speech[i])); + } + + PyDict_SetItemString(nameDict, "words", wordList); + PyDict_SetItemString(nameDict, "parts_of_speech", speechList); + + return nameDict; +} + +static PyObject* BuildSettlement(DFHack::t_settlement& settlement) +{ + PyObject* setDict; + PyObject *local_pos1, *local_pos2; + + setDict = PyDict_New(); + + PyDict_SetItemString(setDict, "origin", PyInt_FromLong(settlement.origin)); + PyDict_SetItemString(setDict, "name", BuildName(settlement.name)); + PyDict_SetItemString(setDict, "world_pos", Py_BuildValue("ii", settlement.world_x, settlement.world_y)); + + local_pos1 = Py_BuildValue("ii", settlement.local_x1, settlement.local_y1); + local_pos2 = Py_BuildValue("ii", settlement.local_x2, settlement.local_y2); + + PyDict_SetItemString(setDict, "local_pos", Py_BuildValue("OO", local_pos1, local_pos2)); + + return setDict; +} + +#endif \ No newline at end of file diff --git a/dfhack/python/DF_Imports.cpp b/dfhack/python/DF_Imports.cpp new file mode 100644 index 000000000..300731e4f --- /dev/null +++ b/dfhack/python/DF_Imports.cpp @@ -0,0 +1,51 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DFIMPORTS__ +#define __DFIMPORTS__ + +#include "Python.h" + +static PyObject* TypesModule = NULL; +static PyObject* CreatureFlags1_type = NULL; +static PyObject* CreatureFlags2_type = NULL; +static PyObject* DesignationFlags_type = NULL; +static PyObject* OccupancyFlags_type = NULL; +static PyObject* ItemFlags_type = NULL; + +static void DoImports() +{ + if(TypesModule == NULL) + { + TypesModule = PyImport_ImportModule("pydftypes"); + + CreatureFlags1_type = PyObject_GetAttrString(TypesModule, "CreatureFlags1"); + CreatureFlags2_type = PyObject_GetAttrString(TypesModule, "CreatureFlags2"); + DesignationFlags_type = PyObject_GetAttrString(TypesModule, "DesignationFlags"); + OccupancyFlags_type = PyObject_GetAttrString(TypesModule, "OccupancyFlags"); + ItemFlags_type = PyObject_GetAttrString(TypesModule, "ItemFlags"); + } +} + +#endif \ No newline at end of file From a48fd263273c70351af38f5febbdbd4de156b0e6 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 6 Apr 2010 16:23:49 -0500 Subject: [PATCH 23/37] changed from using Py_XDECREF to Py_CLEAR --- dfhack/python/DF_API.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/dfhack/python/DF_API.cpp b/dfhack/python/DF_API.cpp index 733b8322a..a3592ba89 100644 --- a/dfhack/python/DF_API.cpp +++ b/dfhack/python/DF_API.cpp @@ -85,6 +85,10 @@ static void DF_API_dealloc(DF_API* self) { if(self != NULL) { + Py_CLEAR(self->mem_info); + Py_CLEAR(self->position); + Py_CLEAR(self->material); + if(self->api_Ptr != NULL) { delete self->api_Ptr; @@ -92,10 +96,6 @@ static void DF_API_dealloc(DF_API* self) self->api_Ptr = NULL; } - Py_XDECREF(self->mem_info); - Py_XDECREF(self->position); - Py_XDECREF(self->material); - self->ob_type->tp_free((PyObject*)self); } } @@ -145,7 +145,7 @@ static PyObject* DF_API_getMemoryInfo(DF_API* self, void* closure) { if(self->api_Ptr != NULL) { - self->mem_info = _PyObject_New(&DF_MemInfo_type); + self->mem_info = PyObject_Call((PyObject*)&DF_MemInfo_type, NULL, NULL); if(self->mem_info != NULL) { @@ -174,7 +174,7 @@ static PyObject* DF_API_getPosition(DF_API* self, void* closure) { if(self->api_Ptr != NULL) { - self->position = _PyObject_New(&DF_Position_type); + self->position = PyObject_Call((PyObject*)&DF_Position_type, NULL, NULL); if(self->position != NULL) { @@ -203,7 +203,7 @@ static PyObject* DF_API_getMaterial(DF_API* self, void* closure) { if(self->api_Ptr != NULL) { - self->material = _PyObject_New(&DF_Material_type); + self->material = PyObject_Call((PyObject*)&DF_Material_type, NULL, NULL); if(self->material != NULL) { From 7759ed7b1a3c73e96f9813cc5de804425f5b1b95 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 6 Apr 2010 17:04:43 -0500 Subject: [PATCH 24/37] imports loaded at module startup --- dfhack/python/DF_CreatureType.cpp | 218 ++++++++++++++++++++++++++++++ dfhack/python/pydfhack.cpp | 5 +- 2 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 dfhack/python/DF_CreatureType.cpp diff --git a/dfhack/python/DF_CreatureType.cpp b/dfhack/python/DF_CreatureType.cpp new file mode 100644 index 000000000..663f68d52 --- /dev/null +++ b/dfhack/python/DF_CreatureType.cpp @@ -0,0 +1,218 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DFCREATURETYPE__ +#define __DFCREATURETYPE__ + +#include "Python.h" +#include "structmember.h" +#include "DF_Imports.cpp" +#include "DF_Helpers.cpp" +#include "DFTypes.h" + +using namespace DFHack; + +struct DF_Creature_Base +{ + PyObject_HEAD + + // simple type stuff + uint32_t position; + PyObject* origin; + uint32_t c_type; + uint8_t profession; + uint16_t mood; + uint32_t happiness; + uint32_t c_id; + uint32_t agility; + uint32_t strength; + uint32_t toughness; + uint32_t money; + int32_t squad_leader_id; + uint8_t sex; + uint32_t pregnancy_timer; + int32_t blood_max, blood_current; + uint32_t bleed_rate; + + PyObject* custom_profession; + + // composites + PyObject *name, *squad_name, *artifact_name; + PyObject* current_job; + + // customs + PyObject *flags1, *flags2; + + // lists + PyObject* skill_list; + PyObject* like_list; + PyObject* trait_list; + PyObject* labor_list; +}; + +static PyObject* BuildCreature(DFHack::t_creature& creature) +{ + DF_Creature_Base* obj; + + obj = (DF_Creature_Base*)PyObject_Call((PyObject*)&DF_Creature_Base_type); + + obj->position = Py_BuildValue("III", creature.x, creature.y, creature.z); + obj->profession = creature.profession; + obj->c_type = creature.type; + obj->mood = creature.mood; + obj->happiness = creature.happiness; + obj->c_id = creature.id; + obj->agility = creature.agility; + obj->strength = creature.strength; + obj->toughness = creature.toughness; + obj->money = creature.money; + obj->squad_leader_id = creature.squad_leader_id; + obj->sex = creature.sex; + obj->pregnancy_timer = creature.pregnancy_timer; + obj->blood_max = creature.blood_max; + obj->blood_current = creature.blood_current; + obj->bleed_rate = creature.bleed_rate) + obj->custom_profession = PyString_FromString(creature.custom_profession); + + obj->flags1 = PyObject_Call(CreatureFlags1_type, PyInt_FromLong(creature.flags1.whole)); + obj->flags2 = PyObject_Call(CreatureFlags2_type, PyInt_FromLong(creature.flags2.whole)); + + obj->current_job = BuildJob(creature.current_job); + obj->name = BuildName(creature.name); + obj->squad_name = BuildName(creature.squad_name); + obj->artifact_name = BuildName(creature.artifact_name); + + obj->skill_list = PyList_New(creature.numSkills); + + for(int i = 0; i < creature.numSkills; i++) + PyList_SetItem(obj->skill_list, i, BuildSkill(creature.skills[i])); + + obj->like_list = PyList_New(creature.numLikes); + + for(int i = 0; i < creature.numLikes; i++) + PyList_SetItem(obj->like_list, i, BuildLike(creature.likes[i])); + + obj->labor_list = PyList_New(NUM_CREATURE_LABORS); + + for(int i = 0; i < NUM_CREATURE_LABORS; i++) + PyList_SetItem(obj->labor_list, i, PyInt_FromLong(creature.labors[i])); + + obj->trait_list = PyList_New(NUM_CREATURE_TRAITS); + + for(int i = 0; i < NUM_CREATURE_TRAITS; i++) + PyList_SetItem(obj->trait_list, i, PyInt_FromLong(creature.traits[i])); + + return obj; +} + +// API type Allocation, Deallocation, and Initialization + +static void DF_Creature_Base_dealloc(DF_Creature_Base* self) +{ + if(self != NULL) + { + Py_CLEAR(self->position); + Py_CLEAR(self->c_type); + Py_CLEAR(self->flags1); + Py_CLEAR(self->flags2); + + Py_CLEAR(self->name); + Py_CLEAR(self->squad_name); + Py_CLEAR(self->artifact_name); + + Py_CLEAR(self->flags1); + Py_CLEAR(self->flags2); + + if(self->labor_list != NULL) + PyList_Clear(self->labor_list); + if(self->trait_list != NULL) + PyList_Clear(self->trait_list); + if(self->skill_list != NULL) + PyList_Clear(self->skill_list); + if(self->like_list != NULL) + PyList_Clear(self->like_list); + + self->ob_type->tp_free((PyObject*)self); + } +} + +static PyMemberDef DF_Creature_Base_members[] = +{ + {"origin", T_UINT, offsetof(DF_Creature_Base, origin), 0, ""}, + {"type", T_UINT, offsetof(DF_Creature_Base, c_type), 0, ""}, + {"flags1", T_OBJECT_EX, offsetof(DF_Creature_Base, flags1), 0, ""}, + {"flags2", T_OBJECT_EX, offsetof(DF_Creature_Base, flags2), 0, ""}, + {"name", T_OBJECT_EX, offsetof(DF_Creature_base, name), 0, ""}, + {"squad_name", T_OBJECT_EX, offsetof(DF_Creature_Base, squad_name), 0, ""}, + {"artifact_name", T_OBJECT_EX, offsetof(DF_Creature_Base, artifact_name), 0, ""}, + {"profession", T_INT, offsetof(DF_Creature_base, profession), 0, ""}, + {"custom_profession", T_OBJECT_EX, offsetof(DF_Creature_Base, custom_profession), 0, ""}, + {"happiness", T_SHORT, offsetof(DF_Creature_Base, happiness), 0, ""}, + {NULL} +}; + +static PyTypeObject DF_Creature_Base_type = +{ + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "pydfhack.Creature_Base", /*tp_name*/ + sizeof(DF_Creature_Base), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)DF_Creature_Base_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "pydfhack CreatureBase objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DF_Creature_Base_methods, /* tp_methods */ + 0, /* tp_members */ + DF_Creature_Base_getterSetters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + 0, /* tp_new */ +}; + +#endif \ No newline at end of file diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index da5e9690e..a1ee27387 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -23,6 +23,7 @@ distribution. */ #include "Python.h" +#include "DF_Imports.cpp" #include "DF_MemInfo.cpp" #include "DF_Material.cpp" #include "DF_API.cpp" @@ -81,5 +82,7 @@ PyMODINIT_FUNC initpydfhack(void) PyModule_AddObject(module, "API", (PyObject*)&DF_API_type); PyModule_AddObject(module, "MemInfo", (PyObject*)&DF_MemInfo_type); PyModule_AddObject(module, "Position", (PyObject*)&DF_Position_type); - PyModule_AddObject(module, "Material", (PyObject*)&DF_Material_type); + PyModule_AddObject(module, "Materials", (PyObject*)&DF_Material_type); + + DoImports(); } \ No newline at end of file From 0ced8164aaaf2f1d7c65da295d20dd0529d40b39 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 6 Apr 2010 18:12:21 -0500 Subject: [PATCH 25/37] added --- dfhack/python/DF_Creatures.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 dfhack/python/DF_Creatures.cpp diff --git a/dfhack/python/DF_Creatures.cpp b/dfhack/python/DF_Creatures.cpp new file mode 100644 index 000000000..0960b1722 --- /dev/null +++ b/dfhack/python/DF_Creatures.cpp @@ -0,0 +1,32 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DFCREATURES__ +#define __DFCREATURES__ + +#include "Python.h" +#include "DFTypes.h" +#include "DF_CreatureType.cpp" + +#endif \ No newline at end of file From ea8290fa6d0d729d68f8edddd42e0574fea8e9c2 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 6 Apr 2010 18:13:42 -0500 Subject: [PATCH 26/37] creature base type defined --- dfhack/python/DF_CreatureType.cpp | 97 +++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 5 deletions(-) diff --git a/dfhack/python/DF_CreatureType.cpp b/dfhack/python/DF_CreatureType.cpp index 663f68d52..0dd74c09b 100644 --- a/dfhack/python/DF_CreatureType.cpp +++ b/dfhack/python/DF_CreatureType.cpp @@ -69,6 +69,8 @@ struct DF_Creature_Base PyObject* like_list; PyObject* trait_list; PyObject* labor_list; + + bool inited; }; static PyObject* BuildCreature(DFHack::t_creature& creature) @@ -128,12 +130,45 @@ static PyObject* BuildCreature(DFHack::t_creature& creature) // API type Allocation, Deallocation, and Initialization +static PyObject* DF_Creature_Base_new(PyTypeObject* type, PyObject* args, PyObject* kwds) +{ + DF_Creature_Base* self; + + self = (DF_Creature_Base*)type->tp_alloc(type, 0); + + if(self != NULL) + self->inited = false; + + return (PyObject*)self; +} + +static int DF_Creature_Base_init(DF_Creature_Base* self, PyObject* args, PyObject* kwds) +{ + if(!inited) + { + self->origin = NULL; + self->flags1 = NULL; + self->flags2 = NULL; + self->name = NULL; + self->squad_name = NULL; + self->artifact_name = NULL; + + self->custom_profession = PyString_FromString(""); + + self->labor_list = NULL; + self->trait_list = NULL; + self->skill_list = NULL; + self->like_list = NULL; + } + + return 0; +} + static void DF_Creature_Base_dealloc(DF_Creature_Base* self) { if(self != NULL) { Py_CLEAR(self->position); - Py_CLEAR(self->c_type); Py_CLEAR(self->flags1); Py_CLEAR(self->flags2); @@ -169,9 +204,61 @@ static PyMemberDef DF_Creature_Base_members[] = {"profession", T_INT, offsetof(DF_Creature_base, profession), 0, ""}, {"custom_profession", T_OBJECT_EX, offsetof(DF_Creature_Base, custom_profession), 0, ""}, {"happiness", T_SHORT, offsetof(DF_Creature_Base, happiness), 0, ""}, + {"id", T_UINT, offsetof(DF_Creature_Base, c_id), 0, ""}, + {"agility", T_UINT, offsetof(DF_Creature_Base, agility), 0, ""}, + {"strength", T_UINT, offsetof(DF_Creature_Base, strength), 0, ""}, + {"toughness", T_UINT, offsetof(DF_Creature_Base, toughness), 0, ""}, + {"money", T_UINT, offsetof(DF_Creature_Base, money), 0, ""}, + {"squad_leader_id", T_INT, offsetof(DF_Creature_Base, squad_leader_id), 0, ""}, + {"sex", T_BYTE, offsetof(DF_Creature_Base, sex), 0, ""}, + {"pregnancy_timer", T_UINT, offsetof(DF_Creature_Base, pregnancy_timer), 0, ""}, + {"blood_max", T_INT, offsetof(DF_Creature_Base, blood_max), 0, ""}, + {"blood_current", T_INT, offsetof(DF_Creature_Base, blood_current), 0, ""}, + {"bleed_rate", T_UINT, offsetof(DF_Creature_Base, bleed_rate), 0, ""}, {NULL} }; +static PyObject* DF_Creature_Base_getLabors(DF_Creature_Base* self, void* closure) +{ + if(self->labor_list != NULL) + return self->labor_list; + + Py_RETURN_NONE; +} + +static PyObject* DF_Creature_Base_getTraits(DF_Creature_Base* self, void* closure) +{ + if(self->trait_list != NULL) + return self->trait_list; + + Py_RETURN_NONE; +} + +static PyObject* DF_Creature_Base_getSkills(DF_Creature_Base* self, void* closure) +{ + if(self->skill_list != NULL) + return self->skill_list; + + Py_RETURN_NONE; +} + +static PyObject* DF_Creature_Base_getLikes(DF_Creature_Base* self, void* closure) +{ + if(self->like_list != NULL) + return self->like_list; + + Py_RETURN_NONE; +} + +static PyGetSetDef DF_Creature_Base_getterSetters[] = +{ + {"labors", (getter)DF_Creature_Base_getLabors, NULL, "labors", NULL}, + {"traits", (getter)DF_Creature_Base_getTraits, NULL, "traits", NULL}, + {"skills", (getter)DF_Creature_Base_getSkills, NULL, "skills", NULL}, + {"likes", (getter)DF_Creature_Base_getLikes, NULL, "likes", NULL}, + {NULL} // Sentinel +}; + static PyTypeObject DF_Creature_Base_type = { PyObject_HEAD_INIT(NULL) @@ -202,17 +289,17 @@ static PyTypeObject DF_Creature_Base_type = 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - DF_Creature_Base_methods, /* tp_methods */ - 0, /* tp_members */ + 0, /* tp_methods */ + DF_Creature_Base_members, /* tp_members */ DF_Creature_Base_getterSetters, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - 0, /* tp_init */ + (initproc)DF_Creature_Base_init, /* tp_init */ 0, /* tp_alloc */ - 0, /* tp_new */ + DF_Creature_base_new, /* tp_new */ }; #endif \ No newline at end of file From cf6052e32dac326e84d8c554bfc7299a029de037 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 7 Apr 2010 13:45:01 -0500 Subject: [PATCH 27/37] fixed init --- dfhack/python/DF_MemInfo.cpp | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/dfhack/python/DF_MemInfo.cpp b/dfhack/python/DF_MemInfo.cpp index fab813db1..86efd35ff 100644 --- a/dfhack/python/DF_MemInfo.cpp +++ b/dfhack/python/DF_MemInfo.cpp @@ -51,27 +51,13 @@ static PyObject* DF_MemInfo_new(PyTypeObject* type, PyObject* args, PyObject* kw self = (DF_MemInfo*)type->tp_alloc(type, 0); if(self != NULL) - { self->mem_Ptr = NULL; - } return (PyObject*)self; } static int DF_MemInfo_init(DF_MemInfo* self, PyObject* args, PyObject* kwds) { - DF_MemInfo* copy = NULL; - - if(self->mem_Ptr == NULL) - { - PyArg_ParseTuple(args, "|O", ©); - - if(copy != NULL) - self->mem_Ptr = new DFHack::memory_info(*(copy->mem_Ptr)); - else - self->mem_Ptr = new DFHack::memory_info(); - } - return 0; } From d15858dd41986d8dbe2ad8eac49ae8ba5fbeead4 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 7 Apr 2010 14:32:50 -0500 Subject: [PATCH 28/37] first commit --- dfhack/python/DF_CreatureManager.cpp | 200 +++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 dfhack/python/DF_CreatureManager.cpp diff --git a/dfhack/python/DF_CreatureManager.cpp b/dfhack/python/DF_CreatureManager.cpp new file mode 100644 index 000000000..47800b7f2 --- /dev/null +++ b/dfhack/python/DF_CreatureManager.cpp @@ -0,0 +1,200 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DFCREATURES__ +#define __DFCREATURES__ + +#include "Python.h" +#include "DFTypes.h" +#include "modules/Creatures.h" +#include "DF_CreatureType.cpp" + +using namespace DFHack; + +struct DF_CreatureManager +{ + PyObject_HEAD + DFHack::Creatures* creature_Ptr; +}; + +// API type Allocation, Deallocation, and Initialization + +static PyObject* DF_CreatureManager_new(PyTypeObject* type, PyObject* args, PyObject* kwds) +{ + DF_CreatureManager* self; + + self = (DF_CreatureManager*)type->tp_alloc(type, 0); + + if(self != NULL) + self->creature_Ptr = NULL; + + return (PyObject*)self; +} + +static int DF_CreatureManager_init(DF_CreatureManager* self, PyObject* args, PyObject* kwds) +{ + return 0; +} + +static void DF_CreatureManager_dealloc(DF_CreatureManager* self) +{ + if(self != NULL) + { + if(self->creature_Ptr != NULL) + { + delete self->creature_Ptr; + + self->creature_Ptr = NULL; + } + + self->ob_type->tp_free((PyObject*)self); + } +} + +// Type methods + +static PyObject* DF_CreatureManager_Start(DF_CreatureManager* self, PyObject* args) +{ + uint32_t numCreatures = 0; + + if(self->creature_Ptr != NULL) + { + if(self->creature_Ptr->Start(numCreatures)) + return PyInt_FromLong(numCreatures); + else + return PyInt_FromLong(-1); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_CreatureManager_Finish(DF_CreatureManager* self, PyObject* args) +{ + if(self->creature_Ptr != NULL) + { + if(self->creature_Ptr->Finish()) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_CreatureManager_ReadCreature(DF_CreatureManager* self, PyObject* args) +{ + uint32_t index; + t_creature furball; + + if(self->creature_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "I", &index)) + return NULL; + + if(self->creature_Ptr->ReadCreature(index, furball)) + { + return BuildCreature(furball); + } + else + { + Py_RETURN_FALSE; + } + } + + Py_RETURN_NONE; +} + +static PyObject* DF_CreatureManager_ReadCreatureInBox(DF_CreatureManager* self, PyObject* args) +{ + int32_t index; + uint32_t x1, y1, z1, x2, y2, z2; + DFHack::t_creature furball; + + if(self->creature_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "iIIIIII", &index, &x1, &y1, &z1, &x2, &y2, &z2)) + return NULL; + + if(self->creature_Ptr->ReadCreatureInBox(index, furball, x1, y1, z1, x2, y2, z2) >= 0) + return BuildCreature(furball); + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyMethodDef DF_CreatureManager_methods[] = +{ + {"Start", (PyCFunction)DF_CreatureManager_Start, METH_NOARGS, ""}, + {"Finish", (PyCFunction)DF_CreatureManager_Finish, METH_NOARGS, ""}, + {"Read_Creature", (PyCFunction)DF_CreatureManager_ReadCreature, METH_VARARGS, ""}, + {"Read_Creature_In_Box", (PyCFunction)DF_CreatureManager_ReadCreatureInBox, METH_VARARGS, ""}, + {NULL} // Sentinel +}; + +static PyTypeObject DF_CreatureManager_type = +{ + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "pydfhack.CreatureManager", /*tp_name*/ + sizeof(DF_CreatureManager), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)DF_CreatureManager_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "pydfhack CreatureManager objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DF_CreatureManager_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)DF_CreatureManager_init, /* tp_init */ + 0, /* tp_alloc */ + DF_CreatureManager_new, /* tp_new */ +}; + +#endif \ No newline at end of file From 07f81674ad9dc3288eec7f6aaa348bc7602e94a0 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 7 Apr 2010 14:33:06 -0500 Subject: [PATCH 29/37] added creature getter --- dfhack/python/DF_API.cpp | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/dfhack/python/DF_API.cpp b/dfhack/python/DF_API.cpp index a3592ba89..2d864d04d 100644 --- a/dfhack/python/DF_API.cpp +++ b/dfhack/python/DF_API.cpp @@ -32,6 +32,7 @@ distribution. #include "DF_MemInfo.cpp" #include "DF_Position.cpp" #include "DF_Material.cpp" +#include "DF_CreatureManager.cpp" using namespace std; using namespace DFHack; @@ -42,6 +43,7 @@ struct DF_API PyObject* mem_info; PyObject* position; PyObject* material; + PyObject* creature; DFHack::API* api_Ptr; }; @@ -68,6 +70,7 @@ static int DF_API_init(DF_API* self, PyObject* args, PyObject* kwds) self->mem_info = NULL; self->position = NULL; self->material = NULL; + self->creature = NULL; if(!PyArg_ParseTuple(args, "s", &memFileString)) return -1; @@ -88,6 +91,7 @@ static void DF_API_dealloc(DF_API* self) Py_CLEAR(self->mem_info); Py_CLEAR(self->position); Py_CLEAR(self->material); + Py_CLEAR(self->creature); if(self->api_Ptr != NULL) { @@ -223,13 +227,43 @@ static PyObject* DF_API_getMaterial(DF_API* self, void* closure) Py_RETURN_NONE; } +static PyObject* DF_API_getCreature(DF_API* self, void* closure) +{ + if(self->creature != NULL) + return self->creature; + + try + { + if(self->api_Ptr != NULL) + { + self->creature = PyObject_Call((PyObject*)&DF_CreatureManager_type, NULL, NULL); + + if(self->creature != NULL) + { + ((DF_CreatureManager*)(self->creature))->creature_Ptr = self->api_Ptr->getCreatures(); + + if(((DF_CreatureManager*)(self->creature))->creature_Ptr != NULL) + return self->creature; + } + } + } + catch(...) + { + PyErr_SetString(PyExc_ValueError, "Error trying to read creature"); + return NULL; + } + + Py_RETURN_NONE; +} + static PyGetSetDef DF_API_getterSetters[] = { {"is_attached", (getter)DF_API_getIsAttached, NULL, "is_attached", NULL}, {"is_suspended", (getter)DF_API_getIsSuspended, NULL, "is_suspended", NULL}, {"memory_info", (getter)DF_API_getMemoryInfo, NULL, "memory_info", NULL}, {"position", (getter)DF_API_getPosition, NULL, "position", NULL}, - {"materials", (getter)DF_API_getMaterial, NULL, "material", NULL}, + {"materials", (getter)DF_API_getMaterial, NULL, "materials", NULL}, + {"creatures", (getter)DF_API_getCreature, NULL, "creatures", NULL}, {NULL} // Sentinel }; From 73d0a103fdd21782b2de4d1fa21563ee7b2ea175 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 7 Apr 2010 14:33:24 -0500 Subject: [PATCH 30/37] fixed initializer and deallocator --- dfhack/python/DF_CreatureType.cpp | 193 +++++++++++++++++++----------- 1 file changed, 125 insertions(+), 68 deletions(-) diff --git a/dfhack/python/DF_CreatureType.cpp b/dfhack/python/DF_CreatureType.cpp index 663f68d52..503a6db56 100644 --- a/dfhack/python/DF_CreatureType.cpp +++ b/dfhack/python/DF_CreatureType.cpp @@ -38,8 +38,7 @@ struct DF_Creature_Base PyObject_HEAD // simple type stuff - uint32_t position; - PyObject* origin; + uint32_t origin; uint32_t c_type; uint8_t profession; uint16_t mood; @@ -58,6 +57,7 @@ struct DF_Creature_Base PyObject* custom_profession; // composites + PyObject* position; PyObject *name, *squad_name, *artifact_name; PyObject* current_job; @@ -71,87 +71,77 @@ struct DF_Creature_Base PyObject* labor_list; }; -static PyObject* BuildCreature(DFHack::t_creature& creature) +// API type Allocation, Deallocation, and Initialization + +static PyObject* DF_Creature_Base_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { - DF_Creature_Base* obj; - - obj = (DF_Creature_Base*)PyObject_Call((PyObject*)&DF_Creature_Base_type); - - obj->position = Py_BuildValue("III", creature.x, creature.y, creature.z); - obj->profession = creature.profession; - obj->c_type = creature.type; - obj->mood = creature.mood; - obj->happiness = creature.happiness; - obj->c_id = creature.id; - obj->agility = creature.agility; - obj->strength = creature.strength; - obj->toughness = creature.toughness; - obj->money = creature.money; - obj->squad_leader_id = creature.squad_leader_id; - obj->sex = creature.sex; - obj->pregnancy_timer = creature.pregnancy_timer; - obj->blood_max = creature.blood_max; - obj->blood_current = creature.blood_current; - obj->bleed_rate = creature.bleed_rate) - obj->custom_profession = PyString_FromString(creature.custom_profession); - - obj->flags1 = PyObject_Call(CreatureFlags1_type, PyInt_FromLong(creature.flags1.whole)); - obj->flags2 = PyObject_Call(CreatureFlags2_type, PyInt_FromLong(creature.flags2.whole)); - - obj->current_job = BuildJob(creature.current_job); - obj->name = BuildName(creature.name); - obj->squad_name = BuildName(creature.squad_name); - obj->artifact_name = BuildName(creature.artifact_name); - - obj->skill_list = PyList_New(creature.numSkills); - - for(int i = 0; i < creature.numSkills; i++) - PyList_SetItem(obj->skill_list, i, BuildSkill(creature.skills[i])); - - obj->like_list = PyList_New(creature.numLikes); - - for(int i = 0; i < creature.numLikes; i++) - PyList_SetItem(obj->like_list, i, BuildLike(creature.likes[i])); - - obj->labor_list = PyList_New(NUM_CREATURE_LABORS); - - for(int i = 0; i < NUM_CREATURE_LABORS; i++) - PyList_SetItem(obj->labor_list, i, PyInt_FromLong(creature.labors[i])); + DF_Creature_Base* self; - obj->trait_list = PyList_New(NUM_CREATURE_TRAITS); + self = (DF_Creature_Base*)type->tp_alloc(type, 0); - for(int i = 0; i < NUM_CREATURE_TRAITS; i++) - PyList_SetItem(obj->trait_list, i, PyInt_FromLong(creature.traits[i])); + if(self != NULL) + { + self->origin = 0; + self->c_type = 0; + self->profession = 0; + self->mood = 0; + self->happiness = 0; + self->c_id = 0; + self->agility = 0; + self->strength = 0; + self->toughness = 0; + self->money = 0; + self->squad_leader_id = 0; + self->sex = 0; + self->pregnancy_timer = 0; + self->blood_max = 0; + self->blood_current = 0; + self->bleed_rate = 0; + + self->custom_profession = PyString_FromString(""); + self->name = PyString_FromString(""); + self->squad_name = PyString_FromString(""); + self->artifact_name = PyString_FromString(""); + + self->skill_list = NULL; + self->like_list = NULL; + self->trait_list = NULL; + self->labor_list = NULL; + } - return obj; + return (PyObject*)self; } -// API type Allocation, Deallocation, and Initialization - static void DF_Creature_Base_dealloc(DF_Creature_Base* self) { if(self != NULL) { Py_CLEAR(self->position); - Py_CLEAR(self->c_type); Py_CLEAR(self->flags1); Py_CLEAR(self->flags2); - + + Py_CLEAR(self->custom_profession); Py_CLEAR(self->name); Py_CLEAR(self->squad_name); Py_CLEAR(self->artifact_name); + Py_CLEAR(self->current_job); Py_CLEAR(self->flags1); Py_CLEAR(self->flags2); - if(self->labor_list != NULL) - PyList_Clear(self->labor_list); - if(self->trait_list != NULL) - PyList_Clear(self->trait_list); - if(self->skill_list != NULL) - PyList_Clear(self->skill_list); - if(self->like_list != NULL) - PyList_Clear(self->like_list); + Py_CLEAR(self->labor_list); + Py_CLEAR(self->trait_list); + Py_CLEAR(self->skill_list); + Py_CLEAR(self->like_list); + + // if(self->labor_list != NULL) + // PyList_Clear(self->labor_list); + // if(self->trait_list != NULL) + // PyList_Clear(self->trait_list); + // if(self->skill_list != NULL) + // PyList_Clear(self->skill_list); + // if(self->like_list != NULL) + // PyList_Clear(self->like_list); self->ob_type->tp_free((PyObject*)self); } @@ -163,13 +153,23 @@ static PyMemberDef DF_Creature_Base_members[] = {"type", T_UINT, offsetof(DF_Creature_Base, c_type), 0, ""}, {"flags1", T_OBJECT_EX, offsetof(DF_Creature_Base, flags1), 0, ""}, {"flags2", T_OBJECT_EX, offsetof(DF_Creature_Base, flags2), 0, ""}, - {"name", T_OBJECT_EX, offsetof(DF_Creature_base, name), 0, ""}, + {"name", T_OBJECT_EX, offsetof(DF_Creature_Base, name), 0, ""}, {"squad_name", T_OBJECT_EX, offsetof(DF_Creature_Base, squad_name), 0, ""}, {"artifact_name", T_OBJECT_EX, offsetof(DF_Creature_Base, artifact_name), 0, ""}, - {"profession", T_INT, offsetof(DF_Creature_base, profession), 0, ""}, + {"profession", T_INT, offsetof(DF_Creature_Base, profession), 0, ""}, {"custom_profession", T_OBJECT_EX, offsetof(DF_Creature_Base, custom_profession), 0, ""}, {"happiness", T_SHORT, offsetof(DF_Creature_Base, happiness), 0, ""}, - {NULL} + {NULL} //Sentinel +}; + +static PyGetSetDef DF_Creature_Base_getterSetters[] = +{ + {NULL} //Sentinel +}; + +static PyMethodDef DF_Creature_Base_methods[] = +{ + {NULL} //Sentinel }; static PyTypeObject DF_Creature_Base_type = @@ -203,7 +203,7 @@ static PyTypeObject DF_Creature_Base_type = 0, /* tp_iter */ 0, /* tp_iternext */ DF_Creature_Base_methods, /* tp_methods */ - 0, /* tp_members */ + DF_Creature_Base_members, /* tp_members */ DF_Creature_Base_getterSetters, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ @@ -212,7 +212,64 @@ static PyTypeObject DF_Creature_Base_type = 0, /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ - 0, /* tp_new */ + DF_Creature_Base_new, /* tp_new */ }; +static PyObject* BuildCreature(DFHack::t_creature& creature) +{ + DF_Creature_Base* obj; + + obj = (DF_Creature_Base*)PyObject_Call((PyObject*)&DF_Creature_Base_type, NULL, NULL); + + obj->position = Py_BuildValue("III", creature.x, creature.y, creature.z); + obj->profession = creature.profession; + obj->c_type = creature.type; + obj->mood = creature.mood; + obj->happiness = creature.happiness; + obj->c_id = creature.id; + obj->agility = creature.agility; + obj->strength = creature.strength; + obj->toughness = creature.toughness; + obj->money = creature.money; + obj->squad_leader_id = creature.squad_leader_id; + obj->sex = creature.sex; + obj->pregnancy_timer = creature.pregnancy_timer; + obj->blood_max = creature.blood_max; + obj->blood_current = creature.blood_current; + obj->bleed_rate = creature.bleed_rate; + + if(creature.custom_profession[0]) + obj->custom_profession = PyString_FromString(creature.custom_profession); + + obj->flags1 = PyObject_Call(CreatureFlags1_type, PyInt_FromLong(creature.flags1.whole), NULL); + obj->flags2 = PyObject_Call(CreatureFlags2_type, PyInt_FromLong(creature.flags2.whole), NULL); + + obj->current_job = BuildJob(creature.current_job); + obj->name = BuildName(creature.name); + obj->squad_name = BuildName(creature.squad_name); + obj->artifact_name = BuildName(creature.artifact_name); + + obj->skill_list = PyList_New(creature.numSkills); + + for(int i = 0; i < creature.numSkills; i++) + PyList_SetItem(obj->skill_list, i, BuildSkill(creature.skills[i])); + + obj->like_list = PyList_New(creature.numLikes); + + for(int i = 0; i < creature.numLikes; i++) + PyList_SetItem(obj->like_list, i, BuildLike(creature.likes[i])); + + obj->labor_list = PyList_New(NUM_CREATURE_LABORS); + + for(int i = 0; i < NUM_CREATURE_LABORS; i++) + PyList_SetItem(obj->labor_list, i, PyInt_FromLong(creature.labors[i])); + + obj->trait_list = PyList_New(NUM_CREATURE_TRAITS); + + for(int i = 0; i < NUM_CREATURE_TRAITS; i++) + PyList_SetItem(obj->trait_list, i, PyInt_FromLong(creature.traits[i])); + + return (PyObject*)obj; +} + #endif \ No newline at end of file From 5ee28646dcb2f4c901d90055e4a9ecc932d2feac Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 7 Apr 2010 14:33:40 -0500 Subject: [PATCH 31/37] check strings for NULL --- dfhack/python/DF_Helpers.cpp | 37 ++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/dfhack/python/DF_Helpers.cpp b/dfhack/python/DF_Helpers.cpp index 8b156e535..587e07f04 100644 --- a/dfhack/python/DF_Helpers.cpp +++ b/dfhack/python/DF_Helpers.cpp @@ -38,7 +38,7 @@ static PyObject* BuildMatglossPair(DFHack::t_matglossPair& matgloss) static PyObject* BuildTreeDesc(DFHack::t_tree_desc& tree) { - Py_BuildValue("OO", BuildMatglossPair(tree.material), Py_BuildValue("III", tree.x, tree.y, tree.z)); + return Py_BuildValue("OO", BuildMatglossPair(tree.material), Py_BuildValue("III", tree.x, tree.y, tree.z)); } static PyObject* BuildSkill(DFHack::t_skill& skill) @@ -53,7 +53,19 @@ static PyObject* BuildJob(DFHack::t_job& job) static PyObject* BuildItemType(DFHack::t_itemType& item) { - return Py_BuildValue("ss", PyString_FromString(item.id), PyString_FromString(item.name)); + PyObject *id, *name; + + if(item.id[0]) + id = PyString_FromString(item.id); + else + id = PyString_FromString(""); + + if(item.name[0]) + name = PyString_FromString(item.name); + else + name = PyString_FromString(""); + + return Py_BuildValue("OO", id, name); } static PyObject* BuildLike(DFHack::t_like& like) @@ -62,7 +74,7 @@ static PyObject* BuildLike(DFHack::t_like& like) item = Py_BuildValue("iii", like.type, like.itemClass, like.itemIndex); - return Py_BuildValue("OOO", item, BuildMatglossPair(item.material), PyBool_FromLong((int)item.active)); + return Py_BuildValue("OOO", item, BuildMatglossPair(like.material), PyBool_FromLong((int)like.active)); } static PyObject* BuildNote(DFHack::t_note& note) @@ -71,7 +83,12 @@ static PyObject* BuildNote(DFHack::t_note& note) PyDict_SetItemString(noteDict, "symbol", PyString_FromFormat("%c", note.symbol)); PyDict_SetItemString(noteDict, "fore_back", Py_BuildValue("II", note.foreground, note.background)); - PyDict_SetItemString(noteDict, "name", PyString_FromString(note.name)); + + if(note.name[0]) + PyDict_SetItemString(noteDict, "name", PyString_FromString(note.name)); + else + PyDict_SetItemString(noteDict, "name", PyString_FromString("")); + PyDict_SetItemString(noteDict, "position", Py_BuildValue("III", note.x, note.y, note.z)); return noteDict; @@ -84,8 +101,16 @@ static PyObject* BuildName(DFHack::t_name& name) nameDict = PyDict_New(); - PyDict_SetItemString(nameDict, "first_name", PyString_FromString(name.first_name)); - PyDict_SetItemString(nameDict, "nickname", PyString_FromString(name.nickname)); + if(name.first_name[0]) + PyDict_SetItemString(nameDict, "first_name", PyString_FromString(name.first_name)); + else + PyDict_SetItemString(nameDict, "first_name", PyString_FromString("")); + + if(name.nickname[0]) + PyDict_SetItemString(nameDict, "nickname", PyString_FromString(name.nickname)); + else + PyDict_SetItemString(nameDict, "nickname", PyString_FromString("")); + PyDict_SetItemString(nameDict, "language", PyInt_FromLong(name.language)); PyDict_SetItemString(nameDict, "has_name", PyBool_FromLong((int)name.has_name)); From 3cfeb806929396e9ea3d9bc17224fa45becb7595 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 7 Apr 2010 14:33:56 -0500 Subject: [PATCH 32/37] updated to add creature manager --- dfhack/python/pydfhack.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index a1ee27387..feb2da2cc 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -26,6 +26,8 @@ distribution. #include "DF_Imports.cpp" #include "DF_MemInfo.cpp" #include "DF_Material.cpp" +#include "DF_CreatureType.cpp" +#include "DF_CreatureManager.cpp" #include "DF_API.cpp" #ifndef PyMODINIT_FUNC @@ -72,17 +74,27 @@ PyMODINIT_FUNC initpydfhack(void) if(PyType_Ready(&DF_Material_type) < 0) return; + if(PyType_Ready(&DF_Creature_Base_type) < 0) + return; + + if(PyType_Ready(&DF_CreatureManager_type) < 0) + return; + module = Py_InitModule3("pydfhack", module_methods, "pydfhack extension module"); Py_INCREF(&DF_API_type); Py_INCREF(&DF_MemInfo_type); Py_INCREF(&DF_Position_type); Py_INCREF(&DF_Material_type); + Py_INCREF(&DF_Creature_Base_type); + Py_INCREF(&DF_CreatureManager_type); PyModule_AddObject(module, "API", (PyObject*)&DF_API_type); PyModule_AddObject(module, "MemInfo", (PyObject*)&DF_MemInfo_type); PyModule_AddObject(module, "Position", (PyObject*)&DF_Position_type); PyModule_AddObject(module, "Materials", (PyObject*)&DF_Material_type); + PyModule_AddObject(module, "Creature_Base", (PyObject*)&DF_Position_type); + PyModule_AddObject(module, "CreatureManager", (PyObject*)&DF_Material_type); DoImports(); } \ No newline at end of file From 6cf74283d623ba0d96f1723ae46128f205f715f7 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 7 Apr 2010 16:26:22 -0500 Subject: [PATCH 33/37] first commit --- dfhack/python/DF_Translation.cpp | 105 +++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 dfhack/python/DF_Translation.cpp diff --git a/dfhack/python/DF_Translation.cpp b/dfhack/python/DF_Translation.cpp new file mode 100644 index 000000000..ce34a28cb --- /dev/null +++ b/dfhack/python/DF_Translation.cpp @@ -0,0 +1,105 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DFTRANSLATION__ +#define __DFTRANSLATION__ + +#include "Python.h" +#include "modules/Translation.h" + +using namespace + +struct DF_Translation +{ + PyObject_HEAD + DFHack::Translation* trans_Ptr; +}; + +// API type Allocation, Deallocation, and Initialization + +static PyObject* DF_Translation_new(PyTypeObject* type, PyObject* args, PyObject* kwds) +{ + DF_Translation* self; + + self = (DF_Translation*)type->tp_alloc(type, 0); + + if(self != NULL) + self->trans_Ptr = NULL; + + return (PyObject*)self; +} + +static int DF_Translation_init(DF_Translation* self, PyObject* args, PyObject* kwds) +{ + return 0; +} + +static void DF_Translation_dealloc(DF_Translation* self) +{ + if(self != NULL) + { + if(self->trans_Ptr != NULL) + { + delete self->trans_Ptr; + + self->trans_Ptr = NULL; + } + + self->ob_type->tp_free((PyObject*)self); + } +} + +// Type methods + +static PyObject* DF_Translation_Start(DF_Translation* self, PyObject* args) +{ + if(self->trans_Ptr != NULL) + { + if(self->trans_Ptr->Start()) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Translation_Finish(DF_Translation* self, PyObject* args) +{ + if(self->trans_Ptr != NULL) + { + if(self->trans_Ptr->Finish()) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Translation_TranslateName(DF_Translation* self, PyObject* args) +{ +} + +#endif \ No newline at end of file From cc7b7d9f0f29c4262ed02a93fffdb3d17f564410 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 7 Apr 2010 16:26:41 -0500 Subject: [PATCH 34/37] updated for API changes --- dfhack/python/DF_CreatureManager.cpp | 2 +- dfhack/python/DF_CreatureType.cpp | 3 ++- dfhack/python/DF_Helpers.cpp | 10 ++++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/dfhack/python/DF_CreatureManager.cpp b/dfhack/python/DF_CreatureManager.cpp index 47800b7f2..9a1d229f2 100644 --- a/dfhack/python/DF_CreatureManager.cpp +++ b/dfhack/python/DF_CreatureManager.cpp @@ -105,7 +105,7 @@ static PyObject* DF_CreatureManager_Finish(DF_CreatureManager* self, PyObject* a static PyObject* DF_CreatureManager_ReadCreature(DF_CreatureManager* self, PyObject* args) { uint32_t index; - t_creature furball; + DFHack::t_creature furball; if(self->creature_Ptr != NULL) { diff --git a/dfhack/python/DF_CreatureType.cpp b/dfhack/python/DF_CreatureType.cpp index 503a6db56..3c55b516e 100644 --- a/dfhack/python/DF_CreatureType.cpp +++ b/dfhack/python/DF_CreatureType.cpp @@ -29,10 +29,11 @@ distribution. #include "structmember.h" #include "DF_Imports.cpp" #include "DF_Helpers.cpp" -#include "DFTypes.h" using namespace DFHack; +#include "modules/Creatures.h" + struct DF_Creature_Base { PyObject_HEAD diff --git a/dfhack/python/DF_Helpers.cpp b/dfhack/python/DF_Helpers.cpp index 587e07f04..65d5f5f9a 100644 --- a/dfhack/python/DF_Helpers.cpp +++ b/dfhack/python/DF_Helpers.cpp @@ -31,15 +31,17 @@ distribution. using namespace DFHack; +#include "modules/Creatures.h" + static PyObject* BuildMatglossPair(DFHack::t_matglossPair& matgloss) { return Py_BuildValue("ii", matgloss.type, matgloss.index); } -static PyObject* BuildTreeDesc(DFHack::t_tree_desc& tree) -{ - return Py_BuildValue("OO", BuildMatglossPair(tree.material), Py_BuildValue("III", tree.x, tree.y, tree.z)); -} +// static PyObject* BuildTreeDesc(DFHack::t_tree_desc& tree) +// { + // return Py_BuildValue("OO", BuildMatglossPair(tree.material), Py_BuildValue("III", tree.x, tree.y, tree.z)); +// } static PyObject* BuildSkill(DFHack::t_skill& skill) { From 4b70268840a4d692b04bac78b7feff671c196019 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 8 Apr 2010 13:15:55 -0500 Subject: [PATCH 35/37] added error checks for strings when building composites --- dfhack/python/DF_Material.cpp | 42 +++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/dfhack/python/DF_Material.cpp b/dfhack/python/DF_Material.cpp index 5c655b4af..340cb84ce 100644 --- a/dfhack/python/DF_Material.cpp +++ b/dfhack/python/DF_Material.cpp @@ -48,11 +48,19 @@ static PyObject* BuildMatgloss(t_matgloss matgloss) matDict = PyDict_New(); - PyDict_SetItemString(matDict, "id", PyString_FromString(matgloss.id)); + if(matgloss.id[0]) + PyDict_SetItemString(matDict, "id", PyString_FromString(matgloss.id)); + else + PyDict_SetItemString(matDict, "id", PyString_FromString("")); + PyDict_SetItemString(matDict, "fore", PyInt_FromLong(matgloss.fore)); PyDict_SetItemString(matDict, "back", PyInt_FromLong(matgloss.back)); PyDict_SetItemString(matDict, "bright", PyInt_FromLong(matgloss.bright)); - PyDict_SetItemString(matDict, "name", PyString_FromString(matgloss.name)); + + if(matgloss.name[0]) + PyDict_SetItemString(matDict, "name", PyString_FromString(matgloss.name)); + else + PyDict_SetItemString(matDict, "name", PyString_FromString("")); return matDict; } @@ -63,14 +71,34 @@ static PyObject* BuildMatglossPlant(t_matglossPlant matgloss) matDict = PyDict_New(); - PyDict_SetItemString(matDict, "id", PyString_FromString(matgloss.id)); + if(matgloss.id[0]) + PyDict_SetItemString(matDict, "id", PyString_FromString(matgloss.id)); + else + PyDict_SetItemString(matDict, "id", PyString_FromString("")); + PyDict_SetItemString(matDict, "fore", PyInt_FromLong(matgloss.fore)); PyDict_SetItemString(matDict, "back", PyInt_FromLong(matgloss.back)); PyDict_SetItemString(matDict, "bright", PyInt_FromLong(matgloss.bright)); - PyDict_SetItemString(matDict, "name", PyString_FromString(matgloss.name)); - PyDict_SetItemString(matDict, "drink_name", PyString_FromString(matgloss.drink_name)); - PyDict_SetItemString(matDict, "food_name", PyString_FromString(matgloss.food_name)); - PyDict_SetItemString(matDict, "extract_name", PyString_FromString(matgloss.extract_name)); + + if(matgloss.name[0]) + PyDict_SetItemString(matDict, "name", PyString_FromString(matgloss.name)); + else + PyDict_SetItemString(matDict, "name", PyString_FromString("")); + + if(matgloss.drink_name[0]) + PyDict_SetItemString(matDict, "drink_name", PyString_FromString(matgloss.drink_name)); + else + PyDict_SetItemString(matDict, "drink_name", PyString_FromString("")); + + if(matgloss.food_name[0]) + PyDict_SetItemString(matDict, "food_name", PyString_FromString(matgloss.food_name)); + else + PyDict_SetItemString(matDict, "food_name", PyString_FromString("")); + + if(matgloss.extract_name[0]) + PyDict_SetItemString(matDict, "extract_name", PyString_FromString(matgloss.extract_name)); + else + PyDict_SetItemString(matDict, "extract_name", PyString_FromString("")); return matDict; } From e536374aa758ba96cbc4f142f8b71d20e3d8fd42 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 8 Apr 2010 13:16:41 -0500 Subject: [PATCH 36/37] added empty initializers for strings --- dfhack/python/DF_CreatureType.cpp | 132 ++++++++++++++++-------------- 1 file changed, 69 insertions(+), 63 deletions(-) diff --git a/dfhack/python/DF_CreatureType.cpp b/dfhack/python/DF_CreatureType.cpp index 3c55b516e..7eae9569e 100644 --- a/dfhack/python/DF_CreatureType.cpp +++ b/dfhack/python/DF_CreatureType.cpp @@ -29,11 +29,10 @@ distribution. #include "structmember.h" #include "DF_Imports.cpp" #include "DF_Helpers.cpp" +#include "modules/Creatures.h" using namespace DFHack; -#include "modules/Creatures.h" - struct DF_Creature_Base { PyObject_HEAD @@ -117,23 +116,23 @@ static void DF_Creature_Base_dealloc(DF_Creature_Base* self) { if(self != NULL) { - Py_CLEAR(self->position); - Py_CLEAR(self->flags1); - Py_CLEAR(self->flags2); + Py_XDECREF(self->position); + Py_XDECREF(self->flags1); + Py_XDECREF(self->flags2); - Py_CLEAR(self->custom_profession); - Py_CLEAR(self->name); - Py_CLEAR(self->squad_name); - Py_CLEAR(self->artifact_name); - Py_CLEAR(self->current_job); + Py_XDECREF(self->custom_profession); + Py_XDECREF(self->name); + Py_XDECREF(self->squad_name); + Py_XDECREF(self->artifact_name); + Py_XDECREF(self->current_job); - Py_CLEAR(self->flags1); - Py_CLEAR(self->flags2); + Py_XDECREF(self->flags1); + Py_XDECREF(self->flags2); - Py_CLEAR(self->labor_list); - Py_CLEAR(self->trait_list); - Py_CLEAR(self->skill_list); - Py_CLEAR(self->like_list); + Py_XDECREF(self->labor_list); + Py_XDECREF(self->trait_list); + Py_XDECREF(self->skill_list); + Py_XDECREF(self->like_list); // if(self->labor_list != NULL) // PyList_Clear(self->labor_list); @@ -222,55 +221,62 @@ static PyObject* BuildCreature(DFHack::t_creature& creature) obj = (DF_Creature_Base*)PyObject_Call((PyObject*)&DF_Creature_Base_type, NULL, NULL); - obj->position = Py_BuildValue("III", creature.x, creature.y, creature.z); - obj->profession = creature.profession; - obj->c_type = creature.type; - obj->mood = creature.mood; - obj->happiness = creature.happiness; - obj->c_id = creature.id; - obj->agility = creature.agility; - obj->strength = creature.strength; - obj->toughness = creature.toughness; - obj->money = creature.money; - obj->squad_leader_id = creature.squad_leader_id; - obj->sex = creature.sex; - obj->pregnancy_timer = creature.pregnancy_timer; - obj->blood_max = creature.blood_max; - obj->blood_current = creature.blood_current; - obj->bleed_rate = creature.bleed_rate; - - if(creature.custom_profession[0]) - obj->custom_profession = PyString_FromString(creature.custom_profession); - - obj->flags1 = PyObject_Call(CreatureFlags1_type, PyInt_FromLong(creature.flags1.whole), NULL); - obj->flags2 = PyObject_Call(CreatureFlags2_type, PyInt_FromLong(creature.flags2.whole), NULL); - - obj->current_job = BuildJob(creature.current_job); - obj->name = BuildName(creature.name); - obj->squad_name = BuildName(creature.squad_name); - obj->artifact_name = BuildName(creature.artifact_name); - - obj->skill_list = PyList_New(creature.numSkills); - - for(int i = 0; i < creature.numSkills; i++) - PyList_SetItem(obj->skill_list, i, BuildSkill(creature.skills[i])); - - obj->like_list = PyList_New(creature.numLikes); - - for(int i = 0; i < creature.numLikes; i++) - PyList_SetItem(obj->like_list, i, BuildLike(creature.likes[i])); - - obj->labor_list = PyList_New(NUM_CREATURE_LABORS); - - for(int i = 0; i < NUM_CREATURE_LABORS; i++) - PyList_SetItem(obj->labor_list, i, PyInt_FromLong(creature.labors[i])); - - obj->trait_list = PyList_New(NUM_CREATURE_TRAITS); + if(obj != NULL) + { + obj->position = Py_BuildValue("III", creature.x, creature.y, creature.z); + obj->profession = creature.profession; + obj->c_type = creature.type; + obj->mood = creature.mood; + obj->happiness = creature.happiness; + obj->c_id = creature.id; + obj->agility = creature.agility; + obj->strength = creature.strength; + obj->toughness = creature.toughness; + obj->money = creature.money; + obj->squad_leader_id = creature.squad_leader_id; + obj->sex = creature.sex; + obj->pregnancy_timer = creature.pregnancy_timer; + obj->blood_max = creature.blood_max; + obj->blood_current = creature.blood_current; + obj->bleed_rate = creature.bleed_rate; + + if(creature.custom_profession[0]) + obj->custom_profession = PyString_FromString(creature.custom_profession); + + obj->flags1 = PyObject_Call(CreatureFlags1_type, PyInt_FromLong(creature.flags1.whole), NULL); + obj->flags2 = PyObject_Call(CreatureFlags2_type, PyInt_FromLong(creature.flags2.whole), NULL); + + obj->current_job = BuildJob(creature.current_job); + obj->name = BuildName(creature.name); + obj->squad_name = BuildName(creature.squad_name); + obj->artifact_name = BuildName(creature.artifact_name); + + obj->skill_list = PyList_New(creature.numSkills); + + for(int i = 0; i < creature.numSkills; i++) + PyList_SetItem(obj->skill_list, i, BuildSkill(creature.skills[i])); + + obj->like_list = PyList_New(creature.numLikes); + + for(int i = 0; i < creature.numLikes; i++) + PyList_SetItem(obj->like_list, i, BuildLike(creature.likes[i])); + + obj->labor_list = PyList_New(NUM_CREATURE_LABORS); + + for(int i = 0; i < NUM_CREATURE_LABORS; i++) + PyList_SetItem(obj->labor_list, i, PyInt_FromLong(creature.labors[i])); + + obj->trait_list = PyList_New(NUM_CREATURE_TRAITS); + + for(int i = 0; i < NUM_CREATURE_TRAITS; i++) + PyList_SetItem(obj->trait_list, i, PyInt_FromLong(creature.traits[i])); + + Py_INCREF((PyObject*)obj); - for(int i = 0; i < NUM_CREATURE_TRAITS; i++) - PyList_SetItem(obj->trait_list, i, PyInt_FromLong(creature.traits[i])); + return (PyObject*)obj; + } - return (PyObject*)obj; + Py_RETURN_NONE; } #endif \ No newline at end of file From b555085e8c2198d282a837a506df8a28f4f9c7ff Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 8 Apr 2010 14:08:58 -0500 Subject: [PATCH 37/37] hopefully fixed reference counting when adding to dictionaries --- dfhack/python/DF_Helpers.cpp | 94 ++++++++++++++++++++++++++++------- dfhack/python/DF_Material.cpp | 43 ++++++++++++---- 2 files changed, 111 insertions(+), 26 deletions(-) diff --git a/dfhack/python/DF_Helpers.cpp b/dfhack/python/DF_Helpers.cpp index 65d5f5f9a..82890a4d8 100644 --- a/dfhack/python/DF_Helpers.cpp +++ b/dfhack/python/DF_Helpers.cpp @@ -79,47 +79,86 @@ static PyObject* BuildLike(DFHack::t_like& like) return Py_BuildValue("OOO", item, BuildMatglossPair(like.material), PyBool_FromLong((int)like.active)); } +//PyDict_SetItem and PyDict_SetItemString don't steal references, so this had to get a bit more complicated... static PyObject* BuildNote(DFHack::t_note& note) { PyObject* noteDict = PyDict_New(); + PyObject* temp; - PyDict_SetItemString(noteDict, "symbol", PyString_FromFormat("%c", note.symbol)); - PyDict_SetItemString(noteDict, "fore_back", Py_BuildValue("II", note.foreground, note.background)); + temp = PyString_FromFormat("%c", note.symbol); + + PyDict_SetItemString(noteDict, "symbol", temp); + + Py_DECREF(temp); + + temp = Py_BuildValue("II", note.foreground, note.background); + + PyDict_SetItemString(noteDict, "fore_back", temp); + + Py_DECREF(temp); if(note.name[0]) - PyDict_SetItemString(noteDict, "name", PyString_FromString(note.name)); + temp = PyString_FromString(note.name); else - PyDict_SetItemString(noteDict, "name", PyString_FromString("")); + PyString_FromString(""); + + PyDict_SetItemString(noteDict, "name", temp); + + Py_DECREF(temp); + + temp = Py_BuildValue("III", note.x, note.y, note.z); - PyDict_SetItemString(noteDict, "position", Py_BuildValue("III", note.x, note.y, note.z)); + PyDict_SetItemString(noteDict, "position", temp); + + Py_DECREF(temp); return noteDict; } +//same here...reference counting is kind of a pain, assuming I'm even doing it right... static PyObject* BuildName(DFHack::t_name& name) { PyObject* nameDict; PyObject *wordList, *speechList; + PyObject* temp; + int wordCount = 7; nameDict = PyDict_New(); if(name.first_name[0]) - PyDict_SetItemString(nameDict, "first_name", PyString_FromString(name.first_name)); + temp = PyString_FromString(name.first_name); else - PyDict_SetItemString(nameDict, "first_name", PyString_FromString("")); + temp = PyString_FromString(""); + + PyDict_SetItemString(nameDict, "first_name", temp); + + Py_DECREF(temp); if(name.nickname[0]) - PyDict_SetItemString(nameDict, "nickname", PyString_FromString(name.nickname)); + temp = PyString_FromString(name.nickname); else - PyDict_SetItemString(nameDict, "nickname", PyString_FromString("")); + temp = PyString_FromString(""); + + PyDict_SetItemString(nameDict, "nickname", temp); + + Py_DECREF(temp); + + temp = PyInt_FromLong(name.language); - PyDict_SetItemString(nameDict, "language", PyInt_FromLong(name.language)); - PyDict_SetItemString(nameDict, "has_name", PyBool_FromLong((int)name.has_name)); + PyDict_SetItemString(nameDict, "language", temp); - wordList = PyList_New(7); - speechList = PyList_New(7); + Py_DECREF(temp); - for(int i = 0; i < 7; i++) + temp = PyBool_FromLong((int)name.has_name); + + PyDict_SetItemString(nameDict, "has_name", temp); + + Py_DECREF(temp); + + wordList = PyList_New(wordCount); + speechList = PyList_New(wordCount); + + for(int i = 0; i < wordCount; i++) { PyList_SetItem(wordList, i, PyInt_FromLong(name.words[i])); PyList_SetItem(wordList, i, PyInt_FromLong(name.parts_of_speech[i])); @@ -128,6 +167,9 @@ static PyObject* BuildName(DFHack::t_name& name) PyDict_SetItemString(nameDict, "words", wordList); PyDict_SetItemString(nameDict, "parts_of_speech", speechList); + Py_DECREF(wordList); + Py_DECREF(speechList); + return nameDict; } @@ -135,18 +177,36 @@ static PyObject* BuildSettlement(DFHack::t_settlement& settlement) { PyObject* setDict; PyObject *local_pos1, *local_pos2; + PyObject* temp; setDict = PyDict_New(); - PyDict_SetItemString(setDict, "origin", PyInt_FromLong(settlement.origin)); - PyDict_SetItemString(setDict, "name", BuildName(settlement.name)); - PyDict_SetItemString(setDict, "world_pos", Py_BuildValue("ii", settlement.world_x, settlement.world_y)); + temp = PyInt_FromLong(settlement.origin); + + PyDict_SetItemString(setDict, "origin", temp); + + Py_DECREF(temp); + + temp = BuildName(settlement.name); + + PyDict_SetItemString(setDict, "name", temp); + + Py_DECREF(temp); + + temp = Py_BuildValue("ii", settlement.world_x, settlement.world_y); + + PyDict_SetItemString(setDict, "world_pos", temp); + + Py_DECREF(temp); local_pos1 = Py_BuildValue("ii", settlement.local_x1, settlement.local_y1); local_pos2 = Py_BuildValue("ii", settlement.local_x2, settlement.local_y2); PyDict_SetItemString(setDict, "local_pos", Py_BuildValue("OO", local_pos1, local_pos2)); + Py_DECREF(local_pos1); + Py_DECREF(local_pos2); + return setDict; } diff --git a/dfhack/python/DF_Material.cpp b/dfhack/python/DF_Material.cpp index 340cb84ce..6e4615441 100644 --- a/dfhack/python/DF_Material.cpp +++ b/dfhack/python/DF_Material.cpp @@ -42,30 +42,53 @@ struct DF_Material // Helpers -static PyObject* BuildMatgloss(t_matgloss matgloss) +static PyObject* BuildMatgloss(t_matgloss& matgloss) { PyObject* matDict; + PyObject* temp; matDict = PyDict_New(); if(matgloss.id[0]) - PyDict_SetItemString(matDict, "id", PyString_FromString(matgloss.id)); + temp = PyString_FromString(matgloss.id); else - PyDict_SetItemString(matDict, "id", PyString_FromString("")); + temp = PyString_FromString(""); - PyDict_SetItemString(matDict, "fore", PyInt_FromLong(matgloss.fore)); - PyDict_SetItemString(matDict, "back", PyInt_FromLong(matgloss.back)); - PyDict_SetItemString(matDict, "bright", PyInt_FromLong(matgloss.bright)); + PyDict_SetItemString(matDict, "id", temp); + + Py_DECREF(temp); + + temp = PyInt_FromLong(matgloss.fore); + + PyDict_SetItemString(matDict, "fore", temp); + + Py_DECREF(temp); + + temp = PyInt_FromLong(matgloss.back); + + PyDict_SetItemString(matDict, "back", temp); + + Py_DECREF(temp); + + temp = PyInt_FromLong(matgloss.bright); + + PyDict_SetItemString(matDict, "bright", temp); + + Py_DECREF(temp); if(matgloss.name[0]) - PyDict_SetItemString(matDict, "name", PyString_FromString(matgloss.name)); + temp = PyString_FromString(matgloss.name); else - PyDict_SetItemString(matDict, "name", PyString_FromString("")); + temp = PyString_FromString(""); + + PyDict_SetItemString(matDict, "name", temp); + + Py_DECREF(temp); return matDict; } -static PyObject* BuildMatglossPlant(t_matglossPlant matgloss) +static PyObject* BuildMatglossPlant(t_matglossPlant& matgloss) { PyObject* matDict; @@ -115,6 +138,8 @@ static PyObject* BuildMatglossList(std::vector & matVec) PyObject* matgloss = BuildMatgloss(*matIter); PyList_Append(matList, matgloss); + + Py_DECREF(matgloss); } return matList;