Class: Fable::CallStack::Thread

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arguments) ⇒ Thread

Returns a new instance of Thread.



255
256
257
258
259
260
261
262
263
# File 'lib/fable/call_stack.rb', line 255

def initialize(*arguments)
  self.previous_pointer = Pointer.null_pointer

  if arguments.size == 0
    self.call_stack = []
  else
    self.initialize_with_thread_object_and_story_context(arguments[0], arguments[1])
  end
end

Instance Attribute Details

#call_stackObject

Returns the value of attribute call_stack.



252
253
254
# File 'lib/fable/call_stack.rb', line 252

def call_stack
  @call_stack
end

#previous_pointerObject

Returns the value of attribute previous_pointer.



252
253
254
# File 'lib/fable/call_stack.rb', line 252

def previous_pointer
  @previous_pointer
end

#thread_indexObject

Returns the value of attribute thread_index.



252
253
254
# File 'lib/fable/call_stack.rb', line 252

def thread_index
  @thread_index
end

Instance Method Details

#copyObject



308
309
310
311
312
313
314
315
316
317
# File 'lib/fable/call_stack.rb', line 308

def copy
  copied_thread = self.class.new
  copied_thread.thread_index = thread_index
  self.call_stack.each do |element|
    copied_thread.call_stack << element.copy
  end

  copied_thread.previous_pointer = previous_pointer
  copied_thread
end

#initialize_with_thread_object_and_story_context(thread_object, story_context) ⇒ Object



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

def initialize_with_thread_object_and_story_context(thread_object, story_context)
  self.call_stack = []
  self.thread_index = thread_object["threadIndex"]

  thread_object["callstack"].each do |element|
    type = PushPopType::TYPE_LOOKUP[element["type"]]
    pointer = Pointer.null_pointer

    current_container_path_string = element["cPath"]

    if current_container_path_string
      thread_pointer_result = story_context.content_at_path(Path.new(current_container_path_string))
      pointer.container = thread_pointer_result.container
      pointer.index = element["idx"]

      if thread_pointer_result.object.nil?
        raise Error, "When loading state, internal story location couldn't be found: #{current_container_path_string}. Has the story changed since this save data was created?"
      elsif thread_pointer_result.approximate?
        story_context.warning("When loading state, internal story location couldn't be found: #{current_container_path_string}, so it wa approximated to #{pointer.container.path.to_s} to recover. Has the story changed since this save data was created?")
      end
    end

    in_expression_evaluation = element["exp"]

    new_element = Element.new(type, pointer, in_expression_evaluation: in_expression_evaluation)

    if element["temp"]
      new_element.temporary_variables = Serializer.convert_to_runtime_objects_hash(element["temp"])
    else
      new_element.temporary_variables = {}
    end

    self.call_stack << new_element
  end

  if thread_object["previousContentObject"]
    previous_path = Path.new(thread_object["previousContentObject"])
    self.previous_pointer = story_context.pointer_at_path(previous_path)
  end

  self
end

#to_hashObject



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

def to_hash
  export = {}

  export["callstack"] = []

  call_stack.each do |element|
    element_export = {}
    if !element.current_pointer.null_pointer?
      element_export["cPath"] = element.current_pointer.container.path.to_s
      element_export["idx"] = element.current_pointer.index
    end

    element_export["exp"] = element.in_expression_evaluation?
    element_export["type"] = PushPopType::TYPES[element.type]

    if element.temporary_variables.any?
      element_export["temp"] = Serializer.convert_hash_of_runtime_objects(element.temporary_variables)
    end

    export["callstack"] << element_export
  end

  export["threadIndex"] = thread_index

  if !previous_pointer.null_pointer?
    export["previousContentObject"] = self.previous_pointer.resolve!.path.to_s
  end

  export
end