目录
1.为什么会写本文章?
由于最近刚了解C#语法想加强下概念,而且本人也是快十年的MC老玩家惹 准备现学现做着手写一款MC启动器.
2.此项目设定环境
.NetFramwork4.7 WPF 这是考虑到现在主流客户机的运行版本。低于这个版本经过我测试Nuget上的KMCCC版本会引用失败.
3.新建项目
--->创建新建项目
---->注意 此处选择WPF应用(.Net Framework) 不要选错了 上面有一个相同的WPF应用 因为此项目用的4.7所以选择.net framework版本
--->选择.NET Framework4.7
4.添加项目所需引用
--->右键项目 选择管理Nuget程序包
--->浏览中搜索KMCCC 会出现OneMinecraftLauncher.Core 下载此程序包
KMCCC可以实现大部分启动器核心功能拥有以下优点
功能多:包含Json解析(LitJson),Java路径获取,Versions版本获取,文件解压,系统信息获取,以及Libraries文件获取、MojangAPI等。
开源:任何人都可以免费使用,如需修改源码,请务必遵守LGPL协议。
简单:启动只需要几句代码即可。
如果需要最新版本的KMCCC可访问Github获取:
https://github.com/MineStudio/KMCCC4
4.基本的启动功能:
基本的启动功能只需要一个comboBox和一个Button即可搞定
--->在工具栏中选中ComboBox与button拖入主窗口.并分别改名为VersionList和Run或者直接复制下面代码到xmal
<ComboBox x:Name="versionsList" HorizontalAlignment="Left" Margin="315,148,0,0" VerticalAlignment="Top" Width="120"/>
<Button x:Name="Run" Content="启动" HorizontalAlignment="Left" Margin="461,148,0,0" VerticalAlignment="Top"/>
--->进入主界面程序 写入引用
using KMCCC.Launcher;
using KMCCC.Authentication;
实例化KMCCC核心对象
public static LauncherCore Core = LauncherCore.Create();
我们需要在窗体初始化时就获取MC版本所以将下面代码添加在构造器中
versionList控件在xmal添加属性 DisplayMemberPath="Id"
KMCCC.Launcher.Version[] versions = Core.GetVersions().ToArray();
versionsList.ItemsSource = versions;
--->将下列代码添加在Run(button) 点击时触发事件中
Core.JavaPath = @"此处添加java路径";
var ver = (KMCCC.Launcher.Version)versionsList.SelectedItem;
var result = Core.Launch(new LaunchOptions
{
Version = ver, //Ver为Versions里你要启动的版本名字
MaxMemory = 1024, //最大内存,int类型
Authenticator = new OfflineAuthenticator("Ocean_Prince"), //离线启动,ZhaiSoul那儿为你要设置的游戏名
//Authenticator = new YggdrasilLogin("邮箱", "密码", true), // 正版启动
Mode = LaunchMode.MCLauncher, //启动模式
Size = new WindowSize { Height = 768, Width = 1280 } //设置窗口大小
});
5.运行
以上过程完毕后先进行一次编译 编译成功跳出界面可以看到combobox中是没有版本的 这是因为.minecraft文件夹中没有任何MC数据 所以现在开始添加MC数据
---->右键解决方案 选择在资源管理器中打开文件夹
----->进入此目录 项目名称\bin\Debug 我们需要在.minecraft文件夹中添加MC数据
---->添加MC数据如下图所示
--->重新编译 运行 成功启动和获取版本!
6.完整代码
using KMCCC.Authentication;
using KMCCC.Launcher;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public static KMCCC.Launcher.Version[] versions;
public static LauncherCore Core = LauncherCore.Create();
public MainWindow()
{
InitializeComponent();
KMCCC.Launcher.Version[] versions = Core.GetVersions().ToArray();
versionsList.ItemsSource = versions;
}
private void Run_Click(object sender, RoutedEventArgs e)
{
Core.JavaPath = @"C:\Program Files\Java\jdk1.8.0_202\bin\javaw.exe";
var ver = (KMCCC.Launcher.Version)versionsList.SelectedItem;
var result = Core.Launch(new LaunchOptions
{
Version = ver, //Ver为Versions里你要启动的版本名字
MaxMemory = 1024, //最大内存,int类型
Authenticator = new OfflineAuthenticator("Ocean_Prince"), //离线启动,ZhaiSoul那儿为你要设置的游戏名
//Authenticator = new YggdrasilLogin("邮箱", "密码", true), // 正版启动,最后一个为是否twitch登录
Mode = LaunchMode.MCLauncher, //启动模式,这个我会在后面解释有哪几种
// Server = new ServerInfo { Address = "服务器IP地址", Port = "服务器端口" }, //设置启动游戏后,自动加入指定IP的服务器,可以不要
Size = new WindowSize { Height = 768, Width = 1280 } //设置窗口大小,可以不要
});
}
}
}
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ComboBox x:Name="versionsList" HorizontalAlignment="Left" Margin="315,148,0,0" VerticalAlignment="Top" Width="120" DisplayMemberPath="Id"/>
<Button x:Name="Run" Content="启动" HorizontalAlignment="Left" Margin="461,148,0,0" VerticalAlignment="Top" Click="Run_Click"/>
</Grid>
</Window>