VB.Net 自定义 Minecraft 启动器

VB.Net Custom Minecraft Launcher

我想为我和我在 VB.Net 的朋友创建我自己的 Minecraft Launcher。我有这段代码可以给我访问令牌。

Private ACCESS_TOKEN As String
Public Function GetAccessToken() As String
    Return ACCESS_TOKEN
End Function
Public Sub ObtainAccessToken()
    Dim username As String = TextBox1.Text
    Dim password As String = TextBox2.Text
    Dim UUID As String = Guid.NewGuid.ToString()
    Dim httpWebRequest = DirectCast(WebRequest.Create("https://authserver.mojang.com/authenticate"), HttpWebRequest)
    httpWebRequest.ContentType = "application/json"
    httpWebRequest.Method = "POST"

    Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
        Dim json As String = (Convert.ToString((Convert.ToString("{""agent"":{""name"":""Minecraft"",""version"":1},""username"":""") & username) + """,""password"":""") & password) + """,""clientToken"":" & ControlChars.Quote & UUID & ControlChars.Quote & "}"

        streamWriter.Write(json)
        streamWriter.Flush()
        streamWriter.Close()

        Dim httpResponse = DirectCast(httpWebRequest.GetResponse(), HttpWebResponse)
        Using streamReader = New StreamReader(httpResponse.GetResponseStream())
            Dim result = streamReader.ReadToEnd()
            ACCESS_TOKEN = result
        End Using
    End Using
End Sub

但是我可以用这个访问令牌做什么?如何直接使用 java 参数启动我的世界?我想启动 minecraft.jar 文件。

在 .minecraft 文件夹中,所有参数都在 version.json

这是 1.8.8 版 json 的片段 (.minecraft/versons/1.8.8)

"id": "1.8.8",
"time": "2016-06-01T07:45:48-04:00",
"releaseTime": "2015-07-27T06:31:28-04: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}"

args在"minecraftArguments"下,jar文件在同一个目录下,命名为1.8.8.jar.

这些是我编程的开始:) 我从 Visual Basic 开始,现在我正在做 C++。这适用于想知道如何制作 Minecraft Launcher 的任何人。

首先你必须下载 "Game Files" 一切都有据可查 HERE。然后就可以开始考虑怎么开始游戏了

您将需要访问权限和客户端令牌我稍微改进了代码:

Imports System.IO
Imports System.Net
Imports System.Web.Script.Serialization ' Add Reference System.Web.Extensions

Module Program

    Public Function Authenticate(ByRef username As String, ByRef password As String) As String

        Dim UUID As String = Guid.NewGuid.ToString()
        Dim Request As HttpWebRequest = DirectCast(WebRequest.Create("https://authserver.mojang.com/authenticate"), HttpWebRequest)
        Request.ContentType = "application/json"
        Request.Method = "POST"

        Using Writer = New StreamWriter(Request.GetRequestStream())

            Dim Json As String = ("{ 'agent': {'name': 'Minecraft', 'version': 1}, 'username': '" + username + "', 'password': '" + password + "', 'clientToken': '" + UUID + "', 'requestUser': true }").Replace("'", Chr(34))

            Writer.Write(Json)
            Writer.Flush()
            Writer.Close()

            Dim Response = DirectCast(Request.GetResponse(), HttpWebResponse)
            Using Reader = New StreamReader(Response.GetResponseStream())
                Return Reader.ReadToEnd()
            End Using

        End Using

    End Function

    Sub Main()

        Dim Response As String = Authenticate("username", "password")
        Dim Serializer As JavaScriptSerializer = New JavaScriptSerializer()

        Dim Dictionary As Dictionary(Of String, Object) = Serializer.Deserialize(Of Dictionary(Of String, Object))(Response)

        Dim AccessToken As String = Dictionary("accessToken")
        Dim ClientToken As String = Dictionary("clientToken")

        Console.WriteLine(AccessToken)
        Console.WriteLine(ClientToken)
        Console.Read()

    End Sub

End Module

在此代码中,您向 mojang 身份验证服务器发送 HTTP POST 请求,然后将响应解析为 json 和 JavaScriptSerializer。访问令牌和客户端令牌存储在 AccessTokenClientToken 变量中。 Mojang 身份验证记录在案 HERE。那么我建议你从 System.Diagnostics 创建 Process 并像这样启动它:

Public Sub LaunchMinecraft(ByRef AccessToken As String, ByRef ClientToken As String)

    Dim Xmx As String = "4G"
    Dim Xms As String = "2G"
    Dim Title As String = "My Minecraft Instance"
    Dim Version As String = "1.7.10"
    Dim AssetIndex As String = "..." ' AssetIndex obtained from mojang server
    Dim Username As String = "..." ' Minecraft username obtained from mojang server
    Dim Libraries As String = "..." ' Paths to all libraries separated by ;

    Dim JavaProcess As Process = New Process()
    JavaProcess.StartInfo.UseShellExecute = False
    JavaProcess.StartInfo.CreateNoWindow = False
    JavaProcess.StartInfo.FileName = "..." ' Path to javaw.exe or java.exe
    JavaProcess.StartInfo.Arguments = "-XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump" +
                            " -Xmx" + Xmx +
                            " -Xms" + Xms +
                            " -Djava.library.path=.//bin/natives" +
                            " -Dminecraft.client.jar=.//bin/modpack.jar" +
                            " -Dminecraft.applet.TargetDirectory=.//" +
                            " -cp .//bin/modpack.jar;" + Libraries + ";.//bin/minecraft.jar" +
                            " -XX:+UnlockExperimentalVMOptions" +
                            " -XX:+UseG1GC -XX:G1NewSizePercent=20" +
                            " -XX:G1ReservePercent=20" +
                            " -XX:MaxGCPauseMillis=50" +
                            " -XX:G1HeapRegionSize=16M" +
                            " net.minecraft.launchwrapper.Launch" +
                            " --gameDir .//" +
                            " --username " + Username +
                            " --assetsDir ..//..//..//cache/assets" +
                            " --assetIndex " + AssetIndex +
                            " --version " + Version +
                            " --uuid " + ClientToken +
                            " --accessToken " + AccessToken +
                            " --userProperties {}" +
                            " --userType mojang" +
                            " --tweakClass cpw.mods.fml.common.launcher.FMLTweaker" +
                            " --title " + Title
    JavaProcess.Start()

End Sub

此代码未经测试,也有相对路径。这只是一个例子。请注意,这是针对改装版 minecraft 的。希望对您有所帮助!