Skip to content

Commit

Permalink
- Moved equality definitions into the DataInfo compound key interface…
Browse files Browse the repository at this point in the history
… to standardize its behaviour.
  • Loading branch information
MapleWheels committed Dec 29, 2024
1 parent 2f8626a commit 3fd848c
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/IDataInfo.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
namespace Barotrauma.LuaCs.Data;
using System;
using System.Collections.Generic;

namespace Barotrauma.LuaCs.Data;

/// <summary>
/// Serves as a compound-key to refer to all resources and information that comes from a specific source.
/// </summary>
public interface IDataInfo
public interface IDataInfo : IEqualityComparer<IDataInfo>, IEquatable<IDataInfo>
{
/// <summary>
/// Package-Unique name to be used internally for all representations of, and references to, this information.
/// Internal name unique within the resources inside a package.
/// </summary>
string InternalName { get; }
/// <summary>
Expand All @@ -17,4 +20,33 @@ public interface IDataInfo
/// Used in place of the package data when the OwnerPackage is missing.
/// </summary>
string FallbackPackageName { get; }

bool IEqualityComparer<IDataInfo>.Equals(IDataInfo x, IDataInfo y)
{
if (x is null || y is null)
return false;
if (x.OwnerPackage is null)
throw new NullReferenceException($"ContentPackage not set for resource {x}!");
if (y.OwnerPackage is null)
throw new NullReferenceException($"ContentPackage not set for resource {y}!");
if (x.InternalName.IsNullOrWhiteSpace())
throw new NullReferenceException($"InternalName not set for resource {x}!");
if (y.InternalName.IsNullOrWhiteSpace())
throw new NullReferenceException($"InternalName not set for resource {y}!");
return x.OwnerPackage == y.OwnerPackage && x.InternalName == y.InternalName;
}

bool IEquatable<IDataInfo>.Equals(IDataInfo other)
{
return Equals(this, other);
}

int IEqualityComparer<IDataInfo>.GetHashCode(IDataInfo obj)
{
if (obj.OwnerPackage is null)
throw new NullReferenceException($"ContentPackage not set for resource {obj}!");
if (obj.InternalName.IsNullOrWhiteSpace())
throw new NullReferenceException($"InternalName is null for object {obj}!");
return obj.InternalName.GetHashCode() + obj.OwnerPackage.GetHashCode();
}
}

0 comments on commit 3fd848c

Please sign in to comment.