Skip to content

Commit

Permalink
VerifyImperatorVersion, VerifyCK3Version (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains authored Sep 5, 2021
1 parent 321fec8 commit e7af714
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 101 deletions.
222 changes: 134 additions & 88 deletions ImperatorToCK3/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,99 +3,145 @@
using commonItems;

namespace ImperatorToCK3 {
public enum IMPERATOR_DE_JURE { REGIONS = 1, COUNTRIES = 2, NO = 3 };
public class Configuration : Parser {
public string SaveGamePath { get; internal set; } = "";
public string ImperatorPath { get; internal set; } = "";
public string ImperatorDocPath { get; internal set; } = "";
public string Ck3Path { get; internal set; } = "";
public string Ck3ModsPath { get; internal set; } = "";
public string OutputModName { get; internal set; } = "";
public IMPERATOR_DE_JURE ImperatorDeJure { get; internal set; } = IMPERATOR_DE_JURE.NO;
public bool ConvertBirthAndDeathDates { get; internal set; } = true;
public enum IMPERATOR_DE_JURE { REGIONS = 1, COUNTRIES = 2, NO = 3 };
public class Configuration : Parser {
public string SaveGamePath { get; internal set; } = "";
public string ImperatorPath { get; internal set; } = "";
public string ImperatorDocPath { get; internal set; } = "";
public string Ck3Path { get; internal set; } = "";
public string Ck3ModsPath { get; internal set; } = "";
public string OutputModName { get; internal set; } = "";
public IMPERATOR_DE_JURE ImperatorDeJure { get; internal set; } = IMPERATOR_DE_JURE.NO;
public bool ConvertBirthAndDeathDates { get; internal set; } = true;

public Configuration() {
Logger.Info("Reading configuration file");
RegisterKeys();
ParseFile("configuration.txt");
ClearRegisteredRules();
SetOutputName();
VerifyImperatorPath();
VerifyCK3Path();
}
public Configuration(ConverterVersion converterVersion) {
Logger.Info("Reading configuration file");
RegisterKeys();
ParseFile("configuration.txt");
ClearRegisteredRules();
SetOutputName();
VerifyImperatorPath();
VerifyImperatorVersion(converterVersion);
VerifyCK3Path();
VerifyCK3Version(converterVersion);
}

void RegisterKeys() {
RegisterKeyword("SaveGame", (sr) => {
SaveGamePath = new SingleString(sr).String;
Logger.Info("Save game set to: " + SaveGamePath);
});
RegisterKeyword("ImperatorDirectory", (sr) => {
ImperatorPath = new SingleString(sr).String;
});
RegisterKeyword("ImperatorDocDirectory", (sr) => {
void RegisterKeys() {
RegisterKeyword("SaveGame", (sr) => {
SaveGamePath = new SingleString(sr).String;
Logger.Info("Save game set to: " + SaveGamePath);
});
RegisterKeyword("ImperatorDirectory", (sr) => {
ImperatorPath = new SingleString(sr).String;
});
RegisterKeyword("ImperatorDocDirectory", (sr) => {
ImperatorDocPath = new SingleString(sr).String;
});
RegisterKeyword("CK3directory", (sr) => {
Ck3Path = new SingleString(sr).String;
});
RegisterKeyword("CK3ModsDirectory", (sr) => {
Ck3ModsPath = new SingleString(sr).String;
});
RegisterKeyword("output_name", (sr) => {
OutputModName = new SingleString(sr).String;
Logger.Info("Output name set to: " + OutputModName);
});
RegisterKeyword("ImperatorDeJure", (sr) => {
var deJureString = new SingleString(sr).String;
try {
ImperatorDeJure = (IMPERATOR_DE_JURE)Convert.ToInt32(deJureString);
Logger.Info("ImperatorDeJure set to: " + deJureString);
} catch (Exception e) {
Logger.Error("Undefined error, ImperatorDeJure value was: " + deJureString + "; Error message: " + e.ToString());
}
});
RegisterKeyword("ConvertCharacterBirthAndDeathDates", (sr) => {
var valStr = new SingleString(sr).String;
if (valStr == "true") {
ConvertBirthAndDeathDates = true;
} else {
ConvertBirthAndDeathDates = false;
}
Logger.Info("Conversion of characters' birth and death dates set to: " + ConvertBirthAndDeathDates);
});
RegisterRegex(CommonRegexes.Catchall, ParserHelpers.IgnoreItem);
}
});
RegisterKeyword("CK3directory", (sr) => {
Ck3Path = new SingleString(sr).String;
});
RegisterKeyword("CK3ModsDirectory", (sr) => {
Ck3ModsPath = new SingleString(sr).String;
});
RegisterKeyword("output_name", (sr) => {
OutputModName = new SingleString(sr).String;
Logger.Info("Output name set to: " + OutputModName);
});
RegisterKeyword("ImperatorDeJure", (sr) => {
var deJureString = new SingleString(sr).String;
try {
ImperatorDeJure = (IMPERATOR_DE_JURE)Convert.ToInt32(deJureString);
Logger.Info("ImperatorDeJure set to: " + deJureString);
} catch (Exception e) {
Logger.Error("Undefined error, ImperatorDeJure value was: " + deJureString + "; Error message: " + e.ToString());
}
});
RegisterKeyword("ConvertCharacterBirthAndDeathDates", (sr) => {
var valStr = new SingleString(sr).String;
if (valStr == "true") {
ConvertBirthAndDeathDates = true;
} else {
ConvertBirthAndDeathDates = false;
}
Logger.Info("Conversion of characters' birth and death dates set to: " + ConvertBirthAndDeathDates);
});
RegisterRegex(CommonRegexes.Catchall, ParserHelpers.IgnoreItem);
}

void VerifyImperatorPath() {
if (!Directory.Exists(ImperatorPath)) {
throw new DirectoryNotFoundException(ImperatorPath + " does not exist!");
}
if (!File.Exists(ImperatorPath + "/binaries/imperator.exe")) {
throw new FileNotFoundException(ImperatorPath + "does not contains Imperator: Rome!");
}
Logger.Info("\tI:R install path is " + ImperatorPath);
}
private void VerifyImperatorPath() {
if (!Directory.Exists(ImperatorPath)) {
throw new DirectoryNotFoundException(ImperatorPath + " does not exist!");
}
if (!File.Exists(ImperatorPath + "/binaries/imperator.exe")) {
throw new FileNotFoundException(ImperatorPath + "does not contains Imperator: Rome!");
}
Logger.Info("\tI:R install path is " + ImperatorPath);
}

void VerifyCK3Path() {
if (!Directory.Exists(Ck3Path)) {
throw new DirectoryNotFoundException(Ck3Path + " does not exist!");
}
if (!File.Exists(Ck3Path + "/binaries/ck3.exe")) {
throw new FileNotFoundException(Ck3Path + " does not contain Crusader Kings III!");
}
Logger.Info("\tCK3 install path is " + Ck3Path);
}
private void VerifyCK3Path() {
if (!Directory.Exists(Ck3Path)) {
throw new DirectoryNotFoundException(Ck3Path + " does not exist!");
}
if (!File.Exists(Ck3Path + "/binaries/ck3.exe")) {
throw new FileNotFoundException(Ck3Path + " does not contain Crusader Kings III!");
}
Logger.Info("\tCK3 install path is " + Ck3Path);
}

void SetOutputName() {
if (OutputModName.Length == 0) {
OutputModName = CommonFunctions.TrimPath(SaveGamePath);
}
OutputModName = CommonFunctions.TrimExtension(OutputModName);
OutputModName = OutputModName.Replace('-', '_');
OutputModName = OutputModName.Replace(' ', '_');
private void SetOutputName() {
if (OutputModName.Length == 0) {
OutputModName = CommonFunctions.TrimPath(SaveGamePath);
}
OutputModName = CommonFunctions.TrimExtension(OutputModName);
OutputModName = OutputModName.Replace('-', '_');
OutputModName = OutputModName.Replace(' ', '_');

OutputModName = CommonFunctions.NormalizeUTF8Path(OutputModName);
Logger.Info("Using output name " + OutputModName);
}
}
OutputModName = CommonFunctions.NormalizeUTF8Path(OutputModName);
Logger.Info("Using output name " + OutputModName);
}

private void VerifyImperatorVersion(ConverterVersion converterVersion) {
var path = Path.Combine(ImperatorPath, "launcher/launcher-settings.json");
var ImpVersion = GameVersion.ExtractVersionFromLauncher(path);
if (ImpVersion is null) {
Logger.Error("Imperator version could not be determined, proceeding blind!");
return;
}

Logger.Info($"Imperator version: {ImpVersion.ToShortString()}");

if (converterVersion.MinSource > ImpVersion) {
Logger.Error($"Imperator version is v{ImpVersion.ToShortString()}," +
$" converter requires minimum v{converterVersion.MinSource.ToShortString()}!");
throw new ArgumentOutOfRangeException("Converter vs Imperator installation mismatch!");
}
if (!converterVersion.MaxSource.IsLargerishThan(ImpVersion)) {
Logger.Error($"Imperator version is v{ImpVersion.ToShortString()}, converter requires maximum v" +
$"{converterVersion.MaxSource.ToShortString()}!");
throw new ArgumentOutOfRangeException("Converter vs Imperator installation mismatch!");
}
}

private void VerifyCK3Version(ConverterVersion converterVersion) {
var path = Path.Combine(Ck3Path, "launcher/launcher-settings.json");
var CK3Version = GameVersion.ExtractVersionFromLauncher(path);
if (CK3Version is null) {
Logger.Error("CK3 version could not be determined, proceeding blind!");
return;
}

Logger.Info($"CK3 version: {CK3Version.ToShortString()}");

if (converterVersion.MinTarget > CK3Version) {
Logger.Error($"CK3 version is v{CK3Version.ToShortString()}, converter requires minimum v" +
$"{converterVersion.MinTarget.ToShortString()}!");
throw new ArgumentOutOfRangeException("Converter vs CK3 installation mismatch!");
}
if (!converterVersion.MaxTarget.IsLargerishThan(CK3Version)) {
Logger.Error($"CK3 version is v{CK3Version.ToShortString()}, converter requires maximum v" +
$"{converterVersion.MaxTarget.ToShortString()} !");
throw new ArgumentOutOfRangeException("Converter vs CK3 installation mismatch!");
}
}
}
}
18 changes: 5 additions & 13 deletions ImperatorToCK3/Converter.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using commonItems;
using commonItems;

namespace ImperatorToCK3 {
class Converter {
internal static class Converter {
public static void ConvertImperatorToCK3(ConverterVersion converterVersion) {
var config = new Configuration();
Logger.Progress("0 %");
var config = new Configuration(converterVersion);
var imperatorWorld = new Imperator.World(config, converterVersion);
var ck3World = new CK3.World(imperatorWorld, config);
Outputter.WorldOutputter.OutputWorld(ck3World, config);
Logger.Info("* Conversion complete! *");
}

private void LogGameVersions(string imperatorPath, string ck3Path) {
// TODO
Logger.Progress("100 %");
}
}
}

0 comments on commit e7af714

Please sign in to comment.