Skip to content

Commit

Permalink
Merge pull request #4 from hmlendea/dev
Browse files Browse the repository at this point in the history
Small improvements
  • Loading branch information
hmlendea authored Nov 27, 2020
2 parents 4e9ac9b + 4521a67 commit fbf408b
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 492,358 deletions.
68 changes: 8 additions & 60 deletions BusinessLogic/LandedTitleManager.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

using NuciExtensions;

using CKCulturalNamesManager.DataAccess.DataObjects;
using CKCulturalNamesManager.DataAccess.IO;
using CKCulturalNamesManager.BusinessLogic.Mapping;
using CKCulturalNamesManager.DataAccess;
using CKCulturalNamesManager.BusinessLogic.Models;
using CKCulturalNamesManager.Models;

Expand All @@ -20,11 +16,13 @@ public sealed class LandedTitleManager
List<LandedTitle> landedTitles;

readonly INameValidator nameValidator;
readonly IFileManager fileManager;

public LandedTitleManager()
{
landedTitles = new List<LandedTitle>();
nameValidator = new NameValidator();
fileManager = new FileManager();

EncodingProvider encodingProvider = CodePagesEncodingProvider.Instance;
Encoding.RegisterProvider(encodingProvider);
Expand Down Expand Up @@ -64,7 +62,7 @@ public void RemoveNames()

public bool CheckIntegrity(string fileName)
{
IEnumerable<LandedTitle> masterTitles = GetTitlesFromFile(fileName);
IEnumerable<LandedTitle> masterTitles = fileManager.Read(fileName);

Dictionary<string, string> violations = new Dictionary<string, string>();

Expand Down Expand Up @@ -127,7 +125,7 @@ public IDictionary<string, int> GetNamesCount()

public IEnumerable<OverwrittenName> GetOverwrittenNames(string fileName)
{
IEnumerable<LandedTitle> masterTitles = GetTitlesFromFile(fileName);
IEnumerable<LandedTitle> masterTitles = fileManager.Read(fileName);
IList<OverwrittenName> overwrittenNames = new List<OverwrittenName>();

foreach (LandedTitle title in landedTitles)
Expand Down Expand Up @@ -160,26 +158,9 @@ public IEnumerable<OverwrittenName> GetOverwrittenNames(string fileName)
return overwrittenNames;
}

public void SaveTitlesCK2(string fileName)
public void SaveTitles(string fileName, string game)
{
LandedTitlesFile<CK2LandedTitleDefinition>.WriteAllTitles(fileName, landedTitles.ToEntities());
string content = ReadWindows1252File(fileName);

content = Regex.Replace(content, "\t", " ");
content = Regex.Replace(content, "= *(\r\n|\r|\n).*{", "={");
content = Regex.Replace(content, "=", " = ");
content = Regex.Replace(content, "\"(\r\n|\r|\n)( *[ekdcb]_)", "\"\n\n$2");

WriteWindows1252File(fileName, content);

List<LandedTitle> oldLandedTitles= landedTitles.ToList();
landedTitles = new List<LandedTitle>();
landedTitles = LoadTitlesFromFile(fileName).ToList();
}

public void SaveTitlesCK3(string fileName)
{
LandedTitlesFile<CK3LandedTitleDefinition>.WriteAllTitles(fileName, landedTitles.ToEntities());
fileManager.Write(landedTitles, fileName, game);
}

// TODO: Better name
Expand Down Expand Up @@ -218,21 +199,6 @@ public List<string> GetNamesOfCultures(List<string> cultureIds)
return findings;
}

string ReadWindows1252File(string filePath)
{
Encoding encoding = Encoding.GetEncoding("windows-1252");

return File.ReadAllText(filePath, encoding);
}

void WriteWindows1252File(string filePath, string content)
{
Encoding encoding = Encoding.GetEncoding("windows-1252");
byte[] contentBytes = encoding.GetBytes(content.ToCharArray());

File.WriteAllBytes(filePath, contentBytes);
}

IEnumerable<LandedTitle> MergeTitles(IEnumerable<LandedTitle> landedTitles)
{
return landedTitles
Expand Down Expand Up @@ -287,28 +253,10 @@ void AddReasonToViolations(Dictionary<string, string> violations, string titleId

IEnumerable<LandedTitle> LoadTitlesFromFile(string fileName)
{
IEnumerable<LandedTitle> titles = GetTitlesFromFile(fileName);
IEnumerable<LandedTitle> titles = fileManager.Read(fileName);
titles = MergeTitles(titles);

return titles;
}

IEnumerable<LandedTitle> GetTitlesFromFile(string fileName)
{
string content = File.ReadAllText(fileName);

IEnumerable<LandedTitleEntity> masterTitleEntities;

if (content.Contains("cultural_names"))
{
masterTitleEntities = LandedTitlesFile<CK3LandedTitleDefinition>.ReadAllTitles(fileName);
}
else
{
masterTitleEntities = LandedTitlesFile<CK2LandedTitleDefinition>.ReadAllTitles(fileName);
}

return masterTitleEntities.ToDomainModels();
}
}
}
97 changes: 97 additions & 0 deletions DataAccess/FileManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

using CKCulturalNamesManager.DataAccess.DataObjects;
using CKCulturalNamesManager.DataAccess.IO;
using CKCulturalNamesManager.BusinessLogic.Mapping;
using CKCulturalNamesManager.Models;

namespace CKCulturalNamesManager.DataAccess
{
public sealed class FileManager : IFileManager
{
public IEnumerable<LandedTitle> Read(string fileName)
{
string filePath = NormaliseFilePath(fileName);
string content = File.ReadAllText(filePath);

IEnumerable<LandedTitleEntity> masterTitleEntities;

if (content.Contains("cultural_names"))
{
masterTitleEntities = LandedTitlesFile<CK3LandedTitleDefinition>.ReadAllTitles(filePath);
}
else
{
masterTitleEntities = LandedTitlesFile<CK2LandedTitleDefinition>.ReadAllTitles(filePath);
}

return masterTitleEntities.ToDomainModels();
}

public void Write(IEnumerable<LandedTitle> landedTitles, string filePath, string game)
{
switch (game.ToLowerInvariant())
{
case "ck2":
WriteCK2(landedTitles, filePath);
break;

case "ck3":
WriteCK3(landedTitles, filePath);
break;

default:
throw new ArgumentException("Invalid game");
}
}

void WriteCK2(IEnumerable<LandedTitle> landedTitles, string fileName)
{
string filePath = NormaliseFilePath(fileName);
LandedTitlesFile<CK2LandedTitleDefinition>.WriteAllTitles(filePath, landedTitles.ToEntities());
string content = ReadWindows1252File(filePath);

content = Regex.Replace(content, "\t", " ");
content = Regex.Replace(content, "= *(\r\n|\r|\n).*{", "={");
content = Regex.Replace(content, "=", " = ");
content = Regex.Replace(content, "\"(\r\n|\r|\n)( *[ekdcb]_)", "\"\n\n$2");

WriteWindows1252File(filePath, content);
}

void WriteCK3(IEnumerable<LandedTitle> landedTitles, string fileName)
{
string filePath = NormaliseFilePath(fileName);
LandedTitlesFile<CK3LandedTitleDefinition>.WriteAllTitles(filePath, landedTitles.ToEntities());
}

string ReadWindows1252File(string filePath)
{
Encoding encoding = Encoding.GetEncoding("windows-1252");

return File.ReadAllText(filePath, encoding);
}

void WriteWindows1252File(string filePath, string content)
{
Encoding encoding = Encoding.GetEncoding("windows-1252");
byte[] contentBytes = encoding.GetBytes(content.ToCharArray());

File.WriteAllBytes(filePath, contentBytes);
}

string NormaliseFilePath(string filePath)
{
if (filePath.EndsWith(".txt"))
{
return filePath;
}

return $"{filePath}.txt";
}
}
}
13 changes: 13 additions & 0 deletions DataAccess/IFileManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;

using CKCulturalNamesManager.Models;

namespace CKCulturalNamesManager.DataAccess
{
public interface IFileManager
{
IEnumerable<LandedTitle> Read(string fileName);

void Write(IEnumerable<LandedTitle> landedTitles, string filePath, string game);
}
}
12 changes: 3 additions & 9 deletions Menus/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,21 +221,15 @@ private void SaveFile()

NuciConsole.Write("Writing output... ");

if (game.ToUpper() == "CK2")
try
{
landedTitleManager.SaveTitlesCK2(fileName);
landedTitleManager.SaveTitles(fileName, game);
}
else if (game.ToUpper() == "CK3")
{
landedTitleManager.SaveTitlesCK3(fileName);
}
else
catch (ArgumentException)
{
NuciConsole.WriteLine("ERROR: Invalid game");
return;
}

NuciConsole.WriteLine("OK");
}

/// <summary>
Expand Down
Loading

0 comments on commit fbf408b

Please sign in to comment.