Search code examples
jsonstringvb.netserializationjsonresult

How to parse JSON string and check if result contains the text, then ignore or allow it?


I'm working on a Minecraft launcher, I have a problem, because my launcher uses the original way, and for example, here is the JSON:

{
  "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.5"
    },
    {
      "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": "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"
}

So, in the "libraries" [, the game libraries listed with "name":, and below, the libraries contains the rules that apply to them, I want to know this rules with the library file name, and apply the actions with it, but I don't have any idea, I using this code, but this only redirect the libraries.

Here is my code:

 Dim item As String = Version
        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")
        json = JSONREADER.ReadToEnd()
        JSONREADER.Close()
        Dim JSONResult As Object = JsonConvert.DeserializeObject(Of Object)(json)
        MinecraftArgs = JSONResult("minecraftArguments")
        AssetIndex = JSONResult("assets")
        MainClass = JSONResult("mainClass")
        For Each i In JSONResult("libraries").Children()
            LibrariesList.Add(i.ToObject(Of MClibraries).name)
        Next

For example:

{
      "name": "org.lwjgl.lwjgl:lwjgl:2.9.1",
      "rules": [
        {
          "action": "allow"
        },
        {
          "action": "disallow",
          "os": {
            "name": "osx"
          }
        }
      ]
    },

The "name" as the name of the file, the "rules" there is two action: allow, disallow, under the disallow, there is the "os":, this sets the action, because

"os": {
                "name": "osx"
              }

this only allows the file in Windows and Linux, but disallows the file in mac OS X, so I want to get and apply this actions.

I want to add these libraries:

"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.5"
    },
    {
      "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"
    },

And add these libraries if the rules allow Windows operating system:

{
      "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": "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/"
        ]
      }
    }
  ],

If in the rules only disallow, and in the disallow only for example OS X, this means the Windows, and the Linux allowed.


Solution

  • First edit your MClibraries class so that it looks like this:

    Public Class MClibraries
        Public name As String
        Public rules As JArray
    End Class
    

    So to do what you're asking I start off with a list and a dictionary:

    Private ReadOnly _librariesList As New List(Of String)
    Private ReadOnly _disallowed As New Dictionary(Of String, String)
    

    NOTE: You already have _librariesList, but yours is named LibrariesList.

    Next, to get the libraries, I did this:

    Dim item As String = Version
    Dim client = New WebClient() With {.Proxy = Nothing}
    Dim json = Await client.DownloadStringTaskAsync(New Uri(String.Format("http://s3.amazonaws.com/Minecraft.Download/versions/{0}/{0}.json", item)))
    Dim jsonResult As Object = JsonConvert.DeserializeObject(Of Object)(json)
    For Each i In jsonResult("libraries")
        Dim entry As MClibraries = i.ToObject(Of MClibraries)() ' Converts object to MClibrary
        _librariesList.Add(entry.name) ' Add the name of each entry to the listbox
        If Not IsNothing(entry.rules) AndAlso entry.rules.Count = 2 Then ' Check to make sure it isn't empty
            _disallowed.Add(entry.rules(1)("os")("name")) ' Gets the os that is disallowed
        End If
    Next
    

    NOTE: Just add what you already have to this

    Now you can access all the disallowed os's with the new dictionary "_disallowed". The key is the os that is disallowed and the value is the package.

    If you want to see if the os matches the user os you can write the following. Also you will need to add the reference to System.Management and import it as well

    Private _currentOs as String 
    
    'The following should go under the loading of a form
    Dim osname As String = (From x In New ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType(Of ManagementObject)()
                Select x.GetPropertyValue("Caption")).FirstOrDefault()
    
    'The following should go where you retrieve the libraries
    For Each i In jsonResult("libraries")
        Dim entry As MClibraries = i.ToObject(Of MClibraries)() ' Converts object to MClibrary
        If Not IsNothing(entry.rules) AndAlso entry.rules.Count = 2 Then ' Check to make sure it isn't empty
            If Not _currentOs.ToLower.Contains(entry.rules(1)("os")("name")) Then
                _librariesList.Add(entry.name)
            End If
        End If
    Next