Skip to content

Commit

Permalink
feat: update asset upgrade system to unregister always included shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Dec 1, 2024
1 parent ee15c28 commit 906bdb6
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Coffee.UISoftMaskInternal.AssetModification;
using UnityEditor;

Expand Down Expand Up @@ -32,7 +34,13 @@ public UISoftMaskModifierRunner()
new SoftMaskableComponentModifier()
}
}),
(".shader", SoftMaskableShaderModifier.Create)
(".shader", SoftMaskableShaderModifier.Create),
(".shader", x => new AlwaysIncludedShadersModifier()
{
path = x,
excludePattern =
new Regex(@"(Hidden/UI/SoftMask|Hidden/UI/TerminalMaskingShape|\(SoftMaskable\))")
})
})
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;

namespace Coffee.UISoftMaskInternal.AssetModification
{
internal class AlwaysIncludedShadersModifier : Modifier
{
public Regex includePattern;
public Regex excludePattern;
protected override string id => "AlwaysIncludedShaders";
private bool _toExclude;
private bool _toInclude;

protected override string ModificationReport()
{
if (_toExclude)
{
return "-> excluded";
}

if (_toInclude)
{
return "-> included";
}

return string.Empty;
}

protected override bool RunModify(bool dryRun)
{
var shader = AssetDatabase.LoadAssetAtPath<Shader>(path);
if (!shader) return false;

var included = AlwaysIncludedShadersProxy.GetShaders()
.Contains(shader);
var shouldExclude = excludePattern?.IsMatch(shader.name) ?? false;
var shouldInclude = includePattern?.IsMatch(shader.name) ?? false;

if (shouldExclude && included)
{
_toExclude = true;
if (!dryRun)
{
AlwaysIncludedShadersProxy.Remove(shader);
}

return true;
}

if (shouldInclude && !included)
{
_toInclude = true;
if (!dryRun)
{
AlwaysIncludedShadersProxy.Add(shader);
}

return true;
}

return false;
}

private static class AlwaysIncludedShadersProxy
{
private static GraphicsSettings s_GraphicsSettings;

private static SerializedProperty GetSerializedProperty()
{
if (!s_GraphicsSettings)
{
s_GraphicsSettings = typeof(GraphicsSettings).GetMethod("GetGraphicsSettings",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
?.Invoke(null, null) as GraphicsSettings;
}

return new SerializedObject(s_GraphicsSettings).FindProperty("m_AlwaysIncludedShaders");
}

public static IEnumerable<Shader> GetShaders()
{
var sp = GetSerializedProperty();
for (var i = 0; i < sp.arraySize; i++)
{
if (sp.GetArrayElementAtIndex(i).objectReferenceValue is Shader shader)
{
yield return shader;
}
}
}

public static void Remove(Shader shader)
{
var changed = false;
var sp = GetSerializedProperty();
for (var i = 0; i < sp.arraySize; i++)
{
// Found the shader to remove.
if (sp.GetArrayElementAtIndex(i).objectReferenceValue == shader)
{
sp.DeleteArrayElementAtIndex(i);
changed = true;
}
}

if (changed)
{
ShrinkArray(sp);
sp.serializedObject.ApplyModifiedProperties();
}
}

public static void Add(Shader shader)
{
var changed = true;
var sp = GetSerializedProperty();
for (var i = 0; i < sp.arraySize; i++)
{
// Already included.
if (sp.GetArrayElementAtIndex(i).objectReferenceValue == shader)
{
changed = false;
break;
}
}

if (changed)
{
var index = sp.arraySize;
sp.InsertArrayElementAtIndex(index);
sp.GetArrayElementAtIndex(index).objectReferenceValue = shader;
ShrinkArray(sp);
sp.serializedObject.ApplyModifiedProperties();
}
}

private static void ShrinkArray(SerializedProperty sp)
{
var removed = 0;
for (var i = 0; i < sp.arraySize; i++)
{
if (!sp.GetArrayElementAtIndex(i).objectReferenceValue)
{
removed++;
}
else if (0 < removed)
{
sp.MoveArrayElement(i, i - removed);
}
}

sp.arraySize -= removed;
}
}
}
}

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

13 changes: 6 additions & 7 deletions Packages/src/Editor/Internal/AssetModification/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@ protected Runner(string name, List<(string ext, Func<string, Modifier> create)>
_factory = factory;
}

private Modifier CreateFromPath(string assetPath)
private IEnumerable<Modifier> CreateFromPath(string assetPath)
{
var ext = Path.GetExtension(assetPath);
return _factory
.FirstOrDefault(x => x.ext == ext)
.create?.Invoke(assetPath);
.Where(x => x.ext == ext)
.Select(f => f.create?.Invoke(assetPath));
}

private Modifier[] GetModifiers(string[] assetPaths)
protected virtual Modifier[] GetModifiers(string[] assetPaths)
{
return assetPaths
.Where(x => x.StartsWith("Assets/", StringComparison.Ordinal))
.Select(CreateFromPath)
.SelectMany(CreateFromPath)
.Where(x => x != null)
.OrderBy(x => Path.GetExtension(x.path))
.ToArray();
Expand Down Expand Up @@ -84,7 +83,7 @@ public virtual void Run(string[] assetPaths, bool dryRun)
finally
{
var sb = new StringBuilder();
sb.Append(dryRun ? "<b>[DryRun]</b> " : "");
sb.Append(dryRun ? "<b><color=orange>[DryRun]</color></b> " : "");
sb.AppendLine($"<b>Modify '{_name}' is {(canceled ? "canceled" : "completed")}.</b>");
sb.AppendLine("==== Modifications ====");
Debug.Log(modifiers.Aggregate(sb, (x, m) => x.Append(m.GetModificationReport())));
Expand Down

0 comments on commit 906bdb6

Please sign in to comment.