From f0871c9942a844fda8bf60a6a79ab0675d71bca2 Mon Sep 17 00:00:00 2001 From: roylee Date: Mon, 14 Apr 2025 16:59:58 +0800 Subject: [PATCH] Fix for differnet kinds of included path --- Editor/PackagePathResolver.cs | 33 ++++++++++++++++ Editor/PackagePathResolver.cs.meta | 11 ++++++ Editor/ShaderVariantTool_Data.cs | 60 +++++++++++++++++++++++++++++- 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 Editor/PackagePathResolver.cs create mode 100644 Editor/PackagePathResolver.cs.meta diff --git a/Editor/PackagePathResolver.cs b/Editor/PackagePathResolver.cs new file mode 100644 index 0000000..157435a --- /dev/null +++ b/Editor/PackagePathResolver.cs @@ -0,0 +1,33 @@ +namespace GfxQA.ShaderVariantTool +{ + using System; + using System.IO; + using System.Reflection; + + using UnityEditor.PackageManager; + + using UnityEngine; + + public static class PackagePathResolver + { + public static string GetPackageAbsolutePath(string packageName) + { + Type packageInfoType = typeof(PackageInfo); + MethodInfo method = packageInfoType.GetMethod("GetAllRegisteredPackages", BindingFlags.NonPublic | BindingFlags.Static); + if (method != null) + { + var packages = (PackageInfo[])method.Invoke(null, null); + foreach (PackageInfo package in packages) + { + if (package.name == packageName) + { + return Path.GetFullPath(package.resolvedPath); + } + } + } + + Debug.LogWarning($"Package '{packageName}' not found."); + return null; + } + } +} \ No newline at end of file diff --git a/Editor/PackagePathResolver.cs.meta b/Editor/PackagePathResolver.cs.meta new file mode 100644 index 0000000..c135b2c --- /dev/null +++ b/Editor/PackagePathResolver.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5b6fa60b27ef455bb4ebdca0aa043213 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/ShaderVariantTool_Data.cs b/Editor/ShaderVariantTool_Data.cs index f40e1dc..6301c10 100644 --- a/Editor/ShaderVariantTool_Data.cs +++ b/Editor/ShaderVariantTool_Data.cs @@ -5,6 +5,7 @@ using UnityEditor; using System.Linq; using System; +using System.IO; using System.Text.RegularExpressions; namespace GfxQA.ShaderVariantTool @@ -148,6 +149,7 @@ public void SetKeywordDeclareType() var shader = Shader.Find(name); var shaderData = ShaderUtil.GetShaderData(shader); var subShader = shaderData.GetSubshader(0); + var shaderPath = AssetDatabase.GetAssetPath(shader); for (int i = 0; i < subShader.PassCount; i++) { //Get source code @@ -160,12 +162,13 @@ public void SetKeywordDeclareType() //Read #include_with_pragmas lines as declare types are in seperate hlsl string pattern = @"#include_with_pragmas\s""(.*)"""; MatchCollection matches = Regex.Matches(shaderCode, pattern); + string shaderDirectoryPath = Path.GetDirectoryName(shaderPath); List matchesList = matches.ToList(); foreach(Match m in matchesList) { //Read hlsl code and find the #pragma lines string hlslPath = m.Groups[1].Value; - string hlslCode = System.IO.File.ReadAllText(hlslPath); + string hlslCode = ReadFromAnyPath(shaderDirectoryPath, hlslPath); GetDirectPragmaKeywordType(hlslCode, ref keywordDeclareType); } } @@ -179,6 +182,61 @@ public void SetKeywordDeclareType() } } + private string ReadFromAnyPath(string sourceDirectory, string path) + { + if (TryReadRelativePath(path, out string content)) + { + return content; + } + + if (TryReadAbsolutePath(path, out content)) + { + return content; + } + + if (TryReadUPMPackagePath(path, out content)) + { + return content; + } + + Debug.LogError($"ShaderVariantTool error. Included file {path} is not found."); + return null; + + bool TryReadRelativePath(string filePath, out string contentRelativePath) + { + string fullPath = Path.Combine(sourceDirectory, filePath); + return TryReadAbsolutePath(fullPath, out contentRelativePath); + } + + bool TryReadAbsolutePath(string filePath, out string contentAbsolutePath) + { + if (File.Exists(filePath)) + { + contentAbsolutePath = File.ReadAllText(filePath); + return true; + } + + contentAbsolutePath = null; + return false; + } + + bool TryReadUPMPackagePath(string filePath, out string contentUPMPackage) + { + const string pattern = @"Packages\/(.*)\/(.*)"; + MatchCollection matches = Regex.Matches(filePath, pattern); + if (matches.Count > 0) + { + string packageName = matches[0].Groups[1].Value; + string packageRoot = PackagePathResolver.GetPackageAbsolutePath(packageName); + string fullPath = Path.Combine(packageRoot, matches[0].Groups[2].Value); + return TryReadAbsolutePath(fullPath, out contentUPMPackage); + } + + contentUPMPackage = null; + return false; + } + } + private void GetDirectPragmaKeywordType(string shaderCode, ref Dictionary keywordDeclareType) { //Read shader code and find the #pragma lines