M common.py +22 -22
@@ 9,16 9,16 @@ SUBVERSION = ('00', '01',)
class PlanetsData(object):
"""Base class for all Planets data objects"""
- filename = None # Default filename
- count = None # Default number of objects in a data file
- pack_length = None # Length of binary data for single object
- pack_format = None # Format of binary data for single object
- fields = () # Fields for single object
+ FILENAME = None # Default filename
+ COUNT = None # Default number of objects in a data file
+ PACK_LENGTH = None # Length of binary data for single object
+ PACK_FORMAT = None # Format of binary data for single object
+ FIELDS = () # Fields for single object
def __init__(self):
# Expand field list where necessary (in case of dictionary fields)
fields = []
- for field in self.fields:
+ for field in self.FIELDS:
# tuple format: ('name', start, end,) start/end must be 'int'
if isinstance(field, tuple) and (len(field) == 3):
for i in range(field[1], field[1]+field[2]):
@@ 26,10 26,10 @@ class PlanetsData(object):
else:
fields.append(field)
fields = tuple(fields)
- if self.__class__.fields is not fields:
- self.__class__.fields = fields
+ if self.__class__.FIELDS is not fields:
+ self.__class__.FIELDS = fields
# Initialize fields
- for field in self.fields:
+ for field in self.FIELDS:
# Dictionary field: see EngSpec class in fixed.py for an example
if isinstance(field, tuple):
setattr(self, str(field[0])+'['+str(field[1])+']', None)
@@ 39,12 39,12 @@ class PlanetsData(object):
def unpack(self, data):
"""Unpacks binary data into class instance variables"""
- if len(data) is not self.pack_length:
+ if len(data) is not self.PACK_LENGTH:
raise ValueError('Binary data passed has incorrect length')
- values = struct.unpack(self.pack_format, data)
- if len(self.fields) is not len(values):
+ values = struct.unpack(self.PACK_FORMAT, data)
+ if len(self.FIELDS) is not len(values):
raise ValueError('Incorrect number of variables in pack format')
- for name, value in zip(self.fields, values):
+ for name, value in zip(self.FIELDS, values):
# Dictionary field: see EngSpec class in fixed.py for an example
if isinstance(name, tuple):
setattr(self, str(name[0])+'['+str(name[1])+']', value)
@@ 64,14 64,14 @@ class PlanetsData(object):
"""Unpacks binary data into a list of object instances"""
result = {}
if count is None:
- if cls.count is not None:
- count = cls.count
- elif len(data) >= cls.pack_length:
- count = int(len(data) / cls.pack_length)
+ if cls.COUNT is not None:
+ count = cls.COUNT
+ elif len(data) >= cls.PACK_LENGTH:
+ count = int(len(data) / cls.PACK_LENGTH)
for i in range(1, count+1): # For compatibility, count from 1 and not 0
instance = cls()
- start = cls.pack_length*(i-1)
- end = cls.pack_length*i
+ start = cls.PACK_LENGTH*(i-1)
+ end = cls.PACK_LENGTH*i
instance.unpack(data[start:end])
if instance.id is None:
instance.id = i
@@ 91,9 91,9 @@ class PlanetsData(object):
(count,) = struct.unpack('< h', f.read(2))
# Load "count" objects or predetermined "count" objects or whole file
if count is not None:
- data = f.read(cls.pack_length * count)
- elif cls.count is not None:
- data = f.read(cls.pack_length * cls.count)
+ data = f.read(cls.PACK_LENGTH * count)
+ elif cls.COUNT is not None:
+ data = f.read(cls.PACK_LENGTH * cls.COUNT)
else:
data = f.read()
result = cls.load(data, count)
M fixed.py +20 -20
@@ 7,11 7,11 @@ from common import PlanetsData
class BeamSpec(PlanetsData):
"""List of starship beam weapons"""
- filename = 'BEAMSPEC.DAT'
- count = 10
- pack_length = 36
- pack_format = '< 20s h h h h h h h h'
- fields = ('name',
+ FILENAME = 'BEAMSPEC.DAT'
+ COUNT = 10
+ PACK_LENGTH = 36
+ PACK_FORMAT = '< 20s h h h h h h h h'
+ FIELDS = ('name',
'cost',
'tritanium',
'duranium',
@@ 23,11 23,11 @@ class BeamSpec(PlanetsData):
class EngSpec(PlanetsData):
"""List of starship engines"""
- filename = 'ENGSPEC.DAT'
- count = 9
- pack_length = 66
- pack_format = '< 20s h h h h h 9i'
- fields = ('name',
+ FILENAME = 'ENGSPEC.DAT'
+ COUNT = 9
+ PACK_LENGTH = 66
+ PACK_FORMAT = '< 20s h h h h h 9i'
+ FIELDS = ('name',
'cost',
'tritanium',
'duranium',
@@ 37,11 37,11 @@ class EngSpec(PlanetsData):
class HullSpec(PlanetsData):
"""List of starship hulls"""
- filename = 'HULLSPEC.DAT'
- # No count, but typically 105 objects in default HULLSPEC.DAT
- pack_length = 60
- pack_format = '< 30s h h h h h h h h h h h h h h h'
- fields = ('name',
+ FILENAME = 'HULLSPEC.DAT'
+ # No COUNT, but typically 105 objects in default HULLSPEC.DAT
+ PACK_LENGTH = 60
+ PACK_FORMAT = '< 30s h h h h h h h h h h h h h h h'
+ FIELDS = ('name',
'picture',
'damaged', # Unused field for picture of damaged ship
'tritanium',
@@ 60,11 60,11 @@ class HullSpec(PlanetsData):
class TorpSpec(PlanetsData):
"""List of starship torpedo weapons"""
- filename = 'TORPSPEC.DAT'
- count = 10
- pack_length = 38
- pack_format = '< 20s h h h h h h h h h'
- fields = ('name',
+ FILENAME = 'TORPSPEC.DAT'
+ COUNT = 10
+ PACK_LENGTH = 38
+ PACK_FORMAT = '< 20s h h h h h h h h h'
+ FIELDS = ('name',
'torpedo_cost',
'launcher_cost',
'tritanium',
M player.py +27 -27
@@ 11,11 11,11 @@ from common import PlanetsData
class Base(PlanetsData):
"""List of player's starbases"""
- # No filename, but typically: BDATAx.DAT or BDATAx.DIS (x=player)
- # No count, but obviously limited by number of planets
- pack_length = 156
- pack_format = '< h h h h h h h h 9h 20h 10h 10h 10h h h h h h h h h h h h'
- fields = ('id',
+ # No FILENAME, but typically: BDATAx.DAT or BDATAx.DIS (x=player)
+ # No COUNT, but obviously limited by number of planets
+ PACK_LENGTH = 156
+ PACK_FORMAT = '< h h h h h h h h 9h 20h 10h 10h 10h h h h h h h h h h h h'
+ FIELDS = ('id',
'owner',
'defense',
'damage',
@@ 42,11 42,11 @@ class Base(PlanetsData):
class Planet(PlanetsData):
"""List of player's planets"""
- # No filename, but typically: PDATAx.DAT or PDATAx.DIS (x=player)
- # No count, but maximum 500
- pack_length = 85
- pack_format = '<hh 3s hhh iiiiiiiiiii hhhhhhhhh i hhh'
- fields = ('owner',
+ # No FILENAME, but typically: PDATAx.DAT or PDATAx.DIS (x=player)
+ # No COUNT, but maximum 500
+ PACK_LENGTH = 85
+ PACK_FORMAT = '<hh 3s hhh iiiiiiiiiii hhhhhhhhh i hhh'
+ FIELDS = ('owner',
'id',
'friendlycode',
'mines',
@@ 84,11 84,11 @@ class Planet(PlanetsData):
class Ship(PlanetsData):
"""List of player's ships"""
- # No filename, but typically: SHIPx.DAT or SHIPx.DIS (x=player)
- # No count, but normally maximum 500 and absolute max of 999 (host999)
- pack_length = 107
- pack_format = '<hh 3s hhhhhhhhhhhhhhhhhhh 20s hhhhhhhhhhhhhhhhhhhhh'
- fields = ('id',
+ # No FILENAME, but typically: SHIPx.DAT or SHIPx.DIS (x=player)
+ # No COUNT, but normally maximum 500 and absolute max of 999 (host999)
+ PACK_LENGTH = 107
+ PACK_FORMAT = '<hh 3s hhhhhhhhhhhhhhhhhhh 20s hhhhhhhhhhhhhhhhhhhhh'
+ FIELDS = ('id',
'owner',
'friendlycode',
'warp',
@@ 135,11 135,11 @@ class Ship(PlanetsData):
class ShipXY(PlanetsData):
"""List of ship coordinates"""
- # No filename, byt typically: SHIPXYx.DAT (x=player)
- count = 500 # Not compatible with host999
- pack_length = 8
- pack_format = '< h h h h'
- fields = ('x',
+ # No FILENAME, byt typically: SHIPXYx.DAT (x=player)
+ COUNT = 500 # Not compatible with host999
+ PACK_LENGTH = 8
+ PACK_FORMAT = '< h h h h'
+ FIELDS = ('x',
'y',
'owner',
'mass')
@@ 155,11 155,11 @@ class ShipXY(PlanetsData):
class Target(PlanetsData):
"""List of of visual enemy ships (contacts)"""
- # No filename, but typically: TARGETx.DAT (x=player)
- # No count, but obviously limited by number of ships
- pack_length = 34
- pack_format = '< h h h h h h h 20s'
- fields = ('id',
+ # No FILENAME, but typically: TARGETx.DAT (x=player)
+ # No COUNT, but obviously limited by number of ships
+ PACK_LENGTH = 34
+ PACK_FORMAT = '< h h h h h h h 20s'
+ FIELDS = ('id',
'owner',
'warp',
'x',
@@ 181,13 181,13 @@ class Target(PlanetsData):
if offset is not None:
f.seek(offset)
(count,) = struct.unpack('< h', f.read(2))
- data = f.read(cls.pack_length * count)
+ data = f.read(cls.PACK_LENGTH * count)
result = cls.load(data, count)
# Load extra data if present in WinPlay-style RST file
if extra_offset is not None:
f.seek(extra_offset)
(count,) = struct.unpack('< i', f.read(4))
- data = f.read(cls.pack_length * count)
+ data = f.read(cls.PACK_LENGTH * count)
extra = cls.load(data, count)
# Extra data has encrypted ship name
for k in extra.keys():