Skip to content

Commit

Permalink
Async socket fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
stenlan committed Sep 19, 2020
1 parent af1a855 commit 4026e67
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 23 deletions.
Binary file modified .vs/AmongUsCapture/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
29 changes: 18 additions & 11 deletions AmongUsCapture/ClientSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,44 @@ namespace AmongUsCapture
class ClientSocket
{
private SocketIO socket;
private string guildID;
private string GuildID;

public async Task Connect(string url, string paramGuildID)
public void Connect(string url, string paramGuildID)
{
guildID = paramGuildID;
GuildID = paramGuildID;
socket = new SocketIO(url);
/*socket.On("hi", response =>
{
string text = response.GetValue<string>();
});*/
socket.OnConnected += async (sender, e) =>
socket.OnConnected += (sender, e) =>
{
await socket.EmitAsync("guildID", guildID);
GameMemReader.getInstance().GameStateChanged += GameStateChangedHandler;
GameMemReader.getInstance().PlayerChanged += PlayerChangedHandler;
UpdateGuildID(paramGuildID, false);
};
await socket.ConnectAsync();

GameMemReader.getInstance().GameStateChanged += GameStateChangedHandler;
GameMemReader.getInstance().PlayerChanged += PlayerChangedHandler;
socket.ConnectAsync();

while(true)
{
string[] command = Console.ReadLine().Split();
if (command.Length > 1 && command[0] == "setid")
{
guildID = command[1];
File.WriteAllText("guildid.txt", guildID);
socket.EmitAsync("guildID", guildID);
UpdateGuildID(command[1]);
}
}
}

private void UpdateGuildID(string newGuildId, bool toFile = true)
{
GuildID = newGuildId;
if (toFile) File.WriteAllText("guildid.txt", GuildID);
socket.EmitAsync("guildID", GuildID).ContinueWith((t) => {
GameMemReader.getInstance().ForceUpdate();
}); ;
}

private void GameStateChangedHandler(object sender, GameStateChangedEventArgs e)
{
socket.EmitAsync("state", JsonSerializer.Serialize(e.NewState)); // could possibly use continueWith() w/ callback if result is needed
Expand Down
65 changes: 53 additions & 12 deletions AmongUsCapture/GameMemReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AmongUsCapture
Expand All @@ -16,6 +17,7 @@ public enum GameState
class GameMemReader
{
private static GameMemReader instance = new GameMemReader();
private bool shouldForceUpdate = false;

public static GameMemReader getInstance()
{
Expand Down Expand Up @@ -49,22 +51,36 @@ public void RunLoop()
{
Console.WriteLine("Connected to Among Us process ({0})", ProcessMemory.process.Id);

int modulesLeft = 2;
foreach (ProcessMemory.Module module in ProcessMemory.modules)
int modulesLeft;

while(true)
{
if (modulesLeft == 0)
break;
else if (module.Name.Equals("GameAssembly.dll", StringComparison.OrdinalIgnoreCase))
modulesLeft = 2;
foreach (ProcessMemory.Module module in ProcessMemory.modules)
{
GameAssemblyPtr = module.BaseAddress;
modulesLeft--;
if (modulesLeft == 0)
break;
else if (module.Name.Equals("GameAssembly.dll", StringComparison.OrdinalIgnoreCase))
{
GameAssemblyPtr = module.BaseAddress;
modulesLeft--;
}
else if (module.Name.Equals("UnityPlayer.dll", StringComparison.OrdinalIgnoreCase))
{
UnityPlayerPtr = module.BaseAddress;
modulesLeft--;
}
}
else if (module.Name.Equals("UnityPlayer.dll", StringComparison.OrdinalIgnoreCase))

if (UnityPlayerPtr == IntPtr.Zero || GameAssemblyPtr == IntPtr.Zero) // either one or both hasn't been found yet
{
Task.Delay(500); // delay and try again
} else
{
UnityPlayerPtr = module.BaseAddress;
modulesLeft--;
break; // we have found all modules
}
}


Console.WriteLine($"({GameAssemblyPtr}) ({UnityPlayerPtr})");
}
Expand Down Expand Up @@ -172,9 +188,28 @@ public void RunLoop()

oldPlayerInfos.Clear();

bool emitAll = false;
if (shouldForceUpdate)
{
shouldForceUpdate = false;
emitAll = true;
}

foreach (KeyValuePair<string, PlayerInfo> kvp in newPlayerInfos) // do this instead of assignment so they don't point to the same object
{
oldPlayerInfos[kvp.Key] = kvp.Value;
PlayerInfo pi = kvp.Value;
oldPlayerInfos[kvp.Key] = pi;
if (emitAll)
{
PlayerChanged.Invoke(this, new PlayerChangedEventArgs()
{
Action = PlayerAction.ForceUpdated,
Name = kvp.Key,
IsDead = pi.GetIsDead(),
Disconnected = pi.GetIsDisconnected(),
Color = pi.GetPlayerColor()
});
}
}

//foreach (KeyValuePair<string, PlayerInfo> kvp in oldPlayerInfos)
Expand All @@ -191,6 +226,11 @@ private static bool ExileEndsGame()
{
return false;
}

public void ForceUpdate()
{
this.shouldForceUpdate = true;
}
}

public class GameStateChangedEventArgs : EventArgs
Expand All @@ -203,7 +243,8 @@ public enum PlayerAction
Joined,
Left,
Died,
ChangedColor
ChangedColor,
ForceUpdated
}

public enum PlayerColor
Expand Down

0 comments on commit 4026e67

Please sign in to comment.