vb.net转换字符串,一次下载文件,多次

时间:2014-11-26 15:09:37

标签: vb.net string download

我正在创建一个Minecraft Launcher。此启动器使用json文件格式来保存设置,加载可用版本,加载库,资产文件,jar文件和更新游戏。 要解析json文件,这个启动器使用Newtonsoft.json。我对所有这些json文件都有一个大问题(你可以查看版本列表:https://s3.amazonaws.com/Minecraft.Download/versions/versions.json。),例如:

这是其中之一:(1.8.1.json,我的启动器使用它来检查库文件是否存在,此json中列出的库文件,列表以行开头:"libraries": [

{
  "id": "1.8.1",
  "time": "2014-11-24T14:13:31+00:00",
  "releaseTime": "2014-11-24T14:13:31+00:00",
  "type": "release",
  "minecraftArguments": "--username ${auth_player_name} --version ${version_name} --gameDir ${game_directory} --assetsDir ${assets_root} --assetIndex ${assets_index_name} --uuid ${auth_uuid} --accessToken ${auth_access_token} --userProperties ${user_properties} --userType ${user_type}",
  "minimumLauncherVersion": 14,
  "assets": "1.8",
  "libraries": [
    {
      "name": "com.ibm.icu:icu4j-core-mojang:51.2"
    },
    {
      "name": "net.sf.jopt-simple:jopt-simple:4.6"
    },
    {
      "name": "com.paulscode:codecjorbis:20101023"
    },
    {
      "name": "com.paulscode:codecwav:20101023"
    },
    {
      "name": "com.paulscode:libraryjavasound:20101123"
    },
    {
      "name": "com.paulscode:librarylwjglopenal:20100824"
    },
    {
      "name": "com.paulscode:soundsystem:20120107"
    },
    {
      "name": "io.netty:netty-all:4.0.23.Final"
    },
    {
      "name": "com.google.guava:guava:17.0"
    },
    {
      "name": "org.apache.commons:commons-lang3:3.3.2"
    },
    {
      "name": "commons-io:commons-io:2.4"
    },
    {
      "name": "commons-codec:commons-codec:1.9"
    },
    {
      "name": "net.java.jinput:jinput:2.0.5"
    },
    {
      "name": "net.java.jutils:jutils:1.0.0"
    },
    {
      "name": "com.google.code.gson:gson:2.2.4"
    },
    {
      "name": "com.mojang:authlib:1.5.17"
    },
    {
      "name": "com.mojang:realms:1.7.3"
    },
    {
      "name": "org.apache.commons:commons-compress:1.8.1"
    },
    {
      "name": "org.apache.httpcomponents:httpclient:4.3.3"
    },
    {
      "name": "commons-logging:commons-logging:1.1.3"
    },
    {
      "name": "org.apache.httpcomponents:httpcore:4.3.2"
    },
    {
      "name": "org.apache.logging.log4j:log4j-api:2.0-beta9"
    },
    {
      "name": "org.apache.logging.log4j:log4j-core:2.0-beta9"
    },
    {
      "name": "org.lwjgl.lwjgl:lwjgl:2.9.1",
      "rules": [
        {
          "action": "allow"
        },
        {
          "action": "disallow",
          "os": {
            "name": "osx"
          }
        }
      ]
    },
    {
      "name": "org.lwjgl.lwjgl:lwjgl_util:2.9.1",
      "rules": [
        {
          "action": "allow"
        },
        {
          "action": "disallow",
          "os": {
            "name": "osx"
          }
        }
      ]
    },
    {
      "name": "org.lwjgl.lwjgl:lwjgl-platform:2.9.1",
      "natives": {
        "linux": "natives-linux",
        "windows": "natives-windows",
        "osx": "natives-osx"
      },
      "extract": {
        "exclude": [
          "META-INF/"
        ]
      },
      "rules": [
        {
          "action": "allow"
        },
        {
          "action": "disallow",
          "os": {
            "name": "osx"
          }
        }
      ]
    },
    {
      "name": "org.lwjgl.lwjgl:lwjgl:2.9.2-nightly-20140822",
      "rules": [
        {
          "action": "allow",
          "os": {
            "name": "osx"
          }
        }
      ]
    },
    {
      "name": "org.lwjgl.lwjgl:lwjgl_util:2.9.2-nightly-20140822",
      "rules": [
        {
          "action": "allow",
          "os": {
            "name": "osx"
          }
        }
      ]
    },
    {
      "name": "org.lwjgl.lwjgl:lwjgl-platform:2.9.2-nightly-20140822",
      "natives": {
        "linux": "natives-linux",
        "windows": "natives-windows",
        "osx": "natives-osx"
      },
      "extract": {
        "exclude": [
          "META-INF/"
        ]
      },
      "rules": [
        {
          "action": "allow",
          "os": {
            "name": "osx"
          }
        }
      ]
    },
    {
      "name": "net.java.jinput:jinput-platform:2.0.5",
      "natives": {
        "linux": "natives-linux",
        "windows": "natives-windows",
        "osx": "natives-osx"
      },
      "extract": {
        "exclude": [
          "META-INF/"
        ]
      }
    },
    {
      "name": "tv.twitch:twitch:6.5"
    },
    {
      "name": "tv.twitch:twitch-platform:6.5",
      "rules": [
        {
          "action": "allow"
        },
        {
          "action": "disallow",
          "os": {
            "name": "linux"
          }
        }
      ],
      "natives": {
        "linux": "natives-linux",
        "windows": "natives-windows-${arch}",
        "osx": "natives-osx"
      },
      "extract": {
        "exclude": [
          "META-INF/"
        ]
      }
    },
    {
      "name": "tv.twitch:twitch-external-platform:4.5",
      "rules": [
        {
          "action": "allow",
          "os": {
            "name": "windows"
          }
        }
      ],
      "natives": {
        "windows": "natives-windows-${arch}"
      },
      "extract": {
        "exclude": [
          "META-INF/"
        ]
      }
    }
  ],
  "mainClass": "net.minecraft.client.main.Main"
}

我想将json文件中的库转换为:<package>/<name>/<version>/<name>-<version>.jar,例如: 库文件详细信息:"name": "tv.twitch:twitch-external-platform:4.5",转换为

电视\抽搐\抽搐外部平台\抽搐外部平台-4.5.jar

然后,下载网址=&#34; https://libraries.minecraft.net/package/name/version/name-version.jar&#34;

<package> = tv
<name> = twitch
<version> = 4.5
<name>-<version>.jar = twitch-external-platform-4.5.jar

并将此转换后的字符串添加到字符串列表中,然后使用webclient下载文件(列在字符串列表中),并一次下载一个,并保存到文件路径,文件路径如下:root + &#34; \ libraries \ package \ name \ version \ name-version.jar。(当下载另一个文件时,下载路径总是在变化 (Dim root as string = Application.StartUpPath

1 个答案:

答案 0 :(得分:0)

    'Convert Download String:

        Public Function ConvertStringDownloadable(ByVal name As String, ByVal natives As Dictionary(Of String, String), ByVal native As Integer) As String
                Dim newName As String
                Dim version = Regex.Match(name, "(\d+\.?){1,}(-?\w+)*$", RegexOptions.Multiline).Value
                Dim typeName As String = Regex.Match(name.Remove(name.LastIndexOf(":"c)), "(-?\w+)+$", RegexOptions.Multiline).Value
                newName = Regex.Replace(name.Replace(version, ""), "[:.]", "/")
                If (IsNothing(natives)) Then
                    newName = String.Format("{0}{2}/{1}-{2}.jar", _
                                            newName, typeName, version)
                Else
                    newName = String.Format("{0}{2}/{1}-{2}-{3}.jar", _
                                            newName, typeName, version, natives.Values(native))
                End If
                If newName.Contains("${arch}") Then
                    newName = newName.Replace("${arch}", "64")
                End If
                Return newName
            End Function

    'Convert Launch String:

    Public Function ConvertStringLaunchable(ByVal name As String, ByVal natives As Dictionary(Of String, String), ByVal native As Integer) As String
            Dim newName As String
            Dim version = Regex.Match(name, "(\d+\.?){1,}(-?\w+)*$", RegexOptions.Multiline).Value
            Dim typeName As String = Regex.Match(name.Remove(name.LastIndexOf(":"c)), "(-?\w+)+$", RegexOptions.Multiline).Value
            newName = Regex.Replace(name.Replace(version, ""), "[:.]", "\")
            If (IsNothing(natives)) Then
                newName = String.Format("{0}{2}\{1}-{2}.jar", _
                                        newName, typeName, version)
            Else
                newName = String.Format("{0}{2}\{1}-{2}-{3}.jar", _
                                        newName, typeName, version, natives.Values(native))
            End If
            If newName.Contains("${arch}") Then
                newName = newName.Replace("${arch}", "64")
            End If
            Return newName
        End Function

'Load library files:

 Public Async Function LoadLibraries() As Task
        Dim item As String = SelectedGameVersion
        Dim client As New WebClient()
        Await client.DownloadFileTaskAsync(New Uri("https://s3.amazonaws.com/Minecraft.Download/versions/" + item + "/" + item + ".json"), Root + "\versions\" + item + "\" + item + ".json")
        Dim JSONREADER As New StreamReader(Root + "\versions\" + item + "\" + item + ".json")
        Dim JSON As String = JSONREADER.ReadToEnd()
        JSONREADER.Close()
        Dim JSONResult = JsonConvert.DeserializeObject(Of Object)(JSON)
        For Each i In JSONResult("libraries").Children()
            temp.ListView1.Items.Add(i.ToObject(Of libraries).name)
        Next
    End Function

为了获取值,我创建了一个字符串列表值列表:

 Dim librariesDownloadString As New List(Of String)
 Dim librariesLaunchString As New List(Of String)

要向列表添加值:

  For Each item1 As ListViewItem In temp.ListView1.Items
            librariesDownloadString.Add(ConvertStringDownloadable(item1.Text, Nothing, 0))
        Next

        For Each item2 As ListViewItem In temp.ListView1.Items
            librariesLaunchString.Add(ConvertStringLaunchable(item2.Text, Nothing, 0))
        Next