Skip to content

Commit

Permalink
Editor: RuntimeSetup clones properties directly
Browse files Browse the repository at this point in the history
  • Loading branch information
ericoporto committed Dec 13, 2023
1 parent da4f295 commit 7cee81f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
10 changes: 1 addition & 9 deletions Editor/AGS.Types/RuntimeSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,16 +425,8 @@ public void FromXml(XmlNode node)

public RuntimeSetup Clone()
{
XmlDocument doc = new XmlDocument();
using (StringWriter stringWriter = new StringWriter())
{
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
this.ToXml(xmlWriter);
doc.LoadXml(stringWriter.ToString());
}

RuntimeSetup clone = new RuntimeSetup(_gameSettings);
clone.FromXml(doc);
Utilities.NaiveCopyProperties(this, clone);
return clone;
}

Expand Down
14 changes: 13 additions & 1 deletion Editor/AGS.Types/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
using System.IO;
using System.Text;
using System.Reflection;

using System.Linq;

namespace AGS.Types
{
public static class Utilities
Expand Down Expand Up @@ -211,5 +212,16 @@ public static Encoding EncodingFromName(string name)
return Utilities.UTF8; // UTF-8 w/o BOM
return Encoding.GetEncoding(name);
}

public static void NaiveCopyProperties(object source_obj, object clone)
{
IEnumerable<PropertyInfo> properties = source_obj.GetType().GetProperties()
.Where(f => f.CanWrite && f.CanRead );

foreach (PropertyInfo prop in properties)
{
prop.SetValue(clone, prop.GetValue(source_obj));
}
}
}
}

0 comments on commit 7cee81f

Please sign in to comment.