Jump to content

[HELP] engineLoadDFF using raw data


Recommended Posts

Hi, 

I'm now trying to base64+teaEncode some of my models, the Wiki says that engineLoadDff can load not only DFFs, but the file buffer too.

As a result, engineReplaceModel fails returning "false".

So I have written some code to encode-decode the model data. Am I right with my understanding of "raw data" needed to give to engineLoadDff as an argument?

P.S. I was looking for issue using forumsearch etc. for a long time, but I haven't found any analogues, everybody uses engineLoadDFF only with filename as argument.

addEventHandler('onClientResourceStart', resourceRoot,  
    function() 
	local buffer	
	local hFile = fileOpen("model.dff")             -- attempt to open the file
	if hFile then                                  -- check if it was successfully opened	
		while not fileIsEOF(hFile) do              -- as long as we're not at the end of the file...
			buffer = fileRead(hFile, 500)          -- ... read the next 500 bytes...
			                  -- ... and output them to the console
		end
		fileClose(hFile)                           -- close the file once we're done with it
	else
		outputConsole("Unable to open the model file")
	end 
	local enBuffer = teaEncode( base64Encode( buffer ), "xTN5#Gqm=sKn**dF" )	
	local deBuffer = base64Decode( teaDecode( enBuffer, "xTN5#Gqm=sKn**dF" ) )
    local txd = engineLoadTXD('tex.txd',true) 
    engineImportTXD(txd, 596) 
    local dff = engineLoadDFF(deBuffer)  
    engineReplaceModel(dff, 596) 
    end  
) 

 

Edited by maximumdrive
Link to comment

Didn't notice my fail while filling the 'buffer' LOL

Then the question: how can I do it correctly? Debug swears on "glueing" the filefragments done in the next way. How can I attach binary bytesets into a complete raw data?

 

while not fileIsEOF(hFile) do              -- as long as we're not at the end of the file...
			buffer = buffer..fileRead(hFile, 500)          -- ... read the next 500 bytes...
			                  -- ... and output them to the console
		end
		fileClose(hFile)       

 

Edited by maximumdrive
Link to comment
  • Discord Moderators

Jeez...
Try to use

print("Enbuffer", enBuffer)

print("Debuffer", deBuffer)

 

enbuffer should print base64

but bebuffer should print some binary text(Or idk, never have tried to print out binary in Lua)

Or maybe it fails because of the modelid, try to use other modelid, like 

587

 

Link to comment
On 6/16/2018 at 15:19, maximumdrive said:

Didn't notice my fail while filling the 'buffer' LOL

Then the question: how can I do it correctly? Debug swears on "glueing" the filefragments done in the next way. How can I attach binary bytesets into a complete raw data?

 


while not fileIsEOF(hFile) do              -- as long as we're not at the end of the file...
			buffer = buffer..fileRead(hFile, 500)          -- ... read the next 500 bytes...
			                  -- ... and output them to the console
		end
		fileClose(hFile)       

 

Are you asking how to dump the whole file in one go? Just use fileGetSize like so:

buffer = fileRead(hFile, fileGetSize(hFile))   

 

Edited by MIKI785
Link to comment
  • 1 month later...
  • Discord Moderators
function File.encode(path, key)
    local uncf = File.open(path)
    if (uncf) then
        local encf = File(path..".encoded")
        encf:write(teaEncode(base64Encode(uncf:read(uncf:getSize())), key))
        encf:close()
        uncf:close()
    end
end

function File.readEncoded(path, key)
    local encf = File.open(path..".encoded")
    if (encf) then
        local decoded = base64Decode(teaDecode(encf:read(encf:getSize()), key))
        encf:close()
        return decoded
    end
end

Note: You need to have OOP enabled in meta.xml(<oop>true</oop>)

Edited by Pirulax
  • Like 1
Link to comment
  • Discord Moderators

Actually, maybe I realized what's the problem with the script that @maxiumdrive posted.
 

addEventHandler('onClientResourceStart', resourceRoot,  
    function() 
	local buffer	
	local hFile = fileOpen("model.dff")                       -- attempt to open the file
	if hFile then                                             -- check if it was successfully opened	
		while not fileIsEOF(hFile) do                         -- as long as we're not at the end of the file...
			buffer = buffer..base64Encode(fileRead(hFile, 500))-- ... read the next 500 bytes...
		end
		fileClose(hFile)                                      -- close the file once we're done with it
	else
		outputConsole("Unable to open the model file")
	end 
	local enBuffer = teaEncode(buffer, "xTN5#Gqm=sKn**dF")	
	local deBuffer = base64Decode(teaDecode(enBuffer, "xTN5#Gqm=sKn**dF"))
    engineImportTXD(engineLoadTXD('tex.txd',true) , 596) 
    engineReplaceModel(engineLoadDFF(deBuffer)  , 596) 
    end  
) 

 

Edited by Pirulax
Link to comment

It's actually better to use encodeString and decodeString (added as of 1.5.5 r11849) as these functions allow you to avoid converting Base64 which is less efficient in terms of data storage - length of data n turns into a length of 4*(n/3) in Base64 (33% data increase), plus padding (=) this can add up to even 36.55% increased data use (16000 bytes is 21848 bytes of base64)

encodeString("tea", DATA_TO_ENCODE, {key="your key"})
decodeString("tea", DATA_TO_DECODE, {key="your key"})
-- The following code can be used to test

local f = File.open("some_model.dff", true)
local data = f:read(f.size) -- read all data
f:close()

local key = "xTN5#Gqm=sKn**dF"

local opt1 = teaEncode(base64Encode(data), key)
local opt2 = teaEncode(data, key)
local opt3 = encodeString("tea", base64Encode(data), {key = key})
local opt4 = encodeString("tea", data, {key = key})

outputChatBox("teaEncode with base64: " .. (base64Decode(teaDecode(opt1, key)) == data and "[OK]" or "[FAIL INTEGRITY TEST]") .. "; Encoded length: " .. #opt1)
outputChatBox("teaEncode without base64: " .. (teaDecode(opt2, key) == data and "[OK]" or "[FAIL INTEGRITY TEST]") .. "; Encoded length: " .. #opt2)
outputChatBox("encodeString with base64: " .. (base64Decode(decodeString("tea", opt3, {key = key})) == data and "[OK]" or "[FAIL INTEGRITY TEST]") .. "; Encoded length: " .. #opt3)
outputChatBox("encodeString without base64: " .. (decodeString("tea", opt4, {key = key}) == data and "[OK]" or "[FAIL INTEGRITY TEST]") .. "; Encoded length: " .. #opt4)

 

Edited by MrTasty
  • Like 1
Link to comment
7 hours ago, MrTasty said:

It's actually better to use encodeString and decodeString (added as of 1.5.5 r11849) as these functions allow you to avoid converting Base64 which is less efficient in terms of data storage - length of data n turns into a length of 4*(n/3) in Base64 (33% data increase), plus padding (=) this can add up to even 36.55% increased data use (16000 bytes is 21848 bytes of base64)


encodeString("tea", DATA_TO_ENCODE, {key="your key"})
decodeString("tea", DATA_TO_DECODE, {key="your key"})

-- The following code can be used to test

local f = File.open("some_model.dff", true)
local data = f:read(f.size) -- read all data
f:close()

local key = "xTN5#Gqm=sKn**dF"

local opt1 = teaEncode(base64Encode(data), key)
local opt2 = teaEncode(data, key)
local opt3 = encodeString("tea", base64Encode(data), {key = key})
local opt4 = encodeString("tea", data, {key = key})

outputChatBox("teaEncode with base64: " .. (base64Decode(teaDecode(opt1, key)) == data and "[OK]" or "[FAIL INTEGRITY TEST]") .. "; Encoded length: " .. #opt1)
outputChatBox("teaEncode without base64: " .. (teaDecode(opt2, key) == data and "[OK]" or "[FAIL INTEGRITY TEST]") .. "; Encoded length: " .. #opt2)
outputChatBox("encodeString with base64: " .. (base64Decode(decodeString("tea", opt3, {key = key})) == data and "[OK]" or "[FAIL INTEGRITY TEST]") .. "; Encoded length: " .. #opt3)
outputChatBox("encodeString without base64: " .. (decodeString("tea", opt4, {key = key}) == data and "[OK]" or "[FAIL INTEGRITY TEST]") .. "; Encoded length: " .. #opt4)

 

base64Decode(teaEncode(...))

it's so easy?

Link to comment
11 hours ago, XaskeL said:

base64Decode(teaEncode(...))

it's so easy?

It might be easy, but it takes up CPU during encoding and decoding, and takes up more space in memory (buffer), on disk (when saved) and in transit (downloading). 

decodeString("tea", data_to_decode, {key = "your key"})

is just as easy, and also more efficient.

Edited by MrTasty
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...