Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follower commands #11

Merged
merged 10 commits into from
Aug 27, 2022
4 changes: 4 additions & 0 deletions COTL_API/COTL_API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<ItemGroup>
<PackageReference Include="CultOfTheLamb.GameLibs" Version="1.0.12-*" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
</Project>
36 changes: 36 additions & 0 deletions COTL_API/CustomFollowerCommand/CustomFollowerCommandItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using COTL_API.Helpers;
using Lamb.UI.FollowerInteractionWheel;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace COTL_API.CustomFollowerCommand
{
public abstract class CustomFollowerCommandItem : CommandItem
{
public virtual string InternalName { get; set; }
public Sprite Icon { get; internal set; }

public virtual List<FollowerCommandCategory> GetCategories() { return new List<FollowerCommandCategory>() { FollowerCommandCategory.DEFAULT_COMMAND }; }

public string ModPrefix;

public virtual List<CommandItem> GetSubCommands()
{
return new List<CommandItem>();
}

public virtual bool CheckSelectionPreconditions(Follower follower)
{
return true;
}

public abstract bool Execute(interaction_FollowerInteraction interaction, FollowerCommands finalCommand = FollowerCommands.None);

public CustomFollowerCommandItem()
{
Icon = TextureHelper.CreateSpriteFromPath(PluginPaths.ResolveAssetPath("placeholder.png"));
}
}
}
107 changes: 107 additions & 0 deletions COTL_API/CustomFollowerCommand/CustomFollowerCommandManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using COTL_API.CustomInventory;
using COTL_API.Guid;
using HarmonyLib;
using Lamb.UI.FollowerInteractionWheel;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace COTL_API.CustomFollowerCommand;

[HarmonyPatch]
public class CustomFollowerCommandManager
{
internal static Dictionary<FollowerCommands, CustomFollowerCommandItem> customCommands = new Dictionary<FollowerCommands, CustomFollowerCommandItem>();

public static FollowerCommands Add(CustomFollowerCommandItem item)
{
var guid = TypeManager.GetModIdFromCallstack(Assembly.GetCallingAssembly());

var followerCommand = GuidManager.GetEnumValue<FollowerCommands>(guid, item.InternalName);
item.Command = followerCommand;
item.SubCommands = item.GetSubCommands();
item.ModPrefix = guid;

customCommands.Add(followerCommand, item);

return followerCommand;
}

[HarmonyPatch(typeof(FollowerCommandGroups), "DefaultCommands", new Type[] { typeof(Follower) })]
[HarmonyPostfix]
public static void FollowerCommandGroups_DefaultCommands(Follower follower, List<CommandItem> __result)
{
customCommands.Values.Do(c => { if (c.GetCategories().Contains(FollowerCommandCategory.DEFAULT_COMMAND) && c.CheckSelectionPreconditions(follower)) __result.Add(c); });
}

[HarmonyPatch(typeof(FollowerCommandGroups), "GiveWorkerCommands", new Type[] { typeof(Follower) })]
[HarmonyPostfix]
public static void FollowerCommandGroups_GiveWorkerCommands(Follower follower, List<CommandItem> __result)
{
customCommands.Values.Do(c => { if (c.GetCategories().Contains(FollowerCommandCategory.GIVE_WORKER_COMMAND) && c.CheckSelectionPreconditions(follower)) __result.Add(c); });
}

[HarmonyPatch(typeof(FollowerCommandGroups), "MakeDemandCommands", new Type[] { typeof(Follower) })]
[HarmonyPostfix]
public static void FollowerCommandGroups_MakeDemandCommands(Follower follower, List<CommandItem> __result)
{
customCommands.Values.Do(c => { if (c.GetCategories().Contains(FollowerCommandCategory.MAKE_DEMAND_COMMAND) && c.CheckSelectionPreconditions(follower)) __result.Add(c); });
}

[HarmonyPatch(typeof(FollowerCommandGroups), "WakeUpCommands", new Type[] { })]
[HarmonyPostfix]
public static void FollowerCommandGroups_WakeUpCommands(List<CommandItem> __result)
{
customCommands.Values.Do(c => { if (c.GetCategories().Contains(FollowerCommandCategory.WAKE_UP_COMMAND)) __result.Add(c); });
}

[HarmonyPatch(typeof(FollowerCommandGroups), "OldAgeCommands", new Type[] { typeof(Follower) })]
[HarmonyPostfix]
public static void FollowerCommandGroups_OldAgeCommands(Follower follower, List<CommandItem> __result)
{
customCommands.Values.Do(c => { if (c.GetCategories().Contains(FollowerCommandCategory.OLD_AGE_COMMAND) && c.CheckSelectionPreconditions(follower)) __result.Add(c); });
}

[HarmonyPatch(typeof(FollowerCommandGroups), "DissenterCommands", new Type[] { typeof(Follower) })]
[HarmonyPostfix]
public static void FollowerCommandGroups_DissenterCommands(Follower follower, List<CommandItem> __result)
{
customCommands.Values.Do(c => { if (c.GetCategories().Contains(FollowerCommandCategory.DISSENTER_COMMAND) && c.CheckSelectionPreconditions(follower)) __result.Add(c); });
}

[HarmonyPatch(typeof(interaction_FollowerInteraction), "OnFollowerCommandFinalized", new Type[] { typeof(FollowerCommands[]) })]
[HarmonyPrefix]
public static bool interaction_FollowerInteraction_OnFollowerCommandFinalized(interaction_FollowerInteraction __instance, FollowerCommands[] followerCommands)
{
FollowerCommands command = followerCommands[0];
FollowerCommands preFinalCommand = ((followerCommands.Length > 1) ? followerCommands[1] : FollowerCommands.None);

if (customCommands.ContainsKey(command) || customCommands.ContainsKey(preFinalCommand))
{
bool shouldClose = true;
if (customCommands.ContainsKey(preFinalCommand))
{
shouldClose = customCommands[preFinalCommand].Execute(__instance, command);
}
else
{
shouldClose = customCommands[command].Execute(__instance);
}
__instance.Close();
return false;
}
return true;
}

[HarmonyPatch(typeof(FontImageNames), "IconForCommand", new Type[] { typeof(FollowerCommands) })]
[HarmonyPrefix]
public static bool interaction_FollowerInteraction_OnFollowerCommandFinalized(FollowerCommands followerCommands, ref string __result)
{
if (customCommands.ContainsKey(followerCommands))
{
__result = $"<sprite name=\"icon_FCOMMAND_{customCommands[followerCommands].ModPrefix}.${customCommands[followerCommands].InternalName}\">";
return false;
}
return true;
}
}
12 changes: 12 additions & 0 deletions COTL_API/CustomFollowerCommand/FollowerCommandCategory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace COTL_API.CustomFollowerCommand
{
public enum FollowerCommandCategory
{
DEFAULT_COMMAND,
DISSENTER_COMMAND,
GIVE_WORKER_COMMAND,
MAKE_DEMAND_COMMAND,
OLD_AGE_COMMAND,
WAKE_UP_COMMAND
}
}
2 changes: 1 addition & 1 deletion COTL_API/CustomInventory/CustomItemManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void ItemInfoCard_Configure(ItemInfoCard __instance, InventoryItem
public static bool FontImageNames_GetIconByType(InventoryItem.ITEM_TYPE Type, ref string __result)
{
if (!CustomItems.ContainsKey(Type)) return true;
__result = $"<sprite name=\"icon_{CustomItems[Type].ModPrefix}.${CustomItems[Type].InternalName}\">";
__result = $"<sprite name=\"icon_ITEM_{CustomItems[Type].ModPrefix}.${CustomItems[Type].InternalName}\">";
return false;
}

Expand Down
28 changes: 28 additions & 0 deletions COTL_API/INDEV/DEBUG_FOLLOWER_COMMAND_CLASS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using COTL_API.CustomFollowerCommand;
using System;
using System.Collections.Generic;
using System.Text;

namespace COTL_API.INDEV
{
public class DEBUG_FOLLOWER_COMMAND_CLASS : CustomFollowerCommandItem
{
public override string InternalName { get => "DEBUG_FOLLOWER_COMMAND"; }

public override bool CheckSelectionPreconditions(Follower follower)
{
return base.CheckSelectionPreconditions(follower);
}

public override bool Execute(interaction_FollowerInteraction interaction, FollowerCommands finalCommand = FollowerCommands.None)
{
interaction.StartCoroutine(interaction.FrameDelayCallback(delegate
{
interaction.eventListener.PlayFollowerVO(interaction.generalAcknowledgeVO);
interaction.follower.Brain.HardSwapToTask(new FollowerTask_InstantPoop());
}));

return true;
}
}
}
34 changes: 34 additions & 0 deletions COTL_API/INDEV/DEBUG_FOLLOWER_COMMAND_CLASS_2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using COTL_API.CustomFollowerCommand;
using Lamb.UI.FollowerInteractionWheel;
using System;
using System.Collections.Generic;
using System.Text;

namespace COTL_API.INDEV
{
public class DEBUG_FOLLOWER_COMMAND_CLASS_2 : CustomFollowerCommandItem
{
public override string InternalName { get => "DEBUG_FOLLOWER_COMMAND_2"; }

public override List<FollowerCommandCategory> GetCategories()
{
return new List<FollowerCommandCategory>() { FollowerCommandCategory.MAKE_DEMAND_COMMAND };
}

public override bool CheckSelectionPreconditions(Follower follower)
{
return base.CheckSelectionPreconditions(follower);
}

public override bool Execute(interaction_FollowerInteraction interaction, FollowerCommands finalCommand = FollowerCommands.None)
{
interaction.follower.Brain.MakeDissenter();
return true;
}

public override List<CommandItem> GetSubCommands()
{
return FollowerCommandGroups.AreYouSureCommands();
}
}
}
14 changes: 11 additions & 3 deletions COTL_API/Icons/IconPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using COTL_API.CustomInventory;
using COTL_API.CustomFollowerCommand;
using COTL_API.CustomInventory;
using COTL_API.Helpers;
using HarmonyLib;
using TMPro;
Expand All @@ -16,13 +17,20 @@ public static bool TMP_SpriteAsset_SearchForSpriteByHashCode(TMP_SpriteAsset spr

foreach (CustomInventoryItem item in CustomItemManager.CustomItems.Values)
{
string name = $"icon_{item.ModPrefix}.${item.InternalName}";
string name = $"icon_ITEM_{item.ModPrefix}.${item.InternalName}";
if (hashCode != HashCode.GetValueHashCode(name)) continue;

spriteIndex = 0;
__result = IconManager.GetIcon(item.InventoryIcon, name, spriteAsset.material.shader, hashCode);
return false;
}
foreach (CustomFollowerCommandItem item in CustomFollowerCommandManager.customCommands.Values)
{
string name = $"icon_FCOMMAND_{item.ModPrefix}.${item.InternalName}";
if (hashCode != HashCode.GetValueHashCode(name)) continue;
spriteIndex = 0;
__result = IconManager.GetIcon(item.Icon, name, spriteAsset.material.shader, hashCode);
return false;
}
return true;
}
}
7 changes: 6 additions & 1 deletion COTL_API/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public class Plugin : BaseUnityPlugin
internal static InventoryItem.ITEM_TYPE DEBUG_ITEM_2;
internal static InventoryItem.ITEM_TYPE DEBUG_ITEM_3;

internal static FollowerCommands DEBUG_FOLLOWER_COMMAND;
internal static FollowerCommands DEBUG_FOLLOWER_COMMAND_2;

private static ConfigEntry<bool> _debugEnabled;

internal static bool DebugEnabled => _debugEnabled.Value;

private void Awake()
Expand All @@ -39,6 +41,9 @@ private void Awake()
_debugEnabled = Config.Bind("", "debug", false, "");

if (!DebugEnabled) return;

DEBUG_FOLLOWER_COMMAND = CustomFollowerCommand.CustomFollowerCommandManager.Add(new INDEV.DEBUG_FOLLOWER_COMMAND_CLASS());
DEBUG_FOLLOWER_COMMAND_2 = CustomFollowerCommand.CustomFollowerCommandManager.Add(new INDEV.DEBUG_FOLLOWER_COMMAND_CLASS_2());

DEBUG_ITEM = CustomInventory.CustomItemManager.Add(new DEBUG_ITEM_CLASS());
DEBUG_ITEM_2 = CustomInventory.CustomItemManager.Add(new DEBUG_ITEM_CLASS_2());
Expand Down
1 change: 1 addition & 0 deletions COTL_API/Skins/SkinManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace COTL_API.Skins;

[HarmonyPatch]
public class SkinManager
{
internal static readonly Dictionary<string, SpineAtlasAsset> CustomAtlases = new();
Expand Down