Sha256: f1da5c35308aabb2ecbb0af2d869c068a19cc06813f05c22850f7cd34edcc991

Contents?: true

Size: 976 Bytes

Versions: 1

Compression:

Stored size: 976 Bytes

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' || Snapshot.save(plot) == snapshot

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gamefic-3.0.0 lib/gamefic/snapshot.rb