Files
GBM/GBM/Managers/mgrXML.vb
2017-11-20 10:54:06 -06:00

114 lines
3.9 KiB
VB.net

Imports GBM.My.Resources
Imports System.Xml.Serialization
Imports System.IO
Imports System.Net
Public Class mgrXML
Public Shared Function ReadMonitorList(ByVal sLocation As String, Optional ByVal bWebRead As Boolean = False) As Hashtable
Dim oList As List(Of Game)
Dim hshList As New Hashtable
Dim hshDupeList As New Hashtable
Dim oGame As clsGame
'If the file doesn't exist return an empty list
If Not File.Exists(sLocation) And Not bWebRead Then
Return hshList
End If
oList = ImportandDeserialize(sLocation, bWebRead)
For Each g As Game In oList
oGame = New clsGame
oGame.Name = g.Name
oGame.ProcessName = g.ProcessName
oGame.AbsolutePath = g.AbsolutePath
oGame.Path = g.Path
oGame.FolderSave = g.FolderSave
oGame.FileType = g.FileType
oGame.ExcludeList = g.ExcludeList
oGame.MonitorOnly = g.MonitorOnly
oGame.Parameter = g.Parameter
oGame.Comments = g.Comments
For Each t As Tag In g.Tags
oGame.ImportTags.Add(t)
Next
'This should be wrapped just in case we get some bad data
Try
hshList.Add(oGame.ProcessName & ":" & oGame.Name, oGame)
Catch e As Exception
'Do Nothing
End Try
Next
Return hshList
End Function
Private Shared Function ReadImportData(ByVal sLocation As String, ByVal bWebRead As Boolean)
Dim oReader As StreamReader
Dim oWebClient As WebClient
If bWebRead Then
oWebClient = New WebClient
oReader = New StreamReader(oWebClient.OpenRead(sLocation))
Else
oReader = New StreamReader(sLocation)
End If
Return oReader
End Function
Public Shared Function ImportandDeserialize(ByVal sLocation As String, Optional ByVal bWebRead As Boolean = False) As List(Of Game)
Dim oReader As StreamReader
Dim oSerializer As XmlSerializer
Dim oExportData As New ExportData
Dim oConfigurations As New List(Of Game)
Try
oReader = ReadImportData(sLocation, bWebRead)
oSerializer = New XmlSerializer(oExportData.GetType(), New XmlRootAttribute("gbm"))
oExportData = oSerializer.Deserialize(oReader)
oReader.Close()
'Compatability Mode
If oExportData.Information.AppVer = 0 Then
oReader = ReadImportData(sLocation, bWebRead)
oSerializer = New XmlSerializer(oConfigurations.GetType(), New XmlRootAttribute("gbm"))
oConfigurations = oSerializer.Deserialize(oReader)
oReader.Close()
Else
oConfigurations = oExportData.Configurations
End If
Catch ex As Exception
mgrCommon.ShowMessage(mgrXML_ErrorImportFailure, ex.Message, MsgBoxStyle.Exclamation)
End Try
Return oConfigurations
End Function
Public Shared Function SerializeAndExport(ByVal oList As List(Of Game), ByVal sLocation As String) As Boolean
Dim oSerializer As XmlSerializer
Dim oWriter As StreamWriter
Dim oExportInformation = New ExportInformation(mgrCommon.DateToUnix(Now), mgrCommon.AppVersion)
Dim oExportData As ExportData
Try
oExportData = New ExportData(oExportInformation, oList)
oSerializer = New XmlSerializer(oExportData.GetType(), New XmlRootAttribute("gbm"))
oWriter = New StreamWriter(sLocation)
oSerializer.Serialize(oWriter.BaseStream, oExportData)
oWriter.Flush()
oWriter.Close()
Return True
Catch ex As Exception
mgrCommon.ShowMessage(mgrXML_ErrorExportFailure, ex.Message, MsgBoxStyle.Exclamation)
Return False
End Try
End Function
End Class