ZeroLauncher/Manager/DataStreamNew.cs

277 lines
5.9 KiB
C#
Raw Normal View History

using System.Buffers;
using System.Diagnostics;
2024-06-23 10:04:00 +08:00
using System.IO;
using System.IO.Compression;
namespace Zerolauncher.Manager
{
class DiyWriteSteamer()
2024-06-23 10:04:00 +08:00
{
ArrayBufferWriter<byte> data = new();
public void WriteInt(int value)
{
data.Write(BitConverter.GetBytes(value));
}
public void WriteString(string value)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(value);
WriteInt(bytes.Length);
data.Write(bytes);
}
public byte[] GetBytes() => data.WrittenSpan.ToArray();
2024-06-23 10:04:00 +08:00
}
class DiyReader(byte[] data, int _offset=0)
2024-06-23 10:04:00 +08:00
{
public int ReadInt()
{
_offset += 4;
return BitConverter.ToInt32(data, _offset - 4);
2024-06-23 10:04:00 +08:00
}
public string ReadStr()
2024-06-23 10:04:00 +08:00
{
int lenght = ReadInt();
_offset += lenght;
return System.Text.Encoding.UTF8.GetString(data, _offset - lenght, lenght);
2024-06-23 10:04:00 +08:00
}
}
2024-06-23 10:04:00 +08:00
internal static class DataStreamNew
{
private const string Path = "users.data";
private static readonly byte[] Key = [9, 5, 33, 64, 99, 200, 66, 77];
2025-02-28 00:18:57 +08:00
private const int HeaderVersion = 20250228;
2024-06-23 10:04:00 +08:00
public static void Load()
{
if (File.Exists(Path))
2024-06-23 10:04:00 +08:00
{
byte[] byteArray;
using (var fs = new FileStream(Path, FileMode.Open, FileAccess.Read))
2024-06-23 10:04:00 +08:00
{
byteArray = new byte[fs.Length];
fs.ReadExactly(byteArray, 0, byteArray.Length);
2024-06-23 10:04:00 +08:00
}
2024-06-23 10:04:00 +08:00
ByteCry(byteArray);
byteArray = Decompress(byteArray);
{
var data = new DiyReader(byteArray);
if (data.ReadInt() != HeaderVersion)
2024-06-23 10:04:00 +08:00
{
OldDataUpdate(data);
return;
}
GroupsData.Init(data);
AccountData.Init(data);
2024-06-23 10:04:00 +08:00
}
return;
}
InitDefaultData();
}
static void OldDataUpdate(DiyReader _item)
2024-06-23 10:04:00 +08:00
{
InitDefaultData();
}
static void InitDefaultData()
{
Trace.WriteLine("加载初始数据");
GroupsData.InitDefaultData();
AccountData.InitDefaultData();
}
public static void Save()
{
var data = new DiyWriteSteamer();
data.WriteInt(HeaderVersion);
GroupsData.Out(data);
AccountData.Out(data);
var buffer = CompressBytes(data.GetBytes());
ByteCry(buffer);
using (FileStream file = new(Path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
2024-06-23 10:04:00 +08:00
{
file.Seek(0, SeekOrigin.Begin);
2024-06-23 10:04:00 +08:00
file.Write(buffer);
}
}
static void ByteCry(byte[] data)
{
for (int i = 0; i < data.Length; i++)
{
data[i] = (byte)(data[i] ^ Key[i % Key.Length]);
2024-06-23 10:04:00 +08:00
}
}
//压缩字节
//1.创建压缩的数据流
//2.设定compressStream为存放被压缩的文件流,并设定为压缩模式
//3.将需要压缩的字节写到被压缩的文件流
static byte[] CompressBytes(byte[] bytes)
{
using var compressStream = new MemoryStream();
using (var zipStream = new GZipStream(compressStream, CompressionMode.Compress))
zipStream.Write(bytes, 0, bytes.Length);
return compressStream.ToArray();
2024-06-23 10:04:00 +08:00
}
//解压缩字节
//1.创建被压缩的数据流
//2.创建zipStream对象并传入解压的文件流
//3.创建目标流
//4.zipStream拷贝到目标流
//5.返回目标流输出字节
static byte[] Decompress(byte[] bytes)
{
using (var compressStream = new MemoryStream(bytes))
{
using (var zipStream = new GZipStream(compressStream, CompressionMode.Decompress))
{
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
return resultStream.ToArray();
}
}
}
}
}
public class AccountNew
{
public int GroupId, ProviderId, ServerId;
2024-06-23 10:04:00 +08:00
public string userName, userPWD, nickName;
}
2024-08-04 18:24:17 +08:00
internal static class AccountData
2024-06-23 10:04:00 +08:00
{
public static int TeamIndex = 0;
2024-06-23 10:04:00 +08:00
2024-08-04 18:24:17 +08:00
public static readonly Dictionary<int, List<AccountNew>> Data = [];
2024-06-23 10:04:00 +08:00
2024-08-04 18:24:17 +08:00
public static readonly Dictionary<string, AccountNew> Dict = [];
2024-06-23 10:04:00 +08:00
public static void Init(DiyReader data)
2024-06-23 10:04:00 +08:00
{
TeamIndex = data.ReadInt();
var accountCount = data.ReadInt();
2024-08-04 18:24:17 +08:00
for (var i = 0; i < accountCount; i++)
2024-06-23 10:04:00 +08:00
{
AddAccount(new AccountNew()
{
GroupId = data.ReadInt(),
ProviderId = data.ReadInt(),
ServerId = data.ReadInt(),
userName = data.ReadStr(),
userPWD = data.ReadStr(),
nickName = data.ReadStr()
2024-06-23 10:04:00 +08:00
});
}
}
public static void InitDefaultData()
{
AddAccount(new AccountNew()
{
GroupId = 0,
ProviderId = 0,
ServerId = 1,
2024-06-23 10:04:00 +08:00
userName = "test",
userPWD = "test",
nickName = "右键修改账号"
});
}
public static void AddAccount(AccountNew item)
{
2024-08-04 18:24:17 +08:00
var count = 0;
while (Dict.ContainsKey(item.nickName))
2024-06-23 10:04:00 +08:00
item.nickName += count++.ToString();
if (CheckGroup(item.GroupId))
Data[item.GroupId].Add(item);
2024-06-23 10:04:00 +08:00
}
public static bool CheckGroup(int groupId)
{
2024-08-04 18:24:17 +08:00
if (!GroupsData.Data.ContainsKey(groupId)) return false;
if (!Data.ContainsKey(groupId)) Data.Add(groupId, []);
return true;
2024-06-23 10:04:00 +08:00
}
public static void RemoveGroup()
{
2024-08-04 18:24:17 +08:00
GroupsData.Data.Remove(TeamIndex);
foreach (var item in Data[TeamIndex])
2024-06-23 10:04:00 +08:00
{
2024-08-04 18:24:17 +08:00
Dict.Remove(item.nickName);
2024-06-23 10:04:00 +08:00
}
2024-08-04 18:24:17 +08:00
Data.Remove(TeamIndex);
2024-06-23 10:04:00 +08:00
}
public static void Out(DiyWriteSteamer data)
2024-06-23 10:04:00 +08:00
{
data.WriteInt(TeamIndex);
var arr = new List<AccountNew>();
foreach (var item in Data.Values)
arr.AddRange(item);
data.WriteInt(arr.Count);
foreach (var item in arr)
2024-06-23 10:04:00 +08:00
{
data.WriteInt(item.GroupId);
data.WriteInt(item.ProviderId);
data.WriteInt(item.ServerId);
data.WriteString(item.userName);
data.WriteString(item.userPWD);
data.WriteString(item.nickName);
2024-06-23 10:04:00 +08:00
}
}
}
2024-08-04 18:24:17 +08:00
internal static class GroupsData
2024-06-23 10:04:00 +08:00
{
2024-08-04 18:24:17 +08:00
public static readonly Dictionary<int, string> Data = [];
public static void Init(DiyReader dataItem)
2024-06-23 10:04:00 +08:00
{
2024-08-04 18:24:17 +08:00
var count = dataItem.ReadInt();
for (var i = 0; i < count; i++)
2024-06-23 10:04:00 +08:00
{
2024-08-04 18:24:17 +08:00
Data.Add(dataItem.ReadInt(), dataItem.ReadStr());
2024-06-23 10:04:00 +08:00
}
}
public static void InitDefaultData()
{
2024-08-04 18:24:17 +08:00
Data.Add(0, "队伍1");
2024-06-23 10:04:00 +08:00
}
public static void Out(DiyWriteSteamer data)
2024-06-23 10:04:00 +08:00
{
data.WriteInt(Data.Count);
2024-08-04 18:24:17 +08:00
foreach (var item in Data)
2024-06-23 10:04:00 +08:00
{
data.WriteInt(item.Key);
data.WriteString(item.Value);
2024-06-23 10:04:00 +08:00
}
}
}
}