Advertisement
filloax

Untitled

Jan 25th, 2017
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.64 KB | None | 0 0
  1. local defaultrundata = {teststring = "", testint = 0, testx = 0, testy = 0}; --self-explanatory
  2.  
  3. local rundata = defaultrundata; --this is the actual mod data, different for each run, that you'll then access in your mod.
  4.  
  5. --When I say data/data list, I mean the table containing all the rundata (the one right below here), which is only modified in the saveData function; when I say rundata, I mean the table containing all the different strings, numbers, whatever, which is modified thorough the mod's code (not in this case, as it's just an example, but whatever).
  6.  
  7. --Return a table with, for keys that exist in both the source and target table, the values of the source table, and for values that only exist in the target, the values of the target table
  8. --We need to do this as simply doing, for example, table={a=1} and newtable = table, and then editing newtable.a like newtable.a=2, would also edit the variable in the old table.
  9. --In this mod, this would edit the defaultrundata, instead of simply setting the variables in the rundata to the default.
  10. --Also, in case the mod is updated and some variables not in the file are in the rundata, this will prevent them from being overriden with a nil when the file is loaded.
  11. --In our case, source is data.rundata[seed] or defaultrundata, and target is rundata (read the comments in loaddata for more info)
  12. function copyTable(source,target)
  13.   local output = {}
  14.  
  15.   if not target then target = source end --This needs to be done, to prevent errors when using a nil as a table
  16.  
  17.   for k,v in pairs(target) do --For every variable in target (not in source as it might not have variables added in a new version)
  18.     if source[k] ~= nil then --If the source contains the variable
  19.      
  20.       if type(source[k]) == "table" then --If the value of the variable is also a table, we need to do this again. (Actually, I'm not sure, but to be safe, do this)
  21.         output[k] = copyTable(source[k],target[k]);
  22.       else
  23.         output[k] = source[k]; --If the value of k isn't a table, just copy it over to the output.
  24.       end
  25.      
  26.     else   --If the variable is only in target
  27.      
  28.       if type(target[k]) == "table" then --If the value of the variable is also a table, we need to do this again. (Actually, I'm not sure, but to be safe, do this)
  29.         output[k] = copyTable(target[k]);
  30.       else
  31.         output[k] = target[k]; --If the value of k isn't a table, just copy it over to the output.
  32.       end
  33.      
  34.     end
  35.   end
  36.  
  37.   return output;
  38. end
  39.  
  40. --DATA STRUCTURE:
  41. --data(saved table) = {
  42. --   rundata = {table with seeds as keys corresponding to that run's rundata},
  43. --   seeds = {table with ids as keys corresponding to that id's seed used for overwriting},
  44. --   lastid = x last id that has been saved
  45. --basically when the data for more than 10 runs is saved, the oldest one is deleted, and a new one is put at that id
  46. function saveData()
  47.   local seed = Game():GetPlayer(0).InitSeed --This isn't the run's seed, but it's still different for every run.
  48.   local data;
  49.  
  50.   if Isaac.HasModData(mod) then
  51.     local loadeddata = Isaac.LoadModData(mod);
  52.     data = load("return "..loadeddata)(); --This is the data list, the one explained above, which contains all the different rundata for each run
  53.    
  54.     if type(data) == "table" and data.lastid ~= nil then --There is some data saved and it's in the right format (meaning, structured like above).
  55.      
  56.       if data.seeds[data.lastid] == seed then --If the last data that has been saved is for the current run, then
  57.        
  58.         data.rundata[seed] = mdata; --Save the current rundata to the data list
  59.                                     --We don't need to worry about using copyTable here, as the data table is only local
  60.        
  61.       else --If it's the first time you're saving data for this run
  62.        
  63.         --Increase the lastid by 1, or set it back to 1 if it's at 10.
  64.         if data.lastid < 10 then
  65.           data.lastid = data.lastid+1;
  66.         else
  67.           data.lastid = 1;
  68.         end
  69.        
  70.         data.rundata[seed] = rundata; --save the current rundata to the data list
  71.        
  72.         if #data.rundata > 10 then --If the data list has more than 10 rundata, remove the oldest one (the one whose seed has the same id we're using)
  73.           data.rundata[data.seeds[data.lastid]] = nil;
  74.         end
  75.        
  76.         data.seeds[data.lastid] = seed; --Add our rundata to the data list.
  77.       end
  78.     else --There already is some data but it's not a table, and it isn't saved the right format. So, I will act like there's no preexisting data and use default data.
  79.      
  80.       data = {rundata={},seeds={},lastid=1}
  81.      
  82.       data.seeds[1] = seed;
  83.       data.rundata[seed] = rundata;  
  84.     end
  85.    
  86.   else --First time saving data, it will just use a default data
  87.     data = {rundata={},seeds={},lastid=1}
  88.    
  89.     data.seeds[1] = seed;
  90.     data.rundata[seed] = rundata;  
  91.   end
  92.  
  93.   Isaac.SaveModData(mod, table_tostring(data)); --Save the data list to file.
  94. end
  95.  
  96. function loadData()
  97.   local seed = Game():GetPlayer(0).InitSeed --This isn't the run's seed, but it's still different for every run.
  98.  
  99.   if Isaac.HasModData(mod) then --if there is a data file then load it
  100.     local data = load("return "..Isaac.LoadModData(mod))(); --load the string as a function, the extra () are to immediately run it
  101.    
  102.     if data.rundata[seed] == nil then --if the data for this run has never been saved, then use the default one
  103.       rundata = copyTable(defaultrundata, rundata);
  104.     else
  105.       rundata = copyTable(data.rundata[seed], rundata);
  106.     end
  107.   else --if there isn't, just use the default one
  108.      rundata = copyTable(defaultrundata, rundata);
  109.   end
  110. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement