예제 #1
0
        /// <summary>
        ///     Tries to parse the <see cref="version" /> to a <see cref="PluginVersion" />
        /// </summary>
        /// <param name="version">The raw string</param>
        /// <param name="result">The output</param>
        /// <returns>True if successful, false if not</returns>
        public static bool TryParse(string version, out PluginVersion result)
        {
            result = null;

            var temp = new PluginVersion();

            if (string.IsNullOrEmpty(version))
            {
                return(false);
            }

            char[] chArray1   = { '.' };
            var    textArray1 = version.Split(chArray1);

            if (textArray1.Length != 2)
            {
                return(false);
            }

            if (!int.TryParse(textArray1[0], NumberStyles.Any, CultureInfo.InvariantCulture, out temp._major) ||
                temp._major < 0)
            {
                return(false);
            }

            if (!int.TryParse(textArray1[1], NumberStyles.Any, CultureInfo.InvariantCulture, out temp._minor) ||
                temp._minor < 0)
            {
                return(false);
            }

            result = temp;
            return(true);
        }
예제 #2
0
        /// <summary>
        ///     Parse the <see cref="version" /> to a <see cref="PluginVersion" />
        /// </summary>
        /// <param name="version">The raw string</param>
        public static PluginVersion Parse(string version)
        {
            var result = new PluginVersion();

            if (string.IsNullOrEmpty(version))
            {
                throw new ArgumentNullException(nameof(version));
            }

            char[] chArray1   = { '.' };
            var    textArray1 = version.Split(chArray1);

            if (textArray1.Length != 2)
            {
                throw new ArgumentException("Couldn't parse string");
            }

            result._major = int.Parse(textArray1[0], CultureInfo.InvariantCulture);
            if (result._major < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(version), "ArgumentOutOfRange_Version");
            }

            result._minor = int.Parse(textArray1[1], CultureInfo.InvariantCulture);
            if (result._minor < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(version), "ArgumentOutOfRange_Version");
            }

            return(result);
        }
예제 #3
0
        public void LoadPlugin(string file, PluginVersion pluginVersion)
        {
            var pluginAssembly = Assembly.Load(Decompress(File.ReadAllBytes(file)));
            var types          = pluginAssembly.GetTypes();
            var commandType    = types.First(x => x.IsSubclassOf(typeof(Command)));

            try
            {
                var command = (Command)Activator.CreateInstance(commandType);
                if (command == null)
                {
                    ErrorReporter.Current.ReportError(new Exception("Command could not be created"), "LoadPlugin: Check");
                    return;
                }

                if (command.Identifier < 1000)
                {
                    ErrorReporter.Current.ReportError(new Exception("Command Id below 1000"), "LoadPlugin: Check");
                    return;
                }

                //prevent duplicate commands
                var allCommands     = Commands.Select(x => (Command)Activator.CreateInstance(x));
                var existingCommand = allCommands.FirstOrDefault(x => x.Identifier == command.Identifier);
                if (existingCommand != null)
                {
                    Commands.Remove(existingCommand.GetType());
                }
            }
            catch (Exception ex)
            {
                ErrorReporter.Current.ReportError(ex, "LoadPlugin: Check");
                return;
            }

            Commands.Add(commandType);

            CommandLoaded?.Invoke(this, new CommandLoadedEventArgs(commandType));
        }