Exemple #1
0
        public async Task DoesntPushContentToQueueWhenOnlyOneSideReceived()
        {
            var content1 = new SourceContent
            {
                SourceSide = SourceSide.Left,
                Data       = new byte[] { 1, 2, 3 }
            };
            var content2 = new SourceContent
            {
                SourceSide = SourceSide.Left,
                Data       = new byte[] { 4, 5, 6 }
            };

            var queue   = Substitute.For <IDifferenceQueue>();
            var storage = Substitute.For <IStorage>();

            queue.SourceContents.Returns(
                Observable.Return(content1.InDiffBag(5))
                .Concat(Observable.Return(content2.InDiffBag(5))));

            var consumer = new SourceConsumer(queue, storage);
            await consumer.RunAsync(Scheduler.Immediate, CancellationToken.None);

            await queue.DidNotReceiveWithAnyArgs().PushReadyAsync(null);
        }
Exemple #2
0
        public async Task DoesntSaveOtherSideInStorageWhenBothSidesReceived()
        {
            var content1 = new SourceContent
            {
                SourceSide = SourceSide.Left,
                Data       = new byte[] { 1, 2, 3 }
            };
            var content2 = new SourceContent
            {
                SourceSide = SourceSide.Right,
                Data       = new byte[] { 4, 5, 6 }
            };
            var ready = new[] { content1, content2 }.AsEnumerable();

            var queue   = Substitute.For <IDifferenceQueue>();
            var storage = Substitute.For <IStorage>();

            storage.LoadSourceAsync(5, content2.SourceSide)
            .Returns(Task.FromResult((content2, true)));
            queue.SourceContents
            .Returns(Observable.Return(content1.InDiffBag(5)));

            var consumer = new SourceConsumer(queue, storage);
            await consumer.RunAsync(Scheduler.Immediate, CancellationToken.None);

            await storage.DidNotReceiveWithAnyArgs().SaveSourceAsync(null);
        }
Exemple #3
0
        private async Task <IActionResult> ReceiveSourceContent(
            int id, SourceSide side, SourceContentRequest model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var content = new SourceContent {
                SourceSide = side
            };

            try
            {
                content.Data = Convert.FromBase64String(model.Data);
            }
            catch (FormatException)
            {
                return(BadRequest());
            }

            await _differenceService.AddSourceAsync(id, content);

            return(Ok());
        }
Exemple #4
0
        public async Task PushesContentToQueueWhenBothSidesReceived()
        {
            var content1 = new SourceContent
            {
                SourceSide = SourceSide.Left,
                Data       = new byte[] { 1, 2, 3 }
            };
            var content2 = new SourceContent
            {
                SourceSide = SourceSide.Right,
                Data       = new byte[] { 4, 5, 6 }
            };
            var ready = new[] { content1, content2 }.AsEnumerable();

            var queue   = Substitute.For <IDifferenceQueue>();
            var storage = Substitute.For <IStorage>();

            storage.LoadSourceAsync(5, content2.SourceSide)
            .Returns(Task.FromResult((content2, true)));
            queue.SourceContents
            .Returns(Observable.Return(content1.InDiffBag(5)));

            var consumer = new SourceConsumer(queue, storage);
            await consumer.RunAsync(Scheduler.Immediate, CancellationToken.None);

            await queue.Received().PushReadyAsync(Arg.Is <DiffBag <IEnumerable <SourceContent> > >(
                                                      pushed => pushed.DiffId == 5 && pushed.Data.SequenceEqual(ready)));
        }
        public async Task PushesSourceContentToQueue()
        {
            var content = new SourceContent
            {
                Data       = new byte[] { 1, 2, 3 },
                SourceSide = SourceSide.Left
            };

            var queue   = Substitute.For <IDifferenceQueue>();
            var storage = Substitute.For <IStorage>();

            var service = new DifferenceService(queue, Enumerable.Empty <IQueueConsumer>(), storage);
            await service.AddSourceAsync(10, content);

            await queue.Received().PushSourceAsync(content.InDiffBag(10));
        }
        public async Task PushesSourceContentToSourceContentsObservable()
        {
            var content = new SourceContent
            {
                SourceSide = SourceSide.Right,
                Data       = new byte[] { 1, 2, 3 }
            };

            DiffBag <SourceContent> received = null;

            var queue = new DifferenceQueue();

            queue.SourceContents.Subscribe(c => received = c);
            await queue.PushSourceAsync(content.InDiffBag(10));

            received.ShouldBeEquivalentTo(content.InDiffBag(10));
        }
Exemple #7
0
        public async Task SavesFirstSourceInStorage()
        {
            var content = new SourceContent
            {
                SourceSide = SourceSide.Left,
                Data       = new byte[] { 1, 2, 3 }
            };

            var queue   = Substitute.For <IDifferenceQueue>();
            var storage = Substitute.For <IStorage>();

            queue.SourceContents.Returns(Observable.Return(content.InDiffBag(5)));

            var consumer = new SourceConsumer(queue, storage);
            await consumer.RunAsync(Scheduler.Immediate, CancellationToken.None);

            await storage.Received().SaveSourceAsync(content.InDiffBag(5));
        }
Exemple #8
0
 public Workbook(string path, SourceContent source)
 { 
     if (source == SourceContent.Local)
     {
         File.SetAttributes(path, FileAttributes.Normal);
         this.FileStream = File.Open(path, FileMode.Open);
     }
     else
     {
         WebRequest req = WebRequest.Create(path);
         req.UseDefaultCredentials = true;
         req.PreAuthenticate = true;
         req.Credentials = CredentialCache.DefaultCredentials;
         
         WebResponse result = req.GetResponse();
         this.FileStream = result.GetResponseStream();
     } 
 }
Exemple #9
0
        public async Task SavesSourceToDataDir(int diffId, SourceSide side)
        {
            var content = new SourceContent
            {
                Data       = new byte[] { 1, 2, 3 },
                SourceSide = side
            };
            var expectedFileName = Path.Combine(_DataDir, string.Concat(
                                                    diffId.ToString(), ".", side.ToString().ToLowerInvariant()));

            File.Delete(expectedFileName);

            var storage = new DiskStorage(_Options);
            await storage.SaveSourceAsync(content.InDiffBag(diffId));

            File.ReadAllBytes(expectedFileName)
            .Should().Equal(new byte[] { 1, 2, 3 });
        }
Exemple #10
0
        public Workbook(string path, SourceContent source)
        {
            if (source == SourceContent.Local)
            {
                File.SetAttributes(path, FileAttributes.Normal);
                this.FileStream = File.Open(path, FileMode.Open);
            }
            else
            {
                WebRequest req = WebRequest.Create(path);
                req.UseDefaultCredentials = true;
                req.PreAuthenticate       = true;
                req.Credentials           = CredentialCache.DefaultCredentials;

                WebResponse result = req.GetResponse();
                this.FileStream = result.GetResponseStream();
            }
        }
Exemple #11
0
        /// <summary>
        /// Loads content of source by diff identifier and side.
        /// </summary>
        /// <param name="diffId">Identifier of diff which the source belongs to.</param>
        /// <param name="side">The side a content had come from.</param>
        /// <returns>The task that loads content of the source.</returns>
        public async Task <(SourceContent, bool)> LoadSourceAsync(int diffId, SourceSide side)
        {
            byte[] bytes;

            var fileName = GetSourceFileName(diffId, side);

            try
            {
                bytes = await File.ReadAllBytesAsync(fileName);
            }
            catch (FileNotFoundException)
            {
                return(null, false);
            }

            var content = new SourceContent
            {
                SourceSide = side,
                Data       = bytes
            };

            return(content, true);
        }
Exemple #12
0
        private static TextureAtlasContent ImportTMX(string filename, ContentImporterContext context)
        {
            TextureAtlasContent output = new TextureAtlasContent();

            output.Identity = new ContentIdentity(filename);

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(filename);

            var map         = xmlDoc.DocumentElement;
            var orientation = GetAttribute(map, "orientation");

            if (orientation != "orthogonal")
            {
                throw new InvalidContentException("Invalid orientation. Only 'orthogonal' is supported for atlases.");
            }
            var renderorder = GetAttribute(map, "renderorder");
            var mapColumns  = GetAttributeAsInt(map, "width").Value;
            var mapRows     = GetAttributeAsInt(map, "height").Value;
            var tileWidth   = GetAttributeAsInt(map, "tilewidth").Value;
            var tileHeight  = GetAttributeAsInt(map, "tileheight").Value;

            output.Width  = mapColumns * tileWidth;
            output.Height = mapRows * tileHeight;

            XmlNode tileset = map["tileset"];

            if (tileset.Attributes["source"] != null)
            {
                throw new InvalidContentException("External Tileset is not supported.");
            }
            if (tileset["tileoffset"] != null)
            {
                throw new InvalidContentException("tileoffset is not supported.");
            }
            var firstgid = GetAttributeAsInt(tileset, "firstgid").Value;

            Dictionary <int, SourceContent> images = new Dictionary <int, SourceContent>();
            TextureImporter txImporter             = new TextureImporter();

            foreach (XmlNode tileNode in tileset.ChildNodes)
            {
                if (tileNode.Name != "tile")
                {
                    continue;
                }
                var     tileId    = GetAttributeAsInt(tileNode, "id").Value;
                XmlNode imageNode = tileNode["image"];

                var source = new SourceContent();

                //var format = GetAttribute(imageNode, "format");
                var imageSource     = GetAttribute(imageNode, "source");
                var fullImageSource = Path.Combine(Path.GetDirectoryName(filename), imageSource);
                var textureContent  = (Texture2DContent)txImporter.Import(fullImageSource, context);
                textureContent.Name = Path.GetFileNameWithoutExtension(fullImageSource);

                source.Texture = textureContent;

                var width  = GetAttributeAsInt(imageNode, "width");
                var height = GetAttributeAsInt(imageNode, "height");

                source.Width  = width ?? textureContent.Mipmaps[0].Width;
                source.Height = height ?? textureContent.Mipmaps[0].Height;

                var transKeyColor = GetAttributeAsColor(imageNode, "trans");
                if (transKeyColor != null)
                {
                    foreach (var mips in textureContent.Faces)
                    {
                        foreach (var mip in mips)
                        {
                            ((PixelBitmapContent <Color>)mip).ReplaceColor(transKeyColor.Value, Color.Transparent);
                        }
                    }
                }

                images.Add(firstgid + tileId, source);
            }

            output.Textures.AddRange(images.Values);

            XmlNode layerNode    = map["layer"];
            var     layerColumns = Convert.ToInt32(map.Attributes["width"].Value, CultureInfo.InvariantCulture);
            var     layerRows    = Convert.ToInt32(map.Attributes["height"].Value, CultureInfo.InvariantCulture);

            XmlNode layerDataNode = layerNode["data"];
            var     encoding      = layerDataNode.Attributes["encoding"].Value;

            if (encoding != "csv")
            {
                throw new InvalidContentException("Invalid encoding. Only 'csv' is supported for data.");
            }
            var data     = layerDataNode.InnerText;
            var dataList = data.Split(',');

            for (int i = 0; i < dataList.Length; i++)
            {
                dataList[i] = dataList[i].Trim();
            }

            for (int y = 0; y < layerRows; y++)
            {
                for (int x = 0; x < layerColumns; x++)
                {
                    var posX = x * tileWidth;
                    var posY = y * tileHeight;

                    var           tilegId = Convert.ToInt32(dataList[y * layerColumns + x], CultureInfo.InvariantCulture);
                    SourceContent image;
                    if (!images.TryGetValue(tilegId, out image))
                    {
                        continue;
                    }

                    if (renderorder == "right-down" || renderorder == "right-up")
                    {
                        posX += (tileWidth - image.Width);
                    }
                    if (renderorder == "right-down" || renderorder == "left-down")
                    {
                        posY += (tileHeight - image.Height);
                    }

                    var sprite = new SpriteContent();
                    sprite.Name    = image.Texture.Name;
                    sprite.Texture = image.Texture;
                    sprite.DestinationRectangle = new Rectangle(posX, posY, image.Width, image.Height);

                    output.Sprites.Add(sprite);
                }
            }

            return(output);
        }
 public OpenXmlWorkbook(string path, SourceContent source)
     : base(path, source)
 {
     this.filePath = path;
 } 
Exemple #14
0
 public OpenXmlWorkbook(string path, SourceContent source)
     : base(path, source)
 {
     this.filePath = path;
 }
Exemple #15
0
 public byte[] GetBody()
 => SourceContent.GetBody();
 /// <summary>
 /// Adds the content of the source.
 /// </summary>
 /// <param name="diffId">Identifier of diff on which the source can be
 /// retrieved in a future.</param>
 /// <param name="content">Source's content to add.</param>
 /// <returns>The task that adds the content.</returns>
 public Task AddSourceAsync(int diffId, SourceContent content) =>
 _queue.PushSourceAsync(content.InDiffBag(diffId));