ZeroLauncher/Manager/DataStreamNew.cs
2024-08-04 18:24:17 +08:00

330 lines
7.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Diagnostics;
using System.IO;
using System.IO.Compression;
namespace Zerolauncher.Manager
{
enum NameSpace
{
Accounts,
Groups,
ecs,
None
}
static class DataStreamNew
{
private const string path = "users.data";
static byte[] key = [9, 5, 33, 64, 99, 200, 66, 77];
static int hearder_version = 20240531;
static void InitHandle(NameSpace ns, DataItem item)
{
switch (ns)
{
case NameSpace.Accounts:
AccountData.Init(item);
break;
case NameSpace.Groups:
GroupsData.Init(item);
break;
}
}
static DataItem[] GetAllData() { return [GroupsData.Out(), AccountData.Out()]; }
public static void Load()
{
if (File.Exists(path))
{
byte[] byteArray;
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byteArray = new byte[fs.Length];
fs.Read(byteArray, 0, byteArray.Length);
}
ByteCry(byteArray);
byteArray = Decompress(byteArray);
{
var data = new DataItem(NameSpace.None, byteArray);
if (data.ReadInt() != hearder_version)
{
data.ReSetOffset();
OldDataUpdate(data);
return;
}
NameSpace _id;
int _lenght;
while (!data.IsEnd())
{
_id = (NameSpace)data.ReadInt();
_lenght = data.ReadInt();
InitHandle(_id, new DataItem(_id, data.GetBytes(_lenght)));
}
}
return;
}
InitDefaultData();
}
static void OldDataUpdate(DataItem _item)
{
InitDefaultData();
}
static void InitDefaultData()
{
Trace.WriteLine("加载初始数据");
GroupsData.InitDefaultData();
AccountData.InitDefaultData();
}
public static void Save()
{
byte[] buffer;
{
DataItem buffers = new(NameSpace.None, []);
buffers.WriteInt(hearder_version);
foreach (DataItem item in GetAllData())
{
buffers.Write(BitConverter.GetBytes((uint)item.id));
buffers.Write(BitConverter.GetBytes(item.value.Length));
buffers.Write(item.value);
}
buffer = CompressBytes(buffers.value);
ByteCry(buffer);
}
using (FileStream file = new(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
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]);
}
}
//压缩字节
//1.创建压缩的数据流
//2.设定compressStream为存放被压缩的文件流,并设定为压缩模式
//3.将需要压缩的字节写到被压缩的文件流
static byte[] CompressBytes(byte[] bytes)
{
using (MemoryStream compressStream = new MemoryStream())
{
using (var zipStream = new GZipStream(compressStream, CompressionMode.Compress))
zipStream.Write(bytes, 0, bytes.Length);
return compressStream.ToArray();
}
}
//解压缩字节
//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();
}
}
}
}
}
class DataItem
{
public NameSpace id;
int offset = 0;
public byte[] value;
public DataItem(NameSpace id, byte[] value)
{
this.id = id;
this.value = value;
}
public bool IsEnd() { return offset >= value.Length - 1; }
public void ReSetOffset()
{
offset = 0;
}
public byte[] GetBytes(int lenght)
{
offset += lenght;
return value.Skip(offset - lenght).Take(lenght).ToArray();
}
public int ReadInt()
{
offset += 4;
return BitConverter.ToInt32(value, offset - 4);
}
public String ReadStr()
{
int lenght = ReadInt();
offset += lenght;
return System.Text.Encoding.UTF8.GetString(value, offset - lenght, lenght);
}
public void Write(byte[] bytes)
{
var arr = new byte[bytes.Length + value.Length];
Array.Copy(value, arr, value.Length);
Array.Copy(bytes, 0, arr, value.Length, bytes.Length);
value = arr;
}
public void WriteInt(int value)
{
Write(BitConverter.GetBytes(value));
}
public void WriteString(string value)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(value);
WriteInt(bytes.Length);
Write(bytes);
}
}
public class AccountNew
{
public int groupId, providerId, serverId;
public string userName, userPWD, nickName;
}
internal static class AccountData
{
public static int TeamIndex;
public static readonly Dictionary<int, List<AccountNew>> Data = [];
public static readonly Dictionary<string, AccountNew> Dict = [];
public static void Init(DataItem dataItem)
{
dataItem.ReSetOffset();
TeamIndex = dataItem.ReadInt();
var accountCount = dataItem.ReadInt();
for (var i = 0; i < accountCount; i++)
{
AddAccount(new AccountNew()
{
groupId = dataItem.ReadInt(),
providerId = dataItem.ReadInt(),
serverId = dataItem.ReadInt(),
userName = dataItem.ReadStr(),
userPWD = dataItem.ReadStr(),
nickName = dataItem.ReadStr()
});
}
}
public static void InitDefaultData()
{
AddAccount(new AccountNew()
{
groupId = 0,
providerId = 0,
serverId = 1,
userName = "test",
userPWD = "test",
nickName = "右键修改账号"
});
}
public static void AddAccount(AccountNew item)
{
var count = 0;
while (Dict.ContainsKey(item.nickName))
item.nickName += count++.ToString();
if (CheckGroup(item.groupId)) Data[item.groupId].Add(item);
}
public static bool CheckGroup(int groupId)
{
if (!GroupsData.Data.ContainsKey(groupId)) return false;
if (!Data.ContainsKey(groupId)) Data.Add(groupId, []);
return true;
}
public static void RemoveGroup()
{
GroupsData.Data.Remove(TeamIndex);
foreach (var item in Data[TeamIndex])
{
Dict.Remove(item.nickName);
}
Data.Remove(TeamIndex);
}
public static DataItem Out()
{
var res = new DataItem(NameSpace.Accounts, []);
res.WriteInt(TeamIndex);
res.WriteInt(Data.Count);
foreach (var item in Data.Values.SelectMany(items => items))
{
res.WriteInt(item.groupId);
res.WriteInt(item.providerId);
res.WriteInt(item.serverId);
res.WriteString(item.userName);
res.WriteString(item.userPWD);
res.WriteString(item.nickName);
}
return res;
}
}
internal static class GroupsData
{
public static readonly Dictionary<int, string> Data = [];
public static void Init(DataItem dataItem)
{
var count = dataItem.ReadInt();
for (var i = 0; i < count; i++)
{
Data.Add(dataItem.ReadInt(), dataItem.ReadStr());
}
}
public static void InitDefaultData()
{
Data.Add(0, "队伍1");
}
public static DataItem Out()
{
var res = new DataItem(NameSpace.Groups, []);
res.WriteInt(Data.Count);
foreach (var item in Data)
{
res.WriteInt(item.Key);
res.WriteString(item.Value);
}
return res;
}
}
}