Skip to content

Commit

Permalink
Merge pull request #18 from hmlendea/titles
Browse files Browse the repository at this point in the history
Title localisations generator for CK2
  • Loading branch information
hmlendea authored Oct 3, 2020
2 parents 4806c67 + b7080c9 commit 9ab2732
Show file tree
Hide file tree
Showing 21 changed files with 325 additions and 82 deletions.
2 changes: 2 additions & 0 deletions Configuration/DataStoreSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public sealed class DataStoreSettings
{
public string LanguageStorePath { get; set; }

public string LocationStorePath { get; set; }

public string TitleStorePath { get; set; }

public string ModOutputDirectory { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion DataAccess/DataObjects/LocationEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public class LocationEntity : EntityBase

public List<GameIdEntity> GameIds { get; set; }

public List<LocationNameEntity> Names { get; set; }
public List<NameEntity> Names { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace MoreCulturalNamesModBuilder.DataAccess.DataObjects
{
[XmlType("Name")]
public class LocationNameEntity
public class NameEntity
{
public LocationNameEntity()
public NameEntity()
{

}

public LocationNameEntity(string language, string value)
public NameEntity(string language, string value)
{
LanguageId = language;
Value = value;
Expand Down
17 changes: 17 additions & 0 deletions DataAccess/DataObjects/TitleEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.Xml.Serialization;

using NuciDAL.DataObjects;

namespace MoreCulturalNamesModBuilder.DataAccess.DataObjects
{
public class TitleEntity : EntityBase
{
[XmlArrayItem("TitleId")]
public List<string> FallbackTitles { get; set; }

public List<GameIdEntity> GameIds { get; set; }

public List<NameEntity> Names { get; set; }
}
}
11 changes: 7 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public class Program
static DataStoreSettings dataStoreSettings = null;
static OutputSettings outputSettings = null;

static string[] LanguageStorePathOptions = { "-l", "--languages" };
static string[] TitlesStorePathOptions = { "-t", "--titles" };
static string[] LanguageStorePathOptions = { "--lang", "--languages" };
static string[] LocationsStorePathOptions = { "--loc", "--locations" };
static string[] TitleStorePathOptions = { "-t", "--titles" };
static string[] VersionOptions = { "-v", "--ver", "--version" };
static string[] OutputDirectoryPathOptions = { "-o", "--out", "--output" };

Expand All @@ -39,7 +40,8 @@ public static void Main(string[] args)
.AddSingleton(dataStoreSettings)
.AddSingleton(outputSettings)
.AddSingleton<IRepository<LanguageEntity>>(s => new XmlRepository<LanguageEntity>(dataStoreSettings.LanguageStorePath))
.AddSingleton<IRepository<LocationEntity>>(s => new XmlRepository<LocationEntity>(dataStoreSettings.TitleStorePath))
.AddSingleton<IRepository<LocationEntity>>(s => new XmlRepository<LocationEntity>(dataStoreSettings.LocationStorePath))
.AddSingleton<IRepository<TitleEntity>>(s => new XmlRepository<TitleEntity>(dataStoreSettings.TitleStorePath))
.AddSingleton<ILocalisationFetcher, LocalisationFetcher>()
.AddSingleton<INameNormaliser, NameNormaliser>()
.AddSingleton<ICK2ModBuilder, CK2ModBuilder>()
Expand Down Expand Up @@ -75,7 +77,8 @@ static void LoadConfiguration(string[] args)
config.Bind(nameof(OutputSettings), outputSettings);

dataStoreSettings.LanguageStorePath = CliArgumentsReader.GetOptionValue(args, LanguageStorePathOptions);
dataStoreSettings.TitleStorePath = CliArgumentsReader.GetOptionValue(args, TitlesStorePathOptions);
dataStoreSettings.LocationStorePath = CliArgumentsReader.GetOptionValue(args, LocationsStorePathOptions);
dataStoreSettings.TitleStorePath = CliArgumentsReader.GetOptionValue(args, TitleStorePathOptions);
outputSettings.ModVersion = CliArgumentsReader.GetOptionValue(args, VersionOptions);
outputSettings.ModOutputDirectory = CliArgumentsReader.GetOptionValue(args, OutputDirectoryPathOptions);
}
Expand Down
2 changes: 2 additions & 0 deletions Service/ILocalisationFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ namespace MoreCulturalNamesModBuilder.Service
public interface ILocalisationFetcher
{
IEnumerable<Localisation> GetGameLocationLocalisations(string locationGameId, string gameId);

Localisation GetTitleLocalisation(string titleId, string languageId, string game);
}
}
60 changes: 56 additions & 4 deletions Service/LocalisationFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ public sealed class LocalisationFetcher : ILocalisationFetcher
{
readonly IRepository<LanguageEntity> languageRepository;
readonly IRepository<LocationEntity> locationRepository;
readonly IRepository<TitleEntity> titleRepository;

readonly ConcurrentDictionary<string, IDictionary<string, string>> languageGameIdsCache;

IDictionary<string, Location> locations;
IDictionary<string, Language> languages;
IDictionary<string, Title> titles;

public LocalisationFetcher(
IRepository<LanguageEntity> languageRepository,
IRepository<LocationEntity> locationRepository)
IRepository<LocationEntity> locationRepository,
IRepository<TitleEntity> titleRepository)
{
this.languageRepository = languageRepository;
this.locationRepository = locationRepository;
this.titleRepository = titleRepository;

languageGameIdsCache = new ConcurrentDictionary<string, IDictionary<string, string>>();

Expand All @@ -44,6 +48,11 @@ void LoadData()
.GetAll()
.ToServiceModels()
.ToDictionary(key => key.Id, val => val);

titles = titleRepository
.GetAll()
.ToServiceModels()
.ToDictionary(key => key.Id, val => val);
}

public IEnumerable<Localisation> GetGameLocationLocalisations(string locationGameId, string game)
Expand All @@ -68,7 +77,7 @@ public IEnumerable<Localisation> GetGameLocationLocalisations(string locationGam
return;
}
localisation.LocationGameId = locationGameId;
localisation.GameId = locationGameId;
localisation.LanguageGameId = languageGameId.Key;
localisations.Add(localisation);
Expand All @@ -77,6 +86,49 @@ public IEnumerable<Localisation> GetGameLocationLocalisations(string locationGam
return localisations;
}

public Localisation GetTitleLocalisation(string titleId, string languageGameId, string game)
{
IList<Localisation> localisations = new List<Localisation>();
Title title = titles[titleId];

if (title.IsEmpty())
{
return null;
}

Language language = languages.Values.
First(lang => lang.GameIds.Any(langGameId =>
langGameId.Game == game &&
langGameId.Id == languageGameId));

List<string> titleIdsToCheck = new List<string>() { title.Id };
List<string> languageIdsToCheck = new List<string>() { language.Id };

titleIdsToCheck.AddRange(title.FallbackTitles);
languageIdsToCheck.AddRange(language.FallbackLanguages);

foreach (string titleIdToCheck in titleIdsToCheck)
{
foreach (string languageIdToCheck in languageIdsToCheck)
{
foreach (Name name in titles[titleIdToCheck].Names)
{
if (name.LanguageId == languageIdToCheck)
{
Localisation localisation = new Localisation();
localisation.Id = titleIdToCheck;
localisation.LanguageId = languageIdToCheck;
localisation.Name = name.Value;

return localisation;
}
}
}
}

return null;
}

Localisation GetLocationLocalisation(Location location, string languageId)
{
if (location.IsEmpty())
Expand All @@ -96,12 +148,12 @@ Localisation GetLocationLocalisation(Location location, string languageId)
{
foreach (string languageIdToCheck in languageIdsToCheck)
{
foreach (LocationName name in locations[locationIdToCheck].Names)
foreach (Name name in locations[locationIdToCheck].Names)
{
if (name.LanguageId == languageIdToCheck)
{
Localisation localisation = new Localisation();
localisation.LocationId = locationIdToCheck;
localisation.Id = locationIdToCheck;
localisation.LanguageId = languageIdToCheck;
localisation.Name = name.Value;

Expand Down
43 changes: 0 additions & 43 deletions Service/Mapping/LocationNameMapping.cs

This file was deleted.

43 changes: 43 additions & 0 deletions Service/Mapping/NameMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Linq;

using MoreCulturalNamesModBuilder.DataAccess.DataObjects;
using MoreCulturalNamesModBuilder.Service.Models;

namespace MoreCulturalNamesModBuilder.Service.Mapping
{
static class NameMapping
{
internal static Name ToServiceModel(this NameEntity dataObject)
{
Name serviceModel = new Name();
serviceModel.LanguageId = dataObject.LanguageId;
serviceModel.Value = dataObject.Value;

return serviceModel;
}

internal static NameEntity ToDataObject(this Name serviceModel)
{
NameEntity dataObject = new NameEntity();
dataObject.LanguageId = serviceModel.LanguageId;
dataObject.Value = serviceModel.Value;

return dataObject;
}

internal static IEnumerable<Name> ToServiceModels(this IEnumerable<NameEntity> dataObjects)
{
IEnumerable<Name> serviceModels = dataObjects.Select(dataObject => dataObject.ToServiceModel());

return serviceModels;
}

internal static IEnumerable<NameEntity> ToDataObjects(this IEnumerable<Name> serviceModels)
{
IEnumerable<NameEntity> dataObjects = serviceModels.Select(serviceModel => serviceModel.ToDataObject());

return dataObjects;
}
}
}
47 changes: 47 additions & 0 deletions Service/Mapping/TitleMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Generic;
using System.Linq;

using MoreCulturalNamesModBuilder.DataAccess.DataObjects;
using MoreCulturalNamesModBuilder.Service.Models;

namespace MoreCulturalNamesModBuilder.Service.Mapping
{
static class TitleMapping
{
internal static Title ToServiceModel(this TitleEntity dataObject)
{
Title serviceModel = new Title();
serviceModel.Id = dataObject.Id;
serviceModel.GameIds = dataObject.GameIds.ToServiceModels();
serviceModel.FallbackTitles = dataObject.FallbackTitles;
serviceModel.Names = dataObject.Names.ToServiceModels();

return serviceModel;
}

internal static TitleEntity ToDataObject(this Title serviceModel)
{
TitleEntity dataObject = new TitleEntity();
dataObject.Id = serviceModel.Id;
dataObject.GameIds = serviceModel.GameIds.ToDataObjects().ToList();
dataObject.FallbackTitles = serviceModel.FallbackTitles.ToList();
dataObject.Names = serviceModel.Names.ToDataObjects().ToList();

return dataObject;
}

internal static IEnumerable<Title> ToServiceModels(this IEnumerable<TitleEntity> dataObjects)
{
IEnumerable<Title> serviceModels = dataObjects.Select(dataObject => dataObject.ToServiceModel());

return serviceModels;
}

internal static IEnumerable<TitleEntity> ToDataObjects(this IEnumerable<Title> serviceModels)
{
IEnumerable<TitleEntity> dataObjects = serviceModels.Select(serviceModel => serviceModel.ToDataObject());

return dataObjects;
}
}
}
2 changes: 2 additions & 0 deletions Service/ModBuilders/CrusaderKings2/CK2HIPModBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ public CK2HIPModBuilder(
INameNormaliser nameNormaliser,
IRepository<LanguageEntity> languageRepository,
IRepository<LocationEntity> locationRepository,
IRepository<TitleEntity> titleRepository,
OutputSettings outputSettings)
: base(
localisationFetcher,
nameNormaliser,
languageRepository,
locationRepository,
titleRepository,
outputSettings)
{
}
Expand Down
Loading

0 comments on commit 9ab2732

Please sign in to comment.