[TOOL] Creating DataSection code from small binary

Advanced game related topics
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

[TOOL] Creating DataSection code from small binary

Post by miso »

I put this tool here. This creates a ready to use datasection code text from a binary. I use it only for game examples to add little .png-s to my code.
Uses the debugoutput window, created code can be copied from there.

Code: Select all

;-MODULE DECLARATION
EnableExplicit
DeclareModule datasec
Declare create(filename.s)  
EndDeclareModule

;-MODULE START
Module datasec
  #MAXDATA_IN_A_ROW = 35
  ;--PUBLIC PROCEDURES
  Procedure create(filename.s)
    If Not #PB_Compiler_Debugger : End : EndIf
    Protected fileh.i,counter.i=0,mybyte.a,outputstring.s = "DataSection"+#CRLF$+"mydata_start:"+#CRLF$
    fileh = ReadFile(#PB_Any,filename.s)
    If Not IsFile(fileh) : Debug "Could not open file "+filename.s : End : EndIf
    Repeat
      If counter = 0 : outputstring + "Data.a ": EndIf
      mybyte = ReadAsciiCharacter(fileh)
      outputstring+"$"+RSet(Hex(mybyte.a,#PB_Ascii),2,"0")
      If counter<>#MAXDATA_IN_A_ROW And Not Eof(fileh) :outputstring+"," : Else :outputstring+#CRLF$ : counter = -1 : EndIf
      counter+1
    Until Eof(fileh)
    CloseFile(fileh)
    outputstring+"mydata_end:"+#CRLF$+"EndDataSection"
    Debug outputstring
  EndProcedure
EndModule

;-EXAMPLE USAGE
datasec::create("mysprite.png")

This version creates quad data (file should be at least a quad (8 byte) long):

Code: Select all

;-MODULE DECLARATION
EnableExplicit
DeclareModule QuadData
  Declare Create(filename.s)
EndDeclareModule
;-MODULE START
Module QuadData
  #MAXDATA_IN_A_ROW = 8
  ;--PUBLIC PROCEDURES
  ;***************************************
  ;Reads a file and creates datasection
  ;code from it using quads to be small.
  ;Output result goes to the debug window.
  ;***************************************
  Procedure Create(filename.s)
    Protected fileh.i , quads.i, remainder.i, output.s, counter.i , i.i , j.i
    Protected Dim Values.a(7)  
    If Not #PB_Compiler_Debugger : End: EndIf
    If FileSize(filename.s)<1 : Debug "File not found or empty." : End : EndIf
    quads = FileSize(filename)/8
    remainder = Mod(FileSize(filename),8)
    fileh = ReadFile(#PB_Any,filename)
    output + "DataSection"+#CRLF$
    For i = 1 To quads
      If counter = 0 : output+"Data.q " : EndIf
      For j = 0 To 7
          values(j) = ReadAsciiCharacter(fileh)
      Next j
      output + "$"
        For j = 7 To 0 Step - 1
          output + RSet(Hex(values(j),#PB_Ascii),2,"0")
        Next j
      If counter <> #MAXDATA_IN_A_ROW And i <> quads: output + "," : Else : output + #CRLF$ : counter = -1 : EndIf
      counter + 1
    Next i
    If Not Eof(fileh) : output + "Data.a ": EndIf
    While Not Eof(fileh)
      output + "$" + RSet(Hex(ReadAsciiCharacter(fileh),#PB_Ascii),2,"0")
      If Not Eof(fileh)
        output + ","
      EndIf
    Wend
    output + #CRLF$ + "EndDataSection"
    Debug output
  EndProcedure
EndModule

;-EXAMPLE USAGE (obviously a valid filename is needed)
quaddata::create("myzip.zip")
Last edited by miso on Thu May 15, 2025 12:11 pm, edited 4 times in total.
Axolotl
Addict
Addict
Posts: 835
Joined: Wed Dec 31, 2008 3:36 pm

Re: [TOOL] Creating DataSection code from small binary

Post by Axolotl »

Thanks for sharing.
Great work.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Little John
Addict
Addict
Posts: 4787
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: [TOOL] Creating DataSection code from small binary

Post by Little John »

miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: [TOOL] Creating DataSection code from small binary

Post by miso »

NIce. Lot of great stuff can be found on this forum, I often miss them.
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: [TOOL] Creating DataSection code from small binary

Post by Caronte3D »

Also... for images (and more) supported by PB you can simply include it, no need to make a data:

Code: Select all

IncludeBinary "mysprite.png"
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: [TOOL] Creating DataSection code from small binary

Post by miso »

IncludeBinary "mysprite.png"
For me include is for this forum. To not to download small media, but copypaste and run.
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: [TOOL] Creating DataSection code from small binary

Post by Caronte3D »

Good point! :wink:
AZJIO
Addict
Addict
Posts: 2187
Joined: Sun May 14, 2017 1:48 am

Re: [TOOL] Creating DataSection code from small binary

Post by AZJIO »

Little John wrote: Tue Mar 04, 2025 7:42 pm I use this:
https://www.purebasic.fr/english/viewtopic.php?t=49196
To be precise, then here is the link
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: [TOOL] Creating DataSection code from small binary

Post by miso »

I was also testing the packers+uncompressing in memory, but I use small 16x16 pngs with a few colors, already so compressed, that any of the packers resulted in a bigger data because of the pack headers. Might be good for other kind of data though.
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: [TOOL] Creating DataSection code from small binary

Post by miso »

Updated first post with a version that creates .q quad data.
Post Reply