JD2022-TU1/main/tools/modules/JD.Guinness.WPF.Module/Configuration/Game.cs

263 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using JD.Guinness.WPF.Module.Configuration.Ps4;
using JD.Guinness.WPF.Module.Configuration.Ps5;
using JD.Guinness.WPF.Module.Configuration.Stadia;
using JD.Guinness.WPF.Module.Configuration.XBoxOne;
using JD.Guinness.WPF.Module.Configuration.XBoxSeriesX;
using JD.Guinness.WPF.Module.Model;
using JD.Guinness.WPF.Module.Model.Ps4;
using JD.Guinness.WPF.Module.Model.Ps5;
using JD.Guinness.WPF.Module.Model.Stadia;
using JD.Guinness.WPF.Module.Model.XBoxOne;
using JD.Guinness.WPF.Module.Model.XBoxSeriesX;
using JD.Oasis.Service.API;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace JD.Guinness.WPF.Module.Configuration
{
public sealed class Game
{
private static readonly Dictionary<int, LanguageSave> _defaultLanguages = new[]
{
new LanguageSave { Id = 26, Ps4Id = 2, Ps5Id = "fr-FR", MicrosoftId = "fr-FR", GoogleId = "fr" },
new LanguageSave { Id = 27, Ps4Id = 4, Ps5Id = "de-DE", MicrosoftId = "de-DE", GoogleId = "de" },
new LanguageSave { Id = 28, Ps4Id = 3, Ps5Id = "es-ES", MicrosoftId = "es-ES", GoogleId = "es" },
new LanguageSave { Id = 29, Ps4Id = 5, Ps5Id = "it-IT", MicrosoftId = "it-IT", GoogleId = "it" },
new LanguageSave { Id = 30, Ps4Id = 6, Ps5Id = "nl-NL", MicrosoftId = "nl-NL", GoogleId = "nl" },
new LanguageSave { Id = 31, Ps4Id = 13, Ps5Id = "sv-SE", MicrosoftId = "sv-SE", GoogleId = "sv" },
new LanguageSave { Id = 32, Ps4Id = 15, Ps5Id = "nb-NO", MicrosoftId = "nb-NO", GoogleId = "no" },
new LanguageSave { Id = 33, Ps4Id = 12, Ps5Id = "fi-FI", MicrosoftId = "fi-FI", GoogleId = "fi" },
new LanguageSave { Id = 34, Ps4Id = 14, Ps5Id = "da-DK", MicrosoftId = "da-DK", GoogleId = "da" },
new LanguageSave { Id = 35, Ps4Id = 17, Ps5Id = "pt-BR", MicrosoftId = "pt-BR" },
new LanguageSave { Id = 36, Ps4Id = 0, Ps5Id = "ja-JP", MicrosoftId = "ja-JP" },
new LanguageSave { Id = 37, Ps4Id = 9, Ps5Id = "ko-KR", MicrosoftId = "ko-KR" },
new LanguageSave { Id = 38, Ps4Id = 1, Ps5Id = "en-US", MicrosoftId = "en-US", GoogleId = "en" },
new LanguageSave { Id = 39, Ps4Id = 11, Ps5Id = "zh-CN", MicrosoftId = "zh-CN" },
new LanguageSave { Id = 41, Ps4Id = 8, Ps5Id = "ru-RU", MicrosoftId = "ru-RU" },
new LanguageSave { Id = 42, Ps4Id = 10, Ps5Id = "zh-HK", MicrosoftId = "zh-HK" },
}.ToDictionary(l => l.Id);
private static readonly Dictionary<Language, string> _empty = new Dictionary<Language, string>();
[JsonProperty(Required = Required.Always)]
public int Version { get; set; } = 1;
[JsonProperty]
public JObject Save { get; set; }
private static string ToAbsolutePath(string path, string basePath) => string.IsNullOrEmpty(path) ? null : Path.Combine(basePath, path);
private static LocalizedLine GetLine(int? id) => id.HasValue ? new LocalizedLine(id.GetValueOrDefault(), _empty, string.Empty, false) : null;
private static Selectable<LanguageInfo> GetLanguage(int id) => new Selectable<LanguageInfo>
{
Value = new LanguageInfo(id)
{
Language = new Language(id, string.Empty, false)
},
IsSelected = true
};
public IEnumerable<LanguageInfo> GetLanguages()
{
LanguageSave GetDefault(int id) => _defaultLanguages.TryGetValue(id, out LanguageSave defaultValue) ? defaultValue : null;
foreach (LanguageSave save in Save?.GetValue(nameof(Configuration.Save.Languages))?.ToObject<LanguageSave[]>() as IEnumerable<LanguageSave> ?? _defaultLanguages.Values)
{
yield return new LanguageInfo(save.DuplicateId ?? save.Id)
{
Language = new Language(save.Id, string.Empty, false),
Ps4Id = save.Ps4Id,
Ps5Id = save.Ps5Id ?? GetDefault(save.Id)?.Ps5Id,
MicrosoftId = save.MicrosoftId ?? GetDefault(save.Id)?.MicrosoftId,
GoogleId = save.GoogleId ?? GetDefault(save.Id)?.GoogleId
};
}
}
public void SetPs4Configuration(string workspacePath, Ps4Configuration configuration)
{
Ps4ConfigurationSave save = Save?.GetValue(nameof(Configuration.Save.Ps4))?.ToObject<Ps4ConfigurationSave>();
configuration.Generate = save?.Generate is true;
configuration.Version = save?.Version ?? string.Empty;
configuration.TitleName = GetLine(save?.TitleName);
configuration.TitleDescription = GetLine(save?.TitleDescription);
configuration.TitleImagePath = ToAbsolutePath(save?.TitleImagePath, workspacePath);
configuration.Zones.Clear();
foreach (Ps4ZoneSave zoneSave in save?.Zones ?? Array.Empty<Ps4ZoneSave>())
{
Ps4Zone zone = new Ps4Zone(Array.Empty<LanguageInfo>())
{
Name = zoneSave.Name,
NpCommId = zoneSave.NpCommId,
TrpFilePath = ToAbsolutePath(zoneSave.TrpFilePath, workspacePath)
};
foreach (int language in zoneSave.Languages ?? Array.Empty<int>())
{
zone.Languages.Add(GetLanguage(language));
}
configuration.Zones.Add(zone);
}
}
public void SetPs5Configuration(string workspacePath, Ps5Configuration configuration)
{
Ps5ConfigurationSave save = Save?.GetValue(nameof(Configuration.Save.Ps5))?.ToObject<Ps5ConfigurationSave>();
configuration.Generate = save?.Generate is true;
configuration.SettingsFilePath = ToAbsolutePath(save?.SettingsFilePath, workspacePath);
configuration.Version = save?.Version ?? string.Empty;
configuration.TitleName = GetLine(save?.TitleName);
configuration.TitleImagePath = ToAbsolutePath(save?.TitleImagePath, workspacePath);
configuration.Languages.Clear();
foreach (int language in save?.Languages ?? Array.Empty<int>())
{
configuration.Languages.Add(GetLanguage(language));
}
}
public void SetXBoxOneConfiguration(string workspacePath, XBoxOneConfiguration configuration)
{
XBoxOneConfigurationSave save = Save?.GetValue(nameof(Configuration.Save.XBoxOne))?.ToObject<XBoxOneConfigurationSave>();
configuration.Generate = save?.Generate is true;
configuration.SettingsFilePath = ToAbsolutePath(save?.SettingsFilePath, workspacePath);
configuration.Languages.Clear();
foreach (int language in save?.Languages ?? Array.Empty<int>())
{
configuration.Languages.Add(GetLanguage(language));
}
}
public void SetXBoxSeriesXConfiguration(string workspacePath, XBoxSeriesXConfiguration configuration)
{
XBoxSeriesXConfigurationSave save = Save?.GetValue(nameof(Configuration.Save.XBoxSeriesX))?.ToObject<XBoxSeriesXConfigurationSave>();
configuration.Generate = save?.Generate is true;
configuration.SettingsFilePath = ToAbsolutePath(save?.SettingsFilePath, workspacePath);
configuration.Languages.Clear();
foreach (int language in save?.Languages ?? Array.Empty<int>())
{
configuration.Languages.Add(GetLanguage(language));
}
}
public void SetStadiaConfiguration(string workspacePath, StadiaConfiguration configuration)
{
StadiaConfigurationSave save = Save?.GetValue(nameof(Configuration.Save.Stadia))?.ToObject<StadiaConfigurationSave>();
configuration.Generate = save?.Generate is true;
configuration.StoreFrontFilePath = ToAbsolutePath(save?.StoreFrontFilePath, workspacePath);
configuration.SyncToStoreFront = save?.SyncToStoreFront is true;
configuration.ExcelFilePath = ToAbsolutePath(save?.ExcelFilePath, workspacePath);
configuration.IconsFolderPath = ToAbsolutePath(save?.IconsFolderPath, workspacePath);
configuration.GenerateExcel = save?.GenerateExcel is true;
configuration.Languages.Clear();
foreach (int language in save?.Languages ?? Array.Empty<int>())
{
configuration.Languages.Add(GetLanguage(language));
}
}
public IEnumerable<Challenge> GetChallenges(string workspacePath)
{
foreach (ChallengeSave save in Save?.GetValue(nameof(Configuration.Save.Challenges))?.ToObject<ChallengeSave[]>() ?? Array.Empty<ChallengeSave>())
{
Challenge challenge = new Challenge
{
Name = GetLine(save.Name),
Objective = GetLine(save.Objective)
};
challenge.CustomData.Add(save.Extra);
if (save.Ps4 != null)
{
Ps4Trophy ps4 = challenge.GetOrAddPlatformChallenge<Ps4Trophy>();
ps4.Grade = save.Ps4.Grade;
ps4.Id = save.Ps4.Id;
ps4.ImagePath = ToAbsolutePath(save.Ps4.ImagePath, workspacePath);
ps4.IsRequiredForPlatinium = save.Ps4.IsRequiredForPlatinium;
ps4.IsSecret = save.Ps4.IsSecret;
ps4.CustomData.Add(save.Ps4.Extra);
}
if (save.Ps5 != null)
{
Ps5Trophy ps5 = challenge.GetOrAddPlatformChallenge<Ps5Trophy>();
ps5.Grade = save.Ps5.Grade;
ps5.Id = save.Ps5.Id;
ps5.ImagePath = ToAbsolutePath(save.Ps5.ImagePath, workspacePath);
ps5.IsRequiredForPlatinium = save.Ps5.IsRequiredForPlatinium;
ps5.IsSecret = save.Ps5.IsSecret;
ps5.CustomData.Add(save.Ps5.Extra);
}
if (save.XBoxOne != null)
{
XBoxOneAchievement xBoxOne = challenge.GetOrAddPlatformChallenge<XBoxOneAchievement>();
xBoxOne.Gamerscore = save.XBoxOne.Gamerscore;
xBoxOne.Id = save.XBoxOne.Id;
xBoxOne.ImagePath = ToAbsolutePath(save.XBoxOne.ImagePath, workspacePath);
xBoxOne.IsBaseAchievement = save.XBoxOne.IsBaseAchievement;
xBoxOne.IsSecret = save.XBoxOne.IsSecret;
xBoxOne.LockedObjectiveId = save.XBoxOne.LockedObjectiveId;
xBoxOne.NameId = save.XBoxOne.NameId;
xBoxOne.UnlockedObjectiveId = save.XBoxOne.UnlockedObjectiveId;
xBoxOne.LockedObjective = GetLine(save.XBoxOne.LockObjective);
xBoxOne.UnlockedObjective = GetLine(save.XBoxOne.UnlockedObjective);
xBoxOne.CustomData.Add(save.XBoxOne.Extra);
}
if (save.XBoxSeriesX != null)
{
XBoxSeriesXAchievement xBoxSeriesX = challenge.GetOrAddPlatformChallenge<XBoxSeriesXAchievement>();
xBoxSeriesX.Gamerscore = save.XBoxOne.Gamerscore;
xBoxSeriesX.Id = save.XBoxOne.Id;
xBoxSeriesX.ImagePath = ToAbsolutePath(save.XBoxOne.ImagePath, workspacePath);
xBoxSeriesX.IsBaseAchievement = save.XBoxOne.IsBaseAchievement;
xBoxSeriesX.IsSecret = save.XBoxOne.IsSecret;
xBoxSeriesX.LockedObjectiveId = save.XBoxOne.LockedObjectiveId;
xBoxSeriesX.NameId = save.XBoxOne.NameId;
xBoxSeriesX.UnlockedObjectiveId = save.XBoxOne.UnlockedObjectiveId;
xBoxSeriesX.LockedObjective = GetLine(save.XBoxOne.LockObjective);
xBoxSeriesX.UnlockedObjective = GetLine(save.XBoxOne.UnlockedObjective);
xBoxSeriesX.CustomData.Add(save.XBoxOne.Extra);
}
if (save.Stadia != null)
{
StadiaAchievement stadia = challenge.GetOrAddPlatformChallenge<StadiaAchievement>();
stadia.Id = save.Stadia.Id;
stadia.Type = save.Stadia.Type;
stadia.LockedImagePath = ToAbsolutePath(save.Stadia.LockedImagePath, workspacePath);
stadia.UnlockedImagePath = ToAbsolutePath(save.Stadia.UnlockedImagePath, workspacePath);
stadia.LockedName = GetLine(save.Stadia.LockedName);
stadia.LockedObjective = GetLine(save.Stadia.LockedObjective);
stadia.UnlockedName = GetLine(save.Stadia.UnlockedName);
stadia.UnlockedObjective = GetLine(save.Stadia.UnlockedObjective);
stadia.CustomData.Add(save.Stadia.Extra);
}
yield return challenge;
}
}
}
}