private byte[] GetBytes(object data) { if (data == null) { return(new byte[0]); } return(WriteEncoding.GetBytes(GetValue(data))); }
public string POST(string uri, string body, Dictionary <string, object> config) { var request = GetRequest(uri, config); request.Method = "POST"; byte[] bytes = WriteEncoding.GetBytes(body); using (Stream writer = request.GetRequestStream()) { writer.Write(bytes, 0, bytes.Length); } return(GetResponseBody(request)); }
/// <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> /// Load the String.srl Data /// </summary> static void LoadData() { Log("Initializing String Reloads...", true); StartPipe(); var Data = new SRLData3(); try { using (StructReader Reader = new StructReader(TLMap)) { if (Reader.PeekInt() == 0x43424C54) { TLBCParser(Reader); return; } if (Reader.PeekInt() == 0x4C5253) { SRL1Parser(Reader); return; } if (Reader.PeekInt() == 0x324C5253) { SRL2Parser(Reader); return; } if (Reader.PeekInt() != 0x334C5253) { Error("Failed to Initialize - Corrupted Data"); Thread.Sleep(5000); Environment.Exit(2); } Reader.Seek(4, 0); if (Reader.ReadUInt16() > 1) { Error("Unexpected SRL Database Format"); Thread.Sleep(5000); Environment.Exit(2); } Reader.Seek(0, 0); Reader.ReadStruct(ref Data); Log("Initializing Replaces...", true); for (uint i = 0; i < Data.RepOri.LongLength; i++) { AppendArray(ref Replaces, Data.RepOri[i]); AppendArray(ref Replaces, Data.RepTrg[i]); } Log("Processing Char Reloads... 1/3", true); CharRld = new Dictionary <ushort, char>(); for (uint i = 0; i < Data.OriLetters.LongLength; i++) { char cOri = Data.OriLetters[i]; char cPrx = Data.MemoryLetters[i]; if (!CharRld.ContainsKey(cPrx)) { CharRld.Add(cPrx, cOri); AppendArray(ref Replaces, cOri.ToString()); AppendArray(ref Replaces, cPrx.ToString()); Range Range = new Range() { Min = cPrx, Max = cPrx }; if (!Ranges.Contains(Range)) { Ranges.Add(Range); } } } Log("Processing Char Reloads... 2/3", true); UnkRld = new Dictionary <ushort, char>(); for (uint i = 0; i < Data.UnkChars.LongLength; i++) { ushort c = Data.UnkChars[i]; if (!UnkRld.ContainsKey(c)) { UnkRld.Add(c, Data.UnkReps[i]); } } if (AutoUnks) { Log("Processing Char Reloads... 3/3", true); for (uint i = 0; i < Data.OriLetters.LongLength; i++) { char Char = Data.MemoryLetters[i]; char OChar = Data.OriLetters[i]; byte[] Buffer = WriteEncoding.GetBytes(Char.ToString()); if (BitConverter.IsLittleEndian) { Buffer = Buffer.Reverse().ToArray(); } if (Buffer.Length > 2) { Warning("Failed to generate Auto Unk Char to the char {0}", Char); continue; } byte[] DW = new byte[2]; Buffer.CopyTo(DW, 0); ushort Unk = BitConverter.ToUInt16(DW, 0); #if DEBUG Log("Unk Added: 0x{0:X4} from {1}", true, Unk, OChar); #endif if (UnkRld.ContainsKey(Unk)) { continue; } UnkRld.Add(Unk, OChar); DW[1] = 0xFF; Unk = BitConverter.ToUInt16(DW, 0); #if DEBUG Log("Unk Added: 0x{0:X4} from {1}", true, Unk, OChar); #endif if (UnkRld.ContainsKey(Unk)) { continue; } UnkRld.Add(Unk, OChar); } } Log("Chars Reloads Initialized, Total entries: {0} + {1}", true, UnkRld.Count, CharRld.Count); Log("Processing String Reloads...", true); List <string> Temp = new List <string>(); StrRld = CreateDictionary(); long ReloadEntries = 0, MaskEntries = 0; foreach (SRLDatabase2 Database in Data.Databases) { for (uint i = 0; i < Database.Original.LongLength; i++) { Application.DoEvents(); string str = SimplfyMatch(Database.Original[i]); if (AllowDuplicates || !ContainsKey(str, true)) { if (IsMask(Database.Original[i])) { if (LiteralMaskMatch) { AddEntry(str, ReplaceChars(Database.Replace[i])); ReloadEntries++; } if (Database.Replace[i].StartsWith(AntiMaskParser)) { Database.Replace[i] = Database.Replace[i].Substring(AntiMaskParser.Length, Database.Replace[i].Length - AntiMaskParser.Length); } else { //Prevent Duplicates if (!Temp.Contains(Database.Original[i])) { Temp.Add(Database.Original[i]); } else { continue; } AddMask(Database.Original[i], ReplaceChars(Database.Replace[i])); MaskEntries++; continue; } } else { AddEntry(str, ReplaceChars(Database.Replace[i])); ReloadEntries++; } } } if (MultipleDatabases) { FinishDatabase(); } } Log("String Reloads Initialized, {0} Databases Created, {1} Reload Entries, {2} Mask Entries", true, Databases.Count, ReloadEntries, MaskEntries); Log("Registring Databases Name...", true); DBNames = new Dictionary <long, string>(); for (long i = 0; i < Data.Databases.Length; i++) { DBNames[i] = Data.Databases[i].Name; Log("Database ID: {0} Named As: {1}", true, i, DBNames[i]); } if (Data.Version > 0) { Log("Loading Intros...", true); SRLIntro Intros = new SRLIntro(); Reader.ReadStruct(ref Intros); Introduction = Intros.Intros; } else { Warning("The SRL Data is outdated, try rebuild it."); } Reader.Close(); } Log("Loading Complete.", true); } catch (Exception ex) { Error("Failed to Execute: {0}\n=========\n{1}", false, ex.Message, ex.StackTrace); Thread.Sleep(5000); Environment.Exit(2); } }