Sha256: 1ead602f893da8ccba89c77b29ed43e558a4658631306c00fa3c54343eef97fb

Contents?: true

Size: 1.14 KB

Versions: 7

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'corelib/marshal' if RUBY_ENGINE == 'opal' # Required in browser
require 'base64'

module Gamefic
  # Save and restore plots.
  #
  module Snapshot
    # Save a base64-encoded snapshot of a plot.
    #
    # @param plot [Plot]
    # @return [String]
    def self.save plot
      cache = plot.detach
      binary = Marshal.dump(plot)
      plot.attach cache
      Base64.encode64(binary)
    end

    # Restore a plot from a base64-encoded string.
    #
    # @param snapshot [String]
    # @return [Plot]
    def self.restore snapshot
      binary = Base64.decode64(snapshot)
      Marshal.load(binary).tap do |plot|
        plot.hydrate
        # @todo Opal marshal dumps are not idempotent
        next if RUBY_ENGINE == 'opal' || match?(plot, snapshot)

        Logging.logger.warn "Scripts modified #{plot.class} data. Snapshot may not have restored properly"
      end
    end

    # True if the plot's state matches the snapshot.
    #
    # @param plot [Plot]
    # @param snapshot [String]
    def self.match?(plot, snapshot)
      save(plot) == snapshot
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gamefic-3.6.0 lib/gamefic/snapshot.rb
gamefic-3.5.0 lib/gamefic/snapshot.rb
gamefic-3.4.0 lib/gamefic/snapshot.rb
gamefic-3.3.0 lib/gamefic/snapshot.rb
gamefic-3.2.1 lib/gamefic/snapshot.rb
gamefic-3.2.0 lib/gamefic/snapshot.rb
gamefic-3.1.0 lib/gamefic/snapshot.rb