Make migration from old save to new save work
This commit is contained in:
@@ -25,6 +25,21 @@ public class BazookaManager : MonoBehaviour
|
||||
DontDestroyOnLoad(gameObject);
|
||||
if (!firstLoadDone)
|
||||
{
|
||||
try
|
||||
{
|
||||
var oldSave = Path.Join(Application.persistentDataPath, OldSavefileEncryption.BAZOOKA_MANAGER_FILE_KEY + ".dat");
|
||||
var newSave = Path.Join(Application.persistentDataPath, "savefile.json");
|
||||
if (
|
||||
File.Exists(oldSave) ||
|
||||
!File.Exists(newSave)
|
||||
)
|
||||
{
|
||||
File.WriteAllBytes(newSave, Encoding.UTF8.GetBytes(OldSavefileEncryption.DecryptRaw(File.ReadAllBytes(oldSave))));
|
||||
File.Delete(oldSave);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
firstLoadDone = true;
|
||||
var save = LoadObject("savefile");
|
||||
var cache = LoadArray("cache");
|
||||
|
||||
@@ -9,39 +9,7 @@ public class OldSavefileEncryption
|
||||
public const string BAZOOKA_MANAGER_KEY = "5de0d5798d4eb751e13b668c1fc5297d";
|
||||
public const string BAZOOKA_MANAGER_FILE_KEY = "e74af7ba21b8b4edd0e3f1d572d63c67";
|
||||
|
||||
public static byte[] EncryptRaw(string text, string key)
|
||||
{
|
||||
if (BAZOOKA_MANAGER_KEY.Trim() == string.Empty)
|
||||
{
|
||||
Debug.LogError("Failed to encrypt: Encryption/Decryption keys not present");
|
||||
return null;
|
||||
}
|
||||
try
|
||||
{
|
||||
using Aes aes = Aes.Create();
|
||||
aes.Key = Encoding.UTF8.GetBytes(key);
|
||||
aes.Mode = CipherMode.CBC;
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
aes.GenerateIV();
|
||||
|
||||
using MemoryStream ms = new();
|
||||
ms.Write(aes.IV, 0, aes.IV.Length);
|
||||
|
||||
using (var cryptoStream = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
|
||||
using (var writer = new StreamWriter(cryptoStream))
|
||||
{
|
||||
writer.Write(text);
|
||||
}
|
||||
|
||||
return ms.ToArray();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static string DecryptRaw(byte[] data, string key)
|
||||
public static string DecryptRaw(byte[] data)
|
||||
{
|
||||
if (BAZOOKA_MANAGER_KEY.Trim() == string.Empty)
|
||||
{
|
||||
@@ -51,7 +19,7 @@ public class OldSavefileEncryption
|
||||
try
|
||||
{
|
||||
using Aes aes = Aes.Create();
|
||||
aes.Key = Encoding.UTF8.GetBytes(key);
|
||||
aes.Key = Encoding.UTF8.GetBytes(BAZOOKA_MANAGER_KEY);
|
||||
aes.Mode = CipherMode.CBC;
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user