private static KspObject ReadObjectTree (KspToken token, Action<KspObject, KspToken> addTokenMapping)
        {
            KspObject obj;

            // Treat global tokens as craft object -> needed for .craft file support
            var type = token.IsGlobalToken () ? KspCraftObject.TypeId : token.Name;

            switch (type) {
            case KspCraftObject.TypeId:
                obj = new KspCraftObject (token.IsGlobalToken ());
                break;
            case KspPartObject.TypeId:
                obj = new KspPartObject (token.IsGlobalToken ());
                break;
            default:
                obj = new KspObject (token.Name, token.IsGlobalToken ());
                break;
            }

            foreach (var t in token.Tokens) {
                obj.AddChild (ReadObjectTree (t, addTokenMapping));
            }

            addTokenMapping (obj, token);
            return obj;
        }
        public void CanUpdatePartReferences ()
        {
            // given
            var obj = new KspObject ("OBJ")
                .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "part1")))
                .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "part2")))
                .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "part3")));

            var parts = obj.Children<KspPartObject> ().ToArray ();

            var part = obj.Children [0]
                .AddProperty (new KspStringProperty ("not-a-link", "not-a-link"))
                .AddProperty (new KspPartLinkProperty (KspPartLinkProperty.Types.Link, "top", parts [0], false))
                .AddProperty (new KspPartLinkProperty (KspPartLinkProperty.Types.Parent, "top", parts [0], false))
                .AddProperty (new KspPartLinkProperty (KspPartLinkProperty.Types.Parent, "top", parts [1], false)) as KspPartObject;

            var newParentLink = new KspPartLinkProperty (KspPartLinkProperty.Types.Parent, "bottom", parts [1], false);

            var expectedProperties = new [] {
                part.Properties [0],
                part.Properties [1],
                part.Properties [2],
                newParentLink
            };

            // when
            part.UpdatePartLinks (KspPartLinkProperty.Types.Parent, new[] { newParentLink });

            // then
            Assert.That (part.Properties, Is.EqualTo (expectedProperties));
        }
 private static void ReadStringProperties (KspObject obj, List<KeyValuePair<String, String>> attributes)
 {
     foreach (var attribute in attributes) {
         if (IsStringAttribute (obj, attribute)) {
             obj.InsertProperty (attributes.IndexOf (attribute), new KspStringProperty (attribute.Key, attribute.Value));
         }
     }
 }
 private static void ReadPartLinkProperties (KspObject obj, List<KeyValuePair<String, String>> attributes)
 {
     foreach (var attribute in attributes) {
         if (IsPartLinkAttribute (obj, attribute)) {
             obj.InsertProperty (attributes.IndexOf (attribute), ReadPartLinkProperty (obj, attribute));
         }
     }
 }
Exemplo n.º 5
0
        public static void SaveToFile (String filePath, KspObject kspObjTree)
        {
            var craftToken = KspObjectWriter.WriteObject (kspObjTree);
            var craftString = KspTokenWriter.WriteToken (craftToken, new StringBuilder ()).ToString ();

            using (var textWriter = new StreamWriter (File.Create (filePath), Encoding.UTF8)) {
                textWriter.Write (craftString);
            }
        }
        public static KspToken WriteObject (KspObject obj)
        {
            var attributes = obj.Properties.Select (p => PropertyToAttribute (obj, p)).ToList ();
            var tokens = obj.Children.Select (WriteObject).ToList ();

            return obj.IsGlobalObject
                ? KspTokenGlobalExtension.CreateGlobalToken (attributes, tokens)
                : new KspToken (obj.Type, attributes, tokens);
        }
        private static IEnumerable<KspObject> FlattenObjectTree (KspObject objTree)
        {
            yield return objTree;

            foreach (var child in objTree.Children) {
                foreach (var obj in FlattenObjectTree (child)) {
                    yield return obj;
                }
            }
        }
        public static TChild Child <TChild> (this KspObject obj, int id) where TChild : KspObject
        {
            if (id < 0)
            {
                return(null);
            }

            var children = obj.Children <TChild> ().ToList();

            return((children.Count > id) ? children [id] : null);
        }
 public void CanFindPropertiesOfAnObjectByName ()
 {
     // given
     var obj = new KspObject ("OBJ")
         .AddProperty (new KspStringProperty ("property", "value1"))
         .AddProperty (new KspStringProperty ("anotherProperty", "value2"))
         .AddProperty (new KspStringProperty ("property", "value3"));
     
     // when / then
     Assert.That (obj.Properties<KspProperty> ("property"), Is.EqualTo (new[] {
         obj.Properties [0],
         obj.Properties [2]
     }));
 }
Exemplo n.º 10
0
        public void PrintsInfoHeaderIfSilentSwitchIsNotOn ()
        {
            // given
            var inputCrafts = new KspObject ("GAME");
            var inputText = KspObjToString (inputCrafts);

            // when
            File.WriteAllText (TestFilePath, inputText);
            var returnCode = Program.Main ("list-crafts", "-i", TestFilePath);

            // then
            Assert.That (StdOutput.ToString (), Is.StringStarting ("KSPPartRemover v"));
            Assert.That (returnCode, Is.EqualTo (0));
        }
 public void CanFindChildrenOfAnObjectByType ()
 {
     // given
     var obj = new KspObject ("OBJ")
         .AddChild (new KspPartObject () // found
             .AddChild (new KspPartObject ())) // not found
         .AddChild (new KspObject (KspPartObject.TypeId) // not found
             .AddChild (new KspPartObject ())) // not found
         .AddChild (new KspPartObject () // found
             .AddChild (new KspObject (KspPartObject.TypeId))); // not found
     
     // when / then
     Assert.That (obj.Children<KspPartObject> (), Is.EqualTo (new[] { obj.Children [0], obj.Children [2] }));
 }
Exemplo n.º 12
0
        private static KeyValuePair<String, String> PropertyToAttribute (KspObject obj, KspProperty property)
        {
            var value = "<KSPPR_NOT_SUPPORTED>";

            var stringProperty = property as KspStringProperty;
            if (stringProperty != null) {
                value = stringProperty.Text;
            }

            var partLinkProperty = property as KspPartLinkProperty;
            if (partLinkProperty != null) {
                value = ResolvePartLinkString (obj, partLinkProperty);
            }

            return new KeyValuePair<String, String> (property.Name, value);
        }
Exemplo n.º 13
0
        public void CanAddChildren ()
        {
            // given
            var children = new[] {
                new KspObject ("child1"),
                new KspObject ("child2"),
                new KspObject ("child3")
            };

            // when
            var obj = new KspObject ("OBJ")
                .AddChild (children [0])
                .AddChild (children [1])
                .AddChild (children [2]);

            // when / then
            Assert.That (obj.Children, Is.EqualTo (children));
        }
Exemplo n.º 14
0
        public void CanAddProperties ()
        {
            // given
            var properties = new[] {
                new KspStringProperty ("property1", "property1Text"),
                new KspStringProperty ("property2", "property2Text"),
                new KspStringProperty ("property3", "property3Text")
            };

            // when
            var obj = new KspObject ("OBJ")
                .AddProperty (properties [0])
                .AddProperty (properties [1])
                .AddProperty (properties [2]);
            
            // when / then
            Assert.That (obj.Properties, Is.EqualTo (properties));
        }
Exemplo n.º 15
0
        private static String ResolvePartLinkString (KspObject obj, KspPartLinkProperty property)
        {
            var craft = obj.Parent as KspCraftObject;

            var sb = new StringBuilder ();

            if (property.Prefix != null) {
                sb.Append (property.Prefix);
                sb.Append (", ");
            }

            if (property.IsIdReference) {
                sb.Append (craft.IdOfChild (property.Part));
            } else {
                sb.Append (property.Part.Name);
            }

            return sb.ToString ();
        }
        public void CanRetrievePartReferences ()
        {
            // given
            var obj = new KspObject ("OBJ")
                .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "part1")))
                .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "part2")))
                .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "part3")));

            var parts = obj.Children<KspPartObject> ().ToArray ();

            var part = obj.Children [0]
                .AddProperty (new KspStringProperty ("not-a-link", "not-a-link"))
                .AddProperty (new KspPartLinkProperty (KspPartLinkProperty.Types.Link, "top", parts [0], false))
                .AddProperty (new KspPartLinkProperty (KspPartLinkProperty.Types.Parent, "top", parts [0], false))
                .AddProperty (new KspPartLinkProperty (KspPartLinkProperty.Types.Parent, "top", parts [1], false)) as KspPartObject;
            
            // when / then
            Assert.That (part.PartLinks (KspPartLinkProperty.Types.Parent), Is.EqualTo (new[] { part.Properties [3], part.Properties [4] }));
        }
        public void CanFindChildById ()
        {
            // given
            var obj = new KspObject ("OBJ")
                .AddChild (new KspCraftObject ())
                .AddChild (new KspPartObject ())
                .AddChild (new KspPartObject ())
                .AddChild (new KspPartObject ())
                .AddChild (new KspCraftObject ());

            // when / then
            Assert.That (obj.Child<KspCraftObject> (-1), Is.Null);
            Assert.That (obj.Child<KspCraftObject> (0), Is.EqualTo (obj.Children [0])); // Ids are per object-type
            Assert.That (obj.Child<KspCraftObject> (1), Is.EqualTo (obj.Children [4]));
            Assert.That (obj.Child<KspCraftObject> (2), Is.Null);

            Assert.That (obj.Child<KspPartObject> (0), Is.EqualTo (obj.Children [1]));
            Assert.That (obj.Child<KspPartObject> (1), Is.EqualTo (obj.Children [2]));
            Assert.That (obj.Child<KspPartObject> (2), Is.EqualTo (obj.Children [3]));
        }
        public static IEnumerable <TChild> Children <TChild> (this KspObject obj, bool recursive = false) where TChild : KspObject
        {
            foreach (var child in obj.Children)
            {
                var match = child as TChild;
                if (match != null)
                {
                    yield return(match);
                }

                if (recursive)
                {
                    var childResults = child.Children <TChild> (recursive);
                    foreach (var childResult in childResults)
                    {
                        yield return(childResult);
                    }
                }
            }
        }
Exemplo n.º 19
0
 public KspObject AddChild(KspObject child)
 {
     child.Parent = this;
     ((List <KspObject>)Children).Add(child);
     return(this);
 }
Exemplo n.º 20
0
 public CraftLookup (KspObject kspObjTree)
 {
     this.kspObjTree = kspObjTree;
 }
Exemplo n.º 21
0
 public KspObject AddChild (KspObject child)
 {
     child.Parent = this;
     ((List<KspObject>)Children).Add (child);
     return this;
 }
Exemplo n.º 22
0
 public KspObject RemoveChild (KspObject child)
 {
     ((List<KspObject>)Children).Remove (child);
     return this;
 }
Exemplo n.º 23
0
 private String KspObjToString (KspObject obj)
 {
     var token = KspObjectWriter.WriteObject (obj);
     return KspTokenWriter.WriteToken (token, new StringBuilder ()).ToString ();
 }
Exemplo n.º 24
0
        public void CanPrintPartDependencies ()
        {
            // given
            var inputCrafts = new KspObject ("GAME")
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "someCraft"))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "fuelTank1")))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "strut"))))
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "anotherCraft"))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "strut")))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "fuelTank2"))))
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "ignored"))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "somePart"))));

            var craft1 = inputCrafts.Children [0];
            var craft2 = inputCrafts.Children [1];

            craft1.Children [0].AddProperty (new KspPartLinkProperty (KspPartLinkProperty.Types.Link, null, (KspPartObject)craft1.Children [1], true));
            craft1.Children [1].AddProperty (new KspPartLinkProperty (KspPartLinkProperty.Types.Parent, null, (KspPartObject)craft1.Children [0], false));
            craft1.Children [1].AddProperty (new KspPartLinkProperty (KspPartLinkProperty.Types.Sym, "top", (KspPartObject)craft1.Children [0], false));
            craft2.Children [0].AddProperty (new KspPartLinkProperty (KspPartLinkProperty.Types.AttN, "bottom", (KspPartObject)craft2.Children [1], false));

            var inputText = KspObjToString (inputCrafts);

            var expectedResult =
                "someCraft:" + Environment.NewLine +
                "\t[1]strut:" + Environment.NewLine +
                "\t\t[0]fuelTank1[parent]" + Environment.NewLine +
                "\t\t[0]fuelTank1[sym(top)]" + Environment.NewLine +
                "anotherCraft:" + Environment.NewLine +
                "\t[0]strut:" + Environment.NewLine +
                "\t\t[1]fuelTank2[attN(bottom)]" + Environment.NewLine;
            
            // when
            File.WriteAllText (TestFilePath, inputText);
            var returnCode = Program.Main ("list-partdeps", "-p", ".*uelTank.*", "-c", ".*Craft", "-i", TestFilePath);

            // then
            Assert.That (StdOutput.ToString (), Is.StringEnding (expectedResult));
            Assert.That (returnCode, Is.EqualTo (0));
        }
Exemplo n.º 25
0
        public void PrintsAndReturnsErrorIfNoCraftWithMatchingCraftNameIsFound ()
        {
            // given
            var inputCrafts = new KspObject ("GAME")
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "someCraft")))
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "anotherCraft")));
            
            var inputText = KspObjToString (inputCrafts);

            // when
            File.WriteAllText (TestFilePath, inputText);
            var returnCode = Program.Main ("remove-parts", "-p", "somePart", "--craft", "nonExistingCraft", "-i", TestFilePath, "-s");

            // then
            Assert.That (StdOutput.ToString (), Is.EqualTo ("ERROR: No craft matching 'nonExistingCraft' found, aborting" + Environment.NewLine));
            Assert.That (returnCode, Is.LessThan (0));
        }
Exemplo n.º 26
0
        public void CanRemovePartsOfMultipleCraftsAndOutputResult ()
        {
            // given
            var inputCrafts = new KspObject ("GAME")
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "craft1"))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "fuelTank")))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "strut")))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "fuelTank"))))
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "craft2"))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "fuelTank")))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "strut")))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "fuelTank"))))
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "craft3"))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "fuelTank")))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "strut")))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "fuelTank"))))
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "craft4")));

            var expectedCrafts = new KspObject ("GAME")
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "craft1"))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "strut"))))
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "craft2"))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "fuelTank")))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "strut")))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "fuelTank"))))
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "craft3"))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "strut"))))
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "craft4")));
            
            var inputText = KspObjToString (inputCrafts);
            var expectedResult = KspObjToString (expectedCrafts);

            // when
            File.WriteAllText (TestFilePath, inputText);
            var returnCode = Program.Main ("remove-parts", "-p", "fuelTank", "-c", "!craft2", "-i", TestFilePath, "-o", TestFilePath, "-s");

            // then
            Assert.That (File.ReadAllText (TestFilePath), Is.EqualTo (expectedResult));
            Assert.That (returnCode, Is.EqualTo (0));
        }
Exemplo n.º 27
0
        public void CanPrintPartList ()
        {
            // given
            var inputCrafts = new KspObject ("GAME")
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "someCraft"))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "fuelTank")))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "strut"))))
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "anotherCraft"))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "strut")))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "fuelTank")))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "ignored"))))
                .AddChild (new KspCraftObject ().AddProperty (new KspStringProperty ("name", "ignored"))
                    .AddChild (new KspPartObject ().AddProperty (new KspStringProperty ("name", "somePart"))));
            
            var inputText = KspObjToString (inputCrafts);

            var expectedResult =
                "someCraft:" + Environment.NewLine +
                "\t[0]fuelTank" + Environment.NewLine +
                "\t[1]strut" + Environment.NewLine +
                "anotherCraft:" + Environment.NewLine +
                "\t[0]strut" + Environment.NewLine +
                "\t[1]fuelTank" + Environment.NewLine;

            // when
            File.WriteAllText (TestFilePath, inputText);
            var returnCode = Program.Main ("list-parts", "-p", "[s,f].*", "-c", ".*Craft", "-i", TestFilePath);

            // then
            Assert.That (StdOutput.ToString (), Is.StringEnding (expectedResult));
            Assert.That (returnCode, Is.EqualTo (0));
        }
        public void CanRetrieveIdOfChild ()
        {
            // given
            var obj = new KspObject ("OBJ")
                .AddChild (new KspCraftObject ())
                .AddChild (new KspPartObject ())
                .AddChild (new KspPartObject ())
                .AddChild (new KspPartObject ())
                .AddChild (new KspCraftObject ());

            // when / then
            Assert.That (obj.IdOfChild (obj.Children [0] as KspCraftObject), Is.EqualTo (0)); // Ids are per object-type
            Assert.That (obj.IdOfChild (obj.Children [4] as KspCraftObject), Is.EqualTo (1));

            Assert.That (obj.IdOfChild (obj.Children [1] as KspPartObject), Is.EqualTo (0));
            Assert.That (obj.IdOfChild (obj.Children [2] as KspPartObject), Is.EqualTo (1));
            Assert.That (obj.IdOfChild (obj.Children [3] as KspPartObject), Is.EqualTo (2));
        }
Exemplo n.º 29
0
        private static KspPartLinkProperty ReadPartLinkProperty (KspObject obj, KeyValuePair<String, String> attribute)
        {
            var prefixValue = attribute.Value.Split (',').Select (s => s.Trim ()).ToArray ();
            var prefix = (prefixValue.Length == 2) ? prefixValue [0] : null;
            var reference = (prefixValue.Length == 2) ? prefixValue [1] : attribute.Value;

            int id;
            var isIdReference = int.TryParse (reference, out id);

            var part = isIdReference
                ? obj.Parent.Child<KspPartObject> (id)
                : obj.Parent.Children<KspPartObject> ().FirstOrDefault (p => p.Name.Equals (reference));
            
            return new KspPartLinkProperty (attribute.Key, prefix, part, isIdReference);
        }
 public static IEnumerable <TProp> Properties <TProp> (this KspObject obj, String name) where TProp : KspProperty =>
 obj.Properties.Where(property => property.Name.Equals(name)).OfType <TProp> ();
        public static int IdOfChild <TChild> (this KspObject obj, TChild child) where TChild : KspObject
        {
            var children = obj.Children <TChild> ().ToList();

            return(children.IndexOf(child));
        }
Exemplo n.º 32
0
 private static bool IsStringAttribute (KspObject obj, KeyValuePair<String, String> attribute) => !IsPartLinkAttribute (obj, attribute);
Exemplo n.º 33
0
 public KspObject RemoveChild(KspObject child)
 {
     ((List <KspObject>)Children).Remove(child);
     return(this);
 }
Exemplo n.º 34
0
        private static bool IsPartLinkAttribute (KspObject obj, KeyValuePair<String, String> attribute)
        {
            if (!(obj.Parent is KspCraftObject)) {
                return false;
            }

            return
            attribute.Key.Equals (KspPartLinkProperty.Types.Link) ||
            attribute.Key.Equals (KspPartLinkProperty.Types.Parent) ||
            attribute.Key.Equals (KspPartLinkProperty.Types.Sym) ||
            attribute.Key.Equals (KspPartLinkProperty.Types.SrfN) ||
            attribute.Key.Equals (KspPartLinkProperty.Types.AttN);
        }