Please log in to view the forum threads.

Be warned, NEVER give your account details to anyone!
Double accounts are unwanted and strictly prohibited!




If your account got hacked please immediately report this to the following Discord id's: Pr0Master#7146 or Snake#0582

You can join our DISCORD support group by using the following link: https://discordapp.com/invite/fgbMxZG


Your Infinity Team

    Lua Memory

    Share
    avatar
    Starkkz
    Developer
    Developer

    Posts : 55
    Points : 75
    Likes : 6
    Age : 21
    Country : Chile/Santiago
    Join date : 2013-05-04

    Lua Memory

    Post by Starkkz on Fri Feb 28, 2014 2:56 am

    I just managed to make a RAM to HDD memory, it's still unstable because I made it in some minutes.

    Features:
    - Every variable you set on the global table will be saved, incluiding sub tables memory

    Issues:
    - The script saves the HDD memory only when a variable in the global environment is modified. The only way I could fix this is by synchronizing the global table with the modified memory in sub tables, this subtables will need a source value to know where it is placed in the global environment.

    Code:

    EnvironmentMetatable = {}

    function GetGlobal()
      local f = io.open("Env.txt", "r")
      if f then
        local TableString = f:read("*a")
        local Table = string.totable(TableString)
        f:close()
        return Table or {}
      end
      return {}
    end

    function SaveGlobal(T)
      local f = io.open("Env.txt", "w")
      if f then
        local TableString = table.serialize(T)
        f:write(TableString)
        f:close()
        return true
      end
      return false
    end

    function EnvironmentMetatable:__newindex(Key, Value)
      local SavedGlobal = GetGlobal()
      if self[Key] then
        setmetatable(self, nil)
        self[Key] = Value
        setmetatable(self, EnvironmentMetatable)
      elseif SavedGlobal then
        rawset(SavedGlobal, Key, Value)
        SaveGlobal(SavedGlobal)
      end
    end

    function EnvironmentMetatable:__index(Key)
      setmetatable(self, nil)
      local Value = self[Key]
      setmetatable(self, EnvironmentMetatable)
      if Value then
        return Value
      end
      
      local SavedGlobal = GetGlobal()
      return rawget(SavedGlobal, Key)
    end

    function serializeElement(object)
      if type(object) == "string" then
        return string.format("%q", object)
      elseif type(object) == "number" then
        return tostring(object)
      elseif type(object) == "table" then
        return table.serialize(object)
      elseif type(object) == "function" then
        local Success, Dump = pcall(string.dump, object)
        if Success then
          return "loadstring(\""..Dump.."\")"
        end
      end
    end

    function table.serialize(t)
      if not t then return nil end
      local Elements = {}
      for k, v in pairs(t) do
        local Key = serializeElement(k)
        local Value = serializeElement(v)
        if Key and Value then
          table.insert(Elements, "["..Key.."]="..Value)
        end
      end
      return "{"..table.concat(Elements, ",").."}"
    end

    function string.totable(s)
      return loadstring("return "..s)()
    end
    setmetatable(_G, EnvironmentMetatable)

    I will probably add this on my admin script to make memory optimization tricks for computers with low ram.

      Current date/time is Sat Jun 23, 2018 4:47 pm