133 lines
3.3 KiB
C#
133 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Collections;
|
|
using System.Text;
|
|
using ln.type;
|
|
|
|
namespace ln.objects.catalog
|
|
{
|
|
public class ODBList : ODBEntity, IEnumerable<ODBEntity>
|
|
{
|
|
Guid identity = Guid.NewGuid();
|
|
public override ODBValue Identity => new ODBGuid(identity);
|
|
|
|
List<ODBEntity> items = new List<ODBEntity>();
|
|
|
|
public ODBList()
|
|
: base(0x02)
|
|
{
|
|
}
|
|
public ODBList(byte[] bytes, int offset, int length)
|
|
: this()
|
|
{
|
|
MemoryStream stream = new MemoryStream(bytes, offset, length);
|
|
identity = new Guid(stream.ReadBytes(16));
|
|
|
|
int nItems = stream.ReadInteger();
|
|
for (int n = 0; n < nItems; n++)
|
|
items.Add(Deserialize(stream));
|
|
}
|
|
|
|
public ODBEntity this[int i]
|
|
{
|
|
get => items[i];
|
|
set => items[i] = value;
|
|
}
|
|
|
|
public void AddRange(IEnumerable<ODBEntity> values)
|
|
{
|
|
foreach (ODBEntity value in values)
|
|
Add(value);
|
|
}
|
|
|
|
public void Add(ODBEntity value)
|
|
{
|
|
items.Add(value);
|
|
}
|
|
public void Remove(ODBEntity value)
|
|
{
|
|
items.Remove(value);
|
|
}
|
|
public void RemoveAt(int i)
|
|
{
|
|
items.RemoveAt(i);
|
|
}
|
|
|
|
public int Count => items.Count;
|
|
|
|
public override ODBEntity Clone()
|
|
{
|
|
ODBList clone = new ODBList();
|
|
clone.identity = identity;
|
|
|
|
foreach (ODBEntity item in items)
|
|
clone.items.Add(item.Clone());
|
|
|
|
return clone;
|
|
}
|
|
|
|
public override byte[] Serialize()
|
|
{
|
|
MemoryStream stream = new MemoryStream();
|
|
BinaryWriter writer = new BinaryWriter(stream);
|
|
|
|
writer.Write(identity.ToByteArray());
|
|
writer.Write(items.Count);
|
|
|
|
foreach (ODBEntity value in items)
|
|
value.Serialize(writer);
|
|
|
|
return stream.ToArray();
|
|
}
|
|
|
|
protected override int compare(ODBEntity other)
|
|
{
|
|
ODBList you = other as ODBList;
|
|
|
|
int d = Count - you.Count;
|
|
if (d != 0)
|
|
return d;
|
|
|
|
for (int n = 0; n < Count; n++)
|
|
{
|
|
d = this[n].CompareTo(you[n]);
|
|
if (d != 0)
|
|
return d;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public IEnumerator<ODBEntity> GetEnumerator()
|
|
{
|
|
return items.GetEnumerator();
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return items.GetEnumerator();
|
|
}
|
|
|
|
static ODBList()
|
|
{
|
|
RegisterDeserializer(0x02, (b, o, l) => new ODBList(b, o, l));
|
|
}
|
|
|
|
public override string ToTreeString(int indent)
|
|
{
|
|
indent += 2;
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
stringBuilder.AppendFormat("{0} Identity={1} Count={2}", GetType().Name, Identity, Count);
|
|
foreach (ODBEntity entity in items)
|
|
{
|
|
stringBuilder.AppendLine();
|
|
stringBuilder.AppendFormat("{0}{1}", new string(' ', indent), entity.ToTreeString(indent));
|
|
}
|
|
|
|
return stringBuilder.ToString();
|
|
}
|
|
|
|
}
|
|
}
|