Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CP1252 encoding for CAOS injection instead of UTF8 #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ obj
packages
.vs
.suo
.idea/.idea.CAOS
5 changes: 3 additions & 2 deletions CAOS/CaosInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class CaosInjector
private EventWaitHandle ResultEventHandle;
private EventWaitHandle RequestRventHandle;
private string GameName;
private static readonly Encoding Encoder = Encoding.GetEncoding(1252);

public CaosInjector(string gameName)
{
Expand Down Expand Up @@ -113,7 +114,7 @@ public CaosResult ExecuteCaos(string CaosAsString, string Action = "execute")
{
//Need more exception checking here - JG
InitInjector();
byte[] CaosBytes = Encoding.UTF8.GetBytes(Action + "\n" + CaosAsString + "\n");
byte[] CaosBytes = Encoder.GetBytes(Action + "\n" + CaosAsString + "\n");
int BufferPosition = 24;
Mutex.WaitOne(1000);
foreach (byte Byte in CaosBytes)
Expand Down Expand Up @@ -142,7 +143,7 @@ public CaosResult ExecuteCaos(string CaosAsString, string Action = "execute")
Mutex.ReleaseMutex();
CloseInjector();
Thread.Sleep(50);
return new CaosResult(ResultCode, Encoding.UTF8.GetString(ResultBytes), ProcessID);
return new CaosResult(ResultCode, Encoder.GetString(ResultBytes), ProcessID);
}

public int ProcessID()
Expand Down
33 changes: 33 additions & 0 deletions Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static void Main(string[] args)
{
TryCatchStrategy(injector);
TryReturnBoolStrategy(injector);
TryCp1252Encoding(injector);
}
else
{
Expand Down Expand Up @@ -64,5 +65,37 @@ private static void TryReturnBoolStrategy(CaosInjector injector)
Console.WriteLine("Execution failed. Game may have exited.");
}
}

private static void TryCp1252Encoding(CaosInjector injector)
{
CaosResult result;
var testString = "bonjour, ça va? très bien!";
if (injector.TryExecuteCaos($"outs \"{testString}\"", out result))
{
if (result.Success)
{
var stringWithoutTerminator = result.Content.Substring(0, result.Content.Length - 1);
if (stringWithoutTerminator == testString)
{
Console.WriteLine("CP1252 Encoding was used successfully");
}
else
{
Console.Error.WriteLine(
"CP1252 result was unexpected. Expected: '" + testString +
"'; Found: '" + result.Content + "'");
}
}
else
{
Debug.Assert(result.ResultCode != 0);
Console.WriteLine($"Error Code: {result.ResultCode}");
}
}
else
{
Console.WriteLine("Execution failed. Game may have exited.");
}
}
}
}