Skip to content

Commit

Permalink
Vanilla rewards will now appear in the spawner as locked, and unlock …
Browse files Browse the repository at this point in the history
…in spawner when rewarded
  • Loading branch information
devyndamonster committed Feb 26, 2022
1 parent 0c3d091 commit 9ec197f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 16 deletions.
1 change: 1 addition & 0 deletions Scripts/Loaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,7 @@ private ItemSpawnerID AddEntryToLegacySpawner(ItemSpawnerEntry entry)
IM.CD[itemSpawnerID.Category].Add(itemSpawnerID);
IM.SCD[itemSpawnerID.SubCategory].Add(itemSpawnerID);
IM.Instance.SpawnerIDDic[itemSpawnerID.MainObject.ItemID] = itemSpawnerID;
OtherLoader.SpawnerIDsByMainObject[itemSpawnerID.MainObject.ItemID] = itemSpawnerID;

return itemSpawnerID;
}
Expand Down
22 changes: 16 additions & 6 deletions Scripts/ObjectTemplates/UnlockedItemSaveData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace OtherLoader
{
Expand All @@ -13,7 +14,12 @@ public class UnlockedItemSaveData
public ItemUnlockMode UnlockMode;
public List<string> UnlockedItemIDs = new List<string>();

public UnlockedItemSaveData() { }
private RewardSystem rewardSystem = new RewardSystem();

public UnlockedItemSaveData()
{
rewardSystem.InitializeFromSaveFile();
}

public UnlockedItemSaveData(ItemUnlockMode unlockMode)
{
Expand All @@ -23,6 +29,8 @@ public UnlockedItemSaveData(ItemUnlockMode unlockMode)
{
AutoUnlockNonRewards = false;
}

rewardSystem.InitializeFromSaveFile();
}

public bool IsItemUnlocked(string itemID)
Expand All @@ -43,7 +51,8 @@ public bool UnlockItem(string itemID)

public bool ShouldAutoUnlockItem(ItemSpawnerID spawnerID)
{
return spawnerID.MainObject == null || ShouldAutoUnlockItem(spawnerID.MainObject, spawnerID.IsReward);
return spawnerID.MainObject == null ||
ShouldAutoUnlockItem(spawnerID.MainObject, spawnerID.IsReward);
}

public bool ShouldAutoUnlockItem(ItemSpawnerEntry spawnerEntry)
Expand All @@ -56,7 +65,7 @@ public bool ShouldAutoUnlockItem(ItemSpawnerEntry spawnerEntry)

public bool ShouldAutoUnlockItem(FVRObject item, bool isReward)
{
if(!IsVanillaUnlocked(item.ItemID) ||
if((isReward && !IsVanillaUnlocked(item)) ||
(!AutoUnlockNonRewards &&
(item.Category == FVRObject.ObjectCategory.Firearm ||
item.Category == FVRObject.ObjectCategory.Thrown ||
Expand All @@ -68,12 +77,13 @@ public bool ShouldAutoUnlockItem(FVRObject item, bool isReward)
return true;
}

public bool IsVanillaUnlocked(string itemID)
public bool IsVanillaUnlocked(FVRObject item)
{
ItemSpawnerID spawnerID;
IM.Instance.SpawnerIDDic.TryGetValue(itemID, out spawnerID);
OtherLoader.SpawnerIDsByMainObject.TryGetValue(item.ItemID, out spawnerID);

return spawnerID == null || GM.Rewards.RewardUnlocks.IsRewardUnlocked(spawnerID);
//We must use our own version of reward system here because the GM does not always perform Awake before the IM
return spawnerID == null || rewardSystem.RewardUnlocks.IsRewardUnlocked(spawnerID.ItemID);
}
}

Expand Down
12 changes: 9 additions & 3 deletions Scripts/OtherLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class OtherLoader : StratumPlugin
public static Dictionary<string, EntryNode> SpawnerEntriesByPath = new Dictionary<string, EntryNode>();
public static Dictionary<string, ItemSpawnerEntry> SpawnerEntriesByID = new Dictionary<string, ItemSpawnerEntry>();
public static Dictionary<string, MediaPath> TutorialBlockVideos = new Dictionary<string, MediaPath>();
public static Dictionary<string, ItemSpawnerID> SpawnerIDsByMainObject = new Dictionary<string, ItemSpawnerID>();
public static UnlockedItemSaveData UnlockSaveData;
public static Sprite LockIcon;

Expand Down Expand Up @@ -367,13 +368,18 @@ public static bool UnlockInteractedItem(FVRPhysicalObject __instance)


[HarmonyPatch(typeof(IM), "RegisterItemIntoMetaTagSystem")]
[HarmonyPostfix]
[HarmonyPostfix]
private static void MetaTagPatch(ItemSpawnerID ID)
{
//If this item is not a reward, unlock it by default
if(UnlockSaveData.ShouldAutoUnlockItem(ID))
if (ID.MainObject != null)
{
UnlockSaveData.UnlockItem(ID.MainObject.ItemID);
SpawnerIDsByMainObject[ID.MainObject.ItemID] = ID;

if (UnlockSaveData.ShouldAutoUnlockItem(ID))
{
UnlockSaveData.UnlockItem(ID.MainObject.ItemID);
}
}

//If this IDs items didn't get added, add it to the firearm page
Expand Down
19 changes: 19 additions & 0 deletions Scripts/Patches/ItemSpawnerPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ private static bool StartPatch(ItemSpawnerUI __instance)
return true;
}

[HarmonyPatch(typeof(IM), "GetAvailableCountInSubCategory")]
[HarmonyPrefix]
private static bool SubCatCountPatch(ItemSpawnerUI __instance, ItemSpawnerID.ESubCategory subcat, ref int __result)
{
__result = IM.SCD[subcat].Count;

return false;
}

[HarmonyPatch(typeof(IM), "GetAvailableInSubCategory")]
[HarmonyPrefix]
private static bool SubCatPatch(ItemSpawnerUI __instance, ItemSpawnerID.ESubCategory subcat, ref List<ItemSpawnerID> __result)
{
__result = new List<ItemSpawnerID>();
__result.AddRange(IM.SCD[subcat]);

return false;
}



[HarmonyPatch(typeof(ItemSpawnerUI), "SetMode_Home")]
Expand Down
41 changes: 34 additions & 7 deletions Scripts/Patches/ItemSpawnerV2Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,7 @@ private static bool RedrawDetailsCanvasPatch(ItemSpawnerV2 __instance)
}

ItemSpawnerEntry secondary = OtherLoader.SpawnerEntriesByID[entry.SecondaryObjectIDs[m]];
if(!secondary.IsReward || GM.Rewards.RewardUnlocks.Rewards.Contains(secondary.MainObjectID))
{
secondaryEntries.Add(secondary);
}
secondaryEntries.Add(secondary);
}


Expand Down Expand Up @@ -865,7 +862,31 @@ private static void TagNamingPatch(ILContext ctx, MethodBase orig)
//Now add our new call for setting the text
c.Emit(OpCodes.Call, ((Action<Text, string, TagType>)CategoricalSetText).Method);
}




[HarmonyPatch(typeof(IM), "GenerateItemDBs")]
[HarmonyILManipulator]
private static void DisableRewardCheckPatch(ILContext ctx, MethodBase orig)
{
ILCursor c = new ILCursor(ctx);

c.GotoNext(
i => i.MatchLdfld(AccessTools.Field(typeof(ItemSpawnerID), "MainObject"))
);

c.RemoveRange(17);

c.Emit(OpCodes.Call, ((Action<ItemSpawnerID>)RegisterItemSpawnerID).Method);

c.GotoNext(
i => i.MatchLdstr("SosigEnemyTemplates")
);

c.Emit(OpCodes.Call, ((Action)PopulateSpawnerEntries).Method);

}



private static IEnumerator SpawnItems(ItemSpawnerV2 instance, ItemSpawnerEntry entry)
Expand Down Expand Up @@ -915,9 +936,15 @@ private static void CategoricalSetText(Text text, string value, TagType tagType)
text.text = value;
}

private static void RegisterItemSpawnerID(ItemSpawnerID spawnerID)
{
if(spawnerID.MainObject != null)
{
IM.RegisterItemIntoMetaTagSystem(spawnerID);
}
}


[HarmonyPatch(typeof(IM), "GenerateItemDBs")]
[HarmonyPostfix]
private static void PopulateSpawnerEntries()
{
foreach(KeyValuePair<ItemSpawnerV2.PageMode,List<string>> PageLists in IM.Instance.PageItemLists)
Expand Down

0 comments on commit 9ec197f

Please sign in to comment.