namespace KMCCC.Launcher { #region using System; using System.Collections.Generic; using System.IO; using System.Linq; using Modules.JVersion; using Tools; #endregion /// /// 启动器核心 /// public partial class LauncherCore { internal int CurrentCode; internal Random Random = new Random(); private IVersionLocator _versionLocator; #region GetVersion /// /// 返回包含全部版本数组 /// /// 版本数组 public IEnumerable GetVersions() { return (VersionLocator == null) ? new Version[0] : _versionLocator.GetAllVersions(); } /// /// 返回指定id的版本 /// /// 要指定的ID /// 指定的版本 public Version GetVersion(string id) { return (VersionLocator == null) ? null : _versionLocator.Locate(id); } #endregion private LauncherCore() { } /// /// 游戏根目录 /// public string GameRootPath { get; private set; } /// /// JAVA目录 /// public string JavaPath { get; set; } /// /// 版本定位器 /// public IVersionLocator VersionLocator { get { return _versionLocator; } set { (_versionLocator = value).Core = this; } } /// /// 从CreationOption创建启动器核心 /// /// 启动器创建选项 /// 创建的启动器核心 public static LauncherCore Create(LauncherCoreCreationOption option) { var launcherCore = new LauncherCore { GameRootPath = option.GameRootPath, JavaPath = option.JavaPath, VersionLocator = option.VersionLocator }; return launcherCore; } public static LauncherCore Create(string gameRootPath = null) { return Create(new LauncherCoreCreationOption(gameRootPath ?? @".minecraft")); } /// /// 启动函数 /// 过程: /// 1. 运行验证器(authenticator),出错返回null /// 2. 继续构造启动参数 /// 3. 遍历Operators对启动参数进行修改 /// 4. 启动 /// /// 启动选项 /// 启动参数的修改器 /// 启动结果 public LaunchResult Launch(LaunchOptions options, params Action[] argumentsOperators) { return LaunchInternal(options, argumentsOperators); } /// /// 游戏退出事件 /// public event Action GameExit; /// /// 游戏Log事件 /// public event Action GameLog; } /// /// 启动器核心选项 /// 以后可能包含更多内容 /// public class LauncherCoreCreationOption { /// /// 核心选项 /// /// 游戏根目录,默认为 ./.minecraft /// JAVA地址,默认为自动搜寻所的第一个 /// Version定位器,默认为 JVersionLoacator public LauncherCoreCreationOption(string gameRootPath = null, string javaPath = null, IVersionLocator versionLocator = null) { GameRootPath = new DirectoryInfo(gameRootPath ?? ".minecraft").FullName; JavaPath = javaPath ?? SystemTools.FindJava().FirstOrDefault(); VersionLocator = versionLocator ?? new JVersionLocator(); if (!Directory.Exists(GameRootPath)) { Directory.CreateDirectory(GameRootPath); } } /// /// 游戏根目录 /// public string GameRootPath { get; internal set; } /// /// JAVA地址 /// public string JavaPath { get; internal set; } /// /// Version定位器 /// public IVersionLocator VersionLocator { get; internal set; } [Obsolete] public static LauncherCoreCreationOption Create(string gameRootPath = null, string javaPath = null, IVersionLocator versionLocator = null) { return new LauncherCoreCreationOption(gameRootPath, javaPath, versionLocator); } } /// /// 启动后返回的启动结果 /// public class LaunchResult { /// /// 获取是否启动成功 /// public bool Success { get; set; } /// /// 获取发生的错误类型 /// public ErrorType ErrorType { get; set; } /// /// 获取错误信息 /// public string ErrorMessage { get; set; } /// /// 启动时发生异常 /// public Exception Exception { get; set; } /// /// 获取启动句柄 /// public LaunchHandle Handle { get; set; } } public enum ErrorType { /// /// 没有错误 /// None, /// /// 没有找到JAVA /// NoJAVA, /// /// 验证失败 /// AuthenticationFailed, /// /// 操作器出现故障 /// OperatorException, /// /// 未知 /// Unknown, /// /// 解压错误 /// UncompressingFailed } }