Worms4Editor/LibXom/Data/XomType.cs

102 lines
2.7 KiB
C#
Raw Permalink Normal View History

2023-01-07 09:36:13 +00:00
using LibXom.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibXom.Data
{
public class XomType : XomFileComponent
{
private byte[] xBytes;
2023-01-07 09:36:13 +00:00
private string name;
private List<XomContainer> xomContainers = new List<XomContainer>();
public override int Id
{
get
{
2023-03-07 07:49:53 +00:00
return this.fileBelongs.calculateIdForXomFileComponent(this.Uuid, fileBelongs.XomTypes);
2023-01-07 09:36:13 +00:00
}
}
public byte[] XBytes
2023-01-07 09:36:13 +00:00
{
get
{
return xBytes;
2023-01-07 09:36:13 +00:00
}
}
public string Name
{
get
{
return name;
}
}
public XomContainer[] Containers
{
get
{
return xomContainers.ToArray();
}
}
private int getContainerIndex(XomContainer container)
{
for (int i = 0; i < xomContainers.Count; i++)
{
if (xomContainers[i].Equals(container))
2023-01-07 09:36:13 +00:00
{
return i;
}
}
2023-03-03 15:59:36 +00:00
throw new XomContainerNotFoundException("container with uuid " + container.Uuid + " not found in type " + this.Name);
2023-01-07 09:36:13 +00:00
}
public void ReplaceContainerData(XomContainer container, byte[] newData)
{
int indx = this.getContainerIndex(container);
this.xomContainers[indx].data = newData;
}
public XomContainer NewContainer()
{
XomContainer xomContainer = new XomContainer(this.fileBelongs, this.Name, new byte[0x3]);
this.xomContainers.Add(xomContainer);
return xomContainer;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
public override bool Equals(object? obj)
{
if (obj is not XomType) return false;
XomType xCompare = obj as XomType;
return xCompare.Name.Equals(this.Name, StringComparison.InvariantCultureIgnoreCase);
}
2023-01-07 09:36:13 +00:00
public void DeleteContainer(XomContainer container)
{
int indx = this.getContainerIndex(container);
this.xomContainers.RemoveAt(indx);
if(this.xomContainers.Count == 0)
this.fileBelongs.deleteXomType(this);
2023-01-07 09:36:13 +00:00
}
internal XomType(XomFile fileFrom, byte[] xBytes, string name, XomContainer[] xomContainers)
2023-01-07 09:36:13 +00:00
{
fileBelongs = fileFrom;
this.xBytes = xBytes;
2023-01-07 09:36:13 +00:00
this.name = name;
this.xomContainers.AddRange(xomContainers);
}
}
}