Imports System.IO
Imports System.Xml
Public Class frmMain
Dim FS As FileStream, BR As BinaryReader, BW As BinaryWriter, j As UInt16
Dim startoffset As Integer = 204004
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strFileName As String
With OpenFileDialog1
.Reset()
.Filter = "Cricket 2007 roster file|*.ros"
If .ShowDialog() = Windows.Forms.DialogResult.Cancel Then Me.Close()
strFileName = .FileName
End With
If strFileName = vbNullString Then Exit Sub
If Not IsNothing(FS) Then
BR.Close()
BW.Close()
FS.Close()
FS = Nothing
End If
cboplayername.Items.Clear()
FS = New FileStream(strFileName, FileMode.Open, FileAccess.ReadWrite)
BR = New BinaryReader(FS)
BW = New BinaryWriter(FS)
Dim chrPlayer(14) As Char, x As Integer, shtid As Short
For x = 0 To 1051 '1052 players
FS.Seek((startoffset + 4 + x * 360), SeekOrigin.Begin)
chrPlayer = BR.ReadChars(14)
cboplayername.Items.Add(Convert.ToString(chrPlayer))
FS.Seek((startoffset + x * 360), SeekOrigin.Begin)
shtid = BR.ReadInt16
cboplayerid.Items.Add(shtid)
Next
cboplayername.SelectedIndex = 0
cboplayerid.SelectedIndex = 0
txtplayerid.Text = cboplayerid.Text
'loading XML file
Dim strAppPath As String
strAppPath = IIf(Strings.Right(Application.StartupPath, 1) = "\", Application.StartupPath, Application.StartupPath + "\")
Dim XDocument As XmlDocument, XNodeList As XmlNodeList, XNode As XmlNode
XDocument = New XmlDocument
Try
XDocument.Load(strAppPath + "PlayerNames.xml")
Catch When Err.Number = 53 'If File Not Found Error
MsgBox("XML file not found" & vbCrLf & "Please make sure PlayerNames.xml is in the root directory", MessageBoxIcon.Error)
Me.Close()
End Try
XNodeList = XDocument.SelectNodes("/PlayerNames/Player")
For Each XNode In XNodeList
Dim genderAttribute As String = XNode.Attributes.GetNamedItem("ID").Value
Dim firstNameValue As String = XNode.Attributes.GetNamedItem("Name").Value
lstxmlplayid.Items.Add(genderAttribute)
lstxmlplayname.Items.Add(firstNameValue)
Next
replacenames()
End Sub
Private Sub replacenames()
Dim xmltotalnumber As Integer = lstxmlplayid.Items.Count, x, a As Integer, z As String
For x = 0 To (xmltotalnumber - 1)
a = cboplayerid.FindStringExact(lstxmlplayid.Items.Item(x))
If a = -1 Then
MsgBox("PlayerID not found for " & lstxmlplayname.Items.Item(x) & vbCrLf & "All names have not been replaced", MsgBoxStyle.Exclamation)
Me.Close()
End If
Dim chrPlayer(13), clrplayer(13) As Char
Dim s As String
If lstxmlplayname.Items.Item(x).Length > 14 Then
MsgBox(lstxmlplayname.Items.Item(x) & " is too long. It will be reduced to 14 characters.", MsgBoxStyle.Information)
lstxmlplayname.Items.Item(x) = Microsoft.VisualBasic.Left(lstxmlplayname.Items.Item(x), 14)
End If
s = lstxmlplayname.Items.Item(x) & New String(Chr(0), (14 - lstxmlplayname.Items.Item(x).Length))
chrPlayer = s.ToCharArray()
FS.Seek(startoffset + a * 360 + 4, SeekOrigin.Begin)
BW.Write(chrPlayer)
z = lstxmlplayname.Items.Item(x)
getnames(z)
If j <> 0 Then
FS.Seek(startoffset + a * 360 + 2, SeekOrigin.Begin)
BW.Write(j)
End If
Next
MsgBox("Names successfully replaced", MsgBoxStyle.Information)
Me.Close()
End Sub