Variable Constants - Code Generation

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Variable Constants - Code Generation

Post 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("_", "");
        }
    }
}
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Variable Constants - Code Generation

Post by Tony Li »

Thanks for sharing!
Post Reply