private static void CompileInkThreaded(object itemObj) { CompilationStackItem item = (CompilationStackItem)itemObj; if (item.state == CompilationStackItem.State.Compiling) { Debug.LogWarning("CompileInkThreaded was called on a file that is already compiling! This is most likely a threading bug. Please report this!"); return; } BeginCompilingFile(item); var inputString = File.ReadAllText(item.inkAbsoluteFilePath); var compiler = new Compiler(inputString, new Compiler.Options { countAllVisits = true, fileHandler = new UnityInkFileHandler(Path.GetDirectoryName(item.inkAbsoluteFilePath)), errorHandler = (string message, ErrorType type) => { InkCompilerLog log; if (InkCompilerLog.TryParse(message, out log)) { if (string.IsNullOrEmpty(log.fileName)) { log.fileName = Path.GetFileName(item.inkAbsoluteFilePath); } item.logOutput.Add(log); } else { Debug.LogWarning("Couldn't parse log " + message); } } }); try { var compiledStory = compiler.Compile(); if (compiledStory != null) { item.compiledJson = compiledStory.ToJson(); } } catch (SystemException e) { item.unhandledErrorOutput.Add(string.Format( "Ink Compiler threw exception \nError: {0}\n---- Trace ----\n{1}\n--------\n", e.Message, e.StackTrace)); } CompleteCompilingFile(item); // This doesn't seem to execute when called in a thread, and I apparently don't have a bloody clue how threads work. // If someone can make this work, I'd rather that TryCompileNextFileInStack ran directly after CompileInkThreaded finishes. // I couldn't make it work, so I've put TryCompileNextFileInStack in Update instead. Bleh! // TryCompileNextFileInStack(); }
private static void CompileInkThreaded(object itemObj) { CompilationStackItem item = (CompilationStackItem)itemObj; // This should be called before this point, but just in case. BeginCompilingFile(item); var inputString = File.ReadAllText(item.inkAbsoluteFilePath); var compiler = new Compiler(inputString, new Compiler.Options { countAllVisits = true, fileHandler = new UnityInkFileHandler(Path.GetDirectoryName(item.inkAbsoluteFilePath)), errorHandler = (string message, ErrorType type) => { InkCompilerLog log; if (InkCompilerLog.TryParse(message, out log)) { if (string.IsNullOrEmpty(log.fileName)) { log.fileName = Path.GetFileName(item.inkAbsoluteFilePath); } item.logOutput.Add(log); } else { Debug.LogWarning("Couldn't parse log " + message); } } }); try { var compiledStory = compiler.Compile(); if (compiledStory != null) { item.compiledJson = compiledStory.ToJson(); } } catch (SystemException e) { item.unhandledErrorOutput.Add(string.Format( "Ink Compiler threw exception \nError: {0}\n---- Trace ----\n{1}\n--------\n", e.Message, e.StackTrace)); } CompleteCompilingFile(item); TryCompileNextFileInStack(); }