|
| 1 | +using System.IO; |
| 2 | +using System.Text; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | +using UnityEditor; |
| 6 | +using UnityEngine; |
| 7 | + |
| 8 | +public class SettingsGen { |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// The path to the settings file to be parsed relative to the root of |
| 12 | + /// the assets directory. |
| 13 | + /// </summary> |
| 14 | + private const string INPUT_PATH = "Scripts/Settings/settings.txt"; |
| 15 | + /// <summary> |
| 16 | + /// The path to the directory that the generated .cs file should be written |
| 17 | + /// to. |
| 18 | + /// </summary> |
| 19 | + /// <remarks> An existing file with the same name will be overwritten!</remarks> |
| 20 | + private const string OUTPUT_DIR = "Scripts/Settings"; |
| 21 | + /// <summary> |
| 22 | + /// The name of the generated C# class. |
| 23 | + /// </summary> |
| 24 | + private const string OUTPUT_CLASS_NAME = "Settings"; |
| 25 | + |
| 26 | + private static readonly Regex PARSING_PATTERN = |
| 27 | + new Regex(@"(bool|string|int|float) ([\w\d_]+);?( ?= ?([^;]+);?)?( ?\(([^)]+)\);?)?"); |
| 28 | + |
| 29 | + private static readonly Regex UPPERCACSE_PATTERN = new Regex("[A-Z]+|[0-9]+"); |
| 30 | + |
| 31 | + private const string IMPORTS = "using UnityEngine;"; |
| 32 | + private const string HELPERS = @" private static bool GetBool(string key) { |
| 33 | + return PlayerPrefs.GetInt(key, 0) == 1; |
| 34 | + } |
| 35 | +
|
| 36 | + private static void SetBool(string key, bool b) { |
| 37 | + PlayerPrefs.SetInt(key, b ? 1 : 0); |
| 38 | + PlayerPrefs.Save(); |
| 39 | + } |
| 40 | + |
| 41 | + public static void Save() { |
| 42 | + PlayerPrefs.Save(); |
| 43 | + } |
| 44 | + |
| 45 | + public static void Reset() { |
| 46 | + PlayerPrefs.DeleteAll(); |
| 47 | + Initialize(); |
| 48 | + }"; |
| 49 | + |
| 50 | + [MenuItem("Tools/SettingsGen/Generate")] |
| 51 | + public static void Generate() { |
| 52 | + |
| 53 | + var inputPath = Path.Combine(Application.dataPath, INPUT_PATH); |
| 54 | + var outputPath = Path.Combine(Application.dataPath, OUTPUT_DIR); |
| 55 | + outputPath = Path.Combine(outputPath, string.Format("{0}.cs", OUTPUT_CLASS_NAME)); |
| 56 | + |
| 57 | + var keyDeclarations = new List<string>(); |
| 58 | + var properties = new List<string>(); |
| 59 | + var initializations = new List<string>(); |
| 60 | + |
| 61 | + var lines = new List<string>(File.ReadAllLines(inputPath)); |
| 62 | + foreach (var line in lines) { |
| 63 | + if ("".Equals(line)) { continue; } |
| 64 | + |
| 65 | + var match = PARSING_PATTERN.Match(line); |
| 66 | + if (match.Success) { |
| 67 | + |
| 68 | + var groups = match.Groups; |
| 69 | + var type = groups[1].Value; |
| 70 | + var propName = groups[2].Value; |
| 71 | + var init = groups[4].Success ? groups[4].Value : null; |
| 72 | + var forcedKey = groups[6].Success ? groups[6].Value : null; |
| 73 | + |
| 74 | + properties.Add(ToProperty(propName, type)); |
| 75 | + keyDeclarations.Add(ToKeyMember(propName, forcedKey)); |
| 76 | + |
| 77 | + if (init != null) { |
| 78 | + initializations.Add(string.Format("{0} = {1};", propName, init)); |
| 79 | + } |
| 80 | + |
| 81 | + } else { |
| 82 | + Debug.Log(string.Format("Invalid line: {0}", line)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + var builder = new StringBuilder(); |
| 87 | + builder.AppendLine(IMPORTS); |
| 88 | + builder.AppendLine(""); |
| 89 | + builder.AppendLine(string.Format("public class {0} {{", OUTPUT_CLASS_NAME)); |
| 90 | + builder.AppendLine(""); |
| 91 | + foreach (var keyInit in keyDeclarations) { |
| 92 | + builder.AppendLine(keyInit); |
| 93 | + } |
| 94 | + builder.AppendLine(""); |
| 95 | + foreach (var prop in properties) { |
| 96 | + builder.AppendLine(prop); |
| 97 | + builder.AppendLine(""); |
| 98 | + } |
| 99 | + builder.AppendLine(CreateInitializer(initializations)); |
| 100 | + builder.AppendLine(HELPERS); |
| 101 | + builder.AppendLine("}"); |
| 102 | + |
| 103 | + var fileInfo = new FileInfo(outputPath); |
| 104 | + if (!fileInfo.Exists) |
| 105 | + Directory.CreateDirectory(fileInfo.Directory.FullName); |
| 106 | + |
| 107 | + File.WriteAllText(outputPath, builder.ToString()); |
| 108 | + } |
| 109 | + |
| 110 | + private static string CreateInitializer(List<string> initializations) { |
| 111 | + |
| 112 | + // Decrease the potential of this initialization flag being used as a settings |
| 113 | + // key |
| 114 | + var uuid = System.Guid.NewGuid().ToString(); |
| 115 | + var firstTimeKey = string.Format("ALREADY_INITIALIZED_{0}", uuid); |
| 116 | + var builder = new StringBuilder(); |
| 117 | + builder.AppendFormat("\tstatic {0}() {{\n", OUTPUT_CLASS_NAME); |
| 118 | + builder.AppendFormat("\t\tif (GetBool(\"{0}\")) {{ return; }}\n", firstTimeKey); |
| 119 | + builder.AppendFormat("\t\tInitialize();\n"); |
| 120 | + builder.AppendFormat("\t\tSetBool(\"{0}\", true);\n", firstTimeKey); |
| 121 | + builder.AppendLine("\t\tSave();"); |
| 122 | + builder.AppendLine("\t}"); |
| 123 | + builder.AppendLine(""); |
| 124 | + builder.AppendLine("\tprivate static void Initialize() {"); |
| 125 | + foreach (var init in initializations) { |
| 126 | + builder.AppendLine(string.Format("\t\t{0}", init)); |
| 127 | + } |
| 128 | + builder.AppendLine("\t}"); |
| 129 | + return builder.ToString(); |
| 130 | + } |
| 131 | + |
| 132 | + private static string ToKeyMember(string pascalCaseKey, string forcedKey) { |
| 133 | + |
| 134 | + var identifier = ToKey(pascalCaseKey); |
| 135 | + var key = forcedKey == null ? identifier : forcedKey; |
| 136 | + return string.Format("\tprivate const string {0} = \"{1}\";", identifier, key); |
| 137 | + } |
| 138 | + |
| 139 | + private static string ToProperty(string propName, string type) { |
| 140 | + |
| 141 | + var key = ToKey(propName); |
| 142 | + |
| 143 | + switch (type) { |
| 144 | + case "bool": return GenerateBoolProperty(propName, key); |
| 145 | + case "string": return GenerateStringProperty(propName, key); |
| 146 | + case "int": return GenerateIntProperty(propName, key); |
| 147 | + case "float": return GenerateFloatProperty(propName, key); |
| 148 | + default: throw new System.Exception(string.Format("Unrecognized type: {0}", type)); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private static string GenerateBoolProperty(string propName, string key) { |
| 153 | + return string.Format("\tpublic static bool {0} {{\n" + |
| 154 | + "\t\tget {{ return GetBool({1}); }}\n" + |
| 155 | + "\t\tset {{ SetBool({1}, value); }}\n" + |
| 156 | + "\t}}", propName, key); |
| 157 | + } |
| 158 | + |
| 159 | + private static string GenerateStringProperty(string propName, string key) { |
| 160 | + return string.Format("\tpublic static string {0} {{\n" + |
| 161 | + "\t\tget {{ return PlayerPrefs.GetString({1}, \"\"); }}\n" + |
| 162 | + "\t\tset {{ PlayerPrefs.SetString({1}, value); Save(); }}\n" + |
| 163 | + "\t}}", propName, key); |
| 164 | + } |
| 165 | + |
| 166 | + private static string GenerateIntProperty(string propName, string key) { |
| 167 | + return string.Format("\tpublic static int {0} {{\n" + |
| 168 | + "\t\tget {{ return PlayerPrefs.GetInt({1}, -1); }}\n" + |
| 169 | + "\t\tset {{ PlayerPrefs.SetInt({1}, value); Save(); }}\n" + |
| 170 | + "\t}}", propName, key); |
| 171 | + } |
| 172 | + |
| 173 | + private static string GenerateFloatProperty(string propName, string key) { |
| 174 | + return string.Format("\tpublic static float {0} {{\n" + |
| 175 | + "\t\tget {{ return PlayerPrefs.GetFloat({1}, -1); }}\n" + |
| 176 | + "\t\tset {{ PlayerPrefs.SetFloat({1}, value); Save(); }}\n" + |
| 177 | + "\t}}", propName, key); |
| 178 | + } |
| 179 | + |
| 180 | + private static string ToKey(string pascalCaseProperty) { |
| 181 | + return string.Format("{0}_KEY", ToUpperCase(pascalCaseProperty)); |
| 182 | + } |
| 183 | + |
| 184 | + private static string ToUpperCase(string pascalCase) { |
| 185 | + |
| 186 | + return UPPERCACSE_PATTERN.Replace(pascalCase, "_$0", int.MaxValue, 2).ToUpper(); |
| 187 | + } |
| 188 | +} |
0 commit comments