PackableList.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections;
  2. namespace InABox.Core
  3. {
  4. public interface IPackableList : IPackable, IList
  5. {
  6. }
  7. public class PackableList<T> : ObservableList<T>, IPackableList where T : BaseObject, IPackable, new()
  8. {
  9. public void Pack(FastBinaryWriter writer)
  10. {
  11. writer.Write(Count);
  12. foreach (var item in this)
  13. item.Pack(writer);
  14. }
  15. public void Unpack(FastBinaryReader reader)
  16. {
  17. var iCount = reader.ReadInt32();
  18. for (var i = 0; i < iCount; i++)
  19. {
  20. var item = new T();
  21. item.Unpack(reader);
  22. Add(item);
  23. }
  24. }
  25. public override int GetHashCode()
  26. {
  27. return base.GetHashCode();
  28. }
  29. public override bool Equals(object obj)
  30. {
  31. var other = obj as PackableList<T>;
  32. if (other == null)
  33. return false;
  34. if (Count != other.Count) // Require equal count.
  35. return false;
  36. for (var i = 0; i < Count; i++)
  37. {
  38. var item1 = this[i];
  39. var item2 = other[i];
  40. if (item1 == null)
  41. {
  42. if (item2 != null)
  43. return false;
  44. }
  45. else if (!item1.Equals(item2))
  46. {
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. }
  53. }