Skip to content

Commit

Permalink
Automatic linking codegen can now be controlled via the YS settings.
Browse files Browse the repository at this point in the history
Had to make a bunch of changes to a bunch of files and also add MiniJSON.
This is because the codegen pipeline can't use the Unity side of things so the built in JSON stuff is out.
Pretty much everything else is just some minor refactoring to make it less all jumbled together into a single file.
  • Loading branch information
McJones committed Jan 17, 2024
1 parent 1ebd52f commit 8dcadfa
Show file tree
Hide file tree
Showing 10 changed files with 885 additions and 103 deletions.
109 changes: 54 additions & 55 deletions Editor/Analysis/ActionsRegistrationGenerator~/ActionsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,56 @@ public class ActionRegistrationSourceGenerator : ISourceGenerator
public void Execute(GeneratorExecutionContext context)
{
var output = GetOutput(context);
output.WriteLine(DateTime.Now);

// we need to know if the settings are configured to not perform codegen to link attributed methods
// this is kinda annoying because the path root of the project settings and the root path of this process are *very* different
// so what we do is we use the included Compilation Assembly additional file that Unity gives us.
// This file if opened has the path of the Unity project, which we can then use to get the settings
// if any stage of this fails then we bail out and assume that codegen is desired
if (context.AdditionalFiles.Any())
{
var relevants = context.AdditionalFiles.Where(i => i.Path.Contains($"{context.Compilation.AssemblyName}.AdditionalFile.txt"));
if (relevants.Any())
{
var arsgacsaf = relevants.First();
if (File.Exists(arsgacsaf.Path))
{
try
{
var projectPath = File.ReadAllText(arsgacsaf.Path);
var fullPath = Path.Combine(projectPath, Yarn.Unity.Editor.YarnSpinnerProjectSettings.YarnSpinnerProjectSettingsPath);
output.WriteLine($"Attempting to read settings file at {fullPath}");

if (!Yarn.Unity.Editor.YarnSpinnerProjectSettings.GetOrCreateSettings(projectPath, output).automaticallyLinkAttributedYarnCommandsAndFunctions)
{
output.WriteLine("Skipping codegen due to settings.");
output.Dispose();
return;
}
}
catch (Exception e)
{
output.WriteLine($"Unable to determine Yarn settings, settings values will be ignored and codegen will occur: {e.Message}");
}
}
else
{
output.WriteLine($"The project settings path metadata file does not exist at: {arsgacsaf.Path}. Settings values will be ignored and codegen will occur");
}
}
else
{
output.WriteLine("Unable to determine Yarn settings path, no file containing the project path metadata was included. Settings values will be ignored and codegen will occur.");
}
}
else
{
output.WriteLine("Unable to determine Yarn settings path as no additional files were included. Settings values will be ignored and codegen will occur.");
}

try
{
output.WriteLine(DateTime.Now);

output.WriteLine("Source code generation for assembly " + context.Compilation.AssemblyName);

if (context.AdditionalFiles.Any()) {
Expand Down Expand Up @@ -322,18 +367,21 @@ public void Initialize(GeneratorInitializationContext context)
context.RegisterForSyntaxNotifications(() => new ClassDeclarationSyntaxReceiver());
}

public ILogger GetOutput(GeneratorExecutionContext context)
public Yarn.Unity.ILogger GetOutput(GeneratorExecutionContext context)
{
if (GetShouldLogToFile(context))
{
var tempPath = System.IO.Path.GetTempPath();

var path = System.IO.Path.Combine(tempPath, $"{nameof(ActionRegistrationSourceGenerator)}-{context.Compilation.AssemblyName}.txt");

var outFile = System.IO.File.Open(path, System.IO.FileMode.Create);

return new FileLogger(new System.IO.StreamWriter(outFile));
} else {
return new NullLogger();
return new Yarn.Unity.FileLogger(new System.IO.StreamWriter(outFile));
}
else
{
return new Yarn.Unity.NullLogger();
}
}

Expand Down Expand Up @@ -365,52 +413,3 @@ public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
}
}
}

public interface ILogger : IDisposable
{
void Write(object obj);
void WriteLine(object obj);
}

public class FileLogger : ILogger
{
System.IO.TextWriter writer;

public FileLogger(TextWriter writer)
{
this.writer = writer;
}

public void Dispose()
{
writer.Dispose();
}

public void Write(object text)
{
writer.Write(text);
}

public void WriteLine(object text)
{
writer.WriteLine(text);
}
}

public class NullLogger : ILogger
{
public void Dispose()
{

}

public void Write(object text)
{

}

public void WriteLine(object text)
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<Compile Include="../Analyser.cs" />
<Compile Include="../Action.cs" />
<Compile Include="../AnalyserException.cs" />
<Compile Include="../../YarnSpinnerProjectSettings.cs" />
<Compile Include="../../MiniJSON.cs" />
<Compile Include="../../ILogger.cs" />
<Compile Include="./*.cs" />
</ItemGroup>

Expand Down
65 changes: 65 additions & 0 deletions Editor/ILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
#endif

namespace Yarn.Unity
{
public interface ILogger : IDisposable
{
void Write(object obj);
void WriteLine(object obj);
}

public class FileLogger : ILogger
{
System.IO.TextWriter writer;

public FileLogger(System.IO.TextWriter writer)
{
this.writer = writer;
}

public void Dispose()
{
writer.Dispose();
}

public void Write(object text)
{
writer.Write(text);
}

public void WriteLine(object text)
{
writer.WriteLine(text);
}
}

public class UnityLogger : ILogger
{
public void Dispose() { }

public void Write(object text)
{
WriteLine(text);
}

public void WriteLine(object text)
{
#if UNITY_EDITOR
Debug.LogWarning(text.ToString());
#endif
}
}

public class NullLogger : ILogger
{
public void Dispose() { }

public void Write(object text) { }

public void WriteLine(object text) { }
}
}
11 changes: 11 additions & 0 deletions Editor/ILogger.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8dcadfa

Please sign in to comment.