HIMBest
本帖最后由 HIMBest 于 2020-10-6 13:01 编辑
MCCC(Kblack's Minecraft Core Code)是Kblack(@zhouyiran2)制作的一个C#版的启动核心,拥有以下优点

功能多:包含Json解析(LitJson),Java路径获取,Versions版本获取,文件解压,系统信息获取,以及Libraries文件获取、MojangAPI等。
开源:任何人都可以免费使用,如需修改源码,请务必遵守LGPL协议。
简单:启动只需要几句代码即可。

前言(不会C#的开发者看过来)
由于https://www.mcbbs.net/thread-492606-1-1.html的这个教程没有提及Visual Basic.net的使用KMCCC方法,导致许多不会C#的.net开发萌新去学习C#(其实我也不会C#),本教程主要针对Visual Basic.net中KMCCC的使用,并会附上对应的C#代码
作者使用KMCCC核心开发的Visual Basic.net开源启动器(以后会用到Python):


源代码尚未整理好,暂不提供
详情请在https://www.mcbbs.net/thread-1125381-1-1.html中查看


适用人群:有一定基础.net开发者,但事实证明Visual Basic.net开发者须有一定的C语言语法基础

KMCCC的GitHub链接:https://github.com/MineStudio/KMCCC
编译KMCCC需要.net Framework 4.0开发环境并且需安装WindowsSDK,用户也许安装.net Framework 4.0(Windows系统应该都自带)才可以使用
PS:如果KMCCC的源码(KMCCC.sln)打不开的话建议使用VS2017及以上版本的IDE

考虑到部分萌新开发者(包括我),本教程将以VWinForm(Windows窗体应用程序)为例:
PS:WPF我不会……


正文
1.打开Visual Studio,新建一个Windows窗体应用程序

PS:图方便用了学习版(VS2010专业版是要收费的)


2.然后添加KMCCC.Pro.dll的引用

PS:不会添加的自己去百度吧

添加以下代码:
Visual Basic.net:
  1. Imports KMCCC.Authentication
  2. Imports KMCCC.Launcher
复制代码
C#:
  1. using KMCCC.Authentication;
  2. using KMCCC.Launcher;
复制代码



3.在Form1中添加一个Button控件和一个ComboList控件


4.创建KMCCC.LauncherCore对象(否则无法启动游戏)(这个Visual Basic.net和C#的方法略有出入)

Visual Basic.net:双击Form1,然后在Public Class Form1()中添加一下代码:
  1. Public Core As LauncherCore = LauncherCore.Create()
复制代码
C#:在Program.cs中找到程序主入口点,然后添加如下代码
  1. public static LauncherCore Core = LauncherCore.Create();
复制代码

5.获取MC版本列表
双击Form1,在Form1_Load事件中添加以下代码:
Visual Basic.net:
  1.         Dim Versions As KMCCC.Launcher.Version() = Me.Core.GetVersions().ToArray()  '获取版本列表
  2.         Me.ComboBox1.DataSource = Versions  '绑定数据源
  3.         Me.ComboBox1.DisplayMember = "Id"  '设置comboBox显示的为版本Id
复制代码
C#:
  1.         var versions = Program.Core.GetVersions().ToArray();  //获取版本列表
  2.         comboBox1.DataSource = versions;  //绑定数据源
  3.         comboBox1.DisplayMember = "Id";  //设置comboBox显示的为版本Id
复制代码

6.启动游戏
双击Button1,在Button1_Click事件中添加如下代码:
Visual Basic.net(参考C#代码):
  1.         Dim Ver As KMCCC.Launcher.Version = CType(Me.ComboBox1.SelectedItem, KMCCC.Launcher.Version) 'Ver为Versions里你要启动的版本名字
  2.         Dim MaxMemory As Integer = 1024 '最大内存,Integer类型
  3.         Dim Authenticator As New KMCCC.Authentication.OfflineAuthenticator("ZhaiSoul") '离线启动,ZhaiSoul那儿为你要设置的游戏名
  4.         'Dim Authenticator As New KMCCC.Authentication.YggdrasilLogin("邮箱", "密码", True)'正版启动,最后一个为是否twitch登录
  5.         Dim Mode As Object = LaunchMode.MCLauncher '启动模式,这个我会在后面解释有哪几种
  6.         Dim Server As New ServerInfo() With {.Address = "服务器IP地址", .Port = "服务器端口"} '设置启动游戏后,自动加入指定IP的服务器,可以不要
  7.         Dim Size As New WindowSize() With {.Height = 852, .Width = 480} '设置窗口大小,可以不要
  8. '若不要上述的某个参数,请在Dim Result As...中删除之
  9. 'PS:由于是通过C#反编译得来的Visual Basic.net代码,可能会有些杂乱
  10.         Dim Result As KMCCC.Launcher.LaunchResult = Me.Core.Launch(New LaunchOptions() With {.Version = Ver,
  11.             .MaxMemory = MaxMemory,
  12.             .Authenticator = Authenticator,
  13.             .Mode = LaunchMode.MCLauncher,
  14.             .Server = Server,
  15.             .Size = Size
  16.             }, New Action(Of MinecraftLaunchArguments)(-1) {})
复制代码
C#:
  1. var ver = (KMCCC.Launcher.Version)comboBox1.SelectedItem;
  2. var result = Program.Core.Launch(new LaunchOptions
  3. {
  4. Version = ver, //Ver为Versions里你要启动的版本名字
  5. MaxMemory = 1024, //最大内存,int类型
  6. Authenticator = new OfflineAuthenticator("ZhaiSoul"), //离线启动,ZhaiSoul那儿为你要设置的游戏名
  7. //Authenticator = new YggdrasilLogin("邮箱", "密码", true), // 正版启动,最后一个为是否twitch登录
  8. Mode = LaunchMode.MCLauncher, //启动模式,这个我会在后面解释有哪几种
  9. Server = new ServerInfo {Address = "服务器IP地址",Port="服务器端口"}, //设置启动游戏后,自动加入指定IP的服务器,可以不要
  10. Size = new WindowSize {Height = 768, Width = 1280} //设置窗口大小,可以不要
  11. });
复制代码

6.1启动模式:
参见原帖:https://www.mcbbs.net/thread-492606-1-1.html
PS:我也没搞明白是怎么回事

Mode为启动模式,KMCCC拥有三种启动模式,分别为默认、BMCL、MCLauncher模式

默认:设置游戏的默认加载目录为.minecraft(即Mods,resourcepacks等文件将放在.minecraft中)
BMCL模式:复制coremods到Versions/版本 里,参见BMCL的启动模式
MCLauncher模式:Mods和resourcepacks等资源文件均放在Versions/版本 里面,适合有不同游戏版本的客户端


7.错误处理(可参照原贴)

Visual Basic.net:
  1.         If Not Result.Success Then
  2.             Select Case Result.ErrorType
  3.                 Case ErrorType.NoJAVA
  4.                     MessageBox.Show("你系统的Java有异常,可能你非正常途径删除过Java,请尝试重新安装Java" & vbLf & "详细信息:" + Result.ErrorMessage, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand)
  5.                     GoTo IL_168
  6.                 Case ErrorType.AuthenticationFailed
  7.                     MessageBox.Show(Me, "正版验证失败!请检查你的账号密码", "账号错误" & vbLf & "详细信息:" + Result.ErrorMessage, MessageBoxButtons.OK, MessageBoxIcon.Hand)
  8.                     GoTo IL_168
  9.                 Case ErrorType.UncompressingFailed
  10.                     MessageBox.Show(Me, "可能的多开或文件损坏,请确认文件完整且不要多开" & vbLf & "如果你不是多开游戏的话,请检查libraries文件夹是否完整" & vbLf & "详细信息:" + Result.ErrorMessage, "可能的多开或文件损坏", MessageBoxButtons.OK, MessageBoxIcon.Hand)
  11.                     GoTo IL_168
  12.                 Case Else
  13.                     MessageBox.Show(Me, Result.ErrorMessage + vbLf + If((Result.Exception Is Nothing), String.Empty, Result.Exception.StackTrace), "启动错误,请将此窗口截图向开发者寻求帮助")
  14.             End Select
  15. IL_168:
  16.         End If
复制代码
C#:
  1.             if (!result.Success)
  2.             {
  3.                 //MessageBox.Show(result.ErrorMessage, result.ErrorType.ToString());
  4.                 switch (result.ErrorType)
  5.                 {
  6.                     case ErrorType.NoJAVA:
  7.                         MessageBox.Show("你系统的Java有异常,可能你非正常途径删除过Java,请尝试重新安装Java\n详细信息:" + result.ErrorMessage,"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  8.                         break;
  9.                     case ErrorType.AuthenticationFailed:
  10.                         MessageBox.Show(this, "正版验证失败!请检查你的账号密码", "账号错误\n详细信息:" + result.ErrorMessage,MessageBoxButtons.OK,MessageBoxIcon.Error);
  11.                         break;
  12.                     case ErrorType.UncompressingFailed:
  13.                         MessageBox.Show(this, "可能的多开或文件损坏,请确认文件完整且不要多开\n如果你不是多开游戏的话,请检查libraries文件夹是否完整\n详细信息:" + result.ErrorMessage, "可能的多开或文件损坏",MessageBoxButtons.OK,MessageBoxIcon.Error);
  14.                         break;
  15.                     default:
  16.                         MessageBox.Show(this,
  17.                             result.ErrorMessage + "\n" +
  18.                             (result.Exception == null ? string.Empty : result.Exception.StackTrace),
  19.                             "启动错误,请将此窗口截图向开发者寻求帮助");
  20.                         break;
  21.                 }
  22.             }
复制代码


8.Visual Basic.net与C#互相转换方法(本教程中C#对应的Visual Basic代码的得到方式)
首先,把C#的代码编译成exe(exe还是dll随你),再使用.net反编译软件(我用的是dnSpy)进行反编译,最后导出工程,语言选择Visual Basic.net
如图:

打开.sin文件,就是Visual Basic.net代码了


占楼待更



Pan$brother
期待更新

HIMBest
本帖最后由 HIMBest 于 2020-8-30 12:24 编辑

9.关于自定义Java路径(原贴上也有,只不过“找不到”)

首先,需要在KMCCC的源代码中添加以下代码:
打开KMCCC.sln,在KMCCC.Shared项目下找到Launcher文件夹中的LaunchHandle.cs
在以下代码的后面
  1. /// <summary>
  2.         ///     最大内存
  3.         /// </summary>
  4.         public int MaxMemory { get; set; }

  5.                 /// <summary>
  6.                 ///     最小内存
  7.                 /// </summary>
  8.                 public int MinMemory { get; set; }

  9.                 /// <summary>
  10.                 ///     启动的版本
  11.                 /// </summary>
  12.                 public Version Version { get; set; }

  13.                 /// <summary>
  14.                 ///     使用的验证器
  15.                 /// </summary>
  16.                 public IAuthenticator Authenticator { get; set; }

  17.                 /// <summary>
  18.                 ///     启动模式
  19.                 /// </summary>
  20.                 public LaunchMode Mode { get; set; }

  21.                 /// <summary>
  22.                 ///     直接连接的服务器
  23.                 /// </summary>
  24.                 public ServerInfo Server { get; set; }
复制代码
添加
  1. /// <summary>
  2. /// Java路径
  3. /// </summary>
  4. public string JavaPath { set; get; }
复制代码
完成效果图:

然后在同文件夹下的LauncherCoreInternal.cs中的
  1. internal LaunchResult LaunchInternal(LaunchOptions options, params Action<MinecraftLaunchArguments>[] argumentsOperators)
  2.                 {
  3.             lock (Locker)
  4.                         {
  5.                                 if (!File.Exists(JavaPath))
  6.                                 {
  7.                                         return new LaunchResult {Success = false, ErrorType = ErrorType.NoJAVA, ErrorMessage = "指定的JAVA位置不存在"};
  8.                                 }
  9.                                 CurrentCode = Random.Next();
  10.                                 var args = new MinecraftLaunchArguments();
  11.                                 var result = GenerateArguments(options, ref args);
  12.                                 if (result != null)
  13.                                 {
  14.                                         return result;
  15.                                 }
  16.                                 if (argumentsOperators == null) return LaunchGame(args);
  17.                                 foreach (var opt in argumentsOperators)
  18.                                 {
  19.                                         try
  20.                                         {
  21.                                                 if (opt != null)
  22.                                                 {
  23.                                                         opt(args);
  24.                                                 }
  25.                                         }
  26.                                         catch (Exception exp)
  27.                                         {
  28.                                                 return new LaunchResult {Success = false, ErrorType = ErrorType.OperatorException, ErrorMessage = "指定的操作器引发了异常", Exception = exp};
  29.                                         }
  30.                                 }
  31.                                 return LaunchGame(args);
  32.                         }
  33.                 }
复制代码
中的
  1. lock (Locker){
复制代码
语句后插入
  1. JavaPath = options.JavaPath;
复制代码
就像这样:


最后编译KMCCC.Pro.dll并在启动代码中加入JavaPath属性,例如
Visual Basic.net:
  1.         Dim Ver As KMCCC.Launcher.Version = CType(Me.ComboBox1.SelectedItem, KMCCC.Launcher.Version) 'Ver为Versions里你要启动的版本名字
  2. Dim JavaPath As String = "C:\Program Files\Java\jre1.8.0_181\bin\javaw.exe"  'Java路径,可以不要
  3.         Dim MaxMemory As Integer = 1024 '最大内存,Integer类型
  4.         Dim Authenticator As New KMCCC.Authentication.OfflineAuthenticator("ZhaiSoul") '离线启动,ZhaiSoul那儿为你要设置的游戏名
  5.         'Dim Authenticator As New KMCCC.Authentication.YggdrasilLogin("邮箱", "密码", True)'正版启动,最后一个为是否twitch登录
  6.         Dim Mode As Object = LaunchMode.MCLauncher '启动模式,这个我会在后面解释有哪几种
  7.         Dim Server As New ServerInfo() With {.Address = "服务器IP地址", .Port = "服务器端口"} '设置启动游戏后,自动加入指定IP的服务器,可以不要
  8.         Dim Size As New WindowSize() With {.Height = 852, .Width = 480} '设置窗口大小,可以不要
  9. '若不要上述的某个参数,请在Dim Result As...中删除之
  10. 'PS:由于是通过C#反编译得来的Visual Basic.net代码,可能会有些杂乱
  11.         Dim Result As KMCCC.Launcher.LaunchResult = Me.Core.Launch(New LaunchOptions() With {.Version = Ver,
  12. .JavaPath = JavaPath,
  13.             .MaxMemory = MaxMemory,
  14.             .Authenticator = Authenticator,
  15.             .Mode = LaunchMode.MCLauncher,
  16.             .Server = Server,
  17.             .Size = Size
  18.             }, New Action(Of MinecraftLaunchArguments)(-1) {})
复制代码
C#:
  1. var ver = (KMCCC.Launcher.Version)comboBox1.SelectedItem;
  2. var result = Program.Core.Launch(new LaunchOptions
  3. {
  4. JavaPath = "C:\Program Files\Java\jre1.8.0_181\bin\javaw.exe"   //Java路径,可以不要
  5. Version = ver, //Ver为Versions里你要启动的版本名字
  6. MaxMemory = 1024, //最大内存,int类型
  7. Authenticator = new OfflineAuthenticator("ZhaiSoul"), //离线启动,ZhaiSoul那儿为你要设置的游戏名
  8. //Authenticator = new YggdrasilLogin("邮箱", "密码", true), // 正版启动,最后一个为是否twitch登录
  9. Mode = LaunchMode.MCLauncher, //启动模式,这个我会在后面解释有哪几种
  10. Server = new ServerInfo {Address = "服务器IP地址",Port="服务器端口"}, //设置启动游戏后,自动加入指定IP的服务器,可以不要
  11. Size = new WindowSize {Height = 768, Width = 1280} //设置窗口大小,可以不要
  12. });
复制代码

注意:若去除JavaPath参数,启动时还是会自动检测Java,建议在KMCCC“找不到”系统Java时使用



HIMBest

更了           

OldGodShen
黄鱼出品,必是垃圾(doge)

HIMBest
OldGodShen 发表于 2020-9-17 22:34
黄鱼出品,必是垃圾(doge)

期待你出品

魔皓
如何编译出来?这个问题带给所有人的困扰(doge)

HIMBest
魔皓 发表于 2020-9-22 08:54
如何编译出来?这个问题带给所有人的困扰(doge)

什么意思

AHpx
教程不错,另外,2020居然还在用visual studio 2010?!这个开发工具太脱节了吧?

HIMBest
AHpx 发表于 2020-10-21 21:21
教程不错,另外,2020居然还在用visual studio 2010?!这个开发工具太脱节了吧? ...

我只是图个方便(电脑带19有点慢[doge])我那个该KMCCC源代码的部分的截屏用的是2019
我那个HBMCL用的也是2019
VS19的编译速度和2010是没法比的(慢的一批[doge])