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.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      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