Class: Fable::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/fable/serializer.rb

Class Method Summary collapse

Class Method Details

.array_to_container(array) ⇒ Object



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
# File 'lib/fable/serializer.rb', line 25

def self.array_to_container(array)
  contents = convert_to_runtime_objects(array, true)
  # Final object in the array is always a combination of
  # - named content
  # - an #f key with the count flags
  final_object = array.last
  bit_flags = 0
  container_name = nil
  named_only_content = {}

  if !final_object.nil?
    final_object.each do |key, value|
      if key == "#f"
        bit_flags = value.to_i
      elsif key == "#n"
        container_name = value
      else
        named_content_item = convert_to_runtime_object(value)
        if named_content_item.is_a?(Container)
          named_content_item.name = key
        end

        named_only_content[key] = named_content_item
      end
    end
  end

  container = Container.new(bit_flags)
  container.add_content(contents)
  if !container_name.nil?
    container.name = container_name
  end

  named_only_content.each do |key, content|
    container.add_to_named_content(content)
  end

  return container
end

.convert_array_of_runtime_objects(array) ⇒ Object



338
339
340
# File 'lib/fable/serializer.rb', line 338

def self.convert_array_of_runtime_objects(array)
  array.map{|x| convert_from_runtime_object(x) }
end

.convert_choice(choice) ⇒ Object



546
547
548
549
550
551
552
553
554
# File 'lib/fable/serializer.rb', line 546

def self.convert_choice(choice)
  return {
    "text" => choice.text,
    "index" => choice.index,
    "originalChoicePath" => choice.source_path,
    "originalThreadIndex" => choice.original_thread_index,
    "targetPath" => choice.path_string_on_choice
  }
end

.convert_choices(choices) ⇒ Object



556
557
558
# File 'lib/fable/serializer.rb', line 556

def self.convert_choices(choices)
  choices.map{|choice| convert_choice(choice) }
end

.convert_control_command(control_command) ⇒ Object



496
497
498
# File 'lib/fable/serializer.rb', line 496

def self.convert_control_command(control_command)
  return control_command.command_type
end

.convert_divert_target_value(divert_target_value) ⇒ Object



481
482
483
# File 'lib/fable/serializer.rb', line 481

def self.convert_divert_target_value(divert_target_value)
  return {"^->" => divert_target_value.value.components_string }
end

.convert_from_choice_point(choice_point) ⇒ Object



447
448
449
450
451
452
# File 'lib/fable/serializer.rb', line 447

def self.convert_from_choice_point(choice_point)
  return {
    "*" => choice_point.path_string_on_choice,
    "flg" => choice_point.flags
  }
end

.convert_from_divert(object) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/fable/serializer.rb', line 410

def self.convert_from_divert(object)
  result = {}
  divert_type_key = "->"

  if divert.external?
    divert_type_key = "x()"
  elsif divert.pushes_to_stack?
    if divert.stack_push_type == PushPopType::TYPES[:function]
      divert_type_key = "f()"
    else
      divert_type_key = "->t->"
    end
  end

  if divert.has_variable_target?
    target_string = divert.variable_divert_name
  else
    target_string = divert.target_path_string
  end

  result[divert_type_key] = target_string

  if divert.has_variable_target?
    result["var"] = true
  end

  if divert.is_conditional?
    result["c"] = true
  end

  if divert.external_arguments > 0
    result["exArgs"] = divert.external_arguments
  end

  return result
end

.convert_from_list(list_value) ⇒ Object



462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/fable/serializer.rb', line 462

def self.convert_from_list(list_value)
  result = {}
  if list_value.is_a?(ListValue)
    raw_list = list_value.value
  else
    raw_list = list_value
  end

  raw_list.list.each do |list_item, value|
    result[list_item.full_name] = value
  end

  if raw_list.list.empty? && !raw_list.origin_names.nil? && !raw_list.origin_names.empty?
    result["origins"] = raw_list.origin_names
  end

  return {"list" => result}
end

.convert_from_runtime_object(object) ⇒ Object

Raises:

  • (ArgumentError)


342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/fable/serializer.rb', line 342

def self.convert_from_runtime_object(object)
  if object.is_a?(Container)
    return convert_from_container(object)
  end

  if object.is_a?(Divert)
    return convert_from_divert(object)
  end

  if object.is_a?(ChoicePoint)
    return convert_from_choice_point(object)
  end

  if object.is_a?(IntValue) || object.is_a?(FloatValue)
    return object.value
  end

  if object.is_a?(StringValue)
    return convert_from_string_value(object)
  end

  if object.is_a?(ListValue) || object.is_a?(InkList)
    return convert_from_list(object)
  end

  if object.is_a?(DivertTargetValue)
    return convert_divert_target_value(object)
  end

  if object.is_a?(VariablePointerValue)
    return convert_variable_pointer_value(object)
  end

  if object.is_a?(Glue)
    return convert_glue(object)
  end

  if object.is_a?(ControlCommand)
    return convert_control_command(object)
  end

  if object.is_a?(NativeFunctionCall)
    return convert_native_function_call(object)
  end

  if object.is_a?(VariableReference)
    return convert_variable_reference(object)
  end

  if object.is_a?(VariableAssignment)
    return convert_variable_assignment(object)
  end

  if object.is_a?(Void)
    return convert_void
  end

  if object.is_a?(Tag)
    return convert_tag(object)
  end

  if object.is_a?(Choice)
    return convert_choice(object)
  end

  raise ArgumentError, "Failed to serialize runtime object: #{object}"
end

.convert_from_string_value(string_value) ⇒ Object



454
455
456
457
458
459
460
# File 'lib/fable/serializer.rb', line 454

def self.convert_from_string_value(string_value)
  if string_value.is_newline?
    return "\n"
  else
    return "^#{string_value.value}"
  end
end

.convert_glue(glue) ⇒ Object



492
493
494
# File 'lib/fable/serializer.rb', line 492

def self.convert_glue(glue)
  return "<>"
end

.convert_hash_of_runtime_objects(hash_value) ⇒ Object



329
330
331
332
333
334
335
336
# File 'lib/fable/serializer.rb', line 329

def self.convert_hash_of_runtime_objects(hash_value)
  result = {}
  hash_value.each do |key, value|
    result[key] = convert_from_runtime_object(value)
  end

  result
end

.convert_native_function_call(native_function_call) ⇒ Object



500
501
502
503
504
505
506
507
# File 'lib/fable/serializer.rb', line 500

def self.convert_native_function_call(native_function_call)
  function_name = native_function_call.name
  if name == "^"
    return "L^"
  else
    return name
  end
end

.convert_tag(tag) ⇒ Object



540
541
542
543
544
# File 'lib/fable/serializer.rb', line 540

def self.convert_tag(tag)
  return {
    "#" => tag.text
  }
end

.convert_to_list_definitions(object) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/fable/serializer.rb', line 314

def self.convert_to_list_definitions(object)
  all_definitions = []

  object.each do |name, list_definition_hash|
    items = {}
    list_definition_hash.each do |key, value|
      items[key] = value.to_i
    end

    all_definitions << ListDefinition.new(name, items)
  end

  return ListDefinitionsOrigin.new(all_definitions)
end

.convert_to_runtime_object(object) ⇒ Object


JSON ENCODING SCHEME


Glue: “<>”, “G<”, “G>”

ControlCommand: “ev”, “out”, “/ev”, “du” “pop”, “->->”, “~ret”, “str”, “/str”, “nop”,

"choiceCnt", "turns", "visit", "seq", "thread", "done", "end"

NativeFunction: “+”, “-”, “/”, “*”, “%” “~”, “==”, “>”, “<”, “>=”, “<=”, “!=”, “!”… etc

Void: “void”

Value: “^string value”, “^^string value beginning with ^”

5, 5.2
{"^->": "path.target"}
{"^var": "varname", "ci": 0}

Container: […]

[...,
    {
        "subContainerName": ...,
        "#f": 5,                    # flags
        "#n": "containerOwnName"    # only if not redundant
    }
]

Divert: {“->”: “path.target”, “c”: true }

{"->": "path.target", "var": true}
{"f()": "path.func"}
{"->t->": "path.tunnel"}
{"x()": "externalFuncName", "exArgs": 5}

Var Assign: “varName”, “re”: true # reassignment

{"temp=": "varName"}

Var ref: “varName”

{"CNT?": "stitch name"}

ChoicePoint: {“*”: pathString,

"flg": 18 }

Choice: Nothing too clever, it’s only used in the save state,

there's not likely to be many of them.

Tag: “the tag text”

Raises:



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
# File 'lib/fable/serializer.rb', line 112

def self.convert_to_runtime_object(object)
  case object
  when Numeric
    return Value.create(object)
  when String
    string = object

    # StringValue
    if string.start_with?("^")
      return StringValue.new(string[1..-1])
    elsif string.start_with?("\n") && string.length == 1
      return StringValue.new("\n")
    end

    # Glue
    if string == "<>"
      return Glue.new
    end

    # Control Commands
    if ControlCommand.is_control_command?(string)
      return ControlCommand.get_control_command(string)
    end

    # Native functions
    # "^" conflicts with the way we identify strings, so now
    # we know it's not a string, we can convert back to the proper symbol
    # for this operator
    if string == "L^"
      string = "^"
    end

    if NativeFunctionCall.is_native_function?(string)
      return NativeFunctionCall.new(string)
    end

    # Pop
    if string == ControlCommand::COMMANDS[:POP_TUNNEL]
      return ControlCommand.get_control_command(string)
    elsif string == ControlCommand::COMMANDS[:POP_FUNCTION]
      return ControlCommand.get_control_command(string)
    end

    if string == "void"
      return Void.new
    end
  when Hash
    given_hash = object

    # Divert target value to path
    if given_hash["^->"]
      return DivertTargetValue.new(Path.new(given_hash["^->"]))
    end

    # VariablePointerType
    if given_hash["^var"]
      variable_pointer = VariablePointerValue.new(given_hash["^var"])
      if given_hash["ci"]
        variable_pointer.context_index = given_hash["ci"]
      end

      return variable_pointer
    end

    # Divert
    is_divert = false
    pushes_to_stack = false
    divert_push_pop_type = PushPopType::TYPES[:function]
    external = false
    value = nil

    if given_hash["->"]
      is_divert = true
      value = given_hash["->"]
    elsif given_hash["f()"]
      is_divert = true
      pushes_to_stack = true
      divert_push_pop_type = PushPopType::TYPES[:function]
      value = given_hash["f()"]
    elsif given_hash["->t->"]
      is_divert = true
      pushes_to_stack = true
      divert_push_pop_type = PushPopType::TYPES[:tunnel]
      value = given_hash["->t->"]
    elsif given_hash["x()"]
      is_divert = true
      external = true
      pushes_to_stack = false
      divert_push_pop_type = PushPopType::TYPES[:function]
      value = given_hash["x()"]
    end

    if is_divert
      divert = Divert.new
      divert.pushes_to_stack = pushes_to_stack
      divert.stack_push_type = divert_push_pop_type
      divert.is_external = external
      target = value.to_s

      if given_hash["var"]
        divert.variable_divert_name = target
      else
        divert.target_path_string = target
      end

      divert.is_conditional = given_hash.has_key?("c")

      if external
        if given_hash["exArgs"]
          divert.external_arguments = given_hash["exArgs"]
        end
      end

      return divert
    end

    # Choice
    if given_hash["*"]
      choice = ChoicePoint.new
      choice.path_string_on_choice = given_hash["*"]

      if given_hash["flg"]
        choice.flags = given_hash["flg"]
      end

      return choice
    end

    # Variable Reference
    if given_hash["VAR?"]
      return VariableReference.new(given_hash["VAR?"])
    elsif given_hash["CNT?"]
      read_count_variable_reference = VariableReference.new(nil)
      read_count_variable_reference.path_string_for_count = given_hash["CNT?"]
      return read_count_variable_reference
    end

    # Variable Assignment
    is_variable_assignment = false
    is_global_variable = false

    if given_hash["VAR="]
      variable_name = given_hash["VAR="]
      is_variable_assignment = true
      is_global_variable = true
    elsif given_hash["temp="]
      variable_name = given_hash["temp="]
      is_variable_assignment = true
      is_global_variable = false
    end

    if is_variable_assignment
      is_new_declaration = !given_hash.has_key?("re")
      variable_assignment = VariableAssignment.new(variable_name, is_new_declaration)
      variable_assignment.global = is_global_variable
      return variable_assignment
    end

    # Tag
    if given_hash["#"]
      return Tag.new(given_hash["#"])
    end

    # List
    if given_hash["list"]
      list_content = given_hash["list"]
      raw_list = InkList.new
      if given_hash["origins"]
        raw_list.set_initial_origin_names(given_hash["origins"])
      end

      list_content.each do |key, value|
        item = InkList::InkListItem.new(full_name: key)
        raw_list.list[item] = value.to_i
      end

      return ListValue.new(raw_list)
    end

    # Used when serializing save state only
    if given_hash["originalChoicePath"]
      return object_to_choice(object)
    end
  when Array
    return array_to_container(object)
  when NilClass
    return nil
  end

  raise Error, "Failed to convert to runtime object: #{object}"
end

.convert_to_runtime_objects(objects, skip_last = false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/fable/serializer.rb', line 3

def self.convert_to_runtime_objects(objects, skip_last = false)
  last_item = objects.last
  runtime_objects = []

  objects.each do |object|
    next if object == last_item && skip_last
    runtime_objects << convert_to_runtime_object(object)
  end

  runtime_objects
end

.convert_to_runtime_objects_hash(hash) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/fable/serializer.rb', line 15

def self.convert_to_runtime_objects_hash(hash)
  runtime_hash = {}

  hash.each do |key, value|
    runtime_hash[key] = convert_to_runtime_object(value)
  end

  return runtime_hash
end

.convert_variable_assignment(variable_assignment) ⇒ Object



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/fable/serializer.rb', line 518

def self.convert_variable_assignment(variable_assignment)
  result = {}

  if variable_assignment.global?
    key = "VAR="
  else
    key = "temp="
  end

  result[key] = variable_assignment.variable_name

  if !variable_assignment.new_declaration?
    result["re"] = true
  end

  return result
end

.convert_variable_pointer_value(variable_pointer_value) ⇒ Object



485
486
487
488
489
490
# File 'lib/fable/serializer.rb', line 485

def self.convert_variable_pointer_value(variable_pointer_value)
  return {
    "^var" => variable_pointer_value.value,
    "ci" => variable_pointer_value.context_index
  }
end

.convert_variable_reference(variable_reference) ⇒ Object



509
510
511
512
513
514
515
516
# File 'lib/fable/serializer.rb', line 509

def self.convert_variable_reference(variable_reference)
  read_count_path = variable_reference.path_string_for_count
  if read_count_path.nil?
    return {"CNT?" => read_count_path}
  else
    return {"VAR?" => variable_reference.name}
  end
end

.convert_voidObject



536
537
538
# File 'lib/fable/serializer.rb', line 536

def self.convert_void
  return "void"
end

.object_to_choice(object) ⇒ Object



304
305
306
307
308
309
310
311
312
# File 'lib/fable/serializer.rb', line 304

def self.object_to_choice(object)
  choice = Choice.new
  choice.text = object["text"]
  choice.index = object["index"].to_i
  choice.source_path = object["originalChoicePath"]
  choice.original_thread_index = object["originalThreadIndex"].to_i
  choice.path_string_on_choice = object["targetPath"]
  return choice
end