static public TCP2_Config CreateFromShader(Shader shader)
    {
        ShaderImporter shaderImporter = ShaderImporter.GetAtPath(AssetDatabase.GetAssetPath(shader)) as ShaderImporter;

        string[] features;
        string[] flags;
        string[] customData;
        Dictionary <string, string> keywords;

        TCP2_ShaderGeneratorUtils.ParseUserData(shaderImporter, out features, out flags, out keywords, out customData);
        if (features != null && features.Length > 0 && features[0] == "USER")
        {
            var config = new TCP2_Config();
            config.ShaderName = shader.name;
            config.Filename   = System.IO.Path.GetFileName(AssetDatabase.GetAssetPath(shader));
            config.Features   = new List <string>(features);
            config.Flags      = (flags != null) ? new List <string>(flags) : new List <string>();
            config.Keywords   = (keywords != null) ? new Dictionary <string, string>(keywords) : new Dictionary <string, string>();
            config.AutoNames();

            //mCurrentShader = shader;
            //mConfigChoice = mUserShadersLabels.IndexOf(shader.name);
            //mDirtyConfig = false;
            //AutoNames();
            //mCurrentHash = mCurrentConfig.ToHash();

            config.isModifiedExternally = false;
            if (customData != null && customData.Length > 0)
            {
                foreach (string data in customData)
                {
                    //Hash
                    if (data.Length > 0 && data[0] == 'h')
                    {
                        string dataHash = data;
                        string fileHash = TCP2_ShaderGeneratorUtils.GetShaderContentHash(shaderImporter);

                        if (!string.IsNullOrEmpty(fileHash) && dataHash != fileHash)
                        {
                            config.isModifiedExternally = true;
                        }
                    }
                    //Timestamp
                    else
                    {
                        ulong timestamp;
                        if (ulong.TryParse(data, out timestamp))
                        {
                            if (shaderImporter.assetTimeStamp != timestamp)
                            {
                                config.isModifiedExternally = true;
                            }
                        }
                    }

                    //Shader Model target
                    if (data.StartsWith("SM:"))
                    {
                        config.shaderTarget = int.Parse(data.Substring(3));
                    }

                    //Configuration Type
                    if (data.StartsWith("CT:"))
                    {
                        config.configType = data.Substring(3);
                    }

                    //Configuration File
                    if (data.StartsWith("CF:"))
                    {
                        config.templateFile = data.Substring(3);
                    }
                }
            }

            return(config);
        }
        else
        {
            return(null);
        }
    }
    private void LoadCurrentConfigFromShader(Shader shader)
    {
        ShaderImporter shaderImporter = ShaderImporter.GetAtPath(AssetDatabase.GetAssetPath(shader)) as ShaderImporter;

        string[] features;
        string[] flags;
        string[] customData;
        Dictionary <string, string> keywords;

        TCP2_ShaderGeneratorUtils.ParseUserData(shaderImporter, out features, out flags, out keywords, out customData);
        if (features != null && features.Length > 0 && features[0] == "USER")
        {
            mCurrentConfig            = new TCP2_Config();
            mCurrentConfig.ShaderName = shader.name;
            mCurrentConfig.Filename   = System.IO.Path.GetFileName(AssetDatabase.GetAssetPath(shader));
            mCurrentConfig.Features   = new List <string>(features);
            mCurrentConfig.Flags      = (flags != null) ? new List <string>(flags) : new List <string>();
            mCurrentConfig.Keywords   = (keywords != null) ? new Dictionary <string, string>(keywords) : new Dictionary <string, string>();
            mCurrentShader            = shader;
            mConfigChoice             = mUserShadersLabels.IndexOf(shader.name);
            mDirtyConfig = false;
            AutoNames();
            mCurrentHash = mCurrentConfig.ToHash();

            mIsModified = false;
            if (customData != null && customData.Length > 0)
            {
                foreach (string data in customData)
                {
                    //Hash
                    if (data.Length > 0 && data[0] == 'h')
                    {
                        string dataHash = data;
                        string fileHash = TCP2_ShaderGeneratorUtils.GetShaderContentHash(shaderImporter);

                        if (!string.IsNullOrEmpty(fileHash) && dataHash != fileHash)
                        {
                            mIsModified = true;
                        }
                    }
                    //Timestamp
                    else
                    {
                        ulong timestamp;
                        if (ulong.TryParse(data, out timestamp))
                        {
                            if (shaderImporter.assetTimeStamp != timestamp)
                            {
                                mIsModified = true;
                            }
                        }
                    }
                }
            }
        }
        else
        {
            EditorApplication.Beep();
            this.ShowNotification(new GUIContent("Invalid shader loaded: it doesn't seem to have been generated by the TCP2 Shader Generator!"));
            mCurrentShader = null;
            NewShader();
        }
    }
Exemple #3
0
    bool ParseUserData(ShaderImporter importer)
    {
        if (string.IsNullOrEmpty(importer.userData))
        {
            return(false);
        }

        string[]      data           = importer.userData.Split(new string[] { "," }, System.StringSplitOptions.RemoveEmptyEntries);
        List <string> customDataList = new List <string>();

        foreach (string d in data)
        {
            if (string.IsNullOrEmpty(d))
            {
                continue;
            }

            switch (d[0])
            {
            //Features
            case 'F':
                if (d == "F")
                {
                    break;                                  //Prevent getting "empty" feature
                }
                this.Features.Add(d.Substring(1));
                break;

            //Flags
            case 'f': this.Flags.Add(d.Substring(1)); break;

            //Keywords
            case 'K':
                string[] kw = d.Substring(1).Split(':');
                if (kw.Length != 2)
                {
                    Debug.LogError("[TCP2 Shader Generator] Error while parsing userData: invalid Keywords format.");
                    return(false);
                }
                else
                {
                    this.Keywords.Add(kw[0], kw[1]);
                }
                break;

            //Custom Data
            case 'c': customDataList.Add(d.Substring(1)); break;

            //old format
            default: this.Features.Add(d); break;
            }
        }

        foreach (string customData in customDataList)
        {
            //Hash
            if (customData.Length > 0 && customData[0] == 'h')
            {
                string dataHash = customData;
                string fileHash = TCP2_ShaderGeneratorUtils.GetShaderContentHash(importer);

                if (!string.IsNullOrEmpty(fileHash) && dataHash != fileHash)
                {
                    this.isModifiedExternally = true;
                }
            }
            //Timestamp
            else
            {
                ulong timestamp;
                if (ulong.TryParse(customData, out timestamp))
                {
                    if (importer.assetTimeStamp != timestamp)
                    {
                        this.isModifiedExternally = true;
                    }
                }
            }

            //Shader Model target
            if (customData.StartsWith("SM:"))
            {
                this.shaderTarget = int.Parse(customData.Substring(3));
            }

            //Configuration Type
            if (customData.StartsWith("CT:"))
            {
                this.configType = customData.Substring(3);
            }

            //Configuration File
            if (customData.StartsWith("CF:"))
            {
                this.templateFile = customData.Substring(3);
            }
        }

        return(true);
    }