Files
Aaru/Aaru.Core/Remote.cs

552 lines
28 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Remote.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles connections to Aaru.Server.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2020-12-31 23:08:23 +00:00
// Copyright © 2011-2021 Natalia Portillo
// ****************************************************************************/
using System;
2019-01-02 04:05:51 +00:00
using System.Collections.Generic;
using System.Diagnostics;
2017-06-03 01:18:36 +01:00
using System.IO;
2019-01-02 04:05:51 +00:00
using System.Linq;
2017-06-03 01:18:36 +01:00
using System.Net;
using System.Text;
2017-12-19 19:33:46 +00:00
using System.Threading;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Metadata;
using Aaru.Console;
using Aaru.Database;
using Aaru.Database.Models;
using Aaru.Dto;
2019-01-02 04:05:51 +00:00
using Microsoft.EntityFrameworkCore;
2018-12-20 00:12:48 +00:00
using Newtonsoft.Json;
2021-09-13 02:48:17 +01:00
using Spectre.Console;
2020-02-27 00:33:26 +00:00
using CdOffset = Aaru.Database.Models.CdOffset;
using Version = Aaru.CommonTypes.Metadata.Version;
2017-06-03 01:18:36 +01:00
2020-02-27 00:33:26 +00:00
namespace Aaru.Core
{
2020-02-29 18:03:35 +00:00
/// <summary>Handles connections to Aaru.Server</summary>
public static class Remote
{
2020-02-29 18:03:35 +00:00
/// <summary>Submits a device report</summary>
/// <param name="report">Device report</param>
2019-01-04 04:16:44 +00:00
public static void SubmitReport(DeviceReportV2 report)
2018-12-20 00:12:48 +00:00
{
2020-02-29 18:03:35 +00:00
var submitThread = new Thread(() =>
2018-12-20 00:12:48 +00:00
{
2021-09-13 18:08:44 +01:00
Spectre.ProgressSingleSpinner(ctx =>
2018-12-20 00:12:48 +00:00
{
2021-09-13 18:08:44 +01:00
ctx.AddTask("Uploading device report").IsIndeterminate();
2020-02-29 18:03:35 +00:00
2021-09-13 18:08:44 +01:00
try
2020-02-29 18:03:35 +00:00
{
2021-09-13 18:08:44 +01:00
string json = JsonConvert.SerializeObject(report, Formatting.Indented,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
byte[] jsonBytes = Encoding.UTF8.GetBytes(json);
var request = WebRequest.Create("https://www.aaru.app/api/uploadreportv2");
((HttpWebRequest)request).UserAgent = $"Aaru {typeof(Version).Assembly.GetName().Version}";
request.Method = "POST";
request.ContentLength = jsonBytes.Length;
request.ContentType = "application/json";
Stream reqStream = request.GetRequestStream();
reqStream.Write(jsonBytes, 0, jsonBytes.Length);
reqStream.Close();
WebResponse response = request.GetResponse();
if(((HttpWebResponse)response).StatusCode != HttpStatusCode.OK)
return;
Stream data = response.GetResponseStream();
var reader = new StreamReader(data ?? throw new InvalidOperationException());
reader.ReadToEnd();
data.Close();
response.Close();
}
catch(WebException)
{
// Can't connect to the server, do nothing
}
2020-02-29 18:03:35 +00:00
2021-09-13 18:08:44 +01:00
// ReSharper disable once RedundantCatchClause
catch
{
#if DEBUG
if(Debugger.IsAttached)
throw;
#endif
}
});
2017-06-03 01:18:36 +01:00
});
2020-02-29 18:03:35 +00:00
2017-06-03 01:18:36 +01:00
submitThread.Start();
}
2019-01-02 04:05:51 +00:00
2021-08-17 21:23:10 +01:00
/// <summary>Updates the main database</summary>
2021-08-17 13:56:05 +01:00
/// <param name="create">If <c>true</c> creates the database from scratch, otherwise updates an existing database</param>
public static void UpdateMainDatabase(bool create)
2019-01-02 04:05:51 +00:00
{
var mctx = AaruContext.Create(Settings.Settings.MainDbPath);
2020-07-10 17:38:52 +01:00
if(create)
{
mctx.Database.EnsureCreated();
mctx.Database.
ExecuteSqlRaw("CREATE TABLE IF NOT EXISTS \"__EFMigrationsHistory\" (\"MigrationId\" TEXT PRIMARY KEY, \"ProductVersion\" TEXT)");
foreach(string migration in mctx.Database.GetPendingMigrations())
{
mctx.Database.
ExecuteSqlRaw($"INSERT INTO \"__EFMigrationsHistory\" (MigrationId, ProductVersion) VALUES ('{migration}', '0.0.0')");
}
}
else
mctx.Database.Migrate();
2019-01-02 04:05:51 +00:00
mctx.SaveChanges();
try
{
long lastUpdate = 0;
DateTime latest = DateTime.MinValue;
if(!create)
{
2021-09-13 02:48:17 +01:00
List<DateTime> latestAll = new();
2020-02-29 18:03:35 +00:00
if(mctx.UsbVendors.Any())
latestAll.Add(mctx.UsbVendors.Max(v => v.ModifiedWhen));
if(mctx.UsbProducts.Any())
latestAll.Add(mctx.UsbProducts.Max(p => p.ModifiedWhen));
if(mctx.CdOffsets.Any())
latestAll.Add(mctx.CdOffsets.Max(o => o.ModifiedWhen));
if(mctx.Devices.Any())
latestAll.Add(mctx.Devices.Max(d => d.LastSynchronized));
2019-01-02 04:05:51 +00:00
if(latestAll.Any())
{
latest = latestAll.Max(t => t);
lastUpdate = (latest.ToFileTimeUtc() - new DateTime(1970, 1, 1).ToFileTimeUtc()) / 10000000;
}
}
if(lastUpdate == 0)
{
create = true;
AaruConsole.WriteLine("Creating main database");
2019-01-02 04:05:51 +00:00
}
else
{
AaruConsole.WriteLine("Updating main database");
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Last update: {0}", latest);
2019-01-02 04:05:51 +00:00
}
DateTime updateStart = DateTime.UtcNow;
2020-02-29 18:03:35 +00:00
var request = WebRequest.Create($"https://www.aaru.app/api/update?timestamp={lastUpdate}");
((HttpWebRequest)request).UserAgent = $"Aaru {typeof(Version).Assembly.GetName().Version}";
2019-01-02 04:05:51 +00:00
request.Method = "GET";
request.ContentType = "application/json";
WebResponse response = request.GetResponse();
if(((HttpWebResponse)response).StatusCode != HttpStatusCode.OK)
{
2020-02-27 23:48:41 +00:00
AaruConsole.ErrorWriteLine("Error {0} when trying to get updated entities.",
2020-02-29 18:03:35 +00:00
((HttpWebResponse)response).StatusCode);
2019-01-02 04:05:51 +00:00
return;
}
2020-02-29 18:03:35 +00:00
Stream data = response.GetResponseStream();
var reader = new StreamReader(data ?? throw new InvalidOperationException());
2021-09-13 02:48:17 +01:00
SyncDto sync = JsonConvert.DeserializeObject<SyncDto>(reader.ReadToEnd()) ?? new SyncDto();
2019-01-02 04:05:51 +00:00
if(create)
{
2021-09-13 02:48:17 +01:00
AnsiConsole.Progress().AutoClear(true).HideCompleted(true).
Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn()).
Start(ctx =>
{
ProgressTask task = ctx.AddTask("Adding USB vendors");
task.MaxValue = sync.UsbVendors.Count;
foreach(UsbVendorDto vendor in sync.UsbVendors)
{
task.Increment(1);
mctx.UsbVendors.Add(new UsbVendor(vendor.VendorId, vendor.Vendor));
}
});
2019-01-02 04:05:51 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Added {0} usb vendors", sync.UsbVendors.Count);
2019-01-02 04:05:51 +00:00
2021-09-13 02:48:17 +01:00
AnsiConsole.Progress().AutoClear(true).HideCompleted(true).
Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn()).
Start(ctx =>
{
ProgressTask task = ctx.AddTask("Adding USB products");
task.MaxValue = sync.UsbProducts.Count;
foreach(UsbProductDto product in sync.UsbProducts)
{
task.Increment(1);
2020-02-29 18:03:35 +00:00
2021-09-13 02:48:17 +01:00
mctx.UsbProducts.Add(new UsbProduct(product.VendorId, product.ProductId,
product.Product));
}
});
2019-01-02 04:05:51 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Added {0} usb products", sync.UsbProducts.Count);
2019-01-02 04:05:51 +00:00
2021-09-13 02:48:17 +01:00
AnsiConsole.Progress().AutoClear(true).HideCompleted(true).
Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn()).
Start(ctx =>
{
ProgressTask task = ctx.AddTask("Adding CompactDisc read offsets");
task.MaxValue = sync.Offsets.Count;
2020-02-29 18:03:35 +00:00
2021-09-13 02:48:17 +01:00
foreach(CdOffsetDto offset in sync.Offsets)
{
task.Increment(1);
mctx.CdOffsets.Add(new CdOffset(offset)
{
Id = offset.Id
});
}
});
2019-01-02 04:05:51 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Added {0} CompactDisc read offsets", sync.Offsets.Count);
2019-01-02 04:05:51 +00:00
2021-09-13 02:48:17 +01:00
AnsiConsole.Progress().AutoClear(true).HideCompleted(true).
Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn()).
Start(ctx =>
{
ProgressTask task = ctx.AddTask("Adding known devices");
task.MaxValue = sync.Devices.Count;
foreach(DeviceDto device in sync.Devices)
2020-02-29 18:03:35 +00:00
2021-09-13 02:48:17 +01:00
{
task.Increment(1);
mctx.Devices.Add(new Device(device)
{
Id = device.Id
});
}
});
2019-01-02 04:05:51 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Added {0} known devices", sync.Devices.Count);
AnsiConsole.Progress().AutoClear(true).HideCompleted(true).
Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn()).
Start(ctx =>
{
ProgressTask task = ctx.AddTask("Adding known iNES/NES 2.0 headers");
task.MaxValue = sync.NesHeaders?.Count ?? 0;
foreach(NesHeaderDto header in sync.NesHeaders ?? new List<NesHeaderDto>())
{
task.Increment(1);
mctx.NesHeaders.Add(new NesHeaderInfo
{
Id = header.Id,
AddedWhen = DateTime.UtcNow,
BatteryPresent = header.BatteryPresent,
ConsoleType = header.ConsoleType,
DefaultExpansionDevice = header.DefaultExpansionDevice,
ExtendedConsoleType = header.ExtendedConsoleType,
FourScreenMode = header.FourScreenMode,
Mapper = header.Mapper,
ModifiedWhen = DateTime.UtcNow,
NametableMirroring = header.NametableMirroring,
Sha256 = header.Sha256,
Submapper = header.Submapper,
VsHardwareType = header.VsHardwareType,
VsPpuType = header.VsPpuType
});
}
});
AaruConsole.WriteLine("Added {0} known iNES/NES 2.0 headers", sync.NesHeaders.Count);
2019-01-02 04:05:51 +00:00
}
else
{
long addedVendors = 0;
long addedProducts = 0;
long addedOffsets = 0;
long addedDevices = 0;
long addedNesHeaders = 0;
long modifiedVendors = 0;
long modifiedProducts = 0;
long modifiedOffsets = 0;
long modifiedDevices = 0;
long modifiedNesHeaders = 0;
2019-01-02 04:05:51 +00:00
2021-09-13 02:48:17 +01:00
AnsiConsole.Progress().AutoClear(true).HideCompleted(true).
Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn()).
Start(ctx =>
{
ProgressTask task = ctx.AddTask("Updating USB vendors");
task.MaxValue = sync.UsbVendors.Count;
foreach(UsbVendorDto vendor in sync.UsbVendors)
{
task.Increment(1);
UsbVendor existing =
mctx.UsbVendors.FirstOrDefault(v => v.Id == vendor.VendorId);
if(existing != null)
{
modifiedVendors++;
existing.Vendor = vendor.Vendor;
existing.ModifiedWhen = updateStart;
mctx.UsbVendors.Update(existing);
}
else
{
addedVendors++;
mctx.UsbVendors.Add(new UsbVendor(vendor.VendorId, vendor.Vendor));
}
}
});
2019-01-02 04:05:51 +00:00
2020-02-29 18:03:35 +00:00
AaruConsole.WriteLine("Added {0} USB vendors", addedVendors);
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Modified {0} USB vendors", modifiedVendors);
2019-01-02 04:05:51 +00:00
2021-09-13 02:48:17 +01:00
AnsiConsole.Progress().AutoClear(true).HideCompleted(true).
Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn()).
Start(ctx =>
{
ProgressTask task = ctx.AddTask("Updating USB products");
task.MaxValue = sync.UsbVendors.Count;
foreach(UsbProductDto product in sync.UsbProducts)
{
task.Increment(1);
UsbProduct existing =
mctx.UsbProducts.FirstOrDefault(p => p.VendorId == product.VendorId &&
p.ProductId == product.ProductId);
if(existing != null)
{
modifiedProducts++;
existing.Product = product.Product;
existing.ModifiedWhen = updateStart;
mctx.UsbProducts.Update(existing);
}
else
{
addedProducts++;
mctx.UsbProducts.Add(new UsbProduct(product.VendorId, product.ProductId,
product.Product));
}
}
});
2019-01-02 04:05:51 +00:00
2020-02-29 18:03:35 +00:00
AaruConsole.WriteLine("Added {0} USB products", addedProducts);
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Modified {0} USB products", modifiedProducts);
2019-01-02 04:05:51 +00:00
2021-09-13 02:48:17 +01:00
AnsiConsole.Progress().AutoClear(true).HideCompleted(true).
Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn()).
Start(ctx =>
{
ProgressTask task = ctx.AddTask("Updating CompactDisc read offsets");
task.MaxValue = sync.Offsets.Count;
foreach(CdOffsetDto offset in sync.Offsets)
{
CdOffset existing = mctx.CdOffsets.FirstOrDefault(o => o.Id == offset.Id);
task.Increment(1);
if(existing != null)
{
modifiedOffsets++;
existing.Agreement = offset.Agreement;
existing.Manufacturer = offset.Manufacturer;
existing.Model = offset.Model;
existing.Submissions = offset.Submissions;
existing.Offset = offset.Offset;
existing.ModifiedWhen = updateStart;
mctx.CdOffsets.Update(existing);
}
else
{
addedOffsets++;
mctx.CdOffsets.Add(new CdOffset(offset)
{
Id = offset.Id
});
}
}
});
2019-01-02 04:05:51 +00:00
2020-02-29 18:03:35 +00:00
AaruConsole.WriteLine("Added {0} CompactDisc read offsets", addedOffsets);
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Modified {0} CompactDisc read offsets", modifiedOffsets);
2019-01-02 04:05:51 +00:00
2021-09-13 02:48:17 +01:00
AnsiConsole.Progress().AutoClear(true).HideCompleted(true).
Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn()).
Start(ctx =>
{
ProgressTask task = ctx.AddTask("Updating known devices");
task.MaxValue = sync.Offsets.Count;
foreach(DeviceDto device in sync.Devices)
{
task.Increment(1);
Device existing = mctx.Devices.FirstOrDefault(d => d.Id == device.Id);
if(existing != null)
{
modifiedDevices++;
mctx.Remove(existing);
existing = new Device(device)
{
Id = device.Id,
OptimalMultipleSectorsRead = device.OptimalMultipleSectorsRead,
CanReadGdRomUsingSwapDisc = device.CanReadGdRomUsingSwapDisc
};
mctx.Devices.Add(existing);
}
else
{
addedDevices++;
mctx.Devices.Add(new Device(device)
{
Id = device.Id,
OptimalMultipleSectorsRead = device.OptimalMultipleSectorsRead,
CanReadGdRomUsingSwapDisc = device.CanReadGdRomUsingSwapDisc
});
}
}
});
2019-01-02 04:05:51 +00:00
2020-02-29 18:03:35 +00:00
AaruConsole.WriteLine("Added {0} known devices", addedDevices);
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Modified {0} known devices", modifiedDevices);
AnsiConsole.Progress().AutoClear(true).HideCompleted(true).
Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn()).
Start(ctx =>
{
ProgressTask task = ctx.AddTask("Updating known iNES/NES 2.0 headers");
task.MaxValue = sync.Offsets.Count;
foreach(NesHeaderDto header in sync.NesHeaders)
{
task.Increment(1);
NesHeaderInfo existing = mctx.NesHeaders.FirstOrDefault(d => d.Id == header.Id);
if(existing != null)
{
modifiedNesHeaders++;
DateTime addedDate = existing.AddedWhen;
mctx.Remove(existing);
existing = new NesHeaderInfo
{
Id = header.Id,
AddedWhen = addedDate,
BatteryPresent = header.BatteryPresent,
ConsoleType = header.ConsoleType,
DefaultExpansionDevice = header.DefaultExpansionDevice,
ExtendedConsoleType = header.ExtendedConsoleType,
FourScreenMode = header.FourScreenMode,
Mapper = header.Mapper,
ModifiedWhen = DateTime.UtcNow,
NametableMirroring = header.NametableMirroring,
Sha256 = header.Sha256,
Submapper = header.Submapper,
VsHardwareType = header.VsHardwareType,
VsPpuType = header.VsPpuType
};
mctx.NesHeaders.Add(existing);
}
else
{
addedNesHeaders++;
mctx.NesHeaders.Add(new NesHeaderInfo
{
Id = header.Id,
AddedWhen = DateTime.UtcNow,
BatteryPresent = header.BatteryPresent,
ConsoleType = header.ConsoleType,
DefaultExpansionDevice = header.DefaultExpansionDevice,
ExtendedConsoleType = header.ExtendedConsoleType,
FourScreenMode = header.FourScreenMode,
Mapper = header.Mapper,
ModifiedWhen = DateTime.UtcNow,
NametableMirroring = header.NametableMirroring,
Sha256 = header.Sha256,
Submapper = header.Submapper,
VsHardwareType = header.VsHardwareType,
VsPpuType = header.VsPpuType
});
}
}
});
AaruConsole.WriteLine("Added {0} known iNES/NES 2.0 headers", addedDevices);
AaruConsole.WriteLine("Modified {0} known iNES/NES 2.0 headers", modifiedDevices);
2019-01-02 04:05:51 +00:00
}
}
2020-02-29 18:03:35 +00:00
catch(Exception ex)
{
AaruConsole.ErrorWriteLine("Exception {0} when updating database.", ex);
}
2019-01-02 04:05:51 +00:00
finally
{
2021-09-13 18:08:44 +01:00
Spectre.ProgressSingleSpinner(ctx =>
{
ctx.AddTask("Saving changes...").IsIndeterminate();
mctx.SaveChanges();
});
2019-01-02 04:05:51 +00:00
}
}
}
2017-12-19 20:33:03 +00:00
}