Skip to content

Commit

Permalink
Merge pull request #75 from hmlendea/optimisation
Browse files Browse the repository at this point in the history
Optimisations and fixes
  • Loading branch information
hmlendea authored Jun 7, 2024
2 parents 70d746e + c6387f3 commit 5f1f6cc
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 139 deletions.
2 changes: 1 addition & 1 deletion MoreCulturalNamesBuilder/MoreCulturalNamesBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="NuciCLI" Version="1.4.2" />
<PackageReference Include="NuciDAL" Version="2.1.0" />
<PackageReference Include="NuciDAL" Version="2.2.0" />
<PackageReference Include="NuciExtensions" Version="4.0.0" />
</ItemGroup>

Expand Down
20 changes: 11 additions & 9 deletions MoreCulturalNamesBuilder/Service/LocalisationFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public IEnumerable<Localisation> GetGameLocationLocalisations(
string locationGameIdType,
string game)
{
ConcurrentBag<Localisation> localisations = new ConcurrentBag<Localisation>();
ConcurrentBag<Localisation> localisations = [];

Location location;

Expand Down Expand Up @@ -108,8 +108,8 @@ Localisation GetLocationLocalisation(Location location, string languageId)

Language language = languages[languageId];

List<string> locationIdsToCheck = new List<string>() { location.Id };
List<string> languageIdsToCheck = new List<string>() { language.Id };
List<string> locationIdsToCheck = [location.Id];
List<string> languageIdsToCheck = [language.Id];

locationIdsToCheck.AddRange(location.FallbackLocations);
languageIdsToCheck.AddRange(language.FallbackLanguages);
Expand All @@ -120,12 +120,14 @@ Localisation GetLocationLocalisation(Location location, string languageId)
{
foreach (Name name in locations[locationIdToCheck].Names.Where(name => name.LanguageId.Equals(languageIdToCheck)))
{
Localisation localisation = new Localisation();
localisation.Id = locationIdToCheck;
localisation.LanguageId = languageIdToCheck;
localisation.Name = name.Value;
localisation.Adjective = name.Adjective;
localisation.Comment = name.Comment;
Localisation localisation = new()
{
Id = locationIdToCheck,
LanguageId = languageIdToCheck,
Name = name.Value,
Adjective = name.Adjective,
Comment = name.Comment
};

return localisation;
}
Expand Down
28 changes: 16 additions & 12 deletions MoreCulturalNamesBuilder/Service/Mapping/GameIdMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@ static class GameIdMapping
{
internal static GameId ToServiceModel(this GameIdEntity dataObject)
{
GameId serviceModel = new GameId();
serviceModel.Game = dataObject.Game;
serviceModel.Type = dataObject.Type;
serviceModel.Parent = dataObject.Parent;
serviceModel.DefaultNameLanguageId = dataObject.DefaultNameLanguageId;
serviceModel.Id = dataObject.Id;
GameId serviceModel = new()
{
Game = dataObject.Game,
Type = dataObject.Type,
Parent = dataObject.Parent,
DefaultNameLanguageId = dataObject.DefaultNameLanguageId,
Id = dataObject.Id
};

return serviceModel;
}

internal static GameIdEntity ToDataObject(this GameId serviceModel)
{
GameIdEntity dataObject = new GameIdEntity();
dataObject.Game = serviceModel.Game;
dataObject.Type = serviceModel.Type;
dataObject.Parent = serviceModel.Parent;
dataObject.DefaultNameLanguageId = serviceModel.DefaultNameLanguageId;
dataObject.Id = serviceModel.Id;
GameIdEntity dataObject = new()
{
Game = serviceModel.Game,
Type = serviceModel.Type,
Parent = serviceModel.Parent,
DefaultNameLanguageId = serviceModel.DefaultNameLanguageId,
Id = serviceModel.Id
};

return dataObject;
}
Expand Down
20 changes: 12 additions & 8 deletions MoreCulturalNamesBuilder/Service/Mapping/LanguageCodeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ static class LanguageCodeMapping
{
internal static LanguageCode ToServiceModel(this LanguageCodeEntity dataObject)
{
LanguageCode serviceModel = new LanguageCode();
serviceModel.ISO_639_1 = dataObject.ISO_639_1;
serviceModel.ISO_639_2 = dataObject.ISO_639_2;
serviceModel.ISO_639_3 = dataObject.ISO_639_3;
LanguageCode serviceModel = new()
{
ISO_639_1 = dataObject.ISO_639_1,
ISO_639_2 = dataObject.ISO_639_2,
ISO_639_3 = dataObject.ISO_639_3
};

return serviceModel;
}

internal static LanguageCodeEntity ToDataObject(this LanguageCode serviceModel)
{
LanguageCodeEntity dataObject = new LanguageCodeEntity();
dataObject.ISO_639_1 = serviceModel.ISO_639_1;
dataObject.ISO_639_2 = serviceModel.ISO_639_2;
dataObject.ISO_639_3 = serviceModel.ISO_639_3;
LanguageCodeEntity dataObject = new()
{
ISO_639_1 = serviceModel.ISO_639_1,
ISO_639_2 = serviceModel.ISO_639_2,
ISO_639_3 = serviceModel.ISO_639_3
};

return dataObject;
}
Expand Down
24 changes: 14 additions & 10 deletions MoreCulturalNamesBuilder/Service/Mapping/LanguageMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@ static class LanguageMapping
{
internal static Language ToServiceModel(this LanguageEntity dataObject)
{
Language serviceModel = new Language();
serviceModel.Id = dataObject.Id;
serviceModel.Code = dataObject.Code?.ToServiceModel();
serviceModel.GameIds = dataObject.GameIds.ToServiceModels();
serviceModel.FallbackLanguages = dataObject.FallbackLanguages.ToList();
Language serviceModel = new()
{
Id = dataObject.Id,
Code = dataObject.Code?.ToServiceModel(),
GameIds = dataObject.GameIds.ToServiceModels(),
FallbackLanguages = dataObject.FallbackLanguages.ToList()
};

return serviceModel;
}

internal static LanguageEntity ToDataObject(this Language serviceModel)
{
LanguageEntity dataObject = new LanguageEntity();
dataObject.Id = serviceModel.Id;
dataObject.Code = serviceModel.Code?.ToDataObject();
dataObject.GameIds = serviceModel.GameIds.ToDataObjects().ToList();
dataObject.FallbackLanguages = serviceModel.FallbackLanguages.ToList();
LanguageEntity dataObject = new()
{
Id = serviceModel.Id,
Code = serviceModel.Code?.ToDataObject(),
GameIds = serviceModel.GameIds.ToDataObjects().ToList(),
FallbackLanguages = serviceModel.FallbackLanguages.ToList()
};

return dataObject;
}
Expand Down
28 changes: 16 additions & 12 deletions MoreCulturalNamesBuilder/Service/Mapping/LocationMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@ static class LocationMapping
{
internal static Location ToServiceModel(this LocationEntity dataObject)
{
Location serviceModel = new Location();
serviceModel.Id = dataObject.Id;
serviceModel.GeoNamesId = dataObject.GeoNamesId;
serviceModel.GameIds = dataObject.GameIds.ToServiceModels();
serviceModel.FallbackLocations = dataObject.FallbackLocations;
serviceModel.Names = dataObject.Names.ToServiceModels();
Location serviceModel = new()
{
Id = dataObject.Id,
GeoNamesId = dataObject.GeoNamesId,
GameIds = dataObject.GameIds.ToServiceModels(),
FallbackLocations = dataObject.FallbackLocations,
Names = dataObject.Names.ToServiceModels()
};

return serviceModel;
}

internal static LocationEntity ToDataObject(this Location serviceModel)
{
LocationEntity dataObject = new LocationEntity();
dataObject.Id = serviceModel.Id;
dataObject.GeoNamesId = serviceModel.GeoNamesId;
dataObject.GameIds = serviceModel.GameIds.ToDataObjects().ToList();
dataObject.FallbackLocations = serviceModel.FallbackLocations.ToList();
dataObject.Names = serviceModel.Names.ToDataObjects().ToList();
LocationEntity dataObject = new()
{
Id = serviceModel.Id,
GeoNamesId = serviceModel.GeoNamesId,
GameIds = serviceModel.GameIds.ToDataObjects().ToList(),
FallbackLocations = serviceModel.FallbackLocations.ToList(),
Names = serviceModel.Names.ToDataObjects().ToList()
};

return dataObject;
}
Expand Down
24 changes: 14 additions & 10 deletions MoreCulturalNamesBuilder/Service/Mapping/NameMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@ static class NameMapping
{
internal static Name ToServiceModel(this NameEntity dataObject)
{
Name serviceModel = new Name();
serviceModel.LanguageId = dataObject.LanguageId;
serviceModel.Value = dataObject.Value;
serviceModel.Adjective = dataObject.Adjective;
serviceModel.Comment = dataObject.Comment;
Name serviceModel = new()
{
LanguageId = dataObject.LanguageId,
Value = dataObject.Value,
Adjective = dataObject.Adjective,
Comment = dataObject.Comment
};

return serviceModel;
}

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

return dataObject;
}
Expand Down
75 changes: 37 additions & 38 deletions MoreCulturalNamesBuilder/Service/ModBuilders/CK2ModBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@
using MoreCulturalNamesBuilder.Configuration;
using MoreCulturalNamesBuilder.DataAccess.DataObjects;
using MoreCulturalNamesBuilder.Service.Models;
using NuciDAL.IO;

namespace MoreCulturalNamesBuilder.Service.ModBuilders
{
public class CK2ModBuilder : ModBuilder
{
protected virtual string LocalisationDirectoryName => "localisation";

protected virtual List<string> ForbiddenTokensForPreviousLine => new List<string>
{ "allow", "dejure_liege_title", "gain_effect", "limit", "trigger" };
protected virtual List<string> ForbiddenTokensForPreviousLine => [
"allow", "dejure_liege_title", "gain_effect", "limit", "trigger"];

protected virtual List<string> ForbiddenTokensForNextLine => new List<string>
{ "any_direct_de_jure_vassal_title", "has_holder", "is_titular", "owner", "show_scope_change" };
protected virtual List<string> ForbiddenTokensForNextLine => [
"any_direct_de_jure_vassal_title", "has_holder", "is_titular", "owner", "show_scope_change"];

readonly ILocalisationFetcher localisationFetcher;
readonly INameNormaliser nameNormaliser;
Expand All @@ -48,8 +49,7 @@ public CK2ModBuilder(

protected override void LoadData()
{
ConcurrentDictionary<string, IEnumerable<Localisation>> concurrentLocalisations =
new ConcurrentDictionary<string, IEnumerable<Localisation>>();
ConcurrentDictionary<string, IEnumerable<Localisation>> concurrentLocalisations = new();

Parallel.ForEach(locationGameIds, locationGameId =>
{
Expand Down Expand Up @@ -113,7 +113,7 @@ protected virtual string GetTitleLocalisationsContent(string line, string gameId
}

string indentation = Regex.Match(line, "^(\\s*)" + gameId + "\\s*=\\s*\\{.*$").Groups[1].Value + " ";
List<string> lines = new List<string>();
List<string> lines = [];

foreach (Localisation localisation in titleLocalisations.OrderBy(x => x.LanguageGameId))
{
Expand Down Expand Up @@ -142,7 +142,7 @@ protected virtual string ReadLandedTitlesFile()
Encoding.GetEncoding("windows-1252"));

protected virtual void WriteLandedTitlesFile(string filePath, string content)
=> WriteWindows1252File(filePath, content);
=> Windows1252File.WriteAllText(filePath, content);

protected virtual string DoCleanLandedTitlesFile(string content)
{
Expand All @@ -168,18 +168,15 @@ protected virtual void CreateLocalisationFiles(string localisationDirectoryPath)
string localisationsDirectoryPath = Path.Combine(OutputDirectoryPath, Settings.Mod.Id, LocalisationDirectoryName);
Directory.CreateDirectory(localisationsDirectoryPath);

foreach (GameId languageGameId in languageGameIds)
{
string filePath = Path.Combine(localisationsDirectoryPath, $"000_{Settings.Mod.Id}_landed_titles.csv");
string content = GenerateLocalisationFileContent();

if (string.IsNullOrWhiteSpace(content))
{
continue;
}
string filePath = Path.Combine(localisationsDirectoryPath, $"000_{Settings.Mod.Id}_landed_titles.csv");
string content = GenerateLocalisationFileContent();

WriteWindows1252File(filePath, content);
if (string.IsNullOrWhiteSpace(content))
{
return;
}

Windows1252File.WriteAllText(filePath, content);
}

void CreateLandedTitlesFile(string landedTitlesDirectoryPath)
Expand All @@ -195,7 +192,7 @@ string BuildLandedTitlesFile()
string landedTitlesFile = ReadLandedTitlesFile();
landedTitlesFile = CleanLandedTitlesFile(landedTitlesFile);

List<string> content = new List<string> { string.Empty };
List<string> content = [string.Empty];
List<string> landedTitlesFileLines = landedTitlesFile.Split('\n').ToList();
landedTitlesFileLines.Add(string.Empty);

Expand Down Expand Up @@ -270,50 +267,52 @@ string CleanLandedTitlesFile(string content)

string GenerateLocalisationFileContent()
{
ConcurrentBag<string> lines = new ConcurrentBag<string>();
ConcurrentBag<string> lines = [];

Parallel.ForEach(localisations.Keys, locationGameId =>
Parallel.ForEach(localisations, localisationKvp =>
{
foreach (Localisation localisation in localisations[locationGameId])
string locationGameId = localisationKvp.Key;

foreach (Localisation localisation in localisationKvp.Value)
{
GameId gameId = locationGameIds.First(x => x.Id.Equals(locationGameId));

if (localisation.LanguageId.Equals(gameId.DefaultNameLanguageId))
{
string normalisedName = nameNormaliser.ToWindows1252(localisation.Name);
lines.Add(
$"{locationGameId}" +
$";{normalisedName};{normalisedName};{normalisedName};;{normalisedName};;;;;;;;;x");
lines.Add(GenerateLocationLocalisationLine(
locationGameId,
localisation.Name));
}

if (string.IsNullOrWhiteSpace(localisation.Adjective))
{
continue;
}

string normalisedAdjective = nameNormaliser.ToWindows1252(localisation.Adjective);
lines.Add(
$"{locationGameId}_adj_{localisation.LanguageGameId}" +
$";{normalisedAdjective};{normalisedAdjective};{normalisedAdjective};;{normalisedAdjective};;;;;;;;;x");
lines.Add(GenerateLocationLocalisationLine(
$"{locationGameId}_adj_{localisation.LanguageGameId}",
localisation.Adjective));

if (localisation.LanguageId.Equals(gameId.DefaultNameLanguageId))
{
lines.Add(
$"{locationGameId}_adj" +
$";{normalisedAdjective};{normalisedAdjective};{normalisedAdjective};;{normalisedAdjective};;;;;;;;;x");
lines.Add(GenerateLocationLocalisationLine(
$"{locationGameId}_adj",
localisation.Adjective));
}
};
}
});

return string.Join(Environment.NewLine, lines.OrderBy(x => x));
}

void WriteWindows1252File(string filePath, string content)
string GenerateLocationLocalisationLine(string localisationKey, string localisationValue)
{
Encoding encoding = Encoding.GetEncoding("windows-1252");
byte[] contentBytes = encoding.GetBytes(content.ToCharArray());
string normalisedName = nameNormaliser.ToWindows1252(localisationValue);
string localisationDefinition =
$"{localisationKey}" +
$";{normalisedName};{normalisedName};{normalisedName};;{normalisedName};;;;;;;;;x";

File.WriteAllBytes(filePath, contentBytes);
return localisationDefinition;
}
}
}
Loading

0 comments on commit 5f1f6cc

Please sign in to comment.