forked from aly/qstbak
120 lines
2.1 KiB
C#
120 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
|
|
namespace Questionable.Model.Questing;
|
|
|
|
public abstract class ElementId : IComparable<ElementId>, IEquatable<ElementId>
|
|
{
|
|
public ushort Value { get; }
|
|
|
|
protected ElementId(ushort value)
|
|
{
|
|
Value = value;
|
|
}
|
|
|
|
public int CompareTo(ElementId? other)
|
|
{
|
|
if ((object)this == other)
|
|
{
|
|
return 0;
|
|
}
|
|
if ((object)other == null)
|
|
{
|
|
return 1;
|
|
}
|
|
return Value.CompareTo(other.Value);
|
|
}
|
|
|
|
public bool Equals(ElementId? other)
|
|
{
|
|
if ((object)other == null)
|
|
{
|
|
return false;
|
|
}
|
|
if ((object)this == other)
|
|
{
|
|
return true;
|
|
}
|
|
if (other.GetType() != GetType())
|
|
{
|
|
return false;
|
|
}
|
|
return Value == other.Value;
|
|
}
|
|
|
|
public override bool Equals(object? obj)
|
|
{
|
|
if (obj == null)
|
|
{
|
|
return false;
|
|
}
|
|
if (this == obj)
|
|
{
|
|
return true;
|
|
}
|
|
if (obj.GetType() != GetType())
|
|
{
|
|
return false;
|
|
}
|
|
return Equals((ElementId)obj);
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return Value.GetHashCode();
|
|
}
|
|
|
|
public static bool operator ==(ElementId? left, ElementId? right)
|
|
{
|
|
return object.Equals(left, right);
|
|
}
|
|
|
|
public static bool operator !=(ElementId? left, ElementId? right)
|
|
{
|
|
return !object.Equals(left, right);
|
|
}
|
|
|
|
public static ElementId FromString(string value)
|
|
{
|
|
if (value.StartsWith("U"))
|
|
{
|
|
return new UnlockLinkId(ushort.Parse(value.Substring(1), CultureInfo.InvariantCulture));
|
|
}
|
|
return new QuestId(ushort.Parse(value, CultureInfo.InvariantCulture));
|
|
}
|
|
|
|
public static List<ElementId> FromStrings(IEnumerable<string> values, out int skippedCount)
|
|
{
|
|
List<ElementId> list = new List<ElementId>();
|
|
skippedCount = 0;
|
|
foreach (string value in values)
|
|
{
|
|
if (TryFromString(value, out ElementId elementId) && elementId != null)
|
|
{
|
|
list.Add(elementId);
|
|
}
|
|
else
|
|
{
|
|
skippedCount++;
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public static bool TryFromString(string value, out ElementId? elementId)
|
|
{
|
|
try
|
|
{
|
|
elementId = FromString(value);
|
|
return true;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
elementId = null;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public abstract override string ToString();
|
|
}
|