Ejemplo n.º 1
0
        /// <summary>
        /// Parses a description of an addon and extracts Type and Tags if it was an appropriate JSON string.
        /// </summary>
        /// <param name="readDescription">The whole description read from the file.</param>
        /// <param name="type">The type of the addon.</param>
        /// <param name="tags">The tag list of the addon.</param>
        /// <returns>The description part of the readDescription input (if it was JSON) or the whole input.</returns>
        public static string ParseDescription(string readDescription, ref string type, ref List <string> tags)
        {
            string description     = readDescription; // By default, the description is the whole we read.
            string newline         = Environment.NewLine.Replace("\r", "\\u000d").Replace("\n", "\\u000a");
            string descTempReplace = readDescription.Replace("\\n", newline).Replace("\\t", "\\u0009");

            using (MemoryStream descStream = new MemoryStream(Encoding.ASCII.GetBytes(descTempReplace)))
            {
                byte[] bytes = new byte[(int)descStream.Length];
                descStream.Read(bytes, 0, (int)descStream.Length);
                descStream.Seek(0, SeekOrigin.Begin);

                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(DescriptionJSON));
                try
                {
                    DescriptionJSON dJSON = (DescriptionJSON)jsonSerializer.ReadObject(descStream);

                    description = dJSON.Description; // If there's a description in the JSON, make it the returned description
                    type        = dJSON.Type;
                    tags        = new List <string>(dJSON.Tags);
                }
                catch (SerializationException)
                {
                    // The description is a plaintext in the file.
                    type = String.Empty;
                    tags = new List <string>();
                }
            }

            return(description);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a JSON string using the properties of the provided Addon.
        /// </summary>
        /// <param name="addon">The addon which metadata is to be used.</param>
        /// <returns>The compiled JSON string.</returns>
        /// <exception cref="AddonJSONException">Errors regarding creating the JSON.</exception>
        public static string BuildDescription(Addon addon)
        {
            DescriptionJSON tree = new DescriptionJSON();
            tree.Description = addon.Description;

            // Load the addon type
            if (addon.Type.ToLowerInvariant() == String.Empty || addon.Type.ToLowerInvariant() == null)
                throw new AddonJSONException("type is empty!");
            else
            {
                if (!SharpGMad.Tags.TypeExists(addon.Type.ToLowerInvariant()))
                    throw new AddonJSONException("type isn't a supported type!");
                else
                    tree.Type = addon.Type.ToLowerInvariant();
            }

            // Parse the tags
            tree.Tags = new List<string>();
            if (addon.Tags.Count > 2)
                throw new AddonJSONException("too many tags - specify 2 only!");
            else
            {
                foreach (string tag in addon.Tags)
                {
                    if (tag == String.Empty || tag == null) continue;

                    if (!SharpGMad.Tags.TagExists(tag.ToLowerInvariant()))
                        throw new AddonJSONException("tag isn't a supported word!");
                    else
                        tree.Tags.Add(tag.ToLowerInvariant());
                }
            }

            string strOutput;

            using (MemoryStream stream = new MemoryStream())
            {
                DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(DescriptionJSON));

                try
                {
                    jsonFormatter.WriteObject(stream, tree);
                }
                catch (SerializationException ex)
                {
                    throw new AddonJSONException("Couldn't create json", ex);
                }

                stream.Seek(0, SeekOrigin.Begin);
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, (int)stream.Length);
                strOutput = Encoding.ASCII.GetString(bytes);
            }

            return strOutput;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a JSON string using the properties of the provided Addon.
        /// </summary>
        /// <param name="addon">The addon which metadata is to be used.</param>
        /// <returns>The compiled JSON string.</returns>
        /// <exception cref="AddonJSONException">Errors regarding creating the JSON.</exception>
        public static string BuildDescription(Addon addon)
        {
            DescriptionJSON tree = new DescriptionJSON();

            tree.Description = addon.Description;

            // Load the addon type
            if (addon.Type.ToLowerInvariant() == String.Empty || addon.Type.ToLowerInvariant() == null)
            {
                throw new AddonJSONException("type is empty!");
            }
            else
            {
                if (!SharpGMad.Tags.TypeExists(addon.Type.ToLowerInvariant()))
                {
                    throw new AddonJSONException("type isn't a supported type!");
                }
                else
                {
                    tree.Type = addon.Type.ToLowerInvariant();
                }
            }

            // Parse the tags
            tree.Tags = new List <string>();
            if (addon.Tags.Count > 2)
            {
                throw new AddonJSONException("too many tags - specify 2 only!");
            }
            else
            {
                foreach (string tag in addon.Tags)
                {
                    if (tag == String.Empty || tag == null)
                    {
                        continue;
                    }

                    if (!SharpGMad.Tags.TagExists(tag.ToLowerInvariant()))
                    {
                        throw new AddonJSONException("tag isn't a supported word!");
                    }
                    else
                    {
                        tree.Tags.Add(tag.ToLowerInvariant());
                    }
                }
            }

            string strOutput;

            using (MemoryStream stream = new MemoryStream())
            {
                DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(DescriptionJSON));

                try
                {
                    jsonFormatter.WriteObject(stream, tree);
                }
                catch (SerializationException ex)
                {
                    throw new AddonJSONException("Couldn't create json", ex);
                }

                stream.Seek(0, SeekOrigin.Begin);
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, (int)stream.Length);
                strOutput = Encoding.ASCII.GetString(bytes);
                strOutput = strOutput.Replace("\\u000d", "").Replace("\\u0009", "\\t").Replace("\\u000a", "\\n");
            }

            return(strOutput);
        }