Fixed yaml bug with leading spaces
This commit is contained in:
@@ -262,7 +262,7 @@ namespace OpenRA
|
|||||||
AssertExists("map.yaml");
|
AssertExists("map.yaml");
|
||||||
AssertExists("map.bin");
|
AssertExists("map.bin");
|
||||||
|
|
||||||
var yaml = new MiniYaml(null, MiniYaml.FromStream(Container.GetContent("map.yaml")));
|
var yaml = new MiniYaml(null, MiniYaml.FromStream(Container.GetContent("map.yaml"), path));
|
||||||
FieldLoader.Load(this, yaml);
|
FieldLoader.Load(this, yaml);
|
||||||
|
|
||||||
// Support for formats 1-3 dropped 2011-02-11.
|
// Support for formats 1-3 dropped 2011-02-11.
|
||||||
|
|||||||
@@ -60,6 +60,8 @@ namespace OpenRA
|
|||||||
{
|
{
|
||||||
Console.WriteLine("Failed to load map: {0}", path);
|
Console.WriteLine("Failed to load map: {0}", path);
|
||||||
Console.WriteLine("Details: {0}", e);
|
Console.WriteLine("Details: {0}", e);
|
||||||
|
Log.Write("Debug", "Failed to load map: {0}", path);
|
||||||
|
Log.Write("Debug", "Details: {0}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,20 +83,20 @@ namespace OpenRA
|
|||||||
|
|
||||||
public class MiniYaml
|
public class MiniYaml
|
||||||
{
|
{
|
||||||
static readonly Func<string, string> StringIdentity = s => s;
|
const int SpacesPerLevel = 4;
|
||||||
static readonly Func<MiniYaml, MiniYaml> MiniYamlIdentity = my => my;
|
static Func<string, string> stringIdentity = s => s;
|
||||||
|
static Func<MiniYaml, MiniYaml> miniYamlIdentity = my => my;
|
||||||
public string Value;
|
public string Value;
|
||||||
public List<MiniYamlNode> Nodes;
|
public List<MiniYamlNode> Nodes;
|
||||||
|
|
||||||
public Dictionary<string, MiniYaml> ToDictionary()
|
public Dictionary<string, MiniYaml> ToDictionary()
|
||||||
{
|
{
|
||||||
return ToDictionary(MiniYamlIdentity);
|
return ToDictionary(miniYamlIdentity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Dictionary<string, TElement> ToDictionary<TElement>(Func<MiniYaml, TElement> elementSelector)
|
public Dictionary<string, TElement> ToDictionary<TElement>(Func<MiniYaml, TElement> elementSelector)
|
||||||
{
|
{
|
||||||
return ToDictionary(StringIdentity, elementSelector);
|
return ToDictionary(stringIdentity, elementSelector);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Dictionary<TKey, TElement> ToDictionary<TKey, TElement>(
|
public Dictionary<TKey, TElement> ToDictionary<TKey, TElement>(
|
||||||
@@ -154,13 +154,44 @@ namespace OpenRA
|
|||||||
{
|
{
|
||||||
var line = ll;
|
var line = ll;
|
||||||
++lineNo;
|
++lineNo;
|
||||||
var commentIndex = line.IndexOf('#');
|
|
||||||
if (commentIndex != -1)
|
if (line.Contains('#'))
|
||||||
line = line.Substring(0, commentIndex).TrimEnd(' ', '\t');
|
line = line.Substring(0, line.IndexOf('#')).TrimEnd(' ', '\t');
|
||||||
var t = line.TrimStart(' ', '\t');
|
if (line.Length == 0)
|
||||||
|
continue;
|
||||||
|
var cp = 0;
|
||||||
|
var level = 0;
|
||||||
|
var spaces = 0;
|
||||||
|
var textStart = false;
|
||||||
|
var c = line[cp];
|
||||||
|
while (!(c == '\n' || c == '\r') && cp < line.Length && !textStart)
|
||||||
|
{
|
||||||
|
c = line[cp];
|
||||||
|
switch (c)
|
||||||
|
{
|
||||||
|
case ' ':
|
||||||
|
spaces++;
|
||||||
|
if (spaces >= SpacesPerLevel)
|
||||||
|
{
|
||||||
|
spaces = 0;
|
||||||
|
level++;
|
||||||
|
}
|
||||||
|
|
||||||
|
cp++;
|
||||||
|
break;
|
||||||
|
case '\t':
|
||||||
|
level++;
|
||||||
|
cp++;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
textStart = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var t = line.Substring(cp);
|
||||||
if (t.Length == 0)
|
if (t.Length == 0)
|
||||||
continue;
|
continue;
|
||||||
var level = line.Length - t.Length;
|
|
||||||
var location = new MiniYamlNode.SourceLocation { Filename = filename, Line = lineNo };
|
var location = new MiniYamlNode.SourceLocation { Filename = filename, Line = lineNo };
|
||||||
|
|
||||||
if (levels.Count <= level)
|
if (levels.Count <= level)
|
||||||
@@ -211,15 +242,15 @@ namespace OpenRA
|
|||||||
return FromLines(File.ReadAllLines(path), path);
|
return FromLines(File.ReadAllLines(path), path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<MiniYamlNode> FromStream(Stream s)
|
public static List<MiniYamlNode> FromStream(Stream s, string fileName = "<no filename available>")
|
||||||
{
|
{
|
||||||
using (var reader = new StreamReader(s))
|
using (var reader = new StreamReader(s))
|
||||||
return FromString(reader.ReadToEnd());
|
return FromString(reader.ReadToEnd(), fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<MiniYamlNode> FromString(string text)
|
public static List<MiniYamlNode> FromString(string text, string fileName = "<no filename available>")
|
||||||
{
|
{
|
||||||
return FromLines(text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries), "<no filename available>");
|
return FromLines(text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries), fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<MiniYamlNode> MergeLiberal(List<MiniYamlNode> a, List<MiniYamlNode> b)
|
public static List<MiniYamlNode> MergeLiberal(List<MiniYamlNode> a, List<MiniYamlNode> b)
|
||||||
|
|||||||
@@ -98,6 +98,8 @@ namespace OpenRA
|
|||||||
/// <summary>Replace special character prefixes with full paths</summary>
|
/// <summary>Replace special character prefixes with full paths</summary>
|
||||||
public static string ResolvePath(string path)
|
public static string ResolvePath(string path)
|
||||||
{
|
{
|
||||||
|
path = path.TrimEnd(new char[] { ' ', '\t' });
|
||||||
|
|
||||||
// paths starting with ^ are relative to the support dir
|
// paths starting with ^ are relative to the support dir
|
||||||
if (path.StartsWith("^"))
|
if (path.StartsWith("^"))
|
||||||
path = SupportDir + path.Substring(1);
|
path = SupportDir + path.Substring(1);
|
||||||
|
|||||||
@@ -26,12 +26,40 @@ namespace OpenRA.Test
|
|||||||
FromParent:
|
FromParent:
|
||||||
FromParentRemove:
|
FromParentRemove:
|
||||||
";
|
";
|
||||||
|
|
||||||
readonly string yamlForChild = @"
|
readonly string yamlForChild = @"
|
||||||
Child:
|
Child:
|
||||||
Inherits: ^Parent
|
Inherits: ^Parent
|
||||||
FromChild:
|
FromChild:
|
||||||
-FromParentRemove:
|
-FromParentRemove:
|
||||||
";
|
";
|
||||||
|
|
||||||
|
readonly string yamlTabStyle = @"
|
||||||
|
Root1:
|
||||||
|
Child1:
|
||||||
|
Attribute1: Test
|
||||||
|
Attribute2: Test
|
||||||
|
Child2:
|
||||||
|
Attribute1: Test
|
||||||
|
Attribute2: Test
|
||||||
|
Root2:
|
||||||
|
Child1:
|
||||||
|
Attribute1: Test
|
||||||
|
";
|
||||||
|
|
||||||
|
readonly string yamlMixedStyle = @"
|
||||||
|
Root1:
|
||||||
|
Child1:
|
||||||
|
Attribute1: Test
|
||||||
|
Attribute2: Test
|
||||||
|
Child2:
|
||||||
|
Attribute1: Test
|
||||||
|
Attribute2: Test
|
||||||
|
Root2:
|
||||||
|
Child1:
|
||||||
|
Attribute1: Test
|
||||||
|
";
|
||||||
|
|
||||||
List<MiniYamlNode> parentList;
|
List<MiniYamlNode> parentList;
|
||||||
List<MiniYamlNode> childList;
|
List<MiniYamlNode> childList;
|
||||||
MiniYaml parent;
|
MiniYaml parent;
|
||||||
@@ -85,5 +113,15 @@ Child:
|
|||||||
Assert.That(res.Key, Is.EqualTo("Child"));
|
Assert.That(res.Key, Is.EqualTo("Child"));
|
||||||
InheritanceTest(res.Value.Nodes);
|
InheritanceTest(res.Value.Nodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestCase(TestName = "Mixed tabs & spaces indents")]
|
||||||
|
public void TestIndents()
|
||||||
|
{
|
||||||
|
var tabs = MiniYaml.FromString(yamlTabStyle, "yamlTabStyle").WriteToString();
|
||||||
|
Console.WriteLine(tabs);
|
||||||
|
var mixed = MiniYaml.FromString(yamlMixedStyle, "yamlMixedStyle").WriteToString();
|
||||||
|
Console.WriteLine(mixed);
|
||||||
|
Assert.That(tabs, Is.EqualTo(mixed));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user