/// <summary>
        /// Loads document froms file.
        /// </summary>
        /// <param name="filePath">Full file path.</param>
        /// <returns>yaml document.</returns>
        public static dynamic FromFile(String filePath)
        {
            var document = YamlParser.Load(filePath).Documents.Last();

            Object result;

            if (TryMapValue(document.Root, out result))
            {
                return(result);
            }

            throw new InvalidOperationException("Unexpected parsed value.");
        }
Beispiel #2
0
        private void ProcessFile(Object state)
        {
            var logger = ServiceLocator.Current.GetInstance <ILogger>();
            var file   = state as String;

            if (!String.IsNullOrEmpty(file) && File.Exists(file))
            {
                Stopwatch startTime = Stopwatch.StartNew();

                while (true)
                {
                    if (IsFileAccessible(file))
                    {
                        try
                        {
                            var yaml = YamlParser.Load(file).Documents;
                            foreach (var document in yaml)
                            {
                                AddNode(document.Root, String.Empty);
                            }

                            break;
                        }
                        catch (Exception e)
                        {
                            logger.ErrorFormat("Some erro was occured during \"{0}\" processing ({1}).", file, e.Message);
                        }
                    }

                    // Calculate the elapsed time and stop if the maximum retry
                    // period has been reached.
                    TimeSpan timeElapsed = startTime.Elapsed;

                    if (timeElapsed > maximumRetryPeriod)
                    {
                        logger.ErrorFormat("The file \"{0}\" could not be processed.", file);
                        break;
                    }

                    Thread.Sleep(retryDelay);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        ///     Loads the contents of a plugin.yml file
        /// </summary>
        /// <param name="ymltext">the yml formatted text</param>
        /// <returns>The InstalledPlugin (me)</returns>
        /// <remarks></remarks>
        public InstalledPlugin LoadPluginYml(string ymltext)
        {
            try
            {
                Scalar   sc  = new Scalar();
                Sequence seq = new Sequence();
                Mapping  map = new Mapping();

                //References to check file types later on
                Type typeScalar   = sc.GetType();
                Type typeSequence = seq.GetType();
                Type typeMapping  = map.GetType();


                if (ymltext == null | string.IsNullOrEmpty(ymltext))
                {
                    return(null);
                }

                YamlStream yml = YamlParser.Load(ymltext);

                if (yml == null)
                {
                    return(null);
                }

                //if mapping start parsing
                if (yml.Documents[0].Root.GetType() == typeMapping)
                {
                    foreach (MappingEntry item in ((Mapping)yml.Documents[0].Root).Enties)
                    {
                        //Check the type, check for possible keys and load the value
                        if (item.Value.GetType() == typeScalar)
                        {
                            switch (((Scalar)item.Key).Text)
                            {
                            case "name":
                                Name = ((Scalar)item.Value).Text;
                                break;

                            case "version":
                                Version = ((Scalar)item.Value).Text;
                                break;

                            case "description":
                                Description = ((Scalar)item.Value).Text;
                                break;

                            case "main":
                                Mainspace = ((Scalar)item.Value).Text;
                                break;

                            case "author":
                                Authors    = new string[1];
                                Authors[0] = ((Scalar)item.Value).Text;
                                break;

                            case "authors":
                                Authors    = new string[1];
                                Authors[0] = ((Scalar)item.Value).Text;
                                break;
                            }
                        }
                        else if (item.Value.GetType() == typeSequence)
                        {
                            switch (((Scalar)item.Key).Text)
                            {
                            case "author":
                                Authors = ArrayFromSequence((Sequence)item.Value);
                                break;

                            case "Authors":
                                Authors = ArrayFromSequence((Sequence)item.Value);
                                break;

                            case "softdepend":
                                Softdepend = ArrayFromSequence((Sequence)item.Value);
                                break;
                            }
                        }
                        else if (item.Value.GetType() == typeMapping)
                        {
                            switch (((Scalar)item.Key).Text)
                            {
                            case "commands":
                                if (item.Value.GetType() == typeMapping)
                                {
                                    Commands = ParseCommands((Mapping)item.Value);
                                }
                                else
                                {
                                    Commands = new List <PluginCommand>();
                                }
                                break;

                            case "permissions":
                                if (item.Value.GetType() == typeMapping)
                                {
                                    Permissions = ParsePermissions((Mapping)item.Value);
                                }
                                else
                                {
                                    Permissions = new List <PluginPermission>();
                                }
                                break;
                            }
                        }
                    }
                }
                return(this);
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Warning, "InstalledPlugin", "An exception occured when trying to parse yml text",
                           ex.Message);
                return(null);
            }
        }