Skip to content

Commit

Permalink
Fix dump database to files
Browse files Browse the repository at this point in the history
* Fix paths getting duplicated resulting in files not being written
* Escape node values before writing
  • Loading branch information
blowfishpro committed Aug 13, 2019
1 parent aa6ff2e commit 4201d5f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
4 changes: 4 additions & 0 deletions ModuleManager/Extensions/UrlFileExtensions.cs
Expand Up @@ -8,5 +8,9 @@ public static string GetUrlWithExtension(this UrlDir.UrlFile urlFile)
{
return $"{urlFile.url}.{urlFile.fileExtension}";
}
public static string GetNameWithExtension(this UrlDir.UrlFile urlFile)
{
return $"{urlFile.name}.{urlFile.fileExtension}";
}
}
}
65 changes: 33 additions & 32 deletions ModuleManager/ModuleManager.cs
Expand Up @@ -430,7 +430,7 @@ private IEnumerator DataBaseReloadWithMM(bool dump = false)

public static void OutputAllConfigs()
{
string path = KSPUtil.ApplicationRootPath + "/_MMCfgOutput/";
string path = Path.GetFullPath(Path.Combine(KSPUtil.ApplicationRootPath, "_MMCfgOutput"));
try
{
Directory.CreateDirectory(path);
Expand All @@ -451,51 +451,52 @@ public static void OutputAllConfigs()
{
Log("Exception while cleaning the export dir\n" + unauthorizedAccessException);
}
Stack<UrlDir> dirs = new Stack<UrlDir>();
dirs.Push(GameDatabase.Instance.root);
Stack<String> paths = new Stack<string>();
paths.Push("");

try
void WriteDirectoryRecursive(UrlDir currentDir, string dirPath)
{
while (dirs.Count > 0)
if (currentDir.files.Count > 0) Directory.CreateDirectory(dirPath);

foreach (UrlDir.UrlFile urlFile in currentDir.files)
{
var currentDir = dirs.Pop();
string currentPath = paths.Pop();

foreach (UrlDir.UrlFile urlFile in currentDir.files)
if (urlFile.fileType != UrlDir.FileType.Config) continue;

Log("Exporting " + urlFile.GetUrlWithExtension());
string filePath = Path.Combine(dirPath, urlFile.GetNameWithExtension());

bool first = true;

using (FileStream stream = new FileStream(filePath, FileMode.Create))
using (StreamWriter writer = new StreamWriter(stream))
{
if (urlFile.fileType == UrlDir.FileType.Config)
foreach (UrlDir.UrlConfig urlConfig in urlFile.configs)
{
string dirPath = path + currentPath;
if (!Directory.Exists(dirPath))
try
{
Directory.CreateDirectory(dirPath);
}
if (first) first = false;
else writer.Write("\n");

Log("Exporting " + currentPath + urlFile.GetUrlWithExtension());
string filePath = dirPath + urlFile.GetUrlWithExtension();
foreach (UrlDir.UrlConfig urlConfig in urlFile.configs)
ConfigNode copy = urlConfig.config.DeepCopy();
copy.EscapeValuesRecursive();
writer.Write(copy.ToString());
}
catch (Exception e)
{
try
{
File.AppendAllText(filePath, urlConfig.config.ToString());
}
catch (Exception e)
{
Log("Exception while trying to write the file " + filePath + "\n" + e);
}
Log("Exception while trying to write the file " + filePath + "\n" + e);
}
}
}
}

foreach (UrlDir urlDir in currentDir.children)
{
dirs.Push(urlDir);
paths.Push(currentPath + urlDir.name + "/");
}
foreach (UrlDir urlDir in currentDir.children)
{
WriteDirectoryRecursive(urlDir, Path.Combine(dirPath, urlDir.name));
}
}

try
{
WriteDirectoryRecursive(GameDatabase.Instance.root, path);
}
catch (DirectoryNotFoundException directoryNotFoundException)
{
Log("Exception while exporting the cfg\n" + directoryNotFoundException);
Expand Down

0 comments on commit 4201d5f

Please sign in to comment.