66 lines
1.8 KiB
C#
66 lines
1.8 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
using Newtonsoft.Json;
|
|
using MindPowerSdk;
|
|
using UnityEngine;
|
|
|
|
namespace MindPowerSdk
|
|
{
|
|
[StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Ansi)]
|
|
public abstract class CRawDataInfo
|
|
{
|
|
public int bExist;
|
|
public int nIndex;
|
|
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 72)]
|
|
public string szDataName;
|
|
|
|
public uint dwLastUseTick;
|
|
public int bEnable;
|
|
public uint pData;
|
|
public uint dwPackOffset;
|
|
public uint dwDataSize;
|
|
public int nID;
|
|
public uint dwLoadCnt;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 所有的配置数据,读取方式都是一样的(只要布局好结构)
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public abstract class CRawDataSet<T> where T : CRawDataInfo
|
|
{
|
|
public T[] Data { get; private set; }
|
|
|
|
private Dictionary<int, int> _idMap;
|
|
|
|
public void LoadBin(string file)
|
|
{
|
|
using var fs = File.OpenRead(file);
|
|
using BinaryReader reader = new BinaryReader(fs);
|
|
|
|
long fileSize = fs.Length;
|
|
uint infoSize = reader.ReadUInt32();
|
|
|
|
int resNum = (int)(fileSize / infoSize);
|
|
Debug.Log($"{file} - {infoSize}({resNum})");
|
|
|
|
Data = new T[resNum];
|
|
_idMap = new Dictionary<int, int>(resNum);
|
|
for (int i = 0; i < resNum; i++)
|
|
{
|
|
byte[] data = reader.ReadBytes((int)infoSize);
|
|
T info = StructReader.ReadStructFromArray<T>(data);
|
|
Data[i] = info;
|
|
_idMap.Add(info.nID, i);
|
|
//Debug.Log(JsonConvert.SerializeObject(info));
|
|
}
|
|
}
|
|
|
|
public T Get(int id)
|
|
{
|
|
return Data[_idMap[id]];
|
|
}
|
|
}
|
|
} |