util.Compress returns "]" every single time regardless of input serverside
9 replies, posted
I'm attempting to compress a JSON table string before networking it over to the client. Originally I was sending the table then compressing it on the client for storage reasons (dumb I know) but eventually I remembered "oh hey yeah networking hates big files and tables get big fast" after a reasonably sized table caused everything to crap out so I swapped the compression to the server side before it gets sent out. Unfortunately this instead returns "]" instead of the compressed string and the weird bit is that even when I send it normal strings like "heylolwhats up dude" it still just returns "]".
Do note that this wasn't happening when it was compressing client side and now after I changed it I can't get it to happen on either side.
Code in question:
concommand.Add("fbg_worldsave", function(ply, cmd, args)
bigTable = {}
local worldObjTable = ents.FindByClass("roblox_brick_*")
--local worldObjDat = {}
--PrintTable(worldObjTable)
bigTable = duplicator.CopyEnts(worldObjTable)
local dupeTable_d = util.TableToJSON(bigTable, true)
print(dupeTable_d)
local dupeTable_c = util.Compress(dupeTable_d)
print("Ugly thing: ")
print(dupeTable_c)
worldSaverThing(ply, dupeTable_c, args[1]) -- DONT RUN THIS IN MP YOUR GONNA BREAK SOME SHIT.
-- Lol in this example I broke that warning ---^
end)
Console:
https://files.facepunch.com/forum/upload/109874/24a3d5db-66ae-4d41-8978-474bdda42b04/image.png
As noted by the client side messages, i'm also printing quick what's shown once it reaches the client in case the net library was just being pissy. Also, due to the table being big I cant show it all. Lastly, any non-local variables you see are initialized locally elsewhere up the code tree outside a function.
just curious, have you tried writing the compressed data to a text file and seeing if it appears in there?
wow would you look at that util.Compress did its job, now we can move on to its buddy util.Decompress which turns that back to the original text! the magic of loseless compression is surely amazing! all hail LZMA!!!
] lua_run_cl a=util.Compress'lolwhat is that'print(a,util.Decompress(a))
] lolwhat is that
The reason for this is because the return from util.Compress is in a compress binary format, it uses LZMA I think. When you try to print the compressed string, containing weird binary characters after the compression, the console just bails out on the strange characters since it's not real ASCII. If you want to print it and see what's happening for human-readability, try doing a Base64 encode on the compressed data before printing.
What is your code to save data to file?
What is your code to send the data over .net?
Sure here it is, I didn't originally post it because it tends to get a bit big:
Also in the init.lua file (gamemode):
function worldSaverThing(ply, worldTable, fileName)
if IsValid(ply) then
net.Start("client_SaveWorld")
net.WriteEntity(ply)
net.WriteTable(worldTable)
net.WriteString(fileName)
net.Send(ply)
end
end
cl_init.lua side:
function magicDupeMachine(dupeTable, fileName)
-- Convert the table to a string (decompressed), Compress it...
local dupeTable_d = util.TableToJSON(dupeTable, false)
local dupeTable_c = util.Compress(dupeTable_d)
PrintTable(dupeTable)
-- Make ourselves a new file name to work with given the filename they want to write to.
local newFileName = "fbg_" .. fileName .. ".txt"
-- Write this to a new file in the /data/ directory!
file.Write(newFileName, dupeTable_C)
if file.Exists(newFileName, "DATA") != true then
print("OOF! Somethings wrong! Saved file not found!")
else
print("DING! File done saving.")
print("File contents:")
print(file.Read(newFileName, "DATA"))
end
end
net.Receive("client_SaveWorld", function()
local ply = net.ReadEntity()
local message = net.ReadTable()
local fileName = net.ReadString()
if LocalPlayer() == ply then
magicDupeMachine(message, fileName)
end
end)
So I am assuming you take the raw table from duplicator lib and push it through net library to client, and compress and save the data on client?
Have you tried checking if the table you receive on client is there in its entirety?
Ignore that code I posted before. I was dumb and forgot I tried to rewrite it the old way. I'll have the original code in a minute here
You can't use WriteString for binary data that has embedded zeroes. Use WriteData
Sorry, you need to Log In to post a reply to this thread.