Skip to content

Commit

Permalink
refactor this one too i guess
Browse files Browse the repository at this point in the history
  • Loading branch information
Govorunb committed Jul 4, 2024
1 parent b9fa70b commit 29c4b0d
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 36 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ resharper_web_config_wrong_module_highlighting = warning
dotnet_diagnostic.RCS1123.severity = none # parentheses in expressions (covered by default dotnet analyzers)
dotnet_diagnostic.RCS1213.severity = none # unused methods (^)
dotnet_diagnostic.RCS1163.severity = none # unused parameter (^)
dotnet_diagnostic.RCS1139.severity = none # missing <summary> on xmldoc (<see cref> is used in places as a way to easily link to random methods)

[*.{appxmanifest,asax,ascx,aspx,axaml,build,cg,cginc,compute,cs,cshtml,dtd,fx,fxh,hlsl,hlsli,hlslinc,master,nuspec,paml,razor,resw,resx,shader,skin,usf,ush,vb,xaml,xamlx,xoml,xsd}]
indent_style = space
Expand Down
6 changes: 6 additions & 0 deletions Immersion/ConsoleCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static string PluginControlCommand(params string[] args)
private static string ProcessCommand(IReadOnlyList<string> args)
{
if (args is not [_, ..]) // shorter to type than { Count: > 0 } ;)
{
return $"""
Usage:
{Command} {SetUsage}
Expand All @@ -40,6 +41,8 @@ private static string ProcessCommand(IReadOnlyList<string> args)
(e.g. `{Command} {ManualSendExample}`)
{Command} {ForcePrioUsage}
""";
}

string subCommand = args[0].ToLower();
return subCommand switch
{
Expand Down Expand Up @@ -114,6 +117,7 @@ private static string EnableDisable(IReadOnlyList<string> args)
private static string SetPronouns(string fullArg)
{
if (!PronounSet.TryParse(fullArg, out PronounSet pronounSet))
{
return """
Could not parse pronouns.
Pronoun sets consist of six parts:
Expand All @@ -127,6 +131,8 @@ Could not parse pronouns.
For others, at least <color=yellow>Subj/Obj/Poss</color> are required.
Example: he/him/his
""";
}

Globals.PlayerPronouns = pronounSet;
return $"Pronouns set to <color=green>{pronounSet}</color>";
}
Expand Down
9 changes: 5 additions & 4 deletions Immersion/Formatting/PronounSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Immersion.Formatting;
/// <param name="IsContraction">Contraction of "X is". E.g. "<b>They're</b> the focus."</param>
/// <param name="HasContraction">Contraction of "X has". E.g. "<b>I've</b> done it."</param>
/// <param name="Reflexive">Reflects back to the subject. E.g. "It seems they've placed <b>themself</b> in a precarious position."</param>
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public readonly record struct PronounSet(string Subject, string Object, string Possessive, string IsContraction, string HasContraction, string Reflexive)
{
#region Predefined
Expand All @@ -33,6 +33,8 @@ public readonly record struct PronounSet(string Subject, string Object, string P
public static bool TryParse(string formatted, out PronounSet pronouns)
{
pronouns = default;
if (string.IsNullOrEmpty(formatted))
return false;
string[] parts = formatted.Split('/');
foreach (PronounSet existing in DefinedSets)
{
Expand All @@ -52,11 +54,10 @@ public static bool TryParse(string formatted, out PronounSet pronouns)
pronouns = existing;
return true;
}
};
}
// these three are the absolute minimum required
if (parts is not [string subject, string @object, string possessive, ..])
// we can't really do much with 2 and below if they're not already defined (caught above) so let's give up
return false;
return false; // we can't really do much with 2 and below if they're not already defined (caught above) so let's give up
(string isContraction, string hasContraction, string reflexive) = parts.Length switch
{
// we're the happiest with a full set
Expand Down
40 changes: 31 additions & 9 deletions Immersion/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,42 @@ namespace Immersion;

public static class Globals
{
private const string _PLAYER_NAME_PLAYERPREFS_KEY = "Immersion_PlayerName";
private const string _BASEURL_PLAYARPREFS_KEY = "Immersion_BaseURL";
private const string _PRONOUNS_PLAYARPREFS_KEY = "Immersion_BaseURL";
private const string PlayerNameKey = $"{nameof(Immersion)}_PlayerName";
private const string BaseUrlKey = $"{nameof(Immersion)}_BaseURL";
private const string PronounsKey = $"{nameof(Immersion)}_Pronouns";

private const string DefaultPlayerName = "Vedal";
private const string DefaultUrl = "http://localhost:8000/subnautica/";
private static readonly PronounSet DefaultPronouns = PronounSet.HeHim;

public static string PlayerName
{
get => PlayerPrefs.GetString(_PLAYER_NAME_PLAYERPREFS_KEY, "Vedal");
set => PlayerPrefs.SetString(_PLAYER_NAME_PLAYERPREFS_KEY, value);
get => PlayerPrefs.GetString(PlayerNameKey, DefaultPlayerName);
set => PlayerPrefs.SetString(PlayerNameKey, value);
}
public static PronounSet PlayerPronouns { get; set; } = PronounSet.HeHim;

public static string BaseUrl
{
get => PlayerPrefs.GetString(_BASEURL_PLAYARPREFS_KEY, "http://localhost:8000/subnautica/");
set => PlayerPrefs.SetString(_BASEURL_PLAYARPREFS_KEY, value);
get => PlayerPrefs.GetString(BaseUrlKey, DefaultUrl);
set => PlayerPrefs.SetString(BaseUrlKey, value);
}

private static PronounSet? _playerPronouns;
public static PronounSet PlayerPronouns
{
get
{
if (_playerPronouns.HasValue)
return _playerPronouns.Value;

if (!PronounSet.TryParse(PlayerPrefs.GetString(PronounsKey, ""), out PronounSet pronouns))
_playerPronouns = DefaultPronouns;
PlayerPronouns = pronouns;
return _playerPronouns.Value;
}
set
{
_playerPronouns = value;
PlayerPrefs.SetString(PronounsKey, value.ToString());
}
}
}
24 changes: 14 additions & 10 deletions Immersion/Patches/CreatureEncounterPatches.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
using Immersion.Trackers;
using Nautilus.Extensions;

namespace Immersion.Patches;

[HarmonyPatch]
public static class CreatureEncounterPatches
{
#nullable enable
private static CreatureEncounters? Encounters => COMPONENT_HOLDER.GetComponent<CreatureEncounters>().Exists();
private static CreatureEncounters Encounters => COMPONENT_HOLDER.GetComponent<CreatureEncounters>();

private static void Notify(TechType techType, MonoBehaviour target)
{
CreatureEncounters comp = Encounters;
if (comp) comp.NotifyCreatureEncounter(techType, target);
}

[HarmonyPatch(typeof(SpikeyTrapAttachTarget), nameof(SpikeyTrapAttachTarget.Attach))]
[HarmonyPostfix]
public static void NotifySpikeyTrapAttack(SpikeyTrapAttachTarget __instance)
{
if (__instance.player != Player.main) return;

Encounters?.NotifyCreatureEncounter(TechType.SpikeyTrap, Player.main);
Notify(TechType.SpikeyTrap, Player.main);
}

[HarmonyPatch(typeof(PlayerLilyPaddlerHypnosis), nameof(PlayerLilyPaddlerHypnosis.StartHypnosis))]
[HarmonyPostfix]
public static void NotifyLilyPaddlerHypnosis()
{
Encounters?.NotifyCreatureEncounter(TechType.LilyPaddler, Player.main);
Notify(TechType.LilyPaddler, Player.main);
}

[HarmonyPatch(typeof(IceWormJumpScareTrigger), nameof(IceWormJumpScareTrigger.InvokeJumpScareEvent))]
Expand All @@ -31,7 +35,7 @@ public static void NotifyIceWormJumpScare(IceWormJumpScareTrigger __instance)
{
if (!__instance.used) return;

Encounters?.NotifyCreatureEncounter(TechType.IceWorm, Player.main);
Notify(TechType.IceWorm, Player.main);
}

private static readonly Dictionary<string, TechType> _cinematics = new() {
Expand All @@ -49,7 +53,7 @@ public static void NotifyIceWormJumpScare(IceWormJumpScareTrigger __instance)
public static void NotifyCinematicAttack(PlayerCinematicController __instance, Player setplayer)
{
if (_cinematics.TryGetValue(__instance.playerViewAnimationName, out TechType techType))
Encounters?.NotifyCreatureEncounter(techType, setplayer);
Notify(techType, setplayer);
}

//[HarmonyPatch(typeof(PlayerCinematicController), nameof(PlayerCinematicController.Start))]
Expand All @@ -66,14 +70,14 @@ public static void NotifyGrabSeatruck(LeviathanMeleeAttack __instance)
// this one can fail so we do a check (and use the field instead of the param)
if (!__instance.heldSeatruck) return;

Encounters?.NotifyCreatureEncounter(__instance.creatureType, __instance.heldSeatruck);
Notify(__instance.creatureType, __instance.heldSeatruck);
}

[HarmonyPatch(typeof(LeviathanMeleeAttack), nameof(LeviathanMeleeAttack.GrabExosuit))]
[HarmonyPostfix]
public static void NotifyGrabSeatruck(LeviathanMeleeAttack __instance, Exosuit exosuit)
public static void NotifyGrabPrawnSuit(LeviathanMeleeAttack __instance, Exosuit exosuit)
{
// can't fail so use param
Encounters?.NotifyCreatureEncounter(__instance.creatureType, exosuit);
Notify(__instance.creatureType, exosuit);
}
}
2 changes: 1 addition & 1 deletion Immersion/Resources/ResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static void InjectAssemblies()
}

private static readonly Assembly _assembly = Assembly.GetExecutingAssembly();
private static readonly Dictionary<string, object> _cache = new();
private static readonly Dictionary<string, object> _cache = [];

public static AssetBundle GetAssetBundle(string name)
{
Expand Down
4 changes: 2 additions & 2 deletions Immersion/Trackers/Backseating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void RerollThresholds()
private List<Bar> bars;

private float _nextUpdate;
private float _updateInterval = 1f;
private float UpdateInterval { get; } = 1f;

protected override void Awake()
{
Expand Down Expand Up @@ -111,7 +111,7 @@ public void FixedUpdate()
float time = Time.fixedTime;
if (time < _nextUpdate) return;

_nextUpdate = time + _updateInterval;
_nextUpdate = time + UpdateInterval;

if (!Player.main) return;
if (!liveMixin) liveMixin = Player.main.liveMixin;
Expand Down
15 changes: 7 additions & 8 deletions Immersion/Trackers/Empathy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public sealed class Empathy : Tracker
];

private float _timeLastNotified;
private static float _penglingCooldown = 5f;
private static float _roadkillCooldown = 60f; // no spam
private static float PenglingCooldown { get; } = 5f;
private static float RoadkillCooldown { get; } = 60f; // no spam

public void OnPenglingPickedUp() => Notify(_penglingMessages, _penglingCooldown);
public void OnRoadkill() => Notify(_roadkillMessages, _roadkillCooldown);
public void OnPenglingPickedUp() => Notify(_penglingMessages, PenglingCooldown);
public void OnRoadkill() => Notify(_roadkillMessages, RoadkillCooldown);

private void Notify(string[] messages, float cooldown)
{
Expand All @@ -33,16 +33,15 @@ private void Notify(string[] messages, float cooldown)

React(Priority.Low, Format.FormatPlayer(messages.GetRandom()));
}
#nullable enable
private static Empathy? Instance => COMPONENT_HOLDER.GetComponent<Empathy>().Exists();
private static Empathy Instance => COMPONENT_HOLDER.GetComponent<Empathy>().Exists();

[HarmonyPatch(typeof(PenguinGroupDefense), nameof(PenguinGroupDefense.AddAggressionToTarget))]
[HarmonyPostfix]
private static void HookAfterPickup(GameObject target)
{
if (target != Player.main.gameObject) return;

Instance?.OnPenglingPickedUp();
if (Instance) Instance.OnPenglingPickedUp();
}

[HarmonyPatch(typeof(LiveMixin), nameof(LiveMixin.TakeDamage))]
Expand All @@ -53,6 +52,6 @@ private static void CheckRoadkill(LiveMixin __instance, DamageType type, GameObj
if (type != DamageType.Collide || !dealer || __instance.IsAlive()) return;
if (!dealer.GetComponent<SeaTruckSegment>() || !__instance.GetComponent<Creature>()) return;

Instance?.OnRoadkill();
if (Instance) Instance.OnRoadkill();
}
}
4 changes: 2 additions & 2 deletions Immersion/Trackers/PlayerFrozen.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Immersion.Formatting;
using Nautilus.Extensions;

namespace Immersion.Trackers;

Expand Down Expand Up @@ -29,7 +28,8 @@ private void FixedUpdate()
{
if (!isFrozen) return;

bool isActuallyFrozen = Player.main.Exists()?.frozenMixin.Exists()?.frozen ?? false;
bool isActuallyFrozen = Player.main && Player.main.frozenMixin
&& Player.main.frozenMixin.frozen;

if (!isActuallyFrozen) OnUnfrozen();
}
Expand Down

0 comments on commit 29c4b0d

Please sign in to comment.