Snippet Lua_serialize_table

Nestharus

o-o
Reaction score
84
Actually LUA_SERIALIZE_TABLE

Courtesy of http://lua-users.org/wiki/SaveTableToFile

Ported to vjass and LUA_FILE_HEADER

LUA_FILE_HEADER
JASS:

//SerializeTable

/*
   lua-users.org/wiki/SaveTableToFile

   Save Table to File/Stringtable
   Load Table from File/Stringtable
   v 0.94
   
   Lua 5.1 compatible
   
   Userdata and indices of these are not saved
   Functions are saved via string.dump, so make sure it has no upvalues
   References are saved
   ----------------------------------------------------
   table.save( table [, filename] )
   
   Saves a table so it can be called via the table.load function again
   table must a object of type 'table'
   filename is optional, and may be a string representing a filename or true/1
   
   table.save( table )
      on success: returns a string representing the table (stringtable)
      (uses a string as buffer, ideal for smaller tables)
   table.save( table, true or 1 )
      on success: returns a string representing the table (stringtable)
      (uses io.tmpfile() as buffer, ideal for bigger tables)
   table.save( table, "filename" )
      on success: returns 1
      (saves the table to file "filename")
   on failure: returns as second argument an error msg
   ----------------------------------------------------
   table.load( filename or stringtable )
   
   Loads a table that has been saved via the table.save function
   
   on success: returns a previously saved table
   on failure: returns as second argument an error msg
   ----------------------------------------------------
   
   chillcode, lua-users.org/wiki/SaveTableToFile
   Licensed under the same terms as Lua itself.
*/

//! externalblock extension=lua FileExporter $FILENAME$
    //! runtextmacro LUA_FILE_HEADER()
    //! i writelua("SerializeTable", [[
    //////////////////////////////////////////////////////////////////
    //code

    //! i do
        //declare local variables
        //exportstring( string )
        //returns a "Lua" portable version of the string
        //! i local function exportstring( s )
            //! i s = string.format( "%q",s )
            //to replace
            //! i s = string.gsub( s,"\\\n","\\n" )
            //! i s = string.gsub( s,"\r","\\r" )
            //! i s = string.gsub( s,string.char(26),"\"..string.char(26)..\"" )
            //! i return s
        //! i end
        //The Save Function
        //! i function table.save(  tbl,filename )
            //! i local charS,charE = "   ","\n"
            //! i local file,err
            // create a pseudo file that writes to a string and return the string
            //! i if not filename then
                //! i file =  { write = function( self,newstr ) self.str = self.str..newstr end, str = "" }
                //! i charS,charE = "",""
            //write table to tmpfile
            //! i elseif filename == true or filename == 1 then
                //! i charS,charE,file = "","",io.tmpfile()
            //write table to file
            //use io.open here rather than io.output, since in windows when clicking on a file opened with io.output will create an error
            //! i else
                //! i file,err = io.open( filename, "w" )
                //! i if err then return _,err end
            //! i end
            //initiate variables for save procedure
            //! i local tables,lookup = { tbl },{ [tbl] = 1 }
            //! i file:write( "return {"..charE )
            //! i for idx,t in ipairs( tables ) do
                //! i if filename and filename ~= true and filename ~= 1 then
                    //! i file:write( "-- Table: {"..idx.."}"..charE )
                //! i end
                //! i file:write( "{"..charE )
                //! i local thandled = {}
                //! i for i,v in ipairs( t ) do
                    //! i thandled<i> = true
                    //escape functions and userdata
                    //! i if type( v ) ~= &quot;userdata&quot; then
                        //only handle value
                        //! i if type( v ) == &quot;table&quot; then
                            //! i if not lookup[v] then
                                //! i table.insert( tables, v )
                                //! i lookup[v] = #tables
                            //! i end
                            //! i file:write( charS..&quot;{&quot;..lookup[v]..&quot;},&quot;..charE )
                        //! i elseif type( v ) == &quot;function&quot; then
                            //! i file:write( charS..&quot;loadstring(&quot;..exportstring(string.dump( v ))..&quot;),&quot;..charE )
                        //! i else
                            //! i local value =  ( type( v ) == &quot;string&quot; and exportstring( v ) ) or tostring( v )
                            //! i file:write(  charS..value..&quot;,&quot;..charE )
                        //! i end
                    //! i end
                //! i end
                //! i for i,v in pairs( t ) do
                    //escape functions and userdata
                    //! i if (not thandled<i>) and type( v ) ~= &quot;userdata&quot; then
                        //handle index
                        //! i if type( i ) == &quot;table&quot; then
                            //! i if not lookup<i> then
                                //! i table.insert( tables,i )
                                //! i lookup<i> = #tables
                            //! i end
                            //! i file:write( charS..&quot;[{&quot;..lookup<i>..&quot;}]=&quot; )
                        //! i else
                            //! i local index = ( type( i ) == &quot;string&quot; and &quot;[&quot;..exportstring( i )..&quot;]&quot; ) or string.format( &quot;[%d]&quot;,i )
                            //! i file:write( charS..index..&quot;=&quot; )
                        //! i end
                        //handle value
                        //! i if type( v ) == &quot;table&quot; then
                            //! i if not lookup[v] then
                                //! i table.insert( tables,v )
                                //! i lookup[v] = #tables
                            //! i end
                            //! i file:write( &quot;{&quot;..lookup[v]..&quot;},&quot;..charE )
                        //! i elseif type( v ) == &quot;function&quot; then
                            //! i file:write( &quot;loadstring(&quot;..exportstring(string.dump( v ))..&quot;),&quot;..charE )
                        //! i else
                            //! i local value =  ( type( v ) == &quot;string&quot; and exportstring( v ) ) or tostring( v )
                            //! i file:write( value..&quot;,&quot;..charE )
                        //! i end
                    //! i end
                //! i end
                //! i file:write( &quot;},&quot;..charE )
            //! i end
            //! i file:write( &quot;}&quot; )
            //Return Values
            //return stringtable from string
            //! i if not filename then
                //set marker for stringtable
                //! i return file.str..&quot;--|&quot;
            //return stringttable from file
            //! i elseif filename == true or filename == 1 then
                //! i file:seek ( &quot;set&quot; )
                //no need to close file, it gets closed and removed automatically
                //set marker for stringtable
                //! i return file:read( &quot;*a&quot; )..&quot;--|&quot;
           //close file and return 1
           //! i else
              //! i file:close()
              //! i return 1
           //! i end
        //! i end

        //The Load Function
        //! i function table.load( sfile )
            //catch marker for stringtable
            //! i if string.sub( sfile,-3,-1 ) == &quot;--|&quot; then
                //! i tables,err = loadstring( sfile )
            //! i else
                //! i tables,err = loadfile( sfile )
            //! i end
            //! i if err then return _,err
            //! i end
            //! i tables = tables()
            //! i for idx = 1,#tables do
                //! i local tolinkv,tolinki = {},{}
                //! i for i,v in pairs( tables[idx] ) do
                    //! i if type( v ) == &quot;table&quot; and tables[v[1] ] then
                            //! i table.insert( tolinkv,{ i,tables[v[1] ] } )
                    //! i end
                    //! i if type( i ) == &quot;table&quot; and tables[i[1] ] then
                            //! i table.insert( tolinki,{ i,tables[i[1] ] } )
                    //! i end
                //! i end
                //link values, first due to possible changes of indices
                //! i for _,v in ipairs( tolinkv ) do
                    //! i tables[idx][v[1] ] = v[2]
                //! i end
                //link indices
                //! i for _,v in ipairs( tolinki ) do
                    //! i tables[idx][v[2] ],tables[idx][v[1] ] =  tables[idx][v[1] ],nil
                //! i end
            //! i end
            //! i return tables[1]
        //! i end
    //close do
    //! i end

    //end code
    //////////////////////////////////////////////////////////////////
    //! i ]])
//! endexternalblock
</i></i></i></i></i>



Test code
JASS:

//! externalblock extension=lua FileExporter $FILENAME$
    //! runtextmacro LUA_FILE_HEADER()
    
    //! i dofile(&quot;SerializeTable&quot;)

    //! i local t = {}
    //! i t.name = &quot;boo&quot;
    //! i t[15] = &quot;rawr&quot;
    //! i t.growl = {}
    //! i t.growl.blemish = &quot;kaka&quot;
    
    //! i local tstr = table.save(t)
    //! i logf(tstr .. &quot;\n\n&quot;)
    //! i local ts = table.load(tstr)
    //! i logf(ts.name)
    //! i logf(ts[15])
    //! i logf(ts.growl.blemish)
    //! i ts.growl.boo = &quot;hello&quot;
//! endexternalblock
 

tooltiperror

Super Moderator
Reaction score
231
Make a thread called something like "Nes' collection of Lua scripts" or something.

"LUA" is not the name of the language. It is Lua, not lua or LUA or LuA or lUa.
 

Nestharus

o-o
Reaction score
84
I know it's Lua... LUA doesn't refer to the language, notice the entire thing is in all caps =). Originally, all of the lua scripts (most of them) were textmacros, and so the convention carried over when it moved into using the file header, which happens to be a textmacro.

Make a thread called something like "Nes' collection of Lua scripts" or something.

They're all separate resources, and as such, should be in separate threads.
 

Bribe

vJass errors are legion
Reaction score
67
Personally, I don't like spellpacks. They are hard to very difficult to review and approve because often times, half of them are total trash, not to mention every spellpack absolutely MUST have something like GTrigger, SpellEvent or SpellEffectEvent.
 

Sim

Forum Administrator
Staff member
Reaction score
534
> They are hard to very difficult to review and approve because often times, half of them are total trash

Rule #10: Only submit what you think is good. Why post something that's bad? If half of the spells are good and the others are trash, submit the good half and screw the other half.
We never had any problem with spellpacks before, there are many of them in the section right now.

> They're all separate resources, and as such, should be in separate threads.

Spellpacks were invented because users such as you posted 5 spells in a row at a given date. It should apply for snippets here too. Especially snippets.
Make it a library or something. "Library of Lua scripts" or whatever.
 

Nestharus

o-o
Reaction score
84
I was never a fan of spellpacks and never look at anything that's a pack ;D.

But these were actually developed over a good period of time and were only all posted at once because I had finished all of these a long time ago at THW : p. If I posted everything I had at THW, it'd be like 2 pages of resources ;D.

A library of Lua scripts is silly... that'd be akin to telling j4l to making all of his scripts a library of vjass scripts no?

I can understand some spellpacks as the spells may be all related to a hero or something (all fitting a theme), but not script packs, lol.

serialize table has nothing in common with the other scripts other than the fact it's written in Lua. GetObjectId is a function to retrieve a unique object id ;|. GetVarObject is a system for managing objects in maps that uses GetObjectId as well as file i/o. I coded GetVarObject quite a long time after I did GetObjectId, and the file header was actually one of the last scripts I coded.


So uh, no : D, lol.


I hope you understand why ;P. It's akin to asking an author to put all of their vjass scripts into one thread as a library.

I didn't even code this one here o-o, I just ported it.
 

Sim

Forum Administrator
Staff member
Reaction score
534
Makes sense.

These are good scripts! They're all approved.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top