if (type (Require) ~= 'function') then loadstring(exports['nc_libraries']:extend('Extend'), 'Require')() end local CLIENT = isElement(localPlayer) local File = _G.File or {} if type (File.InChunks) == 'function' then return File; end Require('Thread'); function fileInChunks(FileElement, ChunkSize, onChunk, onFinish, Data) local getThread = type(getThread) == 'function' and getThread or function () return false end local isThreadValid = type(isThreadValid) == 'function' and isThreadValid or function () return false end local Thread = getThread() local WaitTick = type(WaitTick) == 'function' and WaitTick or function () end local Wait = type(Wait) == 'function' and Wait or function () end local Success = true; local count = 0; ChunkSize = ChunkSize or (512 * 1024) -- 512KB padrão if isThreadValid(Thread) then while not fileIsEOF(FileElement) do count = count + 1; if count % (Data and Data.LoopMax or 30) == 0 then if CLIENT then WaitTick(Data.aWait or 1) else Wait(Data.aWait or 1) end end local _Chunk = fileRead(FileElement, ChunkSize) if not _Chunk then Success = false; break end local _Success, _Err = pcall(onChunk, _Chunk) if not _Success then outputDebugString("Erro no callback do chunk: " .. tostring(_Err), 1) Success = false; break end end if type(onFinish) == 'function' then onFinish(Success) end return Success else CreateThread(function(FileElement, ChunkSize, onChunk, onFinish, Data) while not fileIsEOF(FileElement) do count = count + 1; if count % (Data and Data.LoopMax or 10) == 0 then if CLIENT then WaitTick(1) else Wait(1) end end local _Chunk = fileRead(FileElement, ChunkSize) if not _Chunk then Success = false; break end local _Success, _Err = pcall(onChunk, _Chunk) if not _Success then outputDebugString("Erro no callback do chunk: " .. tostring(_Err), 1) Success = false; break end end if type(onFinish) == 'function' then onFinish(Success) end return Success end, FileElement, ChunkSize, onChunk, onFinish, Data) end return true end function fileInEncodedChunks(FileElement, ChunkSizes, onChunk, onFinish) local getThread = type(getThread) == 'function' and getThread or function () return false end local isThreadValid = type(isThreadValid) == 'function' and isThreadValid or function () return false end local WaitTick = type(WaitTick) == 'function' and WaitTick or function () end local Wait = type(Wait) == 'function' and Wait or function () end local Thread = getThread() local count = 0 local Success = true; local offset = 0 if isThreadValid(Thread) then for i = 1, #ChunkSizes do local chunkLen = tonumber(ChunkSizes[i]) count = count + 1 if count >= 50 then if CLIENT then WaitTick(5) else Wait(1) end count = 0 end fileSetPos(FileElement, offset) local _Chunk = fileRead(FileElement, chunkLen) offset = offset + chunkLen if (not _Chunk or #_Chunk < chunkLen) then outputDebugString("Erro: chunk incompleto ou inválido", 1) Success = false; break end local _Success, _Err = pcall(onChunk, _Chunk) if not _Success then outputDebugString("Erro no callback do chunk: " .. tostring(_Err), 1) Success = false; break end collectgarbage("collect") end if type(onFinish) == 'function' then onFinish(Success) end return Success else CreateThread(function() for i = 1, #ChunkSizes do local chunkLen = tonumber(ChunkSizes[i]) count = count + 1 if count >= 50 then if CLIENT then WaitTick(5) else Wait(1) end count = 0 end fileSetPos(FileElement, offset) local _Chunk = fileRead(FileElement, chunkLen) offset = offset + chunkLen if (not _Chunk or #_Chunk < chunkLen) then outputDebugString("Erro: chunk incompleto ou inválido", 1) Success = false; break end local _Success, _Err = pcall(onChunk, _Chunk) if not _Success then outputDebugString("Erro no callback do chunk: " .. tostring(_Err), 1) Success = false; break end collectgarbage("collect") end if type(onFinish) == 'function' then onFinish(Success) end return Success end) end return true end File.InChunks = function (FileElement, chunkSize, onChunk, onFinish) assert(isElement(FileElement), 'arquivo inválido') return fileInChunks(FileElement, chunkSize, onChunk, onFinish) end File.InEncodedChunks = function (FileElement, chunkSizes, onChunk, onFinish) assert(isElement(FileElement), 'arquivo inválido') return fileInEncodedChunks(FileElement, chunkSizes, onChunk, onFinish) end return File