Skip to content

Commit

Permalink
0.2.0-dev.8
Browse files Browse the repository at this point in the history
Added CustomOptionButton.
CustomOptionHeader is now a derived class of CustomOptionButton.
Property CustomOption.LobbyTextScale has been renamed to HudTextScale.
Property CustomOption.LobbyTextScroller has been renamed to HudTextScroller.
Property CustomOption.ClearDefaultLobbyText has been renamed to ClearDefaultHudText.
Added CustomOption.DefaultNameStringFormat, DefaultValueStringFormat and DefaultHudStringFormat.
Property CustomOption.StringFormat has been renamed to ValueStringFormat.
Property CustomOption.ToStringFormat has been renamed to HudStringFormat.
Protected method CustomOption.GameOptionCreated now returns bool, returning false will destroy the object that was created.
Significantly reduced CustomOption RPC size.
CustomOption values sent during RpcSyncSettings will now be sent one by one, immediately.
CustomStringOption.Values now returns a readonly collection.
CustomToggleOption will now also appear in the options menu for players who aren't the host.
Updated environment variables.
Updated OxygenFilter.
Added readme with installation, development and building instructions.
  • Loading branch information
DorCoMaNdO committed Apr 3, 2021
1 parent 0e76484 commit e2cbdb5
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 97 deletions.
7 changes: 4 additions & 3 deletions Essentials/Essentials.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>latest</LangVersion>
<Version>0.2.0-dev.7</Version>
<Version>0.2.0-dev.8</Version>

<Description>Among Us modding essentials</Description>
<Authors>CoMaNdO</Authors>
Expand All @@ -14,7 +14,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>1701;1702;</NoWarn>

<AmongUs>$(AmongUs_2020-12-9)</AmongUs>
<AmongUs>$(AmongUs_2020-12-9s)</AmongUs>
<Mappings>NuclearPowered/Mappings:0.1.3</Mappings>
<GameVersion>2020.12.9s</GameVersion>
</PropertyGroup>
Expand All @@ -24,6 +24,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>1701;1702;</NoWarn>

<AmongUs>$(AmongUs_2021-3-5s)</AmongUs>
<Mappings>NuclearPowered/Mappings:0.2.0</Mappings>
<GameVersion>2021.3.5s</GameVersion>
</PropertyGroup>
Expand All @@ -39,7 +40,7 @@
<ItemGroup>
<Deobfuscate Include="$(AmongUs)\BepInEx\plugins\Reactor-$(GameVersion).dll" />

<PackageReference Include="Reactor.OxygenFilter.MSBuild" Version="0.2.9" />
<PackageReference Include="Reactor.OxygenFilter.MSBuild" Version="0.2.10" />
</ItemGroup>

<Target Name="Copy" AfterTargets="Reobfuscate">
Expand Down
104 changes: 104 additions & 0 deletions Essentials/Options/CustomOption.Button.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using Essentials.Extensions;
using System;

namespace Essentials.Options
{
/// <summary>
/// A derivative of <see cref="CustomOption"/>, handling "buttons" in the options menu.
/// </summary>
public class CustomOptionButton : CustomOption, IToggleOption
{
public override bool SendRpc { get { return false; } }

/// <summary>
/// Adds an option header.
/// </summary>
/// <param name="title">The title of the header</param>
/// <param name="menu">The button will be visible in the lobby options menu</param>
/// <param name="hud">The button title will appear in the HUD (option list) in the lobby</param>
/// <param name="initialValue">The button's initial (client sided) value, can be used to hide/show other options</param>
public CustomOptionButton(string title, bool menu = true, bool hud = false, bool initialValue = false) : base(title, title, false, CustomOptionType.Toggle, initialValue)
{
HudStringFormat = (_, name, _) => name;

MenuVisible = menu;
HudVisible = hud;
}

protected override bool GameOptionCreated(OptionBehaviour o)
{
if (o is ToggleOption toggle)
{
toggle.TitleText.Text = GetFormattedName();

toggle.CheckMark.enabled = toggle.oldValue = false;

toggle.transform.FindChild("CheckBox")?.gameObject?.SetActive(false);

return true;
}
else if (o is StringOption str) // Display options in menu for non-host
{
str.TitleText.Text = GetFormattedName();

str.Value = str.oldValue = 0;

str.ValueText.Text = GetFormattedValue();

return true;
}

return false;
}

/// <summary>
/// Toggles the option value (called when the button is pressed).
/// </summary>
public virtual void Toggle()
{
SetValue(!GetValue());
}

/// <summary>
/// Sets a new value
/// </summary>
/// <param name="value">The new value</param>
public virtual void SetValue(bool value)
{
SetValue(value, true);
}

/// <returns>The boolean-casted default value.</returns>
public virtual bool GetDefaultValue()
{
return GetDefaultValue<bool>();
}

/// <returns>The boolean-casted old value.</returns>
public virtual bool GetOldValue()
{
return GetOldValue<bool>();
}

/// <returns>The boolean-casted current value.</returns>
public virtual bool GetValue()
{
return GetValue<bool>();
}
}

public partial class CustomOption
{
/// <summary>
/// Adds a "button" in the options menu.
/// </summary>
/// <param name="title">The title of the button</param>
/// <param name="menu">The button will be visible in the lobby options menu</param>
/// <param name="hud">The button title will appear in the HUD (option list) in the lobby</param>
/// <param name="initialValue">The button's initial (client sided) value, can be used to hide/show other options</param>
public static CustomOptionButton AddButton(string title, bool menu = true, bool hud = false, bool initialValue = false)
{
return new CustomOptionButton(title, menu, hud, initialValue);
}
}
}
44 changes: 20 additions & 24 deletions Essentials/Options/CustomOption.Header.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
namespace Essentials.Options
{
/// <summary>
/// A derivative of <see cref="CustomOption"/>, handling option headers.
/// A derivative of <see cref="CustomOptionButton"/>, handling option headers.
/// </summary>
public class CustomOptionHeader : CustomOption
public class CustomOptionHeader : CustomOptionButton
{
public override bool SendRpc { get { return false; } }

/// <summary>
/// Adds an option header.
/// </summary>
/// <param name="title">The title of the header</param>
/// <param name="menu">The header will be visible in the lobby options menu</param>
/// <param name="hud">The header will appear in the option list in the lobby</param>
public CustomOptionHeader(string title, bool menu = true, bool hud = true) : base(title, title, false, CustomOptionType.Toggle, false)
/// <param name="hud">The header will appear in the HUD (option list) in the lobby</param>
/// <param name="initialValue">The header's initial (client sided) value, can be used to hide/show other options</param>
public CustomOptionHeader(string title, bool menu = true, bool hud = true, bool initialValue = false) : base(title, menu, hud, initialValue)
{
OnValueChanged += (sender, args) =>
{
args.Cancel = true;
};

ToStringFormat = (_, name, _) => name;

MenuVisible = menu;
HudVisible = hud;
}

protected override void GameOptionCreated(OptionBehaviour o)
protected override bool GameOptionCreated(OptionBehaviour o)
{
if (o is not ToggleOption toggle) return;
if (!base.GameOptionCreated(o)) return false;

toggle.TitleText.Text = GetFormattedName();
if (o is ToggleOption toggle) toggle.transform.FindChild("Background")?.gameObject?.SetActive(false);

toggle.CheckMark.enabled = toggle.oldValue = false;
return true;
}

toggle.transform.FindChild("Background")?.gameObject?.SetActive(false);
toggle.transform.FindChild("CheckBox")?.gameObject?.SetActive(false);
/// <summary>
/// Toggles the option value (called when the header is pressed).
/// </summary>
public override void Toggle()
{
base.Toggle();
}
}

Expand All @@ -46,10 +41,11 @@ public partial class CustomOption
/// </summary>
/// <param name="title">The title of the header</param>
/// <param name="menu">The header will be visible in the lobby options menu</param>
/// <param name="hud">The header will appear in the option list in the lobby</param>
public static CustomOptionHeader AddHeader(string title, bool menu = true, bool hud = true)
/// <param name="hud">The header will appear in the HUD (option list) in the lobby</param>
/// <param name="initialValue">The header's initial (client sided) value, can be used to hide/show other options</param>
public static CustomOptionHeader AddHeader(string title, bool menu = true, bool hud = true, bool initialValue = false)
{
return new CustomOptionHeader(title, menu, hud);
return new CustomOptionHeader(title, menu, hud, initialValue);
}
}
}
8 changes: 5 additions & 3 deletions Essentials/Options/CustomOption.Number.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public CustomNumberOption(string id, string name, bool saveValue, float value, f
ConfigEntry = saveValue ? EssentialsPlugin.Instance.Config.Bind(PluginID, ConfigID, GetDefaultValue()) : null;
SetValue(ConfigEntry == null ? GetDefaultValue() : ConfigEntry.Value, false);

StringFormat = (sender, value) => value.ToString();
ValueStringFormat = (sender, value) => value.ToString();
}

protected override OptionOnValueChangedEventArgs OnValueChangedEventArgs(object value, object oldValue)
Expand All @@ -83,9 +83,9 @@ protected override OptionValueChangedEventArgs ValueChangedEventArgs(object valu
return new NumberOptionValueChangedEventArgs(value, Value);
}

protected override void GameOptionCreated(OptionBehaviour o)
protected override bool GameOptionCreated(OptionBehaviour o)
{
if (o is not NumberOption number) return;
if (o is not NumberOption number) return false;

number.TitleText.Text = GetFormattedName();
number.ValidRange = new FloatRange(Min, Max);
Expand All @@ -96,6 +96,8 @@ protected override void GameOptionCreated(OptionBehaviour o)
number.Value = number.Field_3 = GetValue();
#endif
number.ValueText.Text = GetFormattedValue();

return true;
}

/// <summary>
Expand Down
32 changes: 24 additions & 8 deletions Essentials/Options/CustomOption.Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ private static List<OptionBehaviour> GetGameOptions(float lowestY)

ToggleOption toggle = Object.Instantiate(toggleOption, toggleOption.transform.parent);//.DontDestroy();

option.OnGameOptionCreated(toggle);
if (!option.OnGameOptionCreated(toggle))
{
toggle.Destroy();

continue;
}

options.Add(toggle);

Expand All @@ -61,7 +66,12 @@ private static List<OptionBehaviour> GetGameOptions(float lowestY)

NumberOption number = Object.Instantiate(numberOption, numberOption.transform.parent);//.DontDestroy();

option.OnGameOptionCreated(number);
if (!option.OnGameOptionCreated(number))
{
number.Destroy();

continue;
}

options.Add(number);

Expand Down Expand Up @@ -94,7 +104,12 @@ private static List<OptionBehaviour> GetGameOptions(float lowestY)

StringOption str = Object.Instantiate(stringOption, stringOption.transform.parent);//.DontDestroy();

option.OnGameOptionCreated(str);
if (!option.OnGameOptionCreated(str))
{
str.Destroy();

continue;
}

options.Add(str);

Expand Down Expand Up @@ -166,15 +181,15 @@ private static IEnumerable<MethodBase> TargetMethods()
private static void Postfix(ref string __result)
{
int firstNewline = __result.IndexOf('\n');
StringBuilder sb = new StringBuilder(ClearDefaultLobbyText ? __result.Substring(0, firstNewline + 1) : __result);
StringBuilder sb = new StringBuilder(ClearDefaultHudText ? __result.Substring(0, firstNewline + 1) : __result);

if (ShamelessPlug) sb.AppendLine("[FF1111FF]DorCoMaNdO on GitHub/Twitter/Twitch[]");
foreach (CustomOption option in Options) if (option.HudVisible) sb.AppendLine(option.ToString());

__result = sb.ToString();

string insert = ":";
if (LobbyTextScroller && (HudManager.Instance?.GameSettings?.Height).GetValueOrDefault() + 0.02F > HudPosition.Height) insert = " (Scroll for more):";
if (HudTextScroller && (HudManager.Instance?.GameSettings?.Height).GetValueOrDefault() + 0.02F > HudPosition.Height) insert = " (Scroll for more):";
__result = __result.Insert(firstNewline, insert);

// Remove last newline (for the scroller to not overscroll one line)
Expand Down Expand Up @@ -293,7 +308,8 @@ public static void Postfix()
if (AmongUsClient.Instance?.AmHost != true || PlayerControl.AllPlayerControls.Count < 2 || !PlayerControl.LocalPlayer) return;

//Rpc.Send(Options.Where(o => o.SendRpc).Select(o => ((string, CustomOptionType, object))o).ToArray());
foreach (CustomOption option in Options) if (option.SendRpc) Rpc.Instance.Send(option);
foreach (CustomOption option in Options) if (option.SendRpc) Rpc.Instance.Send(option, true);
//Rpc.Instance.Send(Options.Where(o => o.SendRpc).Select(o => ((int, CustomOptionType, object))o).ToArray());
}
}

Expand All @@ -308,12 +324,12 @@ private static void UpdateScroller(object sender, EventArgs e)

if (hudManager?.GameSettings?.transform == null) return;

hudManager.GameSettings.scale = LobbyTextScale;
hudManager.GameSettings.scale = HudTextScale;

const float XOffset = 0.066666F, YOffset = 0.1F;

// Scroller disabled
if (!LobbyTextScroller)
if (!HudTextScroller)
{
// Remove scroller if disabled late
if (OptionsScroller != null)
Expand Down
Loading

0 comments on commit e2cbdb5

Please sign in to comment.