Files
SabreTools/SabreTools.DatItems/Input.cs

247 lines
7.1 KiB
C#
Raw Normal View History

2020-09-03 22:28:48 -07:00
using System;
using System.Collections.Generic;
2020-09-02 21:59:26 -07:00
using System.Linq;
2020-09-07 22:00:02 -07:00
using System.Xml.Serialization;
2020-09-03 13:20:56 -07:00
2020-12-08 13:23:59 -08:00
using SabreTools.Core;
using SabreTools.Core.Tools;
2020-09-03 13:20:56 -07:00
using Newtonsoft.Json;
2020-09-02 21:59:26 -07:00
2020-12-08 15:15:41 -08:00
namespace SabreTools.DatItems
2020-09-02 21:59:26 -07:00
{
/// <summary>
/// Represents one ListXML input
/// </summary>
2020-09-08 10:12:41 -07:00
[JsonObject("input"), XmlRoot("input")]
2020-09-02 21:59:26 -07:00
public class Input : DatItem
{
#region Fields
/// <summary>
/// Input service ID
/// </summary>
[JsonProperty("service", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("service")]
2020-09-02 21:59:26 -07:00
public bool? Service { get; set; }
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool ServiceSpecified { get { return Service != null; } }
2020-09-02 21:59:26 -07:00
/// <summary>
/// Determins if this has a tilt sensor
/// </summary>
[JsonProperty("tilt", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("tilt")]
2020-09-02 21:59:26 -07:00
public bool? Tilt { get; set; }
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool TiltSpecified { get { return Tilt != null; } }
2020-09-02 21:59:26 -07:00
/// <summary>
/// Number of players on the input
/// </summary>
[JsonProperty("players", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("players")]
2020-09-03 22:28:48 -07:00
public long? Players { get; set; }
2020-09-02 21:59:26 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool PlayersSpecified { get { return Players != null; } }
2020-09-02 21:59:26 -07:00
/// <summary>
/// Number of coins required
/// </summary>
[JsonProperty("coins", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("coins")]
2020-09-03 22:28:48 -07:00
public long? Coins { get; set; }
2020-09-02 21:59:26 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool CoinsSpecified { get { return Coins != null; } }
2020-09-02 21:59:26 -07:00
/// <summary>
/// Set of controls for the input
/// </summary>
[JsonProperty("controls", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("controls")]
2020-09-02 21:59:26 -07:00
public List<Control> Controls { get; set; }
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool ControlsSpecified { get { return Controls != null && Controls.Count > 0; } }
2020-09-02 21:59:26 -07:00
#endregion
#region Accessors
2020-12-13 13:22:06 -08:00
/// <inheritdoc/>
public override void SetFields(
Dictionary<DatItemField, string> datItemMappings,
Dictionary<MachineField, string> machineMappings)
2020-09-02 21:59:26 -07:00
{
// Set base fields
2020-12-13 13:22:06 -08:00
base.SetFields(datItemMappings, machineMappings);
2020-09-02 21:59:26 -07:00
// Handle Input-specific fields
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Service))
Service = datItemMappings[DatItemField.Service].AsYesNo();
2020-09-02 21:59:26 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Tilt))
Tilt = datItemMappings[DatItemField.Tilt].AsYesNo();
2020-09-02 21:59:26 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Players))
Players = Utilities.CleanLong(datItemMappings[DatItemField.Players]);
2020-09-02 21:59:26 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Coins))
Coins = Utilities.CleanLong(datItemMappings[DatItemField.Coins]);
2020-09-02 21:59:26 -07:00
2020-09-30 13:25:40 -07:00
if (ControlsSpecified)
2020-09-03 15:02:59 -07:00
{
foreach (Control control in Controls)
{
2020-12-13 13:22:06 -08:00
control.SetFields(datItemMappings, machineMappings);
2020-09-03 15:02:59 -07:00
}
}
2020-09-02 21:59:26 -07:00
}
#endregion
#region Constructors
/// <summary>
/// Create a default, empty Input object
/// </summary>
public Input()
{
ItemType = ItemType.Input;
}
#endregion
#region Cloning Methods
public override object Clone()
{
return new Input()
{
ItemType = this.ItemType,
DupeType = this.DupeType,
Machine = this.Machine.Clone() as Machine,
Source = this.Source.Clone() as Source,
Remove = this.Remove,
Service = this.Service,
Tilt = this.Tilt,
Players = this.Players,
Coins = this.Coins,
Controls = this.Controls,
};
}
#endregion
#region Comparision Methods
public override bool Equals(DatItem other)
{
// If we don't have a Input, return false
if (ItemType != other.ItemType)
return false;
// Otherwise, treat it as a Input
Input newOther = other as Input;
// If the Input information matches
2020-09-03 15:02:59 -07:00
bool match = (Service == newOther.Service
2020-09-02 21:59:26 -07:00
&& Tilt == newOther.Tilt
&& Players == newOther.Players
&& Coins == newOther.Coins);
2020-09-03 15:02:59 -07:00
if (!match)
return match;
// If the controls match
2020-09-30 13:25:40 -07:00
if (ControlsSpecified)
2020-09-03 15:02:59 -07:00
{
foreach (Control control in Controls)
{
match &= newOther.Controls.Contains(control);
}
}
2020-09-02 21:59:26 -07:00
2020-09-03 15:02:59 -07:00
return match;
2020-09-02 21:59:26 -07:00
}
#endregion
#region Filtering
2020-12-13 13:22:06 -08:00
/// <inheritdoc/>
public override void RemoveFields(
List<DatItemField> datItemFields,
List<MachineField> machineFields)
2020-09-02 21:59:26 -07:00
{
// Remove common fields first
2020-12-13 13:22:06 -08:00
base.RemoveFields(datItemFields, machineFields);
2020-09-02 21:59:26 -07:00
// Remove the fields
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Service))
2020-09-02 21:59:26 -07:00
Service = null;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Tilt))
2020-09-02 21:59:26 -07:00
Tilt = null;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Players))
2020-09-03 22:28:48 -07:00
Players = 0;
2020-09-02 21:59:26 -07:00
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Coins))
2020-09-02 21:59:26 -07:00
Coins = null;
2020-09-30 13:25:40 -07:00
if (ControlsSpecified)
2020-09-03 15:02:59 -07:00
{
foreach (Control control in Controls)
{
2020-12-13 13:22:06 -08:00
control.RemoveFields(datItemFields, machineFields);
2020-09-03 15:02:59 -07:00
}
}
2020-09-02 21:59:26 -07:00
}
#endregion
#region Sorting and Merging
2020-12-13 13:22:06 -08:00
/// <inheritdoc/>
public override void ReplaceFields(
DatItem item,
List<DatItemField> datItemFields,
List<MachineField> machineFields)
2020-09-02 21:59:26 -07:00
{
// Replace common fields first
2020-12-13 13:22:06 -08:00
base.ReplaceFields(item, datItemFields, machineFields);
2020-09-02 21:59:26 -07:00
// If we don't have a Input to replace from, ignore specific fields
if (item.ItemType != ItemType.Input)
return;
// Cast for easier access
Input newItem = item as Input;
// Replace the fields
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Service))
2020-09-02 21:59:26 -07:00
Service = newItem.Service;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Tilt))
2020-09-02 21:59:26 -07:00
Tilt = newItem.Tilt;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Players))
2020-09-02 21:59:26 -07:00
Players = newItem.Players;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Coins))
2020-09-02 21:59:26 -07:00
Coins = newItem.Coins;
2020-09-03 15:02:59 -07:00
// DatItem_Control_* doesn't make sense here
// since not every control under the other item
// can replace every control under this item
2020-09-02 21:59:26 -07:00
}
#endregion
}
}