/// <summary> /// Gen a Null Terminated String /// </summary> /// <param name="String">The string</param> /// <returns>The Pointer to the new String</returns> internal static IntPtr GenString(string String, bool ForceUnicode = false, IntPtr?ForcePointer = null) { byte[] buffer; if (ForceUnicode) { buffer = Encoding.Unicode.GetBytes(String + "\x0"); } else { if (EncodingModifier != null) { buffer = EncodingModifier.Call("Modifier", "GenString", String); } else { int len = WriteEncoding.GetByteCount(String + "\x0"); buffer = new byte[len]; WriteEncoding.GetBytes(String, 0, String.Length, buffer, 0); #if LEAKING //Less Memory Leak, but works only with some games IntPtr Pointer = LastGenerated; if (LastGenerated == IntPtr.Zero) { Pointer = Marshal.AllocHGlobal(buffer.Length); } else { if (AllocLen < buffer.Length) { while (AllocLen < buffer.Length) { AllocLen++; } Pointer = Marshal.ReAllocHGlobal(Pointer, new IntPtr(AllocLen)); } } LastGenerated = Pointer #endif } } IntPtr Pointer = ForcePointer ?? Marshal.AllocHGlobal(buffer.Length); Marshal.Copy(buffer, 0, Pointer, buffer.Length); if (LogAll) { string New = GetString(Pointer); Log("Old: {0}\nNew: {1}\nHex: {2}", false, String, New, ParseBytes(buffer)); } return(Pointer); }
/// <summary> /// Detect the correct string reader method, and load it /// </summary> /// <param name="Pointer">The pointer to the string</param> /// <param name="Decode">if False, return the hex of the string</param> /// <returns>The String</returns> internal static string GetString(IntPtr Pointer, bool Decode = true, int?Len = null, int?CP = null) { if (EncodingModifier != null) { return(EncodingModifier.Call("Modifier", "GetString", Pointer, Decode)); } if (Unicode && CP == null && Len == null) { return(GetStringW(Pointer, Decode)); } else { return(GetStringA(Pointer, Decode, CP, Len)); } }