Advertisement
artemx32

Untitled

Feb 24th, 2025 (edited)
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | None | 0 0
  1. --@name SF_Processor_Downloader
  2. --@client
  3.  
  4. if CLIENT then
  5.  
  6.     -- Listen for the net message "starfall_processor_download"
  7.     net.receive("starfall_processor_download", function(len)
  8.         -- Read the entire raw data blob as a string
  9.         local rawData = net.readData(len)
  10.  
  11.         -- We'll keep a "cursor" index into rawData
  12.         local pos = 1
  13.        
  14.         -- Helper to read one byte and move pos
  15.         local function readByte()
  16.             if pos > #rawData then return 0 end  -- failsafe
  17.             local n = string.byte(rawData, pos, pos)
  18.             pos = pos + 1
  19.             return n
  20.         end
  21.        
  22.         -- Helper to read a 16-bit unsigned integer (little-endian) via arithmetic
  23.         local function readUInt16()
  24.             local b1 = readByte() or 0
  25.             local b2 = readByte() or 0
  26.             -- Combine little-endian bytes: (lowest-order byte) + (next byte * 256)
  27.             return b1 + b2 * 256
  28.         end
  29.  
  30.         -- Helper to read a 32-bit unsigned integer (little-endian) via arithmetic
  31.         local function readUInt32()
  32.             local b1 = readByte() or 0
  33.             local b2 = readByte() or 0
  34.             local b3 = readByte() or 0
  35.             local b4 = readByte() or 0
  36.             -- Combine little-endian bytes:
  37.             --   b1 + (b2 * 256) + (b3 * 65536) + (b4 * 16777216)
  38.             return b1
  39.                 + (b2 * 256)
  40.                 + (b3 * 65536)
  41.                 + (b4 * 16777216)
  42.         end
  43.        
  44.         -- Helper to read a short-length string:
  45.         --   1) read a uint16 as the string length
  46.         --   2) read that many bytes and build a Lua string
  47.         local function readString()
  48.             local length = readUInt16()
  49.             if length < 1 then return "" end
  50.             local chars = {}
  51.             for i = 1, length do
  52.                 chars[i] = string.char(readByte())
  53.             end
  54.             return table.concat(chars)
  55.         end
  56.        
  57.         ------------------------------------------------------------------------------
  58.         -- EXAMPLE of how you might parse data from the net message.
  59.         -- Adjust to match how your Starfall data is actually structured.
  60.         ------------------------------------------------------------------------------
  61.        
  62.         -- Suppose the first 32 bits represent how many “files” (or code chunks) we have
  63.         local fileCount = readUInt32()
  64.         print("Received fileCount = "..fileCount)
  65.        
  66.         for i = 1, fileCount do
  67.             -- Maybe each segment starts with a file name (or path) stored as length-prefixed string
  68.             local fileName = readString()
  69.            
  70.             -- Then read how many bytes the "file content" has
  71.             local numContentBytes = readUInt32()
  72.             local bytes = {}
  73.            
  74.             for j = 1, numContentBytes do
  75.                 bytes[j] = string.char(readByte())
  76.             end
  77.            
  78.             local fileContent = table.concat(bytes)
  79.             print(string.format("File #%d : %s (%d bytes)", i, fileName, numContentBytes))
  80.            
  81.             -- Now do whatever you want with each fileContent
  82.             -- e.g., parse code, store or print, etc.
  83.         end
  84.        
  85.         -- If there is additional data in the message, you can keep parsing it in a similar manner.
  86.     end)
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement