Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml.Serialization;
- using System.IO;
- using System.Collections;
- namespace Hanafuda.Core
- {
- public abstract class Configuration
- {
- public Configuration()
- {
- this.configuration = new Dictionary<string, object>();
- }
- public Configuration(IDictionary<string, object> configuration)
- {
- this.configuration = new Dictionary<string, object>(configuration);
- }
- public string DefaultSavePath
- {
- get;
- set;
- }
- public object this[string key]
- {
- get
- {
- lock (configuration)
- {
- return configuration[key];
- }
- }
- set
- {
- lock (configuration)
- {
- configuration[key] = value;
- }
- }
- }
- public bool ContainsKey(string key)
- {
- lock (configuration)
- {
- return configuration.ContainsKey(key);
- }
- }
- public virtual void Save(string path)
- {
- using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
- {
- // This is a retarded hack
- var values = configuration.Select(kvp => new ShitNugget(kvp.Key, kvp.Value)).ToArray();
- XmlSerializer serializer = new XmlSerializer(values.GetType());
- serializer.Serialize(stream, values);
- }
- }
- public virtual void Load(string path)
- {
- FileInfo file = new FileInfo(path);
- if (!file.Exists)
- return;
- using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
- {
- XmlSerializer serializer = new XmlSerializer(typeof(ShitNugget[]));
- this.configuration = new Dictionary<string,object>();
- foreach (var kvp in (ShitNugget[])serializer.Deserialize(stream))
- configuration.Add (kvp.Key, kvp.Value);
- }
- }
- private Dictionary<string, object> configuration;
- }
- public class ShitNugget
- {
- public ShitNugget() { }
- public ShitNugget(string key, object value)
- {
- this.Key = key;
- this.Value = value;
- }
- public string Key;
- public object Value;
- }
- }
Add Comment
Please, Sign In to add comment