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


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

mods来自"%appdata% roaming spaceEngineers mods"。" C: setools uncacked_mods "到一个由整数命名的文件夹中,该文件夹等于提取的数字,因此第一个mod为0秒为1,依此类推。

>

典型的mod将其主要数据文件像这样" c: setools unvacked_mods 0 data cubeblocks.sbc",并且内部数据也在xml中。

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

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

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

所以我的问题是,我该如何编写一个函数来执行此操作而不为每个mod编写if函数,我基本上想列出一份香草块的列表,并且确实喜欢一个if blockname = to the the the列表的香草块然后运行MODS功能的支票。

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

Public Sub LoadModDefinitions()
    Dim modpath As String = "C:SEToolsunpacked_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

^这将所有改装的块信息加载到列表中。

和所讨论的功能是下面的功能,我如何在修改模块数据列表中搜索每个数据块,找出哪个包含输入" subtypesearch"&amp;返回该文件内容的特定部分,该文件内容是"one_answers"/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:SEToolsunpacked_mods") Then
            Try
                Directory.CreateDirectory("C:SEToolsunpacked_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:SEToolsActivated.txt") Then
            Try
                File.CreateText("C:SEToolsActivated.txt")
                'Unpack all mods
                unpackAll(sepath, extractto)
            Catch ex As Exception
            End Try
        End If
    Catch ex As Exception
    End Try
End Sub

当前似乎所有文件都在毫无问题的情况下读取到字符串列表中,但我不知道从哪里开始搜索功能。我已经写了一个函数,已经为我以前从事搜索文本框而设计的,并且正在考虑尝试在这里实施它,但是我仍然会担心如何在图标标签之间返回文本绳子分开,但我不确定任何指针吗?

当前搜索功能

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 deSerializer序列化,我为回答了另一个问题。

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

此类对CubeBlocks文件进行了将定义定义词典的字典供您参见。

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获取该块。

相关内容

  • 没有找到相关文章

最新更新