예제 #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);
        }
예제 #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); }
        }
예제 #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 <Modules.Library>();
            ver.Natives   = new List <Native>();
            var libToken = obj["libraries"];
            foreach (JToken lib in libToken)
            {
                var libObj = lib.ToObject <Library>();
                var parts  = libObj.Name.Split(':');

                if (libObj.Natives == null)
                {
                    if (CheckAllowed(libObj.Rules))
                    {
                        Modules.Library library = new Modules.Library()
                        {
                            Package = parts[0],
                            Name    = parts[1],
                            Version = parts[2]
                        };
                        if (!string.IsNullOrWhiteSpace(libObj.Url))
                        {
                            library.Url = libObj.Url;
                        }
                        ver.Libraries.Add(library);
                    }
                }
                else
                {
                    if (CheckAllowed(libObj.Rules))
                    {
                        var native = new Native
                        {
                            Package      = parts[0],
                            Name         = parts[1],
                            Version      = parts[2],
                            NativeSuffix = libObj.Natives["windows"].Replace("${arch}", SystemTools.GetSystemArch() == ArchEnum.x64 ? "64" : "32")
                        };
                        ver.Natives.Add(native);
                        if (libObj.Extract != null)
                        {
                            native.Exclude = libObj.Extract.Exculde;
                        }
                    }
                }

                if (((JObject)lib).ContainsKey("url"))
                {
                }
            }
            #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);
        }
예제 #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)
                                {
                                    jvmArgBuilder.AppendFormat("{0} ", arg["value"].ToString());
                                }
                                else if (arg["value"].Type == JTokenType.Array)
                                {
                                    foreach (var str in arg["value"])
                                    {
                                        jvmArgBuilder.AppendFormat("{0} ", str);
                                    }
                                }
                            }
                        }
                        #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());
        }