/ld27

To get this branch, use:
bzr branch http://9ix.org/bzr/ld27

« back to all changes in this revision

Viewing changes to zoetrope/utils/storage.lua

  • Committer: Josh C
  • Date: 2013-08-25 00:16:36 UTC
  • Revision ID: josh@9ix.org-20130825001636-xoivc9byo1mdx0fu
1 goal in each maze

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
        --              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
 
51
        --              none
54
52
        --
55
53
        -- Returns:
56
54
        --              nothing
57
55
 
58
 
        save = function (self, values, ignoreError)
 
56
        save = function (self, ignoreError)
59
57
                if ignoreError ~= false then ignoreError = true end
60
58
 
61
 
                if values then
62
 
                        for key, value in pairs(values) do
63
 
                                self.data[key] = value
64
 
                        end
65
 
                end
66
 
 
67
59
                local ok, message = pcall(love.filesystem.write, self.filename, dump(self.data))
68
60
 
69
61
                if not ok and not ignoreError then
75
67
        -- Loads data from disk.
76
68
        --
77
69
        -- Arguments:
78
 
        --              ignoreError - silently ignore any errors loading, defaults to true
 
70
        --              ignoreError - silently ignore any errors loading, default to true
79
71
        --
80
72
        -- Returns:
81
73
        --              whether loading actually worked, boolean
83
75
        load = function (self, ignoreError)
84
76
                if ignoreError ~= false then ignoreError = true end
85
77
 
86
 
                local ok = love.filesystem.isFile(self.filename)
 
78
                local ok, data = pcall(love.filesystem.read, self.filename)
87
79
 
88
80
                if ok then
89
 
                        ok, self.data = pcall(loadstring('return ' .. love.filesystem.read(self.filename)))
90
 
 
 
81
                        ok, self.data = pcall(loadstring('return ' .. data))
 
82
                        
91
83
                        if not ok then
92
84
                                if ignoreError then
93
85
                                        self.data = {}
97
89
                        end
98
90
                else
99
91
                        if not ignoreError then
100
 
                                error("could not load storage from disk: " .. love.filesystem.read(self.filename))
 
92
                                error("could not load storage from disk: " .. data)
101
93
                        end
102
94
                end
103
95