about summary refs log tree commit diff stats
path: root/app/assets/javascripts/serializer.js
blob: 70c7f0fd27832828fa3dc52e0a05f24800527970 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
namespace(function() {

window.serializePuzzle = function(puzzle) {
  var s = new Serializer('w')
  var version = 0

  s.writeInt(version)
  s.writeByte(puzzle.width)
  s.writeByte(puzzle.height)
  s.writeString(puzzle.name)

  var genericFlags = 0
  if (puzzle.autoSolved) genericFlags |= GENERIC_FLAG_AUTOSOLVED
  if (puzzle.symmetry) {
    genericFlags |= GENERIC_FLAG_SYMMETRICAL
    if (puzzle.symmetry.x) genericFlags |= GENERIC_FLAG_SYMMETRY_X
    if (puzzle.symmetry.y) genericFlags |= GENERIC_FLAG_SYMMETRY_Y
  }
  if (puzzle.pillar) genericFlags |= GENERIC_FLAG_PILLAR
  s.writeByte(genericFlags)
  for (var x=0; x<puzzle.width; x++) {
    for (var y=0; y<puzzle.height; y++) {
      s.writeCell(puzzle.grid[x][y])
    }
  }

  if (puzzle.path != null) {
    var startPos = puzzle.path.pop()
    if (puzzle.path.length > 0) {
      s.writeInt(puzzle.path.length)
      s.writeByte(startPos.x)
      s.writeByte(startPos.y)
      for (var dir of puzzle.path) s.writeByte(dir)
    }
  } else {
    s.writeInt(0)
  }

  var settingsFlags = 0
  if (puzzle.settings.NEGATIONS_CANCEL_NEGATIONS) settingsFlags |= SETTINGS_FLAG_NCN
  if (puzzle.settings.SHAPELESS_ZERO_POLY)        settingsFlags |= SETTINGS_FLAG_SZP
  if (puzzle.settings.PRECISE_POLYOMINOS)         settingsFlags |= SETTINGS_FLAG_PP
  if (puzzle.settings.FLASH_FOR_ERRORS)           settingsFlags |= SETTINGS_FLAG_FFE
  if (puzzle.settings.FAT_STARTPOINTS)            settingsFlags |= SETTINGS_FLAG_FS
  if (puzzle.settings.CUSTOM_MECHANICS)           settingsFlags |= SETTINGS_FLAG_CM
  if (puzzle.settings.INVISIBLE_SYMMETRY)         settingsFlags |= SETTINGS_FLAG_IS
  s.writeByte(settingsFlags)

  s.writeByte(puzzle.symType)

  return s.str()
}

window.deserializePuzzle = function(data) {
  // Data is JSON, so decode it with the old deserializer
  if (data[0] == '{') return Puzzle.deserialize(data)

  var s = new Serializer('r', data)
  var version = s.readInt()
  if (version > 0) throw Error('Cannot read data from unknown version: ' + version)

  var width = s.readByte()
  var height = s.readByte()
  var puzzle = new Puzzle(Math.floor(width / 2), Math.floor(height / 2))
  puzzle.name = s.readString()

  var genericFlags = s.readByte()
  puzzle.autoSolved = genericFlags & GENERIC_FLAG_AUTOSOLVED
  puzzle.symType = SYM_TYPE_NONE
  if ((genericFlags & GENERIC_FLAG_SYMMETRICAL) != 0) {
    if ((genericFlags & GENERIC_FLAG_SYMMETRY_X) != 0) {
      if ((genericFlags & GENERIC_FLAG_SYMMETRY_Y) != 0) {
        puzzle.symType = SYM_TYPE_ROTATIONAL
      } else {
        puzzle.symType = SYM_TYPE_VERTICAL
      }
    } else if ((genericFlags & GENERIC_FLAG_SYMMETRY_Y) != 0) {
      puzzle.symType = SYM_TYPE_HORIZONTAL
    }
  }
  puzzle.pillar = (genericFlags & GENERIC_FLAG_PILLAR) != 0
  for (var x=0; x<width; x++) {
    for (var y=0; y<height; y++) {
      puzzle.grid[x][y] = s.readCell()
    }
  }

  var pathLength = s.readInt()
  if (pathLength > 0) {
    var path = [{
      'x': s.readByte(),
      'y': s.readByte(),
    }]
    for (var i=0; i<pathLength; i++) path.push(s.readByte())
  }

  var settingsFlags = s.readByte()
  puzzle.settings = {
    NEGATIONS_CANCEL_NEGATIONS: (settingsFlags & SETTINGS_FLAG_NCN) != 0,
    SHAPELESS_ZERO_POLY:        (settingsFlags & SETTINGS_FLAG_SZP) != 0,
    PRECISE_POLYOMINOS:         (settingsFlags & SETTINGS_FLAG_PP) != 0,
    FLASH_FOR_ERRORS:           (settingsFlags & SETTINGS_FLAG_FFE) != 0,
    FAT_STARTPOINTS:            (settingsFlags & SETTINGS_FLAG_FS) != 0,
    CUSTOM_MECHANICS:           (settingsFlags & SETTINGS_FLAG_CM) != 0,
    INVISIBLE_SYMMETRY:         (settingsFlags & SETTINGS_FLAG_IS) != 0,
  }

  if (s.hasLeft(1)) {
    puzzle.symType = s.readByte()
  }

  s.destroy()
  return puzzle
}

class Serializer {
  constructor(mode, data) {
    this.mode = mode
    if (mode == 'r') {
      if (data == null) throw Error('No data provided to a read constructor')
      if (data[0] != '_') throw Error('Cannot read data, improperly prefixed')
      this.data = window.atob(data.slice(1))
      this.index = 0
    } else if (mode == 'w') {
      this.data = []

      var canvas = document.createElement('canvas')
      canvas.height = 1
      canvas.width = 1
      this.colorConverter = canvas.getContext('2d')
    } else {
      throw Error('Unknown serializer mode: ' + mode)
    }

  }

  destroy() {
    if (this.mode == 'r' && this.index < this.data.length) {
      throw Error('Read not done, ' + (this.data.length - this.index) + ' bytes remain')
    }
  }

  str() {
    if (this.mode != 'w') throw Error('Cannot get string from a serializer opened in mode: ' + this.mode)
    return '_' + window.btoa(this.data)
  }

  _checkRead(numBytes = 1) {
    if (this.mode != 'r') throw Error('Cannot read data from a serializer opened in mode: ' + this.mode)
    if (this.data.length < numBytes) throw Error('Cannot read ' + numBytes + ' bytes from a stream with only '+ this.data.length + ' bytes')
  }

  hasLeft(numBytes = 1) {
    return ((this.data.length - this.index) >= numBytes)
  }

  readByte() {
    this._checkRead()
    return this.data.charCodeAt(this.index++)
  }

  writeByte(b) {
    if (b < 0 || b > 0xFF) throw Error('Cannot write out-of-range byte ' + b)
    this.data += String.fromCharCode(b)
  }

  readInt() {
    var b1 = this.readByte() << 0
    var b2 = this.readByte() << 8
    var b3 = this.readByte() << 16
    var b4 = this.readByte() << 24
    return b1 | b2 | b3 | b4
  }

  writeInt(i) {
    if (i < 0 || i > 0xFFFFFFFF) throw Error('Cannot write out-of-range int ' + i)
    var b1 = (i & 0x000000FF) >> 0
    var b2 = (i & 0x0000FF00) >> 8
    var b3 = (i & 0x00FF0000) >> 16
    var b4 = (i & 0xFF000000) >> 24
    this.writeByte(b1)
    this.writeByte(b2)
    this.writeByte(b3)
    this.writeByte(b4)
  }

  readLong() {
    var i1 = this.readInt() << 32
    var i2 = this.readInt()
    return i1 | i2
  }

  writeLong(l) {
    if (l < 0 || l > 0xFFFFFFFFFFFFFFFF) throw Error('Cannot write out-of-range long ' + l)
    var i1 = l & 0xFFFFFFFF
    var i2 = (l - i1) / 0x100000000
    this.writeInt(i1)
    this.writeInt(i2)
  }

  readString() {
    var len = this.readInt()
    this._checkRead(len)
    var str = this.data.substr(this.index, len)
    this.index += len
    return str
  }

  writeString(s) {
    if (s == null) {
      this.writeInt(0)
      return
    }
    this.writeInt(s.length)
    this.data += s
  }

  readColor() {
    var r = this.readByte().toString()
    var g = this.readByte().toString()
    var b = this.readByte().toString()
    var a = this.readByte().toString()
    return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + a + ')'
  }

  writeColor(c) {
    // Adapted from https://gist.github.com/njvack/02ad8efcb0d552b0230d
    this.colorConverter.fillStyle = 'rgba(0, 0, 0, 0)' // Load a default in case we are passed garbage
    this.colorConverter.clearRect(0, 0, 1, 1)
    this.colorConverter.fillStyle = c
    this.colorConverter.fillRect(0, 0, 1, 1)
    var rgba = this.colorConverter.getImageData(0, 0, 1, 1).data
    this.writeByte(rgba[0])
    this.writeByte(rgba[1])
    this.writeByte(rgba[2])
    this.writeByte(rgba[3])
  }

  readCell() {
    var cellType = this.readByte()
    if (cellType === CELL_TYPE_NULL) return null

    var cell = {}
    cell.dir = null
    cell.line = 0
    if (cellType === CELL_TYPE_LINE) {
      cell.type = 'line'
      cell.line = this.readByte()
      var dot = this.readByte()
      if (dot != 0) cell.dot = dot
      var gap = this.readByte()
      if (gap != 0) cell.gap = gap
    } else if (cellType === CELL_TYPE_SQUARE) {
      cell.type = 'square'
      cell.color = this.readColor()
    } else if (cellType === CELL_TYPE_STAR) {
      cell.type = 'star'
      cell.color = this.readColor()
    } else if (cellType === CELL_TYPE_NEGA) {
      cell.type = 'nega'
      cell.color = this.readColor()
    } else if (cellType === CELL_TYPE_TRIANGLE) {
      cell.type = 'triangle'
      cell.color = this.readColor()
      cell.count = this.readByte()
    } else if (cellType === CELL_TYPE_POLY) {
      cell.type = 'poly'
      cell.color = this.readColor()
      cell.polyshape = this.readLong()
    } else if (cellType === CELL_TYPE_YLOP) {
      cell.type = 'ylop'
      cell.color = this.readColor()
      cell.polyshape = this.readLong()
    } else if (cellType == CELL_TYPE_NONCE) {
      cell.type = 'nonce'
    }

    var startEnd = this.readByte()
    if (startEnd & CELL_START)      cell.start = true
    if (startEnd & CELL_END_LEFT)   cell.end = 'left'
    if (startEnd & CELL_END_RIGHT)  cell.end = 'right'
    if (startEnd & CELL_END_TOP)    cell.end = 'top'
    if (startEnd & CELL_END_BOTTOM) cell.end = 'bottom'

    return cell
  }


  writeCell(cell) {
    if (cell == null) {
      this.writeByte(CELL_TYPE_NULL)
      return
    }

    // Write cell type, then cell data, then generic data.
    // Note that cell type starts at 1, since 0 is the "null type".
    if (cell.type == 'line') {
      this.writeByte(CELL_TYPE_LINE)
      this.writeByte(cell.line)
      this.writeByte(cell.dot)
      this.writeByte(cell.gap)
    } else if (cell.type == 'square') {
      this.writeByte(CELL_TYPE_SQUARE)
      this.writeColor(cell.color)
    } else if (cell.type == 'star') {
      this.writeByte(CELL_TYPE_STAR)
      this.writeColor(cell.color)
    } else if (cell.type == 'nega') {
      this.writeByte(CELL_TYPE_NEGA)
      this.writeColor(cell.color)
    } else if (cell.type == 'triangle') {
      this.writeByte(CELL_TYPE_TRIANGLE)
      this.writeColor(cell.color)
      this.writeByte(cell.count)
    } else if (cell.type == 'poly') {
      this.writeByte(CELL_TYPE_POLY)
      this.writeColor(cell.color)
      this.writeLong(cell.polyshape)
    } else if (cell.type == 'ylop') {
      this.writeByte(CELL_TYPE_YLOP)
      this.writeColor(cell.color)
      this.writeLong(cell.polyshape)
    }

    var startEnd = 0
    if (cell.start === true)  startEnd |= CELL_START
    if (cell.end == 'left')   startEnd |= CELL_END_LEFT
    if (cell.end == 'right')  startEnd |= CELL_END_RIGHT
    if (cell.end == 'top')    startEnd |= CELL_END_TOP
    if (cell.end == 'bottom') startEnd |= CELL_END_BOTTOM
    this.writeByte(startEnd)
  }
}

var CELL_TYPE_NULL     = 0
var CELL_TYPE_LINE     = 1
var CELL_TYPE_SQUARE   = 2
var CELL_TYPE_STAR     = 3
var CELL_TYPE_NEGA     = 4
var CELL_TYPE_TRIANGLE = 5
var CELL_TYPE_POLY     = 6
var CELL_TYPE_YLOP     = 7
var CELL_TYPE_NONCE    = 8

var CELL_START         = 1
var CELL_END_LEFT      = 2
var CELL_END_RIGHT     = 4
var CELL_END_TOP       = 8
var CELL_END_BOTTOM    = 16

var GENERIC_FLAG_AUTOSOLVED   = 1
var GENERIC_FLAG_SYMMETRICAL  = 2
var GENERIC_FLAG_SYMMETRY_X   = 4
var GENERIC_FLAG_SYMMETRY_Y   = 8
var GENERIC_FLAG_PILLAR       = 16

var SETTINGS_FLAG_NCN         = 1
var SETTINGS_FLAG_SZP         = 2
var SETTINGS_FLAG_PP          = 4
var SETTINGS_FLAG_FFE         = 8
var SETTINGS_FLAG_FS          = 16
var SETTINGS_FLAG_CM          = 32
var SETTINGS_FLAG_IS          = 64

})