private static XmlObjectNode GetUserVariableObject(XmlUserVariableReference xmlUserVariableReference, string reference)
        {
            XElement currentElement = xmlUserVariableReference._xRoot;

            string[] array = reference.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            var matchQuery = from word in array
                             where word.ToLowerInvariant() == ".."
                             select word;

            int stepsDown = matchQuery.Count();

            for (int i = 0; i < stepsDown; i++)
            {
                currentElement = currentElement.Parent;
            }

            for (int i = stepsDown; i < array.Length; i++)
            {
                //TODO: throws exception
                string type   = array[i].Split('[')[0];
                int    number = GetElementNumber(array[i]);

                currentElement = currentElement.Elements(type).ElementAt(number);
            }

            return(new XmlUserVariable(currentElement));
        }
        private static string GetVariableReferenceString(XmlUserVariableReference xmlUserVariableReference)
        {
            XmlUserVariable var = xmlUserVariableReference.UserVariable;

            if (XmlParserTempProjectHelper.inObjectVarList)
            {
                return(GetStepDownString(5) + XmlConstants.ObjectList + "/"
                       + XmlConstants.Object + GetReferenceNumeration(var.ObjectNum) + "/"
                       + XmlConstants.ScriptList + "/"
                       + XmlConstants.Script + GetReferenceNumeration(var.ScriptNum) + "/"
                       + XmlConstants.BrickList + "/"
                       + XmlConstants.Brick + GetReferenceNumeration(var.BrickNum) + "/"
                       + XmlConstants.UserVariable + GetReferenceNumeration(var.VariableNum));
            }

            else if (XmlParserTempProjectHelper.inProgramVarList)
            {
                return(GetStepDownString(3) + XmlConstants.ObjectList + "/"
                       + XmlConstants.Object + GetReferenceNumeration(var.ObjectNum) + "/"
                       + XmlConstants.ScriptList + "/"
                       + XmlConstants.Script + GetReferenceNumeration(var.ScriptNum) + "/"
                       + XmlConstants.BrickList + "/"
                       + XmlConstants.Brick + GetReferenceNumeration(var.BrickNum) + "/"
                       + XmlConstants.UserVariable + GetReferenceNumeration(var.VariableNum));
            }

            else if (xmlUserVariableReference.UserVariable.ObjectNum != XmlParserTempProjectHelper.currentObjectNum)
            {
                return(GetStepDownString(6) + XmlConstants.Object + GetReferenceNumeration(var.ObjectNum) + "/"
                       + XmlConstants.ScriptList + "/"
                       + XmlConstants.Script + GetReferenceNumeration(var.ScriptNum) + "/"
                       + XmlConstants.BrickList + "/" + XmlConstants.Brick + GetReferenceNumeration(var.BrickNum) + "/"
                       + XmlConstants.UserVariable + GetReferenceNumeration(var.VariableNum));
            }

            else if (xmlUserVariableReference.UserVariable.ScriptNum != XmlParserTempProjectHelper.currentScriptNum)
            {
                return(GetStepDownString(4) + XmlConstants.Script + GetReferenceNumeration(var.ScriptNum) + "/"
                       + XmlConstants.Brick + GetReferenceNumeration(var.BrickNum) + "/"
                       + XmlConstants.UserVariable + GetReferenceNumeration(var.VariableNum));
            }

            else if (xmlUserVariableReference.UserVariable.BrickNum != XmlParserTempProjectHelper.currentBrickNum)
            {
                return(GetStepDownString(2) + XmlConstants.Brick + GetReferenceNumeration(var.BrickNum) + "/"
                       + XmlConstants.UserVariable + GetReferenceNumeration(var.VariableNum));
            }

            else if (xmlUserVariableReference.UserVariable.VariableNum != XmlParserTempProjectHelper.currentVariableNum)
            {
                return(GetStepDownString(1) + XmlConstants.UserVariable + GetReferenceNumeration(var.VariableNum));
            }
            else
            {
                return("the ReferenceHelper is not working properly!");
            }
        }
Esempio n. 3
0
        public override XmlProgram Convert(Program m, XmlModelConvertBackContext c)
        {
            var spriteConverter         = new SpriteConverter();
            var lookConverter           = new LookConverter();
            var soundConverter          = new SoundConverter();
            var localVariableConverter  = new VariableConverter <LocalVariable>();
            var globalVariableConverter = new VariableConverter <GlobalVariable>();
            var uploadHeaderConverter   = new UploadHeaderConverter();

            var localVariables = m.Sprites.ToReadOnlyDictionary(
                keySelector: sprite => sprite,
                elementSelector: sprite => sprite.LocalVariables.ToReadOnlyDictionary(
                    keySelector: variable => variable,
                    elementSelector: variable => (XmlUserVariable)localVariableConverter.Convert(variable, c)));
            var globalVariables = m.GlobalVariables.ToReadOnlyDictionary(
                keySelector: variable => variable,
                elementSelector: variable => (XmlUserVariable)globalVariableConverter.Convert(variable, c));
            var contextBase = new XmlModelConvertBackContextBase(m, globalVariables);
            var sprites     = m.Sprites.ToReadOnlyDictionary(
                keySelector: sprite => sprite,
                elementSelector: sprite =>
            {
                ReadOnlyDictionary <LocalVariable, XmlUserVariable> localVariables2;
                if (!localVariables.TryGetValue(sprite, out localVariables2))
                {
                    localVariables2 = new ReadOnlyDictionary <LocalVariable, XmlUserVariable>(new Dictionary <LocalVariable, XmlUserVariable>());
                }
                return((XmlSprite)spriteConverter.Convert(sprite, new XmlModelConvertBackContext(contextBase, sprite,
                                                                                                 sprite.Looks == null
                            ? null
                            : sprite.Looks.ToReadOnlyDictionary(
                                                                                                     keySelector: look => look,
                                                                                                     elementSelector: look => (XmlLook)lookConverter.Convert(look, c)),
                                                                                                 sprite.Sounds == null
                            ? null
                            : sprite.Sounds.ToReadOnlyDictionary(
                                                                                                     keySelector: sound => sound,
                                                                                                     elementSelector: sound => (XmlSound)soundConverter.Convert(sound, c)),
                                                                                                 localVariables2)));
            });
            var header = (XmlProjectHeader)uploadHeaderConverter.Convert(m.UploadHeader, c);

            header.ProgramName = m.Name;
            header.Description = m.Description;

            List <XmlUserVariableReference> UserVariableReferencesBuffer = new List <XmlUserVariableReference>();

            if (m.GlobalVariables != null)
            {
                List <XmlUserVariable> UserVariables = m.GlobalVariables.Select(variable => globalVariables[variable]).ToList();
                foreach (var userVariable in UserVariables)
                {
                    XmlUserVariableReference userVariableReference = new XmlUserVariableReference();
                    userVariableReference.UserVariable = userVariable;
                    userVariableReference.LoadReference();
                    UserVariableReferencesBuffer.Add(userVariableReference);
                }
            }
            var result = new XmlProgram
            {
                ProgramHeader = header,
                VariableList  = new XmlVariableList
                {
                    ProgramVariableList = new XmlProgramVariableList
                    {
                        UserVariableReferences = UserVariableReferencesBuffer
                    },
                    ObjectVariableList = new XmlObjectVariableList
                    {
                        ObjectVariableEntries = m.Sprites.Select(sprite => new XmlObjectVariableEntry
                        {
                            Sprite       = sprites[sprite],
                            VariableList = new XmlUserVariableList
                            {
                                UserVariables = sprite.LocalVariables == null
                                    ? new List <XmlUserVariable>()
                                    : sprite.LocalVariables.Select(variable => localVariables[sprite][variable]).ToList()
                            }
                        }).ToList()
                    }
                },
                SpriteList = new XmlSpriteList
                {
                    Sprites = m.Sprites.Select(sprite => sprites[sprite]).ToList()
                }
            };

            ServiceLocator.ContextService.UpdateProgramHeader(result);

            return(result);
        }