Guest User

Untitled

a guest
Jan 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Serialization;
  6. using System.IO;
  7. using System.Collections;
  8.  
  9. namespace Hanafuda.Core
  10. {
  11. public abstract class Configuration
  12. {
  13. public Configuration()
  14. {
  15. this.configuration = new Dictionary<string, object>();
  16. }
  17.  
  18. public Configuration(IDictionary<string, object> configuration)
  19. {
  20. this.configuration = new Dictionary<string, object>(configuration);
  21. }
  22.  
  23. public string DefaultSavePath
  24. {
  25. get;
  26. set;
  27. }
  28.  
  29. public object this[string key]
  30. {
  31. get
  32. {
  33. lock (configuration)
  34. {
  35. return configuration[key];
  36. }
  37. }
  38.  
  39. set
  40. {
  41. lock (configuration)
  42. {
  43. configuration[key] = value;
  44. }
  45. }
  46. }
  47.  
  48. public bool ContainsKey(string key)
  49. {
  50. lock (configuration)
  51. {
  52. return configuration.ContainsKey(key);
  53. }
  54. }
  55.  
  56. public virtual void Save(string path)
  57. {
  58. using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
  59. {
  60. // This is a retarded hack
  61. var values = configuration.Select(kvp => new ShitNugget(kvp.Key, kvp.Value)).ToArray();
  62.  
  63. XmlSerializer serializer = new XmlSerializer(values.GetType());
  64. serializer.Serialize(stream, values);
  65. }
  66. }
  67.  
  68. public virtual void Load(string path)
  69. {
  70. FileInfo file = new FileInfo(path);
  71. if (!file.Exists)
  72. return;
  73.  
  74. using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
  75. {
  76. XmlSerializer serializer = new XmlSerializer(typeof(ShitNugget[]));
  77.  
  78. this.configuration = new Dictionary<string,object>();
  79. foreach (var kvp in (ShitNugget[])serializer.Deserialize(stream))
  80. configuration.Add (kvp.Key, kvp.Value);
  81. }
  82. }
  83.  
  84. private Dictionary<string, object> configuration;
  85. }
  86.  
  87. public class ShitNugget
  88. {
  89. public ShitNugget() { }
  90.  
  91. public ShitNugget(string key, object value)
  92. {
  93. this.Key = key;
  94. this.Value = value;
  95. }
  96.  
  97. public string Key;
  98. public object Value;
  99. }
  100. }
Add Comment
Please, Sign In to add comment