aboutsummaryrefslogtreecommitdiffstats
path: root/src/layerdata/levels.coffee
blob: beb0bb8802438e9641f5c9afff458f9c9fa247e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class PSDLevels
  constructor: (@layer, @length) ->
    @file = @layer.file
    @data =
      records: []

  parse: ->
    start = @file.tell()

    version = @file.readShortInt()
    assert version is 1

    @parseLevelRecords()

    # PS CS (8.0) additional information
    if @file.tell() - start < @length - 4
      tag = @file.readf ">4s"
      assert.equal tag, "Lvls"

      version = @file.readShortInt()
      assert.equal version, 3

      # Figure out the total number of level record structures
      # Subtract the legacy number of level record structures (29)
      # to determine how many are remaining in the file for reading.
      @data.levelCount = @file.readShortInt() - 29
      assert levelCount >= 0
      @parseLevelRecords(levelCount)

      # Only return the important data. Don't need to hold on to this
      # entire class.
      return @data


  parseLevelRecords: (count = 29) ->
    # 29 sets of level records. each level has 5 short ints
    for i in [0...count]
      record = {}
      [
        record.inputFloor,     # (0...253)
        record.inputCeiling,   # (2...255)
        record.outputFloor,    # (0...255)
        record.outputCeiling,  # (0...255)
        record.gamma           # (10...999)
      ] = @file.readf ">hhhhh"

      record.gamma /= 100

      # Sets 28 and 29 are reserved
      if i < 27
        assert record.inputFloor >= 0 and record.inputFloor <= 255
        assert record.inputCeiling >= 2 and record.inputCeiling <= 255
        assert record.outputFloor >= 0 and record.outputFloor <= 255
        assert record.outputCeiling >= 0 and record.outputCeiling <= 255
        assert record.gamma >= 0.1 and record.gamma <= 9.99

      @data.records.push record