Namespace RegionReader
Public Class PackAndRepack
#Region " Clean up "
Public Sub Cleanup()
PokemonDescription.Clear()
PokemonHeight.Clear()
PokemonImage.Clear()
PokemonName.Clear()
PokemonNumber.Clear()
PokemonType.Clear()
PokemonWeight.Clear()
End Sub
#End Region
#Region " Packing Files "
Public Function ReadRegion(ByVal pdxFile As String, ByVal exportLists As Boolean) As Boolean
'The Region Reader will just keep everything in memory, so let's hope we can do this!
'First clear the lists
PokemonName.Clear()
PokemonNumber.Clear()
PokemonHeight.Clear()
PokemonWeight.Clear()
PokemonDescription.Clear()
PokemonType.Clear()
Try
Dim CurrentLine As String = String.Empty
Dim ReadStream As New FileStream(pdxFile, FileMode.Open)
If ReadStream.Length = 0 Then Return False
Dim BR As New BinaryReader(ReadStream)
CurrentLine = BR.ReadString()
Try
Do Until CurrentLine = "[END OF LINE]"
Do Until CurrentLine = "[END OF POKEMON]"
'We are at the start of a section.
'First Line = Pokemon Number - Pokemon Name
'Second Line = Pokemon Type
'Third Line = Junk Data
'Fourth Line = Height:Weight
'Fifth Line = Description, but could be multiline
'Sixth Line = [image]:imagesize
'Seventh Line till [END OF POKEMON] = Image
'Line 1
PokemonNumber.Add(CurrentLine.Split("-")(0))
PokemonName.Add(CurrentLine.Split("-")(1))
CurrentLine = BR.ReadString()
'Line 2
PokemonType.Add(CurrentLine)
CurrentLine = BR.ReadString()
'Line 3, extra crap
CurrentLine = BR.ReadString()
'Line 4
PokemonHeight.Add(CurrentLine.Split(":")(0))
PokemonWeight.Add(CurrentLine.Split(":")(1))
CurrentLine = BR.ReadString()
'Line 5
Dim Desc As String = String.Empty
Do Until CurrentLine.Contains("[image]")
Desc &= CurrentLine
CurrentLine = BR.ReadString()
Loop
PokemonDescription.Add(Desc)
'Line 6, this is the image line.
Dim ByteCount As Integer = CInt(CurrentLine.Split(":")(1))
Dim b() As Byte = BR.ReadBytes(ByteCount)
PokemonImage.Add(byteArrayToImage(b))
Do Until CurrentLine.Contains("[END OF POKEMON]")
CurrentLine = BR.ReadString()
Loop
Loop
CurrentLine = BR.ReadString()
Loop
BR.Close()
ReadStream.Close()
Catch ex As Exception
GC.Collect()
MessageBox.Show(ex.ToString)
End Try
Catch ex As Exception
GC.Collect()
MessageBox.Show(ex.ToString)
End Try
GC.Collect()
If exportLists = True Then Call SaveLists()
Return True
End Function
Public Sub SaveLists()
'This is really easy actually :D
Dim Count As Integer = 0
Dim Amount As Integer = PokemonName.Count
If Amount = 0 Then
MessageBox.Show("No files were unpacked, aborting!", "No files to rewrite.", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
'Now we have files to write
Do While Count < Amount
Using FS As New FileStream(String.Format("{0}.{1}", PokemonName(Count), "pidf"), FileMode.Create)
Using BW As New BinaryWriter(FS)
BW.Write(PokemonNumber(Count) & "-" & PokemonName(Count))
BW.Write(PokemonType(Count))
BW.Write("[description]")
BW.Write(String.Format("{0}:{1}", PokemonHeight(Count), PokemonWeight(Count)))
BW.Write(PokemonDescription(Count))
BW.Write(String.Format("{0}:{1}", "[image]", imageToByteArray(PokemonImage(Count)).Length.ToString))
For Each b As Byte In imageToByteArray(PokemonImage(Count))
BW.Write(b)
Next
End Using
End Using
Count += 1
Loop
End Sub
Public Sub PackFiles(ByVal pdxFileName As String, ByVal sourceDirectory As String)
'We are going to read the files and write them at the same time.
Try
pdxFileName = If(pdxFileName.EndsWith(".pdfx") = True, pdxFileName, String.Format("{0}.{1}", pdxFileName, "pdfx"))
Dim WriteStream As New FileStream(sourceDirectory & "\" & pdxFileName, FileMode.Create)
Dim WriteBS As New BinaryWriter(WriteStream)
For Each f As String In Directory.GetFiles(sourceDirectory, "*.pidf")
Using ReadStream As New FileStream(f, FileMode.Open)
Using ReadBR As New BinaryReader(ReadStream)
WriteBS.Write(ReadBR.ReadBytes(ReadStream.Length))
End Using
'WriteBS.Write(0)
WriteBS.Write("[END OF POKEMON]")
End Using
Next
WriteBS.Write("[END OF LINE]")
WriteBS.Close()
WriteStream.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString, String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
#End Region
#Region " Byte Image Manipulation "
Private Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
Dim ms As New MemoryStream(byteArrayIn)
Return Image.FromStream(ms, True, True)
End Function
Private Function imageToByteArray(ByVal imageIn As System.Drawing.Image) As Byte()
Dim ms As New MemoryStream()
imageIn.Save(ms, imageIn.RawFormat)
Dim B() As Byte = ms.GetBuffer()
Return ms.GetBuffer
End Function
#End Region
End Class
End Namespace