2020-07-10 17:48:22 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2017-05-28 21:01:17 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : Remote.cs
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2017-05-28 21:01:17 +01:00
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Core algorithms.
|
2017-05-28 21:01:17 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2020-02-27 20:38:23 +00:00
|
|
|
|
// Handles connections to Aaru.Server.
|
2017-05-28 21:01:17 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2024-12-19 10:45:18 +00:00
|
|
|
|
// Copyright © 2011-2025 Natalia Portillo
|
2017-05-28 21:01:17 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-12-19 03:50:57 +00:00
|
|
|
|
|
2017-12-21 14:41:38 +00:00
|
|
|
|
using System;
|
2019-01-02 04:05:51 +00:00
|
|
|
|
using System.Collections.Generic;
|
2018-12-20 00:16:54 +00:00
|
|
|
|
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;
|
2022-03-26 17:12:46 +00:00
|
|
|
|
using System.Net.Http;
|
2019-01-04 04:10:45 +00:00
|
|
|
|
using System.Text;
|
2022-12-15 02:06:39 +00:00
|
|
|
|
using System.Text.Json;
|
2024-05-11 01:54:16 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes.Metadata;
|
|
|
|
|
|
using Aaru.Database;
|
|
|
|
|
|
using Aaru.Database.Models;
|
|
|
|
|
|
using Aaru.Dto;
|
2025-08-17 05:50:25 +01:00
|
|
|
|
using Aaru.Logging;
|
2019-01-02 04:05:51 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2025-08-20 18:51:05 +01:00
|
|
|
|
using Sentry;
|
2022-11-15 15:58:43 +00: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
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
|
namespace Aaru.Core;
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <summary>Handles connections to Aaru.Server</summary>
|
|
|
|
|
|
public static class Remote
|
2017-05-28 21:01:17 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <summary>Submits a device report</summary>
|
|
|
|
|
|
/// <param name="report">Device report</param>
|
2024-05-11 03:06:10 +01:00
|
|
|
|
public static Task SubmitReportAsync(DeviceReport report)
|
2017-05-28 21:01:17 +01:00
|
|
|
|
{
|
2024-05-11 03:06:10 +01:00
|
|
|
|
return AnsiConsole.Status()
|
|
|
|
|
|
.StartAsync(Localization.Core.Uploading_device_report,
|
|
|
|
|
|
async _ =>
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
string json =
|
|
|
|
|
|
JsonSerializer.Serialize(report,
|
|
|
|
|
|
typeof(DeviceReport),
|
|
|
|
|
|
DeviceReportContext.Default);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2024-05-11 03:06:10 +01:00
|
|
|
|
var httpClient = new HttpClient();
|
2022-03-26 17:12:46 +00:00
|
|
|
|
|
2024-05-11 03:06:10 +01:00
|
|
|
|
httpClient.DefaultRequestHeaders.Add("User-Agent",
|
|
|
|
|
|
$"Aaru {typeof(Version).Assembly.GetName().Version}");
|
2022-03-26 17:12:46 +00:00
|
|
|
|
|
2024-05-11 03:06:10 +01:00
|
|
|
|
httpClient.BaseAddress = new Uri("https://www.aaru.app");
|
2022-03-26 17:12:46 +00:00
|
|
|
|
|
2024-05-11 03:06:10 +01:00
|
|
|
|
HttpResponseMessage response =
|
|
|
|
|
|
await httpClient.PostAsync("/api/uploadreportv2",
|
|
|
|
|
|
new StringContent(json,
|
|
|
|
|
|
Encoding.UTF8,
|
|
|
|
|
|
"application/json"));
|
2022-03-26 17:12:46 +00:00
|
|
|
|
|
2024-05-11 03:06:10 +01:00
|
|
|
|
if(!response.IsSuccessStatusCode) return;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2024-05-11 03:06:10 +01:00
|
|
|
|
Stream data = await response.Content.ReadAsStreamAsync();
|
|
|
|
|
|
var reader = new StreamReader(data);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2024-05-11 03:06:10 +01:00
|
|
|
|
await reader.ReadToEndAsync();
|
|
|
|
|
|
data.Close();
|
|
|
|
|
|
}
|
2025-08-20 18:51:05 +01:00
|
|
|
|
catch(WebException ex)
|
2024-05-11 03:06:10 +01:00
|
|
|
|
{
|
|
|
|
|
|
// Can't connect to the server, do nothing
|
2025-08-20 18:51:05 +01:00
|
|
|
|
SentrySdk.CaptureException(ex);
|
2024-05-11 03:06:10 +01:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2025-08-20 18:51:05 +01:00
|
|
|
|
catch(Exception ex)
|
2024-05-11 03:06:10 +01:00
|
|
|
|
{
|
2025-08-20 18:51:05 +01:00
|
|
|
|
SentrySdk.CaptureException(ex);
|
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
#if DEBUG
|
2024-05-11 03:06:10 +01:00
|
|
|
|
if(Debugger.IsAttached) throw;
|
2024-05-01 04:05:22 +01:00
|
|
|
|
#endif
|
2024-05-11 03:06:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Updates the main database</summary>
|
|
|
|
|
|
/// <param name="create">If <c>true</c> creates the database from scratch, otherwise updates an existing database</param>
|
2024-05-11 01:54:16 +01:00
|
|
|
|
public static async Task UpdateMainDatabaseAsync(bool create)
|
2022-03-06 13:29:38 +00:00
|
|
|
|
{
|
2022-11-15 15:58:43 +00:00
|
|
|
|
var mctx = AaruContext.Create(Settings.Settings.MainDbPath);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
if(create)
|
|
|
|
|
|
{
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await mctx.Database.EnsureCreatedAsync();
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await mctx.Database
|
|
|
|
|
|
.ExecuteSqlRawAsync("CREATE TABLE IF NOT EXISTS \"__EFMigrationsHistory\" (\"MigrationId\" TEXT PRIMARY KEY, \"ProductVersion\" TEXT)");
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
foreach(string migration in await mctx.Database.GetPendingMigrationsAsync())
|
2023-10-03 22:57:50 +01:00
|
|
|
|
{
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await mctx.Database
|
2024-05-01 23:52:03 +01:00
|
|
|
|
#pragma warning disable EF1002
|
2024-05-11 01:54:16 +01:00
|
|
|
|
.ExecuteSqlRawAsync($"INSERT INTO \"__EFMigrationsHistory\" (MigrationId, ProductVersion) VALUES ('{
|
|
|
|
|
|
migration}', '0.0.0')");
|
2024-05-01 23:52:03 +01:00
|
|
|
|
#pragma warning restore EF1002
|
2023-10-03 22:57:50 +01:00
|
|
|
|
}
|
2017-05-28 21:01:17 +01:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
else
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await mctx.Database.MigrateAsync();
|
2019-01-02 04:05:51 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await mctx.SaveChangesAsync();
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
try
|
2019-01-02 04:05:51 +00:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
long lastUpdate = 0;
|
|
|
|
|
|
DateTime latest = DateTime.MinValue;
|
2020-07-10 17:38:52 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(!create)
|
2020-07-10 17:38:52 +01:00
|
|
|
|
{
|
2024-05-01 04:39:38 +01:00
|
|
|
|
List<DateTime> latestAll = [];
|
2020-07-10 17:38:52 +01:00
|
|
|
|
|
2025-11-24 03:00:06 +00:00
|
|
|
|
if(await mctx.UsbVendors.AnyAsync())
|
|
|
|
|
|
latestAll.Add(await mctx.UsbVendors.MaxAsync(static v => v.ModifiedWhen));
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
if(await mctx.UsbProducts.AnyAsync())
|
2025-11-24 03:00:06 +00:00
|
|
|
|
latestAll.Add(await mctx.UsbProducts.MaxAsync(static p => p.ModifiedWhen));
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2025-11-24 03:00:06 +00:00
|
|
|
|
if(await mctx.CdOffsets.AnyAsync())
|
|
|
|
|
|
latestAll.Add(await mctx.CdOffsets.MaxAsync(static o => o.ModifiedWhen));
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2025-11-24 03:00:06 +00:00
|
|
|
|
if(await mctx.Devices.AnyAsync())
|
|
|
|
|
|
latestAll.Add(await mctx.Devices.MaxAsync(static d => d.LastSynchronized));
|
2020-07-10 17:38:52 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(latestAll.Any())
|
2020-07-10 17:38:52 +01:00
|
|
|
|
{
|
2025-11-24 03:00:06 +00:00
|
|
|
|
latest = latestAll.Max(static t => t);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
lastUpdate = (latest.ToFileTimeUtc() - new DateTime(1970, 1, 1).ToFileTimeUtc()) / 10000000;
|
2020-07-10 17:38:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-01-02 04:05:51 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(lastUpdate == 0)
|
2019-01-02 04:05:51 +00:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
create = true;
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Creating_main_database);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Updating_main_database);
|
|
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Last_update_0, latest);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2019-01-02 04:05:51 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
DateTime updateStart = DateTime.UtcNow;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-26 17:12:46 +00:00
|
|
|
|
var httpClient = new HttpClient();
|
|
|
|
|
|
httpClient.DefaultRequestHeaders.Add("User-Agent", $"Aaru {typeof(Version).Assembly.GetName().Version}");
|
2025-01-11 21:20:50 +01:00
|
|
|
|
httpClient.BaseAddress = new Uri("https://www.aaru.app");
|
2022-03-26 17:12:46 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
HttpResponseMessage response = await httpClient.GetAsync($"/api/update?timestamp={lastUpdate}");
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-26 17:12:46 +00:00
|
|
|
|
if(!response.IsSuccessStatusCode)
|
2022-03-06 13:29:38 +00:00
|
|
|
|
{
|
2025-08-20 18:51:05 +01:00
|
|
|
|
AaruLogging.Error(Localization.Core.Error_0_when_trying_to_get_updated_entities, response.StatusCode);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
Stream data = await response.Content.ReadAsStreamAsync();
|
2022-03-26 17:12:46 +00:00
|
|
|
|
var reader = new StreamReader(data);
|
2024-05-11 01:54:16 +01:00
|
|
|
|
SyncDto sync = JsonSerializer.Deserialize<SyncDto>(await reader.ReadToEndAsync()) ?? new SyncDto();
|
2019-01-02 04:05:51 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(create)
|
|
|
|
|
|
{
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await AnsiConsole.Progress()
|
|
|
|
|
|
.AutoClear(true)
|
|
|
|
|
|
.HideCompleted(true)
|
|
|
|
|
|
.Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn())
|
|
|
|
|
|
.Start(ctx =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ProgressTask task = ctx.AddTask(Localization.Core.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));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
});
|
2019-01-02 04:05:51 +00:00
|
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Added_0_usb_vendors, sync.UsbVendors.Count);
|
2019-01-02 04:05:51 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await AnsiConsole.Progress()
|
|
|
|
|
|
.AutoClear(true)
|
|
|
|
|
|
.HideCompleted(true)
|
|
|
|
|
|
.Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn())
|
|
|
|
|
|
.Start(ctx =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ProgressTask task = ctx.AddTask(Localization.Core.Adding_USB_products);
|
|
|
|
|
|
task.MaxValue = sync.UsbProducts.Count;
|
|
|
|
|
|
|
|
|
|
|
|
foreach(UsbProductDto product in sync.UsbProducts)
|
|
|
|
|
|
{
|
|
|
|
|
|
task.Increment(1);
|
|
|
|
|
|
|
|
|
|
|
|
mctx.UsbProducts.Add(new UsbProduct(product.VendorId,
|
|
|
|
|
|
product.ProductId,
|
|
|
|
|
|
product.Product));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
});
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Added_0_usb_products, sync.UsbProducts.Count);
|
2019-01-02 04:05:51 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await AnsiConsole.Progress()
|
|
|
|
|
|
.AutoClear(true)
|
|
|
|
|
|
.HideCompleted(true)
|
|
|
|
|
|
.Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn())
|
|
|
|
|
|
.Start(ctx =>
|
|
|
|
|
|
{
|
2024-05-11 03:06:10 +01:00
|
|
|
|
ProgressTask task =
|
|
|
|
|
|
ctx.AddTask(Localization.Core.Adding_CompactDisc_read_offsets);
|
|
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
task.MaxValue = sync.Offsets.Count;
|
|
|
|
|
|
|
|
|
|
|
|
foreach(CdOffsetDto offset in sync.Offsets)
|
|
|
|
|
|
{
|
|
|
|
|
|
task.Increment(1);
|
|
|
|
|
|
|
|
|
|
|
|
mctx.CdOffsets.Add(new CdOffset(offset)
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = offset.Id
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
});
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Added_0_CompactDisc_read_offsets, sync.Offsets.Count);
|
2019-01-02 04:05:51 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await AnsiConsole.Progress()
|
|
|
|
|
|
.AutoClear(true)
|
|
|
|
|
|
.HideCompleted(true)
|
|
|
|
|
|
.Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn())
|
|
|
|
|
|
.Start(ctx =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ProgressTask task = ctx.AddTask(Localization.Core.Adding_known_devices);
|
|
|
|
|
|
task.MaxValue = sync.Devices.Count;
|
|
|
|
|
|
|
|
|
|
|
|
foreach(DeviceDto device in sync.Devices)
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
{
|
|
|
|
|
|
task.Increment(1);
|
2019-01-02 04:05:51 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
mctx.Devices.Add(new Device(device)
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = device.Id
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2021-09-13 02:48:17 +01:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
});
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Added_0_known_devices, sync.Devices.Count);
|
2019-01-02 04:05:51 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await AnsiConsole.Progress()
|
|
|
|
|
|
.AutoClear(true)
|
|
|
|
|
|
.HideCompleted(true)
|
|
|
|
|
|
.Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn())
|
|
|
|
|
|
.Start(ctx =>
|
|
|
|
|
|
{
|
2024-05-11 03:06:10 +01:00
|
|
|
|
ProgressTask task =
|
|
|
|
|
|
ctx.AddTask(Localization.Core.Adding_known_iNES_NES_2_0_headers);
|
|
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
task.MaxValue = sync.NesHeaders?.Count ?? 0;
|
|
|
|
|
|
|
|
|
|
|
|
foreach(NesHeaderDto header in sync.NesHeaders ?? [])
|
|
|
|
|
|
{
|
|
|
|
|
|
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,
|
|
|
|
|
|
TimingMode = header.TimingMode,
|
|
|
|
|
|
VsHardwareType = header.VsHardwareType,
|
|
|
|
|
|
VsPpuType = header.VsPpuType
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
});
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Added_0_known_iNES_NES_2_0_headers,
|
2022-11-23 16:06:46 +00:00
|
|
|
|
sync.NesHeaders?.Count ?? 0);
|
2022-03-06 13:29:38 +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;
|
|
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await AnsiConsole.Progress()
|
|
|
|
|
|
.AutoClear(true)
|
|
|
|
|
|
.HideCompleted(true)
|
|
|
|
|
|
.Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn())
|
|
|
|
|
|
.StartAsync(async ctx =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ProgressTask task = ctx.AddTask(Localization.Core.Updating_USB_vendors);
|
|
|
|
|
|
task.MaxValue = sync.UsbVendors.Count;
|
|
|
|
|
|
|
|
|
|
|
|
foreach(UsbVendorDto vendor in sync.UsbVendors)
|
|
|
|
|
|
{
|
|
|
|
|
|
task.Increment(1);
|
|
|
|
|
|
|
|
|
|
|
|
UsbVendor existing =
|
|
|
|
|
|
await mctx.UsbVendors.FirstOrDefaultAsync(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
|
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Added_0_usb_vendors, addedVendors);
|
|
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Modified_0_USB_vendors, modifiedVendors);
|
2019-01-02 04:05:51 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await AnsiConsole.Progress()
|
|
|
|
|
|
.AutoClear(true)
|
|
|
|
|
|
.HideCompleted(true)
|
|
|
|
|
|
.Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn())
|
|
|
|
|
|
.StartAsync(async ctx =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ProgressTask task = ctx.AddTask(Localization.Core.Updating_USB_products);
|
|
|
|
|
|
task.MaxValue = sync.UsbVendors.Count;
|
|
|
|
|
|
|
|
|
|
|
|
foreach(UsbProductDto product in sync.UsbProducts)
|
|
|
|
|
|
{
|
|
|
|
|
|
task.Increment(1);
|
|
|
|
|
|
|
|
|
|
|
|
UsbProduct existing =
|
|
|
|
|
|
await mctx.UsbProducts.FirstOrDefaultAsync(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
|
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Added_0_usb_products, addedProducts);
|
|
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Modified_0_USB_products, modifiedProducts);
|
2021-12-08 20:34:36 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await AnsiConsole.Progress()
|
|
|
|
|
|
.AutoClear(true)
|
|
|
|
|
|
.HideCompleted(true)
|
|
|
|
|
|
.Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn())
|
|
|
|
|
|
.StartAsync(async ctx =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ProgressTask task =
|
|
|
|
|
|
ctx.AddTask(Localization.Core.Updating_CompactDisc_read_offsets);
|
|
|
|
|
|
|
|
|
|
|
|
task.MaxValue = sync.Offsets.Count;
|
|
|
|
|
|
|
|
|
|
|
|
foreach(CdOffsetDto offset in sync.Offsets)
|
|
|
|
|
|
{
|
|
|
|
|
|
CdOffset existing =
|
|
|
|
|
|
await mctx.CdOffsets.FirstOrDefaultAsync(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
|
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Added_0_CompactDisc_read_offsets, addedOffsets);
|
|
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Modified_0_CompactDisc_read_offsets, modifiedOffsets);
|
2019-01-02 04:05:51 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await AnsiConsole.Progress()
|
|
|
|
|
|
.AutoClear(true)
|
|
|
|
|
|
.HideCompleted(true)
|
|
|
|
|
|
.Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn())
|
|
|
|
|
|
.StartAsync(async ctx =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ProgressTask task = ctx.AddTask(Localization.Core.Updating_known_devices);
|
|
|
|
|
|
task.MaxValue = sync.Offsets.Count;
|
|
|
|
|
|
|
|
|
|
|
|
foreach(DeviceDto device in sync.Devices)
|
|
|
|
|
|
{
|
|
|
|
|
|
task.Increment(1);
|
|
|
|
|
|
|
|
|
|
|
|
Device existing =
|
|
|
|
|
|
await mctx.Devices.FirstOrDefaultAsync(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
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Added_0_known_devices, addedDevices);
|
|
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Modified_0_known_devices, modifiedDevices);
|
2019-01-02 04:05:51 +00:00
|
|
|
|
|
2024-05-11 01:54:16 +01:00
|
|
|
|
await AnsiConsole.Progress()
|
|
|
|
|
|
.AutoClear(true)
|
|
|
|
|
|
.HideCompleted(true)
|
|
|
|
|
|
.Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn())
|
|
|
|
|
|
.StartAsync(async ctx =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ProgressTask task =
|
|
|
|
|
|
ctx.AddTask(Localization.Core.Updating_known_iNES_NES_2_0_headers);
|
|
|
|
|
|
|
|
|
|
|
|
task.MaxValue = sync.Offsets.Count;
|
|
|
|
|
|
|
|
|
|
|
|
sync.NesHeaders ??= [];
|
|
|
|
|
|
|
|
|
|
|
|
foreach(NesHeaderDto header in sync.NesHeaders)
|
|
|
|
|
|
{
|
|
|
|
|
|
task.Increment(1);
|
|
|
|
|
|
|
|
|
|
|
|
NesHeaderInfo existing =
|
|
|
|
|
|
await mctx.NesHeaders.FirstOrDefaultAsync(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,
|
|
|
|
|
|
TimingMode = header.TimingMode,
|
|
|
|
|
|
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,
|
|
|
|
|
|
TimingMode = header.TimingMode,
|
|
|
|
|
|
VsHardwareType = header.VsHardwareType,
|
|
|
|
|
|
VsPpuType = header.VsPpuType
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2021-12-08 20:34:36 +00:00
|
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Added_0_known_iNES_NES_2_0_headers, addedNesHeaders);
|
|
|
|
|
|
AaruLogging.WriteLine(Localization.Core.Modified_0_known_iNES_NES_2_0_headers, modifiedNesHeaders);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
|
{
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.Error(Localization.Core.Exception_0_when_updating_database, ex);
|
2025-08-17 06:42:43 +01:00
|
|
|
|
AaruLogging.Exception(ex, Localization.Core.Exception_0_when_updating_database, ex.Message);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
Spectre.ProgressSingleSpinner(ctx =>
|
2019-01-02 04:05:51 +00:00
|
|
|
|
{
|
2022-11-23 16:06:46 +00:00
|
|
|
|
ctx.AddTask(Localization.Core.Saving_changes).IsIndeterminate();
|
2022-03-06 13:29:38 +00:00
|
|
|
|
mctx.SaveChanges();
|
|
|
|
|
|
});
|
2019-01-02 04:05:51 +00:00
|
|
|
|
}
|
2017-05-28 21:01:17 +01:00
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|