Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace CedLib
- {
- namespace Persistence
- {
- namespace XMLPersistenceDictionary
- {
- using System.Xml.Serialization;
- [Serializable]
- public class XMLPersistenceDictionary : ICollection, IEnumerable<savenode>
- {
- public Logging logger;
- public List<savenode> innerdict;
- public XMLPersistenceDictionary(Logging _logger)
- {
- logger = _logger;
- innerdict = new List<savenode>();
- logger.log("Initialized new XMLPersistence dictionary", Logging.Priority.Info);
- }
- public XMLPersistenceDictionary()
- {
- innerdict = new List<savenode>();
- }
- public bool ContainsKey(string key)
- {
- return innerdict.Exists(s => s.name == key);
- }
- public bool ContainsValue(string value)
- {
- return innerdict.Exists(s => s.obj == value);
- }
- public void Add(savenode value)
- {
- if (logger != null)
- logger.log("Adding new dictionary item: " + value.name, CedLib.Logging.Priority.Notice);
- innerdict.Add(value);
- }
- public void Add(string name, string obj)
- {
- savenode value = new savenode(name, obj);
- if (logger != null)
- logger.log("Adding new dictionary item: " + value.name, CedLib.Logging.Priority.Notice);
- innerdict.Add(value);
- }
- public void AddRange(savenode[] savenodes)
- {
- logger.log("Adding range of items", Logging.Priority.Notice);
- foreach (var savenode in savenodes)
- {
- if (logger != null)
- logger.log("Adding new dictionary item: " + savenode.name, CedLib.Logging.Priority.Notice);
- innerdict.Add(savenode);
- }
- }
- public void Remove(savenode value)
- {
- if (logger != null)
- logger.log("Removing dictionary item: " + value.name, CedLib.Logging.Priority.Notice);
- innerdict.Remove(value);
- }
- public void Remove(string value)
- {
- if (logger != null)
- logger.log("Removing dictionary item: " + value, CedLib.Logging.Priority.Notice);
- innerdict.Remove(innerdict.Find(s => s.name == value));
- }
- public savenode[] ToSavenodeArray()
- {
- return innerdict.ToArray();
- }
- public int Count
- {
- get { return innerdict.Count; }
- }
- #region IEnumerable shit
- void ICollection.CopyTo(Array myArr, int index)
- {
- foreach (savenode i in innerdict)
- {
- myArr.SetValue(i, index);
- index++;
- }
- }
- public IEnumerator<savenode> GetEnumerator()
- {
- return (IEnumerator<savenode>)innerdict.GetEnumerator();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- // Defer to generic version
- return GetEnumerator();
- }
- bool ICollection.IsSynchronized
- {
- get
- {
- return true;
- }
- }
- object ICollection.SyncRoot
- {
- get
- {
- return this;
- }
- }
- int ICollection.Count
- {
- get
- {
- return innerdict.Count;
- }
- }
- #endregion
- public savenode this[string index]
- {
- get { return innerdict.Find(s => s.name == index); }
- set
- {
- if (innerdict.Count > 0)
- innerdict[innerdict.FindIndex(s => s.name == index)] = value;
- else
- Add(value);
- }
- }
- public savenode this[int index]
- {
- get { return innerdict[index]; }
- set { innerdict[index] = value; }
- }
- public bool save(string filename)
- {
- XmlSerializer serializer = new XmlSerializer(typeof(List<savenode>));
- System.IO.FileStream fstream = new System.IO.FileStream(filename, System.IO.FileMode.OpenOrCreate);
- serializer.Serialize(fstream, innerdict);
- fstream.Flush();
- fstream.Close();
- return true;
- }
- public bool load(string filename)
- {
- XmlSerializer serializer = new XmlSerializer(typeof(List<savenode>));
- System.IO.FileStream fstream = new System.IO.FileStream(filename, System.IO.FileMode.Open);
- innerdict = (List<savenode>)serializer.Deserialize(fstream);
- fstream.Flush();
- fstream.Close();
- return true;
- }
- }
- [Serializable]
- public class savenode
- {
- public savenode(string _name, string _object)
- {
- name = _name;
- obj = _object;
- Console.WriteLine("New node: " + _name);
- }
- public savenode(string _name, string _object, XMLPersistenceDictionary _childnodes)
- {
- name = _name;
- obj = _object;
- childnodes = _childnodes;
- Console.WriteLine("New node: " + _name);
- }
- public savenode() { }
- public override string ToString()
- {
- return name + ":" + obj;
- }
- public void Add(object snode)
- {
- if (childnodes == null)
- childnodes = new XMLPersistenceDictionary();
- childnodes.Add((savenode)snode);
- }
- public void Add(savenode snode)
- {
- if (childnodes == null)
- childnodes = new XMLPersistenceDictionary();
- childnodes.Add(snode);
- }
- public void Add(string name,string obj)
- {
- if (childnodes == null)
- childnodes = new XMLPersistenceDictionary();
- childnodes.Add(name,obj);
- }
- public string name { get; set; }
- public string obj { get; set; }
- [XmlArray("childnodes")]
- public XMLPersistenceDictionary childnodes;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement