Esempio n. 1
0
        private static void loadConfig(bool loadedPrev = false)
        {
            // If file/directory doesn't exist
            if (!checkFiles())
            {
                // Stop loading configuration
                return;
            }

            // If config has been loaded already
            if (loadedPrev)
            {
                // Resetup the scoring manager to clear current configs,
                // We need to clear current configs incase the newly loaded config file has had a section removed
                // If this is the case, unless we clear current configs, the section that was removed will persist
                // through the new loading
                ScoringManager.Setup();
            }

            /* Open file with only read permissions.
             * We don't need write permissions as we want
             * as little file permission errors to occur
             * as possible. We do not need write permissions
             * until it is time to save */
            using (ConfigFileStream = File.Open(CurrentConfigPath, FileMode.Open, FileAccess.Read))
            {
                LastUpdated = File.GetLastWriteTime(CurrentConfigPath);

                try
                {
                    // Create aes managed class for creating decryptor
                    AesManaged aesManaged = new AesManaged();
                    aesManaged.KeySize = 256;
                    aesManaged.Mode    = CipherMode.CBC;
                    aesManaged.Padding = PaddingMode.PKCS7;
                    aesManaged.Key     = Encoding.ASCII.GetBytes(Key);
                    aesManaged.IV      = Encoding.ASCII.GetBytes(IV);
                    ICryptoTransform decryptor = aesManaged.CreateDecryptor();

                    // Crypto stream to decrypt config file
                    using (CryptoStream cryptoStream = new CryptoStream(ConfigFileStream, decryptor, CryptoStreamMode.Read))
                        // Binary reader for parsing of data
                        using (BinaryReader reader = new BinaryReader(cryptoStream))
                        {
                            loadOutputFiles(reader);

                            loadTranslations(reader);

                            // Get number of sections
                            int count = reader.ReadInt32();

                            // For each config section, load it
                            for (int i = 0; i < count; i++)
                            {
                                // Get config section type
                                ESectionType type = (ESectionType)reader.ReadInt32();

                                // Get length of data for specific section
                                int bufferLength = reader.ReadInt32();

                                // Read buffer for section to isolate from others
                                byte[] buffer = reader.ReadBytes(bufferLength);

                                // Find section for loading
                                ISection config = ScoringManager.ScoringSections.FirstOrDefault(x => x.Type == type);

                                // Possibly removed section, skip it
                                if (config == null)
                                {
                                    continue;
                                }

                                // Create isolated binary reader for section loading
                                using (MemoryStream bufferStream = new MemoryStream(buffer))
                                    using (BinaryReader sectionReader = new BinaryReader(bufferStream))
                                    {
                                        try
                                        {
                                            // Load config
                                            config.Load(sectionReader);
                                        }
                                        catch
                                        {
                                            // Issue with specific section, likely something was added to section
                                            // But it's an outdated config file
                                        }
                                    }
                            }
                        }
                }
                catch
                {
                    // Likely outdated configuration file with updated program. No action is likely needed.
                    // TODO, add version checking to allow outdated configuration files
                }
            }
        }