Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- fCompress/fRead usage
- playerStats={
- wins=123,
- loses=456
- }
- --variable names don't take up space in the file!
- playerStatNames={"wins","loses"}
- --stats are only 2 bytes each!
- playerStatBytes={2,2}
- --fCompress
- local savedata=fCompress({playerStats.wins,playerStats.loses},playerStatBytes)
- #savedata --only 4 bytes!
- --fRead
- local statArray=fRead(savedata,playerStatBytes)
- for i,stat in ipairs(playerStatNames) do
- playerStats[stat]=statArray[i]
- end
- --Byte size
- --1 bytes max # 256^1-1 = 255
- --2 bytes max # 256^2-1 = 65535
- --3 bytes max # 256^3-1 = 16777215
- --4 bytes max # 256^4-1 = 4294967295
- ]]--
- function fCompress(nArray,nSizeArray)
- local output={}
- for i,bytes in ipairs(nSizeArray) do
- nArray[i]=math.min(nArray[i],256^bytes-1)
- local mod=0
- for ii=0,bytes-1 do
- mod=(nArray[i]-mod)/256^ii%256
- table.insert(output,string.char(mod))
- end
- end
- return table.concat(output)
- end
- function fRead(fString,nSizeArray)
- local output={}
- local index=1
- for i,bytes in ipairs(nSizeArray) do
- local number=0
- for i=0,bytes-1 do
- number=number+string.byte(string.sub(fString,index+i,index+i))*256^i
- end
- table.insert(output,number)
- index=index+bytes
- end
- return output
- end
Advertisement
Add Comment
Please, Sign In to add comment