Code for converting DeleD bmp files + material references

Share your advanced PureBasic knowledge/code with the community.
Godai
Enthusiast
Enthusiast
Posts: 171
Joined: Thu Oct 05, 2006 8:13 pm

Code for converting DeleD bmp files + material references

Post by Godai »

Code updated For 5.20+

Simple compile as an exe and drop it into your export directory.
After run all BMPs + their references inside the material file will be converted to png.
A backup is kept of the material files, as well as the original files, although a quick batch job should get rid of those :)

Sure beats doing it manually.

Code: Select all

; Convert all BMP images to PNG and convert all references in all material files.

; Forward declare functions
Declare ConvertBMP(filename$)
Declare ConvertMaterialBMPEntries(filename$)

; Use PNG encoder
UsePNGImageEncoder() 

; Attempt to open console
If (OpenConsole())

  ; A little text
  PrintN("Please stand by as we convert your stuff(tm)")
  PrintN("")
  
  ; Go through current directory
  Directory$ = "."   
  
  ; First to the BMP's
  If ExamineDirectory(0, Directory$, "*.*")  
    While NextDirectoryEntry(0)
      If DirectoryEntryType(0) = #PB_DirectoryEntry_File
      
        ; Check which file it is
        Select Right(DirectoryEntryName(0), 3)
        
          Case "bmp"
          
            ConvertBMP(DirectoryEntryName(0))
            
          Case "ial" ; <- LOL :)
          
            ConvertMaterialBMPEntries(DirectoryEntryName(0))
            
         EndSelect
         
        EndIf
                  
    Wend
    
    FinishDirectory(0)
    
  EndIf

EndIf

; Convert BMP to PNG
Procedure ConvertBMP(filename$)
        
  PrintN("Converting file '" + filename$ + "' to PNG.")
  
  If LoadImage(0, filename$)
  
          If SaveImage(0, Left(filename$, Len(filename$)-4)+".png", #PB_ImagePlugin_PNG) = 0     
          
            PrintN("Error saving image '"+ filename$ + "'")
          
          Else
          
            PrintN("Succesfully converted '" + filename$ + "'")
            
          EndIf
  Else
  
    PrintN("Error loading image '"+ filename$ + "'")
  
  EndIf
  
EndProcedure

; Convert BMP entries in material
Procedure ConvertMaterialBMPEntries(filename$)

  ; Create backup (no error check mind you :)
  CopyFile(filename$, filename$ + "$$$")
  
  PrintN("Converting material file '" + filename$ + "'")
  
  ; Open material
  If OpenFile(0, filename$+"$$$")
  
    ; Create new material file
    CreateFile(1, filename$)
    
    ; Replace
    While Not Eof(0)
    
      temp$ = ReadString(0, #PB_Ascii)
      If FindString(temp$,"bmp", 0) <> 0
      
        temp$ = ReplaceString(temp$, "bmp", "png")        
                                
      EndIf
      
      ; Write to file
      WriteString(1, temp$+Chr(13)+Chr(10), #PB_Ascii)
    
    Wend
  
    CloseFile(1)
    CloseFile(0)
    
    PrintN("Succesfully converted material file '" + filename$ + "'")
  
  Else
  
    PrintN("Error opening material file '" + filename$ + "'")
  
  EndIf

EndProcedure


[/code]