Skip to content

Commit

Permalink
moved try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
hollowstrawberry committed Jun 10, 2020
1 parent fa6ead6 commit 2fa9087
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 64 deletions.
38 changes: 21 additions & 17 deletions PolyConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class PolyConverter
Converters = new JsonConverter[] { new VectorJsonConverter(), new PolyJsonConverter() },
};

static readonly List<char> logChars = new List<char> { 'F', 'E', '+', '@', '*', '.', '>' };
static readonly List<char> logChars = new List<char> { 'F', 'E', '+', '@', '*', '>' };


public void Main()
public void ConvertAll()
{
var resultLog = new List<string>();
int fileCount = 0, backups = 0;
Expand All @@ -51,12 +51,7 @@ public void Main()
string layoutPath = JsonExtensionRegex.Replace(path, LayoutExtension);
string backupPath = JsonExtensionRegex.Replace(path, BackupExtension);

try { resultLog.Add(JsonToLayout(path, layoutPath, backupPath)); }
catch (Exception e)
{
resultLog.Add($"[Fatal Error] Couldn't convert \"{PathTrim(path)}\":\n {e}///");
continue;
}
resultLog.Add(JsonToLayout(path, layoutPath, backupPath));

fileCount++;
}
Expand All @@ -65,19 +60,15 @@ public void Main()
string newPath = LayoutExtensionRegex.Replace(path, JsonExtension);
if (File.Exists(newPath)) continue;

try { resultLog.Add(LayoutToJson(path, newPath)); }
catch (Exception e)
{
resultLog.Add($"[Fatal Error] Couldn't convert \"{PathTrim(path)}\":\n {e}///");
continue;
}
resultLog.Add(LayoutToJson(path, newPath));

fileCount++;
}
}

resultLog = resultLog
.Where(s => !string.IsNullOrWhiteSpace(s) && logChars.Contains(s[1]))
.OrderBy(s => logChars.IndexOf(s[1]))
.Where(s => !string.IsNullOrWhiteSpace(s))
.OrderBy(s => logChars.Contains(s[1]) ? logChars.IndexOf(s[1]) : logChars.Last())
.ToList();

foreach (string msg in resultLog)
Expand All @@ -94,6 +85,12 @@ public void Main()


public string LayoutToJson(string layoutPath, string jsonPath)
{
try { return InnerLayoutToJson(layoutPath, jsonPath); }
catch (Exception e) { return $"[Fatal Error] Couldn't convert \"{PathTrim(layoutPath)}\":\n {e}///"; }
}

private string InnerLayoutToJson(string layoutPath, string jsonPath)
{
int _ = 0;
var bytes = File.ReadAllBytes(layoutPath);
Expand All @@ -114,7 +111,14 @@ public string LayoutToJson(string layoutPath, string jsonPath)
return $"[+] Created \"{PathTrim(jsonPath)}\"";
}

public string JsonToLayout(string jsonPath, string layoutPath, string? backupPath)

public string JsonToLayout(string jsonPath, string layoutPath, string backupPath)
{
try { return InnerJsonToLayout(jsonPath, layoutPath, backupPath); }
catch (Exception e) { return $"[Fatal Error] Couldn't convert \"{PathTrim(layoutPath)}\":\n {e}///"; }
}

private string InnerJsonToLayout(string jsonPath, string layoutPath, string? backupPath)
{
string json = File.ReadAllText(jsonPath);
object data;
Expand Down
78 changes: 31 additions & 47 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static int Main(string[] args)
}
catch (Exception)
{
Console.WriteLine("[Fatal Error] Failed to grab Poly Bridge 2 location from {ManualGamePath}");
Console.WriteLine($"[Fatal Error] Failed to grab Poly Bridge 2 location from {ManualGamePath}");
if (!hasArgs)
{
Console.WriteLine("\n[#] The program will now close.");
Expand Down Expand Up @@ -84,68 +84,52 @@ public static int Main(string[] args)

Console.WriteLine();

try
if (hasArgs)
{
if (hasArgs)
{
string filePath = string.Join(' ', args).Trim();
string filePath = string.Join(' ', args).Trim();

if (JsonExtensionRegex.IsMatch(filePath))
{
string newPath = JsonExtensionRegex.Replace(filePath, LayoutExtension);
string backupPath = JsonExtensionRegex.Replace(filePath, BackupExtension);
if (JsonExtensionRegex.IsMatch(filePath))
{
string newPath = JsonExtensionRegex.Replace(filePath, LayoutExtension);
string backupPath = JsonExtensionRegex.Replace(filePath, BackupExtension);

string result = new PolyConverter().JsonToLayout(filePath, newPath, backupPath);
string result = new PolyConverter().JsonToLayout(filePath, newPath, backupPath);

Console.WriteLine(result);
if (result.Contains("Invalid json")) return ExitCodeJsonError;
if (result.Contains("Error") && result.Contains("file")) return ExitCodeFileError;
if (result.Contains("Error")) return ExitCodeConversionError;
return ExitCodeSuccessful;
}
else if (LayoutExtensionRegex.IsMatch(filePath))
{
string newPath = LayoutExtensionRegex.Replace(filePath, JsonExtension);
Console.WriteLine(result);
if (result.Contains("Invalid json")) return ExitCodeJsonError;
if (result.Contains("Error") && result.Contains("file")) return ExitCodeFileError;
if (result.Contains("Error")) return ExitCodeConversionError;
return ExitCodeSuccessful;
}
else if (LayoutExtensionRegex.IsMatch(filePath))
{
string newPath = LayoutExtensionRegex.Replace(filePath, JsonExtension);

string result = new PolyConverter().LayoutToJson(filePath, newPath);
string result = new PolyConverter().LayoutToJson(filePath, newPath);

Console.WriteLine(result);
if (result.Contains("Error") && result.Contains("file")) return ExitCodeFileError;
if (result.Contains("Error")) return ExitCodeConversionError;
return ExitCodeSuccessful;
}
else
{
Console.WriteLine($"[Error] File extension must be either {LayoutExtension} or {JsonExtension}");
return ExitCodeConversionError;
}
Console.WriteLine(result);
if (result.Contains("Error") && result.Contains("file")) return ExitCodeFileError;
if (result.Contains("Error")) return ExitCodeConversionError;
return ExitCodeSuccessful;
}
else
{
while (true)
{
Console.WriteLine("\n");

new PolyConverter().Main();

Console.WriteLine("\n[#] Press Enter to run the program again.");
Console.ReadLine();
}
Console.WriteLine($"[Error] File extension must be either {LayoutExtension} or {JsonExtension}");
return ExitCodeFileError;
}

}
catch (Exception e)
else
{
Console.WriteLine($"[Fatal Error] An unexpected problem occured:\n {e}");
Console.WriteLine("\n[#] The program will now close.");
if (!hasArgs)
while (true)
{
Console.WriteLine("\n");

new PolyConverter().ConvertAll();

Console.WriteLine("\n[#] Press Enter to run the program again.");
Console.ReadLine();
return ExitCodeUnexpectedError;
}
}

return ExitCodeSuccessful;
}

static string GetPolyBridge2SteamPath()
Expand Down

0 comments on commit 2fa9087

Please sign in to comment.