Skip to content

Commit

Permalink
Fix file separators and os specifics #3
Browse files Browse the repository at this point in the history
  • Loading branch information
rquinio committed Feb 11, 2018
1 parent 6b06c14 commit 31b8368
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 23 deletions.
6 changes: 3 additions & 3 deletions PortraitBuilder/Engine/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public List<DLC> LoadDLCs(Boolean clean) {
}

public List<DLC> LoadDLCs() {
string dlcFolder = user.GameDir + "dlc" + Path.DirectorySeparatorChar;
string dlcFolder = Path.Combine(user.GameDir,"dlc");
logger.Info("Loading DLCs from " + dlcFolder);
List<DLC> dlcs = dlcReader.ParseFolder(dlcFolder);

Expand All @@ -120,11 +120,11 @@ private void UnzipDLCs(List<DLC> dlcs) {
FastZip fastZip = new FastZip();
foreach (DLC dlc in dlcs) {
string dlcCode = dlc.DLCFile.Replace(".dlc", "");
string newDlcAbsolutePath = user.DlcDir + dlcCode + Path.DirectorySeparatorChar;
string newDlcAbsolutePath = Path.Combine(user.DlcDir, dlcCode);
if (!Directory.Exists(newDlcAbsolutePath)) {
logger.Info(string.Format("Extracting {0} to {1}", dlc.Name, newDlcAbsolutePath));
// Filter only portraits files, to gain speed/space
string fileFilter = @"interface;gfx/characters";
string fileFilter = @"interface;gfx"+Path.DirectorySeparatorChar+"characters";
fastZip.ExtractZip(dlc.AbsolutePath, newDlcAbsolutePath, fileFilter);

// In any case, create the directory, so that it is ignored for next load.
Expand Down
6 changes: 3 additions & 3 deletions PortraitBuilder/Parser/DLCReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public List<DLC> ParseFolder(string folder) {
foreach (FileInfo dlcFile in dlcFiles) {
DLC dlc = Parse(dlcFile.FullName);
if (dlc != null && dlc.Archive != null) {
// Note: path will be overriden when extracting the archive
dlc.AbsolutePath = folder + dlc.Archive.Replace("dlc/", "");
dlcs.Add(dlc);
// Note: path will be overriden when extracting the archive
dlc.AbsolutePath = Path.Combine(folder, dlc.Archive.Substring("dlc".Length +1)); // Remove "dlc/" from path
dlcs.Add(dlc);
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion PortraitBuilder/Parser/ModReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public List<Mod> ParseFolder(string folder) {
try {
Mod mod = Parse(modFile.FullName);
if (mod != null && mod.Path != null) {
mod.AbsolutePath = folder + Path.DirectorySeparatorChar + mod.Path.Replace("mod/", "");
mod.AbsolutePath = Path.Combine(folder, mod.Path.Substring("mod".Length + 1)); // Remove "mod/" from path
mods.Add(mod);
}
} catch (Exception e) {
Expand Down
12 changes: 6 additions & 6 deletions PortraitBuilder/Parser/PortraitReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ public PortraitData Parse(string dir) {
try {
List<string> fileNames = new List<string>();

if (Directory.Exists(dir + @"\interface\")) {
fileNames.AddRange(Directory.GetFiles(dir + @"\interface\", "*.gfx"));
if (Directory.Exists(Path.Combine(dir, "interface"))) {
fileNames.AddRange(Directory.GetFiles(Path.Combine(dir, "interface"), "*.gfx"));
}
// interface/portraits seems to be loaded after interface/, and override (cf byzantinegfx)
if (Directory.Exists(dir + @"\interface\portraits\")) {
fileNames.AddRange(Directory.GetFiles(dir + @"\interface\portraits\", "*.gfx"));
if (Directory.Exists(Path.Combine(dir, "interface", "portraits"))) {
fileNames.AddRange(Directory.GetFiles(Path.Combine(dir, "interface", "portraits"), "*.gfx"));
}

foreach (string fileName in fileNames) {
Parse(fileName, data);
}

if (Directory.Exists(dir + @"\interface\portrait_offsets\")) {
string[] offsetFileNames = Directory.GetFiles(dir + @"\interface\portrait_offsets\", "*.txt");
if (Directory.Exists(Path.Combine(dir, "interface", "portrait_offsets"))) {
string[] offsetFileNames = Directory.GetFiles(Path.Combine(dir, "interface", "portrait_offsets"), "*.txt");
foreach (string offsetFileName in offsetFileNames) {
Dictionary<string, Point> offsets = portraitOffsetReader.Parse(offsetFileName);
data.Offsets = data.Offsets.Concat(offsets).GroupBy(d => d.Key).ToDictionary(d => d.Key, d => d.First().Value);
Expand Down
4 changes: 2 additions & 2 deletions PortraitBuilder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ static void Main(string[] args) {

Application.ApplicationExit += new EventHandler(onExitApplication);

if (!Directory.Exists("dlc/"))
Directory.CreateDirectory("dlc/");
if (!Directory.Exists("dlc"))
Directory.CreateDirectory("dlc");

if (!File.Exists("gamedir")) {
string dir = null;
Expand Down
29 changes: 22 additions & 7 deletions PortraitBuilder/UI/PortraitBuilderForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,33 @@ private string readGameDir() {
/// Read userdir.txt in Steam directory for the path to mod dir, or default to pre-defined location
/// </summary>
private string readModDir(string gameDir) {
string modDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Paradox Interactive", "Crusader Kings II") + Path.DirectorySeparatorChar;
string userDir = getDefaultUserDir();
string userdirFilePath = Path.Combine(gameDir, "userdir.txt");
if(File.Exists(userdirFilePath)){
logger.Info("Reading userdir.txt to determine the mod directory.");
Stream stream = new FileStream(userdirFilePath, FileMode.Open);
StreamReader reader = new StreamReader(stream, Encoding.Default);
modDir = reader.ReadLine() + Path.DirectorySeparatorChar;
logger.Info("Found userdir.txt with path: " + modDir);
}
modDir = Path.Combine(modDir, "mod");
return modDir;
}
userDir = reader.ReadLine() + Path.DirectorySeparatorChar;
logger.Info("Found userdir.txt with path: " + userDir);
}
return Path.Combine(userDir, "mod");
}

private string getDefaultUserDir() {
string userDir = null;
PlatformID os = System.Environment.OSVersion.Platform;
if (os == PlatformID.Win32NT || os == PlatformID.Win32Windows) {
userDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Paradox Interactive", "Crusader Kings II");
} else if (os == PlatformID.MacOSX || (int) os == 128) {
// Environment.SpecialFolder.MyDocuments does not add /Documents/ on Mac
userDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Documents", "Paradox Interactive", "Crusader Kings II");
} else if (os == PlatformID.Unix) {
userDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), ".paradoxinteractive", "Crusader Kings II");
} else {
logger.Error("Unkown platformID " + os);
}
return userDir;
}

/// <summary>
/// Entry point for re-drawing based on updated portrait.
Expand Down
2 changes: 1 addition & 1 deletion PortraitBuilder/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</log4net>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="lib;libs" xmlns="" />
<probing privatePath="lib" />
</assemblyBinding>
</runtime>
<startup useLegacyV2RuntimeActivationPolicy="true">
Expand Down

0 comments on commit 31b8368

Please sign in to comment.