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
+83
View File
@@ -0,0 +1,83 @@
Public Class Game
Private sGameName As String
Private sProcessName As String
Private sPath As String
Private bAbsolutePath As Boolean
Private bFolderSave As Boolean
Private sFileType As String
Private sExcludeList As String
Private oTags As List(Of Tag)
Property Name As String
Set(value As String)
sGameName = value
End Set
Get
Return sGameName
End Get
End Property
Property ProcessName As String
Set(value As String)
sProcessName = value
End Set
Get
Return sProcessName
End Get
End Property
Property Path As String
Set(value As String)
sPath = mgrPath.ReverseSpecialPaths(value)
End Set
Get
Return mgrPath.ReplaceSpecialPaths(sPath)
End Get
End Property
Property AbsolutePath As Boolean
Set(value As Boolean)
bAbsolutePath = value
End Set
Get
Return bAbsolutePath
End Get
End Property
Property FolderSave As Boolean
Set(value As Boolean)
bFolderSave = value
End Set
Get
Return bFolderSave
End Get
End Property
Property FileType As String
Set(value As String)
sFileType = value
End Set
Get
Return sFileType
End Get
End Property
Property ExcludeList As String
Set(value As String)
sExcludeList = value
End Set
Get
Return sExcludeList
End Get
End Property
Property Tags As List(Of Tag)
Get
Return oTags
End Get
Set(value As List(Of Tag))
oTags = value
End Set
End Property
End Class
+13
View File
@@ -0,0 +1,13 @@
Public Class Tag
Private sTagName As String
Property Name As String
Get
Return sTagName
End Get
Set(value As String)
sTagName = value
End Set
End Property
End Class
+25 -15
View File
@@ -18,6 +18,7 @@
Private bDuplicate As Boolean = False Private bDuplicate As Boolean = False
Private sDOSBoxProcess As String = String.Empty Private sDOSBoxProcess As String = String.Empty
Private bTempGame As Boolean = False Private bTempGame As Boolean = False
Private oImportTags As New List(Of Tag)
Property ID As String Property ID As String
Set(value As String) Set(value As String)
@@ -156,40 +157,40 @@
End Set End Set
End Property End Property
Property Enabled As Boolean Property Enabled As Boolean
Set(value As Boolean)
bEnabled = value
End Set
Get Get
Return bEnabled Return bEnabled
End Get End Get
Set(value As Boolean)
bEnabled = value
End Set
End Property End Property
Property MonitorOnly As Boolean Property MonitorOnly As Boolean
Set(value As Boolean)
bMonitorOnly = value
End Set
Get Get
Return bMonitorOnly Return bMonitorOnly
End Get End Get
Set(value As Boolean)
bMonitorOnly = value
End Set
End Property End Property
Property Duplicate As Boolean Property Duplicate As Boolean
Set(value As Boolean)
bDuplicate = value
End Set
Get Get
Return bDuplicate Return bDuplicate
End Get End Get
Set(value As Boolean)
bDuplicate = value
End Set
End Property End Property
Property DOSBoxProcess As String Property DOSBoxProcess As String
Set(value As String)
sDOSBoxProcess = value
End Set
Get Get
Return sDOSBoxProcess Return sDOSBoxProcess
End Get End Get
Set(value As String)
sDOSBoxProcess = value
End Set
End Property End Property
ReadOnly Property TruePath As String ReadOnly Property TruePath As String
@@ -213,6 +214,15 @@
End Set End Set
End Property End Property
Property ImportTags As List(Of Tag)
Get
Return oImportTags
End Get
Set(value As List(Of Tag))
oImportTags = value
End Set
End Property
Public Function SyncEquals(obj As Object) As Boolean Public Function SyncEquals(obj As Object) As Boolean
Dim oGame As clsGame = TryCast(obj, clsGame) Dim oGame As clsGame = TryCast(obj, clsGame)
If oGame Is Nothing Then If oGame Is Nothing Then
+2
View File
@@ -113,6 +113,8 @@
<Import Include="System.Windows.Forms" /> <Import Include="System.Windows.Forms" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Classes\XML Serialize Classes\Tag.vb" />
<Compile Include="Classes\XML Serialize Classes\Game.vb" />
<Compile Include="Classes\clsGameTag.vb" /> <Compile Include="Classes\clsGameTag.vb" />
<Compile Include="Classes\clsPathVariable.vb" /> <Compile Include="Classes\clsPathVariable.vb" />
<Compile Include="Classes\clsBackup.vb" /> <Compile Include="Classes\clsBackup.vb" />
+24 -1
View File
@@ -100,6 +100,29 @@
Return hshList Return hshList
End Function 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 Public Shared Function GetTagsByGameMulti(ByVal sMonitorIDs As List(Of String)) As Hashtable
Dim oDatabase As New mgrSQLite(mgrSQLite.Database.Local) Dim oDatabase As New mgrSQLite(mgrSQLite.Database.Local)
Dim oData As DataSet Dim oData As DataSet
@@ -116,7 +139,7 @@
hshParams.Add("MonitorID" & iCounter, s) hshParams.Add("MonitorID" & iCounter, s)
iCounter += 1 iCounter += 1
Next Next
sSQL = sSQL.TrimEnd(",") sSQL = sSQL.TrimEnd(",")
sSQL &= ")" sSQL &= ")"
+52 -3
View File
@@ -45,6 +45,8 @@ Public Class mgrMonitorList
Dim hshSyncItems As Hashtable Dim hshSyncItems As Hashtable
Dim oFromItem As clsGame Dim oFromItem As clsGame
Dim oToItem As clsGame Dim oToItem As clsGame
Dim oTag As clsTag
Dim oGameTag As clsGameTag
Dim iItems As Integer = 0 Dim iItems As Integer = 0
Cursor.Current = Cursors.WaitCursor Cursor.Current = Cursors.WaitCursor
@@ -74,6 +76,20 @@ Public Class mgrMonitorList
For Each oGame As clsGame In frm.ImportData.Values For Each oGame As clsGame In frm.ImportData.Values
If Not DoDuplicateListCheck(oGame.Name, oGame.TrueProcess) Then If Not DoDuplicateListCheck(oGame.Name, oGame.TrueProcess) Then
DoListAdd(oGame, mgrSQLite.Database.Local) 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 iItems += 1
End If End If
Next Next
@@ -88,12 +104,16 @@ Public Class mgrMonitorList
End Sub End Sub
Public Shared Sub ExportMonitorList(ByVal sLocation As String) 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 Dim bSuccess As Boolean
bSuccess = mgrXML.ExportMonitorList(hshList, sLocation) bSuccess = mgrXML.SerializeAndExport(oList, sLocation)
If bSuccess Then 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 If
End Sub End Sub
@@ -207,6 +227,7 @@ Public Class mgrMonitorList
Return False Return False
End If End If
End If End If
Return True
End Function End Function
Public Shared Function ReadList(ByVal eListType As eListTypes, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) As Hashtable 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 Return hshList
End Function 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) Public Shared Sub DoListAdd(ByVal oGame As clsGame, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local)
Dim oDatabase As New mgrSQLite(iSelectDB) Dim oDatabase As New mgrSQLite(iSelectDB)
Dim sSQL As String Dim sSQL As String
+62 -65
View File
@@ -1,11 +1,12 @@
Imports System.Xml Imports System.Xml.Serialization
Imports System.IO Imports System.IO
Imports System.Text Imports System.Net
Public Class mgrXML Public Class mgrXML
Public Shared Function ReadMonitorList(ByVal sLocation As String, Optional ByVal bWebRead As Boolean = False) As Hashtable 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 hshList As New Hashtable
Dim hshDupeList As New Hashtable Dim hshDupeList As New Hashtable
Dim oGame As clsGame Dim oGame As clsGame
@@ -16,82 +17,78 @@ Public Class mgrXML
Return hshList Return hshList
End If End If
Try oList = ImportandDeserialize(sLocation, bWebRead)
xFileReader = New XmlTextReader(sLocation)
xFileReader.WhitespaceHandling = WhitespaceHandling.None
While (xFileReader.Read()) For Each g As Game In oList
If xFileReader.Name = "app" Then oGame = New clsGame
oGame = New clsGame oGame.Name = g.Name
oGame.Name = xFileReader.GetAttribute("name") oGame.ProcessName = g.ProcessName
xFileReader.Read() oGame.AbsolutePath = g.AbsolutePath
oGame.ProcessName = xFileReader.ReadElementString("process") oGame.Path = g.Path
oGame.AbsolutePath = xFileReader.ReadElementString("absolutepath") oGame.FolderSave = g.FolderSave
oGame.Path = xFileReader.ReadElementString("savelocation") oGame.FileType = g.FileType
oGame.FolderSave = xFileReader.ReadElementString("foldersave") oGame.ExcludeList = g.ExcludeList
oGame.FileType = xFileReader.ReadElementString("filetype") For Each t As Tag In g.Tags
oGame.ExcludeList = xFileReader.ReadElementString("excludelist") oGame.ImportTags.Add(t)
Next
If hshList.Contains(oGame.ProcessName) Or hshDupeList.Contains(oGame.ProcessName) Then If hshList.Contains(oGame.ProcessName) Or hshDupeList.Contains(oGame.ProcessName) Then
oDupeGame = DirectCast(hshList.Item(oGame.ProcessName), clsGame) oDupeGame = DirectCast(hshList.Item(oGame.ProcessName), clsGame)
If Not hshDupeList.Contains(oGame.ProcessName) Then If Not hshDupeList.Contains(oGame.ProcessName) Then
hshDupeList.Add(oGame.ProcessName, oDupeGame) hshDupeList.Add(oGame.ProcessName, oDupeGame)
hshList.Remove(oDupeGame.ProcessName) hshList.Remove(oDupeGame.ProcessName)
oDupeGame.Duplicate = True oDupeGame.Duplicate = True
oDupeGame.ProcessName = oDupeGame.ProcessName & ":" & oDupeGame.Name oDupeGame.ProcessName = oDupeGame.ProcessName & ":" & oDupeGame.Name
hshList.Add(oDupeGame.ProcessName, oDupeGame) hshList.Add(oDupeGame.ProcessName, oDupeGame)
End If
oGame.ProcessName = oGame.ProcessName & ":" & oGame.Name
oGame.Duplicate = True
End If
hshList.Add(oGame.ProcessName, oGame)
End If End If
End While oGame.ProcessName = oGame.ProcessName & ":" & oGame.Name
oGame.Duplicate = True
End If
xFileReader.Close() hshList.Add(oGame.ProcessName, oGame)
Next
'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
Return hshList Return hshList
End Function End Function
Public Shared Function ExportMonitorList(ByVal hshList As Hashtable, ByVal sLocation As String) As Boolean Public Shared Function ImportandDeserialize(ByVal sLocation As String, Optional ByVal bWebRead As Boolean = False) As List(Of Game)
Dim xFileWriter As XmlTextWriter Dim oReader As StreamReader
Dim oWebClient As WebClient
Dim oSerializer As XmlSerializer
Dim oList As New List(Of Game)
Try Try
xFileWriter = New XmlTextWriter(sLocation, System.Text.Encoding.UTF8) If bWebRead Then
xFileWriter.Formatting = Formatting.Indented oWebClient = New WebClient
xFileWriter.WriteStartDocument() oReader = New StreamReader(oWebClient.OpenRead(sLocation))
xFileWriter.WriteComment("GBM Export: " & Date.Now) Else
xFileWriter.WriteComment("Entries: " & hshList.Count) oReader = New StreamReader(sLocation)
xFileWriter.WriteStartElement("aMon") End If
For Each o As clsGame In hshList.Values
xFileWriter.WriteStartElement("app") oSerializer = New XmlSerializer(oList.GetType(), New XmlRootAttribute("gbm"))
xFileWriter.WriteAttributeString("name", o.Name) oList = oSerializer.Deserialize(oReader)
xFileWriter.WriteElementString("process", o.TrueProcess) oReader.Close()
xFileWriter.WriteElementString("absolutepath", o.AbsolutePath) Catch ex As Exception
xFileWriter.WriteElementString("savelocation", o.TruePath) MsgBox("The XML file cannot be read, it may be an invalid format or corrupted." & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "Game Backup Monitor")
xFileWriter.WriteElementString("foldersave", o.FolderSave) End Try
xFileWriter.WriteElementString("filetype", o.FileType)
xFileWriter.WriteElementString("excludelist", o.ExcludeList) Return oList
xFileWriter.WriteEndElement() End Function
Next
xFileWriter.WriteEndElement() Public Shared Function SerializeAndExport(ByVal oList As List(Of Game), ByVal sLocation As String) As Boolean
xFileWriter.WriteEndDocument() Dim oSerializer As XmlSerializer
xFileWriter.Flush() Dim oWriter As StreamWriter
xFileWriter.Close()
Try
oSerializer = New XmlSerializer(oList.GetType(), New XmlRootAttribute("gbm"))
oWriter = New StreamWriter(sLocation)
oSerializer.Serialize(oWriter.BaseStream, oList)
Return True Return True
Catch ex As Exception 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 Return False
End Try End Try
End Function End Function
End Class End Class