Advertisement
Wojbie

SerializeRec - Recursive Table Serialize

Oct 19th, 2015 (edited)
1,007
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.44 KB | None | 0 0
  1. -------------------------------------------------------------------------------------------
  2. -- Wojbies API 4.0 - Recursive Table Serialize - Compatible with textutils.unserialize() --
  3. -------------------------------------------------------------------------------------------
  4. --   Copyright (c) 2015-2021 Wojbie (wojbie@wojbie.net)
  5. --   Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
  6. --   1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7. --   2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  8. --   3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  9. --   4. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. --   5. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
  11. --   NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. YOU ACKNOWLEDGE THAT THIS SOFTWARE IS NOT DESIGNED, LICENSED OR INTENDED FOR USE IN THE DESIGN, CONSTRUCTION, OPERATION OR MAINTENANCE OF ANY NUCLEAR FACILITY.
  12.  
  13.  
  14. --### Initializing
  15. local s = shell and {} or (_ENV or getfenv())
  16. s.versionName = "Recursive Table Serialize By Wojbie"
  17. s.versionNum = 4.0 --2019-12-29
  18.  
  19. --# Serializes recursive tables into flat textutils.unserialize valid program string.
  20. s.serializeRec = function(t)
  21.     local function serializeFlat(data)
  22.         --return string.gsub(textutils.serialize(data), "\n%s*", "")
  23.         return textutils.serialize(data)
  24.     end
  25.     if type(t) ~= "table" then
  26.             return serializeFlat(t) --it was not table - do normal serialize
  27.     end
  28.     local tImput = {t}
  29.     local tList = {}
  30.     local tLook = {}
  31.     local tMark = {}
  32.     local tOutp = {"(function() "}
  33.     local cur, nTab
  34.     while #tImput > 0 do
  35.             cur = table.remove(tImput)
  36.             nTab = #tList + 1
  37.             tList[nTab] = cur
  38.             nTab = "t" .. nTab
  39.             tLook[cur] = nTab
  40.             if next(cur) == nil then
  41.                     -- Empty tables are simple
  42.                     tOutp[#tOutp + 1] = nTab .. "={};"
  43.             else
  44.                     --Look over the table, make copy
  45.                     local Basic = {}
  46.                     for k, v in pairs(cur) do
  47.                             if type(k) == "table" or type(v) == "table" then
  48.                                     table.insert(tMark, {cur, k, v})
  49.                                     if type(k) == "table" and not tLook[k] then tImput[#tImput + 1] = k end
  50.                                     if type(v) == "table" and not tLook[v] then tImput[#tImput + 1] = v end
  51.                             else
  52.                                     Basic[k] = v
  53.                             end
  54.                     end
  55.                     tOutp[#tOutp + 1] = nTab .. "=" .. serializeFlat(Basic) .. ";"
  56.             end
  57.     end
  58.     while #tMark > 0 do
  59.             cur = table.remove(tMark)
  60.             tOutp[#tOutp + 1] = tLook[cur[1]] .. "[" .. (type(cur[2]) == "table" and tLook[cur[2]] or serializeFlat(cur[2])) .. "]=" .. (type(cur[3]) == "table" and tLook[cur[3]] or serializeFlat(cur[3])) .. ";"
  61.     end
  62.     tOutp[#tOutp + 1] = "return t1 end)()"
  63.     return table.concat(tOutp)
  64. end
  65.  
  66. --### Finalizing
  67. return s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement