public BukgetPluginVersion(BukgetPlugin plugin, String jsonCode)
        {
            {
                InitFields();
                //create JSON object
                JsonObject json = (JsonObject)JsonConvert.Import(jsonCode);

                Plugin = plugin;

                if (json["version"] == null)
                {
                    VersionNumber = null;
                    return;
                }

                VersionNumber = json["version"].ToString();

                {
                    CompatibleBuilds =
                        JsonParser.ParseJsonStringList(
                            json["game_versions"]);
                }


                if (json["download"] != null)
                {
                    // BukkitDev Compliancy
                    // Since this program is distributed using dev.bukkit.org, every download url needs to have the "http://dev.bukkit.org" part hardcoded

                    DownloadLink = json["download"].ToString();
                    if (DownloadLink.StartsWith("http://") || DownloadLink.StartsWith("https://"))
                    {
                        DownloadLink = DownloadLink.Substring(DownloadLink.IndexOf('/', 10));
                        DownloadLink = "http://dev.bukkit.org/" + DownloadLink;
                    }
                }


                if (json["link"] != null)
                {
                    PageLink = json["link"].ToString();
                }


                if (json["date"] != null)
                {
                    ReleaseDate = new DateTime(1970, 1, 1).AddSeconds(Convert.ToDouble(json["date"].ToString()));
                }


                if (json["filename"] != null)
                {
                    Filename = json["filename"].ToString();
                }


                if (json["status"] != null)
                {
                    Type = (PluginStatus)Enum.Parse(typeof(PluginStatus), json["status"].ToString().Replace("-", "_"));
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     Parse a bukget plugin from the json result
        /// </summary>
        /// <param name="jsonCode"></param>
        /// <returns></returns>
        public BukgetPlugin(string jsonCode)
        {
            InitFields();
            if (string.IsNullOrEmpty(jsonCode))
            {
                throw new FormatException("Invalid JSON supplied: string is empty");
            }

            if (Equals(jsonCode, "[]"))
            {
                throw new FormatException("Invalid JSON supplied: array is empty");
            }
            // Load the string into a json object
            JsonObject json;

            // In case of an array, load the first entry
            try
            {
                if (jsonCode.StartsWith("["))
                {
                    json = (JsonObject)JsonConvert.Import <JsonArray>(jsonCode)[0];
                }
                else
                {
                    json = JsonConvert.Import <JsonObject>(jsonCode);
                }
            }
            catch (Exception exception)
            {
                throw new FormatException("Invalid JSON supplied: " + exception);
            }

            if (json["main"] != null)
            {
                Main = (string)json["main"];
            }


            if (json["plugin_name"] != null)
            {
                Name = (string)json["plugin_name"];
            }

            // If no name or mainspace, return
            if (string.IsNullOrEmpty(Main) || string.IsNullOrEmpty(Name))
            {
                return;
            }

            Logger.Log(LogLevel.Info, "BukGetAPI", "parsing plugin:" + Name, Main);

            // Slug
            if (json["slug"] != null)
            {
                Slug = (String)json["slug"];
            }
            // Description
            if (json["description"] != null)
            {
                Description = (String)json["description"];
            }
            // BukkitDev link
            if (json["dbo_page"] != null)
            {
                BukkitDevLink = (String)json["dbo_page"];
            }
            // Website
            if (json["link"] != null)
            {
                Website = (String)json["link"];
            }
            // Stage
            if (json["stage"] != null)
            {
                try
                {
                    Status = (PluginStatus)Enum.Parse(typeof(PluginStatus), json["stage"].ToString());
                }
                catch (Exception e)
                {
                    Logger.Log(LogLevel.Warning, "BukgetV3", "Couldn't parse plugin status", e.Message);
                }
            }
            // Authors
            AuthorsList = JsonParser.ParseJsonStringList(json["authors"]);

            // Categories
            // load strings
            List <String> categories = JsonParser.ParseJsonStringList(json["categories"]);

            // convert to enum values
            foreach (string category in categories)
            {
                CategoryList.Add((PluginCategory)Enum.Parse(typeof(PluginCategory), category));
            }

            // Versions
            IEnumerable <JsonObject> versions = JsonParser.ParseJsonObjectList(json["versions"]);

            foreach (JsonObject jsonObject in versions)
            {
                BukgetPluginVersion v = new BukgetPluginVersion(this, jsonObject.ToString());
                if (v.VersionNumber != null)
                {
                    VersionsList.Add(v);
                }
            }
            LastParsedPlugin = this;
        }