Can now read messages in RST files
2 files changed, 31 insertions(+), 3 deletions(-)

M player.py
M result.py
M player.py +28 -2
@@ 2,6 2,7 @@ 
 Classes for reading player data such as found in RST files.
 """
 
+import struct
 from common import PlanetsData
 
 class Base(PlanetsData):

          
@@ 34,7 35,7 @@ class Base(PlanetsData):
               'build_torptype',
               'build_torpcount',
               'unused')
-
+    
 class Planet(PlanetsData):
     """List of player's planets"""
     # No FILENAME, but typically: PDATAx.DAT or PDATAx.DIS (x=player)

          
@@ 126,4 127,29 @@ class Ship(PlanetsData):
               'transfer_supplies',
               'transfer_id',
               'intercept_id',
-              'credits')
  No newline at end of file
+              'credits')
+    
+def read_messages(f):
+    """Reads and returns list of messages found in RST or MDATAx.DAT files"""
+    result = {}
+    # Message count should preceed messages (arguments are ignored)
+    (count,) = struct.unpack('< h', f.read(2))
+    # Get pointer + size of each message (if count > 0)
+    if count:
+        pointers = {}
+        sizes = {}
+        for i in range(1, count+1):
+            (pointers[i],) = struct.unpack('< i', f.read(4))
+            pointers[i] = int(pointers[i]) - 1 # Fix BASIC-style pointer
+            (sizes[i],) = struct.unpack('< h', f.read(2))
+    for key, value in pointers.items():
+        f.seek(value)
+        data = f.read(sizes[key])
+        message = ''
+        for i in range(0, len(data)):
+            char = chr(ord(data[i]) - 13)
+            if ord(char) == 13:
+                char = chr(10) # Unix-style newline
+            message += char
+        result[key] = message
+    return result
  No newline at end of file

          
M result.py +3 -1
@@ 3,7 3,7 @@ Functions for reading and parsing RST (r
 """
 
 import struct
-from player import Base, Planet, Ship
+from player import Base, Planet, Ship, read_messages
 
 class InvalidResultFile(Exception):
     """Exception raised when the RST file is invalid or corrupt"""

          
@@ 20,6 20,8 @@ def read(f, filesize):
     result['planets'] = Planet.read(f, has_count=True)
     f.seek(pointers['bases'])
     result['bases'] = Base.read(f, has_count=True)
+    f.seek(pointers['messages'])
+    result['messages'] = read_messages(f)
     return result
     
 def _valid_pointer(pointer, filesize, datasize):