Skip to content

Commit b1c065c

Browse files
committed
Save and Rename Dialogs and General restructuring and refactoring
1 parent ed862c3 commit b1c065c

35 files changed

+3386
-1152
lines changed

.vs/Evolution/xs/UserPrefs.xml

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,22 @@
11
<Properties StartupConfiguration="{3D4C3948-95AB-35D4-8403-2BDCFF7854FF}|">
2-
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/Scripts/Saving/CreatureSaver.cs">
2+
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/Scripts/Settings/Settings.cs">
33
<Files>
44
<File FileName="Assets/Scripts/Controllers/SimulationFileManager.cs" Line="17" Column="2" />
55
<File FileName="Assets/Scripts/View/ListLayoutGroup.cs" Line="23" Column="19" />
66
<File FileName="Assets/Scripts/View/ButtonManager.cs" Line="73" Column="16" />
7-
<File FileName="Assets/Scripts/Controllers/CreatureBuilder.cs" Line="880" Column="42" />
8-
<File FileName="Assets/Scripts/Saving/CreatureSaver.cs" Line="44" Column="90" />
7+
<File FileName="Assets/Scripts/Controllers/CreatureBuilder.cs" Line="760" Column="53" />
8+
<File FileName="Assets/Scripts/Saving/CreatureSaver.cs" Line="330" Column="40" />
99
<File FileName="Assets/Scripts/View/FileSelectionViewController.cs" Line="56" Column="44" NotebookId="1" />
1010
<File FileName="Assets/Scripts/View/ViewController.cs" Line="70" Column="8" NotebookId="1" />
1111
<File FileName="Assets/Scripts/View/SimulationLoadDialog.cs" Line="47" Column="56" NotebookId="1" />
12-
<File FileName="Assets/Scripts/Saving/EvolutionSaver.cs" Line="428" Column="23" NotebookId="1" />
12+
<File FileName="Assets/Scripts/Saving/SimulationSerializer.cs" Line="428" Column="23" NotebookId="1" />
1313
<File FileName="Assets/Scripts/View/SelectableListItemView.cs" Line="20" Column="2" NotebookId="1" />
14-
<File FileName="Assets/Scripts/Controllers/CreatureFileManager.cs" Line="99" Column="27" NotebookId="1" />
14+
<File FileName="Assets/Scripts/Controllers/CreatureFileManager.cs" Line="29" Column="39" NotebookId="1" />
15+
<File FileName="Assets/Scripts/View/CreatureDesignControlsView.cs" Line="16" Column="45" NotebookId="1" />
16+
<File FileName="Assets/Scripts/Settings/PlayerPrefsKeys.cs" Line="1" Column="1" NotebookId="1" />
17+
<File FileName="Assets/Scripts/Settings/SettingsMenu.cs" Line="1" Column="1" NotebookId="1" />
18+
<File FileName="Assets/Scripts/Settings/Settings.cs" Line="28" Column="3" NotebookId="1" />
1519
</Files>
16-
<Pads>
17-
<Pad Id="ProjectPad">
18-
<State name="__root__">
19-
<Node name="Evolution" expanded="True">
20-
<Node name="Assets" expanded="True">
21-
<Node name="Scripts" expanded="True">
22-
<Node name="Controllers" expanded="True" />
23-
<Node name="Saving" expanded="True">
24-
<Node name="CreatureSaver.cs" selected="True" />
25-
</Node>
26-
<Node name="View" expanded="True" />
27-
</Node>
28-
</Node>
29-
</Node>
30-
</State>
31-
</Pad>
32-
<Pad Id="ClassPad">
33-
<State name="__root__">
34-
<Node name="Evolution" expanded="True">
35-
<Node name="Assembly-CSharp-Editor" selected="True" />
36-
</Node>
37-
</State>
38-
</Pad>
39-
</Pads>
4020
</MonoDevelop.Ide.Workbench>
4121
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
4222
<MonoDevelop.Ide.ItemProperties.Evolution PreferredExecutionTarget="Unity.Editor" />

.vs/Evolution/xs/sqlite3/storage.ide

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

Assets/Editor/SettingsGen.cs

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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+
}

Assets/Scripts/Saving/EvolutionSaver.cs.meta renamed to Assets/Editor/SettingsGen.cs.meta

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Resources/Materials/Gaussian Blur.mat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Material:
6666
- _Mode: 0
6767
- _OcclusionStrength: 1
6868
- _Parallax: 0.02
69-
- _Radius: 6.6
69+
- _Radius: 14.8
7070
- _SmoothnessTextureChannel: 0
7171
- _SpecularHighlights: 1
7272
- _SrcBlend: 1

0 commit comments

Comments
 (0)