// This method checks if the encryptation was enabled or disabled // So it can migrate files private int MigrationNeeded(BaseConfigImplementation implementation) { string cryptoPath = Path.Combine(mManager.Path, $"{implementation.Name}{GlobalSettings.DEFAULT_CRYPTO_EXTENSION}"); string normalPath = Path.Combine(mManager.Path, $"{implementation.Name}{GlobalSettings.DEFAULT_EXTENSION}"); // Check for migration if (File.Exists(cryptoPath) && mManager.Encryptation) // If there are encrypted files and encryptation is enabled, return 0 { return(0); } else if (File.Exists(cryptoPath) && !mManager.Encryptation) // If there are encrypted files but encryptation is disabled, return 1 { return(1); } else if (File.Exists(normalPath) && !mManager.Encryptation) // If there are normal files and encryptation is disabled, return 0 { return(0); } else if (File.Exists(normalPath) && mManager.Encryptation) // If there are normal files and encryptation is enabled, return 2 { return(2); } else if (File.Exists(normalPath) && File.Exists(cryptoPath) && mManager.Encryptation) // If there are normal and encrypted files and encryptation is enabled, return -1 { return(-1); } else if (File.Exists(normalPath) && File.Exists(cryptoPath) && !mManager.Encryptation) // If there are normal and encrypted files and encryptation is disabled, return -2 { return(-2); } else // If there are another case, return 1 { return(3); } }
private void DeserializeContentFromFile(BaseConfigImplementation implementation) { object deserialized = null; // If the file exists and contains data... if (implementation.File != null & implementation.File.Exists && File.ReadAllText(implementation.File.FullName).Length > 0) { // Get serializer mode var saveMode = mManager.SaveMode; string fileContent; // Store file content // Decrypt the file if encryptation is enabled if (mManager.Encryptation) { fileContent = implementation.File.Decrypt(); } else { fileContent = File.ReadAllText(implementation.File.FullName); // Otherwise, read the whole file } switch (saveMode) { case SaveModes.Json: deserialized = JsonConvert.DeserializeObject(fileContent, implementation.Type); break; case SaveModes.Xml: deserialized = XmlSerializer.Deserialize(fileContent, implementation.Type); break; case SaveModes.Binary: deserialized = BinarySerializer.Deserialize(fileContent); break; } implementation.RuntimeInstance = deserialized; } else { implementation.RuntimeInstance = Activator.CreateInstance(implementation.Type); // Create the file if (!File.Exists(implementation.File.FullName)) { using (var fs = File.Create(implementation.File.FullName)) { } } } }
// Migrate files private void MigrateFiles(int migrationCheck, BaseConfigImplementation implementation) { string cryptoPath = Path.Combine(mManager.Path, $"{implementation.Name}{GlobalSettings.DEFAULT_CRYPTO_EXTENSION}"); string normalPath = Path.Combine(mManager.Path, $"{implementation.Name}{GlobalSettings.DEFAULT_EXTENSION}"); // Migrate from encrypted to normal if (migrationCheck == 1) { implementation.File = new ConfigFile(cryptoPath); implementation.File.Decrypt(); // Decrypt the file // Change the extension //File.Move(implementation.File.FullName, Path.ChangeExtension(implementation.File.FullName, GlobalSettings.DEFAULT_EXTENSION)); } // Migrate from normal to encrypted else if (migrationCheck == 2) { implementation.File = new ConfigFile(normalPath); implementation.File.Encrypt(); // Encrypt the file // Change the extension //File.Move(implementation.File.FullName, Path.ChangeExtension(implementation.File.FullName, GlobalSettings.DEFAULT_CRYPTO_EXTENSION)); } // Delete normal files else if (migrationCheck == -1) { implementation.File = new ConfigFile(cryptoPath); File.Delete(normalPath); } // Delete encrypted files else if (migrationCheck == -2) { implementation.File = new ConfigFile(normalPath); File.Delete(cryptoPath); } // Otherwise, just create the config file else { implementation.File = new ConfigFile(normalPath); } }