Skip to content

Commit

Permalink
Restructure code
Browse files Browse the repository at this point in the history
  • Loading branch information
VioVayo committed Mar 24, 2023
1 parent b2d4130 commit 161840c
Show file tree
Hide file tree
Showing 21 changed files with 595 additions and 328 deletions.
Binary file modified HugMod/Assets/hug_bundle
Binary file not shown.
4 changes: 2 additions & 2 deletions HugMod/Assets/hug_bundle.manifest
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
ManifestFileVersion: 0
CRC: 2035962915
CRC: 4286573153
Hashes:
AssetFileHash:
serializedVersion: 2
Hash: 6b4888750e29bc0c2f383a16efc70007
Hash: 56a78e3700c5c2ce3fbf9650e89543d0
TypeTreeHash:
serializedVersion: 2
Hash: 81be94c203cbc62109b6f5f6fe206615
Expand Down
71 changes: 43 additions & 28 deletions HugMod/HugComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class HugComponent : MonoBehaviour

//tabled
private ScreenPrompt hugPrompt = new(InputLibrary.interactSecondary, "<CMD> " + "Hug");
private Vector3 focusPoint = new(0, 0.1f, 0);
private Vector3 focusPoint = new(0, 0, 0);
private string hugTrigger = "react_None";
private bool fullbodyReact = true, keepFootAnimRight = false, keepFootAnimLeft = false, keepHandAnimRight = false, keepHandAnimLeft = false;

Expand Down Expand Up @@ -52,16 +52,21 @@ public class HugComponent : MonoBehaviour
public event Action OnDestroyEvent;


private void Awake() { SetHugEnabled(false); }

public void Initialise()
{
var flag = gameObject.activeInHierarchy;
gameObject.SetActive(true);

HugReceiver = gameObject.GetComponentInChildren<InteractReceiver>();
if (HugReceiver == null)
{
var receiverCollider = gameObject.GetComponentInChildren<OWCollider>();
if (receiverCollider == null)
{
HugModInstance.ModHelper.Console.WriteLine($"Couldn't find OWCollider in children of object \"{gameObject.name}\". HugComponent will be disabled.", MessageType.Error);
enabled = false;
SetHugEnabled(false);
return;
}
if (receiverCollider.gameObject.layer == LayerMask.NameToLayer("Ignore Raycast")) receiverCollider.gameObject.layer = LayerMask.NameToLayer("Default");
Expand All @@ -83,7 +88,7 @@ public void Initialise()
}
else
{
HugAnimator = gameObject.GetComponentInChildren<Animator>(true);
HugAnimator = gameObject.GetComponentInChildren<Animator>();
lookSpring = new(50, 14, 1);
}

Expand All @@ -96,13 +101,15 @@ public void Initialise()
}

hugOverrider.runtimeAnimatorController = AltRuntimeController;
SetHugEnabled(true);
gameObject.SetActive(flag);
}

private bool AddTriggerCollider(Collider compareCollider)
{
if (compareCollider == null) return false;

var triggerObj = compareCollider.gameObject.transform.CreateChild("Hug_TriggerCollider");
var triggerObj = compareCollider.gameObject.CreateChild("Hug_TriggerCollider");
triggerObj.layer = LayerMask.NameToLayer("Ignore Raycast");

hugTriggerCollider = triggerObj.AddComponent<HugTriggerCollider>();
Expand All @@ -115,11 +122,10 @@ private bool AddTriggerCollider(Collider compareCollider)
else
{
hugTriggerCollider.Remove();
HugModInstance.ModHelper.Console.WriteLine($"Collider type associated with object \"{gameObject.name}\" is not supported. HugComponent will be disabled.", MessageType.Error);
enabled = false;
HugModInstance.ModHelper.Console.WriteLine($"Collider type is not supported. HugComponent associated with object \"{gameObject.name}\" will be disabled.", MessageType.Error);
SetHugEnabled(false);
return false;
}

return true;
}

Expand Down Expand Up @@ -169,7 +175,7 @@ public void SetHugEnabled(bool enable)
public void SetLookAtPlayerEnabled(bool enable)
{
if (hugIK != null) hugIK.enabled = enable;
else HugModInstance.ModHelper.Console.WriteLine($"Couldn't find HugIK Component on object \"{HugAnimator.gameObject.name}\".", MessageType.Error);
else HugModInstance.ModHelper.Console.WriteLine($"Couldn't find HugIK Component associated with object \"{gameObject.name}\".", MessageType.Error);
}
public void ForceLookAtPlayer(bool enable) { forceLookAtPlayer = enable; }
public void SetPrompt(string name) { hugPrompt = new(InputLibrary.interactSecondary, "<CMD> " + "Hug " + name); }
Expand Down Expand Up @@ -207,8 +213,8 @@ public void GetUnderlayTransitionData(out float transitionTime, out int transiti
transitionClip = this.transitionClip;
}

public bool IsSequenceInProgress() { return sequenceInProgress; }
public bool IsAnimatorControllerSwapped() { return HugAnimator.runtimeAnimatorController == hugOverrider; }
public bool IsSequenceInProgress() => sequenceInProgress;
public bool IsAnimatorControllerSwapped() => (HugAnimator != null) && (HugAnimator.runtimeAnimatorController == hugOverrider);

public void ChangeInteractReceiver(InteractReceiver newReceiver, bool resetTriggerCollider = false)
{
Expand All @@ -218,6 +224,7 @@ public void ChangeInteractReceiver(InteractReceiver newReceiver, bool resetTrigg
HugReceiver.OnLoseFocus -= DisableHug;
}
HugReceiver = newReceiver;
if (newReceiver == null) return;
newReceiver.OnGainFocus += EnableHug;
newReceiver.OnLoseFocus += DisableHug;

Expand All @@ -226,34 +233,34 @@ public void ChangeInteractReceiver(InteractReceiver newReceiver, bool resetTrigg
}
public void ChangeTriggerCollider(Collider newCompareCollider)
{
if (hugTriggerCollider != null) hugTriggerCollider.Remove();
hugTriggerCollider.Exists()?.Remove();
AddTriggerCollider(newCompareCollider);
}
public void ChangePrimaryAnimator(Animator newAnimator, RuntimeAnimatorController newAnimatorController)
public void ChangePrimaryAnimator(Animator newAnimator, bool resetAnimatorController = true)
{
HugAnimator = newAnimator;
initialRuntimeController = newAnimatorController;
isAnimated = newAnimator != null && newAnimator.avatar.isHuman;
if (resetAnimatorController && isAnimated) initialRuntimeController = newAnimator.runtimeAnimatorController;

if ((hugIK == null && !isAnimated) || (hugIK != null && isAnimated && hugIK.gameObject == newAnimator.gameObject)) return;
if (hugIK != null) hugIK.Remove();
if (newAnimator == null) return;
hugIK = newAnimator.gameObject.AddComponent<HugIK>();
hugIK.SetHugComponent(this);
hugIK.Exists()?.Remove();
hugIK = newAnimator.Exists()?.gameObject.AddComponent<HugIK>();
hugIK.Exists()?.SetHugComponent(this);
}
public void ChangeAnimatorController(RuntimeAnimatorController newAnimatorController) { initialRuntimeController = newAnimatorController; }
public void ChangeSecondaryAnimator(Animator newAnimator) { SecondaryAnimator = newAnimator; }
public void ChangeCharacterAnimController(CharacterAnimController newAnimController)
public void ChangeCharacterAnimController(CharacterAnimController newCharacterAnimController)
{
characterAnimController = newAnimController;
lookSpring = newAnimController.lookSpring;
characterAnimController = newCharacterAnimController;
lookSpring = newCharacterAnimController.Exists()?.lookSpring ?? new(50, 14, 1);
}


//Hug stuff
private void EnableHug()
{
if (!enabled) return;
Locator.GetPromptManager().AddScreenPrompt(hugPrompt, PromptPosition.Center, true);
Locator.GetPromptManager().AddScreenPrompt(hugPrompt, PromptPosition.Center, OWInput.IsInputMode(InputMode.Character));
canHug = true;
}

Expand All @@ -280,10 +287,9 @@ public void OnAnimatorIK()

private void Update()
{
if (OWInput.GetInputMode() == InputMode.Character) hugPrompt.SetVisibility(true);
else hugPrompt.SetVisibility(false);
hugPrompt.SetVisibility(OWInput.IsInputMode(InputMode.Character));

if (OWInput.IsNewlyPressed(InputLibrary.interactSecondary) && canHug && OWInput.GetInputMode() == InputMode.Character) BeginHugSequence();
if (OWInput.IsNewlyPressed(InputLibrary.interactSecondary, InputMode.Character) && canHug) BeginHugSequence();
}


Expand Down Expand Up @@ -320,7 +326,7 @@ private IEnumerator HugSequenceMain()
{
CommenceHug();
yield return null;
if (isAnimated && HugAnimator.enabled && hugTrigger != "react_None")
if (isAnimated && hugTrigger != "react_None" && HugAnimator.enabled)
{
var wasHugging = IsAnimatorControllerSwapped();
while (HugAnimator.GetCurrentAnimatorClipInfo(0).Length == 0) yield return null;
Expand Down Expand Up @@ -373,7 +379,7 @@ private void PlayHugAnimation(bool wasHugging)
HugAnimator.SetLayerWeight(HugAnimator.GetLayerIndex("Keep Right Hand Pose"), keepHandAnimRight ? 1 : 0);
HugAnimator.SetLayerWeight(HugAnimator.GetLayerIndex("Keep Left Hand Pose"), keepHandAnimLeft ? 1 : 0);
}
SecondaryAnimator?.Play(SecondaryAnimator.GetCurrentAnimatorStateInfo(0).shortNameHash, 0, time);
SecondaryAnimator.Exists()?.Play(SecondaryAnimator.GetCurrentAnimatorStateInfo(0).shortNameHash, 0, time);
HugAnimator.SetTrigger(hugTrigger);
}

Expand Down Expand Up @@ -411,7 +417,7 @@ private IEnumerator ResetAnimator(float delay)
HugAnimator.Play(stateHash, 0, time);
if (HugAnimator.layerCount > 1 && HugAnimator.GetCurrentAnimatorStateInfo(1).shortNameHash == stateHash) HugAnimator.Play(stateHash, 1, time);
}
SecondaryAnimator?.Play(SecondaryAnimator.GetCurrentAnimatorStateInfo(0).shortNameHash, 0, time);
SecondaryAnimator.Exists()?.Play(SecondaryAnimator.GetCurrentAnimatorStateInfo(0).shortNameHash, 0, time);
OnHugFinish?.Invoke();
sequenceInProgress = false;
}
Expand Down Expand Up @@ -456,6 +462,15 @@ private void CancelPending()
}


private void OnDestroy() { OnDestroyEvent?.Invoke(); }
private void OnDestroy()
{
SetHugEnabled(false);
if (HugReceiver != null)
{
HugReceiver.OnGainFocus -= EnableHug;
HugReceiver.OnLoseFocus -= DisableHug;
}
OnDestroyEvent?.Invoke();
}
}
}
15 changes: 13 additions & 2 deletions HugMod/HugMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class HugMod : ModBehaviour
{
public static HugMod HugModInstance;
public static Harmony HarmonyInstance;
public static HugModSettings Settings = new();
public static RuntimeAnimatorController AltRuntimeController;
public static bool PlayerHasDLC;

Expand All @@ -21,7 +22,6 @@ private void Awake()
{
HugModInstance = this;
HarmonyInstance = new Harmony("VioVayo.HugMod");
HugPatches.Apply();
}

private bool MurderCheck()
Expand All @@ -37,9 +37,11 @@ private void Start()
{
ModHelper.Console.WriteLine("Murder is bad >:C", MessageType.Error);
this.enabled = false;
return;
return;
}

var settings = HugModInstance.ModHelper.Storage.Load<HugModSettings>("settings.json");
if (settings != null) Settings = settings;
var hugBundle = ModHelper.Assets.LoadBundle("Assets/hug_bundle");
AltRuntimeController = hugBundle.LoadAsset<GameObject>("Assets/HugModAssets/HugAnims.prefab").GetComponent<Animator>().runtimeAnimatorController;

Expand All @@ -57,7 +59,16 @@ private void Start()
TargetManager.SetUpHugTargets(loadScene);
};

HugPatches.Apply();
ModHelper.Console.WriteLine($"{nameof(HugMod)} is loaded! " + (PlayerHasDLC ? HuggableFrightsMain.HFEnabledMessage(false) : ""), MessageType.Success);
}

public static void UpdateSettings() { HugModInstance.ModHelper.Storage.Save(Settings, "settings.json"); }

public class HugModSettings
{
public bool FriendmakerModeEnabled = false;
public bool SillyGhostNames = false;
}
}
}
26 changes: 16 additions & 10 deletions HugMod/HugModApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ public void SetAnimationMasks(GameObject hugObject, bool fullbodyReact, bool kee
else HugMod.HugModInstance.ModHelper.Console.WriteLine($"Couldn't find HugComponent on \"{hugObject.name}\".", MessageType.Error);
}

public void SetUnderlayTransition(GameObject hugObject, string transitionClipName, int transitionHash, float transitionTime)
public void SetUnderlayTransition(GameObject hugObject, AnimationClip transitionClip, int transitionHash, float transitionTime)
{
if (hugObject.TryGetComponent(out HugComponent hug)) hug.SetUnderlayTransition(transitionClipName, transitionHash, transitionTime);
if (hugObject.TryGetComponent(out HugComponent hug)) hug.SetUnderlayTransition(transitionClip, transitionHash, transitionTime);
else HugMod.HugModInstance.ModHelper.Console.WriteLine($"Couldn't find HugComponent on \"{hugObject.name}\".", MessageType.Error);
}

public void SetUnderlayTransition(GameObject hugObject, AnimationClip transitionClip, int transitionHash, float transitionTime)
public void SetUnderlayTransition(GameObject hugObject, string transitionClipName, int transitionHash, float transitionTime)
{
if (hugObject.TryGetComponent(out HugComponent hug)) hug.SetUnderlayTransition(transitionClip, transitionHash, transitionTime);
if (hugObject.TryGetComponent(out HugComponent hug)) hug.SetUnderlayTransition(transitionClipName, transitionHash, transitionTime);
else HugMod.HugModInstance.ModHelper.Console.WriteLine($"Couldn't find HugComponent on \"{hugObject.name}\".", MessageType.Error);
}


//-----Get stuff-----
public InteractReceiver GetHugReceiver(GameObject hugObject)
public InteractReceiver GetInteractReceiver(GameObject hugObject)
{
if (hugObject.TryGetComponent(out HugComponent hug)) return hug.HugReceiver;
else
Expand All @@ -96,7 +96,7 @@ public InteractReceiver GetHugReceiver(GameObject hugObject)
}
}

public Animator GetHugAnimator(GameObject hugObject)
public Animator GetPrimaryAnimator(GameObject hugObject)
{
if (hugObject.TryGetComponent(out HugComponent hug)) return hug.HugAnimator;
else
Expand Down Expand Up @@ -204,9 +204,15 @@ public void ChangeTriggerCollider(GameObject hugObject, Collider newCompareColli
else HugMod.HugModInstance.ModHelper.Console.WriteLine($"Couldn't find HugComponent on \"{hugObject.name}\".", MessageType.Error);
}

public void ChangePrimaryAnimator(GameObject hugObject, Animator newAnimator, RuntimeAnimatorController newAnimatorController)
public void ChangePrimaryAnimator(GameObject hugObject, Animator newAnimator, bool resetAnimatorController)
{
if (hugObject.TryGetComponent(out HugComponent hug)) hug.ChangePrimaryAnimator(newAnimator, resetAnimatorController);
else HugMod.HugModInstance.ModHelper.Console.WriteLine($"Couldn't find HugComponent on \"{hugObject.name}\".", MessageType.Error);
}

public void ChangeAnimatorController(GameObject hugObject, RuntimeAnimatorController newAnimatorController)
{
if (hugObject.TryGetComponent(out HugComponent hug)) hug.ChangePrimaryAnimator(newAnimator, newAnimatorController);
if (hugObject.TryGetComponent(out HugComponent hug)) hug.ChangeAnimatorController(newAnimatorController);
else HugMod.HugModInstance.ModHelper.Console.WriteLine($"Couldn't find HugComponent on \"{hugObject.name}\".", MessageType.Error);
}

Expand All @@ -216,9 +222,9 @@ public void ChangeSecondaryAnimator(GameObject hugObject, Animator newAnimator)
else HugMod.HugModInstance.ModHelper.Console.WriteLine($"Couldn't find HugComponent on \"{hugObject.name}\".", MessageType.Error);
}

public void ChangeCharacterAnimController(GameObject hugObject, CharacterAnimController newAnimController)
public void ChangeCharacterAnimController(GameObject hugObject, CharacterAnimController newCharacterAnimController)
{
if (hugObject.TryGetComponent(out HugComponent hug)) hug.ChangeCharacterAnimController(newAnimController);
if (hugObject.TryGetComponent(out HugComponent hug)) hug.ChangeCharacterAnimController(newCharacterAnimController);
else HugMod.HugModInstance.ModHelper.Console.WriteLine($"Couldn't find HugComponent on \"{hugObject.name}\".", MessageType.Error);
}
}
Expand Down
14 changes: 12 additions & 2 deletions HugMod/HugModUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ namespace HugMod
{
public static class HugModUtilities
{
public static T[] AddToArray<T>(T[] array, params T[] toAdd)
public static T[] AddToArray<T>(this T[] array, params T[] toAdd)
{
var list = array.ToList();
foreach (var addition in toAdd) list.Add(addition);
return list.ToArray();
}

public static Transform FindInDescendants(this GameObject gameObject, string name, bool includeInactive = true)
=> gameObject.GetComponentsInChildren<Transform>(includeInactive).FirstOrDefault(obj => obj.gameObject.name == name);

public static bool TryGetComponentInChildren<T>(this GameObject gameObject, out T component) where T : Component
{
return gameObject.GetComponentsInChildren<Transform>(includeInactive).Where(obj => obj.gameObject.name == name).First();
component = gameObject.GetComponentInChildren<T>();
return component != null;
}

public static GameObject CreateChild(this GameObject parentObject, string name, Vector3 localPosition = default, Vector3 localEulerAngles = default, float scaleMultiplier = 1)
=> parentObject.transform.CreateChild(name, localPosition, localEulerAngles, scaleMultiplier);

public static GameObject CreateChild(this Transform parentTransform, string name, Vector3 localPosition = default, Vector3 localEulerAngles = default, float scaleMultiplier = 1)
{
var childObj = new GameObject(name);
Expand All @@ -26,5 +33,8 @@ public static GameObject CreateChild(this Transform parentTransform, string name
childObj.transform.localScale = scaleMultiplier * Vector3.one;
return childObj;
}

public static T Exists<T>(this T obj) where T : Object
=> (obj != null) ? obj : null; //Enable use of null conditional operators with destroyed Unity objects
}
}
11 changes: 1 addition & 10 deletions HugMod/HugPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class HugPatches
public static IEnumerable<CodeInstruction> PlayerCharacterController_UpdateMovement_Transpiler(IEnumerable<CodeInstruction> instructions)
{
var matcher = new CodeMatcher(instructions).MatchForward(false,
new CodeMatch(OpCodes.Call, AccessTools.Method(typeof(OWInput), "OWInput.GetAxisValue", new Type[] { typeof(IInputCommands), typeof(InputMode) })),
new CodeMatch(OpCodes.Call, AccessTools.Method(typeof(OWInput), nameof(OWInput.GetAxisValue), new Type[] { typeof(IInputCommands), typeof(InputMode) })),
new CodeMatch(OpCodes.Stloc_0)
).Advance(2);

Expand All @@ -45,14 +45,5 @@ public static bool UnityLogger_OnLogMessageReceived(string message)
// Filter out goofy error that doesn't actually break anything
return !message.EndsWith(") is out of bounds (size=0)");
}


//ONLY FOR DEBUG
[HarmonyPrefix]
[HarmonyPatch(typeof(ShipLogManager), nameof(ShipLogManager.CheckForCompletionAchievement))]
public static bool ShipLogManager_CheckForCompletionAchievement_Prefix()
{
return false;
}
}
}
Loading

0 comments on commit 161840c

Please sign in to comment.