Skip to content

Commit

Permalink
1049
Browse files Browse the repository at this point in the history
  • Loading branch information
ZsFabTest committed Sep 2, 2023
1 parent ed26acb commit e30d4fa
Show file tree
Hide file tree
Showing 34 changed files with 1,742 additions and 2,198 deletions.
53 changes: 53 additions & 0 deletions Nebula/CrowdedMod/Components/AbstractPagingBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using UnityEngine;

namespace CrowdedMod.Components;

// Interface until unhollower implements generic il2cpp (if it's possible)
/// <summary>
/// This class is not actually abstract because unhollower does not support it <br/>
/// You need to implement <see cref="OnPageChanged"/> and <see cref="MaxPageIndex"/>
/// </summary>
public class AbstractPagingBehaviour : MonoBehaviour
{
public AbstractPagingBehaviour(IntPtr ptr) : base(ptr)
{
}

private int _page;

public virtual int MaxPerPage => 15;
// public virtual IEnumerable<T> Targets { get; }

public virtual int PageIndex
{
get => _page;
set
{
_page = value;
OnPageChanged();
}
}

public virtual int MaxPageIndex => throw new NotImplementedException();
// public virtual int MaxPages => Targets.Count() / MaxPerPage;

public virtual void OnPageChanged() => throw new NotImplementedException();

public void Start()
{
OnPageChanged();
}

public virtual void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow) || Input.mouseScrollDelta.y > 0f)
{
PageIndex = Mathf.Clamp(PageIndex - 1, 0, MaxPageIndex);
}
else if (Input.GetKeyDown(KeyCode.DownArrow) || Input.mouseScrollDelta.y < 0f)
{
PageIndex = Mathf.Clamp(PageIndex + 1, 0, MaxPageIndex);
}
}
}
58 changes: 58 additions & 0 deletions Nebula/CrowdedMod/Components/MeetingHudPagingBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Il2CppInterop.Runtime.Attributes;
using Reactor.Utilities.Attributes;
using UnityEngine;

namespace CrowdedMod.Components;

[RegisterInIl2Cpp]
public class MeetingHudPagingBehaviour : AbstractPagingBehaviour
{
public MeetingHudPagingBehaviour(IntPtr ptr) : base(ptr)
{
}

internal MeetingHud meetingHud = null!;

[HideFromIl2Cpp]
public IEnumerable<PlayerVoteArea> Targets => meetingHud.playerStates.OrderBy(p => p.AmDead);
public override int MaxPageIndex => (Targets.Count() - 1) / MaxPerPage;

public override void Update()
{
base.Update();

if (meetingHud.state is MeetingHud.VoteStates.Animating or MeetingHud.VoteStates.Proceeding)
{
return; // TimerText does not update there
}

meetingHud.TimerText.text += $" ({PageIndex + 1}/{MaxPageIndex + 1})";
}

public override void OnPageChanged()
{
var i = 0;

foreach (var button in Targets) {
if (i >= PageIndex * MaxPerPage && i < (PageIndex + 1) * MaxPerPage) {
button.gameObject.SetActive(true);

var relativeIndex = i % MaxPerPage;
var row = relativeIndex / 3;
var buttonTransform = button.transform;
buttonTransform.localPosition = meetingHud.VoteOrigin +
new Vector3(
meetingHud.VoteButtonOffsets.x * (relativeIndex % 3),
meetingHud.VoteButtonOffsets.y * row,
buttonTransform.localPosition.z
);
} else {
button.gameObject.SetActive(false);
}
i++;
}
}
}
47 changes: 47 additions & 0 deletions Nebula/CrowdedMod/Components/ShapeShifterPagingBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Il2CppInterop.Runtime.Attributes;
using Reactor.Utilities.Attributes;
using UnityEngine;

namespace CrowdedMod.Components;

[RegisterInIl2Cpp]
public class ShapeShifterPagingBehaviour : AbstractPagingBehaviour
{
public ShapeShifterPagingBehaviour(IntPtr ptr) : base(ptr)
{
}

public ShapeshifterMinigame shapeshifterMinigame = null!;
[HideFromIl2Cpp]
public IEnumerable<ShapeshifterPanel> Targets => shapeshifterMinigame.potentialVictims.ToArray();

public override int MaxPageIndex => (Targets.Count() - 1) / MaxPerPage;

public override void OnPageChanged()
{
var i = 0;

foreach (var panel in Targets)
{
if (i >= PageIndex * MaxPerPage && i < (PageIndex + 1) * MaxPerPage) {
panel.gameObject.SetActive(true);

var relativeIndex = i % MaxPerPage;
var row = relativeIndex / 3;
var buttonTransform = panel.transform;
buttonTransform.localPosition = new Vector3(
shapeshifterMinigame.XStart + shapeshifterMinigame.XOffset * (relativeIndex % 3),
shapeshifterMinigame.YStart + shapeshifterMinigame.YOffset * row,
buttonTransform.localPosition.z
);
} else {
panel.gameObject.SetActive(false);
}

i++;
}
}
}
160 changes: 160 additions & 0 deletions Nebula/CrowdedMod/Patches/CreateGameOptionsPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
using System;
using AmongUs.GameOptions;
using HarmonyLib;
using Reactor.Utilities.Extensions;
using TMPro;
using UnityEngine;

namespace CrowdedMod.Patches
{
internal static class CreateGameOptionsPatches
{
[HarmonyPatch(typeof(CreateOptionsPicker), nameof(CreateOptionsPicker.Awake))]
public static class CreateOptionsPicker_Awake
{
public static void Postfix(CreateOptionsPicker __instance)
{
if (__instance.mode != SettingsMode.Host) return;

{
var firstButtonRenderer = __instance.MaxPlayerButtons[0];
firstButtonRenderer.GetComponentInChildren<TextMeshPro>().text = "-";
firstButtonRenderer.enabled = false;

var firstButtonButton = firstButtonRenderer.GetComponent<PassiveButton>();
firstButtonButton.OnClick.RemoveAllListeners();
firstButtonButton.OnClick.AddListener((Action)(() =>
{
for (var i = 1; i < 11; i++)
{
var playerButton = __instance.MaxPlayerButtons[i];
var tmp = playerButton.GetComponentInChildren<TextMeshPro>();
var newValue = Mathf.Max(byte.Parse(tmp.text) - 10, byte.Parse(playerButton.name));
tmp.text = newValue.ToString();
}
__instance.UpdateMaxPlayersButtons(__instance.GetTargetOptions());
}));
firstButtonRenderer.Destroy();

var lastButtonRenderer = __instance.MaxPlayerButtons[^1];
lastButtonRenderer.GetComponentInChildren<TextMeshPro>().text = "+";
lastButtonRenderer.enabled = false;

var lastButtonButton = lastButtonRenderer.GetComponent<PassiveButton>();
lastButtonButton.OnClick.RemoveAllListeners();
lastButtonButton.OnClick.AddListener((Action)(() =>
{
for (var i = 1; i < 11; i++)
{
var playerButton = __instance.MaxPlayerButtons[i];
var tmp = playerButton.GetComponentInChildren<TextMeshPro>();
var newValue = Mathf.Min(byte.Parse(tmp.text) + 10,
127 - 14 + byte.Parse(playerButton.name));
tmp.text = newValue.ToString();
}
__instance.UpdateMaxPlayersButtons(__instance.GetTargetOptions());
}));
lastButtonRenderer.Destroy();

for (var i = 1; i < 11; i++)
{
var playerButton = __instance.MaxPlayerButtons[i].GetComponent<PassiveButton>();
var text = playerButton.GetComponentInChildren<TextMeshPro>();

playerButton.OnClick.RemoveAllListeners();
playerButton.OnClick.AddListener((Action)(() =>
{
var maxPlayers = byte.Parse(text.text);
var maxImp = Mathf.Min(__instance.GetTargetOptions().NumImpostors, maxPlayers / 2);
__instance.GetTargetOptions().SetInt(Int32OptionNames.NumImpostors, maxImp);
__instance.ImpostorButtons[1].TextMesh.text = maxImp.ToString();
__instance.SetMaxPlayersButtons(maxPlayers);
}));
}

foreach (var button in __instance.MaxPlayerButtons)
{
button.enabled =
button.GetComponentInChildren<TextMeshPro>().text == __instance.GetTargetOptions().MaxPlayers.ToString();
}
}

{
var secondButton = __instance.ImpostorButtons[1];
secondButton.SpriteRenderer.enabled = false;
secondButton.transform.FindChild("ConsoleHighlight").gameObject.Destroy();
secondButton.PassiveButton.Destroy();
secondButton.BoxCollider.Destroy();

var secondButtonText = secondButton.TextMesh;
secondButtonText.text = __instance.GetTargetOptions().NumImpostors.ToString();

var firstButton = __instance.ImpostorButtons[0];
firstButton.SpriteRenderer.enabled = false;
firstButton.TextMesh.text = "-";

var firstPassiveButton = firstButton.PassiveButton;
firstPassiveButton.OnClick.RemoveAllListeners();
firstPassiveButton.OnClick.AddListener((Action)(() => {
var newVal = Mathf.Clamp(
byte.Parse(secondButtonText.text) - 1,
1,
__instance.GetTargetOptions().MaxPlayers / 2
);
__instance.SetImpostorButtons(newVal);
secondButtonText.text = newVal.ToString();
}));

var thirdButton = __instance.ImpostorButtons[2];
thirdButton.SpriteRenderer.enabled = false;
thirdButton.TextMesh.text = "+";

var thirdPassiveButton = thirdButton.PassiveButton;
thirdPassiveButton.OnClick.RemoveAllListeners();
thirdPassiveButton.OnClick.AddListener((Action)(() => {
var newVal = Mathf.Clamp(
byte.Parse(secondButtonText.text) + 1,
1,
__instance.GetTargetOptions().MaxPlayers / 2
);
__instance.SetImpostorButtons(newVal);
secondButtonText.text = newVal.ToString();
}));
}
}
}

[HarmonyPatch(typeof(CreateOptionsPicker), nameof(CreateOptionsPicker.UpdateMaxPlayersButtons))]
public static class CreateOptionsPicker_UpdateMaxPlayersButtons
{
public static bool Prefix(CreateOptionsPicker __instance, [HarmonyArgument(0)] IGameOptions opts)
{
if (__instance.CrewArea)
{
__instance.CrewArea.SetCrewSize(opts.MaxPlayers, opts.NumImpostors);
}

var selectedAsString = opts.MaxPlayers.ToString();
for (var i = 1; i < __instance.MaxPlayerButtons.Count - 1; i++)
{
__instance.MaxPlayerButtons[i].enabled = __instance.MaxPlayerButtons[i].GetComponentInChildren<TextMeshPro>().text == selectedAsString;
}

return false;
}
}

[HarmonyPatch(typeof(CreateOptionsPicker), nameof(CreateOptionsPicker.UpdateImpostorsButtons))]
public static class CreateOptionsPicker_UpdateImpostorsButtons
{
public static bool Prefix()
{
return false;
}
}
}
}
Loading

0 comments on commit e30d4fa

Please sign in to comment.