Stop Files From Editing When App Is Updating

I need help i just realised a problem i have and i dunno how to solve it. I have a game app which saves scores to a leaderboard and reads them via .json. Now the problem comes when I go to update the app and they have to update how would I stop it clearing the scores they have put on ?

Assuming you’re using the functions to load and save a JSON table to a file, this is what I am doing:

local function saveTable( t, filename ) local path = system.pathForFile( filename, system.DocumentsDirectory ) local file = io.open( path, "w" ) if file then local contents = json.encode( t ) file:write( contents ) io.close( file ) return true else return false end end local function loadTable( filename ) local path = system.pathForFile( filename, system.DocumentsDirectory ) local contents, t = "", {} local file = io.open( path, "r" ) if file then local contents = file:read( "\*a" ) t = json.decode( contents ) io.close( file ) return t end return nil end ... local highscores = loadTable( "highscores.txt" ) or {} if highscores == nil then --- no previously saved scores, create a new one for i = 1, 10 do highscores[i] = 0 end else --- a high score table was loaded! end

 

Basically, with the line local highscores = loadTable( “highscores.txt” ) or {}, it will attempt to load a high score table, and if there is none, the function will return with nil.  But since you put an or {} , at least it will fall back to an empty table.

The idea is similar to my own. I have my files stored in the system.ResourceDirectory ? Would it make a difference if the files where stored in the system.DocumentsDirectory. The fact being if someone has a high score on their phone and I send out a update I dont want that score to get reset.

You cannot write to the system.ResourceDirectory, as it is where your code and assets are located, and is protected by iOS.  If you’re saving a file, it’s likely that you’re already placing it in system.DocumentsDirectory, which is a “permanent” sandboxed read/write area for your app.  If a user downloads an app update from the App Store, that sandboxed area stays intact, and everything in the system.ResourceDirectory is overwritten, technically.

Would i be able to then do something like this ?

 

local jsonFile = function( filename )
    
    local path = system.pathForFile( filename, system.DocumentsDirectory )
    local contents
    
    local file = io.open( path, “r” )
    
    if file then
        contents = file:read( “*a” )
        io.close( file )
        return contents
    end
    
    return “nil”
    
end local function decode( jsonName )     oldTable = {}
    tryOldTable = json.decode( jsonFile( jsonName ) )
    
    return tryOldTable
    
end local saveFile = function( filename )
    
    local path = system.pathForFile( filename, system.ResourceDirectory )
    
    local file = io.open( path, “w” )
    
    if file then
        file:write( json.encode( newTable ) )
        
        file:close()
        return true
    else
        return false
    end
    
end local jsonFileNew = function( filename )
    
    local path = system.pathForFile( filename, system.ResourceDirectory )
    local contents
    
    local file = io.open( path, “r” )
    
    if file then
        contents = file:read( “*a” )
        io.close( file )
        return contents
    end
    
    return false
    
end local function decodeNew( jsonName )     newTable = {}
    newTable.position = {}
    tryNewTable = json.decode( jsonFileNew( jsonName ) )
    
    for i,pos in pairs( leaderTable.position ) do
        newTable.position[tonumber(i)] = pos
    end
    
    saveFile( jsonName )
    
end local easyScores = decode( “easyGame.json” ) or {} if easyScores == “nil” then    
    decodeNew( “easyGame.json” )    
else
    — a high score table was loaded!
end

 

 

 

I want the saveFile to then save the open file in the system.DocumentsDirectory.

Assuming you’re using the functions to load and save a JSON table to a file, this is what I am doing:

local function saveTable( t, filename ) local path = system.pathForFile( filename, system.DocumentsDirectory ) local file = io.open( path, "w" ) if file then local contents = json.encode( t ) file:write( contents ) io.close( file ) return true else return false end end local function loadTable( filename ) local path = system.pathForFile( filename, system.DocumentsDirectory ) local contents, t = "", {} local file = io.open( path, "r" ) if file then local contents = file:read( "\*a" ) t = json.decode( contents ) io.close( file ) return t end return nil end ... local highscores = loadTable( "highscores.txt" ) or {} if highscores == nil then --- no previously saved scores, create a new one for i = 1, 10 do highscores[i] = 0 end else --- a high score table was loaded! end

 

Basically, with the line local highscores = loadTable( “highscores.txt” ) or {}, it will attempt to load a high score table, and if there is none, the function will return with nil.  But since you put an or {} , at least it will fall back to an empty table.

The idea is similar to my own. I have my files stored in the system.ResourceDirectory ? Would it make a difference if the files where stored in the system.DocumentsDirectory. The fact being if someone has a high score on their phone and I send out a update I dont want that score to get reset.

You cannot write to the system.ResourceDirectory, as it is where your code and assets are located, and is protected by iOS.  If you’re saving a file, it’s likely that you’re already placing it in system.DocumentsDirectory, which is a “permanent” sandboxed read/write area for your app.  If a user downloads an app update from the App Store, that sandboxed area stays intact, and everything in the system.ResourceDirectory is overwritten, technically.

Would i be able to then do something like this ?

 

local jsonFile = function( filename )
    
    local path = system.pathForFile( filename, system.DocumentsDirectory )
    local contents
    
    local file = io.open( path, “r” )
    
    if file then
        contents = file:read( “*a” )
        io.close( file )
        return contents
    end
    
    return “nil”
    
end local function decode( jsonName )     oldTable = {}
    tryOldTable = json.decode( jsonFile( jsonName ) )
    
    return tryOldTable
    
end local saveFile = function( filename )
    
    local path = system.pathForFile( filename, system.ResourceDirectory )
    
    local file = io.open( path, “w” )
    
    if file then
        file:write( json.encode( newTable ) )
        
        file:close()
        return true
    else
        return false
    end
    
end local jsonFileNew = function( filename )
    
    local path = system.pathForFile( filename, system.ResourceDirectory )
    local contents
    
    local file = io.open( path, “r” )
    
    if file then
        contents = file:read( “*a” )
        io.close( file )
        return contents
    end
    
    return false
    
end local function decodeNew( jsonName )     newTable = {}
    newTable.position = {}
    tryNewTable = json.decode( jsonFileNew( jsonName ) )
    
    for i,pos in pairs( leaderTable.position ) do
        newTable.position[tonumber(i)] = pos
    end
    
    saveFile( jsonName )
    
end local easyScores = decode( “easyGame.json” ) or {} if easyScores == “nil” then    
    decodeNew( “easyGame.json” )    
else
    — a high score table was loaded!
end

 

 

 

I want the saveFile to then save the open file in the system.DocumentsDirectory.