December 03, 2020, 08:19:24 PM

Author Topic: [bb] Blitz Resource Compiler v0.3 by superqix [ 1+ years ago ]  (Read 604 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Blitz Resource Compiler v0.3
Author : superqix
Posted : 1+ years ago

Description : BASE64 encode a file for "include"ing into your Blitz project for later retrival.  Perfect for adding small graphics or sounds in your Blitz EXE file.

Updated to include multiple files in one resource, single point of entry, and Win32 GUI for adding files.

Grab the full package (including a Win32 GUI and source) here:
<a href="http://turbo.gamedev.net/blitzres03.zip" target="_blank">http://turbo.gamedev.net/blitzres03.zip[/url]


Code :
Code: BlitzBasic
  1. ; ******************************************
  2. ; * Blltz Resource Compiler v0.3
  3. ; ******************************************
  4. ; * BASE64 encode a file into BB source to
  5. ; * add to a Blitz project for retrival
  6. ; *
  7. ; * Perfect for including small graphics or
  8. ; * sounds in your Blitz EXE file
  9. ; ******************************************
  10. ; * Released under the LPGL 9/03/2002 by
  11. ; * Michael Wilson wilson(at)no2games.com
  12. ; ******************************************
  13.  
  14. ; ******************************************
  15. ; * About:
  16. ; *
  17. ; * These functions will create a new *.bb
  18. ; * Blitz source file with a BASE64-like
  19. ; * encoded version of the files added with
  20. ; * ResourceAddFile() and encoded with
  21. ; * ResourceEncodeAll().
  22. ; *
  23. ; * The unencoding function LoadResource()
  24. ; * is appended to the top of the new
  25. ; * *.bb file, ready to be included in your
  26. ; * project
  27. ; *
  28. ; * Since I'm using text encodeing the file
  29. ; * will take up 133% the space of the
  30. ; * original, so I'd limit it's use to
  31. ; * ????k of resource files, but I don't
  32. ; * know what the practical limit is.
  33. ; ******************************************
  34.  
  35. Type ResourceFile
  36.  Field FileName$
  37. End Type
  38.  
  39. ; ******************************************
  40. ; * Change EncodeTable$ to encrypt your
  41. ; * files, Must be 64 unique characters,
  42. ; ******************************************
  43.  
  44. Global EncodeTable$ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz{}"
  45. If Len(EncodeTable$) <> 64 Then RuntimeError("Encode Table must be 64 unique characters ("+Len(EncodeTable$)+")")
  46.  
  47. ; ******************************************
  48. ; * Function ResourceAddFile(FileName$)
  49. ; ******************************************
  50.  
  51. Function ResourceAddFile(FileName$)
  52.     Resources.ResourceFile = New ResourceFile
  53.     ResourcesFileName$ = FileName$
  54. End Function
  55.  
  56. ; ******************************************
  57. ; * Function ResourceEncodeAll(OutputFile$)
  58. ; ******************************************
  59.  
  60. Function ResourceEncodeAll(OutputFile$)
  61.        
  62.         OutFile = WriteFile(OutputFile$)
  63.         If OutFile = 0 Then RuntimeError("Can not make output file.")
  64.        
  65.         WriteLine OutFile,"Type TempResourceFile"
  66.         WriteLine OutFile,"  Field FileName$"
  67.         WriteLine OutFile,"End Type"                   
  68.         WriteLine OutFile,""
  69.  
  70.         WriteLine OutFile,"Function FreeResources()"
  71.         WriteLine OutFile,"  For TempFiles.TempResourceFile = Each TempResourceFile "
  72.         WriteLine OutFile,"  DeleteFile TempFilesFileName$ "
  73.         WriteLine OutFile,"  Delete TempFiles "
  74.         WriteLine OutFile,"  Next "
  75.         WriteLine OutFile,"End Function "
  76.         WriteLine OutFile,""
  77.  
  78.         WriteLine OutFile,"Function LoadResource$(Resource$)"
  79.         WriteLine OutFile,"  Select Upper$(Resource$)"
  80.     For Resources.ResourceFile = Each ResourceFile
  81.                 ResourceFile$ = Upper$(ExtractFilename$(ResourcesFileName$))
  82.                 ResourceName$ = ExtractFilename$(Replace(ResourcesFileName$,".","_"))
  83.                 WriteLine OutFile,"    Case " + Chr$(34) + ResourceFile$ + Chr$(34) +" Restore "+ResourceName$
  84.     Next
  85.         WriteLine OutFile,"  End Select"                       
  86.         WriteLine OutFile,"  EncodeTable$ = " + Chr$(34) + EncodeTable$ + Chr$(34)
  87.         WriteLine OutFile,"  TempFile$ = Str Int Rnd(11111,99999)"
  88.         WriteLine OutFile,"  TempFile$ = SystemProperty ("+ Chr$(34) +"tempdir" + Chr$(34) +")+" + Chr$(34) +"~bb" + Chr$(34) + "+TempFile$+" + Chr$(34) + ".tmp" + Chr$(34)
  89.         WriteLine OutFile,"  OutFile = WriteFile(TempFile$)"
  90.         WriteLine OutFile,"   Repeat"
  91.         WriteLine OutFile,"     Read Output$"
  92.         WriteLine OutFile,"     If Output$ = "+ Chr$(34) +"!EOF"+ Chr$(34) +" Then Exit"
  93.     WriteLine OutFile,"     ByteCount% = Len(Output$)"
  94.     WriteLine OutFile,"     For i = 1 To ByteCount% Step 4"
  95.     WriteLine OutFile,"       Word% = ((Instr(EncodeTable$,Mid$(Output$,i,1)) - 1) And 63) Shl 18"
  96.     WriteLine OutFile,"       Word = Word + (((Instr(EncodeTable$,Mid$(Output$,i+1,1)) - 1) And 63) Shl 12)"
  97.     WriteLine OutFile,"       Word = Word + (((Instr(EncodeTable$,Mid$(Output$,i+2,1)) - 1) And 63) Shl 6)"
  98.     WriteLine OutFile,"       Word = Word + ((Instr(EncodeTable$,Mid$(Output$,i+3,1)) - 1) And 63)"
  99.     WriteLine OutFile,"       Byte% = Word Shr 16 And 255"
  100.     WriteLine OutFile,"       Byte2% = Word Shr 8 And 255"
  101.     WriteLine OutFile,"       Byte3% = Word And 255"
  102.     WriteLine OutFile,"       WriteByte OutFile, Byte"
  103.     WriteLine OutFile,"       WriteByte OutFile, Byte2"
  104.     WriteLine OutFile,"       WriteByte OutFile, Byte3"
  105.     WriteLine OutFile,"     Next"
  106.         WriteLine OutFile,"   Forever"
  107.         WriteLine OutFile,"  CloseFile OutFile"
  108.         WriteLine OutFile,"  TempFiles.TempResourceFile = New TempResourceFile"
  109.         WriteLine OutFile,"  TempFilesFileName$ = TempFile$"
  110.         WriteLine OutFile,"  Return TempFile$"
  111.         WriteLine OutFile,"End Function"
  112.         WriteLine OutFile,""
  113.        
  114.     For Resources.ResourceFile = Each ResourceFile
  115.                 InputFile$ = ResourcesFileName$
  116.                 ResourceName$ = ExtractFilename$(Replace(ResourcesFileName$,".","_"))
  117.                 InFile = ReadFile(InputFile$)
  118.                 WriteLine OutFile,""
  119.                 WriteLine OutFile,"." + ResourceName$
  120.                 ByteCount% = 0
  121.                 While Not Eof(InFile)
  122.                         Byte% = ReadByte(InFile)
  123.                         Byte2% = ReadByte(InFile)
  124.                         Byte3% = ReadByte(InFile)
  125.                         Word% = (Byte Shl 16) Or (Byte2 Shl 8) Or Byte3                
  126.                         ; BASE 64 Encode
  127.                         Output$ = Output$ + Mid$(EncodeTable$,((Word Shr 18) + 1) ,1)
  128.                         Output$ = Output$ + Mid$(EncodeTable$,(((Word Shr 12) And 63) + 1) ,1)
  129.                         Output$ = Output$ + Mid$(EncodeTable$,(((Word Shr 6) And 63) + 1) ,1)
  130.                         Output$ = Output$ + Mid$(EncodeTable$,((Word And 63) + 1) ,1)
  131.                         ByteCount = ByteCount + 3
  132.                         If ByteCount >= 64 Then
  133.                                 WriteLine OutFile, "Data " + Chr$(34) + Output$ + Chr$(34)
  134.                                 ByteCount = 0
  135.                                 Output$ = ""
  136.                         End If
  137.                 Wend
  138.                 If Output$ <> "" Then WriteLine OutFile, "Data " + Chr$(34) + Output$ + Chr$(34)
  139.                 WriteLine OutFile, "Data " + Chr$(34) + "!EOF" + Chr$(34)
  140.                 Output$ = ""
  141.                 CloseFile InFile
  142.     Next        
  143.         CloseFile OutFile
  144.        
  145. End Function
  146.  
  147. ; ******************************************
  148. ; * Function ExtractFilename$(FileName$)
  149. ; ******************************************
  150.  
  151. Function ExtractFilename$(FileName$)
  152.  
  153.         FileName$=""+FileName$
  154.         LastBackSlash = 1
  155.         Repeat
  156.                 NextBackSlash = Instr(filename$,"",LastBackSlash)
  157.                 If NextBackSlash > 0 Then LastBackSlash = NextBackSlash + 1
  158.         Until NextBackSlash = 0
  159.         tmp$ = Right$(FileName$,Len(FileName$)-LastBackSlash+1)
  160.         Return tmp$
  161.  
  162. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal