Make alterations to libzni

This commit is contained in:
Tara 2022-12-27 20:58:21 -07:00
parent 7b776ecfea
commit 0638f7e642
19 changed files with 1824 additions and 183 deletions

188
Hashing.cs Normal file
View file

@ -0,0 +1,188 @@
using LibZNI.Serialization;
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
{
public class Hash : Serializable
{
public byte[] hashed;
private readonly byte signature = 0x9f;
public int passnum = 0;
public Hash(int len)
{
hashed = new byte[len];
//hashed[0] = signature;
passnum = 0;
}
public byte get_sig_at_pass(int pass)
{
byte start = signature;
for (int i = 0; i < pass; i++)
{
start = (byte)wrap_add((int)start, (int)signature);
}
return start;
}
public Hash(byte[] bs, int pass)
{
hashed = bs;
passnum = pass;
}
public static Hash compute(int len, byte[] data)
{
if (len <= 0) throw new ArgumentOutOfRangeException();
byte[] arr = new byte[len];
int p = 0;//pointer to position in [arr]
foreach (byte b in data)
{
//arr[p] += b;// treat as a number.
int exist = arr[p];
exist += b;
while (exist > 255) exist -= 255;
if (exist < 0) exist *= -1;
arr[p] = (byte)exist;
p++;
if (p >= len)
{
p = 0;// reset the pointer
}
}
return new Hash(arr, 1);
}
public static Hash operator +(Hash a, Hash b)
{
Hash ret = new Hash(a.hashed, a.passnum);
int p = 2; // We do want to add the position signed bit. As that information is unique to a piece of source data
for (p = 0; p < b.hashed.Length; p++)
{
int exist = a.hashed[p];
exist = wrap_add(exist, b.hashed[p]);
ret.hashed[p] = (byte)exist;
}
ret.passnum++;
return ret;
}
/// <summary>
/// Wrap adds, with a cap at 255
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns>Wrapped integer</returns>
public static int wrap_add(int a, int b)
{
a = a + b;
while (a > 255) a -= 255;
if (a < 0) a *= -1;
return a;
}
/// <summary>
/// Wrap subtracts with a cap of 255
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns>Wrapped integer</returns>
public static int wrap_sub(int a, int b)
{
int ret = a - b;
if (ret < 0) ret += 255;
return ret;
}
public static Hash operator -(Hash a, Hash b)
{
Hash ret = new Hash(a.hashed, a.passnum);
int p = 1; // We do want to add the position signed bit. As that information is unique to a piece of source data
for (p = 0; p < b.hashed.Length; p++)
{
int exist = a.hashed[p];
exist = wrap_sub(exist, b.hashed[p]);
ret.hashed[p] = (byte)exist;
}
ret.passnum--;
/*
if (ret.get_sig_at_pass(ret.passnum) == ret.hashed[0])
{
// success
}
else throw new Exception("Fatal error in pass. Validation failed");
*/
return ret;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
foreach (byte b in serialize())
{
sb.Append(b.ToString("X2"));
}
return sb.ToString();
}
public byte[] serialize()
{
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(passnum);
bw.Write(hashed.Length);
bw.Write(hashed);
return ms.ToArray();
}
public void deserialize(byte[] streams)
{
MemoryStream ms = new MemoryStream(streams);
BinaryReader br = new BinaryReader(ms);
passnum = br.ReadInt32();
hashed = br.ReadBytes(br.ReadInt32());
br.Close();
}
public override void save(Folder f)
{
f.Add(new IntTag("pass", passnum));
f.Add(new ByteArrayTag("hash",hashed));
}
public override void load(Folder f)
{
passnum = f["pass"].IntValue;
hashed = (f["hash"] as ByteArrayTag).Value;
}
}
}

View file

@ -8,14 +8,6 @@
<Configurations>Debug;Release;DebPub;KVPBuild</Configurations>
</PropertyGroup>
<ItemGroup>
<None Remove="TEA.cs.ignore" />
</ItemGroup>
<ItemGroup>
<Compile Include="TEA.cs.ignore" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MySqlConnector" Version="2.1.11" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />

View 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
View 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;
}
}
}

View 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);
}
}
}

View 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);
}
}
}

View 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);
}
}
}

View 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);
}
}
}

View 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);
}
}
}

View 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());
}
}
}

View 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;
}
}
}

View 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);
}
}
}

View 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();
}
}
}

View 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);
}
}
}

View 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);
}
}
}

View 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();
}
}

View 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
}
}

View file

@ -1,175 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace LibZNI
{
public class XTEA
{
int[] xtea_key = new int[4];
int rounds = 6;
int DELTA;
int SECRET = 99999;
XTEA()
{
DELTA = int.Parse("0x9E3779B9", System.Globalization.NumberStyles.HexNumber);
}
#pragma warning disable IDE1006 // Naming Styles
public int hex2int(string hex)
#pragma warning restore IDE1006 // Naming Styles
{
if(hex.Substring(0,2) == "0x")
{
return int.Parse(hex, System.Globalization.NumberStyles.HexNumber);
}
if (hex.Substring(0, 1) == "x")
{
return int.Parse("0" + hex, System.Globalization.NumberStyles.HexNumber);
}
return int.Parse("0x" + hex, System.Globalization.NumberStyles.HexNumber);
}
#pragma warning disable IDE1006 // Naming Styles
void set_xtea_key(int[] key)
#pragma warning restore IDE1006 // Naming Styles
{
xtea_key = key;
}
#pragma warning disable IDE1006 // Naming Styles
public int[] make_key(string str)
#pragma warning restore IDE1006 // Naming Styles
{
MD5 sum = MD5.Create();
str = Convert.ToHexString(sum.ComputeHash(Encoding.UTF8.GetBytes(str)));
return new int[4]
{
hex2int(str.Substring(0, 8)),
hex2int(str.Substring(8, 8)),
hex2int(str.Substring(16, 8)),
hex2int(str.Substring(24, 8))
};
}
static string ConvertToBase64Arithmetic(uint i)
{
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
StringBuilder sb = new StringBuilder();
do
{
sb.Insert(0, alphabet[(int)(i % 64)]);
i = i / 64;
} while (i != 0);
return sb.ToString();
}
#pragma warning disable IDE1006 // Naming Styles
public string encipher(int v0, int v1)
#pragma warning restore IDE1006 // Naming Styles
{
int num = rounds;
int sum = 0;
do
{
v0 += (((v1 << 4) ^ ((v1 >> 5) & 0x07FFFFFF)) + v1) ^ (sum + xtea_key[sum & 3]);
sum += DELTA;
v1 += (((v0 << 4) ^ ((v0 >> 5) & 0x07FFFFFF)) + v0) ^ (sum + xtea_key[(sum >> 11) & 3]);
num--;
} while (num >0);
return ConvertToBase64Arithmetic((uint)v0).Substring(0, 6) + ConvertToBase64Arithmetic((uint)v1).Substring(0, 6);
}
#pragma warning disable IDE1006 // Naming Styles
public string decipher(int v0, int v1)
#pragma warning restore IDE1006 // Naming Styles
{
int num = rounds;
int sum = DELTA * num;
do
{
v1 -= (((v0 << 4) ^ ((v0 >> 5) & 0x07FFFFFF)) + v0) ^ (sum + xtea_key[(sum >> 11) & 3]);
sum -= DELTA;
v0 -= (((v1 << 4) ^ ((v1 >> 5) & 0x07FFFFFF)) + v1) ^ (sum + xtea_key[sum & 3]);
num--;
} while (num > 0);
return ConvertToBase64Arithmetic((uint)v0).Substring(0, 5) + ConvertToBase64Arithmetic((uint)v1).Substring(0, 5);
}
#pragma warning disable IDE1006 // Naming Styles
public string encrypt_string(string str)
#pragma warning restore IDE1006 // Naming Styles
{
str = Tools.Base64Encode(str);
int i = str.IndexOf('=');
if(i != -1)
{
str = str.Remove(i);
}
int len = str.Length;
str += "AAAAAAAAAA=";
string result="";
i = 0;
do
{
int i1 = int.Parse(Tools.Base64Decode(str.Substring(i, 5)+"A="));
int i2 = int.Parse(Tools.Base64Decode(str.Substring(i + 5, 5) + "A="));
result += encipher(i1, i2);
i += 10;
} while (i < len);
return result;
}
/*
public string decrypt_string(string str)
{
int len = str.Length;
int i = 0;
string result = "";
do
{
int v0;
int v1;
v0 = int.Parse(Tools.Base64Decode(str.Substring(i, 5) + "=="));
i += 6;
v1 = int.Parse(Tools.Base64Decode(str.Substring(i, 5) + "=="));
i += 6;
result += decipher(v0, v1);
} while (i < len);
i = result.Length - 1;
while(result.Substring(i-1,(i-(i-1))) == "AA")
{
result = result.Remove(i, ((i - i)==0? 1 : i));
i--;
}
i = result.Length - 1;
result = result.Substring()
}*/
}
}

129
Versioning.cs Normal file
View file

@ -0,0 +1,129 @@
using LibZNI.Serialization;
using LibZNI.Serialization.ZNIFile;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime;
using System.Text;
using System.Threading.Tasks;
namespace LibZNI
{
public class VersionNumberDifferentException : Exception
{
public VersionNumberDifferentException(Version one, Version two) :
base($"The version numbers are not identical. Current version {one} ; Other {two}")
{
}
}
public class Version : Serializable
{
public List<int> ver { get; set; } = new List<int>();
public Version() { }
public Version(int major, int minor, int revision, int build, int cycleStatus, int cycleNum)
{
ver = new List<int>();
ver.Add(major);
ver.Add(minor);
ver.Add(revision);
ver.Add(build);
ver.Add(cycleStatus);
ver.Add(cycleNum);
}
public int Compare(Version other)
{
for(int i=0;i<ver.Count;i++)
{
int cur = ver[i];
int oth = other.ver[i];
if (cur < oth) return 1;
else if (cur > oth) return 2;
}
return 0;
}
public Version(string versionStr)
{
ver = new List<int>();
List<string> split = versionStr.llParseStringKeepNulls(new string[] { "." }, new string[] { "R", "A", "B", "RC", "DEV" });
for(int i=0;i<split.Count;i++)
{
if (i == 4)
{
switch (split[i])
{
case "R":
ver.Add(0);
break;
case "A":
ver.Add(1);
break;
case "B":
ver.Add(2);
break;
case "RC":
ver.Add(3);
break;
case "DEV":
ver.Add(4);
break;
default:
ver.Add(4);
break;
}
}
else ver.Add(int.Parse(split[i]));
}
}
public override string ToString()
{
string CYCLE = "";
switch (ver[4])
{
case 0:
CYCLE = "R";
break;
case 1:
CYCLE = "A";
break;
case 2:
CYCLE = "B";
break;
case 3:
CYCLE = "RC";
break;
case 4:
CYCLE = "DEV";
break;
default:
CYCLE = "DEV";
break;
}
return $"{ver[0]}.{ver[1]}.{ver[2]}.{ver[3]}.{CYCLE}.{ver[5]}";
}
public override void load(Folder f)
{
ListTag lt = f["Version"] as ListTag;
ver = new List<int>();
foreach(Tag tag in lt)
{
IntTag it = tag as IntTag;
ver.Add(it.IntValue);
}
}
public override void save(Folder f)
{
ListTag lt = new ListTag(TagType.INTEGER, "Version");
foreach(int v in ver)
{
IntTag i = new IntTag(v);
lt.Add(i);
}
f.Add(lt);
}
}
}