class Fable::CallStack::Thread

Attributes

call_stack[RW]
previous_pointer[RW]
thread_index[RW]

Public Class Methods

new(*arguments) click to toggle source
# 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

Public Instance Methods

copy() click to toggle source
# 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) click to toggle source
# 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_hash() click to toggle source
# 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