Page 1 of 1

Variable Constants - Code Generation

Posted: Fri Aug 07, 2020 10:30 pm
by VoodooDetective
I was thinking it'd be nice to be able to reference variable names as constants so I wrote a little script to help me. It occurred to me that it might be a worthwhile addition so I figured I'd share in case:
A) anyone was interested
B) it seemed worth adding (albeit after some serious cleanup)

Here's what I wrote:

Code: Select all

using System.Globalization;
using System.IO;
using PixelCrushers.DialogueSystem;
using UnityEditor;
using UnityEngine;

namespace EditorMods
{
    public class DialogueDatabaseConstantGenerator : MonoBehaviour
    {
        private const string MenuRoot = "Tools/Dialogue System Addons/";
        private const string DatabaseLocation = "Assets/Dialogue";
        private const string OutputScriptName = "DialogueSystemVariableConstants";
        private const string DialogueDatabaseName = "Voodoo Detective Dialogue Database.asset";
        private const string LanguageCode = "en-US";

        /// <summary>
        /// Generate a file full of constants for each database variable. 
        /// </summary>
        [MenuItem(MenuRoot + "Generate Variable Constants", false, 50)]
        public static void GenerateVariableConstants()
        {
            // Fetch Database
            Object[] assets = AssetDatabase.LoadAllAssetsAtPath(DatabaseLocation + "/" + DialogueDatabaseName);
            if (assets.Length != 1) throw new System.Exception("Could not find database.");
            DialogueDatabase database = assets[0] as DialogueDatabase;
            if (database == null) throw new System.Exception("Dialogue database could not be loaded.");
            TextInfo textInfo = new CultureInfo(LanguageCode, false).TextInfo;

            // Create File
            string fileLocation = DatabaseLocation + "/" + OutputScriptName + ".cs";
            if (File.Exists(fileLocation)) File.Delete(fileLocation);
            using (StreamWriter constantFile = new StreamWriter(fileLocation))
            {
                // Generate Script 
                constantFile.WriteLine("namespace PixelCrushers.DialogueSystem");
                constantFile.WriteLine("{");
                constantFile.WriteLine("    /// <summary>");
                constantFile.WriteLine("    /// This file was generated. Do not edit.");
                constantFile.WriteLine("    /// </summary>");
                constantFile.WriteLine("    public class " + OutputScriptName);
                constantFile.WriteLine("    {");
                foreach (Variable var in database.variables)
                {
                    constantFile.WriteLine("        public const string " + SanitizeVariableName(textInfo, var.Name) + " = \"" + var.Name + "\";");
                }
                constantFile.WriteLine("    }");
                constantFile.WriteLine("}");
            }

            // Refresh Database
            AssetDatabase.Refresh();
        }

        private static string SanitizeVariableName(TextInfo textInfo, string varName)
        {
            return textInfo.ToTitleCase(varName).Replace("_", "");
        }
    }
}

Re: Variable Constants - Code Generation

Posted: Sat Aug 08, 2020 8:47 am
by Tony Li
Thanks for sharing!