/*
 * +================================================================================+
 *      __  ___  ______________  __  __  ______________   ____
 *     / / / / |/ /  _/_  __/\ \/ / / / / /_  __/  _/ /  / __/
 *    / /_/ /    // /  / /    \  / / /_/ / / / _/ // /___\ \
 *    \____/_/|_/___/ /_/     /_/  \____/ /_/ /___/____/___/
 *
 * +================================================================================+
 *
 *    "THE BEER-WARE LICENSE"
 *    -----------------------
 *
 *    As long as you retain this notice you can do whatever you want with this
 *    stuff. However it is expressly forbidden to sell or commercially distribute
 *    Unity Utils or parts of it apart from your games.
 *    If you meet any of the authors some day, and you think this stuff is
 *    worth it, you can buy them a beer in return.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *    THE SOFTWARE.
 *
 * +================================================================================+
 *
 *    Authors:
 *    David Krummenacher (dave@timbookedtwo.com)
 *
 */

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

namespace Utils.Helpers {

	/// <summary>
	/// Enables you to read and write serializable data to a binary file.
	/// </summary>
	public abstract class BinaryFileHelper {
	
		/// <summary>
		/// Writes serializable data to a file.
		/// </summary>
		/// <param name="filePath">The path to the file.</param>
		/// <param name="data">Data.</param>
		public static void WriteToFile(string filePath, object data) {
			// Try to write file
			try {
				using (Stream stream = File.Create(filePath)) {
					BinaryFormatter serializer = new BinaryFormatter();
					serializer.Serialize(stream, data);
				}
			} catch (Exception e) {
				Debug.LogException(e);
			}
		}
	
		/// <summary>
		/// Reads serialized data from a file.
		/// Returns 'null' if the file is not found.
		/// </summary>
		/// <returns>The data inside the file or null.</returns>
		/// <param name="filePath">The path to the file.</param>
		public static object ReadFromFile(string filePath) {
			// Try to read file or return null
			if (File.Exists(filePath)) {
				object data = null;
				
				try {
					using (Stream stream = File.OpenRead(filePath)) {
						BinaryFormatter deserializer = new BinaryFormatter();
						data = deserializer.Deserialize(stream);
					}
				} catch (Exception e) {
					Debug.LogException(e);
				}
				
				return data;
			} else {
				// File not found warning
				Debug.LogWarning("File \"" + filePath + "\" not found! Returning null.");
				return null;
			}
		}
	}
}
