Make alterations to libzni
This commit is contained in:
parent
7b776ecfea
commit
0638f7e642
19 changed files with 1824 additions and 183 deletions
15
Serialization/Serializable.cs
Normal file
15
Serialization/Serializable.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
using LibZNI.Serialization.ZNIFile;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization
|
||||
{
|
||||
public abstract class Serializable
|
||||
{
|
||||
public abstract void save(Folder f);
|
||||
public abstract void load(Folder f);
|
||||
}
|
||||
}
|
58
Serialization/TagIO.cs
Normal file
58
Serialization/TagIO.cs
Normal file
|
@ -0,0 +1,58 @@
|
|||
using LibZNI.Serialization.ZNIFile;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// This class contains helper functions for interacting with streams and files that are encoded using ZNIFile's structure
|
||||
/// Additionally, this provides a overlay as it interacts with the final data set, and can compress and or encrypt.
|
||||
/// </summary>
|
||||
public static class TagIO
|
||||
{
|
||||
public static void WriteOnStream(Stream s, Tag x)
|
||||
{
|
||||
BinaryWriter bw = new BinaryWriter(s);
|
||||
x.WriteTag(bw);
|
||||
}
|
||||
|
||||
public static Folder ReadFromStream(Stream s)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
Folder folder = new Folder();
|
||||
BinaryReader br = new BinaryReader(s);
|
||||
TagType type = (TagType)br.ReadInt32();
|
||||
if (type == TagType.FOLDER)
|
||||
{
|
||||
// Read the file!
|
||||
folder.ReadTag(br);
|
||||
}
|
||||
|
||||
return folder;
|
||||
}catch(Exception e)
|
||||
{
|
||||
return new Folder();
|
||||
}
|
||||
}
|
||||
|
||||
public static void SaveToFile(string FileName, Tag x)
|
||||
{
|
||||
FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
|
||||
WriteOnStream(fs, x);
|
||||
fs.Close();
|
||||
}
|
||||
public static Folder ReadFromFile(string FileName)
|
||||
{
|
||||
FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
|
||||
Folder f = ReadFromStream(fs);
|
||||
fs.Close();
|
||||
return f;
|
||||
}
|
||||
}
|
||||
}
|
74
Serialization/ZNIFile/BoolTag.cs
Normal file
74
Serialization/ZNIFile/BoolTag.cs
Normal file
|
@ -0,0 +1,74 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization.ZNIFile
|
||||
{
|
||||
public class BoolTag : Tag
|
||||
{
|
||||
private bool BoolVal;
|
||||
public bool Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return BoolVal;
|
||||
}
|
||||
}
|
||||
|
||||
public BoolTag(string _Name, bool val)
|
||||
{
|
||||
Name = _Name;
|
||||
BoolVal = val;
|
||||
}
|
||||
public BoolTag(bool boolVal) : this(null, boolVal)
|
||||
{
|
||||
}
|
||||
public BoolTag() : this(null, false) { }
|
||||
|
||||
public override TagType Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return TagType.BOOL;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool ReadTag(BinaryReader br)
|
||||
{
|
||||
Name = br.ReadString();
|
||||
BoolVal = br.ReadBoolean();
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void Rename(string old, string newName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SkipTag(BinaryReader br)
|
||||
{
|
||||
_ = new BoolTag().ReadTag(br);
|
||||
}
|
||||
|
||||
public override void WriteData(BinaryWriter bw)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void WriteTag(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(((int)Type));
|
||||
bw.Write(Name);
|
||||
|
||||
bw.Write(Value);
|
||||
}
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new BoolTag(Name, Value);
|
||||
}
|
||||
}
|
||||
}
|
76
Serialization/ZNIFile/ByteArrayTag.cs
Normal file
76
Serialization/ZNIFile/ByteArrayTag.cs
Normal file
|
@ -0,0 +1,76 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization.ZNIFile
|
||||
{
|
||||
public class ByteArrayTag : Tag
|
||||
{
|
||||
private byte[] BArrVal;
|
||||
public byte[] Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return BArrVal;
|
||||
}
|
||||
}
|
||||
|
||||
public ByteArrayTag(string _Name, byte[] val)
|
||||
{
|
||||
Name = _Name;
|
||||
BArrVal = val;
|
||||
}
|
||||
public ByteArrayTag(byte[] boolVal) : this(null, boolVal)
|
||||
{
|
||||
}
|
||||
public ByteArrayTag() : this(null, new byte[] {}) { }
|
||||
|
||||
public override TagType Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return TagType.BYTEARRAY;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool ReadTag(BinaryReader br)
|
||||
{
|
||||
Name = br.ReadString();
|
||||
int count = br.ReadInt32();
|
||||
BArrVal = br.ReadBytes(count);
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void Rename(string old, string newName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SkipTag(BinaryReader br)
|
||||
{
|
||||
_ = new BoolTag().ReadTag(br);
|
||||
}
|
||||
|
||||
public override void WriteData(BinaryWriter bw)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void WriteTag(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(((int)Type));
|
||||
bw.Write(Name);
|
||||
bw.Write(Value.Length);
|
||||
|
||||
bw.Write(Value);
|
||||
}
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new ByteArrayTag(Name, Value);
|
||||
}
|
||||
}
|
||||
}
|
74
Serialization/ZNIFile/ByteTag.cs
Normal file
74
Serialization/ZNIFile/ByteTag.cs
Normal file
|
@ -0,0 +1,74 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization.ZNIFile
|
||||
{
|
||||
public class ByteTag : Tag
|
||||
{
|
||||
private byte ByteVal;
|
||||
public byte Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return ByteVal;
|
||||
}
|
||||
}
|
||||
|
||||
public ByteTag(string _Name, byte val)
|
||||
{
|
||||
Name = _Name;
|
||||
ByteVal = val;
|
||||
}
|
||||
public ByteTag(byte _ByteVal) : this(null, _ByteVal)
|
||||
{
|
||||
}
|
||||
public ByteTag() : this(null, 0) { }
|
||||
|
||||
public override TagType Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return TagType.BYTE;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool ReadTag(BinaryReader br)
|
||||
{
|
||||
Name = br.ReadString();
|
||||
ByteVal = br.ReadByte();
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void Rename(string old, string newName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SkipTag(BinaryReader br)
|
||||
{
|
||||
_ = new ByteTag().ReadTag(br);
|
||||
}
|
||||
|
||||
public override void WriteData(BinaryWriter bw)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void WriteTag(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(((int)Type));
|
||||
bw.Write(Name);
|
||||
|
||||
bw.Write(Value);
|
||||
}
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new ByteTag(Name, Value);
|
||||
}
|
||||
}
|
||||
}
|
70
Serialization/ZNIFile/DoubleTag.cs
Normal file
70
Serialization/ZNIFile/DoubleTag.cs
Normal file
|
@ -0,0 +1,70 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization.ZNIFile
|
||||
{
|
||||
public class DoubleTag : Tag
|
||||
{
|
||||
public override TagType Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return TagType.DOUBLE;
|
||||
}
|
||||
}
|
||||
|
||||
private double DoubleVal;
|
||||
public double Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return DoubleVal;
|
||||
}
|
||||
}
|
||||
|
||||
public DoubleTag() : this(null, 0) { }
|
||||
public DoubleTag(double Val) : this(null, Val) { }
|
||||
public DoubleTag(string DName, double val)
|
||||
{
|
||||
Name = DName;
|
||||
DoubleVal = val;
|
||||
}
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new DoubleTag(Name, Value);
|
||||
}
|
||||
|
||||
public override bool ReadTag(BinaryReader br)
|
||||
{
|
||||
Name = br.ReadString();
|
||||
DoubleVal = br.ReadDouble();
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void Rename(string old, string newName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SkipTag(BinaryReader br)
|
||||
{
|
||||
_ = new DoubleTag().ReadTag(br);
|
||||
}
|
||||
|
||||
public override void WriteData(BinaryWriter bw)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void WriteTag(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(Name);
|
||||
bw.Write(Value);
|
||||
}
|
||||
}
|
||||
}
|
74
Serialization/ZNIFile/FloatTag.cs
Normal file
74
Serialization/ZNIFile/FloatTag.cs
Normal file
|
@ -0,0 +1,74 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization.ZNIFile
|
||||
{
|
||||
public class FloatTag : Tag
|
||||
{
|
||||
private float FloatVal;
|
||||
public float Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return FloatVal;
|
||||
}
|
||||
}
|
||||
|
||||
public FloatTag(string _Name, float val)
|
||||
{
|
||||
Name = _Name;
|
||||
FloatVal = val;
|
||||
}
|
||||
public FloatTag(float _FloatVal) : this(null, _FloatVal)
|
||||
{
|
||||
}
|
||||
public FloatTag() : this(null, 0f) { }
|
||||
|
||||
public override TagType Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return TagType.FLOAT;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool ReadTag(BinaryReader br)
|
||||
{
|
||||
Name = br.ReadString();
|
||||
FloatVal = br.ReadSingle();
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void Rename(string old, string newName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SkipTag(BinaryReader br)
|
||||
{
|
||||
_ = new FloatTag().ReadTag(br);
|
||||
}
|
||||
|
||||
public override void WriteData(BinaryWriter bw)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void WriteTag(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(((int)Type));
|
||||
bw.Write(Name);
|
||||
|
||||
bw.Write(Value);
|
||||
}
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new FloatTag(Name, Value);
|
||||
}
|
||||
}
|
||||
}
|
241
Serialization/ZNIFile/Folder.cs
Normal file
241
Serialization/ZNIFile/Folder.cs
Normal file
|
@ -0,0 +1,241 @@
|
|||
using Microsoft.VisualBasic;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization.ZNIFile
|
||||
{
|
||||
public class Folder : Tag, ICollection<Tag>, ICollection
|
||||
{
|
||||
public Folder() { }
|
||||
public Folder(string name)
|
||||
{
|
||||
this.Name = name;
|
||||
}
|
||||
public Collection<Tag> Tags { get; set; } = new Collection<Tag>();
|
||||
public override TagType Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return TagType.FOLDER;
|
||||
}
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return Tags.Count;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsReadOnly
|
||||
{
|
||||
get
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public object SyncRoot
|
||||
{
|
||||
get
|
||||
{
|
||||
return (Tags as ICollection).SyncRoot;
|
||||
}
|
||||
}
|
||||
|
||||
bool ICollection.IsSynchronized
|
||||
{
|
||||
get
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(Tag item)
|
||||
{
|
||||
if (item == null) throw new Exception("Bad item!");
|
||||
|
||||
Tags.Add(item);
|
||||
item.Parent = this;
|
||||
}
|
||||
|
||||
public override Tag this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
return Tags.ElementAt(index);
|
||||
}
|
||||
set
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
string TagName = Tags.ElementAt(index).Name;
|
||||
this[TagName] = value;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Tags.Add(value);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
public override Tag this[string index]
|
||||
{
|
||||
get
|
||||
{
|
||||
return Tags.Where(x => x.Name == index).FirstOrDefault();
|
||||
}
|
||||
set
|
||||
{
|
||||
if(Tags.Select(x=>x.Name == index).Count() != 0)
|
||||
{
|
||||
Tags.RemoveAt(Tags.IndexOf(Tags.Where(x => x.Name == index).FirstOrDefault()));
|
||||
}
|
||||
|
||||
Tags.Add(value);
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
foreach(Tag t in Tags)
|
||||
{
|
||||
t.Parent = null;
|
||||
}
|
||||
Tags = new Collection<Tag>();
|
||||
}
|
||||
|
||||
public bool Contains(Tag item)
|
||||
{
|
||||
return Tags.Contains(item);
|
||||
}
|
||||
|
||||
public void CopyTo(Tag[] array, int arrayIndex)
|
||||
{
|
||||
Tags.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public void CopyTo(Array array, int index)
|
||||
{
|
||||
CopyTo((Tag[])array, index);
|
||||
}
|
||||
|
||||
public IEnumerator<Tag> GetEnumerator()
|
||||
{
|
||||
return Tags.GetEnumerator();
|
||||
}
|
||||
|
||||
public bool Remove(Tag item)
|
||||
{
|
||||
return Tags.Remove(item);
|
||||
}
|
||||
|
||||
|
||||
public override bool ReadTag(BinaryReader br)
|
||||
{
|
||||
if(Parent != null)
|
||||
{
|
||||
SkipTag(br);
|
||||
return false;
|
||||
}
|
||||
Name = br.ReadString(); // Per ZNIFile standards, each tag reads its own name!
|
||||
|
||||
while(true)
|
||||
{
|
||||
TagType next = (TagType)br.ReadInt32();
|
||||
Tag _next = null;
|
||||
switch (next)
|
||||
{
|
||||
case TagType.FOLDER:
|
||||
_next = new Folder();
|
||||
break;
|
||||
case TagType.BOOL:
|
||||
_next = new BoolTag();
|
||||
break;
|
||||
case TagType.BYTE:
|
||||
_next = new ByteTag();
|
||||
break;
|
||||
case TagType.DOUBLE:
|
||||
_next = new DoubleTag();
|
||||
break;
|
||||
case TagType.FLOAT:
|
||||
_next = new FloatTag();
|
||||
break;
|
||||
case TagType.INTEGER:
|
||||
_next = new IntTag();
|
||||
break;
|
||||
case TagType.LIST:
|
||||
_next = new ListTag();
|
||||
break;
|
||||
case TagType.LONG:
|
||||
_next = new LongTag();
|
||||
break;
|
||||
case TagType.STRING:
|
||||
_next = new StringTag();
|
||||
break;
|
||||
case TagType.END:
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_next.ReadTag(br))
|
||||
{
|
||||
Tags.Add(_next);
|
||||
}
|
||||
_next.Parent = this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public override void SkipTag(BinaryReader br)
|
||||
{
|
||||
_ = new Folder().ReadTag(br);
|
||||
}
|
||||
|
||||
public override void WriteData(BinaryWriter bw)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void WriteTag(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(((int)Type)); // Write int (0), folder
|
||||
bw.Write(Name);
|
||||
|
||||
foreach (Tag t in Tags)
|
||||
{
|
||||
t.WriteTag(bw);
|
||||
}
|
||||
bw.Write(((int)TagType.END));
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public override void Rename(string old, string newName)
|
||||
{
|
||||
// do nothing. The folder's collection will be automatically updated.
|
||||
}
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Folder(this);
|
||||
}
|
||||
|
||||
public Folder(Folder existing)
|
||||
{
|
||||
Name = existing.Name;
|
||||
Tags = new Collection<Tag>(Tags.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
52
Serialization/ZNIFile/Header.cs
Normal file
52
Serialization/ZNIFile/Header.cs
Normal file
|
@ -0,0 +1,52 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization.ZNIFile
|
||||
{
|
||||
public class Header : Serializable
|
||||
{
|
||||
public const string SIGNATURE = "ZNIFile";
|
||||
public Version VERSION = new Version(1,0,0,1,0,0);
|
||||
|
||||
public override void load(Folder f)
|
||||
{
|
||||
|
||||
Folder x = f["Header"] as Folder;
|
||||
if (x == null) return;
|
||||
|
||||
if (x["Signature"].StringValue == SIGNATURE)
|
||||
{
|
||||
Version ver = new Version();
|
||||
ver.load(f);
|
||||
if (VERSION.Compare(ver) == 0)
|
||||
{
|
||||
return;
|
||||
} else throw new VersionNumberDifferentException(VERSION, ver);
|
||||
|
||||
}else
|
||||
{
|
||||
throw new Exception("Header failed validation");
|
||||
}
|
||||
}
|
||||
|
||||
public override void save(Folder f)
|
||||
{
|
||||
Folder x = new Folder("Header");
|
||||
x.Add(new StringTag("Signature", SIGNATURE));
|
||||
VERSION.save(x);
|
||||
|
||||
f.Add(x);
|
||||
}
|
||||
|
||||
public static Folder GetHeader()
|
||||
{
|
||||
Folder f = new Folder("temp"); // Initialize a temporary header
|
||||
Header x = new Header();
|
||||
x.save(f);
|
||||
return f["Header"] as Folder;
|
||||
}
|
||||
}
|
||||
}
|
72
Serialization/ZNIFile/IntTag.cs
Normal file
72
Serialization/ZNIFile/IntTag.cs
Normal file
|
@ -0,0 +1,72 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization.ZNIFile
|
||||
{
|
||||
public class IntTag : Tag
|
||||
{
|
||||
|
||||
public IntTag() : this(null, 0)
|
||||
{ }
|
||||
public IntTag(int Value) : this(null, Value)
|
||||
{
|
||||
}
|
||||
public IntTag(string _Name, int Val)
|
||||
{
|
||||
this.Name = _Name;
|
||||
IntVal = Val;
|
||||
}
|
||||
private int IntVal;
|
||||
public int Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return IntVal;
|
||||
}
|
||||
}
|
||||
public override TagType Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return TagType.INTEGER;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool ReadTag(BinaryReader br)
|
||||
{
|
||||
Name = br.ReadString();
|
||||
IntVal = br.ReadInt32();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public override void SkipTag(BinaryReader br)
|
||||
{
|
||||
_ = new IntTag().ReadTag(br);
|
||||
}
|
||||
public override void WriteTag(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(((int)Type));
|
||||
bw.Write(Name);
|
||||
bw.Write(Value);
|
||||
}
|
||||
|
||||
public override void WriteData(BinaryWriter bw)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public override void Rename(string old, string newName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new IntTag(Name, Value);
|
||||
}
|
||||
}
|
||||
}
|
285
Serialization/ZNIFile/ListTag.cs
Normal file
285
Serialization/ZNIFile/ListTag.cs
Normal file
|
@ -0,0 +1,285 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization.ZNIFile
|
||||
{
|
||||
public class ListTag : Tag, IList<Tag>, IList
|
||||
{
|
||||
private TagType _subtype;
|
||||
private List<Tag> _tags;
|
||||
public List<Tag> Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return _tags;
|
||||
}
|
||||
}
|
||||
|
||||
public ListTag() : this(TagType.STRING, null, new List<Tag>())
|
||||
{
|
||||
}
|
||||
public ListTag(TagType sub) : this(sub, null, new List<Tag>()) { }
|
||||
public ListTag(TagType sub, string name) : this(sub,name, new List<Tag>()) { }
|
||||
public ListTag(TagType sub, string name, List<Tag> tags)
|
||||
{
|
||||
_tags = tags;
|
||||
Name = name;
|
||||
setSubtype(sub);
|
||||
}
|
||||
public void setSubtype(TagType itemTypes)
|
||||
{
|
||||
_subtype = itemTypes;
|
||||
}
|
||||
public TagType getListType()
|
||||
{
|
||||
return _subtype;
|
||||
}
|
||||
public override TagType Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return TagType.LIST;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsFixedSize
|
||||
{
|
||||
get
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsReadOnly
|
||||
{
|
||||
get
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return _tags.Count;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsSynchronized
|
||||
{
|
||||
get
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public object SyncRoot
|
||||
{
|
||||
get
|
||||
{
|
||||
return ((IList)_tags).SyncRoot;
|
||||
}
|
||||
}
|
||||
|
||||
object IList.this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
return _tags[index];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_tags.Count >= index) _tags[index] = (Tag)value;
|
||||
else _tags.Add((Tag)value);
|
||||
}
|
||||
}
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override bool ReadTag(BinaryReader br)
|
||||
{
|
||||
_subtype = (TagType)br.ReadInt32();
|
||||
Name = br.ReadString();
|
||||
while (true)
|
||||
{
|
||||
TagType next = (TagType)br.ReadInt32();
|
||||
Tag _next = null;
|
||||
switch(next)
|
||||
{
|
||||
case TagType.FOLDER:
|
||||
_next = new Folder();
|
||||
break;
|
||||
case TagType.BOOL:
|
||||
_next = new BoolTag();
|
||||
break;
|
||||
case TagType.BYTE:
|
||||
_next = new ByteTag();
|
||||
break;
|
||||
case TagType.DOUBLE:
|
||||
_next = new DoubleTag();
|
||||
break;
|
||||
case TagType.FLOAT:
|
||||
_next = new FloatTag();
|
||||
break;
|
||||
case TagType.INTEGER:
|
||||
_next = new IntTag();
|
||||
break;
|
||||
case TagType.LIST:
|
||||
_next = new ListTag();
|
||||
break;
|
||||
case TagType.LONG:
|
||||
_next = new LongTag();
|
||||
break;
|
||||
case TagType.STRING:
|
||||
_next = new StringTag();
|
||||
break;
|
||||
case TagType.END:
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_next.ReadTag(br))
|
||||
{
|
||||
_tags.Add(_next);
|
||||
}
|
||||
_next.Parent = this;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public override void Rename(string old, string newName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SkipTag(BinaryReader br)
|
||||
{
|
||||
_ = new ListTag(_subtype).ReadTag(br);
|
||||
}
|
||||
|
||||
public override void WriteData(BinaryWriter bw)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void WriteTag(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(((int)Type));
|
||||
bw.Write(((int)_subtype));
|
||||
bw.Write(Name);
|
||||
|
||||
foreach(Tag x in _tags)
|
||||
{
|
||||
x.WriteTag(bw);
|
||||
}
|
||||
|
||||
bw.Write(((int)TagType.END));
|
||||
}
|
||||
|
||||
public int Add(object value)
|
||||
{
|
||||
if (value is Tag)
|
||||
{
|
||||
Tag tx = (Tag)value;
|
||||
Add(tx);
|
||||
return _tags.IndexOf(tx);
|
||||
}
|
||||
else return -1;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
foreach(Tag x in _tags)
|
||||
{
|
||||
x.Parent = null;
|
||||
}
|
||||
_tags.Clear();
|
||||
}
|
||||
|
||||
public bool Contains(object value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public int IndexOf(object value)
|
||||
{
|
||||
if (!(value is Tag)) return -1;
|
||||
return _tags.IndexOf((Tag)value);
|
||||
}
|
||||
|
||||
public void Insert(int index, object value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Remove(object value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void CopyTo(Array array, int index)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IEnumerator GetEnumerator()
|
||||
{
|
||||
return _tags.GetEnumerator();
|
||||
}
|
||||
|
||||
public int IndexOf(Tag item)
|
||||
{
|
||||
return _tags.IndexOf(item);
|
||||
}
|
||||
|
||||
public void Insert(int index, Tag item)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Add(Tag item)
|
||||
{
|
||||
item.Parent = this;
|
||||
_tags.Add(item);
|
||||
}
|
||||
|
||||
public bool Contains(Tag item)
|
||||
{
|
||||
return _tags.Contains(item);
|
||||
}
|
||||
|
||||
public void CopyTo(Tag[] array, int arrayIndex)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool Remove(Tag item)
|
||||
{
|
||||
if (Contains(item))
|
||||
{
|
||||
item.Parent = null;
|
||||
_tags.Remove(item);
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
|
||||
IEnumerator<Tag> IEnumerable<Tag>.GetEnumerator()
|
||||
{
|
||||
return _tags.GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
74
Serialization/ZNIFile/LongTag.cs
Normal file
74
Serialization/ZNIFile/LongTag.cs
Normal file
|
@ -0,0 +1,74 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization.ZNIFile
|
||||
{
|
||||
public class LongTag : Tag
|
||||
{
|
||||
private long LongVal;
|
||||
public long Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return LongVal;
|
||||
}
|
||||
}
|
||||
|
||||
public LongTag(string _Name, long val)
|
||||
{
|
||||
Name = _Name;
|
||||
LongVal = val;
|
||||
}
|
||||
public LongTag(long _LongVal) : this(null, _LongVal)
|
||||
{
|
||||
}
|
||||
public LongTag() : this(null, 0) { }
|
||||
|
||||
public override TagType Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return TagType.LONG;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool ReadTag(BinaryReader br)
|
||||
{
|
||||
Name = br.ReadString();
|
||||
LongVal = br.ReadInt64();
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void Rename(string old, string newName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SkipTag(BinaryReader br)
|
||||
{
|
||||
_ = new LongTag().ReadTag(br);
|
||||
}
|
||||
|
||||
public override void WriteData(BinaryWriter bw)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void WriteTag(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(((int)Type));
|
||||
bw.Write(Name);
|
||||
|
||||
bw.Write(Value);
|
||||
}
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new LongTag(Name, Value);
|
||||
}
|
||||
}
|
||||
}
|
75
Serialization/ZNIFile/StringTag.cs
Normal file
75
Serialization/ZNIFile/StringTag.cs
Normal file
|
@ -0,0 +1,75 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization.ZNIFile
|
||||
{
|
||||
public class StringTag : Tag
|
||||
{
|
||||
private string StrVal;
|
||||
public string Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return StrVal;
|
||||
}
|
||||
}
|
||||
|
||||
public override TagType Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return TagType.STRING;
|
||||
}
|
||||
}
|
||||
|
||||
public StringTag() : this(null, "")
|
||||
{ }
|
||||
public StringTag(string Value) : this(null, Value)
|
||||
{
|
||||
}
|
||||
public StringTag(string _Name, string Val)
|
||||
{
|
||||
this.Name = _Name;
|
||||
StrVal = Val;
|
||||
}
|
||||
|
||||
public override bool ReadTag(BinaryReader br)
|
||||
{
|
||||
Name = br.ReadString();
|
||||
StrVal = br.ReadString();
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void Rename(string old, string newName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SkipTag(BinaryReader br)
|
||||
{
|
||||
_ = br.ReadString();
|
||||
_ = br.ReadString();
|
||||
}
|
||||
|
||||
public override void WriteData(BinaryWriter bw)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void WriteTag(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(((int)Type));
|
||||
bw.Write(Name);
|
||||
bw.Write(Value);
|
||||
}
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new StringTag(Name, Value);
|
||||
}
|
||||
}
|
||||
}
|
240
Serialization/ZNIFile/Tag.cs
Normal file
240
Serialization/ZNIFile/Tag.cs
Normal file
|
@ -0,0 +1,240 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization.ZNIFile
|
||||
{
|
||||
public abstract class Tag : ICloneable, IComparable<Tag>
|
||||
{
|
||||
public Tag Parent { get; internal set; }
|
||||
public abstract TagType Type { get; }
|
||||
|
||||
public bool HasValue
|
||||
{
|
||||
get
|
||||
{
|
||||
switch(Type)
|
||||
{
|
||||
case TagType.FOLDER:
|
||||
case TagType.LIST:
|
||||
case TagType.END:
|
||||
case TagType.INVALID:
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal string _Name="";
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return _Name;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_Name == value) return;
|
||||
if (value == null)
|
||||
{
|
||||
value = "";
|
||||
}
|
||||
Folder f = Parent as Folder;
|
||||
if(f != null)
|
||||
{
|
||||
f.Rename(_Name, value);
|
||||
}
|
||||
_Name = value;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract bool ReadTag(BinaryReader br);
|
||||
public abstract void SkipTag(BinaryReader br);
|
||||
public abstract void WriteTag(BinaryWriter bw);
|
||||
public abstract void WriteData(BinaryWriter bw);
|
||||
|
||||
private string Error = "Invalid tag type";
|
||||
public virtual Tag this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new InvalidOperationException(Error);
|
||||
}
|
||||
set
|
||||
{
|
||||
throw new InvalidOperationException(Error);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual Tag this[string index]
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new InvalidOperationException(Error);
|
||||
}
|
||||
set
|
||||
{
|
||||
throw new InvalidOperationException(Error);
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetCanonicalName(TagType type)
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
case TagType.FOLDER:
|
||||
{
|
||||
return "Folder";
|
||||
}
|
||||
case TagType.STRING:
|
||||
{
|
||||
return "String";
|
||||
}
|
||||
case TagType.INTEGER:
|
||||
{
|
||||
return "Integer";
|
||||
}
|
||||
case TagType.LIST:
|
||||
{
|
||||
return "List";
|
||||
}
|
||||
case TagType.BOOL:
|
||||
{
|
||||
return "Bool";
|
||||
}
|
||||
case TagType.DOUBLE:
|
||||
{
|
||||
return "Double";
|
||||
}
|
||||
case TagType.FLOAT:
|
||||
{
|
||||
return "Float";
|
||||
}
|
||||
case TagType.LONG:
|
||||
{
|
||||
return "Long";
|
||||
}
|
||||
case TagType.BYTE:
|
||||
{
|
||||
return "Invalid";
|
||||
}
|
||||
}
|
||||
return "Invalid";
|
||||
}
|
||||
|
||||
public string StringValue
|
||||
{
|
||||
get
|
||||
{
|
||||
switch(Type)
|
||||
{
|
||||
case TagType.STRING:
|
||||
{
|
||||
return (this as StringTag).Value;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new Exception("Invalid type");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int IntValue
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (Type)
|
||||
{
|
||||
case TagType.INTEGER:
|
||||
return (this as IntTag).Value;
|
||||
default:
|
||||
throw new Exception("Invalid type");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool BoolValue
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (Type)
|
||||
{
|
||||
case TagType.BOOL:
|
||||
return (this as BoolTag).Value;
|
||||
default:
|
||||
throw new Exception("Invalid type");
|
||||
}
|
||||
}
|
||||
}
|
||||
public double DoubleValue
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (Type)
|
||||
{
|
||||
case TagType.DOUBLE:
|
||||
return (this as DoubleTag).Value;
|
||||
default:
|
||||
throw new Exception("Invalid type");
|
||||
}
|
||||
}
|
||||
}
|
||||
public float FloatValue
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (Type)
|
||||
{
|
||||
case TagType.FLOAT:
|
||||
return (this as FloatTag).Value;
|
||||
default:
|
||||
throw new Exception("Invalid type");
|
||||
}
|
||||
}
|
||||
}
|
||||
public long LongValue
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (Type)
|
||||
{
|
||||
case TagType.LONG:
|
||||
return (this as LongTag).Value;
|
||||
default:
|
||||
throw new Exception("Invalid type");
|
||||
}
|
||||
}
|
||||
}
|
||||
public byte ByteValue
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (Type)
|
||||
{
|
||||
case TagType.BYTE:
|
||||
return (this as ByteTag).Value;
|
||||
default:
|
||||
throw new Exception("Invalid type");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void Rename(string old, string newName);
|
||||
|
||||
public abstract object Clone();
|
||||
|
||||
public int CompareTo(Tag other)
|
||||
{
|
||||
if (ID == other.ID) return 0;
|
||||
else return 1;
|
||||
}
|
||||
|
||||
private Guid ID { get; set; } = Guid.NewGuid();
|
||||
}
|
||||
}
|
27
Serialization/ZNIFile/TagType.cs
Normal file
27
Serialization/ZNIFile/TagType.cs
Normal file
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI.Serialization.ZNIFile
|
||||
{
|
||||
public enum TagType
|
||||
{
|
||||
FOLDER = 0,
|
||||
STRING = 1,
|
||||
INTEGER = 2,
|
||||
LIST = 3, // List can be any valid Tag Type
|
||||
END = 4, // Present at the end of a folder or list
|
||||
BOOL = 5,
|
||||
DOUBLE = 6,
|
||||
FLOAT = 7,
|
||||
LONG = 8,
|
||||
BYTE = 9,
|
||||
BYTEARRAY = 10,
|
||||
|
||||
|
||||
|
||||
INVALID=99
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue