Namespace IndividualFile
Public Class Saver
Private m_FileName As String = ""
Public PKMNumber As Integer = -1
Public PKMName As String = ""
Public PKMType As String = ""
Public PKMDescription As String = ""
Public PKMImage As Image
Public PKMHeight As String
Public PKMWidth As String
Public Sub New(ByVal filename As String)
m_FileName = filename
End Sub
Public Sub Save()
If IsInvalid() Then Throw New ArgumentException()
Dim fs As New FileStream(m_FileName, FileMode.OpenOrCreate, FileAccess.Write)
Dim br As New BinaryWriter(fs)
br.Write(Me.PKMNumber & "-" & Me.PKMName)
br.Write(Me.PKMType)
br.Write("[description]")
br.Write(String.Format("{0}:{1}", PKMHeight, PKMWidth))
br.Write(Me.PKMDescription)
br.Write(String.Format("{0}:{1}", "[image]", imageToByteArray(Me.PKMImage).Length.ToString))
For Each b As Byte In imageToByteArray(Me.PKMImage)
br.Write(b)
Next
br.Close()
End Sub
Private Function IsInvalid() As Boolean
Select Case True
Case Me.PKMNumber <= 0, Me.PKMName = "", Me.PKMType = "", m_FileName = "", Me.PKMImage Is Nothing
Return True
End Select
Return False
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 Class
Public Class Opener
Private m_FileName As String = ""
Public PKMNumber As Integer = -1
Public PKMName As String = ""
Public PKMType As String = ""
Public PKMDescription As String = ""
Public PKMImage As Image
Public PKMNHeight As String
Public PKMNWidth As String
Public Sub New(ByVal filename As String)
m_FileName = filename
End Sub
Public Function Open() As Boolean
If IsInvalid() Then Throw New ArgumentException()
Dim FS As New FileStream(m_FileName, FileMode.Open)
If FS.Length = 0 Then
MessageBox.Show("This file is empty.")
Return False
End If
Dim BR As New BinaryReader(FS)
'First Line = Pokemon Number - Pokemon Name
PKMName = BR.ReadString()
'The Pokemon name contains two things: Number-Name
Dim CHolder() As String = PKMName.Split("-")
PKMNumber = CInt(CHolder(0))
PKMName = CHolder(1)
'Second Line = Pokemon Type
PKMType = BR.ReadString()
'Third Line = Junk Data
BR.ReadString()
'Fourth Line = Height:Weight
Dim tempData() As String = BR.ReadString.Split(":")
PKMNHeight = tempData(0)
PKMNWidth = tempData(1)
'Fifth Line = Description, but could be multiline
Dim CurrLine As String = BR.ReadString()
Do Until CurrLine.Contains("[image]")
PKMDescription &= CurrLine
Try
CurrLine = BR.ReadString
Catch ex As Exception
Exit Do 'We're outside the loop.....so rawr
End Try
Loop
'So now the current line is [image], split it to get the number of bytes to read.
Dim BytesRead() As String = CurrLine.Split(":")
Dim BytesToRead As Integer = CInt(BytesRead(1))
'Now we have the position...
'Now read the rest of the file!
Dim imgbytes() As Byte
imgbytes = BR.ReadBytes(BytesToRead)
Me.PKMImage = byteArrayToImage(imgbytes)
BR.Close()
Return True
End Function
Private Function IsInvalid() As Boolean
Return Not (m_FileName <> "" AndAlso IO.File.Exists(m_FileName))
End Function
Private Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
Dim ms As New MemoryStream(byteArrayIn)
Return Image.FromStream(ms, True, True)
End Function
End Class
End Namespace