Skip to content

Commit

Permalink
Merge pull request #12 from MyDragonBreath/dev
Browse files Browse the repository at this point in the history
v0.0.6
  • Loading branch information
MyDragonBreath authored Jun 23, 2023
2 parents 4006083 + c345e5b commit d372d20
Show file tree
Hide file tree
Showing 15 changed files with 237 additions and 74 deletions.
15 changes: 7 additions & 8 deletions MCI/InstanceControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace MCI
{
public static class InstanceControl
{
public static Dictionary<int, ClientData> clients = new();
internal static Dictionary<int, ClientData> clients = new();

public static Dictionary<byte, int> PlayerIdClientId = new();
internal static Dictionary<byte, int> PlayerIdClientId = new();

public const int MaxID = 100;

Expand All @@ -30,13 +30,14 @@ public static void SwitchTo(byte playerId)
PlayerControl.LocalPlayer.NetTransform.RpcSnapTo(PlayerControl.LocalPlayer.transform.position);
PlayerControl.LocalPlayer.moveable = false;

Object.Destroy(PlayerControl.LocalPlayer.lightSource);
var light = PlayerControl.LocalPlayer.lightSource;

var newPlayer = Utils.PlayerById(playerId);

HudManager.Instance.KillButton.buttonLabelText.gameObject.SetActive(false);

PlayerControl.LocalPlayer = newPlayer;
PlayerControl.LocalPlayer.lightSource = light;
PlayerControl.LocalPlayer.moveable = true;

AmongUsClient.Instance.ClientId = PlayerControl.LocalPlayer.OwnerId;
Expand All @@ -45,15 +46,13 @@ public static void SwitchTo(byte playerId)
HudManager.Instance.SetHudActive(true);

//hacky "fix" for twix and det

HudManager.Instance.KillButton.transform.parent.GetComponentsInChildren<Transform>().ToList().ForEach((x) => { if (x.gameObject.name == "KillButton(Clone)") Object.Destroy(x.gameObject); });
HudManager.Instance.KillButton.transform.GetComponentsInChildren<Transform>().ToList().ForEach((x) => { if (x.gameObject.name == "KillTimer_TMP(Clone)") Object.Destroy(x.gameObject); });
HudManager.Instance.transform.GetComponentsInChildren<Transform>().ToList().ForEach((x) => { if (x.gameObject.name == "KillButton(Clone)") Object.Destroy(x.gameObject); });

PlayerControl.LocalPlayer.lightSource = Object.Instantiate(PlayerControl.LocalPlayer.LightPrefab);
PlayerControl.LocalPlayer.lightSource.transform.SetParent(PlayerControl.LocalPlayer.transform);
PlayerControl.LocalPlayer.lightSource.transform.localPosition = PlayerControl.LocalPlayer.Collider.offset;
PlayerControl.LocalPlayer.lightSource.Initialize(PlayerControl.LocalPlayer.Collider.offset * 0.5f);
light.transform.SetParent(PlayerControl.LocalPlayer.transform);
light.transform.localPosition = PlayerControl.LocalPlayer.Collider.offset;
Camera.main.GetComponent<FollowerCamera>().SetTarget(PlayerControl.LocalPlayer);
PlayerControl.LocalPlayer.MyPhysics.ResetMoveState(true);
KillAnimation.SetMovement(PlayerControl.LocalPlayer, true);
Expand Down
8 changes: 4 additions & 4 deletions MCI/MCI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

<PropertyGroup>
<GamePlatform Condition="'$(GamePlatform)' == ''">Steam</GamePlatform>
<GameVersion Condition="'$(GamePlatform)' == 'Steam'">2022.12.14</GameVersion>
<GameVersion Condition="'$(GamePlatform)' == 'Itch'">2022.12.14</GameVersion>
<GameVersion Condition="'$(GamePlatform)' == 'Steam'">2023.6.13</GameVersion>
<GameVersion Condition="'$(GamePlatform)' == 'Itch'">2023.6.13</GameVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BepInEx.Unity.IL2CPP" Version="6.0.0-be.664" />
<PackageReference Include="BepInEx.Unity.IL2CPP" Version="6.0.0-be.670" />
<PackageReference Include="AmongUs.GameLibs.$(GamePlatform)" Version="$(GameVersion)" PrivateAssets="all" />

<PackageReference Include="BepInEx.AutoPlugin" Version="1.1.0" PrivateAssets="all" />
<PackageReference Include="BepInEx.IL2CPP.MSBuild" Version="2.0.1" PrivateAssets="all" />

<PackageReference Include="System.Text.Json" Version="5.0.2" PrivateAssets="all" />
<PackageReference Include="System.Text.Json" Version="6.0.7" PrivateAssets="all" />
</ItemGroup>

<Target Name="Copy" AfterTargets="Build" Condition="'$(AmongUs)' != ''">
Expand Down
24 changes: 17 additions & 7 deletions MCI/MCIPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,28 @@ namespace MCI
{
[BepInAutoPlugin("dragonbreath.au.mci", "MCI", VersionString)]
[BepInProcess("Among Us.exe")]
[BepInDependency(SubmergedCompatibility.SUBMERGED_GUID, BepInDependency.DependencyFlags.SoftDependency)]
public partial class MCIPlugin : BasePlugin
{
public const string VersionString = "0.0.5";
public static System.Version vVersion = new(VersionString);
public const string VersionString = "0.0.6";
internal static Version vVersion = new(VersionString);
public Harmony Harmony { get; } = new(Id);

public static MCIPlugin Singleton { get; private set; } = null;

public static string RobotName { get; set; } = "Bot";

public static bool Enabled { get; set; } = true;
public static bool IKnowWhatImDoing { get; set; } = false;
public override void Load()
{
if (Singleton != null) return;
Singleton = this;

Harmony.PatchAll();
UpdateChecker.checkForUpdate();
UpdateChecker.CheckForUpdate();

SubmergedCompatibility.Initialize();

SceneManager.add_sceneLoaded((Action<Scene, LoadSceneMode>)((scene, _) =>
{
Expand All @@ -27,12 +40,9 @@ public override void Load()
}));
}


public static bool Persistence = true;

internal static bool Persistence = true;
}


[HarmonyPatch(typeof(GameStartManager), nameof(GameStartManager.Update))]
public static class CountdownPatch
{
Expand Down
8 changes: 5 additions & 3 deletions MCI/Patches/AirshipSpawn.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using HarmonyLib;
using System;
using UnityEngine;

namespace MCI.Patches
{
Expand All @@ -12,10 +12,12 @@ public sealed class AirshipSpawn

public static void Postfix(SpawnInMinigame __instance)
{
if (!MCIPlugin.Enabled) return;
foreach (var player in PlayerControl.AllPlayerControls)
{
if (!player.Data.PlayerName.Contains("Robot")) continue;
var rand = new Random().Next(0, __instance.Locations.Count);
if (!player.Data.PlayerName.Contains(MCIPlugin.RobotName)) continue;
var rand = Random.Range(0, __instance.Locations.Count);

player.gameObject.SetActive(true);
player.NetTransform.RpcSnapTo(__instance.Locations[rand].Location);
}
Expand Down
10 changes: 8 additions & 2 deletions MCI/Patches/KeyboardJoystick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ public sealed class Keyboard_Joystick

public static void Postfix()
{
if (!MCIPlugin.Enabled) return;
if (Input.GetKeyDown(KeyCode.F5))
{
controllingFigure = PlayerControl.LocalPlayer.PlayerId;
if (PlayerControl.AllPlayerControls.Count == 15 && !Input.GetKeyDown(KeyCode.F6)) return; //press f6 and f5 to bypass limit
Utils.CleanUpLoad();
Utils.CreatePlayerInstance("Robot");
Utils.CreatePlayerInstance(MCIPlugin.RobotName);
}

if (Input.GetKeyDown(KeyCode.F9))
Expand All @@ -32,7 +33,12 @@ public static void Postfix()
InstanceControl.SwitchTo((byte)controllingFigure);
}

if (Input.GetKeyDown(KeyCode.F6))
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.F6))
{
MCIPlugin.IKnowWhatImDoing = !MCIPlugin.IKnowWhatImDoing;
Utils.UpdateNames(MCIPlugin.RobotName);
}
else if (Input.GetKeyDown(KeyCode.F6))
{
MCIPlugin.Persistence = !MCIPlugin.Persistence;
}
Expand Down
21 changes: 15 additions & 6 deletions MCI/Patches/LocalOnly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@ public sealed class LocalOnly
{
public static void Postfix()
{
Object.Destroy(GameObject.Find("HowToPlayButton"));
Object.Destroy(GameObject.Find("PlayOnlineButton"));
Object.Destroy(GameObject.Find("FreePlayButton"));
GameObject.Find("PlayLocalButton").transform.localPosition = new Vector3(0, -1f, 0);

var inf = new GameObject("Info");
inf.transform.position = new Vector3(0, -1.75f, 0);
var tmp = inf.AddComponent<TMPro.TextMeshPro>();
tmp.alignment = TMPro.TextAlignmentOptions.Center;
tmp.horizontalAlignment = TMPro.HorizontalAlignmentOptions.Center;
tmp.text = "MCI only supports localhosted lobbies.";
tmp.color = Color.red;
tmp.fontSize = 3.25f;
tmp.fontSize = 2.25f;

var pos = inf.AddComponent<AspectPosition>();
pos.Alignment = AspectPosition.EdgeAlignments.RightBottom;
pos.DistanceFromEdge = new Vector3(3.2f, 1, 200);
pos.AdjustPosition();
}
}

[HarmonyPatch(typeof(LobbyBehaviour), nameof(LobbyBehaviour.Start))]
public sealed class LobbyCheck
{
public static void Postfix()
{
MCIPlugin.Enabled = AmongUsClient.Instance.NetworkMode == NetworkModes.LocalGame;
}
}
}
1 change: 1 addition & 0 deletions MCI/Patches/OnGameStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public sealed class OnGameStart

public static void Postfix(AmongUsClient __instance)
{
if (!MCIPlugin.Enabled) return;
foreach (var p in __instance.allClients)
{
p.IsReady = true;
Expand Down
3 changes: 2 additions & 1 deletion MCI/Patches/OnLobbyStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ public sealed class OnLobbyStart

public static void Postfix()
{
if (!MCIPlugin.Enabled) return;
if (MCIPlugin.Persistence && InstanceControl.clients.Count != 0)
{
int count = InstanceControl.clients.Count;
InstanceControl.clients.Clear();
InstanceControl.PlayerIdClientId.Clear();
for (int i = 0; i < count; i++)
{
Utils.CreatePlayerInstance("Robot");
Utils.CreatePlayerInstance("Bot");
}
}
}
Expand Down
1 change: 1 addition & 0 deletions MCI/Patches/PatchMeetingHud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public sealed class SameVoteAll
{
public static void Postfix(MeetingHud __instance, ref byte suspectStateIdx)
{
if (!MCIPlugin.Enabled) return;
foreach (PlayerControl player in PlayerControl.AllPlayerControls)
{
__instance.CmdCastVote(player.PlayerId, suspectStateIdx);
Expand Down
19 changes: 17 additions & 2 deletions MCI/Patches/PingTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,23 @@ public static void Postfix(PingTracker __instance)
position.AdjustPosition();
__instance.text.text +=
"\n<color=#ff6700FF>MCI v" + MCIPlugin.VersionString + "</color>";
__instance.text.text += (MCIPlugin.Persistence) ?
" <color=#00ff00FF>[✓]</color>": " <color=#ff0000FF>[X]</color>";
__instance.text.text += MCIPlugin.Enabled ? " <color=#00ff00FF>[Active]</color>" : " <color=#ff0000FF>[InActive]</color>";

if (MCIPlugin.Enabled)
{
if (!MCIPlugin.IKnowWhatImDoing)
{
__instance.text.text += MCIPlugin.Persistence ?
" <color=#00ff00FF>[✓]</color>" : " <color=#ff0000FF>[X]</color>";
}
else
{
__instance.text.text += MCIPlugin.Persistence ?
" <color=#00ff00FF>[<color=#ccaa00FF>✓</color>]</color>" : " <color=#ff0000FF>[<color=#ccaa00FF>X</color>]</color>";
}

__instance.text.text += (SubmergedCompatibility.Loaded && GameOptionsManager.Instance.currentNormalGameOptions.MapId == 5) ? " <color=#00ccccFF>[Submerged]</color>" : " ";
}
if (UpdateChecker.needsUpdate) __instance.text.text += " - <color=#ff0000FF>UPDATE AVAILABLE</color>";
__instance.text.text +=
"\n by MyDragonBreath, whichTwix";
Expand Down
87 changes: 87 additions & 0 deletions MCI/SubmergedCompatibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using BepInEx;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime;
using Il2CppInterop.Runtime.InteropTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

/// <summary>
/// ///
/// https://www.youtube.com/watch?v=-1qju6V1jLM
/// - MDB
///
/// </summary>
namespace MCI
{
public class SubmergedCompatibility
{
public const string SUBMERGED_GUID = "Submerged";
public const ShipStatus.MapType SUBMERGED_MAP_TYPE = (ShipStatus.MapType)5;

public static SemanticVersioning.Version Version { get; private set; }
public static bool Loaded { get; private set; }
public static BasePlugin Plugin { get; private set; }
public static Assembly Assembly { get; private set; }

public static Type[] Types { get; private set; }
public static Dictionary<string, Type> InjectedTypes { get; private set; }

private static Type CustomPlayerData;
private static FieldInfo hasMap;

private static Type SpawnInState;
private static FieldInfo currentState;

public static void Initialize()
{
Loaded = IL2CPPChainloader.Instance.Plugins.TryGetValue(SUBMERGED_GUID, out PluginInfo plugin);
if (!Loaded) return;

Plugin = plugin!.Instance as BasePlugin;
Version = plugin.Metadata.Version;

Assembly = Plugin!.GetType().Assembly;
Types = AccessTools.GetTypesFromAssembly(Assembly);
InjectedTypes = (Dictionary<string, Type>)AccessTools.PropertyGetter(Array.Find(Types, t => t.Name == "ComponentExtensions"), "RegisteredTypes")
.Invoke(null, Array.Empty<object>());

CustomPlayerData = InjectedTypes.Where(t => t.Key == "CustomPlayerData").Select(x => x.Value).First();
hasMap = AccessTools.Field(CustomPlayerData, "hasMap");

SpawnInState = Types.First(t => t.Name == "SpawnInState");

var subSpawnSystem = Types.First(t => t.Name == "SubmarineSpawnInSystem");
var GetReadyPlayerAmount = AccessTools.Method(subSpawnSystem, "GetReadyPlayerAmount");
currentState = AccessTools.Field(subSpawnSystem, "currentState");

MCIPlugin.Singleton.Harmony.Patch(GetReadyPlayerAmount, new HarmonyMethod(AccessTools.Method(typeof(SubmergedCompatibility), nameof(ReadyPlayerAmount))));
}

public static bool ReadyPlayerAmount(dynamic __instance, ref int __result)
{
if (!Loaded) return true;
if (MCIPlugin.Enabled)
{
__result = __instance.GetTotalPlayerAmount();
Enum.TryParse(SpawnInState, "Done", true, out object e);
currentState.SetValue(__instance, e);
return false;
}
return true;
}

public static void ImpartSub(PlayerControl bot)
{
var comp = TryCast(bot.gameObject.AddComponent(Il2CppType.From(CustomPlayerData)), CustomPlayerData);
hasMap.SetValue(comp, true);
}

public static object TryCast(Il2CppObjectBase self, Type type)
{
return AccessTools.Method(self.GetType(), nameof(Il2CppObjectBase.TryCast)).MakeGenericMethod(type).Invoke(self, Array.Empty<object>());
}
}
}
Loading

0 comments on commit d372d20

Please sign in to comment.