Created
August 29, 2019 07:43
-
-
Save andybak/1973ba4fdee5e215ecb28f0db7f0aadb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <copyright file="UpdateShaderGraphAssemblyInfo.cs" company="BovineLabs"> | |
// Copyright (c) BovineLabs. All rights reserved. | |
// </copyright> | |
namespace BovineLabs.Vision.Editor.ShaderGraph | |
{ | |
using System; | |
using System.IO; | |
using System.Linq; | |
using UnityEditor; | |
using UnityEditor.Compilation; | |
/// <summary> | |
/// Adds our assembly to the shader graph AssemblyInfo to give us internal access. A nasty hack required for now. | |
/// </summary> | |
[InitializeOnLoad] | |
public static class UpdateShaderGraphAssemblyInfo | |
{ | |
private const string ShaderGraphAssemblyName = "Unity.ShaderGraph.Editor"; | |
private const string AssemblyInfoName = "AssemblyInfo.cs"; | |
// This line should refer to your own assembly | |
private const string VisibleLine = "[assembly: InternalsVisibleTo(\"BovineLabs.Vision.Editor\")]"; | |
static UpdateShaderGraphAssemblyInfo() | |
{ | |
CompilationPipeline.assemblyCompilationStarted += OnAssemblyCompilationStarted; | |
} | |
private static void OnAssemblyCompilationStarted(string assemblyName) | |
{ | |
var asmdef = CompilationPipeline.GetAssemblyDefinitionFilePathFromAssemblyName(assemblyName); | |
var name = Path.GetFileNameWithoutExtension(asmdef); | |
if (name == null || !name.Equals(ShaderGraphAssemblyName)) | |
{ | |
return; | |
} | |
var directory = Path.GetDirectoryName(asmdef); | |
var assemblyInfoPath = Path.Combine(directory ?? throw new NullReferenceException(), AssemblyInfoName); | |
var assemblyInfo = File.ReadAllLines(assemblyInfoPath); | |
// Already setup | |
if (assemblyInfo.Contains(VisibleLine)) | |
{ | |
return; | |
} | |
TurnOffReadOnly(assemblyInfoPath); | |
File.AppendAllText(assemblyInfoPath, VisibleLine); | |
} | |
private static void TurnOffReadOnly(string file) | |
{ | |
File.SetAttributes(file, File.GetAttributes(file) & ~FileAttributes.ReadOnly); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment