I'm making an asset compiler. Is there a way to dig into a b3d file to figure out what images it's using for textures?
Asset Compiler
Blitz3D Forums/Blitz3D Programming/Asset Compiler Is it a text or a binary file? I've really only worked with text files. Anyone have any simple code that can read data from, or write data to, a b3d file?
B3d files are binary, but open one up in Notepad, and you can see that all of the textures it uses are still just normal text. As for code to read and write b3d files, look in the Specs and Utils link up above, and there's some sample code for reading b3d files. If all you want to do though is extract file names from the b3d file, then you could try something like this:
The 'B3dFile' parameter should be an open file handle (using OpenFile() or ReadFile()). Once it's done, you should have a list of all the files in the B3d file. Just loop through the B3dTexture type to read all of them. Hope that's what you're looking for.
Type B3dTexture Field FileName$ End Type Function GetB3dTextures(B3dFile) While Not Eof(B3dFile) Byte=ReadByte(B3dFile) If Byte > 32 NewName$=NewName$+Chr(Byte) Else If NewName$ <> "" Tex.B3dTexture=New B3dTexture Tex\FileName$=Trim(NewName$) EndIf EndIf Wend For Tex.B3dTexture=Each B3dTexture If Len(Tex\FileName$)=1 Delete Tex Else If Instr(Tex\FileName$,".")=False Delete Tex EndIf EndIf Next End Function
The 'B3dFile' parameter should be an open file handle (using OpenFile() or ReadFile()). Once it's done, you should have a list of all the files in the B3d file. Just loop through the B3dTexture type to read all of them. Hope that's what you're looking for.
I tried that, it seemed to loop for infinity. I did notice it was catching the textue names, which is what I want. Unfortunately, it doesn't seem to end it's loop.
Heres the code I tested with.
; LoadMesh Example
; ----------------
Graphics3D 640,480
SetBuffer BackBuffer()
camera=CreateCamera()
light=CreateLight()
RotateEntity light,90,0,0
; Load mesh
drum=LoadMesh("Objects\NarnFighterRT_Final.B3D")
GetB3dTextures("Objects\NarnFighterRT_Final.B3D")
PositionEntity drum,0,0,MeshDepth(drum)*2
While Not KeyDown( 1 )
RenderWorld
Flip
Wend
End
Type B3dTexture
Field FileName$
End Type
Function GetB3dTextures(B3dFilename$)
Stop
B3dFile= ReadFile(B3dFilename$)
While Not Eof(B3dFile)
Byte=ReadByte(B3dFile)
If Byte > 32
NewName$=NewName$+Chr(Byte)
Else
If NewName$ <> ""
Tex.B3dTexture=New B3dTexture
Tex\FileName$=Trim(NewName$)
DebugLog Newname
EndIf
EndIf
Wend
For Tex.B3dTexture=Each B3dTexture
If Len(Tex\FileName$)=1
Delete Tex
Else
If Instr(Tex\FileName$,".")=False
Delete Tex
EndIf
EndIf
Next
CloseFile B3DFile
End Function
Heres the code I tested with.
; LoadMesh Example
; ----------------
Graphics3D 640,480
SetBuffer BackBuffer()
camera=CreateCamera()
light=CreateLight()
RotateEntity light,90,0,0
; Load mesh
drum=LoadMesh("Objects\NarnFighterRT_Final.B3D")
GetB3dTextures("Objects\NarnFighterRT_Final.B3D")
PositionEntity drum,0,0,MeshDepth(drum)*2
While Not KeyDown( 1 )
RenderWorld
Flip
Wend
End
Type B3dTexture
Field FileName$
End Type
Function GetB3dTextures(B3dFilename$)
Stop
B3dFile= ReadFile(B3dFilename$)
While Not Eof(B3dFile)
Byte=ReadByte(B3dFile)
If Byte > 32
NewName$=NewName$+Chr(Byte)
Else
If NewName$ <> ""
Tex.B3dTexture=New B3dTexture
Tex\FileName$=Trim(NewName$)
DebugLog Newname
EndIf
EndIf
Wend
For Tex.B3dTexture=Each B3dTexture
If Len(Tex\FileName$)=1
Delete Tex
Else
If Instr(Tex\FileName$,".")=False
Delete Tex
EndIf
EndIf
Next
CloseFile B3DFile
End Function
Sorry, ignore the stop commend, I forget to take that out before I posted.