using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
namespace TS_Server.DataTools
{
public static class SwapitemData
{
public static Dictionary swapitemInfoDatas = new Dictionary();
public static T ReadFromItems<T>(Stream fs)
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(T))];
fs.Read(buffer, 0, Marshal.SizeOf(typeof(T)));
GCHandle Handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
T RetVal = (T)Marshal.PtrToStructure(Handle.AddrOfPinnedObject(), typeof(T));
Handle.Free();
return RetVal;
}
public static bool loadItems()
{
try
{
swapitemInfoDatas?.Clear();
swapitemInfoDatas = new Dictionary<byte, SwapitemInfos>();
using (FileStream fileStream = new FileStream("SwapItem.dat", FileMode.Open, FileAccess.Read))
{
long length = fileStream.Length;
byte[] swapitemDatas = new byte[length];
fileStream.Read(swapitemDatas, 0, (int)length);
fileStream.Close();
DatReader swapDatReader = new DatReader(swapitemDatas);
swapDatReader.DecodeAll();
Stream stream = new MemoryStream(swapDatReader.decodedData);
for (int i = 0; i < swapDatReader.count; i++)
{
SwapitemInfos swapitemInfos = ReadFromItems<SwapitemInfos>(stream);
swapitemInfoDatas.Add((byte)i, swapitemInfos);
}
}
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 获取需要增加的Item, 输入月份和顺序
/// </summary>
/// <param name="month"></param>
/// <param name="index"></param>
/// <returns></returns>
public static ushort GetAddItem(byte month,byte index)
{
swapitemInfoDatas.TryGetValue((byte)(month - 1), out SwapitemInfos swapitemInfos);
if(swapitemInfos.list.Length >= index)
{
return swapitemInfos.list[index].id1;
}
return 0;
}
/// <summary>
/// 获取需要增加的Item, 输入月份和顺序
/// </summary>
/// <param name="month"></param>
/// <param name="index"></param>
/// <returns></returns>
public static SwapitemInfo GetSwapitemInfo(byte month, byte index)
{
swapitemInfoDatas.TryGetValue((byte)(month - 1), out SwapitemInfos swapitemInfos);
return swapitemInfos.list[index-1];
}
}
}
using System.Runtime.InteropServices;
namespace TS_Server.DataTools
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct SwapitemInfo
{
public ushort id1;
public byte count1;
public ushort id2;
public byte count2;
public ushort id3;
public byte count3;
public ushort id4;
public byte count4;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct SwapitemInfos
{
public byte month;
public byte count;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 15)]
public SwapitemInfo[] list;
}
}
public void DecodeAll()
{
position = 0;
size = ReadInt32(bigEndian: false, signed: false);
count = ReadInt32(bigEndian: false, signed: false);
keys[0] = ReadBytes(size, count + 1);
keys[1] = ReadBytes(size, count + 2);
int decodeIndex = 0;
decodedData = new byte[count size];
for (int i = 1; i <= count; i++)
{
decodeIndex = i % 2;
byte[] tempData = ReadBytes(size, i);
tempData = TransformBytes(tempData, keys[decodeIndex]);
Array.Copy(tempData, 0, decodedData, size (i - 1), size);
}
position = 0;
}