public void XmlSpriteReferencesTest()
        {
            XmlProgram programm = new XmlProgram()
            {
                SpriteList = new XmlSpriteList()
                {
                    Sprites = new List <XmlSprite>(),
                },
                VariableList = new XmlVariableList()
                {
                    ObjectVariableList = new XmlObjectVariableList()
                    {
                        ObjectVariableEntries = new List <XmlObjectVariableEntry>(),
                    }
                }
            };

            programm.SpriteList.Sprites.Add(new XmlSprite());
            programm.SpriteList.Sprites.Add(new XmlSprite());

            programm.VariableList.ObjectVariableList.ObjectVariableEntries.Add(new XmlObjectVariableEntry());
            programm.VariableList.ObjectVariableList.ObjectVariableEntries.Add(new XmlObjectVariableEntry());

            XmlParserTempProjectHelper.Program = programm;


            programm.VariableList.ObjectVariableList.ObjectVariableEntries[0].Sprite = programm.SpriteList.Sprites[0];
            programm.VariableList.ObjectVariableList.ObjectVariableEntries[1].Sprite = programm.SpriteList.Sprites[1];

            var referenceObject = "<object reference=\"../../../../objectList/object[2]\" />";
            var testObject      = programm.VariableList.ObjectVariableList.ObjectVariableEntries[1].XmlSpriteReference.CreateXml().ToString();

            Assert.AreEqual(referenceObject, testObject);
        }
        public async Task <Program> CopyProgramPart2(string sourceProgramName,
                                                     string newProgramName)
        {
            using (var storage = StorageSystem.GetStorage())
            {
                var sourcePath      = Path.Combine(StorageConstants.ProgramsPath, sourceProgramName);
                var destinationPath = Path.Combine(StorageConstants.ProgramsPath, newProgramName);

                await storage.CopyDirectoryAsync(sourcePath, destinationPath);

                var tempXmlPath = Path.Combine(destinationPath, StorageConstants.ProgramCodePath);
                var xml         = await storage.ReadTextFileAsync(tempXmlPath);

                var xmlProgram = new XmlProgram(xml)
                {
                    ProgramHeader = { ProgramName = newProgramName }
                };

                var path = Path.Combine(StorageConstants.ProgramsPath,
                                        newProgramName, StorageConstants.ProgramCodePath);
                var programConverter = new ProgramConverter();
                var program          = programConverter.Convert(xmlProgram);

                var xmlString = xmlProgram.ToXmlString();
                await storage.WriteTextFileAsync(path, xmlString);

                return(program);
            }
        }
        public static void CheckProgram(XmlProgram program)
        {
            const int moleIndex = 2;


            var foreverBegin = program.SpriteList.Sprites[moleIndex].Scripts.Scripts[0].Bricks.Bricks.FirstOrDefault(
                a => a is XmlForeverBrick) as XmlForeverBrick;
            var foreverEnd = program.SpriteList.Sprites[moleIndex].Scripts.Scripts[0].Bricks.Bricks.FirstOrDefault(
                a => a is XmlForeverLoopEndBrick) as XmlForeverLoopEndBrick;

            if (foreverBegin == null)
            {
                Debugger.Break();
            }

            if (foreverEnd == null)
            {
                Debugger.Break();
            }
        }
        public void UpdateProgramHeader(XmlProgram program)
        {
            program.ProgramHeader.ApplicationBuildName = ServiceLocator.
                                                         SystemInformationService.CurrentApplicationBuildName;

            program.ProgramHeader.ApplicationBuildNumber = ServiceLocator.
                                                           SystemInformationService.CurrentApplicationBulidNumber;

            program.ProgramHeader.ApplicationName = XmlConstants.ApplicationName;

            program.ProgramHeader.ApplicationVersion = ServiceLocator.
                                                       SystemInformationService.CurrentApplicationVersion;

            program.ProgramHeader.CatrobatLanguageVersion = XmlConstants.TargetIDEVersion;

            program.ProgramHeader.DeviceName = ServiceLocator.
                                               SystemInformationService.DeviceName;

            program.ProgramHeader.Platform = ServiceLocator.
                                             SystemInformationService.PlatformName;

            program.ProgramHeader.PlatformVersion = ServiceLocator.
                                                    SystemInformationService.PlatformVersion;

            program.ProgramHeader.ScreenWidth = ServiceLocator.
                                                SystemInformationService.ScreenWidth * ServiceLocator.
                                                SystemInformationService.ScaleFactor;

            program.ProgramHeader.ScreenHeight = ServiceLocator.
                                                 SystemInformationService.ScreenHeight * ServiceLocator.
                                                 SystemInformationService.ScaleFactor;

            // TODO: check if and how the following properties should be set
            //program.ProjectHeader.DateTimeUpload = "";
            //program.ProjectHeader.ProgramLicense = "";
            //program.ProjectHeader.MediaLicense = "";
            //program.ProjectHeader.ScreenHeight = "";
            //program.ProjectHeader.ScreenWidth = "";
            //program.ProjectHeader.Tags = "";
        }
        public async Task <CheckProgramResult> CheckProgram(string pathToProgramDirectory)
        {
            var pathToProgramCodeFile = Path.Combine(pathToProgramDirectory, StorageConstants.ProgramCodePath);

            XmlProgram    convertedProgram  = null;
            var           checkResult       = new CheckProgramResult();
            PortableImage programScreenshot = null;
            string        programName       = null;
            string        programCode       = null;

            using (var storage = StorageSystem.GetStorage())
            {
                programScreenshot =
                    await storage.LoadImageAsync(Path.Combine(
                                                     pathToProgramDirectory,
                                                     StorageConstants.ProgramManualScreenshotPath)) ??
                    await storage.LoadImageAsync(Path.Combine(
                                                     pathToProgramDirectory,
                                                     StorageConstants.ProgramAutomaticScreenshotPath));

                if (!await storage.FileExistsAsync(pathToProgramCodeFile))
                {
                    checkResult.State = ProgramState.FilesMissing;
                    return(checkResult);
                }
                programCode = await storage.ReadTextFileAsync(pathToProgramCodeFile);
            }

            var converterResult = await CatrobatVersionConverter.
                                  ConvertToXmlVersion(programCode, XmlConstants.TargetIDEVersion);

            if (converterResult.Error != CatrobatVersionConverter.VersionConverterStatus.NoError)
            {
                switch (converterResult.Error)
                {
                case CatrobatVersionConverter.VersionConverterStatus.VersionTooNew:
                    checkResult.State = ProgramState.VersionTooNew;
                    break;

                case CatrobatVersionConverter.VersionConverterStatus.VersionTooOld:
                    checkResult.State = ProgramState.VersionTooOld;
                    break;

                default:
                    checkResult.State = ProgramState.Damaged;
                    break;
                }
                return(checkResult);
            }

            try
            {
                convertedProgram = new XmlProgram(converterResult.Xml);
                programName      = convertedProgram.ProgramHeader.ProgramName;
            }
            catch (Exception)
            {
                checkResult.State         = ProgramState.Damaged;
                checkResult.ProgramHeader = null;
                checkResult.Program       = null;
                return(checkResult);
            }

            try
            {
                ProgramConverter programConverter = new ProgramConverter();
                checkResult.Program = programConverter.Convert(convertedProgram);
                NativeWrapper.SetProject(convertedProgram);
            }
            catch (Exception)
            {
                checkResult.State         = ProgramState.ErrorInThisApp;
                checkResult.ProgramHeader = null;
                checkResult.Program       = null;
                return(checkResult);
            }

            if (programName == null)
            {
                programName = XmlProgramHelper.GetProgramName(converterResult.Xml);
            }

            checkResult.ProgramHeader = new LocalProgramHeader
            {
                Screenshot  = programScreenshot,
                ProjectName = programName,
            };

            checkResult.State = ProgramState.Valid;
            return(checkResult);
        }
        public async Task LoadSampleProgram()
        {
            using (var storage = StorageSystem.GetStorage())
            {
                await storage.DeleteDirectoryAsync("");
            }

            foreach (KeyValuePair <string, string> pair in _sampleProjectNames)
            {
                var projectFileName = pair.Key;
                var projectName     = pair.Value;
                var resourcePath    = string.Format("SamplePrograms/{0}", projectFileName);

                Stream resourceStream = null;

                try
                {
                    var resourceLoader = ServiceLocator.ResourceLoaderFactory.CreateResourceLoader();
                    resourceStream = await resourceLoader.OpenResourceStreamAsync(ResourceScope.Resources, resourcePath);


                    if (resourceStream != null)
                    {
                        var projectFolderPath = Path.Combine(StorageConstants.ProgramsPath, projectName);

                        using (var storage = StorageSystem.GetStorage())
                        {
                            if (!await storage.DirectoryExistsAsync(projectFolderPath))
                            {
                                await ServiceLocator.ZipService.UnzipCatrobatPackageIntoIsolatedStorage(resourceStream, StorageConstants.ProgramsPath + "/" + projectName);
                            }
                        }

                        using (var storage = StorageSystem.GetStorage())
                        {
                            var textFilePath = Path.Combine(StorageConstants.ProgramsPath, projectName, StorageConstants.ProgramCodePath);
                            var xml          = await storage.ReadTextFileAsync(textFilePath);

                            var xmlProgram = new XmlProgram(xml)
                            {
                                ProgramHeader = { ProgramName = projectName }
                            };


                            var path = Path.Combine(StorageConstants.ProgramsPath,
                                                    projectFileName, StorageConstants.ProgramCodePath);
                            var xmlString = xmlProgram.ToXmlString();
                            await storage.WriteTextFileAsync(path, xmlString);
                        }
                    }
                }
                catch (Exception)
                {
                    Debugger.Break(); // sample project does not exist: please remove from _sampleProjectNames or add to Core/Resources/SamplePrograms
                }
                finally
                {
                    if (resourceStream != null)
                    {
                        resourceStream.Dispose();
                    }
                }
            }
        }