交叉引用 xml 已作为字符串加载到列表中的文件,将它们分开并从中检索特定数据

Cross reference xml files that have been loaded into a list as strings, break them apart and retrieve specific data from them

Main Form Code: https://pastebin.com/31gjpzm6 < Outdated
Blueprint Input Code W/ Mods: https://pastebin.com/KsF2vAWw < Outdated

来自“%AppData%\Roaming\SpaceEngineers\Mods”的模组被提取到 "C:\SETools\unpacked_mods\" 到一个以整数命名的文件夹中,该整数等于提取的数字,因此第一个 mod 是 0,第二个是 1,依此类推。

一个典型的 mod 将有它的主数据文件 "C:\SETools\unpacked_mods[=39=]\data\CubeBlocks.sbc" 并且内部数据也在 XML 中。

CubeBlocks.sbc Mod Info File: https://pastebin.com/UhhGtEhN

所以我想做的是交叉引用蓝图文件中的 modded 子类型 ID 和所有 mod 文件中的子类型 ID,并检查它们是否匹配,如果匹配则抓取他们的 DDS 格式的图标并将其发送到已经在上面的 Form1 代码中生成的图片框。我想通过检索字符串的路径来做到这一点。

Screenshot: http://imgur.com/a/3bIfN

所以我的问题是我如何编写一个函数来执行此操作而不为每个 mod 编写一个 IF 函数,基本上我想制作一个 vanilla 块类型的列表并且像 if blockname 一样= 原版方块列表中的任何项目,然后 运行 检查 mod 的功能。

到目前为止我得到了什么:

Public Sub LoadModDefinitions()
    Dim modpath As String = "C:\SETools\unpacked_mods\"

    For Each entry In Directory.GetFiles(modpath, "CubeBlocks.sbc") 'For each zipfilename in the given directory extract them to the given folder
        Dim filereader As StreamReader = New StreamReader(modpath)
        For Each block In entry
            ModdedCubeblockDefinitions.Add(filereader.ReadToEnd(modpath))
        Next block
    Next
End Sub

^ 这会将所有 modded 块信息加载到列表中。

而有问题的功能是下面的那个我如何搜索 modded 块数据列表中的每个数据块,找出哪个包含输入 "subtypesearch" & return 该文件内容的特定块,它是“”和“/icon”之间的文件路径

Public Function SearchModdedCubeDefinitions(subtypesearch As String) 'WIP WIP WIP WIP WIP WIP
    Dim BlockToSearch As String = subtypesearch

    Return Nothing
End Function

并自动提取 mods 个文件

'Check if program has been ran before and extract all mods if it hasnt
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load
    Try
        'Check if the program directory exists & if not then create it
        If Not Directory.Exists("C:\SETools") Then
            Try
                Directory.CreateDirectory("C:\SETools")
            Catch ex As Exception
            End Try
        End If
        'Check if the mod extraction directory exists in the C drive & if not then create it
        If Not Directory.Exists("C:\SETools\unpacked_mods") Then
            Try
                Directory.CreateDirectory("C:\SETools\unpacked_mods")
            Catch ex As Exception
            End Try
        End If
        'Check if the file that determins whether or not the program has been run before exists or not & if not then create it
        If Not File.Exists("C:\SETools\Activated.txt") Then
            Try
                File.CreateText("C:\SETools\Activated.txt")
                'Unpack all mods
                unpackAll(sepath, extractto)
            Catch ex As Exception
            End Try
        End If
    Catch ex As Exception
    End Try
End Sub

目前似乎所有文件都被毫无问题地读入字符串列表,但我什至不知道从哪里开始使用搜索功能。我已经为我之前正在做的事情编写了一个函数来搜索文本框,并且正在考虑尝试在这里实现它,但我仍然会关心如何 return 图标标签之间的文本,我是考虑使用正则表达式字符串拆分,但我不太确定任何指针?

当前搜索功能

Private Function FindWords(ByVal TextSearched As String, ByVal Paragraph As String) As Integer
    Dim location As Integer = 0
    Dim occurances As Integer = 0
    Do
        location = TextSearched.IndexOf(Paragraph, location)
        If location <> -1 Then
            occurances += 1
            location += Paragraph.Length
        End If
    Loop Until location = -1
    Return occurances
End Function

使用序列化解决问题

XMLCleaner.vb

Public Class XMLCleaner
    Public Function CleanXML(inputfile As String)
        Dim originalxml As String = inputfile
        Dim outputxml As String = Nothing

        'only keep requested xml lines and remove xsi:type from <definition> element
        For Each line In originalxml.Split(vbNewLine)
            If line.ToString().Contains("<?xml") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("<Definitions xmlns:xsi") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("<CubeBlocks>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("<Definition xsi:type") Then
                outputxml += "<Definition>" + vbNewLine
            ElseIf line.ToString().Contains("<Definition>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("<Id>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("</Id>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("<TypeId>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("<SubtypeId>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("<DisplayName>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("<Icon>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("<CubeSize>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("<BlockPairName>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("<BuildTimeSeconds>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("<Components>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("<Component Subtype") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("</Components>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("</Definition>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("</CubeBlocks>") Then
                outputxml += line.ToString() + vbNewLine
            ElseIf line.ToString().Contains("</Definitions>") Then
                outputxml += line.ToString() + vbNewLine
            End If
        Next

        Return outputxml
    End Function
End Class

此方法仅从每个 mod 的 cubeblocks 文件中提取需要的数据,使其可由我为回答另一个问题而制作的通用 cubeblocks 反序列化器序列化。

CubeBlocksSerializer.vb

Option Strict Off
Option Explicit On

Imports System.Xml.Serialization

<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.7.3081.0"),
 System.SerializableAttribute(),
 System.Diagnostics.DebuggerStepThroughAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True),
 System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)>
Partial Public Class Definitions
    Private cubeBlocksField() As DefinitionsDefinition
    <System.Xml.Serialization.XmlArrayItemAttribute("Definition", IsNullable:=False)>
    Public Property CubeBlocks() As DefinitionsDefinition()
        Get
            Return Me.cubeBlocksField
        End Get
        Set
            Me.cubeBlocksField = Value
        End Set
    End Property
End Class

<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.7.3081.0"),
 System.SerializableAttribute(),
 System.Diagnostics.DebuggerStepThroughAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)>
Partial Public Class DefinitionsDefinition
    Private idField As DefinitionsDefinitionID
    Private displayNameField As String
    Private iconField As String
    Private cubeSizeField As String
    Private componentsField() As DefinitionsDefinitionComponent
    Private blockPairNameField As String
    Private buildTimeSecondsField As Integer

    Public Property Id() As DefinitionsDefinitionID
        Get
            Return Me.idField
        End Get
        Set
            Me.idField = Value
        End Set
    End Property

    Public Property DisplayName() As String
        Get
            Return Me.displayNameField
        End Get
        Set
            Me.displayNameField = Value
        End Set
    End Property

    Public Property Icon() As String
        Get
            Return Me.iconField
        End Get
        Set
            Me.iconField = Value
        End Set
    End Property

    Public Property CubeSize() As String
        Get
            Return Me.cubeSizeField
        End Get
        Set
            Me.cubeSizeField = Value
        End Set
    End Property

    <System.Xml.Serialization.XmlArrayItemAttribute("Component", IsNullable:=False)>
    Public Property Components() As DefinitionsDefinitionComponent()
        Get
            Return Me.componentsField
        End Get
        Set
            Me.componentsField = Value
        End Set
    End Property

    Public Property BlockPairName() As String
        Get
            Return Me.blockPairNameField
        End Get
        Set
            Me.blockPairNameField = Value
        End Set
    End Property

    Public Property BuildTimeSeconds() As Integer
        Get
            Return Me.buildTimeSecondsField
        End Get
        Set
            Me.buildTimeSecondsField = Value
        End Set
    End Property
End Class

<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.7.3081.0"),
 System.SerializableAttribute(),
 System.Diagnostics.DebuggerStepThroughAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)>
Partial Public Class DefinitionsDefinitionID
    Private typeIdField As String
    Private subtypeIdField As String

    Public Property TypeId() As String
        Get
            Return Me.typeIdField
        End Get
        Set
            Me.typeIdField = Value
        End Set
    End Property

    Public Property SubtypeId() As String
        Get
            Return Me.subtypeIdField
        End Get
        Set
            Me.subtypeIdField = Value
        End Set
    End Property
End Class

<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.7.3081.0"),
 System.SerializableAttribute(),
 System.Diagnostics.DebuggerStepThroughAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)>
Partial Public Class DefinitionsDefinitionComponent
    Private subtypeField As String
    Private countField As Integer
    Private countFieldSpecified As Boolean

    <System.Xml.Serialization.XmlAttributeAttribute()>
    Public Property Subtype() As String
        Get
            Return Me.subtypeField
        End Get
        Set
            Me.subtypeField = Value
        End Set
    End Property

    <System.Xml.Serialization.XmlAttributeAttribute()>
    Public Property Count() As Integer
        Get
            Return Me.countField
        End Get
        Set
            Me.countField = Value
        End Set
    End Property

    <System.Xml.Serialization.XmlIgnoreAttribute()>
    Public Property CountSpecified() As Boolean
        Get
            Return Me.countFieldSpecified
        End Get
        Set
            Me.countFieldSpecified = Value
        End Set
    End Property
End Class

这 class 将 cubeblocks 文件反序列化为 DefinitionsDefinition class 的字典,见下文。

Main.vb

Public ModBlockDefinitionDictionary As New Dictionary(Of String, DefinitionsDefinition)

Public Sub LoadModDefinitions()
        Dim modpath As String = My.Settings.SpaceEngineersWorkingDirectory
        Dim id As Integer = 0
        Dim Cleaner As New XMLCleaner()

        For Each folder In Directory.GetDirectories(modpath)
            Dim folder_complete As String = folder.ToString() + "\Data"

            For Each file In Directory.GetFiles(folder_complete, "CubeBlocks.sbc") 'For each zipfilename in the given directory extract them to the given folder
                Dim moddedxml As String = Cleaner.CleanXML(System.IO.File.ReadAllText(file))
                Dim DefinitionData As New Definitions()
                Dim xmlSerializer As XmlSerializer = New XmlSerializer(GetType(Definitions))

                Try
                    Dim streamread As New StringReader(moddedxml)
                    DefinitionData = xmlSerializer.Deserialize(streamread)
                Catch ex As Exception

                End Try

                For Each BlockDefinition In DefinitionData.CubeBlocks()
                    Try
                        If Not ModBlockDefinitionDictionary(BlockDefinition.Id.SubtypeId.ToString()) Is Nothing Then
                            'Block already exists dont add it
                        Else
                            ModBlockDefinitionDictionary.Add(BlockDefinition.Id.SubtypeId.ToString(), BlockDefinition)
                        End If
                    Catch ex As Exception

                    End Try
                Next
            Next

            id += 1
        Next
    End Sub

然后对于每个要检查的 mod 方块,您可以使用其子类型 ID 作为键从 ModBlockDefintionsDictonary 中获取该方块。