Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="YamlMapping"/> class.
 /// </summary>
 /// <param name="mapping">The mapping.</param>
 public YamlMapping(Mapping mapping)
 {
     foreach (var entry in mapping.Enties)
     {
         Object value;
         if (YamlDocument.TryMapValue(entry.Value, out value))
         {
             innerValues[(entry.Key as Scalar).Text] = value;
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        ///     Parse permissions from plugin.yml
        /// </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        private List<PluginPermission> ParsePermissions(Mapping map)
        {
            try
            {
                List<PluginPermission> l = new List<PluginPermission>();

                Scalar sc = new Scalar();
                Type typeScalar = sc.GetType();
                Type typeMapping = map.GetType();
                if (map.GetType() != typeMapping) return l;
                foreach (MappingEntry entry in map.Enties)
                {
                    PluginPermission pp = new PluginPermission {Name = ((Scalar) entry.Key).Text};
                    if (entry.Value.GetType() != typeMapping) continue;
                    foreach (MappingEntry secondlevel in ((Mapping) entry.Value).Enties)
                    {
                        if (secondlevel.Key.GetType() != typeScalar) continue;
                        switch (((Scalar) secondlevel.Key).Text)
                        {
                            case "description":
                                pp.Description = ((Scalar) secondlevel.Value).Text;
                                break;
                            case "default":

                                break;
                            case "children":
                                pp.Children = new List<PluginPermissionChild>();
                                foreach (MappingEntry thirdlevel in ((Mapping) secondlevel.Value).Enties)
                                {
                                    PluginPermissionChild ppc = new PluginPermissionChild
                                    {
                                        Name = ((Scalar) thirdlevel.Key).Text
                                    };
                                    pp.Children.Add(ppc);
                                }

                                break;
                        }
                    }
                    l.Add(pp);
                }
                return l;
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Warning, "InstalledPlugin",
                    "An exception occured when trying to load plugin permissions", ex.Message);
                return new List<PluginPermission>();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Parse commands from plugin.yml
        /// </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        private static List<PluginCommand> ParseCommands(Mapping map)
        {
            try
            {
                List<PluginCommand> l = new List<PluginCommand>();

                Scalar sc = new Scalar();
                Sequence seq = new Sequence();
                Type typeScalar = sc.GetType();
                Type typeSequence = seq.GetType();
                Type typeMapping = map.GetType();
                if (map.GetType() != typeMapping) return l;

                foreach (MappingEntry entry in map.Enties)
                {
                    PluginCommand pc = new PluginCommand {Name = ((Scalar) entry.Key).Text};

                    if (entry.Value.GetType() == typeMapping)
                    {
                        foreach (MappingEntry secondlevel in ((Mapping) entry.Value).Enties)
                        {
                            if (secondlevel.Key.GetType() != typeScalar) continue;
                            switch (((Scalar) secondlevel.Key).Text)
                            {
                                case "description":
                                    pc.Description = ((Scalar) secondlevel.Value).Text;
                                    break;
                                case "usage":
                                    pc.Usage = ((Scalar) secondlevel.Value).Text;
                                    break;
                                case "alias":
                                    if (entry.Value.GetType() == typeSequence)
                                    {
                                        pc.Aliases = ArrayFromSequence((Sequence) secondlevel.Value);
                                    }
                                    else if (entry.Value.GetType() == typeScalar)
                                    {
                                        pc.Aliases = new string[1];
                                        // ReSharper disable once PossibleInvalidCastException
                                        pc.Aliases[0] = ((Scalar) entry.Value).Text;
                                    }
                                    break;
                                case "aliases":
                                    if (entry.Value.GetType() == typeSequence)
                                    {
                                        pc.Aliases = ArrayFromSequence((Sequence) secondlevel.Value);
                                    }
                                    else if (entry.Value.GetType() == typeScalar)
                                    {
                                        pc.Aliases = new string[1];
            // ReSharper disable once PossibleInvalidCastException
                                        pc.Aliases[0] = ((Scalar) entry.Value).Text;
                                    }
                                    break;
                            }
                        }
                    }

                    l.Add(pc);
                }
                return l;
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Warning, "InstalledPlugin",
                    "An exception occured when trying to load plugin commands", ex.Message);
                return new List<PluginCommand>();
            }
        }
Ejemplo n.º 4
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;
            }
        }
Ejemplo n.º 5
0
 private static IEnumerable<FeatureDescriptor> GetFeaturesForExtension(Mapping features, ExtensionDescriptor extensionDescriptor)
 {
     var featureDescriptors = new List<FeatureDescriptor>();
     if (features != null) {
         foreach (var entity in features.Entities) {
             var featureDescriptor = new FeatureDescriptor {
                 Extension = extensionDescriptor,
                 Name = entity.Key.ToString(),
             };
             var featureMapping = (Mapping)entity.Value;
             foreach (var featureEntity in featureMapping.Entities) {
                 if (String.Equals(featureEntity.Key.ToString(), "description", StringComparison.OrdinalIgnoreCase)) {
                     featureDescriptor.Description = featureEntity.Value.ToString();
                 }
                 else if (String.Equals(featureEntity.Key.ToString(), "category", StringComparison.OrdinalIgnoreCase)) {
                     featureDescriptor.Category = featureEntity.Value.ToString();
                 }
                 else if (String.Equals(featureEntity.Key.ToString(), "dependencies", StringComparison.OrdinalIgnoreCase)) {
                     featureDescriptor.Dependencies = ParseFeatureDependenciesEntry(featureEntity.Value.ToString());
                 }
             }
             featureDescriptors.Add(featureDescriptor);
         }
     }
     if (!featureDescriptors.Any(fd => fd.Name == extensionDescriptor.Name)) {
         featureDescriptors.Add(new FeatureDescriptor {
             Name = extensionDescriptor.Name,
             Dependencies = new string[0],
             Extension = extensionDescriptor,
         });
     }
     return featureDescriptors;
 }
Ejemplo n.º 6
0
		private Mapping ParseBlockMapping(out bool success)
		{
			Mapping mapping = new Mapping();

			while (true)
			{
				int seq_start_position1 = position;
				MappingEntry mappingEntry = ParseBlockMappingEntry(out success);
				if (success) { mapping.Enties.Add(mappingEntry); }
				else
				{
					Error("Failed to parse BlockMappingEntry of BlockMapping.");
					break;
				}

				while (true)
				{
					while (true)
					{
						int seq_start_position2 = position;
						ParseIndent(out success);
						if (!success)
						{
							Error("Failed to parse Indent of BlockMapping.");
							break;
						}

						mappingEntry = ParseBlockMappingEntry(out success);
						if (success) { mapping.Enties.Add(mappingEntry); }
						else
						{
							Error("Failed to parse BlockMappingEntry of BlockMapping.");
							position = seq_start_position2;
						}
						break;
					}
					if (!success) { break; }
				}
				success = true;
				break;
			}
			if (!success) { Error("Failed to parse Enties of BlockMapping."); }
			return mapping;
		}
Ejemplo n.º 7
0
		private Mapping ParseFlowMapping(out bool success)
		{
			Mapping mapping = new Mapping();
			int start_position = position;

			MatchTerminal('{', out success);
			if (!success)
			{
				Error("Failed to parse '{' of FlowMapping.");
				position = start_position;
				return mapping;
			}

			ParseSeparationLinesInFlow(out success);
			success = true;

			while (true)
			{
				int seq_start_position1 = position;
				MappingEntry mappingEntry = ParseFlowMappingEntry(out success);
				if (success) { mapping.Enties.Add(mappingEntry); }
				else
				{
					Error("Failed to parse FlowMappingEntry of FlowMapping.");
					break;
				}

				while (true)
				{
					while (true)
					{
						int seq_start_position2 = position;
						MatchTerminal(',', out success);
						if (!success)
						{
							Error("Failed to parse ',' of FlowMapping.");
							break;
						}

						ParseSeparationLinesInFlow(out success);
						success = true;

						mappingEntry = ParseFlowMappingEntry(out success);
						if (success) { mapping.Enties.Add(mappingEntry); }
						else
						{
							Error("Failed to parse FlowMappingEntry of FlowMapping.");
							position = seq_start_position2;
						}
						break;
					}
					if (!success) { break; }
				}
				success = true;
				break;
			}
			if (!success)
			{
				Error("Failed to parse Enties of FlowMapping.");
				position = start_position;
				return mapping;
			}

			MatchTerminal('}', out success);
			if (!success)
			{
				Error("Failed to parse '}' of FlowMapping.");
				position = start_position;
			}

			return mapping;
		}