Skip to content

Commit

Permalink
Merge branch 'master' into code-input-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nzbr committed Oct 9, 2020
2 parents 68cb62c + 3328b37 commit 7f0f2c7
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 82 deletions.
36 changes: 11 additions & 25 deletions AmongUsCapture/ClientSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace AmongUsCapture
public class ClientSocket
{
public event EventHandler OnConnected;
public event EventHandler OnDisconnected;

private SocketIO socket;
private string ConnectCode;
Expand All @@ -20,19 +21,17 @@ public void Init()
socket = new SocketIO();

IPCadapter.getInstance().OnToken += OnTokenHandler;
socket.OnConnected += (sender, e) =>
OnConnected += (sender, e) =>
{
//Settings.conInterface.WriteTextFormatted($"[§bClientSocket§f] Connected successfully!");
Settings.form.setColor(MetroColorStyle.Green);
Settings.conInterface.WriteModuleTextColored("ClientSocket", Color.Cyan, "Connected successfully!");
GameMemReader.getInstance().GameStateChanged += GameStateChangedHandler;
GameMemReader.getInstance().PlayerChanged += PlayerChangedHandler;
GameMemReader.getInstance().JoinedLobby += JoinedLobbyHandler;
var OnOnConnected = this.OnConnected;
if (OnOnConnected != null) OnOnConnected(this, new EventArgs());
};

socket.OnDisconnected += (sender, e) =>
OnDisconnected += (sender, e) =>
{
Settings.form.setColor(MetroColorStyle.Red);
//Settings.conInterface.WriteTextFormatted($"[§bClientSocket§f] Lost connection!");
Expand All @@ -43,12 +42,13 @@ public void Init()
};
}

private void OnTokenHandler(object sender, StartToken token)
public void OnTokenHandler(object sender, StartToken token)
{
if (socket.Connected)
{
socket.DisconnectAsync().ContinueWith((t) =>
{
OnDisconnected?.Invoke(this, EventArgs.Empty);
this.Connect(token.Host, token.ConnectCode);
});
} else
Expand All @@ -57,15 +57,15 @@ private void OnTokenHandler(object sender, StartToken token)
}
}

public void Connect(string url, string connectCode)
private void Connect(string url, string connectCode)
{
try
{
socket.ServerUri = new Uri(url);
socket.ConnectAsync().ContinueWith(t =>
{
OnConnected?.Invoke(this, EventArgs.Empty);
SendConnectCode(connectCode);
});
} catch (ArgumentNullException) {
Console.WriteLine("Invalid bot host, not connecting");
Expand All @@ -74,34 +74,20 @@ public void Connect(string url, string connectCode)
}
}

public void SendConnectCode(string connectCode)
{
SendConnectCode(connectCode, null);
}

public void SendConnectCode(string connectCode, EventHandler callback)
public void SendConnectCode(string connectCode, EventHandler callback = null)
{
ConnectCode = connectCode;
socket.EmitAsync("connect", ConnectCode).ContinueWith((_) =>
socket.EmitAsync("connectCode", ConnectCode).ContinueWith((_) =>
{
GameMemReader.getInstance().ForceUpdatePlayers();
GameMemReader.getInstance().ForceTransmitState();
GameMemReader.getInstance().ForceTransmitLobby();
if (callback != null)
{
callback.Invoke(this, new EventArgs());
}
callback?.Invoke(this, new EventArgs());
});
Settings.conInterface.WriteModuleTextColored("ClientSocket", Color.Cyan, $"Connection code ({Color.Red.ToTextColor()}{connectCode}{UserForm.NormalTextColor.ToTextColor()}) sent to server.");
//Program.conInterface.WriteModuleTextColored("GameMemReader", System.Drawing.Color.Aqua, $"Connection code ({connectCode}) sent to server.");
}

public void SendRoomCode(LobbyEventArgs args)
{
socket.EmitAsync("lobby", JsonSerializer.Serialize(args));
Settings.conInterface.WriteModuleTextColored("ClientSocket", Color.Cyan, $"Room code ({Color.Yellow.ToTextColor()}{args.LobbyCode}{UserForm.NormalTextColor.ToTextColor()}) sent to server.");
}

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 All @@ -115,7 +101,7 @@ private void PlayerChangedHandler(object sender, PlayerChangedEventArgs e)
private void JoinedLobbyHandler(object sender, LobbyEventArgs e)
{
socket.EmitAsync("lobby", JsonSerializer.Serialize(e));
Settings.conInterface.WriteModuleTextColored("ClientSocket", Color.Cyan, $"Room code ({Color.Yellow.ToTextColor()}{e.LobbyCode}{UserForm.NormalTextColor.ToTextColor()}) sent to server.");
}

}
}
2 changes: 1 addition & 1 deletion AmongUsCapture/ConsoleTypes/FormConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class FormConsole : ConsoleInterface
public FormConsole(UserForm userForm)
{
form = userForm;
logFile = File.CreateText("CaptureLog.txt");
logFile = File.CreateText(Path.Combine(Directory.GetParent(Program.GetExecutablePath()).FullName, "CaptureLog.txt"));
}

public void WriteColoredText(string ColoredText)
Expand Down
15 changes: 10 additions & 5 deletions AmongUsCapture/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ static class Program
{
const string UriScheme = "aucapture";
const string FriendlyName = "AmongUs Capture";
private static UserForm form;
private static Mutex mutex = null;
private static bool doConsole = true;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (doConsole)
if (Settings.PersistentSettings.debugConsole)
{
AllocConsole(); // needs to be the first call in the program to prevent weird bugs
}
Expand All @@ -41,7 +41,7 @@ static void Main(string[] args)
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ClientSocket socket = new ClientSocket();
var form = new UserForm(socket);
form = new UserForm(socket);
Settings.form = form;
Settings.conInterface = new FormConsole(form); //Create the Form Console interface.
Task.Factory.StartNew(() => socket.Init()).Wait(); // run socket in background. Important to wait for init to have actually finished before continuing
Expand All @@ -61,9 +61,14 @@ private enum URIStartResult
CONTINUE
}

public static string GetExecutablePath()
{
return Process.GetCurrentProcess().MainModule.FileName;
}

private static URIStartResult HandleURIStart(string[] args)
{
Console.WriteLine(Process.GetCurrentProcess().MainModule.FileName);
Console.WriteLine(GetExecutablePath());
const string appName = "AmongUsCapture";
mutex = new Mutex(true, appName, out bool createdNew);
bool wasURIStart = args.Length > 0 && args[0].StartsWith(UriScheme + "://");
Expand Down Expand Up @@ -96,7 +101,7 @@ static void RegisterProtocol() //myAppPath = full path to your application
{
// Replace typeof(App) by the class that contains the Main method or any class located in the project that produces the exe.
// or replace typeof(App).Assembly.Location by anything that gives the full path to the exe
string applicationLocation = Process.GetCurrentProcess().MainModule.FileName;
string applicationLocation = GetExecutablePath();

key.SetValue("", "URL:" + FriendlyName);
key.SetValue("URL Protocol", "");
Expand Down
3 changes: 3 additions & 0 deletions AmongUsCapture/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public interface IPersistentSettings
[Option(Alias = "Host", DefaultValue = "http://localhost:8123")]
string host { get; set; }

[Option(Alias = "DebugConsole", DefaultValue = false)]
bool debugConsole { get; }

}

public interface IGameOffsets
Expand Down
Loading

0 comments on commit 7f0f2c7

Please sign in to comment.