Add ATA comparer.

This commit is contained in:
2019-11-09 23:49:48 +00:00
parent 77090f12c6
commit 0467aab2e2
4 changed files with 235 additions and 1 deletions

View File

@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using DiscImageChef.Decoders.ATA;
using DiscImageChef.Server.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -140,5 +142,133 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
return RedirectToAction(nameof(Index));
}
public IActionResult Compare(int id, int rightId)
{
var model = new CompareModel();
model.LeftId = id;
model.RightId = rightId;
CommonTypes.Metadata.Ata left = _context.Ata.FirstOrDefault(l => l.Id == id);
CommonTypes.Metadata.Ata right = _context.Ata.FirstOrDefault(r => r.Id == rightId);
if(left is null)
{
model.ErrorMessage = $"ATA with id {id} has not been found";
model.HasError = true;
return View(model);
}
if(right is null)
{
model.ErrorMessage = $"ATA with id {rightId} has not been found";
model.HasError = true;
return View(model);
}
Identify.IdentifyDevice? leftNullable = left.IdentifyDevice;
Identify.IdentifyDevice? rightNullable = right.IdentifyDevice;
if(!leftNullable.HasValue &&
!rightNullable.HasValue)
{
model.AreEqual = true;
return View(model);
}
if(leftNullable.HasValue &&
!rightNullable.HasValue)
{
model.ValueNames.Add("Decoded");
model.LeftValues.Add("decoded");
model.RightValues.Add("null");
return View(model);
}
if(!leftNullable.HasValue)
{
model.ValueNames.Add("Decoded");
model.LeftValues.Add("null");
model.RightValues.Add("decoded");
return View(model);
}
Identify.IdentifyDevice leftValue = left.IdentifyDevice.Value;
Identify.IdentifyDevice rightValue = right.IdentifyDevice.Value;
model.ValueNames = new List<string>();
model.LeftValues = new List<string>();
model.RightValues = new List<string>();
foreach(FieldInfo fieldInfo in leftValue.GetType().GetFields())
{
object? lv = fieldInfo.GetValue(leftValue);
object? rv = fieldInfo.GetValue(rightValue);
if(fieldInfo.FieldType.IsArray)
{
object[] la = lv as object[];
object[] ra = rv as object[];
switch(la)
{
case null when ra is null: continue;
case null:
model.ValueNames.Add(fieldInfo.Name);
model.LeftValues.Add("null");
model.RightValues.Add("[]");
continue;
}
if(ra is null)
{
model.ValueNames.Add(fieldInfo.Name);
model.LeftValues.Add("[]");
model.RightValues.Add("null");
continue;
}
if(la.SequenceEqual(ra))
continue;
model.ValueNames.Add(fieldInfo.Name);
model.LeftValues.Add("[]");
model.RightValues.Add("[]");
}
else if(lv == null &&
rv == null) { }
else if(lv != null &&
rv == null)
{
model.ValueNames.Add(fieldInfo.Name);
model.LeftValues.Add($"{lv}");
model.RightValues.Add("null");
}
else if(lv == null)
{
model.ValueNames.Add(fieldInfo.Name);
model.LeftValues.Add("null");
model.RightValues.Add($"{rv}");
}
else if(!lv.Equals(rv))
{
model.ValueNames.Add(fieldInfo.Name);
model.LeftValues.Add($"{lv}");
model.RightValues.Add($"{rv}");
}
}
model.AreEqual = model.LeftValues.Count == 0 && model.RightValues.Count == 0;
return View(model);
}
}
}

View File

@@ -0,0 +1,82 @@
@model CompareModel
@{
Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
ViewBag.Title = "DiscImageChef";
}
@{
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Details.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : DiscImageChef Server.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2019 Natalia Portillo
// ****************************************************************************/
}
<h2>Comparing ATA IDENTIFY ID @Model.LeftId with ID @Model.RightId</h2>
@if (Model.AreEqual)
{
<p>No differences found.</p>
return;
}
@if (Model.HasError)
{
<p class="alert-info">@Model.ErrorMessage</p>
return;
}
<table>
<thead>
<tr>
<th>
Value name
</th>
<th>
ID: @Model.LeftId
</th>
<th>
ID: @Model.RightId
</th>
<th></th>
</tr>
</thead>
<tbody>
@for (var i = 0; i < Model.ValueNames.Count; i++)
{
<tr>
<td>
@Model.ValueNames[i]
</td>
<td>
@Model.LeftValues[i]
</td>
<td>
@Model.RightValues[i]
</td>
</tr>
}
</tbody>
</table>
<a asp-action="Index" class="btn btn-primary">Back to List</a>
<a asp-action="Delete" asp-route-id="@Model.LeftId" class="btn btn-danger">Delete ID @Model.LeftId</a>
<a asp-action="Delete" asp-route-id="@Model.RightId" class="btn btn-danger">Delete ID @Model.RightId</a>

View File

@@ -41,5 +41,11 @@
@Html.Raw(Html.EncodedMultiLineText(Identify.Prettify(Model.Identify)))
</div>
<div>
<a asp-action="Index" class="btn btn-secondary">Back to List</a>
<form asp-action="Compare">
<input type="hidden" asp-for="Id" />
Compare to:
<input name="RightId" type="number" />
<a asp-action="Index" class="btn btn-secondary">Back to List</a>
<input class="btn btn-primary" type="submit" value="Compare" />
</form>
</div>

View File

@@ -0,0 +1,16 @@
using System.Collections.Generic;
namespace DiscImageChef.Server.Models
{
public class CompareModel
{
public int LeftId { get; set; }
public int RightId { get; set; }
public List<string> ValueNames { get; set; }
public List<string> LeftValues { get; set; }
public List<string> RightValues { get; set; }
public bool HasError { get; set; }
public string ErrorMessage { get; set; }
public bool AreEqual { get; set; }
}
}