-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from hmlendea/dev
Small improvements
- Loading branch information
Showing
5 changed files
with
121 additions
and
492,358 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.