Skip to content

Commit

Permalink
feat: unsupported callback serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Chillu1 committed Jun 27, 2024
1 parent d2b4963 commit 895e9f8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using ModiBuff.Core.Units;

namespace ModiBuff.Extensions.Serialization.Json
Expand All @@ -14,7 +15,11 @@ public SaveController(string fileName)
{
Path = Environment.CurrentDirectory;

_options = new JsonSerializerOptions { WriteIndented = true, IncludeFields = true };
_options = new JsonSerializerOptions
{
WriteIndented = true, IncludeFields = true,
//DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
};
}

public string Save<T>(T obj) => JsonSerializer.Serialize(obj, _options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ public SaveData SaveState()
{
if (_unsavableEffects.Count > 0)
{
var unsupportedInstructionEffects = _unsavableEffects
.Where(SpecialInstructionEffects.IsUnsupportedEffectType).ToArray();
var nonSpecialInstructionEffects = _unsavableEffects
.Where(e => !SpecialInstructionEffects.IsSpecialInstructionEffect(e)).ToArray();

if (unsupportedInstructionEffects.Length > 0)
Logger.LogError("[ModiBuff] Saving recipe with unsupported effects: " +
string.Join(", ", unsupportedInstructionEffects.Select(e => e.Name)));

if (nonSpecialInstructionEffects.Length > 0)
Logger.LogWarning("[ModiBuff] Saving recipe with unsavable effects, please implement " +
$"{nameof(ISaveableRecipeEffect)} for the following effects: " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace ModiBuff.Core
public static class SpecialInstructionEffects //TODO Rename
{
private static readonly HashSet<Type> effectTypes;
private static readonly HashSet<Type> unsupportedEffectTypes;

static SpecialInstructionEffects()
{
Expand All @@ -20,6 +21,15 @@ static SpecialInstructionEffects()
typeof(DispelRegisterEffect),
typeof(CallbackUnitRegisterEffect<>),
};

unsupportedEffectTypes = new HashSet<Type>(new TypeEqualityComparer())
{
typeof(CallbackRegisterEffect<>),
typeof(CallbackStateSaveRegisterEffect<,>),
typeof(CallbackEffectRegisterEffect<>),
typeof(CallbackStateEffectRegisterEffect<,>),
typeof(CallbackEffectRegisterEffectUnits<>),
};
}

public static bool AddSpecialInstructionEffect(Type effectType) => effectTypes.Add(effectType);
Expand All @@ -28,6 +38,10 @@ static SpecialInstructionEffects()
public static bool IsSpecialInstructionEffect<T>() where T : IEffect => IsSpecialInstructionEffect(typeof(T));
public static bool IsSpecialInstructionEffect(IEffect effect) => IsSpecialInstructionEffect(effect.GetType());

public static bool IsUnsupportedEffectType(Type effectType) => unsupportedEffectTypes.Contains(effectType);
public static bool IsUnsupportedEffectType<T>() where T : IEffect => IsUnsupportedEffectType(typeof(T));
public static bool IsUnsupportedEffectType(IEffect effect) => IsUnsupportedEffectType(effect.GetType());

/// <summary>
/// Checks that the types are of same type, if both types are generic, only checks the top level type.
/// </summary>
Expand Down

0 comments on commit 895e9f8

Please sign in to comment.