/ld27

To get this branch, use:
bzr branch /bzr/ld27

« back to all changes in this revision

Viewing changes to zoetrope/utils/storage.lua

  • Committer: Josh C
  • Date: 2014-07-03 14:41:57 UTC
  • Revision ID: josh@9ix.org-20140703144157-xydxt62xzcdq6bdk
cluke009 zoetrope + my spritebatch changes

Show diffs side-by-side

added added

removed removed

11
11
-- retains your data across a single app session. If you want to be notified
12
12
-- when errors occur, check the <save()> and <load()> methods' arguments.
13
13
--
 
14
-- Make sure to set your app's identity in conf.lua, so that your
14
15
-- files don't clobber some other app's. See https://love2d.org/wiki/Config_Files
15
16
-- for details.
16
17
--
48
48
        -- Saves data to disk.
49
49
        --
50
50
        -- Arguments:
51
 
        --              none
 
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
52
54
        --
53
55
        -- Returns:
54
56
        --              nothing
55
57
 
56
 
        save = function (self, ignoreError)
 
58
        save = function (self, values, ignoreError)
57
59
                if ignoreError ~= false then ignoreError = true end
58
60
 
 
61
                if values then
 
62
                        for key, value in pairs(values) do
 
63
                                self.data[key] = value
 
64
                        end
 
65
                end
 
66
 
59
67
                local ok, message = pcall(love.filesystem.write, self.filename, dump(self.data))
60
68
 
61
69
                if not ok and not ignoreError then
67
75
        -- Loads data from disk.
68
76
        --
69
77
        -- Arguments:
70
 
        --              ignoreError - silently ignore any errors loading, default to true
 
78
        --              ignoreError - silently ignore any errors loading, defaults to true
71
79
        --
72
80
        -- Returns:
73
81
        --              whether loading actually worked, boolean
75
83
        load = function (self, ignoreError)
76
84
                if ignoreError ~= false then ignoreError = true end
77
85
 
78
 
                local ok, data = pcall(love.filesystem.read, self.filename)
 
86
                local ok = love.filesystem.isFile(self.filename)
79
87
 
80
88
                if ok then
81
 
                        ok, self.data = pcall(loadstring('return ' .. data))
82
 
                        
 
89
                        ok, self.data = pcall(loadstring('return ' .. love.filesystem.read(self.filename)))
 
90
 
83
91
                        if not ok then
84
92
                                if ignoreError then
85
93
                                        self.data = {}
89
97
                        end
90
98
                else
91
99
                        if not ignoreError then
92
 
                                error("could not load storage from disk: " .. data)
 
100
                                error("could not load storage from disk: " .. love.filesystem.read(self.filename))
93
101
                        end
94
102
                end
95
103