'---------------------------------------------------------------'
' DecompressDungeon.vbs                                         '
' Version 1.1 - April 11, 2009                                  '
' Written by Christophe Fontanel                                '
'                                                               '
' Please visit The Dungeon Master & Chaos Strikes Back          '
' Encyclopaedia at http://dmweb.free.fr/ for more information   '
'---------------------------------------------------------------'

' 1.1: Automatic output file name


Option Explicit
'On Error Resume Next

Const ForReading = 1, ForWriting = 2

Dim ScriptArguments, FileSystemObject, SourceFileName, SourceFile, SourceFileSize, DestinationFileName, DestinationFile, DestinationFileContent
Dim i, FileSignature, Table(19), UnCompressedSize, CompressedDungeon, Index, BitsInBuffer, BitBuffer


Function GetBits(Number)
	If BitsInBuffer < Number Then
		BitBuffer = BitBuffer * 2^8 + (Asc(Mid(CompressedDungeon, Index + 1, 1)))
		BitsInBuffer = BitsInBuffer + 8
		Index = Index + 1
	End If
	GetBits = BitBuffer \ 2^(BitsInBuffer - Number)
	BitBuffer = BitBuffer And (2^(BitsInBuffer - Number) - 1)
	BitsInBuffer = BitsInBuffer - Number
End Function


Set ScriptArguments = WScript.Arguments
If ScriptArguments.Count = 0 Then
	MsgBox 	"This tool uncompresses compressed dungeons." & vbCrLf & vbCrLf & _
		"Usage example: at a command prompt, type the following command line:" & vbCrLf & _
		"   UnCompressDungeon.vbs DUNGEON.DAT" & vbCrLf & _
		"You can also drag and drop a dungeon file on UnCompressDungeon.vbs in Windows Explorer." & vbCrLf & vbCrLf &  _
		"The uncompressed file will be saved in the script's folder.", vbOKOnly + vbInformation, "UnCompressDungeon Help"
Else
	Set FileSystemObject = CreateObject("Scripting.FileSystemObject")

	SourceFileName = ScriptArguments(0)
	Set SourceFile = FileSystemObject.GetFile(SourceFileName)
	SourceFileSize = SourceFile.Size
	Set SourceFile = FileSystemObject.OpenTextFile(SourceFileName, ForReading)

	FileSignature = Asc(SourceFile.Read(1)) + 256 * Asc(SourceFile.Read(1))
	If FileSignature = 33028 Then	' 8104h
		' Little Endian
		UnCompressedSize = Asc(SourceFile.Read(1)) + 256 * Asc(SourceFile.Read(1)) + 65536 * Asc(SourceFile.Read(1)) + 16777216 * Asc(SourceFile.Read(1))
	Else
		If FileSignature = 1153  Then	' 0481h
			' Big Endian
			UnCompressedSize = 16777216 * Asc(SourceFile.Read(1)) + 65536 * Asc(SourceFile.Read(1)) + 256 * Asc(SourceFile.Read(1)) + Asc(SourceFile.Read(1))
		Else
			MsgBox "Invalid dungeon file. The dungeon may already be uncompressed.", vbOKOnly + vbCritical, "UnCompressDungeon Error"
		End If
	End If

	SourceFile.Read(2)

	For i = 0 To 19
		Table(i) = SourceFile.Read(1)
	Next

	CompressedDungeon = SourceFile.Read(SourceFileSize - 28)

	BitBuffer = 0			' The BitBuffer is empty
	BitsInBuffer = 0		' So there are no bits stored in the buffer
	Index = 0
	DestinationFileContent = ""
	While Len(DestinationFileContent) < UnCompressedSize
		If GetBits(1) = 0 Then
			DestinationFileContent = DestinationFileContent & Table(GetBits(2))
		Else
			If GetBits(1) = 0 Then
				DestinationFileContent = DestinationFileContent & Table(GetBits(4) + 4)
			Else
				DestinationFileContent = DestinationFileContent & Chr(GetBits(8))
			End If
		End If
	Wend
	SourceFile.Close

	' Build destination file name with full path
	DestinationFileName = SourceFileName & ".UNCOMPRESSED"
	
	Err.Clear
	Set DestinationFile = FileSystemObject.OpenTextFile(DestinationFileName, ForWriting, True)
	If Err.Number <> 0 Then
		MsgBox "Unable to create the destination file: " & DestinationFileName & vbCrLf & Err.Description, vbOKOnly + vbCritical, "UnCompressedDungeon Error"
	Else
		Err.Clear
		DestinationFile.Write(DestinationFileContent)
		If Err.Number <> 0 Then
			MsgBox "Unable to write the destination file." & vbCrLf & Err.Description, vbOKOnly + vbCritical, "UnCompressedDungeon Error"
		Else
			MsgBox "The file " & vbCrLf & vbCrLf & DestinationFileName & vbCrLf & vbCrLf & " was successfully created.", vbOKOnly + vbInformation, "UnCompressedDungeon Information"
		End If
		DestinationFile.Close
	End If
End If
