1340 lines
30 KiB
C#
1340 lines
30 KiB
C#
![]() |
// ***********************************************************************
|
|||
|
// Company :
|
|||
|
// Author : Kimch
|
|||
|
// Created :
|
|||
|
//
|
|||
|
// Last Modified By : Kimch
|
|||
|
// Last Modified On :
|
|||
|
// ***********************************************************************
|
|||
|
#define USE_OBFUSCATE
|
|||
|
|
|||
|
namespace G
|
|||
|
{
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using System.IO.Compression;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
using CodeStage.AntiCheat.ObscuredTypes;
|
|||
|
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
using IntList = System.Collections.Generic.List<CodeStage.AntiCheat.ObscuredTypes.ObscuredInt>;
|
|||
|
using StringList = System.Collections.Generic.List<string>;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 文件存储方式
|
|||
|
/// </summary>
|
|||
|
public class Archive : System.IDisposable
|
|||
|
{
|
|||
|
#region Field
|
|||
|
|
|||
|
public const ushort MAGIC = 12356;
|
|||
|
public const ushort VERSION = 9;
|
|||
|
|
|||
|
private string _localId;
|
|||
|
private string _serverId;
|
|||
|
|
|||
|
private readonly string _secretKey;
|
|||
|
private readonly string _fileName;
|
|||
|
|
|||
|
private int _modifiedCount;
|
|||
|
|
|||
|
private readonly Dictionary<string, ObscuredInt> _sInts = new Dictionary<string, ObscuredInt>();
|
|||
|
private readonly Dictionary<string, IntList> _sIntLists = new Dictionary<string, IntList>();
|
|||
|
|
|||
|
private readonly Dictionary<string, string> _sStrings = new Dictionary<string, string>();
|
|||
|
private readonly Dictionary<string, StringList> _sStringLists = new Dictionary<string, StringList>();
|
|||
|
|
|||
|
private readonly Dictionary<int, ObscuredInt> _iInts = new Dictionary<int, ObscuredInt>();
|
|||
|
private readonly Dictionary<int, IntList> _iIntLists = new Dictionary<int, IntList>();
|
|||
|
|
|||
|
private readonly Dictionary<int, string> _iStrings = new Dictionary<int, string>();
|
|||
|
private readonly Dictionary<int, StringList> _iStringLists = new Dictionary<int, StringList>();
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region PROPERTY
|
|||
|
|
|||
|
public string localId
|
|||
|
{
|
|||
|
get { return _localId; }
|
|||
|
}
|
|||
|
|
|||
|
public string serverId
|
|||
|
{
|
|||
|
get { return _serverId; }
|
|||
|
}
|
|||
|
|
|||
|
private bool modified
|
|||
|
{
|
|||
|
get { return _modifiedCount > 0; }
|
|||
|
set { _modifiedCount = value ? 1 : 0; }
|
|||
|
}
|
|||
|
|
|||
|
public ushort loadVersion
|
|||
|
{
|
|||
|
get;
|
|||
|
private set;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public bool isCheat
|
|||
|
{
|
|||
|
get;
|
|||
|
private set;
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Constructor
|
|||
|
|
|||
|
private Archive(string local, string server)
|
|||
|
{
|
|||
|
_localId = string.IsNullOrEmpty(local) ? SystemInfo.deviceUniqueIdentifier : local;
|
|||
|
_serverId = string.IsNullOrEmpty(server) ? "guest" : server;
|
|||
|
|
|||
|
_secretKey = "afkdieolspfsfopeocpmnlghskfjlghw";
|
|||
|
_fileName = KUtils.GenerateHash(_localId) + ".gaz";
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Get Set Key
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="defaultValue"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public int GetInt(string key, int defaultValue = 0)
|
|||
|
{
|
|||
|
if (_sInts.TryGetValue(key, out var result))
|
|||
|
{
|
|||
|
return result;
|
|||
|
}
|
|||
|
return defaultValue;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 加密
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="defaultValue"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public int GetInt(int key, int defaultValue = 0)
|
|||
|
{
|
|||
|
if (_iInts.TryGetValue(key, out var result))
|
|||
|
{
|
|||
|
return result;
|
|||
|
}
|
|||
|
return defaultValue;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public IntList GetIntList(string key)
|
|||
|
{
|
|||
|
if (!_sIntLists.TryGetValue(key, out var result))
|
|||
|
{
|
|||
|
result = new IntList();
|
|||
|
_sIntLists.Add(key, result);
|
|||
|
};
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public IntList GetIntList(int key)
|
|||
|
{
|
|||
|
if (!_iIntLists.TryGetValue(key, out var result))
|
|||
|
{
|
|||
|
result = new IntList();
|
|||
|
_iIntLists.Add(key, result);
|
|||
|
};
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="value"></param>
|
|||
|
public void SetInt(string key, int value)
|
|||
|
{
|
|||
|
if (_sInts.TryGetValue(key, out var result))
|
|||
|
{
|
|||
|
if (value != result)
|
|||
|
{
|
|||
|
_sInts[key] = value;
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_sInts.Add(key, value);
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="value"></param>
|
|||
|
public void SetInt(int key, int value)
|
|||
|
{
|
|||
|
if (_iInts.TryGetValue(key, out var result))
|
|||
|
{
|
|||
|
if (value != result)
|
|||
|
{
|
|||
|
_iInts[key] = value;
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_iInts.Add(key, value);
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="value"></param>
|
|||
|
public void SetIntList(string key, IntList value)
|
|||
|
{
|
|||
|
if (_sIntLists.TryGetValue(key, out var result))
|
|||
|
{
|
|||
|
if (result != value)
|
|||
|
_sIntLists[key] = value;
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_sIntLists.Add(key, value);
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="value"></param>
|
|||
|
public void SetIntList(int key, IntList value)
|
|||
|
{
|
|||
|
if (_iIntLists.TryGetValue(key, out var result))
|
|||
|
{
|
|||
|
if (result != value)
|
|||
|
_iIntLists[key] = value;
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_iIntLists.Add(key, value);
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="defaultValue"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public string GetString(string key, string defaultValue = null)
|
|||
|
{
|
|||
|
if (_sStrings.TryGetValue(key, out string result))
|
|||
|
{
|
|||
|
return result;
|
|||
|
}
|
|||
|
return defaultValue;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="defaultValue"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public string GetString(int key, string defaultValue = null)
|
|||
|
{
|
|||
|
if (_iStrings.TryGetValue(key, out string result))
|
|||
|
{
|
|||
|
return result;
|
|||
|
}
|
|||
|
return defaultValue;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public StringList GetStringList(string key)
|
|||
|
{
|
|||
|
if (!_sStringLists.TryGetValue(key, out var result))
|
|||
|
{
|
|||
|
result = new StringList();
|
|||
|
_sStringLists.Add(key, result);
|
|||
|
};
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public StringList GetStringList(int key)
|
|||
|
{
|
|||
|
if (!_iStringLists.TryGetValue(key, out var result))
|
|||
|
{
|
|||
|
result = new StringList();
|
|||
|
_iStringLists.Add(key, result);
|
|||
|
};
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="value"></param>
|
|||
|
public void SetString(string key, string value)
|
|||
|
{
|
|||
|
if (_sStrings.TryGetValue(key, out string result))
|
|||
|
{
|
|||
|
if (value != result)
|
|||
|
{
|
|||
|
_sStrings[key] = value;
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_sStrings.Add(key, value);
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="value"></param>
|
|||
|
public void SetString(int key, string value)
|
|||
|
{
|
|||
|
if (_iStrings.TryGetValue(key, out string result))
|
|||
|
{
|
|||
|
if (value != result)
|
|||
|
{
|
|||
|
_iStrings[key] = value;
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_iStrings.Add(key, value);
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="value"></param>
|
|||
|
public void SetStringList(string key, StringList value)
|
|||
|
{
|
|||
|
if (_sStringLists.TryGetValue(key, out var result))
|
|||
|
{
|
|||
|
if (result != value)
|
|||
|
_sStringLists[key] = value;
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_sStringLists.Add(key, value);
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="value"></param>
|
|||
|
public void SetStringList(int key, StringList value)
|
|||
|
{
|
|||
|
if (_iStringLists.TryGetValue(key, out var result))
|
|||
|
{
|
|||
|
if (result != value)
|
|||
|
_iStringLists[key] = value;
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_iStringLists.Add(key, value);
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
public void RemoveInt(string key)
|
|||
|
{
|
|||
|
if (_sInts.Remove(key))
|
|||
|
{
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
public void RemoveInt(int key)
|
|||
|
{
|
|||
|
if (_iInts.Remove(key))
|
|||
|
{
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
public void RemoveIntList(string key)
|
|||
|
{
|
|||
|
if (_sIntLists.Remove(key))
|
|||
|
{
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
public void RemoveIntList(int key)
|
|||
|
{
|
|||
|
if (_iIntLists.Remove(key))
|
|||
|
{
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
public void RemoveString(string key)
|
|||
|
{
|
|||
|
if (_sStrings.Remove(key))
|
|||
|
{
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
public void RemoveString(int key)
|
|||
|
{
|
|||
|
if (_iStrings.Remove(key))
|
|||
|
{
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
public void RemoveStringList(string key)
|
|||
|
{
|
|||
|
if (_sStringLists.Remove(key))
|
|||
|
{
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
public void RemoveStringList(int key)
|
|||
|
{
|
|||
|
if (_iStringLists.Remove(key))
|
|||
|
{
|
|||
|
_modifiedCount++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public void RemoveAll()
|
|||
|
{
|
|||
|
_sInts.Clear();
|
|||
|
_sIntLists.Clear();
|
|||
|
|
|||
|
_iInts.Clear();
|
|||
|
_iIntLists.Clear();
|
|||
|
|
|||
|
_sStrings.Clear();
|
|||
|
_sStringLists.Clear();
|
|||
|
|
|||
|
_iStrings.Clear();
|
|||
|
_iStringLists.Clear();
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region READ AND WRITE
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="version"></param>
|
|||
|
public bool CheckAnti(ushort version)
|
|||
|
{
|
|||
|
#if !UNITY_EDITOR
|
|||
|
this.loadVersion = version;
|
|||
|
var result =
|
|||
|
GetInt(Item.Id.kCoin) > 999_000_000 ||
|
|||
|
GetInt(Item.Id.kChip) > 996_000_000 ||
|
|||
|
GetInt(Item.Id.kStone) > 9_000_000 ||
|
|||
|
GetInt(14) > 9998 ||
|
|||
|
GetInt(15) > 9798 ||
|
|||
|
GetInt(19) > 96998 ||
|
|||
|
GetInt(25) > 9598 ||
|
|||
|
GetInt(25) > 9498 ||
|
|||
|
GetInt(29) > 9298 ||
|
|||
|
GetInt(52) > 99798;
|
|||
|
if (result)
|
|||
|
{
|
|||
|
isCheat = true;
|
|||
|
RemoveAll();
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
#endif
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>Reads the profiles.</summary>
|
|||
|
private bool ReadFromStream(MemoryStream stream, bool checkId)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (!ReadCRC(stream))
|
|||
|
return false;
|
|||
|
|
|||
|
var version = VERSION;
|
|||
|
using (var binaryReader = new BinaryReader(stream, System.Text.Encoding.UTF8, true))
|
|||
|
{
|
|||
|
var crc = binaryReader.ReadUInt16();
|
|||
|
var magic = binaryReader.ReadUInt16();
|
|||
|
if (MAGIC != magic)
|
|||
|
return false;
|
|||
|
|
|||
|
version = binaryReader.ReadUInt16();
|
|||
|
if (VERSION < version || version < 3)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
var tmpLocalId = binaryReader.ReadString();
|
|||
|
if (checkId && _localId != tmpLocalId)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
if (version >= 5)
|
|||
|
{
|
|||
|
var tmpServerId = binaryReader.ReadString();
|
|||
|
if (checkId && tmpServerId != "guest" && _serverId != tmpServerId)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//sInt
|
|||
|
_sInts.Clear();
|
|||
|
int length = binaryReader.ReadInt32();
|
|||
|
for (int i = 0; i < length; i++)
|
|||
|
{
|
|||
|
var key = binaryReader.ReadString();
|
|||
|
var value = binaryReader.ReadInt32();
|
|||
|
_sInts.Add(key, value);
|
|||
|
}
|
|||
|
|
|||
|
//iInt
|
|||
|
_iInts.Clear();
|
|||
|
length = binaryReader.ReadInt32();
|
|||
|
for (int i = 0; i < length; i++)
|
|||
|
{
|
|||
|
var key = binaryReader.ReadInt32();
|
|||
|
var value = binaryReader.ReadInt32();
|
|||
|
_iInts.Add(key, value);
|
|||
|
}
|
|||
|
|
|||
|
//sString
|
|||
|
_sStrings.Clear();
|
|||
|
length = binaryReader.ReadInt32();
|
|||
|
for (int i = 0; i < length; i++)
|
|||
|
{
|
|||
|
var key = binaryReader.ReadString();
|
|||
|
var value = binaryReader.ReadString();
|
|||
|
_sStrings.Add(key, value);
|
|||
|
}
|
|||
|
|
|||
|
//iString
|
|||
|
_iStrings.Clear();
|
|||
|
length = binaryReader.ReadInt32();
|
|||
|
for (int i = 0; i < length; i++)
|
|||
|
{
|
|||
|
var key = binaryReader.ReadInt32();
|
|||
|
var value = binaryReader.ReadString();
|
|||
|
_iStrings.Add(key, value);
|
|||
|
}
|
|||
|
|
|||
|
// sIntList
|
|||
|
_sIntLists.Clear();
|
|||
|
length = binaryReader.ReadInt32();
|
|||
|
for (int i = 0; i < length; i++)
|
|||
|
{
|
|||
|
var key = binaryReader.ReadString();
|
|||
|
int length2 = binaryReader.ReadInt32();
|
|||
|
var tmpList = new IntList(length2);
|
|||
|
for (int j = 0; j < length2; j++)
|
|||
|
{
|
|||
|
var value = binaryReader.ReadInt32();
|
|||
|
tmpList.Add(value);
|
|||
|
}
|
|||
|
_sIntLists.Add(key, tmpList);
|
|||
|
}
|
|||
|
|
|||
|
// iIntList
|
|||
|
_iIntLists.Clear();
|
|||
|
length = binaryReader.ReadInt32();
|
|||
|
for (int i = 0; i < length; i++)
|
|||
|
{
|
|||
|
var key = binaryReader.ReadInt32();
|
|||
|
int length2 = binaryReader.ReadInt32();
|
|||
|
var tmpList = new IntList(length2);
|
|||
|
for (int j = 0; j < length2; j++)
|
|||
|
{
|
|||
|
var value = binaryReader.ReadInt32();
|
|||
|
tmpList.Add(value);
|
|||
|
}
|
|||
|
_iIntLists.Add(key, tmpList);
|
|||
|
}
|
|||
|
|
|||
|
// sStringList
|
|||
|
_sStringLists.Clear();
|
|||
|
length = binaryReader.ReadInt32();
|
|||
|
for (int i = 0; i < length; i++)
|
|||
|
{
|
|||
|
var key = binaryReader.ReadString();
|
|||
|
int length2 = binaryReader.ReadInt32();
|
|||
|
var tmpList = new StringList(length2);
|
|||
|
for (int j = 0; j < length2; j++)
|
|||
|
{
|
|||
|
var value = binaryReader.ReadString();
|
|||
|
tmpList.Add(value);
|
|||
|
}
|
|||
|
_sStringLists.Add(key, tmpList);
|
|||
|
}
|
|||
|
|
|||
|
// iStringList
|
|||
|
_iStringLists.Clear();
|
|||
|
length = binaryReader.ReadInt32();
|
|||
|
for (int i = 0; i < length; i++)
|
|||
|
{
|
|||
|
var key = binaryReader.ReadInt32();
|
|||
|
int length2 = binaryReader.ReadInt32();
|
|||
|
var tmpList = new StringList(length2);
|
|||
|
for (int j = 0; j < length2; j++)
|
|||
|
{
|
|||
|
var value = binaryReader.ReadString();
|
|||
|
tmpList.Add(value);
|
|||
|
}
|
|||
|
_iStringLists.Add(key, tmpList);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//
|
|||
|
//CheckAnti(version);
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (System.Exception ex)
|
|||
|
{
|
|||
|
Debug.LogException(ex);
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private bool ReadCRC(MemoryStream stream)
|
|||
|
{
|
|||
|
stream.TryGetBuffer(out var segment);
|
|||
|
this.Decrypt(segment.Array, segment.Offset, segment.Count);
|
|||
|
return GetCRC16(segment.Array, segment.Offset, segment.Count);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>Writes to stream.</summary>
|
|||
|
private bool WriteToStream(MemoryStream stream)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
using (var binaryWriter = new BinaryWriter(stream, System.Text.Encoding.UTF8, true))
|
|||
|
{
|
|||
|
binaryWriter.Write(MAGIC);//crc
|
|||
|
binaryWriter.Write(MAGIC);
|
|||
|
binaryWriter.Write(VERSION);
|
|||
|
//
|
|||
|
binaryWriter.Write(_localId);
|
|||
|
binaryWriter.Write(_serverId);
|
|||
|
|
|||
|
// int
|
|||
|
binaryWriter.Write(_sInts.Count);
|
|||
|
foreach (var current in _sInts)
|
|||
|
{
|
|||
|
binaryWriter.Write(current.Key);
|
|||
|
binaryWriter.Write(current.Value);
|
|||
|
}
|
|||
|
binaryWriter.Write(_iInts.Count);
|
|||
|
foreach (var current in _iInts)
|
|||
|
{
|
|||
|
binaryWriter.Write(current.Key);
|
|||
|
binaryWriter.Write(current.Value);
|
|||
|
}
|
|||
|
|
|||
|
//string
|
|||
|
binaryWriter.Write(_sStrings.Count);
|
|||
|
foreach (var current in _sStrings)
|
|||
|
{
|
|||
|
binaryWriter.Write(current.Key);
|
|||
|
binaryWriter.Write(current.Value);
|
|||
|
}
|
|||
|
binaryWriter.Write(_iStrings.Count);
|
|||
|
foreach (var current in _iStrings)
|
|||
|
{
|
|||
|
binaryWriter.Write(current.Key);
|
|||
|
binaryWriter.Write(current.Value);
|
|||
|
}
|
|||
|
|
|||
|
// sIntList
|
|||
|
binaryWriter.Write(_sIntLists.Count);
|
|||
|
foreach (var current in _sIntLists)
|
|||
|
{
|
|||
|
binaryWriter.Write(current.Key);
|
|||
|
binaryWriter.Write(current.Value.Count);
|
|||
|
foreach (int current1 in current.Value)
|
|||
|
{
|
|||
|
binaryWriter.Write(current1);
|
|||
|
}
|
|||
|
}
|
|||
|
binaryWriter.Write(_iIntLists.Count);
|
|||
|
foreach (var current in _iIntLists)
|
|||
|
{
|
|||
|
binaryWriter.Write(current.Key);
|
|||
|
binaryWriter.Write(current.Value.Count);
|
|||
|
foreach (int current1 in current.Value)
|
|||
|
{
|
|||
|
binaryWriter.Write(current1);
|
|||
|
}
|
|||
|
}
|
|||
|
// sStringList
|
|||
|
binaryWriter.Write(_sStringLists.Count);
|
|||
|
foreach (var current in _sStringLists)
|
|||
|
{
|
|||
|
binaryWriter.Write(current.Key);
|
|||
|
binaryWriter.Write(current.Value.Count);
|
|||
|
foreach (string current1 in current.Value)
|
|||
|
{
|
|||
|
binaryWriter.Write(current1);
|
|||
|
}
|
|||
|
}
|
|||
|
binaryWriter.Write(_sStringLists.Count);
|
|||
|
foreach (var current in _sStringLists)
|
|||
|
{
|
|||
|
binaryWriter.Write(current.Key);
|
|||
|
binaryWriter.Write(current.Value.Count);
|
|||
|
foreach (string current1 in current.Value)
|
|||
|
{
|
|||
|
binaryWriter.Write(current1);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
WriteCRC(stream);
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (System.Exception ex)
|
|||
|
{
|
|||
|
Debug.LogException(ex);
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
private void WriteCRC(MemoryStream stream)
|
|||
|
{
|
|||
|
stream.TryGetBuffer(out var segment);
|
|||
|
SetCRC16(segment.Array, segment.Offset, segment.Count);
|
|||
|
Encrypt(segment.Array, segment.Offset, segment.Count);
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
private static readonly ushort[] _Crc16 = {
|
|||
|
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
|
|||
|
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
|
|||
|
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
|
|||
|
0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
|
|||
|
0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
|
|||
|
0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
|
|||
|
0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
|
|||
|
0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
|
|||
|
0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
|
|||
|
0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
|
|||
|
0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
|
|||
|
0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
|
|||
|
0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
|
|||
|
0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
|
|||
|
0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
|
|||
|
0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
|
|||
|
0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
|
|||
|
0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
|
|||
|
0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
|
|||
|
0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
|
|||
|
0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
|
|||
|
0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
|
|||
|
0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
|
|||
|
0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
|
|||
|
0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
|
|||
|
0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
|
|||
|
0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
|
|||
|
0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
|
|||
|
0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
|
|||
|
0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
|
|||
|
0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
|
|||
|
0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
|
|||
|
};
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="bytes"></param>
|
|||
|
/// <param name="offset"></param>
|
|||
|
/// <param name="length"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static ushort CalcCRC16(byte[] bytes, int offset, int length)
|
|||
|
{
|
|||
|
ushort crc = 0;
|
|||
|
for (int i = offset; i < length; i++)
|
|||
|
{
|
|||
|
byte j = (byte)((crc >> 8) ^ bytes[i]);
|
|||
|
crc = (ushort)((crc << 8) ^ _Crc16[j]);
|
|||
|
}
|
|||
|
return crc;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="bytes"></param>
|
|||
|
/// <param name="offset"></param>
|
|||
|
/// <param name="length"></param>
|
|||
|
public static void SetCRC16(byte[] bytes, int offset, int length)
|
|||
|
{
|
|||
|
//zip 自己校验
|
|||
|
//return true;
|
|||
|
var crc = CalcCRC16(bytes, offset + 2, length);
|
|||
|
var crcBytes = System.BitConverter.GetBytes(crc);
|
|||
|
bytes[offset + 0] = crcBytes[0];
|
|||
|
bytes[offset + 1] = crcBytes[1];
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="bytes"></param>
|
|||
|
/// <param name="offset"></param>
|
|||
|
/// <param name="length"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static bool GetCRC16(byte[] bytes, int offset, int length)
|
|||
|
{
|
|||
|
//zip 自己校验
|
|||
|
//return true;
|
|||
|
var read = System.BitConverter.ToUInt16(bytes, offset);
|
|||
|
|
|||
|
var crc = CalcCRC16(bytes, offset + 2, length);
|
|||
|
//var readCrc = bytes[0] ^ (bytes[1] << 8);
|
|||
|
return crc == read || read == MAGIC;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>Encrypts the specified udid.</summary>
|
|||
|
private void Encrypt(byte[] data, int offset, int length)
|
|||
|
{
|
|||
|
Obfuscate(_secretKey, data, offset, length);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>Decrypts the specified udid.</summary>
|
|||
|
private void Decrypt(byte[] data, int offset, int length)
|
|||
|
{
|
|||
|
Obfuscate(_secretKey, data, offset, length);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="oldV"></param>
|
|||
|
/// <param name="newV"></param>
|
|||
|
private void FixVersion(int oldV, int newV)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
private static string _BackupFileName
|
|||
|
{
|
|||
|
get { return "backup.gaz"; }
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="fileName"></param>
|
|||
|
/// <param name="stream"></param>
|
|||
|
/// <param name="backup"></param>
|
|||
|
/// <returns></returns>
|
|||
|
private static bool ReadAllBytesLocalOrBackup(string fileName, Stream stream, bool backup)
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(KFileUtils.LocalDocumentsPath))
|
|||
|
{
|
|||
|
if (!backup)
|
|||
|
{
|
|||
|
string formalFile = KFileUtils.Combine(KFileUtils.LocalDocumentsPath, fileName);
|
|||
|
if (KFileUtils.ExistsFile(formalFile))
|
|||
|
{
|
|||
|
return KFileUtils.ReadAllBytes(formalFile, stream);
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
string backupFile = KFileUtils.Combine(KFileUtils.LocalDocumentsPath, _BackupFileName);
|
|||
|
if (KFileUtils.ExistsFile(backupFile))
|
|||
|
{
|
|||
|
return KFileUtils.ReadAllBytes(backupFile, stream);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="fileName"></param>
|
|||
|
/// <param name="stream"></param>
|
|||
|
/// <param name="backup"></param>
|
|||
|
/// <returns></returns>
|
|||
|
private static async Task<bool> ReadAllBytesLocalOrBackupAsync(string fileName, Stream stream, bool backup)
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(KFileUtils.LocalDocumentsPath))
|
|||
|
{
|
|||
|
if (!backup)
|
|||
|
{
|
|||
|
string formalFile = KFileUtils.Combine(KFileUtils.LocalDocumentsPath, fileName);
|
|||
|
if (KFileUtils.ExistsFile(formalFile))
|
|||
|
{
|
|||
|
var result = await KFileUtils.ReadAllBytesAsync(formalFile, stream);
|
|||
|
return result;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
string backupFile = KFileUtils.Combine(KFileUtils.LocalDocumentsPath, _BackupFileName);
|
|||
|
if (KFileUtils.ExistsFile(backupFile))
|
|||
|
{
|
|||
|
var result = await KFileUtils.ReadAllBytesAsync(backupFile, stream);
|
|||
|
return result;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="fileName"></param>
|
|||
|
/// <param name="stream"></param>
|
|||
|
/// <param name="backup"></param>
|
|||
|
/// <returns></returns>
|
|||
|
private static int WriteBytesToLocalAndBackup(string fileName, Stream stream, bool backup)
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(KFileUtils.LocalDocumentsPath))
|
|||
|
{
|
|||
|
string formalFile = KFileUtils.Combine(KFileUtils.LocalDocumentsPath, fileName);
|
|||
|
if (KFileUtils.WriteBytes(formalFile, stream))
|
|||
|
{
|
|||
|
if (backup && KFileUtils.ExistsFile(formalFile))
|
|||
|
{
|
|||
|
string backupFile = KFileUtils.Combine(KFileUtils.LocalDocumentsPath, _BackupFileName);
|
|||
|
KFileUtils.CopyFile(formalFile, backupFile);
|
|||
|
return 2;
|
|||
|
}
|
|||
|
return 1;
|
|||
|
}
|
|||
|
}
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="fileName"></param>
|
|||
|
/// <param name="stream"></param>
|
|||
|
/// <param name="backup"></param>
|
|||
|
/// <returns></returns>
|
|||
|
private static async Task<bool> WriteBytesToLocalAndBackupAsync(string fileName, Stream stream, bool backup)
|
|||
|
{
|
|||
|
bool result = false;
|
|||
|
if (!string.IsNullOrEmpty(KFileUtils.LocalDocumentsPath))
|
|||
|
{
|
|||
|
string formalFile = KFileUtils.Combine(KFileUtils.LocalDocumentsPath, fileName);
|
|||
|
result = await KFileUtils.WriteAllBytesAsync(formalFile, stream);
|
|||
|
if (result && backup && KFileUtils.ExistsFile(formalFile))
|
|||
|
{
|
|||
|
string backupFile = KFileUtils.Combine(KFileUtils.LocalDocumentsPath, _BackupFileName);
|
|||
|
KFileUtils.CopyFile(formalFile, backupFile);
|
|||
|
}
|
|||
|
}
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="fileName"></param>
|
|||
|
/// <param name="backup"></param>
|
|||
|
private static void DeleteLocalAndBackup(string fileName, bool backup)
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(KFileUtils.LocalDocumentsPath))
|
|||
|
{
|
|||
|
string formalFile = KFileUtils.Combine(KFileUtils.LocalDocumentsPath, fileName);
|
|||
|
KFileUtils.DeleteFile(formalFile);
|
|||
|
if (backup)
|
|||
|
{
|
|||
|
string backupFile = KFileUtils.Combine(KFileUtils.LocalDocumentsPath, _BackupFileName);
|
|||
|
KFileUtils.DeleteFile(backupFile);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>Obfuscates the specified udid.</summary>
|
|||
|
private static void Obfuscate(string udid, byte[] data, int offset, int length)
|
|||
|
{
|
|||
|
#if USE_OBFUSCATE
|
|||
|
int dl = data.Length;
|
|||
|
int ul = udid.Length;
|
|||
|
|
|||
|
for (int i = 0, j = 0; i < dl; i++, j++)
|
|||
|
{
|
|||
|
j = j < ul ? j : 0;
|
|||
|
data[i] = (byte)(data[i] ^ udid[j]);
|
|||
|
}
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
private bool WriteToStream(bool compress)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_stream.SetLength(0);
|
|||
|
if (compress)
|
|||
|
{
|
|||
|
_zipStream.SetLength(0);
|
|||
|
if (!this.WriteToStream(_zipStream))
|
|||
|
return false;
|
|||
|
//
|
|||
|
_zipStream.Position = 0;
|
|||
|
using (var gzip = new GZipStream(_stream, CompressionMode.Compress, true))
|
|||
|
{
|
|||
|
_zipStream.CopyTo(gzip);
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if (!this.WriteToStream(_stream))
|
|||
|
return false;
|
|||
|
}
|
|||
|
_stream.Position = 0;
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (System.Exception ex)
|
|||
|
{
|
|||
|
Debug.LogException(ex);
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="checkUId"></param>
|
|||
|
/// <returns></returns>
|
|||
|
private bool ReadFromStream(bool compress, bool checkUId = true)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (compress)
|
|||
|
{
|
|||
|
_zipStream.SetLength(0);
|
|||
|
_stream.Position = 0;
|
|||
|
using (var gzip = new GZipStream(_stream, CompressionMode.Decompress, true))
|
|||
|
{
|
|||
|
gzip.CopyTo(_zipStream);
|
|||
|
}
|
|||
|
_zipStream.Position = 0;
|
|||
|
return ReadFromStream(_zipStream, checkUId);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_stream.Position = 0;
|
|||
|
return ReadFromStream(_stream, checkUId);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (System.Exception ex)
|
|||
|
{
|
|||
|
Debug.LogException(ex);
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>Loads this instance.</summary>
|
|||
|
private bool LoadFile()
|
|||
|
{
|
|||
|
_stream.SetLength(0);
|
|||
|
if (ReadAllBytesLocalOrBackup(_fileName, _stream, false))
|
|||
|
{
|
|||
|
if (ReadFromStream(true))
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
_stream.SetLength(0);
|
|||
|
if (ReadAllBytesLocalOrBackup(_fileName, _stream, true))
|
|||
|
{
|
|||
|
if (ReadFromStream(true))
|
|||
|
{
|
|||
|
Debug.Log("读取备份存档");
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
private async Task LoadFileAsync()
|
|||
|
{
|
|||
|
_stream.SetLength(0);
|
|||
|
if (await ReadAllBytesLocalOrBackupAsync(_fileName, _stream, false))
|
|||
|
{
|
|||
|
if (ReadFromStream(true))
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
_stream.SetLength(0);
|
|||
|
if (await ReadAllBytesLocalOrBackupAsync(_fileName, _stream, true))
|
|||
|
{
|
|||
|
if (ReadFromStream(true))
|
|||
|
{
|
|||
|
Debug.Log("读取备份存档.");
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>Saves this instance.</summary>
|
|||
|
private void SaveFile(bool force = false)
|
|||
|
{
|
|||
|
if (this.modified || force)
|
|||
|
{
|
|||
|
this.modified = false;
|
|||
|
|
|||
|
if (WriteToStream(true))
|
|||
|
WriteBytesToLocalAndBackup(_fileName, _stream, true);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
private async Task SaveFileAsync(bool force = false)
|
|||
|
{
|
|||
|
if (this.modified || force)
|
|||
|
{
|
|||
|
this.modified = false;
|
|||
|
|
|||
|
if (WriteToStream(true))
|
|||
|
await WriteBytesToLocalAndBackupAsync(_fileName, _stream, true);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
private void DeleteFile()
|
|||
|
{
|
|||
|
DeleteLocalAndBackup(_fileName, true);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public string ToBase64Text(bool compress)
|
|||
|
{
|
|||
|
if (WriteToStream(compress))
|
|||
|
if (_stream.TryGetBuffer(out var segment))
|
|||
|
{
|
|||
|
var result = System.Convert.ToBase64String(segment.Array, segment.Offset, segment.Count);
|
|||
|
return result ?? string.Empty;
|
|||
|
}
|
|||
|
return string.Empty;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="text"></param>
|
|||
|
public void FromBase64Text(string text, bool compress)
|
|||
|
{
|
|||
|
Reset();
|
|||
|
if (string.IsNullOrEmpty(text))
|
|||
|
return;
|
|||
|
|
|||
|
byte[] data = System.Convert.FromBase64String(text);
|
|||
|
_stream.SetLength(0);
|
|||
|
_stream.Write(data, 0, data.Length);
|
|||
|
if (ReadFromStream(compress, false))
|
|||
|
{
|
|||
|
//SaveFile(true);
|
|||
|
SaveFileAsync(true);
|
|||
|
Debug.Log("读取云存档成功");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Debug.Log("读取云存档失败");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private readonly MemoryStream _zipStream = new MemoryStream(32 * 1024);
|
|||
|
private readonly MemoryStream _stream = new MemoryStream(16 * 1024);
|
|||
|
public void Dispose()
|
|||
|
{
|
|||
|
_zipStream.Dispose();
|
|||
|
_stream.Dispose();
|
|||
|
}
|
|||
|
|
|||
|
#region MANAGER
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="uid"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static Archive Create(string uid)
|
|||
|
{
|
|||
|
return new Archive(uid, "");
|
|||
|
}
|
|||
|
|
|||
|
public static Archive Create(string localId, string serverId)
|
|||
|
{
|
|||
|
return new Archive(localId, serverId);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public bool Load()
|
|||
|
{
|
|||
|
return LoadFile();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public void Save()
|
|||
|
{
|
|||
|
//SaveFile();
|
|||
|
SaveFileAsync();
|
|||
|
}
|
|||
|
|
|||
|
public Task LoadAsync()
|
|||
|
{
|
|||
|
return LoadFileAsync();
|
|||
|
}
|
|||
|
|
|||
|
public async void SaveAsync()
|
|||
|
{
|
|||
|
await SaveFileAsync();
|
|||
|
}
|
|||
|
|
|||
|
public void Reset()
|
|||
|
{
|
|||
|
this.modified = false;
|
|||
|
RemoveAll();
|
|||
|
}
|
|||
|
|
|||
|
public void Delete()
|
|||
|
{
|
|||
|
Reset();
|
|||
|
DeleteFile();
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|