示例#1
0
 public IdenticonGenerator(string algorithm, Size size, Color defaultBackgroundColor, Size defaultBlocks, Encoding encoding, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator)
 {
     this.DefaultAlgorithm = algorithm;
     this.DefaultSize = size;
     this.DefaultBackgroundColor = defaultBackgroundColor;
     this.DefaultBlocks = defaultBlocks;
     this.DefaultEncoding = encoding;
     this.DefaultBlockGenerators = blockGenerators;
     this.DefaultBrushGenerator = brushGenerator;
 }
        public void Parse <TPackage>(IBlockGenerator <TBlock, TPackage> inGenerator, BlockMetaCache inCache = null)
            where TPackage : ScriptableDataBlockPackage <TBlock>
        {
            if (m_Parsed)
            {
                return;
            }

            TPackage self = (TPackage)this;

            BlockParser.Parse(ref self, name, Source(), BlockParsingRules.Default, inGenerator, inCache);
        }
        public IEnumerator ParseAsync <TPackage>(IBlockParsingRules inRules, IBlockGenerator <TBlock, TPackage> inGenerator, BlockMetaCache inCache = null)
            where TPackage : ScriptableDataBlockPackage <TBlock>
        {
            if (m_Parsed)
            {
                return(null);
            }

            TPackage self = (TPackage)this;

            return(BlockParser.ParseAsync(ref self, name, Source(), inRules, inGenerator, inCache));
        }
示例#4
0
            public void Dispose()
            {
                if (Builder != null)
                {
                    BlockParser.ReturnStringBuilder(Builder);
                    Builder = null;
                }

                if (ContentBuilder != null)
                {
                    BlockParser.ReturnStringBuilder(ContentBuilder);
                    ContentBuilder = null;
                }

                PrefixPriorities = null;
                Rules            = null;
                Generator        = null;
                TagDelimiters    = null;
                Cache            = null;

                Package      = null;
                CurrentBlock = null;
            }
示例#5
0
 public Bitmap Create(byte[] value, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator)
 {
     return this.Create(value, size, backgroundcolor, blocks, blockGenerators, brushGenerator, this.DefaultAlgorithm);
 }
示例#6
0
        public Bitmap Create(byte[] value, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator, string algorithm)
        {
            var ha = HashAlgorithm.Create(algorithm);
            if (ha == null)
                throw new ArgumentOutOfRangeException(string.Format("Unknown algorithm '{0}'", algorithm));

            if (blocks.Width < 1)
                throw new ArgumentOutOfRangeException("blockshorizontal");
            if (blocks.Height < 1)
                throw new ArgumentOutOfRangeException("blocksvertical");

            if (size.Width <= 0)
                throw new ArgumentOutOfRangeException("width");
            if (size.Height <= 0)
                throw new ArgumentOutOfRangeException("height");

            if (blockGenerators.Length == 0)
                throw new ArgumentException("blockgenerators");

            if (blockGenerators.Any(b => b == null))
                throw new ArgumentNullException("blockgenerators");

            size.Width -= size.Width % blocks.Width;
            size.Height -= size.Height % blocks.Height;

            if (size.Width <= 0)
                throw new ArgumentOutOfRangeException("width after rounding to nearest value");
            if (size.Height <= 0)
                throw new ArgumentOutOfRangeException("height after rounding to nearest value");

            bool hasunevencols = blocks.Width % 2 != 0;
            var allblockgens = blockGenerators.ToArray();
            var symblockgens = blockGenerators.Where(sbg => sbg.IsSymmetric).ToArray();

            if (hasunevencols && symblockgens.Length==0)
                throw new Exception("At least one symmetrical blockgenerator required for identicons with uneven number of horizontal blocks");

            ha.Initialize();
            var hash = ha.ComputeHash(value);
            var blockwidth = (int)Math.Ceiling((double)size.Width / blocks.Width);
            var blockheight = (int)Math.Ceiling((double)size.Height / blocks.Height);

            var result = new Bitmap(size.Width, size.Height);
            using (var bgbrush = new SolidBrush(backgroundcolor))
            using (var gfx = Graphics.FromImage(result))
            {
                gfx.FillRectangle(bgbrush, 0, 0, size.Width, size.Height);

                var dhash = hash.Concat(hash).ToArray();
                int hashlen = hash.Length;
                int i = 0;
                int halfwidth = blocks.Width / 2;
                for (var x = 0; x < (hasunevencols ? halfwidth+1 : halfwidth); x++)
                {
                    for (var y = 0; y < blocks.Height; y++, i++)
                    {
                        var blockgen = GetBlockGenerator((x == halfwidth && hasunevencols) ? symblockgens : allblockgens, hash[i % hashlen]);
                        var seed = BitConverter.ToUInt32(dhash, i % hashlen);

                        using (var fgbrush = brushGenerator.GetBrush(seed))
                        {
                            Rectangle rl = new Rectangle(x * blockwidth, y * blockheight, blockwidth, blockheight);
                            blockgen.Draw(gfx, rl, bgbrush, fgbrush, seed, false);

                            if ((x != halfwidth) || ((x==halfwidth) && !hasunevencols))
                            {
                                Rectangle rr = new Rectangle((size.Width - blockwidth) - (x * blockwidth), y * blockheight, blockwidth, blockheight);
                                blockgen.Draw(gfx, rr, bgbrush, fgbrush, seed, true);
                            }
                        }
                    }
                }
            }
            return result;
        }
示例#7
0
 public Bitmap Create(IPAddress ipaddress, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator)
 {
     return this.Create(ipaddress, size, backgroundcolor, blocks, blockGenerators, brushGenerator, this.DefaultAlgorithm);
 }
示例#8
0
        public Bitmap Create(IPAddress ipaddress, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator, string algorithm)
        {
            if (ipaddress == null)
                throw new ArgumentNullException("ipaddress");

            return this.Create(ipaddress.GetAddressBytes(), size, backgroundcolor, blocks, blockGenerators, brushGenerator, algorithm);
        }
示例#9
0
 public Bitmap Create(string value, Size size, Color backgroundcolor, Size blocks, Encoding encoding, IBlockGenerator[] blockGenerators)
 {
     return this.Create(value, size, backgroundcolor, blocks, encoding, blockGenerators, this.DefaultBrushGenerator);
 }
示例#10
0
        public Bitmap Create(string value, Size size, Color backgroundcolor, Size blocks, Encoding encoding, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator, string algorithm)
        {
            if (this.DefaultEncoding == null)
                throw new ArgumentNullException("encoding");

            return this.Create(encoding.GetBytes(value ?? string.Empty), size, backgroundcolor, blocks, blockGenerators, brushGenerator);
        }
示例#11
0
 public void InjectDependency(IBlockGenerator dependency)
 {
     _blockGenerator = dependency;
 }
示例#12
0
        /// <summary>
        /// Parses the given file contents into blocks.
        /// </summary>
        static public TPackage Parse <TBlock, TPackage>(string inFileName, StringSlice inFile, IBlockParsingRules inRules, IBlockGenerator <TBlock, TPackage> inGenerator, BlockMetaCache inCache = null)
            where TBlock : class, IDataBlock
            where TPackage : class, IDataBlockPackage <TBlock>
        {
            IEnumerator parser;
            TPackage    package = ParseAsync(inFileName, inFile, inRules, inGenerator, inCache, out parser);

            if (parser != null)
            {
                while (parser.MoveNext())
                {
                    ;
                }
            }

            return(package);
        }
示例#13
0
        static internal IEnumerator ParseFile(string inFileName, StringSlice inFile, TPackage ioPackage, IBlockParsingRules inRules, IBlockGenerator <TBlock, TPackage> inGenerator, BlockMetaCache inCache)
        {
            var state = new ParseState();

            state.PrefixPriorities = GeneratePrefixPriority(inRules);
            state.Rules            = inRules;
            state.Generator        = inGenerator;
            state.TagDelimiters    = new BlockParser.BlockTagDelimiters(inRules);
            state.Package          = ioPackage;
            state.Builder          = BlockParser.RentStringBuilder();
            state.ContentBuilder   = BlockParser.RentStringBuilder();
            state.Cache            = inCache ?? BlockMetaCache.Default;

            using (var disposeRef = state)
            {
                uint lineNumber = 0;
                state.Position = new BlockFilePosition(inFileName, lineNumber);
                inGenerator.OnStart(state, state.Package);

                foreach (var rawLine in SplitIntoLines(inRules, inFile))
                {
                    lineNumber++;
                    state.Position = new BlockFilePosition(inFileName, lineNumber);

                    LineResult result = ParseLine(ref state, rawLine);
                    if (result == LineResult.Exception)
                    {
                        inGenerator.OnEnd(state, state.Package, true);
                        yield break;
                    }

                    yield return(null);
                }

                if (state.CurrentBlock != null)
                {
                    state.Error |= !TryEndBlock(ref state, StringSlice.Empty);
                }

                inGenerator.OnEnd(state, state.Package, state.Error);
            }
        }
示例#14
0
 public IdenticonGenerator(string algorithm, Size size, Color defaultBackgroundColor, Size defaultBlocks, Encoding encoding, IBlockGenerator[] blockGenerators)
     : this(algorithm, size, defaultBackgroundColor, defaultBlocks, encoding, blockGenerators, IdenticonGenerator.DefaultBrushGeneratorConfig)
 {
 }
示例#15
0
        /// <summary>
        /// Parses the given file contents into blocks asynchronously.
        /// Each MoveNext() call on the returned IEnumerator will parse one line.
        /// </summary>
        static public TPackage ParseAsync <TBlock, TPackage>(string inFileName, StringSlice inFile, IBlockParsingRules inRules, IBlockGenerator <TBlock, TPackage> inGenerator, BlockMetaCache inCache, out IEnumerator outLoader)
            where TBlock : class, IDataBlock
            where TPackage : class, IDataBlockPackage <TBlock>
        {
            if (inFile.IsEmpty)
            {
                outLoader = null;
                return(null);
            }

            if (inRules == null)
            {
                throw new ArgumentNullException("inRules");
            }
            if (inGenerator == null)
            {
                throw new ArgumentNullException("inGenerator");
            }

            string   fileName = string.IsNullOrEmpty(inFileName) ? NullFilename : inFileName;
            TPackage package  = inGenerator.CreatePackage(fileName);

            outLoader = InternalBlockParser <TBlock, TPackage> .ParseFile(fileName, inFile, package, inRules, inGenerator, inCache);

            return(package);
        }
示例#16
0
 /// <summary>
 /// Parses the given file contents into blocks asynchronously.
 /// Each MoveNext() call on the returned IEnumerator will parse one line.
 /// </summary>
 static public TPackage ParseAsync <TBlock, TPackage>(string inFileName, StringSlice inFile, IBlockParsingRules inRules, IBlockGenerator <TBlock, TPackage> inGenerator, out IEnumerator outLoader)
     where TBlock : class, IDataBlock
     where TPackage : class, IDataBlockPackage <TBlock>
 {
     return(ParseAsync <TBlock, TPackage>(inFileName, inFile, inRules, inGenerator, null, out outLoader));
 }
示例#17
0
        /// <summary>
        /// Parses the given file contents into blocks
        /// and merges into the given package.
        /// </summary>
        static public void Parse <TBlock, TPackage>(ref TPackage ioPackage, string inFileName, StringSlice inFile, IBlockParsingRules inRules, IBlockGenerator <TBlock, TPackage> inGenerator, BlockMetaCache inCache = null)
            where TBlock : class, IDataBlock
            where TPackage : class, IDataBlockPackage <TBlock>
        {
            IEnumerator parser = ParseAsync(ref ioPackage, inFileName, inFile, inRules, inGenerator, inCache);

            if (parser != null)
            {
                while (parser.MoveNext())
                {
                    ;
                }
            }
        }
示例#18
0
 public IdenticonGenerator WithBlockGenerators(IBlockGenerator[] blockGenerators)
 {
     this.DefaultBlockGenerators = blockGenerators;
     return this;
 }
示例#19
0
 public BlockSpawner(IBlockGenerator blockGenerator, IBlockPositionHolder positionHolder)
 {
     this.blockGenerator = blockGenerator;
     this.positionHolder = positionHolder;
 }
示例#20
0
 private IBlockGenerator GetBlockGenerator(IBlockGenerator[] generators, byte seed)
 {
     var totalweight = generators.Sum(w => w.Weight);
     var f = ((double)totalweight / 256);
     //Determine which generator to use
     var r = seed * f;   //Determine "random" number from hash from 0 .. totalweight
     int g = -1;
     while (r >= 0)
         r -= generators[++g].Weight;
     return generators[g];
 }
示例#21
0
        /// <summary>
        /// Parses the given file contents into blocks asynchronously
        /// and merges into the given package.
        /// Each MoveNext() call on the returned IEnumerator will parse one line.
        /// </summary>
        static public IEnumerator ParseAsync <TBlock, TPackage>(ref TPackage ioPackage, string inFileName, StringSlice inFile, IBlockParsingRules inRules, IBlockGenerator <TBlock, TPackage> inGenerator, BlockMetaCache inCache = null)
            where TBlock : class, IDataBlock
            where TPackage : class, IDataBlockPackage <TBlock>
        {
            if (inFile.IsEmpty)
            {
                return(null);
            }

            if (inRules == null)
            {
                throw new ArgumentNullException("inRules");
            }
            if (inGenerator == null)
            {
                throw new ArgumentNullException("inGenerator");
            }

            string fileName = string.IsNullOrEmpty(inFileName) ? NullFilename : inFileName;

            if (ioPackage == null)
            {
                ioPackage = inGenerator.CreatePackage(fileName);
            }
            return(InternalBlockParser <TBlock, TPackage> .ParseFile(fileName, inFile, ioPackage, inRules, inGenerator, inCache));
        }
示例#22
0
 public DecompressTask(string compressedFilename, string decompressedFileName, IBlockGenerator <BlockMetadata> blockGenerator)
 {
     _compressedFilename   = compressedFilename;
     _decompressedFileName = decompressedFileName;
     _blockGenerator       = blockGenerator;
 }
示例#23
0
 private IEnumerable<IBlock> LoadLibrary(IEnumerable<Bitmap> libraryBase, IBlockGenerator generator)
 {
     return libraryBase.SelectMany(generator.GenerateBlocks);
 }