Skip to content

Instantly share code, notes, and snippets.

@hsienwei
Created February 8, 2018 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsienwei/6eb886d0579117a26235c437d18561d4 to your computer and use it in GitHub Desktop.
Save hsienwei/6eb886d0579117a26235c437d18561d4 to your computer and use it in GitHub Desktop.
第一種作法, c++指標到c#從IntPtr轉換成byte後再依照自己的指定順序塞進結構中
PLUGIN_DLL_API const void* GetDataPtr(const int data_id, int &length)
{
length = sizeof(Data);
const Data* Dta = ... // 資料來源
if (NULL == Dta)
{
return NULL;
}
return (const void*)Dta;
}
[DllImport(PlatformDllName)]
private static extern System.IntPtr GetDataPtr(int data_id, ref int Length);
public static byte[] GetDataBytes(int data_id)
{
int Length = 0;
System.IntPtr ptr = GetDataPtr(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 Data BytesToData(byte[] Source)
{
Data Dta = new Data();
unsafe
{
fixed (byte* pbuffer = Source)
{
byte* scan = pbuffer;
GetBuffByte(ref Dta.m_OpenFlag, ref scan);
GetBuffInt(ref Dta.m_ID, ref Source, ref scan, pbuffer);
GetBuffByte(ref Dta.m_ShowFlag, ref scan);
GetBuffInt(ref Dta.m_BuildType, ref Source, ref scan, pbuffer);
GetBuffInt(ref Dta.m_NameID, ref Source, ref scan, pbuffer);
GetBuffInt(ref Dta.m_DescriptionID, ref Source, ref scan, pbuffer);
GetBuffInt(ref Dta.m_BuildLV, ref Source, ref scan, pbuffer);
GetBuffInt(ref Dta.m_Group, ref Source, ref scan, pbuffer);
GetBuffString(ref Dta.m_IconName, ref Source, ref scan, pbuffer, 100);
GetBuffInt16(ref Dta.m_TestInt16, ref Source, ref scan, pbuffer);
GetBuffInt64(ref Dta.m_TestInt64, ref Source, ref scan, pbuffer);
GetBuffUInt16(ref Dta.m_TestUInt16, ref Source, ref scan, pbuffer);
GetBuffUInt64(ref Dta.m_TestUInt64, ref Source, ref scan, pbuffer);
GetBuffIntPtr(ref Dta.m_TestInt16Vec, ref Source, ref scan, pbuffer);
GetBuffIntAry(ref Dta.m_FrontBuildID, ref Source, ref scan, pbuffer);
GetBuffIntAry(ref Dta.m_FrontTechID, ref Source, ref scan, pbuffer);
GetBuffIntAry(ref Dta.m_UpCostType, ref Source, ref scan, pbuffer);
GetBuffIntAry(ref Dta.m_UpCostValue, ref Source, ref scan, pbuffer);
GetBuffInt(ref Dta.m_BuildTime, ref Source, ref scan, pbuffer);
GetBuffIntAry(ref Dta.m_BuildBuffID, ref Source, ref scan, pbuffer);
GetBuffIntAry(ref Dta.m_BuildBuffIDPlus, ref Source, ref scan, pbuffer);
GetBuffInt(ref Dta.m_BuildFightBuffType, ref Source, ref scan, pbuffer);
GetBuffInt(ref Dta.m_BuildFightBuffValue, ref Source, ref scan, pbuffer);
GetBuffInt(ref Dta.m_BuildShipID, ref Source, ref scan, pbuffer);
GetBuffString(ref Dta.m_FileName, ref Source, ref scan, pbuffer, 100);
GetBuffString(ref Dta.m_PrefabResPathLow, ref Source, ref scan, pbuffer, 100);
GetBuffFloat(ref Dta.m_PrefabLowSize, ref Source, ref scan, pbuffer);
GetBuffString(ref Dta.m_PrefabResPathHigh, ref Source, ref scan, pbuffer, 100);
GetBuffFloat(ref Dta.m_PrefabHighSize, ref Source, ref scan, pbuffer);
GetBuffString(ref Dta.m_UIStaticDisplay, ref Source, ref scan, pbuffer, 100);
GetBuffFloat(ref Dta.m_UIDisplaySize, ref Source, ref scan, pbuffer);
GetBuffIntPtr(ref Dta.m_TestUInt16Vec, ref Source, ref scan, pbuffer);
}
}
return Dta;
}
static unsafe void GetBuffIntPtr(ref IntPtr Target, ref byte[] array, ref byte* scan, byte* Origin)
{
if (Marshal.SizeOf(typeof(IntPtr)) == 8)
{
Target = (IntPtr)System.BitConverter.ToUInt64(array, (int)(scan - Origin));
scan += 8;
}
else
{
Target = (IntPtr)System.BitConverter.ToUInt32(array, (int)(scan - Origin));
scan += 4;
}
}
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;
}
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;
}
}
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;
}
static unsafe void GetBuffUInt16(ref UInt16 Target, ref byte[] array, ref byte* scan, byte* Origin)
{
Target = System.BitConverter.ToUInt16(array, (int)(scan - Origin));
scan += 2;
}
static unsafe void GetBuffUInt64(ref UInt64 Target, ref byte[] array, ref byte* scan, byte* Origin)
{
Target = System.BitConverter.ToUInt64(array, (int)(scan - Origin));
scan += 8;
}
static unsafe void GetBuffInt(ref int Target, ref byte[] array, ref byte* scan, byte* Origin)
{
Target = System.BitConverter.ToInt32(array, (int)(scan - Origin));
scan += 4;
}
static unsafe void GetBuffIntAry(ref int[] 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;
}
}
static unsafe void GetBuffByte(ref byte Target, ref byte* scan)
{
byte[] a = new byte[1];
a[0] = *(byte*)(scan);
UnityEngine.Debug.Log(System.BitConverter.ToString(a));
Target = *(byte*)(scan);
scan += 1;
}
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;
}
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