Insanely huge initial commit

This commit is contained in:
2026-02-21 17:04:05 -08:00
parent 9cdd36191a
commit 613d75914a
22525 changed files with 4035207 additions and 0 deletions

View File

@@ -0,0 +1,171 @@
using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace MoreMountains.Tools
{
/// <summary>
/// Allows the save and load of objects in a specific folder and file.
///
/// How to use (at a minimum) :
///
/// Save : MMSaveLoadManager.Save(TestObject, FileName+SaveFileExtension, FolderName);
///
/// Load : TestObject = (YourObjectClass)MMSaveLoadManager.Load(typeof(YourObjectClass), FileName + SaveFileExtension, FolderName);
///
/// Delete save : MMSaveLoadManager.DeleteSave(FileName+SaveFileExtension, FolderName);
///
/// Delete save folder : MMSaveLoadManager.DeleteSaveFolder(FolderName);
///
/// You can also specify what IMMSaveLoadManagerMethod the system should use. By default it's binary but you can also pick binary encrypted, json, or json encrypted
/// You'll find examples of how to set each of these in the MMSaveLoadTester class
///
/// </summary>
public static class MMSaveLoadManager
{
/// the method to use when saving and loading files (has to be the same at both times of course)
public static IMMSaveLoadManagerMethod saveLoadMethod = new MMSaveLoadManagerMethodBinary();
/// the default top level folder the system will use to save the file
private const string _baseFolderName = "/MMData/";
/// the name of the save folder if none is provided
private const string _defaultFolderName = "MMSaveLoadManager";
/// <summary>
/// Determines the save path to use when loading and saving a file based on a folder name.
/// </summary>
/// <returns>The save path.</returns>
/// <param name="folderName">Folder name.</param>
static string DetermineSavePath(string folderName = _defaultFolderName)
{
string savePath;
// depending on the device we're on, we assemble the path
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
savePath = Application.persistentDataPath + _baseFolderName;
}
else
{
savePath = Application.persistentDataPath + _baseFolderName;
}
#if UNITY_EDITOR
savePath = Application.dataPath + _baseFolderName;
#endif
savePath = savePath + folderName + "/";
return savePath;
}
/// <summary>
/// Determines the name of the file to save
/// </summary>
/// <returns>The save file name.</returns>
/// <param name="fileName">File name.</param>
static string DetermineSaveFileName(string fileName)
{
return fileName;
}
/// <summary>
/// Save the specified saveObject, fileName and foldername into a file on disk.
/// </summary>
/// <param name="saveObject">Save object.</param>
/// <param name="fileName">File name.</param>
/// <param name="foldername">Foldername.</param>
public static void Save(object saveObject, string fileName, string foldername = _defaultFolderName)
{
string savePath = DetermineSavePath(foldername);
string saveFileName = DetermineSaveFileName(fileName);
// if the directory doesn't already exist, we create it
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
// we serialize and write our object into a file on disk
FileStream saveFile = File.Create(savePath + saveFileName);
saveLoadMethod.Save(saveObject, saveFile);
saveFile.Close();
}
/// <summary>
/// Load the specified file based on a file name into a specified folder
/// </summary>
/// <param name="fileName">File name.</param>
/// <param name="foldername">Foldername.</param>
public static object Load(System.Type objectType, string fileName, string foldername = _defaultFolderName)
{
string savePath = DetermineSavePath(foldername);
string saveFileName = savePath + DetermineSaveFileName(fileName);
object returnObject;
// if the MMSaves directory or the save file doesn't exist, there's nothing to load, we do nothing and exit
if (!Directory.Exists(savePath) || !File.Exists(saveFileName))
{
return null;
}
FileStream saveFile = File.Open(saveFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
returnObject = saveLoadMethod.Load(objectType, saveFile);
saveFile.Close();
return returnObject;
}
/// <summary>
/// Removes a save from disk
/// </summary>
/// <param name="fileName">File name.</param>
/// <param name="folderName">Folder name.</param>
public static void DeleteSave(string fileName, string folderName = _defaultFolderName)
{
string savePath = DetermineSavePath(folderName);
string saveFileName = DetermineSaveFileName(fileName);
if (File.Exists(savePath + saveFileName))
{
File.Delete(savePath + saveFileName);
}
}
/// <summary>
/// Deletes the whole save folder
/// </summary>
/// <param name="folderName"></param>
public static void DeleteSaveFolder(string folderName = _defaultFolderName)
{
string savePath = DetermineSavePath(folderName);
if (Directory.Exists(savePath))
{
DeleteDirectory(savePath);
}
}
/// <summary>
/// Deletes the specified directory
/// </summary>
/// <param name="target_dir"></param>
public static void DeleteDirectory(string target_dir)
{
string[] files = Directory.GetFiles(target_dir);
string[] dirs = Directory.GetDirectories(target_dir);
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
foreach (string dir in dirs)
{
DeleteDirectory(dir);
}
Directory.Delete(target_dir, false);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: aa3d0794b4f8e4b40b6a3dde5b794c0b
timeCreated: 1478535356
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,43 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Runtime.Serialization.Formatters.Binary;
namespace MoreMountains.Tools
{
/// <summary>
/// This save load method saves and loads files as binary files
/// </summary>
public class MMSaveLoadManagerMethodBinary : IMMSaveLoadManagerMethod
{
/// <summary>
/// Saves the specified object to disk at the specified location after serializing it
/// </summary>
/// <param name="objectToSave"></param>
/// <param name="saveFile"></param>
public void Save(object objectToSave, FileStream saveFile)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(saveFile, objectToSave);
saveFile.Close();
}
/// <summary>
/// Loads the specified file from disk and deserializes it
/// </summary>
/// <param name="objectType"></param>
/// <param name="saveFile"></param>
/// <returns></returns>
public object Load(System.Type objectType, FileStream saveFile)
{
object savedObject;
BinaryFormatter formatter = new BinaryFormatter();
savedObject = formatter.Deserialize(saveFile);
saveFile.Close();
return savedObject;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 339f02b3d924b984db779a1af1a59be2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,60 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Runtime.Serialization.Formatters.Binary;
namespace MoreMountains.Tools
{
/// <summary>
/// This save load method saves and loads files as encrypted binary files
/// </summary>
public class MMSaveLoadManagerMethodBinaryEncrypted : MMSaveLoadManagerEncrypter, IMMSaveLoadManagerMethod
{
/// <summary>
/// Saves the specified object to disk at the specified location after encrypting it
/// </summary>
/// <param name="objectToSave"></param>
/// <param name="saveFile"></param>
public void Save(object objectToSave, FileStream saveFile)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream memoryStream = new MemoryStream();
formatter.Serialize(memoryStream, objectToSave);
memoryStream.Position = 0;
Encrypt(memoryStream, saveFile, Key);
saveFile.Flush();
memoryStream.Close();
saveFile.Close();
}
/// <summary>
/// Loads the specified file from disk, decrypts it, and deserializes it
/// </summary>
/// <param name="objectType"></param>
/// <param name="saveFile"></param>
/// <returns></returns>
public object Load(System.Type objectType, FileStream saveFile)
{
object savedObject;
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream memoryStream = new MemoryStream();
try
{
Decrypt(saveFile, memoryStream, Key);
}
catch (CryptographicException ce)
{
Debug.LogError("[MMSaveLoadManager] Encryption key error: " + ce.Message);
return null;
}
memoryStream.Position = 0;
savedObject = formatter.Deserialize(memoryStream);
memoryStream.Close();
saveFile.Close();
return savedObject;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 12d7216a3e967954ca8945932aa92fd4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,47 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace MoreMountains.Tools
{
public class MMSaveLoadManagerMethodJson : IMMSaveLoadManagerMethod
{
/// <summary>
/// Saves the specified object at the specified location after converting it to json
/// </summary>
/// <param name="objectToSave"></param>
/// <param name="saveFile"></param>
public void Save(object objectToSave, FileStream saveFile)
{
string json = JsonUtility.ToJson(objectToSave);
// if you prefer using NewtonSoft's JSON lib uncomment the line below and commment the line above
//string json = Newtonsoft.Json.JsonConvert.SerializeObject(objectToSave);
StreamWriter streamWriter = new StreamWriter(saveFile);
streamWriter.Write(json);
streamWriter.Close();
saveFile.Close();
}
/// <summary>
/// Loads the specified file and decodes it
/// </summary>
/// <param name="objectType"></param>
/// <param name="saveFile"></param>
/// <returns></returns>
public object Load(System.Type objectType, FileStream saveFile)
{
object savedObject; // = System.Activator.CreateInstance(objectType);
StreamReader streamReader = new StreamReader(saveFile, Encoding.UTF8);
string json = streamReader.ReadToEnd();
savedObject = JsonUtility.FromJson(json, objectType);
// if you prefer using NewtonSoft's JSON lib uncomment the line below and commment the line above
//savedObject = Newtonsoft.Json.JsonConvert.DeserializeObject(json,objectType);
streamReader.Close();
saveFile.Close();
return savedObject;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 148ba46d50e5e7346a7ac051cf1b6617
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,64 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace MoreMountains.Tools
{
public class MMSaveLoadManagerMethodJsonEncrypted : MMSaveLoadManagerEncrypter, IMMSaveLoadManagerMethod
{
/// <summary>
/// Saves the specified object at the specified location to disk, converts it to json and encrypts it
/// </summary>
/// <param name="objectToSave"></param>
/// <param name="saveFile"></param>
public void Save(object objectToSave, FileStream saveFile)
{
string json = JsonUtility.ToJson(objectToSave);
// if you prefer using NewtonSoft's JSON lib uncomment the line below and commment the line above
//string json = Newtonsoft.Json.JsonConvert.SerializeObject(objectToSave);
using (MemoryStream memoryStream = new MemoryStream())
using (StreamWriter streamWriter = new StreamWriter(memoryStream))
{
streamWriter.Write(json);
streamWriter.Flush();
memoryStream.Position = 0;
Encrypt(memoryStream, saveFile, Key);
}
saveFile.Close();
}
/// <summary>
/// Loads the specified file, decrypts it and decodes it
/// </summary>
/// <param name="objectType"></param>
/// <param name="saveFile"></param>
/// <returns></returns>
public object Load(System.Type objectType, FileStream saveFile)
{
object savedObject = null;
using (MemoryStream memoryStream = new MemoryStream())
using (StreamReader streamReader = new StreamReader(memoryStream))
{
try
{
Decrypt(saveFile, memoryStream, Key);
}
catch (CryptographicException ce)
{
Debug.LogError("[MMSaveLoadManager] Encryption key error: " + ce.Message);
return null;
}
memoryStream.Position = 0;
savedObject = JsonUtility.FromJson(streamReader.ReadToEnd(), objectType);
// if you prefer using NewtonSoft's JSON lib uncomment the line below and commment the line above
//savedObject = Newtonsoft.Json.JsonConvert.DeserializeObject(sr.ReadToEnd(), objectType);
}
saveFile.Close();
return savedObject;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8b50f078655cbd34fb69ee48182dbf37
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,72 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace MoreMountains.Tools
{
/// <summary>
/// An interface to implement save and load using different methods (binary, json, etc)
/// </summary>
public interface IMMSaveLoadManagerMethod
{
void Save(object objectToSave, FileStream saveFile);
object Load(System.Type objectType, FileStream saveFile);
}
/// <summary>
/// The possible methods to save and load files to and from disk available in the MMSaveLoadManager
/// </summary>
public enum MMSaveLoadManagerMethods { Json, JsonEncrypted, Binary, BinaryEncrypted };
/// <summary>
/// This class implements methods to encrypt and decrypt streams
/// </summary>
public abstract class MMSaveLoadManagerEncrypter
{
/// <summary>
/// The Key to use to save and load the file
/// </summary>
public string Key { get; set; } = "yourDefaultKey";
protected string _saltText = "SaltTextGoesHere";
/// <summary>
/// Encrypts the specified input stream into the specified output stream using the key passed in parameters
/// </summary>
/// <param name="inputStream"></param>
/// <param name="outputStream"></param>
/// <param name="sKey"></param>
protected virtual void Encrypt(Stream inputStream, Stream outputStream, string sKey)
{
RijndaelManaged algorithm = new RijndaelManaged();
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sKey, Encoding.ASCII.GetBytes(_saltText));
algorithm.Key = key.GetBytes(algorithm.KeySize / 8);
algorithm.IV = key.GetBytes(algorithm.BlockSize / 8);
CryptoStream cryptostream = new CryptoStream(inputStream, algorithm.CreateEncryptor(), CryptoStreamMode.Read);
cryptostream.CopyTo(outputStream);
}
/// <summary>
/// Decrypts the input stream into the output stream using the key passed in parameters
/// </summary>
/// <param name="inputStream"></param>
/// <param name="outputStream"></param>
/// <param name="sKey"></param>
protected virtual void Decrypt(Stream inputStream, Stream outputStream, string sKey)
{
RijndaelManaged algorithm = new RijndaelManaged();
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sKey, Encoding.ASCII.GetBytes(_saltText));
algorithm.Key = key.GetBytes(algorithm.KeySize / 8);
algorithm.IV = key.GetBytes(algorithm.BlockSize / 8);
CryptoStream cryptostream = new CryptoStream(inputStream, algorithm.CreateDecryptor(), CryptoStreamMode.Read);
cryptostream.CopyTo(outputStream);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b681190ef64d3e2438805b489a9ac9a4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: a74b1b5d357902346825fdfd125ea399
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,104 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace MoreMountains.Tools
{
/// <summary>
/// A test object to store data to test the MMSaveLoadManager class
/// </summary>
[System.Serializable]
public class MMSaveLoadTestObject
{
public string SavedText;
}
/// <summary>
/// A simple class used in the MMSaveLoadTestScene to test the MMSaveLoadManager class
/// </summary>
public class MMSaveLoadTester : MonoBehaviour
{
[Header("Bindings")]
/// the text to save
public InputField TargetInputField;
[Header("Save settings")]
/// the chosen save method (json, encrypted json, binary, encrypted binary)
public MMSaveLoadManagerMethods SaveLoadMethod = MMSaveLoadManagerMethods.Binary;
/// the name of the file to save
public string FileName = "TestObject";
/// the name of the destination folder
public string FolderName = "MMTest/";
/// the extension to use
public string SaveFileExtension = ".testObject";
/// the key to use to encrypt the file (if needed)
public string EncryptionKey = "ThisIsTheKey";
/// Test button
[MMInspectorButton("Save")]
public bool TestSaveButton;
/// Test button
[MMInspectorButton("Load")]
public bool TestLoadButton;
/// Test button
[MMInspectorButton("Reset")]
public bool TestResetButton;
protected IMMSaveLoadManagerMethod _saveLoadManagerMethod;
/// <summary>
/// Saves the contents of the TestObject into a file
/// </summary>
public virtual void Save()
{
InitializeSaveLoadMethod();
MMSaveLoadTestObject testObject = new MMSaveLoadTestObject();
testObject.SavedText = TargetInputField.text;
MMSaveLoadManager.Save(testObject, FileName+SaveFileExtension, FolderName);
}
/// <summary>
/// Loads the saved data
/// </summary>
public virtual void Load()
{
InitializeSaveLoadMethod();
MMSaveLoadTestObject testObject = (MMSaveLoadTestObject)MMSaveLoadManager.Load(typeof(MMSaveLoadTestObject), FileName + SaveFileExtension, FolderName);
TargetInputField.text = testObject.SavedText;
}
/// <summary>
/// Resets all saves by deleting the whole folder
/// </summary>
protected virtual void Reset()
{
MMSaveLoadManager.DeleteSaveFolder(FolderName);
}
/// <summary>
/// Creates a new MMSaveLoadManagerMethod and passes it to the MMSaveLoadManager
/// </summary>
protected virtual void InitializeSaveLoadMethod()
{
switch(SaveLoadMethod)
{
case MMSaveLoadManagerMethods.Binary:
_saveLoadManagerMethod = new MMSaveLoadManagerMethodBinary();
break;
case MMSaveLoadManagerMethods.BinaryEncrypted:
_saveLoadManagerMethod = new MMSaveLoadManagerMethodBinaryEncrypted();
(_saveLoadManagerMethod as MMSaveLoadManagerEncrypter).Key = EncryptionKey;
break;
case MMSaveLoadManagerMethods.Json:
_saveLoadManagerMethod = new MMSaveLoadManagerMethodJson();
break;
case MMSaveLoadManagerMethods.JsonEncrypted:
_saveLoadManagerMethod = new MMSaveLoadManagerMethodJsonEncrypted();
(_saveLoadManagerMethod as MMSaveLoadManagerEncrypter).Key = EncryptionKey;
break;
}
MMSaveLoadManager.saveLoadMethod = _saveLoadManagerMethod;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 58f9c048a89d0d5448bf5972cdc13fad
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: