# Loadsave

# Overview

Spyric Loadsave is a quick and simple way for saving & loading data to & from external files. The data is encoded using base64 (with a feint) and it is protected by SHA-256 hash, which makes your save data resilient against tampering.

Spyric Loadsave also maintains a backup of your local save files and will attempt to restore the main save file in the event that it has been removed, corrupted or tampered with.

# Gotchas

  • You must always use the same values for salt and pepper when loading and saving a specific file. The same pepper is used for all files in your app, but the salt should be different for each specific save file. However, once you save data to a file using certain values for salt and pepper, then you must use the same values again when loading the files, otherwise the file will fail to be authenticated and the data fails to be loaded.
  • If you wish to learn more about what salt and pepper are, please refer to these Wikipedia articles on salt and pepper.

# Setting up

Spyric Loadsave is a fully open-sourced plugin. In order to set it up, all you need to do is download it from GitHub, then place it in your project's root folder and require it.

local loadsave = require( "loadsave" )

# Functions & syntax

setPepper: set a pepper for all save files (optional, but highly encouraged).

loadsave.setPepper( pepper )
pepper
string. Any random password, passphrase or just random text, etc. that is used in the creation of the SHA-256 hash.

save: saves data to a local save file.

local didSave = loadsave.save( data, filename, salt [, directory] )
data
table/string. The data that you want to save. It can be either a Lua table or a string.
filename
string. The name of the save file.
salt
string. Any random password, passphrase or just random text, etc. that is used in the creation of the SHA-256 hash.
directory (optional)
constant. The base directory where the file will be saved to. Default is system.DocumentsDirectory.

load: loads data from a local save file.

local data = loadsave.load( filename, salt [, directory] )
filename
string. The name of the save file.
salt
string. Any random password, passphrase or just random text, etc. that is used in the creation of the SHA-256 hash.
directory (optional)
constant. The base directory where the file will be loaded from. Default is system.DocumentsDirectory.

# Examples

Saving and loading a string to/from a local save file.

local loadsave = require( "loadsave" )
loadsave.setPepper( "secret pa$$word" )

-- Create a message and save it:
local message = "Hello world!"
local didSave = loadsave.save( message, "myString.txt", "another secret key!" )
print( didSave ) -- output: true

-- Some time later, load it:
local newMessage = loadsave.load( "myString.txt", "another secret key!" )
print( newMessage ) -- output: "Hello world!"

Saving and loading a Lua table to/from a local save file.

local loadsave = require( "loadsave" )
loadsave.setPepper( "secret pa$$word" )

-- Create a message and save it:
local playerData = {
	userID = 1512,
	avatar = "myPhoto.jpg",
	currency = 2940,
	password = "p@ssW0rd"
}
local didSave = loadsave.save( playerData, "playerData.json", "a different key" )
print( didSave ) -- output: true

-- Some time later, load it:
local newData = loadsave.load( "playerData.json", "a different key" )
print( newData ) -- output: table: 0x950258