using Tango.Editors;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
///
///
/// A collection of extension methods.
///
public static class IConfigurableExtensions
{
///
/// Applies the specified configuration file with an optional animation if supported by the configurable type.
///
/// The file path to where a configuration has been saved.
/// The animation.
public static void SetConfiguration(this IConfigurable configurable, String filePath, ConfigurationAnimation animation)
{
IConfiguration config = null;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
BinaryFormatter f = new BinaryFormatter();
config = f.Deserialize(fs) as IConfiguration;
}
if (config != null)
{
configurable.SetConfiguration(config, animation);
}
else
{
throw new ArgumentException("The specified file path does not contain any saved configuration.");
}
}
///
/// Applies the specified configuration stream with an optional animation if supported by the configurable type.
///
/// The stream where a configuration has been saved.
/// The animation.
public static void SetConfiguration(this IConfigurable configurable, Stream stream, ConfigurationAnimation animation)
{
IConfiguration config = null;
BinaryFormatter f = new BinaryFormatter();
config = f.Deserialize(stream) as IConfiguration;
if (config != null)
{
configurable.SetConfiguration(config, animation);
}
else
{
throw new ArgumentException("The specified stream does not contain any saved configuration.");
}
}
///
/// Applies the specified configuration with no animation.
///
/// The configurable.
/// The configuration.
public static void SetConfiguration(this IConfigurable configurable, IConfiguration configuration)
{
configurable.SetConfiguration(configuration, new ConfigurationAnimation());
}
///
/// Clones this configurable.
///
/// The configurable instance.
///
public static IConfigurable Clone(this IConfigurable configurable)
{
IConfigurable cloned = Activator.CreateInstance(configurable.GetType()) as IConfigurable;
cloned.SetConfiguration(configurable.GetConfiguration());
try
{
configurable.GetType().GetProperty("ID").SetValue(cloned, Guid.NewGuid().ToString());
}
catch { }
return cloned;
}
}