Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

demo takes path, completely manages file

  • Loading branch information...
commit dd4073bcb969d3754ddfeb28e376f198565f91ec 1 parent f542d32
@onethirtyfive onethirtyfive authored
Showing with 26 additions and 11 deletions.
  1. +0 −5 skadi/__init__.py
  2. +25 −4 skadi/demo.py
  3. +1 −2  tests/test_demo.py
View
5 skadi/__init__.py
@@ -58,11 +58,6 @@ class InvalidDemo(RuntimeError):
def load(io, tick=0):
- if io.read(8) != "PBUFDEM\0":
- raise InvalidDemo('malformed header')
-
- io.read(4) # game summary offset in file in bytes
-
demo_io = d_io.construct(io)
prologue = i_p.construct(demo_io)
View
29 skadi/demo.py
@@ -1,6 +1,10 @@
+from __future__ import absolute_import
+
import collections as c
import copy
+import io as _io
+from skadi import *
from skadi import index as i
from skadi.engine import world as w
from skadi.engine import game_event as e_ge
@@ -173,10 +177,27 @@ def advance(self, tick, pbmsg):
class Demo(object):
- def __init__(self, prologue, io):
- self.prologue = prologue
- self.io = io
- self._tell = io.tell()
+ def __init__(self, abspath):
+ infile = _io.open(abspath, 'r+b')
+ if infile.read(8) != "PBUFDEM\0":
+ raise InvalidDemo('malformed header')
+
+ gio = bytearray(infile.read(4)) # LE uint file offset
+ gio = sum(gio[i] << (i * 8) for i in range(4))
+
+ try:
+ tell = infile.tell()
+ infile.seek(gio)
+ p, m = d_io.construct(infile).read()
+ self.file_info = d_io.parse(p.kind, p.compressed, m)
+ assert p.kind == pb_d.DEM_FileInfo
+ infile.seek(tell)
+ except EOFError:
+ raise InvalidDemo('no end game summary')
+
+ self.prologue = load(infile)
+ self.io = infile
+ self._tell = infile.tell()
def stream(self, tick=0):
self.io.seek(self._tell)
View
3  tests/test_demo.py
@@ -20,8 +20,7 @@ class TestDemo(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Cache demo for re-use in multiple tests
- with _io.open(DEMO_FILE_PATH, 'r+b') as infile:
- cls.demo = demo.construct(load(infile), infile)
+ cls.demo = demo.construct(DEMO_FILE_PATH)
def test_demo_construct(self):
assert self.demo
Please sign in to comment.
Something went wrong with that request. Please try again.