VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_Settings.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using System.Linq;
5 #if UNITY_EDITOR
6 using UnityEditor;
7 #endif
8 
9 namespace AVR.Core {
10  /// <summary>
11  /// Contains constan values, settings etc. which can all be set through *.avr files. Duplicate settings will be overwritten in alphabetical order of the filename.
12  /// </summary>
13  [ExecuteInEditMode]
14  public class AVR_Settings
15  {
16  private static bool initialized = false;
17 
18  /// <summary>
19  /// Initialize settings. Calling multiple times will have no effect, use reconfigure() if you want to re-parse all settingfiles.
20  /// </summary>
21  [RuntimeInitializeOnLoadMethod]
22  #if UNITY_EDITOR
23  [InitializeOnLoadMethod]
24  #endif
25  public static void initialize() {
26  if(initialized) return;
27  settings = AVR_SettingsParser.AutoParseSettingFiles();
28  initialized = true;
29  }
30 
31  /// <summary>
32  /// Re-initializes settings. Note: This will re-parse all settings files and might take a little.
33  /// </summary>
34  public static void reconfigure() {
35  initialized = false;
36  initialize();
37  }
38 
39  /// <summary>
40  /// Dictionary containing all settings by their key. A setting declared as domain = { setting = true } will be retrievable with the token /domain/setting
41  /// </summary>
42  public static Dictionary<string, string> settings = new Dictionary<string, string>();
43 
44  /// <summary>
45  /// Get a registered setting of the type KeyCode.
46  /// </summary>
47  /// <param name="token"> Token identifying the given setting. A setting declared as domain = { setting = true } will be retrievable with the token /domain/setting. </param>
48  /// <returns> Registered setting if a valid is found, otherwise KeyCode.None. </returns>
49  public static KeyCode get_key(string token) {
50  KeyCode key;
51  if (System.Enum.TryParse(AVR.Core.AVR_Settings.get_string(token), out key))
52  {
53  return key;
54  }
55  else {
56  AVR.Core.AVR_DevConsole.cerror(token + " does not contain a valid key or does not exist!", "AVR_Settings");
57  return KeyCode.None;
58  }
59  }
60 
61  /// <summary>
62  /// Get a registered setting of the type int.
63  /// </summary>
64  /// <param name="token"> Token identifying the given setting. A setting declared as domain = { setting = true } will be retrievable with the token /domain/setting. </param>
65  /// <returns> Registered setting if a valid is found, otherwise 0. </returns>
66  public static int get_int(string token)
67  {
68  if (settings.TryGetValue(token, out string ret))
69  {
70  return int.Parse(ret);
71  }
72  else if(!initialized) {
73  initialize();
74  return get_int(token);
75  }
76  else
77  {
78  AVR_DevConsole.warn("Settings do not contain key \"" + token + "\". Using default value 0.");
79  return 0;
80  }
81  }
82 
83  /// <summary>
84  /// Get a registered setting of the type string.
85  /// </summary>
86  /// <param name="token"> Token identifying the given setting. A setting declared as domain = { setting = true } will be retrievable with the token /domain/setting. </param>
87  /// <returns> Registered setting if a valid is found, otherwise an empty string. </returns>
88  public static string get_string(string token) {
89  if (settings.TryGetValue(token, out string ret))
90  {
91  return ret;
92  }
93  else if (!initialized)
94  {
95  initialize();
96  return get_string(token);
97  }
98  else
99  {
100  AVR_DevConsole.warn("Settings do not contain key \"" + token + "\". Using default value (empty string).");
101  return "";
102  }
103  }
104 
105  /// <summary>
106  /// Get a registered setting of the type float.
107  /// </summary>
108  /// <param name="token"> Token identifying the given setting. A setting declared as domain = { setting = true } will be retrievable with the token /domain/setting. </param>
109  /// <returns> Registered setting if a valid is found, otherwise 1.0. </returns>
110  public static float get_float(string token) {
111  if(settings.TryGetValue(token, out string ret)) {
112  return float.Parse(ret);
113  }
114  else if (!initialized)
115  {
116  initialize();
117  return get_float(token);
118  }
119  else {
120  AVR_DevConsole.warn("Settings do not contain key \""+token+"\". Using default value of 1.0f.");
121  return 1.0f;
122  }
123  }
124 
125  /// <summary>
126  /// Get a registered setting of the type bool.
127  /// </summary>
128  /// <param name="token"> Token identifying the given setting. A setting declared as domain = { setting = true } will be retrievable with the token /domain/setting. </param>
129  /// <returns> Registered setting if a valid is found, otherwise false. </returns>
130  public static bool get_bool(string token) {
131  if (settings.TryGetValue(token, out string ret)) {
132  return bool.Parse(ret);
133  }
134  else if (!initialized)
135  {
136  initialize();
137  return get_bool(token);
138  }
139  else
140  {
141  AVR_DevConsole.warn("Settings do not contain key \"" + token + "\". Using default value of false.");
142  return false;
143  }
144  }
145 
146  /// <summary>
147  /// Check if a given token is registered as a setting.
148  /// </summary>
149  /// <param name="token"> Token to check for </param>
150  /// <returns> True if a setting with this token exists, otherwise false. </returns>
151  public static bool token_exists(string token) {
152  return settings.ContainsKey(token);
153  }
154 
155  /// <summary>
156  /// Set a setting with a specific token to a specific value.
157  /// </summary>
158  public static void set(string token, object value) {
159  settings[token] = value.ToString();
160  }
161 
162  /// <summary>
163  /// Builds a tree-structure that contains all settings. Note: This may take a while to complete.
164  /// </summary>
165  /// <returns> Root node of the tree </returns>
166  public static SettingsTreeNode build_tree() {
167  AVR.Core.AVR_DevConsole.print("Building SettingsTree...");
168  SettingsTreeNode root = new SettingsTreeNode("");
169 
170  foreach(string key in settings.Keys) {
171  string[] dir = key.Split('/').Skip(1).ToArray();
172  add_node(dir, root, settings[key]);
173  }
174 
175  return root;
176  }
177 
178  private static void add_node(string[] dir, SettingsTreeNode node, string value) {
179  foreach(string d in dir) {
180  if(!node.has_child(d)) {
181  node.add_child(new SettingsTreeNode(d));
182  }
183  node = node.get_children().First((c) => c.name==d);
184  }
185  node.value = value;
186  }
187  }
188 
189  /// <summary>
190  /// Class representing a node from a settings-tree returned by AVR_Settings.build_tree()
191  /// </summary>
192  public class SettingsTreeNode {
193  public string fullname;
194  public string name;
195  private List<SettingsTreeNode> children;
196  public string value;
197 
198  public bool foldout;
199 
200  public SettingsTreeNode(string name) {
201  this.fullname = this.name = name;
202  this.children = new List<SettingsTreeNode>();
203  this.foldout = false;
204  this.value = "";
205  }
206 
207  public bool is_leaf() {
208  return this.children.Count<1;
209  }
210 
211  public bool has_child(string c) {
212  return children.Any((child) => child.name==c);
213  }
214 
215  public void add_child(SettingsTreeNode n) {
216  this.children.Add(n);
217  n.fullname = this.fullname+"/"+n.fullname;
218  }
219 
221  return children.ToArray();
222  }
223 
224  public void traverse(System.Action<SettingsTreeNode> func) {
225  func.Invoke(this);
226  foreach(SettingsTreeNode c in children) {
227  c.traverse(func);
228  }
229  }
230  }
231 }
static void add_node(string[] dir, SettingsTreeNode node, string value)
SettingsTreeNode[] get_children()
static KeyCode get_key(string token)
Get a registered setting of the type KeyCode.
Definition: AVR_Settings.cs:49
static void initialize()
Initialize settings. Calling multiple times will have no effect, use reconfigure() if you want to re-...
Definition: AVR_Settings.cs:25
Contains constan values, settings etc. which can all be set through *.avr files. Duplicate settings w...
Definition: AVR_Settings.cs:14
static bool token_exists(string token)
Check if a given token is registered as a setting.
static float get_float(string token)
Get a registered setting of the type float.
void traverse(System.Action< SettingsTreeNode > func)
List< SettingsTreeNode > children
SettingsTreeNode(string name)
static void set(string token, object value)
Set a setting with a specific token to a specific value.
static SettingsTreeNode build_tree()
Builds a tree-structure that contains all settings. Note: This may take a while to complete...
void add_child(SettingsTreeNode n)
static int get_int(string token)
Get a registered setting of the type int.
Definition: AVR_Settings.cs:66
static bool get_bool(string token)
Get a registered setting of the type bool.
static string get_string(string token)
Get a registered setting of the type string.
Definition: AVR_Settings.cs:88
bool has_child(string c)
static void reconfigure()
Re-initializes settings. Note: This will re-parse all settings files and might take a little...
Definition: AVR_Settings.cs:34
Class representing a node from a settings-tree returned by AVR_Settings.build_tree() ...