135 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			135 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.IO;
 | |
| using System.Linq;
 | |
| using System.Xml.Linq;
 | |
| using TechnologyAssembler.Core.Assets;
 | |
| 
 | |
| namespace HashCalculator.GUI
 | |
| {
 | |
|     public class AssetEntry : IEquatable<AssetEntry>
 | |
|     {
 | |
|         public string Type { get; }
 | |
|         public string Name { get; }
 | |
|         public IEnumerable<string> DisplayLabels { get; }
 | |
|         public uint InstanceId => SageHash.CalculateLowercaseHash(Name);
 | |
| 
 | |
|         public string NameString => $"{Type}:{Name}";
 | |
|         public string InstanceIdString => $"{InstanceId:X8} ({InstanceId})";
 | |
| 
 | |
|         public AssetEntry(XElement element)
 | |
|         {
 | |
|             if (element == null)
 | |
|             {
 | |
|                 throw new ArgumentNullException($"{nameof(element)} is null");
 | |
|             }
 | |
| 
 | |
|             if (element.Name.Namespace != ModXml.EalaAsset)
 | |
|             {
 | |
|                 throw new NotSupportedException();
 | |
|             }
 | |
| 
 | |
|             Type = element.Name.LocalName;
 | |
|             var id = element.Attribute("id")?.Value;
 | |
|             Name = id ?? throw new NotSupportedException();
 | |
| 
 | |
|             var labels = from name in element.Elements(ModXml.EalaAsset + "DisplayName")
 | |
|                          select name.Value;
 | |
|             var transformLabels = from name in element.Elements(ModXml.EalaAsset + "DisplayNameTransformed")
 | |
|                                   select name.Value;
 | |
|             DisplayLabels = labels.Concat(transformLabels).ToArray();
 | |
|         }
 | |
| 
 | |
|         public AssetEntry(Asset asset)
 | |
|         {
 | |
|             if(asset is null)
 | |
|             {
 | |
|                 throw new ArgumentNullException($"{nameof(asset)} is null");
 | |
|             }
 | |
|             Type = asset.TypeName;
 | |
|             Name = asset.InstanceName;
 | |
|             DisplayLabels = Enumerable.Empty<string>();
 | |
|             if(InstanceId != asset.InstanceId || SageHash.CalculateBinaryHash(Type) != asset.TypeId)
 | |
|             {
 | |
|                 throw new InvalidDataException();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public bool Equals(AssetEntry entry)
 | |
|         {
 | |
|             return this == entry;
 | |
|         }
 | |
| 
 | |
|         // override object.Equals
 | |
|         public override bool Equals(object obj)
 | |
|         {
 | |
|             //       
 | |
|             // See the full list of guidelines at
 | |
|             //   http://go.microsoft.com/fwlink/?LinkID=85237  
 | |
|             // and also the guidance for operator== at
 | |
|             //   http://go.microsoft.com/fwlink/?LinkId=85238
 | |
|             //
 | |
| 
 | |
|             if (!(obj is AssetEntry entry))
 | |
|             {
 | |
|                 return false;
 | |
|             }
 | |
| 
 | |
|             return this == entry;
 | |
|         }
 | |
| 
 | |
|         public static bool operator ==(AssetEntry a, AssetEntry b)
 | |
|         {
 | |
|             if (a is null || b is null)
 | |
|             {
 | |
|                 return a is null == b is null;
 | |
|             }
 | |
| 
 | |
|             if (ReferenceEquals(a, b))
 | |
|             {
 | |
|                 return true;
 | |
|             }
 | |
| 
 | |
|             return a.Type == b.Type && a.InstanceId == b.InstanceId;
 | |
|         }
 | |
| 
 | |
|         public static bool operator !=(AssetEntry a, AssetEntry b)
 | |
|         {
 | |
|             return !(a == b);
 | |
|         }
 | |
| 
 | |
|         // override object.GetHashCode
 | |
|         public override int GetHashCode()
 | |
|         {
 | |
|             return HashCode.Combine(Type, InstanceId);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     internal class DisplayAssetEntry : IComparable<DisplayAssetEntry>
 | |
|     {
 | |
|         public string Name { get; }
 | |
|         public string InstanceId { get; }
 | |
| 
 | |
|         public DisplayAssetEntry(AssetEntry entry)
 | |
|         {
 | |
|             Name = entry.NameString;
 | |
|             InstanceId = entry.InstanceIdString;
 | |
|         }
 | |
| 
 | |
|         public int CompareTo(DisplayAssetEntry other)
 | |
|         {
 | |
|             return string.CompareOrdinal(Name, other.Name);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     internal class LocalizedDisplayAssetEntry : DisplayAssetEntry
 | |
|     {
 | |
|         public string LocalizedNames { get; }
 | |
| 
 | |
|         public LocalizedDisplayAssetEntry(AssetEntry entry) : base(entry)
 | |
|         {
 | |
|             LocalizedNames = entry.DisplayLabels.Aggregate((x, y) => $"{x} {y}");
 | |
|         }
 | |
|     }
 | |
| }
 |