public void SaveToFile <T> (T dataToStore, string dataName) { string fileName = _savingLocation + "/" + dataName + "." + _method.ToString().ToLower(); try { switch (_method) { case StorageMethod.Binary: BinaryFormatter formatter = new BinaryFormatter(); using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { formatter.Serialize(stream, dataToStore); } break; case StorageMethod.XML: XmlSerializer serializer = new XmlSerializer(typeof(T)); using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { serializer.Serialize(stream, dataToStore); } break; case StorageMethod.JSON: string serializedData = JsonUtility.ToJson(dataToStore, true); File.WriteAllText(fileName, serializedData); break; } } catch (System.Exception ex) { Debug.LogWarning("File writing error: " + ex.Message); } }
public (DataType t, object data) ReadData(StorageMethod storageSlot) { DataType t = DataType.None; object data = null; using (MemoryStream stream = new MemoryStream()) { pngOriginal.WriteToStream(stream, true, true); stream.Seek(0, SeekOrigin.Begin); } bool hasEOF = false; int IDATs = 0; foreach (PNGChunk chunk in pngOriginal.Chunks) { if (chunk.Name == "_EOF") { hasEOF = true; } if (chunk.Name == "IDAT") { IDATs++; } } StegoProvider pr = Providers.XOREOF; if (storageSlot == StorageMethod.IDAT) { pr = Providers.XORIDAT; } if (!hasEOF && storageSlot == StorageMethod.EOF) { provider = null; Logger.Log($"There is no data in {storageSlot.ToString()}", Logger.LOG_LEVEL.ERR); } else if (IDATs <= 1) { provider = null; Logger.Log($"There is no data in {storageSlot.ToString()}", Logger.LOG_LEVEL.ERR); } else { try { provider = (SteganographyProvider)Activator.CreateInstance(pr.ProviderType, pngOriginal, true); provider.SetPassword(password); t = provider.Extract(out data); return(t, data); } catch (InvalidPasswordException) { Logger.Log("The password was incorrect.", Logger.LOG_LEVEL.ERR); } } return(DataType.None, null); }
/// <summary> /// Generic method used for reading save data /// </summary> /// <returns>Read data</returns> /// <param name="method">Storing method</param> /// <typeparam name="T">Typeof data to store</typeparam> public static T LoadFromFIle <T>(StorageMethod method, string name) { T storedData = default(T); string fileName = SavingLocation + name + "." + method.ToString().ToLower(); Debug.Log("File: " + fileName); try { switch (method) { case StorageMethod.Binary: using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { BinaryFormatter formatter = new BinaryFormatter(); storedData = (T)formatter.Deserialize(stream); stream.Close(); stream.Dispose(); } break; case StorageMethod.XML: using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { XmlSerializer serializer = new XmlSerializer(typeof(T)); storedData = (T)serializer.Deserialize(stream); stream.Close(); stream.Dispose(); } break; case StorageMethod.JSON: string serializedData = File.ReadAllText(fileName); storedData = JsonUtility.FromJson <T>(serializedData); break; } } catch (System.Exception ex) { Debug.LogWarning("File reading error: " + ex.Message); } return(storedData); }