Skip to content

Commit

Permalink
Small fixes + chat message events
Browse files Browse the repository at this point in the history
  • Loading branch information
stenlan committed Sep 24, 2020
1 parent 35c2652 commit 9388fed
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 10 deletions.
77 changes: 68 additions & 9 deletions AmongUsCapture/GameMemReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ public static GameMemReader getInstance()

public event EventHandler<PlayerChangedEventArgs> PlayerChanged;

public event EventHandler<ChatMessageEventArgs> ChatMessageAdded;


public Dictionary<string, PlayerInfo> oldPlayerInfos = new Dictionary<string, PlayerInfo>(10); // Important: this is making the assumption that player names are unique. They are, but for better tracking of players and to eliminate any ambiguity the keys of this probably need to be the players' network IDs instead
public Dictionary<string, PlayerInfo> newPlayerInfos = new Dictionary<string, PlayerInfo>(10); // container for new player infos. Also has capacity 10 already assigned so no internal resizing of the data structure is needed

private IntPtr GameAssemblyPtr = IntPtr.Zero;
private GameState oldState = GameState.LOBBY;
private bool exileCausesEnd = false;

private int prevChatBubsVersion;

public void RunLoop()
{
while (true)
Expand Down Expand Up @@ -77,6 +82,7 @@ public void RunLoop()


Console.WriteLine($"({GameAssemblyPtr})");
prevChatBubsVersion = ProcessMemory.Read<int>(GameAssemblyPtr, 0xD0B25C, 0x5C, 0, 0x28, 0xC, 0x14, 0x10);
}
}

Expand Down Expand Up @@ -155,7 +161,7 @@ public void RunLoop()
if (this.shouldTransmitState)
{
shouldTransmitState = false;
GameStateChanged.Invoke(this, new GameStateChangedEventArgs() { NewState = state });
GameStateChanged?.Invoke(this, new GameStateChangedEventArgs() { NewState = state });
} else if (state != oldState)
{
GameStateChanged?.Invoke(this, new GameStateChangedEventArgs() { NewState = state });
Expand Down Expand Up @@ -190,7 +196,7 @@ public void RunLoop()
PlayerInfo oldPlayerInfo = oldPlayerInfos[playerName];
if (!oldPlayerInfo.GetIsDead() && pi.GetIsDead()) // player just died
{
PlayerChanged.Invoke(this, new PlayerChangedEventArgs()
PlayerChanged?.Invoke(this, new PlayerChangedEventArgs()
{
Action = PlayerAction.Died,
Name = playerName,
Expand All @@ -204,15 +210,15 @@ public void RunLoop()
{
PlayerChanged?.Invoke(this, new PlayerChangedEventArgs()
{
Action = PlayerAction.Disconnected,
Action = PlayerAction.ChangedColor,
Name = playerName,
IsDead = pi.GetIsDead(),
Disconnected = pi.GetIsDisconnected(),
Color = pi.GetPlayerColor()
});
}

if(oldPlayerInfo.GetIsDisconnected() != pi.GetIsDisconnected())
if(!oldPlayerInfo.GetIsDisconnected() && pi.GetIsDisconnected())
{
PlayerChanged?.Invoke(this, new PlayerChangedEventArgs()
{
Expand Down Expand Up @@ -269,11 +275,58 @@ public void RunLoop()
}
}

//foreach (KeyValuePair<string, PlayerInfo> kvp in oldPlayerInfos)
//{
// PlayerInfo pi = kvp.Value;
// Console.WriteLine($"Player ID {pi.PlayerId}; Name: {ProcessMemory.ReadString((IntPtr)pi.PlayerName)}; Color: {pi.ColorId}; Dead: " + ((pi.IsDead > 0) ? "yes" : "no"));
//}
IntPtr chatBubblesPtr = ProcessMemory.Read<IntPtr>(GameAssemblyPtr, 0xD0B25C, 0x5C, 0, 0x28, 0xC, 0x14);
int poolSize = 20; // = ProcessMemory.Read<int>(GameAssemblyPtr, 0xD0B25C, 0x5C, 0, 0x28, 0xC, 0xC)
int numChatBubbles = ProcessMemory.Read<int>(chatBubblesPtr, 0xC);
int chatBubsVersion = ProcessMemory.Read<int>(chatBubblesPtr, 0x10);
IntPtr chatBubblesAddr = ProcessMemory.Read<IntPtr>(chatBubblesPtr, 0x8) + 0x10;
IntPtr[] chatBubblePtrs = ProcessMemory.ReadArray(chatBubblesAddr, numChatBubbles);

int newMsgs = 0;

if (chatBubsVersion > prevChatBubsVersion) // new message has been sent
{
if (chatBubsVersion > poolSize) // increments are twofold (push to and pop from pool)
{
if (prevChatBubsVersion > poolSize)
{
newMsgs = (chatBubsVersion - prevChatBubsVersion) >> 1;
}
else
{
newMsgs = (poolSize - prevChatBubsVersion) + ((chatBubsVersion - poolSize) >> 1);
}
}
else // single increments
{
newMsgs = chatBubsVersion - prevChatBubsVersion;
}
}
else if (chatBubsVersion < prevChatBubsVersion) // reset
{
if (chatBubsVersion > poolSize) // increments are twofold (push to and pop from pool)
{
newMsgs = poolSize + ((chatBubsVersion - poolSize) >> 1);
}
else // single increments
{
newMsgs = chatBubsVersion;
}
}

prevChatBubsVersion = chatBubsVersion;

for (int i = numChatBubbles - newMsgs; i < numChatBubbles; i++)
{
string msgText = ProcessMemory.ReadString(ProcessMemory.Read<IntPtr>(chatBubblePtrs[i], 0x20, 0x28));
if (msgText.Length == 0) continue;
string msgSender = ProcessMemory.ReadString(ProcessMemory.Read<IntPtr>(chatBubblePtrs[i], 0x1C, 0x28));
ChatMessageAdded?.Invoke(this, new ChatMessageEventArgs()
{
Sender = msgSender,
Message = msgText
});
}

Thread.Sleep(250);
}
Expand Down Expand Up @@ -330,4 +383,10 @@ public class PlayerChangedEventArgs : EventArgs
public bool Disconnected { get; set; }
public PlayerColor Color { get; set; }
}

public class ChatMessageEventArgs : EventArgs
{
public string Sender { get; set; }
public string Message { get; set; }
}
}
11 changes: 11 additions & 0 deletions AmongUsCapture/ProcessMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ public static string ReadString(IntPtr address)
return System.Text.Encoding.Unicode.GetString(rawString);
}

public static IntPtr[] ReadArray(IntPtr address, int size)
{
byte[] bytes = Read(address, size * 4);
IntPtr[] ints = new IntPtr[size];
for (int i = 0; i < size; i++)
{
ints[i] = (IntPtr) BitConverter.ToUInt32(bytes, i * 4);
}
return ints;
}

private static byte[] Read(IntPtr address, int numBytes)
{
byte[] buffer = new byte[numBytes];
Expand Down
2 changes: 1 addition & 1 deletion AmongUsCapture/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace AmongUsCapture
{
static class Program
{
private static bool debugGui = true;
private static bool debugGui = false;
public static ConsoleInterface conInterface = null;
/// <summary>
/// The main entry point for the application.
Expand Down
7 changes: 7 additions & 0 deletions AmongUsCapture/UserForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ public UserForm(ClientSocket sock)
InitializeComponent();
GameMemReader.getInstance().GameStateChanged += GameStateChangedHandler;
GameMemReader.getInstance().PlayerChanged += UserForm_PlayerChanged;
GameMemReader.getInstance().ChatMessageAdded += OnChatMessageAdded;
if(DarkTheme())
{
EnableDarkTheme();
}

}

private void OnChatMessageAdded(object sender, ChatMessageEventArgs e)
{
WriteLineToConsole($"[CHAT] {e.Sender}: {e.Message}");
}

private bool DarkTheme()
{
bool is_dark_mode = false;
Expand Down

0 comments on commit 9388fed

Please sign in to comment.