/ld27

To get this branch, use:
bzr branch http://9ix.org/bzr/ld27
1 by Josh C
zoetrope 1.4
1
-- Class: Storage
2
-- Allows your app to save data between sessions -- high scores or game
3
-- progress, for example. A single storage instance corresponds to a
4
-- file on disk. If you'd like to maintain individual files (e.g. so
5
-- a user can email a save game to a friend), you'll need to create a
6
-- separate storage instance for each one.
7
--
8
-- Your data is saved on disk in Lua format. This means that if someone
9
-- figures out where your data files are saved, it is very trivial for them
10
-- to change the data. If saving or loading fails, then this class still
11
-- retains your data across a single app session. If you want to be notified
12
-- when errors occur, check the <save()> and <load()> methods' arguments.
13
--
35 by Josh C
cluke009 zoetrope + my spritebatch changes
14
-- Make sure to set your app's identity in conf.lua, so that your
1 by Josh C
zoetrope 1.4
15
-- files don't clobber some other app's. See https://love2d.org/wiki/Config_Files
16
-- for details.
17
--
18
--
19
-- Extends:
20
--		<Class>
21
22
Storage = Class:extend{
23
	-- Property: data
24
	-- Use this property to store whatever data you like. You can
25
	-- nest tables inside this.
26
	data = {},
27
28
	-- Property: filename
29
	-- What filename to store the data on disk under. See
30
	-- https://love2d.org/wiki/love.filesystem for where exactly this
31
	-- will be saved. Make sure to set this if your app is using
32
	-- multiple storage objects -- otherwise they will overwrite
33
	-- each other.
34
35
	filename = 'storage.dat',
36
37
	new = function (self, obj)
38
		obj = obj or {}
39
		self:extend(obj)
40
41
		if obj.filename then obj:load() end
42
43
		if obj.onNew then obj:onNew() end
44
		return obj
45
	end,
46
47
	-- Method: save
48
	-- Saves data to disk.
49
	--
50
	-- Arguments:
35 by Josh C
cluke009 zoetrope + my spritebatch changes
51
	--		values - table of values to set before saving, optional -- these
52
	--		act in addition to anything previously set in the <data>
53
	--		ignoreError - silently ignore any errors saving, defaults to true
1 by Josh C
zoetrope 1.4
54
	--
55
	-- Returns:
56
	--		nothing
57
35 by Josh C
cluke009 zoetrope + my spritebatch changes
58
	save = function (self, values, ignoreError)
1 by Josh C
zoetrope 1.4
59
		if ignoreError ~= false then ignoreError = true end
60
35 by Josh C
cluke009 zoetrope + my spritebatch changes
61
		if values then
62
			for key, value in pairs(values) do
63
				self.data[key] = value
64
			end
65
		end
66
1 by Josh C
zoetrope 1.4
67
		local ok, message = pcall(love.filesystem.write, self.filename, dump(self.data))
68
69
		if not ok and not ignoreError then
70
			error("could not save storage from disk: " .. message)
71
		end
72
	end,
73
74
	-- Method: load
75
	-- Loads data from disk.
76
	--
77
	-- Arguments:
35 by Josh C
cluke009 zoetrope + my spritebatch changes
78
	--		ignoreError - silently ignore any errors loading, defaults to true
1 by Josh C
zoetrope 1.4
79
	--
80
	-- Returns:
81
	--		whether loading actually worked, boolean
82
83
	load = function (self, ignoreError)
84
		if ignoreError ~= false then ignoreError = true end
85
35 by Josh C
cluke009 zoetrope + my spritebatch changes
86
		local ok = love.filesystem.isFile(self.filename)
1 by Josh C
zoetrope 1.4
87
88
		if ok then
35 by Josh C
cluke009 zoetrope + my spritebatch changes
89
			ok, self.data = pcall(loadstring('return ' .. love.filesystem.read(self.filename)))
90
1 by Josh C
zoetrope 1.4
91
			if not ok then
92
				if ignoreError then
93
					self.data = {}
94
				else
95
					error("could not deserialize storage data: " .. self.data)
96
				end
97
			end
98
		else
99
			if not ignoreError then
35 by Josh C
cluke009 zoetrope + my spritebatch changes
100
				error("could not load storage from disk: " .. love.filesystem.read(self.filename))
1 by Josh C
zoetrope 1.4
101
			end
102
		end
103
104
		return ok
105
	end
106
}