Пример #1
0
        private static int ReadToken (String[] lines, int index, out KspToken token)
        {
            if ((index + 1) >= lines.Length) {
                token = null;
                return index;
            }

            var name = lines [index].Trim ();
            var isGlobalToken = name.Contains ("=");
            var attributes = new List<KeyValuePair<String, String>> ();
            var tokens = new List<KspToken> ();

            if (!isGlobalToken) {
                index++;
                if (lines [index++] != "{") {
                    throw new FormatException ();
                }
            }

            index = ReadAttributes (lines, index, attributes.Add);
            index = ReadTokens (lines, index, tokens.Add);

            if (!isGlobalToken) {
                if (index >= lines.Length || lines [index++] != "}") {
                    throw new FormatException ();
                }
                token = new KspToken (name, attributes, tokens);
            } else {
                token = KspTokenGlobalExtension.CreateGlobalToken (attributes, tokens);
            }

            return index;
        }
Пример #2
0
        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);
        }
Пример #3
0
        private static void WriteToken(KspToken token, StringBuilder sb, int lvl)
        {
            if (String.IsNullOrEmpty(token.Name))
            {
                return;
            }

            if (!token.IsGlobalToken())
            {
                WriteLine(token.Name, sb, lvl);
                WriteLine("{", sb, lvl);
            }

            var contentLevel = (token.IsGlobalToken() ? lvl : lvl + 1);

            foreach (var a in token.Attributes)
            {
                WriteAttribute(a, sb, contentLevel);
            }

            foreach (var t in token.Tokens)
            {
                WriteToken(t, sb, contentLevel);
            }

            if (!token.IsGlobalToken())
            {
                WriteLine("}", sb, lvl);
            }
        }
        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;
        }
Пример #5
0
        private static void WriteToken (KspToken token, StringBuilder sb, int lvl)
        {
            if (String.IsNullOrEmpty (token.Name)) {
                return;
            }

            if (!token.IsGlobalToken ()) {
                WriteLine (token.Name, sb, lvl);
                WriteLine ("{", sb, lvl);
            }

            var contentLevel = (token.IsGlobalToken () ? lvl : lvl + 1);

            foreach (var a in token.Attributes) {
                WriteAttribute (a, sb, contentLevel);
            }

            foreach (var t in token.Tokens) {
                WriteToken (t, sb, contentLevel);
            }

            if (!token.IsGlobalToken ()) {
                WriteLine ("}", sb, lvl);
            }
        }
        public static KspObject ReadObject (KspToken token)
        {
            var tokenMapping = new Dictionary<KspObject, KspToken> ();

            var objectTree = ReadObjectTree (token, tokenMapping.Add);

            var allObjects = FlattenObjectTree (objectTree).ToArray ();

            foreach (var obj in allObjects) {
                ReadStringProperties (obj, tokenMapping [obj].Attributes.ToList ());
            }

            foreach (var obj in allObjects) {
                ReadPartLinkProperties (obj, tokenMapping [obj].Attributes.ToList ());
            }

            return objectTree;
        }
Пример #7
0
        public static KspObject ReadObject(KspToken token)
        {
            var tokenMapping = new Dictionary <KspObject, KspToken> ();

            var objectTree = ReadObjectTree(token, tokenMapping.Add);

            var allObjects = FlattenObjectTree(objectTree).ToArray();

            foreach (var obj in allObjects)
            {
                ReadStringProperties(obj, tokenMapping [obj].Attributes.ToList());
            }

            foreach (var obj in allObjects)
            {
                ReadPartLinkProperties(obj, tokenMapping [obj].Attributes.ToList());
            }

            return(objectTree);
        }
Пример #8
0
        private static int ReadToken(String[] lines, int index, out KspToken token)
        {
            if ((index + 1) >= lines.Length)
            {
                token = null;
                return(index);
            }

            var name          = lines [index].Trim();
            var isGlobalToken = name.Contains("=");
            var attributes    = new List <KeyValuePair <String, String> > ();
            var tokens        = new List <KspToken> ();

            if (!isGlobalToken)
            {
                index++;
                if (lines [index++] != "{")
                {
                    throw new FormatException();
                }
            }

            index = ReadAttributes(lines, index, attributes.Add);
            index = ReadTokens(lines, index, tokens.Add);

            if (!isGlobalToken)
            {
                if (index >= lines.Length || lines [index++] != "}")
                {
                    throw new FormatException();
                }
                token = new KspToken(name, attributes, tokens);
            }
            else
            {
                token = KspTokenGlobalExtension.CreateGlobalToken(attributes, tokens);
            }

            return(index);
        }
Пример #9
0
 public static StringBuilder WriteToken(KspToken token, StringBuilder sb)
 {
     WriteToken(token, sb, 0);
     return(sb);
 }
Пример #10
0
 public static bool IsGlobalToken(this KspToken token)
 {
     return(Object.Equals(token.Name, GlobalTokenName));
 }
Пример #11
0
 public static StringBuilder WriteToken (KspToken token, StringBuilder sb)
 {
     WriteToken (token, sb, 0);
     return sb;
 }