1 using System.Collections;
2 using System.Collections.Generic;
5 using System.Text.RegularExpressions;
19 string[] files = System.IO.Directory.GetFiles(Application.dataPath+
"/..",
"*.avr", SearchOption.AllDirectories);
20 System.Array.Sort(files, (a, b) =>
string.Compare(b, a));
22 Dictionary<string, string> entries =
new Dictionary<string, string>();
23 foreach(
string file
in files) {
24 if(exclude_overridesettings && file.Contains(
"~overridesettings.avr"))
continue;
25 entries = ParseSettings(file, entries);
36 public static Dictionary<string, string>
ParseSettings(
string filepath, Dictionary<string, string> entries = null) {
37 AVR.Core.AVR_DevConsole.print(
"Parsing settingsfile: "+filepath);
40 content = File.ReadAllText(filepath);
41 }
catch(System.Exception) {
42 AVR.Core.AVR_DevConsole.error(
"Could not open settings file: " + filepath);
47 content = Regex.Replace(content,
"#.*(\\r\\n|\\n|\\r)",
"");
50 List<string> tokens =
new List<string>(Regex.Split(content,
"(?<!\\G\\S{0,99999}[\"'].{0,99999})\\s|(?<=\\G\\S{0,99999}\".{0,99999}\"\\S{0,99999})\\s|(?<=\\G\\S{0,99999}'.{0,99999}'\\S{0,99999})\\s"));
53 for(
int i=0; i<tokens.Count; i++) {
54 tokens[i] = tokens[i].Trim().Trim(
'\"');
55 if(tokens[i].Length<1) {
62 if(entries==null) entries =
new Dictionary<string, string>();
63 parseDepth(
"", 0, tokens, entries);
69 private static int parseDepth(
string context,
int i, List<string> content, Dictionary<string, string> entries) {
70 while (i < content.Count)
72 if (content[i] ==
"}")
return i + 1;
84 else if (is_identifier(content[i]) && content[i + 1]==
"=" && content[i + 2]==
"{")
86 i = parseDepth(context +
"/" + content[i], i + 3, content, entries);
88 else if (is_identifier(content[i]) && content[i + 1]==
"=")
90 entries[context +
"/" + content[i]] = content[i + 2];
95 AVR.Core.AVR_DevConsole.cerror(
"Incorrect Token Syntax at (index "+i+
"): " + content[i] +
" " + content[i + 1] +
" " + content[i + 2],
"SettingsParser");
105 return Regex.IsMatch(str,
"\\w+");
static Dictionary< string, string > AutoParseSettingFiles(bool exclude_overridesettings=false)
Automatically parse all settingsfiles found that have the *.avr suffix. Duplicate settings are overwr...
static int parseDepth(string context, int i, List< string > content, Dictionary< string, string > entries)
static bool is_identifier(string str)
Parser used to parse setting-files.
static Dictionary< string, string > ParseSettings(string filepath, Dictionary< string, string > entries=null)
Parse a single given settings-file.