//Helper function that makes it easier to get the stored yaml object public static YamlObject GetYamlObject(MlfBlock block) { if (block == null) { return(null); } return((YamlObject)block.GetFormatData(formatDataKey)); }
//Helper function that formats a string with properties public static string ApplyPropertiesToString(string str, MlfBlock block) { string appliedString = str; foreach (KeyValuePair <string, MlfProperty> property in block.MlfProperties) { string v = property.Value.Value.ToString(); appliedString = appliedString.Replace("$" + property.Key, v); } return(appliedString); }
public override void OnMlfInstanceInterpret(MlfInstance instance) { MlfBlock propertyBlock = instance.GetBlock("Properties"); if (propertyBlock == null) { instance.defaultProperties = null; return; } //Only support yaml format if (propertyBlock.format != "yaml") { Debug.LogError("[MLF] Properties block only supports the 'yaml' format \n" + instance.path); return; } Dictionary <string, MlfProperty> properties = new Dictionary <string, MlfProperty>(); Yaml.YamlObject yaml = (Yaml.YamlObject)propertyBlock.GetFormatData(YamlFormat.formatDataKey); //Root foreach (YamlNode node in ((YamlSequenceNode)yaml.RootNode).Children) { //Should only be a single child for this entry foreach (var entry in ((YamlMappingNode)node).Children) { MlfProperty property = new MlfProperty(); //Scalar -> value = Type if (entry.Value.NodeType == YamlNodeType.Scalar) { System.Type type = null; if (!TypeFinder.TryFindType(((YamlScalarNode)entry.Value).Value, out type)) { continue; } property.type = type; } //Mapping -> read children values else if (entry.Value.NodeType == YamlNodeType.Mapping) { IDictionary <YamlNode, YamlNode> entries = ((YamlMappingNode)entry.Value).Children; if (entries.ContainsKey("specialtype")) { property.specialType = ((YamlScalarNode)entries["specialtype"]).Value; } if (entries.ContainsKey("type")) { System.Type type = null; if (!TypeFinder.TryFindType(((YamlScalarNode)entries["type"]).Value, out type)) { continue; } property.type = type; } //Default value if (entries.ContainsKey("default")) { property.Value = ((YamlScalarNode)entries["default"]).Value; } //Value value, equivilant to default if (entries.ContainsKey("value")) { property.Value = ((YamlScalarNode)entries["value"]).Value; } if (entries.ContainsKey("tooltip")) { property.tooltip = ((YamlScalarNode)entries["tooltip"]).Value; } if (entries.ContainsKey("static")) { //TODO: Use actual resolver instead of crappy check string staticVar = ((YamlScalarNode)entries["static"]).Value.ToLower().Trim(); if (staticVar == "yes" || staticVar == "true") { property.staticVar = true; } else { property.staticVar = false; } } } properties[((YamlScalarNode)entry.Key).Value.TrimStart('$')] = property; } } Dictionary <string, MlfProperty> oldProperties = instance.defaultProperties; instance.defaultProperties = properties; //Replace old values if they exist if (oldProperties != null) { foreach (KeyValuePair <string, MlfProperty> pair in oldProperties) { if (instance.defaultProperties.ContainsKey(pair.Key)) { if (instance.defaultProperties[pair.Key].CanAssignValue(pair.Value.Value)) { instance.defaultProperties[pair.Key].Value = pair.Value.Value; } } } } if (instance.defaultProperties.Count == 0) { return; } //Replace properties in contents //TODO: Make individual formats handle this in a "post post processor", as well as //fix issues with parts of words being replaced! foreach (MlfBlock block in instance.Blocks) { foreach (KeyValuePair <string, MlfProperty> pair in instance.defaultProperties) { block.Content = Regex.Replace(block.Content, @"\s*\$" + pair.Key + @"\s*", Snek.SnekScriptEngine.propertyPrefix + pair.Key); } } }
public static List <MlfBlock> FindBlocks(MlfInstance instance, string script, string path) { List <MlfBlock> codeblocks = new List <MlfBlock>(); //Block header pattern string pattern = @"(?:@.+\n|\r)?\[\[.+\]\]"; MatchCollection matches = Regex.Matches(script, pattern); string[] headers = new string[matches.Count]; for (int i = 0; i < matches.Count; i++) { headers[i] = matches[i].Value; } string[] contents = Regex.Split(script, pattern); int[] lineCounts = new int[contents.Length]; //Build line counts for (int i = 0; i < lineCounts.Length; i++) { lineCounts[i] = Regex.Matches(contents[i], "\n|\r\n?").Count - 1; } //Build blocks for (int i = 0; i < headers.Length; i++) { Match m = Regex.Match(headers[i], @"(|@.+\n|\r)\[\[(.+?)\s*(|"".+"")\s*(|\(.+\))\]\]"); int format_group = 1; int id_group = 2; int tag_group = 3; int argument_group = 4; MlfBlock block = new MlfBlock(instance); block.line = lineCounts[i]; block.path = path; //ID Required if (!m.Groups[id_group].Success) { continue; } //ID block.id = m.Groups[id_group].Value; //Format if (m.Groups[format_group].Success) { block.format = m.Groups[format_group].Value.TrimStart('@').Trim(); } //Tags if (m.Groups[tag_group].Success && m.Groups[tag_group].Value.Length != 0) { block.tags = new List <string>(m.Groups[tag_group].Value.Trim('"').Split(',')); } else { block.tags = new List <string>(); } //Arguments if (m.Groups[argument_group].Success && m.Groups[argument_group].Value.Length != 0) { block.arguments = new List <string>(m.Groups[argument_group].Value.Trim('(', ')').Split(',')); } else { block.arguments = new List <string>(); } //Expression if (contents.Length > i + 1) { block.Content = contents[i + 1]; } block.path = path; codeblocks.Add(block); } //Find "root code" (Code without a block) and implement it into all blocks. if (codeblocks.Count > 1) { foreach (MlfBlock block in codeblocks) { block.AddPrefixText(contents[0]); } } return(codeblocks); }