Skip to content

Commit

Permalink
Merge pull request #112 from Ewanderer/106-externalcardmeta
Browse files Browse the repository at this point in the history
106 externalcardmeta
  • Loading branch information
Ewanderer authored Dec 8, 2023
2 parents 3f3b4fa + 404c108 commit 699cc7f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
28 changes: 25 additions & 3 deletions CobaltCoreModding.Components/Services/CardRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,16 @@ internal static void PatchCardMetas()
{
IDictionary card_meta_dictionary = TypesAndEnums.DbType.GetField("cardMetas")?.GetValue(null) as IDictionary ?? throw new Exception("card meta dictionary not found");
var deck_field = TypesAndEnums.CardMetaType.GetField("deck") ?? throw new Exception();
var extra_glossary_field = TypesAndEnums.CardMetaType.GetField("extraGlossary") ?? throw new Exception();

foreach (var card in registered_cards.Values.Where(e => e != null && e.ActualDeck != null))
{
if (card == null) continue;

if (!card_meta_dictionary.Contains(card.CardType.Name))
continue;
{
//make new meta
var new_meta = Activator.CreateInstance(TypesAndEnums.CardMetaType);
card_meta_dictionary.Add(card.CardType.Name, new_meta);
}
var meta = card_meta_dictionary[card.CardType.Name];
var deck_val = TypesAndEnums.IntToDeck(card.ActualDeck?.Id);
if (deck_val == null)
Expand All @@ -207,6 +210,25 @@ internal static void PatchCardMetas()
deck_field.SetValue(meta, deck_val);
}

foreach (var card in registered_cards.Values.Where(e => e.ExtraGlossary.Any()))
{
if (!card_meta_dictionary.Contains(card.CardType.Name))
{
//make new meta
var new_meta = Activator.CreateInstance(TypesAndEnums.CardMetaType);
card_meta_dictionary.Add(card.CardType.Name, new_meta);
}
var meta = card_meta_dictionary[card.CardType.Name];
var old_val = extra_glossary_field.GetValue(meta) as IEnumerable<string>;
if (old_val == null)
{
Logger?.LogError("Cannot extract extra glossary from card meta for {0}", card.GlobalName);
continue;
}
var new_val = old_val.Concat(card.ExtraGlossary).ToArray();
extra_glossary_field.SetValue(meta, new_val);
}

foreach (var overwrite in card_overwrites.Where(e => e.Value != null && e.Value.ActualDeck != null))
{
var card = overwrite.Value;
Expand Down
33 changes: 32 additions & 1 deletion CobaltCoreModding.Definitions/ExternalItems/ExternalCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ public ExternalCard(string globalName, Type cardType, ExternalSprite cardArt, Ex
CardType = cardType;
CardArt = cardArt;
ActualDeck = actualDeck;
ExtraGlossary = Array.Empty<string>();
}

/// <summary>
///
/// </summary>
/// <param name="globalName"></param>
/// <param name="cardType"></param>
/// <param name="cardArt"></param>
/// <param name="actualDeck"></param>
/// <param name="extraGlossary"></param>
/// <exception cref="ArgumentException"></exception>
public ExternalCard(string globalName, Type cardType, ExternalSprite cardArt, ExternalDeck? actualDeck = null, IEnumerable<string>? extraGlossary = null)
{
if (string.IsNullOrWhiteSpace(globalName)) throw new ArgumentException("External card without global name");
if (cardArt.Id == null) throw new ArgumentException($"Card Art not registered in External Card {globalName}");
if (actualDeck != null && actualDeck.Id == null) throw new ArgumentException($"Unregistered External Deck in Card {globalName}");
GlobalName = globalName;
CardType = cardType;
CardArt = cardArt;
ActualDeck = actualDeck;
ExtraGlossary = extraGlossary?.ToArray() ?? Array.Empty<string>();
}

/// <summary>
Expand All @@ -31,10 +53,20 @@ public ExternalCard(string globalName, Type cardType, ExternalSprite cardArt, Ex
public ExternalDeck? ActualDeck { get; init; }

public ExternalSprite CardArt { get; init; }

public Type CardType { get; init; }

public string DescALocKey => "card." + CardType.Name + ".descA";

public string DescBLocKey => "card." + CardType.Name + ".descB";

public string DescLocKey => "card." + CardType.Name + ".desc";

/// <summary>
/// Glossary entries that will be added! to card meta.
/// </summary>
public IEnumerable<string> ExtraGlossary { get; init; }

public string GlobalName { get; init; }

public string NameLocKey => "card." + CardType.Name + ".name";
Expand Down Expand Up @@ -70,7 +102,6 @@ public void AddLocalisation(string name, string? desc = null, string? descA = nu

public void GenerateCardNamesFromResourceFile()
{

}

public void GetLocalisation(string locale, out string? name, out string? description, out string? descriptionA, out string? descriptionB)
Expand Down
28 changes: 28 additions & 0 deletions CobaltCoreModding.Definitions/ExternalItems/ExternalCardMeta.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace CobaltCoreModding.Definitions.ExternalItems
{
public class ExternalCardMeta
{
public ExternalCardMeta(string globalName, bool? dontOffer = null, bool? unreleased = null, bool? dontLoc = null, bool? weirdCard = null, int? deck = null, int? rarity = null, int[]? upgradesTo = null, string[]? extraGlossary = null)
{
GlobalName = globalName;
DontOffer = dontOffer;
Unreleased = unreleased;
DontLoc = dontLoc;
WeirdCard = weirdCard;
UpgradesTo = upgradesTo;
Deck = deck;
Rarity = rarity;
ExtraGlossary = extraGlossary;
}

public int? Deck { get; init; }
public bool? DontLoc { get; init; }
public bool? DontOffer { get; init; }
public string[]? ExtraGlossary { get; init; }
public string GlobalName { get; init; }
public int? Rarity { get; init; }
public bool? Unreleased { get; init; }
public int[]? UpgradesTo { get; init; }
public bool? WeirdCard { get; init; }
}
}
2 changes: 1 addition & 1 deletion DemoMod/ModManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void LoadManifest(ICardRegistry registry)
if (card_art_sprite == null)
return;
//make card meta data
var card = new ExternalCard("Ewanderer.DemoMod.DemoCard", typeof(EWandererDemoCard), card_art_sprite, null);
var card = new ExternalCard("Ewanderer.DemoMod.DemoCard", typeof(EWandererDemoCard), card_art_sprite, null, new string[] { "status.stun" });
//add card name in english
card.AddLocalisation(addin?.tbValue.Text ?? "Schwarzmagier");
//register card in the db extender.
Expand Down

0 comments on commit 699cc7f

Please sign in to comment.