public static T CreateAndReadFromNbt <T>(NbtCompound root, INbtIoConfig config = null) where T : INbtIoCapable, new() { var obj = new T(); ReadFromNbt(obj, typeof(T), root, config); return(obj); }
public static object CreateAndReadFromNbt(Type type, NbtCompound root, INbtIoConfig config = null) { if (type == typeof(NbtCompound)) { return(root); } var obj = (INbtIoCapable)Activator.CreateInstance(type, true); ReadFromNbt(obj, type, root, config); return(obj); }
private static void ReadFromNbt(INbtIoCapable target, Type type, NbtCompound root, INbtIoConfig config = null) { if (target == null) { throw new ArgumentException(nameof(target)); } if (target is INbtCustomReader reader) { reader.Read(null, root); return; } var baseType = type.BaseType; if (baseType != null && !baseType.Equals(typeof(object)) && typeof(INbtIoCapable).IsAssignableFrom(baseType)) { ReadFromNbt(target, baseType, root, config); } GetProcessor(type).ReadFromNbtCompound(target, root); if (target is INbtPostRead postreader) { postreader.PostRead(null, root); } }
public static List <T> ToFrameworkList <T>(this NbtList list, INbtIoConfig config = null) where T : INbtIoCapable, new() { return(new List <T>(list.OfType <NbtCompound>().Select(c => CreateAndReadFromNbt <T>(c, config)))); }
/// <summary> /// Fill fields and properties decorated with <see cref="NbtEntryAttribute"/> with information from Nbt storage. /// Raise exceptions accordingly. /// Use fields if possible, because getter/setter may not exist for properties even if they defined in the code. /// </summary> /// <param name="target">Object to be filled</param> /// <param name="root">Nbt storage</param> public static void ReadFromNbt(INbtIoCapable target, NbtCompound root, INbtIoConfig config = null) { ReadFromNbt(target, target.GetType(), root, config); }