Advertisement
Jousway

SMConfig.lua

Jul 16th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.38 KB | None | 0 0
  1. Config = {}
  2.  
  3. function Config.Save(key,value,file)
  4.     if not FILEMAN:DoesFileExist(file) then
  5.         local Createfile = RageFileUtil.CreateRageFile()
  6.         Createfile:Open(file, 2)
  7.         Createfile:Write("")
  8.         Createfile:Close()
  9.         Createfile:destroy()
  10.     end
  11.  
  12.  
  13.     local Container = {}
  14.  
  15.     local configfile = RageFileUtil.CreateRageFile()
  16.     configfile:Open(file, 1)
  17.    
  18.     local configcontent = configfile:Read()
  19.    
  20.     configfile:Close()
  21.    
  22.     local found = false
  23.    
  24.     for line in string.gmatch(configcontent.."\n", "(.-)\n") do
  25.         for KeyVal, Val in string.gmatch(line, "(.-)=(.+)") do
  26.             if key == KeyVal then Val = value found = true end
  27.             Container[KeyVal] = Val
  28.         end    
  29.     end
  30.    
  31.     if found == false then Container[key] = value end
  32.    
  33.     local output = ""
  34.    
  35.     for k,v in pairs(Container) do
  36.         output = output..k.."="..v.."\n"
  37.     end
  38.    
  39.     configfile:Open(file, 2)
  40.     configfile:Write(output)
  41.     configfile:Close()
  42.     configfile:destroy()
  43. end
  44.  
  45. function Config.Load(key,file) 
  46.     if not FILEMAN:DoesFileExist(file) then return false end
  47.    
  48.     local Container = {}
  49.  
  50.     local configfile = RageFileUtil.CreateRageFile()
  51.     configfile:Open(file, 1)
  52.    
  53.     local configcontent = configfile:Read()
  54.    
  55.     configfile:Close()
  56.     configfile:destroy()
  57.    
  58.     for line in string.gmatch(configcontent.."\n", "(.-)\n") do
  59.         for KeyVal, Val in string.gmatch(line, "(.-)=(.+)") do
  60.             if key == KeyVal then return Val end
  61.         end    
  62.     end
  63. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement