Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --@name SF_Processor_Downloader
- --@client
- if CLIENT then
- -- Listen for the net message "starfall_processor_download"
- net.receive("starfall_processor_download", function(len)
- -- Read the entire raw data blob as a string
- local rawData = net.readData(len)
- -- We'll keep a "cursor" index into rawData
- local pos = 1
- -- Helper to read one byte and move pos
- local function readByte()
- if pos > #rawData then return 0 end -- failsafe
- local n = string.byte(rawData, pos, pos)
- pos = pos + 1
- return n
- end
- -- Helper to read a 16-bit unsigned integer (little-endian) via arithmetic
- local function readUInt16()
- local b1 = readByte() or 0
- local b2 = readByte() or 0
- -- Combine little-endian bytes: (lowest-order byte) + (next byte * 256)
- return b1 + b2 * 256
- end
- -- Helper to read a 32-bit unsigned integer (little-endian) via arithmetic
- local function readUInt32()
- local b1 = readByte() or 0
- local b2 = readByte() or 0
- local b3 = readByte() or 0
- local b4 = readByte() or 0
- -- Combine little-endian bytes:
- -- b1 + (b2 * 256) + (b3 * 65536) + (b4 * 16777216)
- return b1
- + (b2 * 256)
- + (b3 * 65536)
- + (b4 * 16777216)
- end
- -- Helper to read a short-length string:
- -- 1) read a uint16 as the string length
- -- 2) read that many bytes and build a Lua string
- local function readString()
- local length = readUInt16()
- if length < 1 then return "" end
- local chars = {}
- for i = 1, length do
- chars[i] = string.char(readByte())
- end
- return table.concat(chars)
- end
- ------------------------------------------------------------------------------
- -- EXAMPLE of how you might parse data from the net message.
- -- Adjust to match how your Starfall data is actually structured.
- ------------------------------------------------------------------------------
- -- Suppose the first 32 bits represent how many “files” (or code chunks) we have
- local fileCount = readUInt32()
- print("Received fileCount = "..fileCount)
- for i = 1, fileCount do
- -- Maybe each segment starts with a file name (or path) stored as length-prefixed string
- local fileName = readString()
- -- Then read how many bytes the "file content" has
- local numContentBytes = readUInt32()
- local bytes = {}
- for j = 1, numContentBytes do
- bytes[j] = string.char(readByte())
- end
- local fileContent = table.concat(bytes)
- print(string.format("File #%d : %s (%d bytes)", i, fileName, numContentBytes))
- -- Now do whatever you want with each fileContent
- -- e.g., parse code, store or print, etc.
- end
- -- If there is additional data in the message, you can keep parsing it in a similar manner.
- end)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement