コード例 #1
0
ファイル: JVersionLocator.cs プロジェクト: CrystalStu/KMCCC
        /// <summary>
        ///     获取Version信息,当出现错误时会返回null
        /// </summary>
        /// <param name="id">版本id</param>
        /// <returns>Version的信息</returns>
        internal Version GetVersionInternal(string id)
        {
            try
            {
                if (_locatingVersion.Contains(id))
                {
                    return(null);
                }
                _locatingVersion.Add(id);

                if (_versions.TryGetValue(id, out Version version))
                {
                    return(version);
                }

                var jver = LoadVersion(_core.GetVersionJsonPath(id));
                if (jver == null)
                {
                    return(null);
                }

                version = new Version();
                if (string.IsNullOrWhiteSpace(jver.Id))
                {
                    return(null);
                }
                if (jver.arguments == null && string.IsNullOrWhiteSpace(jver.MinecraftArguments))
                {
                    return(null);
                }
                if (string.IsNullOrWhiteSpace(jver.MainClass))
                {
                    return(null);
                }
                if (string.IsNullOrWhiteSpace(jver.Assets))
                {
                    jver.Assets = "legacy";
                }
                if (jver.Libraries == null)
                {
                    return(null);
                }
                version.Id = jver.Id;
                if (jver.AssetsIndex != null)
                {
                    version.AssetsIndex = new GameFileInfo()
                    {
                        ID        = jver.AssetsIndex.ID,
                        Path      = jver.AssetsIndex.Path,
                        SHA1      = jver.AssetsIndex.SHA1,
                        Size      = jver.AssetsIndex.Size,
                        TotalSize = jver.AssetsIndex.TotalSize,
                        Url       = jver.AssetsIndex.Url
                    };
                }
                if (jver.Downloads != null)
                {
                    version.Downloads = new Download()
                    {
                        Client = jver.Downloads.Client != null ? new GameFileInfo()
                        {
                            SHA1 = jver.Downloads.Client.SHA1,
                            Size = jver.Downloads.Client.Size,
                            Url  = jver.Downloads.Client.Url
                        } : null,
                        Server = jver.Downloads.Server != null ? new GameFileInfo()
                        {
                            SHA1 = jver.Downloads.Server.SHA1,
                            Size = jver.Downloads.Server.Size,
                            Url  = jver.Downloads.Server.Url
                        } : null
                    };
                }
                version.MinecraftArguments = jver.MinecraftArguments ?? UsefulTools.PrintfArray(jver.arguments.game);
                version.Assets             = jver.Assets;
                version.MainClass          = jver.MainClass;
                version.JarId     = jver.JarId;
                version.Libraries = new List <Library>();
                version.Natives   = new List <Native>();
                foreach (var lib in jver.Libraries)
                {
                    if (string.IsNullOrWhiteSpace(lib.Name))
                    {
                        continue;
                    }
                    var names = lib.Name.Split(':');
                    if (names.Length != 3)
                    {
                        continue;
                    }
                    if (lib.Natives == null)
                    {
                        if (!IfAllowed(lib.Rules))
                        {
                            continue;
                        }
                        version.Libraries.Add(new Library
                        {
                            NS      = names[0],
                            Name    = names[1],
                            Version = names[2]
                        });
                    }
                    else
                    {
                        if (!IfAllowed(lib.Rules))
                        {
                            continue;
                        }
                        var native = new Native
                        {
                            NS           = names[0],
                            Name         = names[1],
                            Version      = names[2],
                            NativeSuffix = lib.Natives[GetOSSpecifiedNative()].Replace("${arch}", SystemTools.GetArch())
                        };
                        version.Natives.Add(native);
                        if (lib.Extract != null)
                        {
                            native.Options = new UnzipOptions {
                                Exclude = lib.Extract.Exculde
                            };
                        }
                    }
                }

                if (jver.InheritsVersion != null)
                {
                    var target = GetVersionInternal(jver.InheritsVersion);
                    if (target == null)
                    {
                        return(null);
                    }
                    else
                    {
                        if (version.Assets == "legacy")
                        {
                            version.Assets = null;
                        }
                        version.AssetsIndex        = version.AssetsIndex ?? target.AssetsIndex;
                        version.Downloads          = version.Downloads ?? target.Downloads;
                        version.Assets             = version.Assets ?? target.Assets;
                        version.JarId              = version.JarId ?? target.JarId;
                        version.MainClass          = version.MainClass ?? target.MainClass;
                        version.MinecraftArguments = version.MinecraftArguments ?? target.MinecraftArguments;
                        version.Natives.AddRange(target.Natives);
                        version.Libraries.AddRange(target.Libraries);
                        version.AssetsIndex = version.AssetsIndex ?? target.AssetsIndex;
                    }
                }


                version.JarId = version.JarId ?? version.Id;
                _versions.Add(version.Id, version);
                return(version);
            }
            catch (System.Exception)
            {
                return(null);
            }
            finally
            {
                _locatingVersion.Remove(id);
            }
        }
コード例 #2
0
ファイル: JVersionLocator.cs プロジェクト: nakeliss/KMCCC
        /// <summary>
        ///     获取Version信息,当出现错误时会返回null
        /// </summary>
        /// <param name="id">版本id</param>
        /// <returns>Version的信息</returns>
        internal Version GetVersionInternal(string id)
        {
            try
            {
                if (_locatingVersion.Contains(id))
                {
                    return(null);
                }
                _locatingVersion.Add(id);

                Version version;
                if (_versions.TryGetValue(id, out version))
                {
                    return(version);
                }

                var jver = LoadVersion(_core.GetVersionJsonPath(id));
                if (jver == null)
                {
                    return(null);
                }

                version = new Version();
                if (string.IsNullOrWhiteSpace(jver.Id))
                {
                    return(null);
                }
                if (jver.arguments == null && string.IsNullOrWhiteSpace(jver.MinecraftArguments))
                {
                    return(null);
                }
                if (string.IsNullOrWhiteSpace(jver.MainClass))
                {
                    return(null);
                }
                if (string.IsNullOrWhiteSpace(jver.Assets))
                {
                    jver.Assets = "legacy";
                }
                if (jver.Libraries == null)
                {
                    return(null);
                }
                version.Id = jver.Id;
                version.MinecraftArguments = jver.MinecraftArguments ?? UsefulTools.PrintfArray(jver.arguments.game);
                version.Assets             = jver.Assets;
                version.MainClass          = jver.MainClass;
                version.JarId     = jver.JarId;
                version.Libraries = new List <Library>();
                version.Natives   = new List <Native>();
                foreach (var lib in jver.Libraries)
                {
                    if (string.IsNullOrWhiteSpace(lib.Name))
                    {
                        continue;
                    }
                    var names = lib.Name.Split(':');
                    if (names.Length != 3)
                    {
                        continue;
                    }
                    if (lib.Natives == null)
                    {
                        if (!IfAllowed(lib.Rules))
                        {
                            continue;
                        }
                        version.Libraries.Add(new Library
                        {
                            NS      = names[0],
                            Name    = names[1],
                            Version = names[2]
                        });
                    }
                    else
                    {
                        if (!IfAllowed(lib.Rules))
                        {
                            continue;
                        }
                        var native = new Native
                        {
                            NS           = names[0],
                            Name         = names[1],
                            Version      = names[2],
                            NativeSuffix = lib.Natives["windows"].Replace("${arch}", SystemTools.GetArch())
                        };
                        version.Natives.Add(native);
                        if (lib.Extract != null)
                        {
                            native.Options = new UnzipOptions {
                                Exclude = lib.Extract.Exculde
                            };
                        }
                    }
                }
                if (jver.InheritsVersion != null)
                {
                    var target = GetVersionInternal(jver.InheritsVersion);
                    if (target == null)
                    {
                        return(null);
                    }
                    else
                    {
                        if (version.Assets == "legacy")
                        {
                            version.Assets = null;
                        }
                        version.Assets             = version.Assets ?? target.Assets;
                        version.JarId              = version.JarId ?? target.JarId;
                        version.MainClass          = version.MainClass ?? target.MainClass;
                        version.MinecraftArguments = version.MinecraftArguments ?? target.MinecraftArguments;
                        version.Natives.AddRange(target.Natives);
                        version.Libraries.AddRange(target.Libraries);
                    }
                }
                version.JarId = version.JarId ?? version.Id;
                _versions.Add(version.Id, version);
                return(version);
            }
            catch
            {
                return(null);
            }
            finally
            {
                _locatingVersion.Remove(id);
            }
        }