Example #1
0
        /// <summary>
        /// 从注册表寻找本机JAVA列表
        /// </summary>
        /// <returns></returns>
        public static List <Java> GetJavaList()
        {
            List <Java> javas        = new List <Java>();
            RegistryKey localMachine = Registry.LocalMachine.OpenSubKey("SOFTWARE");

            switch (SystemTools.GetSystemArch())
            {
            case ArchEnum.x32:
                var jres = GetJavaRegisterPath(localMachine);
                javas.AddRange(jres.Select(x => new Java(x.Value, x.Key, ArchEnum.x32)));
                break;

            case ArchEnum.x64:
                var jres64 = GetJavaRegisterPath(localMachine);
                javas.AddRange(jres64.Select(x => new Java(x.Value, x.Key, ArchEnum.x64)));
                var jres32 = GetJavaRegisterPath(localMachine.OpenSubKey("Wow6432Node"));
                javas.AddRange(jres32.Select(x => new Java(x.Value, x.Key, ArchEnum.x32)));
                break;
            }
            return(javas);
        }
Example #2
0
        /// <summary>
        /// 从所给JAVA列表中寻找最适合本机的JAVA
        /// </summary>
        /// <param name="javalist"></param>
        /// <returns></returns>
        public static Java GetSuitableJava(List <Java> javalist)
        {
            try
            {
                List <Java> goodjava = new List <Java>();
                if (SystemTools.GetSystemArch() == ArchEnum.x64)
                {
                    foreach (var item in javalist)
                    {
                        if (item.Arch == ArchEnum.x64)
                        {
                            goodjava.Add(item);
                        }
                    }
                    if (goodjava.Count == 0)
                    {
                        goodjava.AddRange(javalist);
                    }
                }
                else
                {
                    goodjava = javalist;
                }

                var java8 = goodjava.Where((x) =>
                {
                    return(x.Version.StartsWith("1.8"));
                });
                if (java8.Count() != 0)
                {
                    return(java8.OrderByDescending(x => x.Version).ToList().FirstOrDefault());
                }
                else
                {
                    return(goodjava.OrderByDescending(a => a.Version).ToList().FirstOrDefault());
                }
            }
            catch (Exception)
            { return(null); }
        }
Example #3
0
        public Modules.Version JsonToVersion(JObject obj)
        {
            Modules.Version ver = new Modules.Version();
            ver = obj.ToObject <Modules.Version>();
            JObject innerVer = null;

            if (ver.InheritsVersion != null)
            {
                SendDebugLog(string.Format("检测到\"{0}\"继承于\"{1}\"", ver.ID, ver.InheritsVersion));
                string innerJsonStr = GetVersionJsonText(ver.InheritsVersion);
                if (innerJsonStr != null)
                {
                    innerVer = JObject.Parse(innerJsonStr);
                }
            }
            if (obj.ContainsKey("arguments"))
            {
                #region 处理新版本引导

                SendDebugLog(string.Format("检测到\"{0}\"使用新版本启动参数", ver.ID));

                #region 处理版本继承
                if (innerVer != null && innerVer.ContainsKey("arguments"))
                {
                    JObject innerVerArg = (JObject)innerVer["arguments"];
                    if (innerVerArg.ContainsKey("game"))
                    {
                        ver.MinecraftArguments += string.Format("{0} ", ParseGameArgFromJson(innerVerArg["game"]));
                    }
                    if (innerVerArg.ContainsKey("jvm"))
                    {
                        ver.JvmArguments += string.Format("{0} ", ParseJvmArgFromJson(innerVerArg["jvm"]));
                    }
                }
                #endregion

                JObject verArg = (JObject)obj["arguments"];

                #region 游戏参数
                if (verArg.ContainsKey("game"))
                {
                    JToken gameArg = verArg["game"];
                    ver.MinecraftArguments += ParseGameArgFromJson(gameArg);
                }
                #endregion

                #region JVM参数
                if (verArg.ContainsKey("jvm"))
                {
                    JToken jvmArg = verArg["jvm"];
                    ver.JvmArguments += ParseJvmArgFromJson(jvmArg);
                }
                #endregion
            }
            #endregion
            else
            {
                #region 旧版本添加默认JVM参数
                SendDebugLog(string.Format("检测到\"{0}\"使用旧版本启动参数", ver.ID));
                ver.JvmArguments = "-Djava.library.path=${natives_directory} -cp ${classpath}";
                #endregion
            }

            #region 处理依赖
            ver.Libraries = new List <Library>();
            ver.Natives   = new List <Native>();
            var libToken = obj["libraries"];
            foreach (JToken lib in libToken)
            {
                var libObj = lib.ToObject <JLibrary>();

                if (CheckAllowed(libObj.Rules))
                {
                    var parts = libObj.Name.Split(':');

                    string package = parts[0];
                    string name    = parts[1];
                    string version = parts[2];

                    if (libObj.Natives == null)
                    {
                        //不为native
                        Library library = new Library()
                        {
                            Package = package,
                            Name    = name,
                            Version = version
                        };
                        if (!string.IsNullOrWhiteSpace(libObj.Url))
                        {
                            library.Url = libObj.Url;
                        }
                        if (libObj.Downloads?.Artifact != null)
                        {
                            library.LibDownloadInfo = libObj.Downloads.Artifact;
                        }
                        ver.Libraries.Add(library);
                    }
                    else
                    {
                        //为native
                        var native = new Native()
                        {
                            Package      = package,
                            Name         = name,
                            Version      = version,
                            NativeSuffix = libObj.Natives["windows"].Replace("${arch}", SystemTools.GetSystemArch() == ArchEnum.x64 ? "64" : "32")
                        };
                        if (libObj.Extract != null)
                        {
                            native.Exclude = libObj.Extract.Exculde;
                        }
                        if (libObj.Downloads?.Artifact != null)
                        {
                            native.LibDownloadInfo = libObj.Downloads.Artifact;
                        }
                        if (libObj.Downloads?.Classifiers?.Natives != null)
                        {
                            native.NativeDownloadInfo = libObj.Downloads.Classifiers.Natives;
                        }
                        ver.Natives.Add(native);
                    }
                }
            }
            #endregion

            #region 处理版本继承
            if (innerVer != null)
            {
                var iv = JsonToVersion(innerVer);
                if (iv != null)
                {
                    ver.Assets     = iv.Assets;
                    ver.AssetIndex = iv.AssetIndex;
                    ver.Natives.AddRange(iv.Natives);
                    ver.Libraries.AddRange(iv.Libraries);
                    ver.Jar = iv.ID;
                }
            }
            #endregion

            return(ver);
        }
Example #4
0
        private string ParseJvmArgFromJson(JToken jvmArg)
        {
            StringBuilder jvmArgBuilder = new StringBuilder();

            foreach (var arg in jvmArg)
            {
                if (arg.Type == JTokenType.String)
                {
                    jvmArgBuilder.AppendFormat("{0} ", arg.ToString());
                }
                else if (arg.Type == JTokenType.Object)
                {
                    JObject argObj = (JObject)arg;
                    if (argObj.ContainsKey("rules"))
                    {
                        JToken rules = arg["rules"];
                        #region 规则参数处理
                        foreach (var rule in rules)
                        {
                            if (rule["action"].ToString() == "allow")
                            {
                                #region 判断是否合法
                                JObject OS = (JObject)rule["os"];
                                if (OS.ContainsKey("arch"))
                                {
                                    string arch = OS["arch"].ToString();
                                    switch (SystemTools.GetSystemArch())
                                    {
                                    case ArchEnum.x32:
                                        if (arch != "x86")
                                        {
                                            continue;
                                        }
                                        break;

                                    case ArchEnum.x64:
                                        if (arch != "x64")
                                        {
                                            continue;
                                        }
                                        break;

                                    default:
                                        continue;
                                    }
                                }
                                if (OS.ContainsKey("name"))
                                {
                                    if (OS["name"].ToString() == "windows")
                                    {
                                        if (OS.ContainsKey("version"))
                                        {
                                            if (!Regex.Match(Environment.OSVersion.Version.ToString(), OS["version"].ToString()).Success)
                                            {
                                                continue;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        continue;
                                    }
                                }
                                #endregion

                                if (arg["value"].Type == JTokenType.String)
                                {
                                    string value = arg["value"].ToString();
                                    if (value.Contains(" "))
                                    {
                                        jvmArgBuilder.AppendFormat("\"{0}\" ", value);
                                    }
                                    else
                                    {
                                        jvmArgBuilder.AppendFormat("{0} ", value);
                                    }
                                }
                                else if (arg["value"].Type == JTokenType.Array)
                                {
                                    foreach (var str in arg["value"])
                                    {
                                        string value = str.ToString();
                                        if (value.Contains(" "))
                                        {
                                            jvmArgBuilder.AppendFormat("\"{0}\" ", value);
                                        }
                                        else
                                        {
                                            jvmArgBuilder.AppendFormat("{0} ", value);
                                        }
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        JToken valueJtoken = argObj["value"];
                        if (valueJtoken.Type == JTokenType.String)
                        {
                            jvmArgBuilder.AppendFormat("{0} ", valueJtoken.ToString());
                        }
                        else if (valueJtoken.Type == JTokenType.Array)
                        {
                            foreach (var item in valueJtoken)
                            {
                                jvmArgBuilder.AppendFormat("{0} ", item.ToString());
                            }
                        }
                    }
                }
            }
            return(jvmArgBuilder.ToString().Trim());
        }