Skip to content

Instantly share code, notes, and snippets.

@hsienwei
Created March 20, 2018 09:54
Show Gist options
  • Save hsienwei/7b43759538f96423c3bbe9842403ad6f to your computer and use it in GitHub Desktop.
Save hsienwei/7b43759538f96423c3bbe9842403ad6f to your computer and use it in GitHub Desktop.
一個用來將指定byte buff轉其他型態資料的類別.
class ByteConverter
{
/*public static byte[] BuffToBuildData(int data_id)
{
int Length = 0;
System.IntPtr ptr = GamePlayer_GetBattleReportBaseInfoBuf(data_id, ref Length);
if (ptr == System.IntPtr.Zero)
{
return null;
}
byte[] Source = new byte[Length];
Marshal.Copy(ptr, Source, 0, Length);
return Source;
}
public static BuildData2 BuffToBuildData(byte[] Source)
{
BuildData2 a = new BuildData2();
UnityEngine.Debug.Log("1");
unsafe
{
fixed (byte* pbuffer = Source)
{
byte* scan = pbuffer;
GetBuffInt(ref a.m_ID, ref Source, ref scan, pbuffer);
GetBuffInt(ref a.m_BuildType, ref Source, ref scan, pbuffer);
GetBuffInt(ref a.m_NameID, ref Source, ref scan, pbuffer);
GetBuffInt(ref a.m_DescriptionID, ref Source, ref scan, pbuffer);
GetBuffInt(ref a.m_BuildLV, ref Source, ref scan, pbuffer);
GetBuffInt(ref a.m_Group, ref Source, ref scan, pbuffer);
GetBuffByte(ref a.m_OpenFlag, ref scan);
GetBuffByte(ref a.m_ShowFlag, ref scan);
GetBuffString(ref a.m_IconName, ref Source, ref scan, pbuffer, 100);
GetBuffIntAry(ref a.m_FrontBuildID, ref Source, ref scan, pbuffer);
GetBuffIntAry(ref a.m_FrontTechID, ref Source, ref scan, pbuffer);
GetBuffIntAry(ref a.m_UpCostType, ref Source, ref scan, pbuffer);
GetBuffIntAry(ref a.m_UpCostValue, ref Source, ref scan, pbuffer);
GetBuffInt(ref a.m_BuildTime, ref Source, ref scan, pbuffer);
GetBuffIntAry(ref a.m_BuildBuffID, ref Source, ref scan, pbuffer);
GetBuffIntAry(ref a.m_BuildBuffIDPlus, ref Source, ref scan, pbuffer);
GetBuffInt(ref a.m_BuildFightBuffType, ref Source, ref scan, pbuffer);
GetBuffInt(ref a.m_BuildFightBuffValue, ref Source, ref scan, pbuffer);
GetBuffInt(ref a.m_BuildShipID, ref Source, ref scan, pbuffer);
GetBuffString(ref a.m_FileName, ref Source, ref scan, pbuffer, 100);
GetBuffString(ref a.m_PrefabResPathLow, ref Source, ref scan, pbuffer, 100);
GetBuffFloat(ref a.m_PrefabLowSize, ref Source, ref scan, pbuffer);
GetBuffString(ref a.m_PrefabResPathHigh, ref Source, ref scan, pbuffer, 100);
GetBuffFloat(ref a.m_PrefabHighSize, ref Source, ref scan, pbuffer);
GetBuffString(ref a.m_UIStaticDisplay, ref Source, ref scan, pbuffer, 100);
GetBuffFloat(ref a.m_UIDisplaySize, ref Source, ref scan, pbuffer);
}
}
return a;
}
*/
public static unsafe void GetBuffInt32(ref Int32 Target, ref byte[] array, ref byte* scan, byte* Origin)
{
Target = System.BitConverter.ToInt32(array, (int)(scan - Origin));
scan += 4;
}
public static unsafe void GetBuffInt16(ref Int16 Target, ref byte[] array, ref byte* scan, byte* Origin)
{
Target = System.BitConverter.ToInt16(array, (int)(scan - Origin));
scan += 2;
}
public static unsafe void GetBuffInt64(ref Int64 Target, ref byte[] array, ref byte* scan, byte* Origin)
{
Target = System.BitConverter.ToInt64(array, (int)(scan - Origin));
scan += 8;
}
public static unsafe void GetBuffInt32Ary(ref Int32[] Target, ref byte[] array, ref byte* scan, byte* Origin)
{
for (int i = 0; i < Target.Length; ++i)
{
Target[i] = System.BitConverter.ToInt32(array, (int)(scan - Origin));
scan += 4;
}
}
public static unsafe void GetBuffInt16Ary(ref Int16[] Target, ref byte[] array, ref byte* scan, byte* Origin)
{
for (int i = 0; i < Target.Length; ++i)
{
Target[i] = System.BitConverter.ToInt16(array, (int)(scan - Origin));
scan += 2;
}
}
public static unsafe void GetBuffUInt32Ary(ref UInt32[] Target, ref byte[] array, ref byte* scan, byte* Origin)
{
for (int i = 0; i < Target.Length; ++i)
{
Target[i] = System.BitConverter.ToUInt32(array, (int)(scan - Origin));
scan += 4;
}
}
public static unsafe void GetBuffUInt16Ary(ref UInt16[] Target, ref byte[] array, ref byte* scan, byte* Origin)
{
for (int i = 0; i < Target.Length; ++i)
{
Target[i] = System.BitConverter.ToUInt16(array, (int)(scan - Origin));
scan += 2;
}
}
public static unsafe void GetBuffByte(ref byte Target, ref byte* scan)
{
Target = *(byte*)(scan);
scan += 1;
}
public static unsafe void GetBuffFloat(ref float Target, ref byte[] array, ref byte* scan, byte* Origin)
{
byte[] Tmp = new byte[4];
Tmp[0] = array[(int)(scan - Origin) + 0];
Tmp[1] = array[(int)(scan - Origin) + 1];
Tmp[2] = array[(int)(scan - Origin) + 2];
Tmp[3] = array[(int)(scan - Origin) + 3];
UnityEngine.Debug.Log("GetBuffFloat_1_" + System.BitConverter.ToString(Tmp));
Target = System.BitConverter.ToSingle(array, (int)(scan - Origin));
UnityEngine.Debug.Log("GetBuffFloat_2_" + System.BitConverter.ToString(System.BitConverter.GetBytes(Target)));
scan += 4;
}
public static unsafe void GetBuffString(ref string Target, ref byte[] array, ref byte* scan, byte* Origin, int ByteSize)
{
//
// 這樣的作法要自己找字串結尾..
//
Target = (Encoding.UTF8.GetString(array, (int)(scan - Origin), ByteSize));
//Target = Target.TrimEnd('\0');
int i = Target.IndexOf('\0');
if (i >= 0) Target = Target.Substring(0, i);
scan += ByteSize;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment