Skip to content

Commit 088f8a3

Browse files
committed
Added SetSpecialAbilityIdentifiers.
Refactored to now check if the ability already exists. Added Card_AttachAbilities_NewSpecialAbilities.cs to add SpecialAbilities. Cleaned up AbilitiesUtil.cs and AbilityIdentifier.cs
1 parent c019a75 commit 088f8a3

7 files changed

Lines changed: 167 additions & 67 deletions

File tree

Models/AbilityIdentifier.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
1-
using System;
21
using System.Collections.Generic;
32
using DiskCardGame;
4-
using UnityEngine;
53

64
namespace APIPlugin
75
{
86
public class AbilityIdentifier
97
{
10-
private static List<AbilityIdentifier> ids = new List<AbilityIdentifier>();
8+
private static List<AbilityIdentifier> ids = new();
119
private string guid;
12-
private string name;
13-
public Ability id;
10+
private string name;
11+
public Ability id;
1412

1513
private AbilityIdentifier(string guid, string name)
1614
{
1715
this.guid = guid;
1816
this.name = name;
19-
ids.Add(this);
17+
ids.Add(this);
2018
}
2119

22-
public static AbilityIdentifier GetAbilityIdentifier(string guid, string name)
23-
{
24-
if (ids.Exists((AbilityIdentifier x) => x.guid == guid && x.name == name))
25-
{
26-
return ids.Find((AbilityIdentifier x) => x.guid == guid && x.name == name);
27-
}
28-
else
29-
{
30-
return new AbilityIdentifier(guid, name);
31-
}
32-
}
20+
public static AbilityIdentifier GetAbilityIdentifier(string guid, string name)
21+
{
22+
return ids.Exists(x => x.guid == guid && x.name == name)
23+
? ids.Find(x => x.guid == guid && x.name == name)
24+
: new AbilityIdentifier(guid, name);
25+
}
3326

34-
public override String ToString()
27+
public override string ToString()
3528
{
36-
return $"{this.guid}({this.name})";
29+
return $"{guid}({name})";
3730
}
3831
}
39-
}
32+
}

Models/NewAbility.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace APIPlugin
77
{
88
public class NewAbility
99
{
10-
public static List<NewAbility> abilities = new List<NewAbility>();
10+
public static List<NewAbility> abilities = new();
1111
public Ability ability;
1212
public AbilityInfo info;
1313
public Type abilityBehaviour;
@@ -16,14 +16,14 @@ public class NewAbility
1616

1717
public NewAbility(AbilityInfo info, Type abilityBehaviour, Texture tex, AbilityIdentifier id = null)
1818
{
19-
this.ability = (Ability) 100 + NewAbility.abilities.Count;
20-
info.ability = this.ability;
19+
ability = (Ability) 100 + abilities.Count;
20+
info.ability = ability;
2121
this.info = info;
2222
this.abilityBehaviour = abilityBehaviour;
2323
tex.filterMode = FilterMode.Point;
2424
this.tex = tex;
2525
this.id = id;
26-
NewAbility.abilities.Add(this);
26+
abilities.Add(this);
2727
if (id != null){
2828
id.id = ability;
2929
}

Models/NewCard.cs

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using CardLoaderPlugin.lib;
34
using DiskCardGame;
45
using UnityEngine;
@@ -7,17 +8,21 @@ namespace APIPlugin
78
{
89
public static class NewCard
910
{
10-
public static List<CardInfo> cards = new List<CardInfo>();
11-
public static Dictionary<int,List<AbilityIdentifier>> abilityIds = new Dictionary<int,List<AbilityIdentifier>>();
12-
public static Dictionary<int,EvolveIdentifier> evolveIds = new Dictionary<int,EvolveIdentifier>();
13-
public static Dictionary<int,IceCubeIdentifier> iceCubeIds = new Dictionary<int,IceCubeIdentifier>();
14-
public static Dictionary<int,TailIdentifier> tailIds = new Dictionary<int,TailIdentifier>();
15-
16-
public static void Add(CardInfo card, List<AbilityIdentifier> abilityIds = null, EvolveIdentifier evolveId = null,
11+
public static List<CardInfo> cards = new();
12+
13+
public static Dictionary<int, List<AbilityIdentifier>> abilityIds = new();
14+
public static Dictionary<int, List<SpecialAbilityIdentifier>> specialAbilityIds = new();
15+
public static Dictionary<int, EvolveIdentifier> evolveIds = new();
16+
public static Dictionary<int, IceCubeIdentifier> iceCubeIds = new();
17+
public static Dictionary<int, TailIdentifier> tailIds = new();
18+
19+
public static void Add(CardInfo card, List<AbilityIdentifier> abilityIdsParam = null,
20+
List<SpecialAbilityIdentifier> specialAbilitiesIdsParam = null,
21+
EvolveIdentifier evolveId = null,
1722
IceCubeIdentifier iceCubeId = null, TailIdentifier tailId = null)
1823
{
1924
NewCard.cards.Add(card);
20-
handleIdentifiers(card, abilityIds, evolveId, iceCubeId, tailId);
25+
HandleIdentifiers(card, abilityIdsParam, specialAbilitiesIdsParam, evolveId, iceCubeId, tailId);
2126
Plugin.Log.LogInfo($"Loaded custom card {card.name}!");
2227
}
2328

@@ -30,7 +35,8 @@ public static void Add(string name, List<CardMetaCategory> metaCategories, CardC
3035
bool hideAttackAndHealth = false, int cost = 0, int bonesCost = 0, int energyCost = 0,
3136
List<GemType> gemsCost = null, SpecialStatIcon specialStatIcon = SpecialStatIcon.None,
3237
List<Tribe> tribes = null, List<Trait> traits = null, List<SpecialTriggeredAbility> specialAbilities = null,
33-
List<Ability> abilities = null, List<AbilityIdentifier> abilityIds = null, EvolveParams evolveParams = null,
38+
List<Ability> abilities = null, List<AbilityIdentifier> abilityIdsParam = null,
39+
List<SpecialAbilityIdentifier> specialAbilitiesIdsParam = null, EvolveParams evolveParams = null,
3440
string defaultEvolutionName = null, TailParams tailParams = null, IceCubeParams iceCubeParams = null,
3541
bool flipPortraitForStrafe = false, bool onePerDeck = false,
3642
List<CardAppearanceBehaviour.Appearance> appearanceBehaviour = null, Texture2D tex = null,
@@ -133,33 +139,51 @@ public static void Add(string name, List<CardMetaCategory> metaCategories, CardC
133139

134140
NewCard.cards.Add(card);
135141

136-
handleIdentifiers(card, abilityIds, evolveId, iceCubeId, tailId);
142+
HandleIdentifiers(card, abilityIdsParam, specialAbilitiesIdsParam, evolveId, iceCubeId, tailId);
137143

138144
Plugin.Log.LogInfo($"Loaded custom card {name}!");
139145
}
140146

141-
private static void handleIdentifiers(CardInfo card, List<AbilityIdentifier> abilityIds, EvolveIdentifier evolveId,
142-
IceCubeIdentifier iceCubeId, TailIdentifier tailId)
147+
private static void HandleIdentifiers(
148+
CardInfo card,
149+
List<AbilityIdentifier> abilityIdsParam,
150+
List<SpecialAbilityIdentifier> specialAbilitiesIdsParam,
151+
EvolveIdentifier evolveId,
152+
IceCubeIdentifier iceCubeId,
153+
TailIdentifier tailId)
143154
{
144155
// Handle AbilityIdentifier
145156
List<AbilityIdentifier> toRemove = new List<AbilityIdentifier>();
146-
if (abilityIds is not null)
157+
if (abilityIdsParam is not null)
147158
{
148-
foreach (AbilityIdentifier id in abilityIds)
159+
foreach (var id in abilityIdsParam.Where(id => id.id != 0))
149160
{
150-
if (id.id != 0)
151-
{
152-
card.abilities.Add(id.id);
153-
}
161+
card.abilities.Add(id.id);
154162
}
163+
155164
foreach (AbilityIdentifier id in toRemove)
156165
{
157-
abilityIds.Remove(id);
166+
abilityIdsParam.Remove(id);
167+
}
168+
169+
if (abilityIdsParam.Count > 0)
170+
{
171+
NewCard.abilityIds[NewCard.cards.Count - 1] = abilityIdsParam;
158172
}
159173
}
160-
if (abilityIds is not null && abilityIds.Count > 0)
174+
175+
// Handle SpecialAbilityIds
176+
if (specialAbilitiesIdsParam is not null)
161177
{
162-
NewCard.abilityIds[NewCard.cards.Count - 1] = abilityIds;
178+
foreach (var id in specialAbilitiesIdsParam.Where(id => id.id != 0))
179+
{
180+
card.specialAbilities.Add(id.id);
181+
}
182+
183+
if (specialAbilitiesIdsParam.Count > 0)
184+
{
185+
NewCard.specialAbilityIds[NewCard.cards.Count - 1] = specialAbilitiesIdsParam;
186+
}
163187
}
164188

165189
// Handle EvolveIdentifier
@@ -209,9 +233,10 @@ private static void DetermineAndSetCardArt(
209233
pixelTex.name = newName;
210234
pixelTex.filterMode = FilterMode.Point;
211235

212-
card.pixelPortrait = Sprite.Create(pixelTex, CardUtils.DefaultCardPixelArtRect, CardUtils.DefaultVector2);
236+
card.pixelPortrait =
237+
Sprite.Create(pixelTex, CardUtils.DefaultCardPixelArtRect, CardUtils.DefaultVector2);
213238
card.pixelPortrait.name = newName;
214239
}
215240
}
216241
}
217-
}
242+
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using APIPlugin;
43
using DiskCardGame;
54
using HarmonyLib;
65
using UnityEngine;
76

87
namespace API.Patches
98
{
10-
[HarmonyPatch(typeof(AbilitiesUtil), "LoadAbilityIcon", new Type[] { typeof(string), typeof(bool), typeof(bool) })]
9+
[HarmonyPatch(typeof(AbilitiesUtil), "LoadAbilityIcon",
10+
typeof(string), typeof(bool), typeof(bool))]
1111
public class AbilitiesUtil_LoadAbilityIcon
1212
{
1313
public static bool Prefix(string abilityName, CardTriggerHandler __instance, ref Texture __result)
@@ -18,16 +18,14 @@ public static bool Prefix(string abilityName, CardTriggerHandler __instance, ref
1818
return true;
1919
}
2020

21-
NewAbility newAbility = NewAbility.abilities.Find((NewAbility x) => x.ability == (Ability)ability);
21+
NewAbility newAbility = NewAbility.abilities.Find(x => x.ability == (Ability)ability);
2222
__result = newAbility.tex;
2323
return false;
2424
}
2525
}
2626

27-
[HarmonyPatch(typeof(AbilitiesUtil),
28-
"GetAbilities",
29-
new Type[] { typeof(bool), typeof(bool), typeof(int), typeof(int), typeof(AbilityMetaCategory) }
30-
)]
27+
[HarmonyPatch(typeof(AbilitiesUtil), "GetAbilities",
28+
typeof(bool), typeof(bool), typeof(int), typeof(int), typeof(AbilityMetaCategory))]
3129
public class AbilitiesUtil_GetAbilities
3230
{
3331
public static void Postfix(
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using APIPlugin;
3+
using DiskCardGame;
4+
using HarmonyLib;
5+
using UnityEngine;
6+
7+
namespace API.Patches
8+
{
9+
[HarmonyPatch(typeof(Card), nameof(Card.AttachAbilities), typeof(CardInfo))]
10+
public class Card_AttachAbilities_NewSpecialAbilities
11+
{
12+
[HarmonyPrefix]
13+
public static bool Prefix(CardInfo info, Card __instance)
14+
{
15+
Plugin.Log.LogDebug(
16+
$"Called Card.AttachAbilities with [{info.name}] has [{info.specialAbilities.Count}] special abilities");
17+
18+
// if the card's special triggered ability exists in the NewSpecialAbility list,
19+
// then we loop to assign to the game object.
20+
// if the ability does not exist, return true running the original code
21+
if (NewSpecialAbility.specialAbilities.Exists(ability =>
22+
info.specialAbilities.Contains(ability.specialTriggeredAbility)))
23+
{
24+
foreach (var specialTriggeredAbility in info.specialAbilities)
25+
{
26+
NewSpecialAbility newAbility = NewSpecialAbility.specialAbilities
27+
.Find(x => x.specialTriggeredAbility == specialTriggeredAbility);
28+
Type type = newAbility.abilityBehaviour;
29+
Plugin.Log.LogInfo($"-> Type is [{type}]");
30+
Component baseC = __instance;
31+
SpecialCardBehaviour item = baseC.gameObject.GetComponent(type) as SpecialCardBehaviour
32+
?? baseC.gameObject.AddComponent(type) as SpecialCardBehaviour;
33+
}
34+
35+
return false;
36+
}
37+
38+
return true;
39+
}
40+
}
41+
}

Plugin.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using BepInEx;
2-
using BepInEx.Logging;
32
using BepInEx.Configuration;
4-
using HarmonyLib;
3+
using BepInEx.Logging;
54
using DiskCardGame;
5+
using HarmonyLib;
66
using UnityEngine.SceneManagement;
7+
78
#pragma warning disable 169
89

910
namespace APIPlugin
@@ -38,6 +39,7 @@ private void Awake()
3839
private void Start()
3940
{
4041
SetAbilityIdentifiers();
42+
SetSpecialAbilityIdentifiers();
4143
SetEvolveIdentifiers();
4244
SetIceCubeIdentifiers();
4345
SetTailIdentifiers();

0 commit comments

Comments
 (0)