CLI: Handle attempted output to directory that is file gracefully

This commit is contained in:
Katy Coe
2020-06-20 19:34:34 +02:00
parent 44df54b639
commit ca113fb055
2 changed files with 27 additions and 5 deletions

View File

@@ -226,6 +226,9 @@ namespace Il2CppInspector.CLI
writer.WriteFilesByClassTree(csOut, options.SeparateAssemblyAttributesFiles); writer.WriteFilesByClassTree(csOut, options.SeparateAssemblyAttributesFiles);
break; break;
} }
if (writer.GetAndClearLastException() is Exception ex)
Console.WriteLine("An error occurred: " + ex.Message);
} }
// IDA Python script output // IDA Python script output

View File

@@ -21,6 +21,7 @@ namespace Il2CppInspector.Outputs
public class CSharpCodeStubs public class CSharpCodeStubs
{ {
private readonly Il2CppModel model; private readonly Il2CppModel model;
private Exception lastException;
// Namespace prefixes whose contents should be skipped // Namespace prefixes whose contents should be skipped
public List<string> ExcludedNamespaces { get; set; } public List<string> ExcludedNamespaces { get; set; }
@@ -42,6 +43,13 @@ namespace Il2CppInspector.Outputs
public CSharpCodeStubs(Il2CppModel model) => this.model = model; public CSharpCodeStubs(Il2CppModel model) => this.model = model;
// Get the last error that occurred and clear the error state
public Exception GetAndClearLastException() {
var ex = lastException;
lastException = null;
return ex;
}
public void WriteSingleFile(string outFile) => WriteSingleFile(outFile, t => t.Index); public void WriteSingleFile(string outFile) => WriteSingleFile(outFile, t => t.Index);
public void WriteSingleFile<TKey>(string outFile, Func<TypeInfo, TKey> orderBy) { public void WriteSingleFile<TKey>(string outFile, Func<TypeInfo, TKey> orderBy) {
@@ -95,7 +103,7 @@ namespace Il2CppInspector.Outputs
} }
); );
if (separateAttributes && usedAssemblies.Any()) if (separateAttributes && usedAssemblies.Any() && lastException == null)
foreach (var asm in usedAssemblies) foreach (var asm in usedAssemblies)
File.WriteAllText(Path.Combine(outPath, Path.GetFileNameWithoutExtension(asm.ShortName), "AssemblyInfo.cs"), generateAssemblyInfo(new [] {asm})); File.WriteAllText(Path.Combine(outPath, Path.GetFileNameWithoutExtension(asm.ShortName), "AssemblyInfo.cs"), generateAssemblyInfo(new [] {asm}));
@@ -110,6 +118,9 @@ namespace Il2CppInspector.Outputs
// Output source files in tree format with separate assembly attributes // Output source files in tree format with separate assembly attributes
var assemblies = WriteFilesByClassTree(outPath, true); var assemblies = WriteFilesByClassTree(outPath, true);
if (lastException != null)
return;
// Per-project (per-assembly) solution definition and configuration // Per-project (per-assembly) solution definition and configuration
var slnProjectDefs = new StringBuilder(); var slnProjectDefs = new StringBuilder();
var slnProjectConfigs = new StringBuilder(); var slnProjectConfigs = new StringBuilder();
@@ -262,13 +273,21 @@ namespace Il2CppInspector.Outputs
var usings = nsRefs.OrderBy(n => (n.StartsWith("System.") || n == "System") ? "0" + n : "1" + n); var usings = nsRefs.OrderBy(n => (n.StartsWith("System.") || n == "System") ? "0" + n : "1" + n);
// Ensure output directory exists // Ensure output directory exists and is not a file
if (!string.IsNullOrEmpty(Path.GetDirectoryName(outFile))) var dir = Path.GetDirectoryName(outFile);
Directory.CreateDirectory(Path.GetDirectoryName(outFile)); if (!string.IsNullOrEmpty(dir)) {
try {
Directory.CreateDirectory(dir);
}
catch (IOException ex) {
lastException = ex;
return false;
}
}
// Sanitize leafname (might be class name with invalid characters) // Sanitize leafname (might be class name with invalid characters)
var leafname = Regex.Replace(Path.GetFileName(outFile), @"[<>:""\|\?\*]", "_"); var leafname = Regex.Replace(Path.GetFileName(outFile), @"[<>:""\|\?\*]", "_");
outFile = Path.Combine(Path.GetDirectoryName(outFile), leafname); outFile = Path.Combine(dir, leafname);
// Create output file // Create output file
bool fileWritten = false; bool fileWritten = false;