table.save and/or table.load not working for me anymore

Post Reply
Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

table.save and/or table.load not working for me anymore

Post by Caled »

Code: Select all

--- Skirmish
skirmishTargets={}
sTar="notarget"


function skTar_save()
	table.save("skTable", skirmishTargets)
end

function skTar_load()
	table.load("skTable", skirmishTargets)
end


function skTar_add(n)
	n = n:gsub("^%l", string.upper) -- capitalize the target
	table.insert(skirmishTargets, n)
	skTar_show(skirmishTargets)
end

function skTar_show()
	for k, v in pairs(skirmishTargets) do
		echo(v .. ", ")
	end
	echo("\n")
end


function skTar_clear()
	skirmishTargets={}
end
This worked for a few days. On startup I just had to run skTar_load() and my list of enemy targets would be restored.

Today, it did not work. I assumed that I must have accidentally cleared then saved it last night, so I put all the names back in, ran:
skTar_save()
skTar_show()
skTar_clear()
skTar_show()

No names. For some reason, I can no longer save this list with table.save and restore it. Halp :?

Thylacine
Posts: 28
Joined: Sun May 10, 2009 5:04 am

Re: table.save and/or table.load not working for me anymore

Post by Thylacine »

Caled,

I had the same problem a while ago, try specifying an absolute path for the files (eg: "/home/usr/.config/Mudlet") and/or saving the following as a script (it's just the save/load functions in LuaGlobal.lua):

Code: Select all

function table.save( sfile, t )
 if t == nil then t = _G end
 local tables = {}
 table.insert( tables, t )
 local lookup = { [t] = 1 }
 local file = io.open( sfile, "w" )
 file:write( "return {" )
 for i,v in ipairs( tables ) do
  table.pickle( v, file, tables, lookup )
 end
 file:write( "}" )
 file:close()
end
 
function table.pickle( t, file, tables, lookup )
 file:write( "{" )
 for i,v in pairs( t ) do
  -- escape functions
  if type( v ) ~= "function" and type( v ) ~= "userdata" and (i ~= "string" and i ~= "xpcall" and i ~= "package" and i ~= "os" and i ~= "io" and i ~= "math" and i ~= "debug" and i ~= "coroutine" and i ~= "_G" and i ~= "_VERSION" and i ~= "table") then
   -- handle index
   if type( i ) == "table" then
    if not lookup[i] then
     table.insert( tables, i )
     lookup[i] = table.maxn( tables )
    end
    file:write( "[{"..lookup[i].."}] = " )
   else
    local index = ( type( i ) == "string" and "[ "..string.enclose( i, 50 ).." ]" ) or string.format( "[%d]", i )
    file:write( index.." = " )
   end
   -- handle value
   if type( v ) == "table" then
    if not lookup[v] then
     table.insert( tables, v )
     lookup[v] = table.maxn( tables )
    end
    file:write( "{"..lookup[v].."}," )
   else
    local value =  ( type( v ) == "string" and string.enclose( v, 50 ) ) or tostring( v )
    file:write( value.."," )
   end
  end
 end
 file:write( "},\n" )  
end
 
-- enclose string by long brakets ( string, maxlevel )
function string.enclose( s, maxlevel )
 s = "["..s.."]"
 local level = 0
 while 1 do
  if maxlevel and level == maxlevel then
   error( "error: maxlevel too low, "..maxlevel )
  -- 
  elseif string.find( s, "%["..string.rep( "=", level ).."%[" ) or string.find( s, "]"..string.rep( "=", level ).."]" ) then
   level = level + 1
  else
   return "["..string.rep( "=", level )..s..string.rep( "=", level ).."]"
  end
 end
end

function table.load( sfile, loadinto )
 local tables = dofile( sfile )
 if tables then
  if loadinto ~= nil and type(loadinto) == "table" then
   table.unpickle( tables[1], tables, loadinto )
  else
   table.unpickle( tables[1], tables, _G )
  end
 end
end
 
function table.unpickle( t, tables, tcopy, pickled )
 pickled = pickled or {}
 pickled[t] = tcopy
 for i,v in pairs( t ) do
  local i2 = i
  if type( i ) == "table" then
   local pointer = tables[ i[1] ]
   if pickled[pointer] then
    i2 = pickled[pointer]
   else
    i2 = {}
    table.unpickle( pointer, tables, i2, pickled )
   end
  end
  local v2 = v
  if type( v ) == "table" then
   local pointer = tables[ v[1] ]
   if pickled[pointer] then
    v2 = pickled[pointer]
   else
    v2 = {}
    table.unpickle( pointer, tables, v2, pickled )
   end
  end
  tcopy[i2] = v2
 end
end

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: table.save and/or table.load not working for me anymore

Post by Caled »

Oddly, restarting my computer fixed it. I will try the full pathname if it reoccurs though.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: table.save and/or table.load not working for me anymore

Post by Heiko »

You must use absolute path names for table.load() and table.save(). Otherwise the result is undefined.

Post Reply