Example #1
0
        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);
            }
        }
Example #2
0
            public void ReadFromNbtCompound(INbtIoCapable target, NbtCompound compound)
            {
                foreach (var entry in _nbtEntries)
                {
                    var result = compound.TryGet(entry.Attribute.TagName ?? entry.Field.Name, out var tag);
                    if (!result)
                    {
                        if (!entry.Attribute.Optional)
                        {
                            ExceptionHelper.ThrowParseMissingError(entry.Field.Name, ParseErrorLevel.Exception);
                        }

                        continue;
                    }

                    SetValueToClassMember(tag, entry, target);
                }
            }
Example #3
0
 /// <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);
 }