Exemplo n.º 1
0
        /// <summary>Loads the given config into an object from the xml file in the config folder</summary>
        /// <typeparam name="T">The object type to be returned and loaded</typeparam>
        /// <param name="Filename">The name of the config</param>
        /// <returns>Loads the given config into an object from the xml file in the config folder</returns>
        /// <exception cref="ArgumentException" />
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="ArgumentOutOfRangeException" />
        /// <exception cref="COMException" />
        /// <exception cref="DirectoryNotFoundException" />
        /// <exception cref="FileNotFoundException" />
        /// <exception cref="InvalidComObjectException" />
        /// <exception cref="IOException" />
        /// <exception cref="MethodAccessException" />
        /// <exception cref="MemberAccessException" />
        /// <exception cref="MissingMethodException" />
        /// <exception cref="NotSupportedException" />
        /// <exception cref="PathTooLongException" />
        /// <exception cref="SecurityException" />
        /// <exception cref="UnauthorizedAccessException" />
        /// <exception cref="TargetInvocationException" />
        /// <exception cref="TypeLoadException" />
        public static T LoadConfig <T>(String Filename)
        {
            String          Filepath = ConfigOptions.ConfigFolder + Filename + ".xml";
            EventWaitHandle Lock     = ConfigLoader.GetLock(Filepath);
            T Out = default;

            if (File.Exists(Filepath))
            {
                IConfigDeserializer <T> deserializer;
                FileStream reader = null;

                Lock.WaitOne();
#if !DEBUG
                try {
#endif
                deserializer = ConfigLoader._SerializerFactory.GetDeserializer <T>();
                reader       = new FileStream(Filepath, FileMode.Open);

                Out = deserializer.Deserialize(reader);
                reader.Close();
#if !DEBUG
            }
            catch (Exception ex) {
                if (reader != null)
                {
                    reader.Close();
                }

                ErrorLoading(Filepath, ex);
            }
#endif
                Lock.Set();
            }

            if (Out == null)
            {
                Out = Activator.CreateInstance <T>();

                if (Out.GetType().GetInterface(nameof(INewConfig)) != null)
                {
                    var Temp = (INewConfig)Out;
                    Temp.SetNewInformation();
                }

#if Debug
                Task.Run(() => SaveConfig(Out, Filename));
#else
                SaveConfig(Out, Filename);
#endif
            }

            return(Out);
        }
Exemplo n.º 2
0
        /// <summary>Save the given object into a file</summary>
        /// <param name="ConfigObject">The object to save</param>
        /// <param name="Filename">The filename the objects should get</param>
        /// <exception cref="ArgumentException" />
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="ArgumentOutOfRangeException" />
        /// <exception cref="DirectoryNotFoundException" />
        /// <exception cref="FileNotFoundException" />
        /// <exception cref="IOException" />
        /// <exception cref="InvalidOperationException" />
        /// <exception cref="NotSupportedException" />
        /// <exception cref="PathTooLongException" />
        /// <exception cref="UnauthorizedAccessException" />
        /// <exception cref="System.Security.SecurityException" />
        public static void SaveConfig(Object ConfigObject, String Filename)
        {
            //Get file info on the file to save to
            String          Filepath = ConfigOptions.ConfigFolder + Filename + ConfigOptions.ConfigExtension;
            var             FI       = new FileInfo(Filepath);
            EventWaitHandle Lock     = ConfigLoader.GetLock(Filepath);

            //Check parent directory
            DirectoryInfo DI = FI.Directory;

            //Create if missing
            if (!DI.Exists)
            {
                DI.Create();
            }

            //Setup
            IConfigSerializer <Object> serializer = null;
            FileStream writer = null;

            Lock.WaitOne();
#if !DEBUG
            try {
#endif
            //Assign serializer and stream
            serializer = ConfigLoader._SerializerFactory.GetSerializer(ConfigObject.GetType());
            writer     = new FileStream(FI.FullName, FileMode.Create);

            //Serialize object
            serializer.Serialize(ConfigObject, writer);

            //Flush and close
            writer.Flush();
            writer.Close();
#if !DEBUG
        }
#pragma warning disable 168
        catch (Exception ex) {
#pragma warning restore 168
            //Close stream if open
            if (writer != null)
            {
                writer.Close();
            }

            ErrorSaving(Filepath, ex);
        }
#endif

            Lock.Set();
        }
        /// <summary>Retrieves the config using the given type, either loads it from memory or from the file, if none are succesfull a new instance is created and saved</summary>
        /// <param name="T">the object type to get</param>
        /// <param name="PresistObject">If true the object is stored in memory, if false then the config is just loaded from the file</param>
        /// <returns>Returns a <see cref="Object"/></returns>
        /// <exception cref="AmbiguousMatchException" />
        /// <exception cref="ArgumentException" />
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="ArgumentOutOfRangeException" />
        /// <exception cref="COMException" />
        /// <exception cref="DirectoryNotFoundException" />
        /// <exception cref="FileNotFoundException" />
        /// <exception cref="InvalidComObjectException" />
        /// <exception cref="IOException" />
        /// <exception cref="MethodAccessException" />
        /// <exception cref="MemberAccessException" />
        /// <exception cref="MissingMethodException" />
        /// <exception cref="NotSupportedException" />
        /// <exception cref="PathTooLongException" />
        /// <exception cref="SecurityException" />
        /// <exception cref="TypeLoadException" />
        /// <exception cref="TargetInvocationException" />
        /// <exception cref="UnauthorizedAccessException" />
        public static Object Get(Type T, Boolean PresistObject)
        {
            if (PresistObject)
            {
                if (!ConfigMapper._Configs.ContainsKey(T))
                {
                    Load(T);
                }

                return(ConfigMapper._Configs[T]);
            }

            String Name = GetName(T);

            return(ConfigLoader.LoadConfig(T, Name));
        }
        /// <summary>Pre initializes the values inside of this class</summary>
        /// <param name="LoadConfigObjects">Marks if it should load the given objects from the loaded assemblies</param>
        /// <exception cref="AppDomainUnloadedException" />
        /// <exception cref="AmbiguousMatchException" />
        /// <exception cref="ArgumentException" />
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="FileNotFoundException" />
        /// <exception cref="NotSupportedException" />
        /// <exception cref="OverflowException" />
        /// <exception cref="TypeLoadException" />
        public static void Preload(Boolean LoadConfigObjects = true)
        {
            //Preload the config loader
            ConfigLoader.Preload();

            if (LoadConfigObjects)
            {
                //Load config objects from assemblies
                Assembly[] Assemblies = AppDomain.CurrentDomain.GetAssemblies();

                for (Int32 I = 0; I < Assemblies.Length; I++)
                {
                    Preload(Assemblies[I]);
                }
            }
        }
        /// <summary>Retrieves the config using the given type, either loads it from memory or from the file, if none are succesfull a new instance is created and saved</summary>
        /// <typeparam name="T">The config object to be returned</typeparam>
        /// <param name="PresistObject">If true the object is stored in memory, if false then the config is just loaded from the file</param>
        /// <returns>Retrieves the config using the given type, either loads it from memory or from the file, if none are succesfull a new instance is created and saved</returns>
        /// <exception cref="AmbiguousMatchException" />
        /// <exception cref="ArgumentException" />
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="ArgumentOutOfRangeException" />
        /// <exception cref="COMException" />
        /// <exception cref="DirectoryNotFoundException" />
        /// <exception cref="FileNotFoundException" />
        /// <exception cref="InvalidComObjectException" />
        /// <exception cref="IOException" />
        /// <exception cref="MethodAccessException" />
        /// <exception cref="MemberAccessException" />
        /// <exception cref="MissingMethodException" />
        /// <exception cref="NotSupportedException" />
        /// <exception cref="PathTooLongException" />
        /// <exception cref="SecurityException" />
        /// <exception cref="TypeLoadException" />
        /// <exception cref="TargetInvocationException" />
        /// <exception cref="UnauthorizedAccessException" />
        public static T Get <T>(Boolean PresistObject)
        {
            Type Temp = typeof(T);

            if (PresistObject)
            {
                if (!ConfigMapper._Configs.ContainsKey(Temp))
                {
                    Load(Temp);
                }

                return((T)ConfigMapper._Configs[Temp]);
            }

            String Name = GetName(Temp);

            return(ConfigLoader.LoadConfig <T>(Name));
        }
Exemplo n.º 6
0
        /// <summary>saves all the config in memory to a file</summary>
        /// <exception cref="AmbiguousMatchException" />
        /// <exception cref="ArgumentException" />
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="ArgumentOutOfRangeException" />
        /// <exception cref="DirectoryNotFoundException" />
        /// <exception cref="FileNotFoundException" />
        /// <exception cref="IOException" />
        /// <exception cref="InvalidOperationException" />
        /// <exception cref="NotSupportedException" />
        /// <exception cref="PathTooLongException" />
        /// <exception cref="SecurityException" />
        /// <exception cref="UnauthorizedAccessException" />
        public static void SaveAll()
        {
            var Keys = new Type[ConfigMapper._Configs.Count];

            ConfigMapper._Configs.Keys.CopyTo(Keys, 0);
            Type   Key;
            Object Item;

            for (Int32 I = 0; I < Keys.Length; I++)
            {
                Key = Keys[I];

                if (ConfigMapper._Configs.ContainsKey(Key))
                {
                    Item = ConfigMapper._Configs[Key];

                    ConfigLoader.SaveConfig(Item, GetName(Key));
                }
            }
        }
        /// <summary>Initializes the instance of <see cref="ConfigLoader"/></summary>
        static ConfigLoader()
        {
            ConfigLoader._Locks     = new EventWaitHandle[ConfigOptions.LockCount];
            ConfigLoader._Factories = new ConcurrentDictionary <String, IConfigSerializerFactory>();
            Int32 Count = ConfigLoader._Locks.Length;

            for (Int32 I = 0; I < Count; I++)
            {
                ConfigLoader._Locks[I] = new AutoResetEvent(true);
            }

            Assembly[] Assemblies = AppDomain.CurrentDomain.GetAssemblies();
            Count = Assemblies.Length;

            for (Int32 I = 0; I < Count; I++)
            {
                ConfigLoader.AddFactories(Assemblies[I]);
            }

            ConfigLoader.SwitchFactory();
        }
Exemplo n.º 8
0
        /// <summary>Loads the specified object from file storage or creates a new instance</summary>
        /// <param name="T">The type of the object to retrieve</param>
        /// <param name="Filename">the name of the config</param>
        /// <returns>A <see cref="Object"/> is returned</returns>
        /// <exception cref="ArgumentException" />
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="ArgumentOutOfRangeException" />
        /// <exception cref="COMException" />
        /// <exception cref="DirectoryNotFoundException" />
        /// <exception cref="FileNotFoundException" />
        /// <exception cref="InvalidComObjectException" />
        /// <exception cref="IOException" />
        /// <exception cref="MethodAccessException" />
        /// <exception cref="MemberAccessException" />
        /// <exception cref="MissingMethodException" />
        /// <exception cref="NotSupportedException" />
        /// <exception cref="PathTooLongException" />
        /// <exception cref="SecurityException" />
        /// <exception cref="UnauthorizedAccessException" />
        /// <exception cref="TargetInvocationException" />
        /// <exception cref="TypeLoadException" />
        public static Object LoadConfig(Type T, String Filename)
        {
            //Create the filepath of where the config file could be found
            String          Filepath = ConfigOptions.ConfigFolder + Filename + ConfigOptions.ConfigExtension;
            EventWaitHandle Lock     = ConfigLoader.GetLock(Filepath);
            Object          Out      = null;

            //Check if file exists
            if (File.Exists(Filepath))
            {
                //Setup
                IConfigDeserializer <Object> deserializer;
                FileStream reader = null;

                Lock.WaitOne();
#if !DEBUG
                try {
#endif
                //Create the deserializer and stream
                deserializer = ConfigLoader._SerializerFactory.GetDeserializer(T);
                reader       = new FileStream(Filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                //Deserialize file into object
                Out = deserializer.Deserialize(reader);
                reader.Close();
#if !DEBUG
            }
            catch (Exception ex) {
                //Close of the stream, write error report
                if (reader != null)
                {
                    reader.Close();
                }

                ErrorLoading(Filepath, ex);
            }
#endif
                Lock.Set();
            }

            //Checks if object has not been succesfully been deserialized.
            if (Out == null)
            {
                //Create new instance of the object
                Out = Activator.CreateInstance(T);

                //Check if the object has the given object
                if (Out.GetType().GetInterface(nameof(INewConfig)) != null)
                {
                    var Temp = (INewConfig)Out;
                    Temp.SetNewInformation();
                }

#if !DEBUG
                Task.Run(() => SaveConfig(Out, Filename));
#else
                SaveConfig(Out, Filename);
#endif
            }

            return(Out);
        }