示例#1
0
        public async Task <Stream> CreateProgramPackageForExport(string programName)
        {
            var streamResult = new MemoryStream();

            var tempPath          = Path.Combine(StorageConstants.TempProgramExportPath, programName);
            var programFolderPath = Path.Combine(StorageConstants.ProgramsPath, programName);
            var programCodePath   = Path.Combine(tempPath, StorageConstants.ProgramCodePath);

            using (var storage = StorageSystem.GetStorage())
            {
                await storage.DeleteDirectoryAsync(tempPath);

                await storage.CopyDirectoryAsync(programFolderPath, tempPath);

                var programCode = await storage.ReadTextFileAsync(programCodePath);

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

                if (converterResult.Error != CatrobatVersionConverter.VersionConverterStatus.NoError)
                {
                    return(null);
                }
            }

            var zipService = new ZipService();
            await zipService.ZipCatrobatPackage(streamResult, tempPath);

            streamResult.Seek(0, SeekOrigin.Begin);
            return(streamResult);
        }
        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);
        }