using System; using System.Collections.Generic; using System.Globalization; namespace Questionable.Model.Questing; public abstract class ElementId : IComparable, IEquatable { 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 FromStrings(IEnumerable values, out int skippedCount) { List list = new List(); 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(); }