Example #1
0
        private void CopySaveInfoToStructure(JObject saveData, SaveInformation saveInfo)
        {
            var info = saveData[nameof(Save.Info)] ?? throw new JsonException("Save is missing info field");

            foreach (var property in BaseThriveConverter.PropertiesOf(saveInfo))
            {
                info[property.Name] = JToken.FromObject(property.GetValue(saveInfo));
            }
        }
Example #2
0
        private void CopySaveInfoToStructure(JObject saveData, SaveInformation saveInfo)
        {
            var info = saveData[nameof(Save.Info)];

            foreach (var property in BaseThriveConverter.PropertiesOf(saveInfo))
            {
                info[property.Name] = JToken.FromObject(property.GetValue(saveInfo));
            }
        }
Example #3
0
    /// <summary>
    ///   Copies the properties and fields that the Thrive JSON converter saves into JSON
    /// </summary>
    /// <remarks>
    ///   <para>
    ///     This doesn't clone cloneable properties, so the source object should not be used anymore after this as
    ///     reference properties can get changed in it after target starts running.
    ///   </para>
    ///   <para>
    ///     TODO: make this respect AssignOnlyChildItemsOnDeserializeAttribute to handle nested such objects
    ///   </para>
    /// </remarks>
    /// <param name="target">Object to set the properties on</param>
    /// <param name="source">Object to copy things from</param>
    /// <param name="ignoreMembers">Member names to skip copying</param>
    public static void CopyJSONSavedPropertiesAndFields(object target, object source,
                                                        List <string>?ignoreMembers = null)
    {
        ignoreMembers ??= new List <string>();

        var type = target.GetType();

        // TODO: should this verify that source.GetType() == type?

        foreach (var field in BaseThriveConverter.FieldsOf(target))
        {
            if (IsNameLoadedFromSaveName(field.Name) || BaseThriveConverter.IsIgnoredGodotMember(field.Name, type))
            {
                continue;
            }

            if (ignoreMembers.Contains(field.Name))
            {
                continue;
            }

            field.SetValue(target, field.GetValue(source));
        }

        foreach (var property in BaseThriveConverter.PropertiesOf(target))
        {
            if (IsNameLoadedFromSaveName(property.Name) ||
                BaseThriveConverter.IsIgnoredGodotMember(property.Name, type))
            {
                continue;
            }

            if (ignoreMembers.Contains(property.Name))
            {
                continue;
            }

            var set = property.GetSetMethodOnDeclaringType() ??
                      throw new InvalidOperationException($"Could not find property setter for {property.Name}");

            set.Invoke(target, new[] { property.GetValue(source) });
        }
    }
Example #4
0
    /// <summary>
    ///   Copies the properties and fields that the Thrive JSON converter saves into JSON
    /// </summary>
    /// <remarks>
    ///   <para>
    ///     This doesn't clone cloneable properties, so the source object should not be used anymore after this as
    ///     reference properties can get changed in it after target starts running.
    ///   </para>
    /// </remarks>
    /// <param name="target">Object to set the properties on</param>
    /// <param name="source">Object to copy things from</param>
    /// <param name="ignoreMembers">Member names to skip copying</param>
    /// <typeparam name="T">The type of object to handle</typeparam>
    public static void CopyJSONSavedPropertiesAndFields <T>(T target, T source, List <string> ignoreMembers = null)
    {
        if (ignoreMembers == null)
        {
            ignoreMembers = new List <string>();
        }

        var type = typeof(T);

        foreach (var field in BaseThriveConverter.FieldsOf(target))
        {
            if (IsNameLoadedFromSaveName(field.Name) || BaseThriveConverter.IsIgnoredGodotMember(field.Name, type))
            {
                continue;
            }

            if (ignoreMembers.Contains(field.Name))
            {
                continue;
            }

            field.SetValue(target, field.GetValue(source));
        }

        foreach (var property in BaseThriveConverter.PropertiesOf(target))
        {
            if (IsNameLoadedFromSaveName(property.Name) ||
                BaseThriveConverter.IsIgnoredGodotMember(property.Name, type))
            {
                continue;
            }

            if (ignoreMembers.Contains(property.Name))
            {
                continue;
            }

            var set = property.GetSetMethodOnDeclaringType();

            set.Invoke(target, new object[] { property.GetValue(source) });
        }
    }