예제 #1
0
        public static MVersion ParseFromJson(string json)
        {
            try
            {
                var job = JObject.Parse(json);

                // id
                var id = job["id"]?.ToString();
                if (string.IsNullOrEmpty(id))
                {
                    throw new MVersionParseException("Empty version id");
                }

                var version = new MVersion(id);

                // javaVersion
                version.JavaVersion = job["javaVersion"]?["component"]?.ToString();

                // assets
                var assetindex = job["assetIndex"];
                var assets     = job["assets"];
                if (assetindex != null)
                {
                    version.AssetId   = assetindex["id"]?.ToString();
                    version.AssetUrl  = assetindex["url"]?.ToString();
                    version.AssetHash = assetindex["sha1"]?.ToString();
                }
                else if (assets != null)
                {
                    version.AssetId = assets.ToString();
                }

                // client jar
                var client = job["downloads"]?["client"];
                if (client != null)
                {
                    version.ClientDownloadUrl = client["url"]?.ToString();
                    version.ClientHash        = client["sha1"]?.ToString();
                }

                // libraries
                if (job["libraries"] is JArray libJArr)
                {
                    var libList   = new List <MLibrary>(libJArr.Count);
                    var libParser = new MLibraryParser();
                    foreach (var item in libJArr)
                    {
                        var libs = libParser.ParseJsonObject((JObject)item);
                        if (libs != null)
                        {
                            libList.AddRange(libs);
                        }
                    }

                    version.Libraries = libList.ToArray();
                }

                // mainClass
                version.MainClass = job["mainClass"]?.ToString();

                // argument
                version.MinecraftArguments = job["minecraftArguments"]?.ToString();

                var ag = job["arguments"];
                if (ag != null)
                {
                    if (ag["game"] is JArray gameArg)
                    {
                        version.GameArguments = argParse(gameArg);
                    }
                    if (ag["jvm"] is JArray jvmArg)
                    {
                        version.JvmArguments = argParse(jvmArg);
                    }
                }

                // metadata
                version.ReleaseTime = job["releaseTime"]?.ToString();

                var type = job["type"]?.ToString();
                version.TypeStr = type;
                version.Type    = MVersionTypeConverter.FromString(type);

                // inherits
                if (job["inheritsFrom"] != null)
                {
                    version.IsInherited     = true;
                    version.ParentVersionId = job["inheritsFrom"]?.ToString();
                }
                else
                {
                    version.Jar = version.Id;
                }

                return(version);
            }
            catch (MVersionParseException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new MVersionParseException(ex);
            }
        }
        public static MVersion ParseFromJson(string json)
        {
            var version = new MVersion();
            var job     = JObject.Parse(json);

            // id
            version.Id = job["id"]?.ToString();

            // assets
            var assetindex = (JObject)job["assetIndex"];
            var assets     = job["assets"];

            if (assetindex != null)
            {
                version.AssetId   = n(assetindex["id"]?.ToString());
                version.AssetUrl  = n(assetindex["url"]?.ToString());
                version.AssetHash = n(assetindex["sha1"]?.ToString());
            }
            else if (assets != null)
            {
                version.AssetId = assets.ToString();
            }

            // client jar
            var client = job["downloads"]?["client"];

            if (client != null)
            {
                version.ClientDownloadUrl = client["url"]?.ToString();
                version.ClientHash        = client["sha1"]?.ToString();
            }

            // libraries
            var libJArr = (JArray)job["libraries"];
            var libList = new List <MLibrary>(libJArr.Count);

            foreach (var item in libJArr)
            {
                var libs = MLibraryParser.ParseJsonObject((JObject)item);
                if (libs != null)
                {
                    libList.AddRange(libs);
                }
            }
            version.Libraries = libList.ToArray();

            // mainClass
            version.MainClass = n(job["mainClass"]?.ToString());

            // argument
            var ma = job["minecraftArguments"]?.ToString();

            if (ma != null)
            {
                version.MinecraftArguments = ma;
            }

            var ag = job["arguments"];

            if (ag != null)
            {
                if (ag["game"] != null)
                {
                    version.GameArguments = argParse((JArray)ag["game"]);
                }
                if (ag["jvm"] != null)
                {
                    version.JvmArguments = argParse((JArray)ag["jvm"]);
                }
            }

            // metadata
            version.ReleaseTime = job["releaseTime"]?.ToString();

            var ype = job["type"]?.ToString();

            version.TypeStr = ype;
            version.Type    = MVersionTypeConverter.FromString(ype);

            // inherits
            if (job["inheritsFrom"] != null)
            {
                version.IsInherited     = true;
                version.ParentVersionId = job["inheritsFrom"].ToString();
            }
            else
            {
                version.Jar = version.Id;
            }

            return(version);
        }