Rewrote import / export

This commit is contained in:
Michael J. Seiferling
2015-11-14 01:14:44 -06:00
parent 5357fb214d
commit ebf5fb5f4d
7 changed files with 261 additions and 84 deletions
+24 -1
View File
@@ -100,6 +100,29 @@
Return hshList
End Function
Public Shared Function GetTagsByGameForExport(ByVal sMonitorID As String) As List(Of Tag)
Dim oDatabase As New mgrSQLite(mgrSQLite.Database.Local)
Dim oData As DataSet
Dim sSQL As String
Dim oList As New List(Of Tag)
Dim hshParams As New Hashtable
Dim oTag As Tag
sSQL = "SELECT TagID, tags.Name FROM gametags NATURAL JOIN tags WHERE MonitorID = @ID"
hshParams.Add("ID", sMonitorID)
oData = oDatabase.ReadParamData(sSQL, hshParams)
For Each dr As DataRow In oData.Tables(0).Rows
oTag = New Tag
oTag.Name = CStr(dr(1))
oList.Add(oTag)
Next
Return oList
End Function
Public Shared Function GetTagsByGameMulti(ByVal sMonitorIDs As List(Of String)) As Hashtable
Dim oDatabase As New mgrSQLite(mgrSQLite.Database.Local)
Dim oData As DataSet
@@ -116,7 +139,7 @@
hshParams.Add("MonitorID" & iCounter, s)
iCounter += 1
Next
sSQL = sSQL.TrimEnd(",")
sSQL &= ")"
+52 -3
View File
@@ -45,6 +45,8 @@ Public Class mgrMonitorList
Dim hshSyncItems As Hashtable
Dim oFromItem As clsGame
Dim oToItem As clsGame
Dim oTag As clsTag
Dim oGameTag As clsGameTag
Dim iItems As Integer = 0
Cursor.Current = Cursors.WaitCursor
@@ -74,6 +76,20 @@ Public Class mgrMonitorList
For Each oGame As clsGame In frm.ImportData.Values
If Not DoDuplicateListCheck(oGame.Name, oGame.TrueProcess) Then
DoListAdd(oGame, mgrSQLite.Database.Local)
'Handle Tag Import (TODO: This could use some optimization, way too many DB hits.)
For Each t As Tag In oGame.ImportTags
If mgrTags.DoCheckDuplicate(t.Name) Then
oTag = mgrTags.DoTagGetbyName(t.Name)
Else
oTag = New clsTag
oTag.Name = t.Name
mgrTags.DoTagAdd(oTag)
End If
oGameTag = New clsGameTag
oGameTag.MonitorID = oGame.ID
oGameTag.TagID = oTag.ID
mgrGameTags.DoGameTagAdd(oGameTag)
Next
iItems += 1
End If
Next
@@ -88,12 +104,16 @@ Public Class mgrMonitorList
End Sub
Public Shared Sub ExportMonitorList(ByVal sLocation As String)
Dim hshList As Hashtable = ReadList(eListTypes.FullList, mgrSQLite.Database.Local)
'Dim hshList As Hashtable = ReadList(eListTypes.FullList, mgrSQLite.Database.Local)
'Dim bSuccess As Boolean
'bSuccess = mgrXML.ExportMonitorList(hshList, sLocation)
Dim oList As List(Of Game) = ReadListForExport()
Dim bSuccess As Boolean
bSuccess = mgrXML.ExportMonitorList(hshList, sLocation)
bSuccess = mgrXML.SerializeAndExport(oList, sLocation)
If bSuccess Then
MsgBox("Export Complete. " & hshList.Count & " entries have been exported.", MsgBoxStyle.Information, "Game Backup Monitor")
MsgBox("Export Complete. " & oList.Count & " entries have been exported.", MsgBoxStyle.Information, "Game Backup Monitor")
End If
End Sub
@@ -207,6 +227,7 @@ Public Class mgrMonitorList
Return False
End If
End If
Return True
End Function
Public Shared Function ReadList(ByVal eListType As eListTypes, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) As Hashtable
@@ -272,6 +293,34 @@ Public Class mgrMonitorList
Return hshList
End Function
Public Shared Function ReadListForExport(Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) As List(Of Game)
Dim oDatabase As New mgrSQLite(iSelectDB)
Dim oData As DataSet
Dim sSQL As String
Dim sID As String
Dim oList As New List(Of Game)
Dim oGame As Game
sSQL = "SELECT * from monitorlist ORDER BY Name Asc"
oData = oDatabase.ReadParamData(sSQL, New Hashtable)
For Each dr As DataRow In oData.Tables(0).Rows
oGame = New Game
sID = CStr(dr(0))
oGame.Name = CStr(dr(1))
oGame.ProcessName = CStr(dr(2))
If Not IsDBNull(dr(3)) Then oGame.Path = CStr(dr(3))
oGame.AbsolutePath = CBool(dr(4))
oGame.FolderSave = CBool(dr(5))
If Not IsDBNull(dr(6)) Then oGame.FileType = CStr(dr(6))
If Not IsDBNull(dr(8)) Then oGame.ExcludeList = CStr(dr(8))
oGame.Tags = mgrGameTags.GetTagsByGameForExport(sID)
oList.Add(oGame)
Next
Return oList
End Function
Public Shared Sub DoListAdd(ByVal oGame As clsGame, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local)
Dim oDatabase As New mgrSQLite(iSelectDB)
Dim sSQL As String
+62 -65
View File
@@ -1,11 +1,12 @@
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Imports System.Text
Imports System.Net
Public Class mgrXML
Public Shared Function ReadMonitorList(ByVal sLocation As String, Optional ByVal bWebRead As Boolean = False) As Hashtable
Dim xFileReader As XmlTextReader
Dim oList As List(Of Game)
Dim hshList As New Hashtable
Dim hshDupeList As New Hashtable
Dim oGame As clsGame
@@ -16,82 +17,78 @@ Public Class mgrXML
Return hshList
End If
Try
xFileReader = New XmlTextReader(sLocation)
xFileReader.WhitespaceHandling = WhitespaceHandling.None
oList = ImportandDeserialize(sLocation, bWebRead)
While (xFileReader.Read())
If xFileReader.Name = "app" Then
oGame = New clsGame
oGame.Name = xFileReader.GetAttribute("name")
xFileReader.Read()
oGame.ProcessName = xFileReader.ReadElementString("process")
oGame.AbsolutePath = xFileReader.ReadElementString("absolutepath")
oGame.Path = xFileReader.ReadElementString("savelocation")
oGame.FolderSave = xFileReader.ReadElementString("foldersave")
oGame.FileType = xFileReader.ReadElementString("filetype")
oGame.ExcludeList = xFileReader.ReadElementString("excludelist")
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
For Each t As Tag In g.Tags
oGame.ImportTags.Add(t)
Next
If hshList.Contains(oGame.ProcessName) Or hshDupeList.Contains(oGame.ProcessName) Then
oDupeGame = DirectCast(hshList.Item(oGame.ProcessName), clsGame)
If Not hshDupeList.Contains(oGame.ProcessName) Then
hshDupeList.Add(oGame.ProcessName, oDupeGame)
hshList.Remove(oDupeGame.ProcessName)
oDupeGame.Duplicate = True
oDupeGame.ProcessName = oDupeGame.ProcessName & ":" & oDupeGame.Name
hshList.Add(oDupeGame.ProcessName, oDupeGame)
End If
oGame.ProcessName = oGame.ProcessName & ":" & oGame.Name
oGame.Duplicate = True
End If
hshList.Add(oGame.ProcessName, oGame)
If hshList.Contains(oGame.ProcessName) Or hshDupeList.Contains(oGame.ProcessName) Then
oDupeGame = DirectCast(hshList.Item(oGame.ProcessName), clsGame)
If Not hshDupeList.Contains(oGame.ProcessName) Then
hshDupeList.Add(oGame.ProcessName, oDupeGame)
hshList.Remove(oDupeGame.ProcessName)
oDupeGame.Duplicate = True
oDupeGame.ProcessName = oDupeGame.ProcessName & ":" & oDupeGame.Name
hshList.Add(oDupeGame.ProcessName, oDupeGame)
End If
End While
oGame.ProcessName = oGame.ProcessName & ":" & oGame.Name
oGame.Duplicate = True
End If
xFileReader.Close()
'We need to trigger a manual garbage collection here to prevent issues with the reader freezing up with multiple uses.
'There's no way to properly dispose a xml text reader in .NET 4, that's only fixed in 4.5+.
GC.Collect()
Catch ex As Exception
MsgBox("An error occured reading the monitor list import file." & vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "Game Backup Monitor")
End Try
hshList.Add(oGame.ProcessName, oGame)
Next
Return hshList
End Function
Public Shared Function ExportMonitorList(ByVal hshList As Hashtable, ByVal sLocation As String) As Boolean
Dim xFileWriter As XmlTextWriter
Public Shared Function ImportandDeserialize(ByVal sLocation As String, Optional ByVal bWebRead As Boolean = False) As List(Of Game)
Dim oReader As StreamReader
Dim oWebClient As WebClient
Dim oSerializer As XmlSerializer
Dim oList As New List(Of Game)
Try
xFileWriter = New XmlTextWriter(sLocation, System.Text.Encoding.UTF8)
xFileWriter.Formatting = Formatting.Indented
xFileWriter.WriteStartDocument()
xFileWriter.WriteComment("GBM Export: " & Date.Now)
xFileWriter.WriteComment("Entries: " & hshList.Count)
xFileWriter.WriteStartElement("aMon")
For Each o As clsGame In hshList.Values
xFileWriter.WriteStartElement("app")
xFileWriter.WriteAttributeString("name", o.Name)
xFileWriter.WriteElementString("process", o.TrueProcess)
xFileWriter.WriteElementString("absolutepath", o.AbsolutePath)
xFileWriter.WriteElementString("savelocation", o.TruePath)
xFileWriter.WriteElementString("foldersave", o.FolderSave)
xFileWriter.WriteElementString("filetype", o.FileType)
xFileWriter.WriteElementString("excludelist", o.ExcludeList)
xFileWriter.WriteEndElement()
Next
xFileWriter.WriteEndElement()
xFileWriter.WriteEndDocument()
xFileWriter.Flush()
xFileWriter.Close()
If bWebRead Then
oWebClient = New WebClient
oReader = New StreamReader(oWebClient.OpenRead(sLocation))
Else
oReader = New StreamReader(sLocation)
End If
oSerializer = New XmlSerializer(oList.GetType(), New XmlRootAttribute("gbm"))
oList = oSerializer.Deserialize(oReader)
oReader.Close()
Catch ex As Exception
MsgBox("The XML file cannot be read, it may be an invalid format or corrupted." & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "Game Backup Monitor")
End Try
Return oList
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
Try
oSerializer = New XmlSerializer(oList.GetType(), New XmlRootAttribute("gbm"))
oWriter = New StreamWriter(sLocation)
oSerializer.Serialize(oWriter.BaseStream, oList)
Return True
Catch ex As Exception
MsgBox("An error occured exporting the monitor list. " & ex.Message, MsgBoxStyle.Exclamation, "Game Backup Monitor")
MsgBox("An error occured exporting the XML data." & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "Game Backup Monitor")
Return False
End Try
End Function
End Class