public ulong GetPluginId(IPlugin plugin)
        {
            MurmurHash2UInt32Hack hasher = new MurmurHash2UInt32Hack();
            uint val1 = hasher.Hash(ASCIIEncoding.Unicode.GetBytes(plugin.Name));
            uint val2 = hasher.Hash(ASCIIEncoding.Unicode.GetBytes(plugin.PluginVersion.ToString()));

            return((ulong)(val1 + val2));
        }
示例#2
0
        internal string GetContentFromHttpContext(HttpContextBase httpContext, ControllerContext controllerContext)
        {
            var urlHelper = new UrlHelper(controllerContext.RequestContext);

            StringBuilder sb = new StringBuilder();

            foreach (var ses in (from s in MothScriptHelper.Stylesheets.SelectMany(i => i.Items)
                                 group s by s.Category into g
                                 select g.Select(x => x.Filename)))
            {
                var stylesheets = ses.ToList();

                var key = "scripthelper.rendercss." + string.Join("|", stylesheets.ToArray());

                sb.AppendLine(Provider.GetFromCache(key, () =>
                {
                    // hashcode bepalen
                    StringBuilder stylo = MothScriptHelper.GetFileContent(stylesheets, httpContext);

                    var outputFile = Encoding.UTF8.GetBytes(stylo.ToString());
                    var hashcode   = HashingInstance.Hash(outputFile);

                    string url = urlHelper.Content("~/resources/css/");
                    return(string.Format("<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"{2}?keys={0}&amp;{1}\" />", string.Join("|", stylesheets.ToArray()), hashcode, url));
                }, Provider.CacheDurations.ExternalScript));
            }

            return(sb.ToString());
        }
示例#3
0
        internal string GetScriptsFromHttpContext(HttpContextBase httpContext, ControllerContext controllerContext)
        {
            var urlHelper = new UrlHelper(new RequestContext(httpContext, controllerContext.RouteData));

            StringBuilder sb = new StringBuilder();

            foreach (var ses in (from s in MothScriptHelper.Scripts.SelectMany(i => i.Items)
                                 group s by s.Category into g
                                 select g.Select(x => x.Filename)))
            {
                var scripts = ses.ToList();

                //var scripts = MothScriptHelper.Scripts[cat].ToList();

                var key = "scripthelper.renderscripts." + string.Join("|", scripts.ToArray());

                sb.AppendLine(Provider.GetFromCache(key, () =>
                {
                    // hashcode bepalen
                    StringBuilder script = MothScriptHelper.GetFileContent(scripts, httpContext);

                    var outputFile = Encoding.UTF8.GetBytes(script.ToString());
                    var hashcode   = HashingInstance.Hash(outputFile);

                    var keySb = new StringBuilder();
                    if (Provider.Enable.ScriptMinification)
                    {
                        string url = urlHelper.Content("~/resources/javascript/");
                        keySb.AppendLine(string.Format("<script src=\"{2}?keys={0}&amp;{1}\"></script>", string.Join("|", scripts.ToArray()), hashcode, url));
                    }
                    else
                    {
                        foreach (var s in scripts)
                        {
                            string url = urlHelper.Content(s);
                            keySb.AppendLine(string.Format("<script src=\"{0}?{1}\"></script>", url, hashcode));
                        }
                    }
                    return(keySb.ToString());
                }, Provider.CacheDurations.ExternalScript));
            }

            sb.AppendLine(RenderDataUriFallback(httpContext, controllerContext));

            if (MothScriptHelper.InlineScripts.Any())
            {
                var isb = new StringBuilder("<script>");
                foreach (var s in MothScriptHelper.InlineScripts)
                {
                    isb.AppendLine(s);
                }
                isb.Append("</script>");

                sb.Append(isb.ToString());
            }

            return(sb.ToString());
        }
示例#4
0
        private SpriteRuleSet GenerateSpriteDefinition(string file)
        {
            if (!file.StartsWith("~/")) file = "~/" + file;

            return _provider.GetFromCache("spritedefinition." + file, () =>
            {
                var parser = new BoneSoft.CSS.CSSParser();
                var fullCssPath = HttpContext.Server.MapPath(file);
                string allText = System.IO.File.ReadAllText(fullCssPath);

                if (_provider.Enable.CssTidy)
                {
                    allText = new CssTidy().Tidy(fullCssPath);
                }

                var doc = parser.ParseText(allText);

                MurmurHash2UInt32Hack hashing = new MurmurHash2UInt32Hack();
                uint hash = 0;

                // check whether we have to sprite some images
                List<SpriteRule> rules = new List<SpriteRule>();
                List<RuleSet> spritesFromCss = doc.RuleSets.Where(d => d.Declarations.Any(r => r.Name.Equals("moth-sprite", StringComparison.OrdinalIgnoreCase))).ToList();
                foreach (var sprite in spritesFromCss)
                {
                    var spriteName = sprite.Declarations.First(r => r.Name == "moth-sprite");

                    Term term = GetImageTerm(sprite);

                    string localPath = term.Value;

                    var fullLocalPath = Path.Combine(HttpContext.Server.MapPath(Path.GetDirectoryName(file)), localPath);
                    var bytes = System.IO.File.ReadAllBytes(fullLocalPath);

                    var spriteRule = new SpriteRule
                    {
                        Bytes = bytes,
                        Filename = localPath,
                        SpriteName = spriteName.Expression.Terms.First().Value
                    };

                    using (var imageStream = new MemoryStream(bytes))
                    {
                        var bitmap = Image.FromStream(imageStream);

                        spriteRule.ImageFormat = bitmap.RawFormat;
                        spriteRule.Width = bitmap.Width;
                        spriteRule.Height = bitmap.Height;

                        //spriteRule.X = 0;
                        //spriteRule.Y = positionTable.ContainsKey(spriteRule.SpriteName) ? positionTable[spriteRule.SpriteName] : 0;
                    }

                    /*if (!positionTable.ContainsKey(spriteRule.SpriteName))
                        positionTable.Add(spriteRule.SpriteName, 0);

                    positionTable[spriteRule.SpriteName] += spriteRule.Height;

                    sprite.Declarations.Add(new Declaration {Name = "background-position-x", Expression = new Expression { Terms = new List<Term>{ new Term { Value = spriteRule.X + "px"}}}});
                    sprite.Declarations.Add(new Declaration { Name = "background-position-y", Expression = new Expression { Terms = new List<Term> { new Term { Value = -spriteRule.Y + "px" } } } });
                   */

                    sprite.Declarations.Add(new Declaration { Name = "moth-original-filename", Expression = new Expression { Terms = new List<Term> { new Term { Value = localPath } } } });

                    term.Value = string.Format("../../resources/sprites/?file={0}&key={1}&type={2}", file, spriteName.Expression, DataUriHelper.GetStringFromImageType(spriteRule.ImageFormat));

                    hash ^= hashing.Hash(bytes);

                    rules.Add(spriteRule);
                }

                // sort sprites
                var blah = rules.ToArray();
                new SpriteAlghoritms().AlgSortByArea(1200, blah);

                rules = blah.ToList();

                // add hash to every call
                foreach (var sprite in spritesFromCss)
                {
                    var term = GetImageTerm(sprite);
                    term.Value += string.Format("&{0}", hash);

                    var spriteRule = rules.First(r => r.Filename == sprite.Declarations.First(f => f.Name == "moth-original-filename").Expression.Terms.First().Value);

                    // set position-x and position-y
                    sprite.Declarations.Add(new Declaration { Name = "background-position-x", Expression = new Expression { Terms = new List<Term> { new Term { Value = spriteRule.X + "px" } } } });
                    sprite.Declarations.Add(new Declaration { Name = "background-position-y", Expression = new Expression { Terms = new List<Term> { new Term { Value = -spriteRule.Y + "px" } } } });

                    // remove moth- extensions
                    sprite.Declarations = sprite.Declarations.Where(s => !s.Name.StartsWith("moth-")).ToList();
                }

                // find all URL declarations that aren't already processed by the spriting
                foreach(var uri in doc.RuleSets
                    .SelectMany(d=>d.Declarations.SelectMany(r=>r.Expression.Terms.Where(t=>t.Type == TermType.Url))))
                {
                    MakeUriTagRelative(uri, fullCssPath);
                }

                //foreach(var image in doc.RuleSets
                //    .Where(d => !d.Declarations.Any(r => r.Name.Equals("moth-original-filename", StringComparison.OrdinalIgnoreCase)))
                //    .Where(d=>d.Declarations.Any(r => r.Name.Equals("background-image", StringComparison.OrdinalIgnoreCase))
                //    || d.Declarations.Any(r => r.Name.Equals("background", StringComparison.OrdinalIgnoreCase))).ToList())
                //{
                //    var imageTag = GetImageTerm(image);

                //    // replace the current value of the image tag with a new relative path

                //}

                return new SpriteRuleSet
                {
                    Hash = hash.ToString(),
                    Document = doc,
                    Rules = rules
                };
            }, _provider.CacheDurations.ExternalScript);
        }
示例#5
0
        private SpriteRuleSet GenerateSpriteDefinition(string file)
        {
            if (!file.StartsWith("~/"))
            {
                file = "~/" + file;
            }

            return(_provider.GetFromCache("spritedefinition." + file, () =>
            {
                var parser = new BoneSoft.CSS.CSSParser();
                var fullCssPath = HttpContext.Server.MapPath(file);
                string allText = System.IO.File.ReadAllText(fullCssPath);

                if (_provider.Enable.CssTidy)
                {
                    allText = new CssTidy().Tidy(fullCssPath);
                }

                var doc = parser.ParseText(allText);

                MurmurHash2UInt32Hack hashing = new MurmurHash2UInt32Hack();
                uint hash = 0;

                // check whether we have to sprite some images
                List <SpriteRule> rules = new List <SpriteRule>();
                List <RuleSet> spritesFromCss = doc.RuleSets.Where(d => d.Declarations.Any(r => r.Name.Equals("moth-sprite", StringComparison.OrdinalIgnoreCase))).ToList();
                foreach (var sprite in spritesFromCss)
                {
                    var spriteName = sprite.Declarations.First(r => r.Name == "moth-sprite");

                    Term term = GetImageTerm(sprite);

                    string localPath = term.Value;

                    var fullLocalPath = Path.Combine(HttpContext.Server.MapPath(Path.GetDirectoryName(file)), localPath);
                    var bytes = System.IO.File.ReadAllBytes(fullLocalPath);

                    var spriteRule = new SpriteRule
                    {
                        Bytes = bytes,
                        Filename = localPath,
                        SpriteName = spriteName.Expression.Terms.First().Value
                    };

                    using (var imageStream = new MemoryStream(bytes))
                    {
                        var bitmap = Image.FromStream(imageStream);

                        spriteRule.ImageFormat = bitmap.RawFormat;
                        spriteRule.Width = bitmap.Width;
                        spriteRule.Height = bitmap.Height;

                        //spriteRule.X = 0;
                        //spriteRule.Y = positionTable.ContainsKey(spriteRule.SpriteName) ? positionTable[spriteRule.SpriteName] : 0;
                    }

                    /*if (!positionTable.ContainsKey(spriteRule.SpriteName))
                     *  positionTable.Add(spriteRule.SpriteName, 0);
                     *
                     * positionTable[spriteRule.SpriteName] += spriteRule.Height;
                     *
                     * sprite.Declarations.Add(new Declaration {Name = "background-position-x", Expression = new Expression { Terms = new List<Term>{ new Term { Value = spriteRule.X + "px"}}}});
                     * sprite.Declarations.Add(new Declaration { Name = "background-position-y", Expression = new Expression { Terms = new List<Term> { new Term { Value = -spriteRule.Y + "px" } } } });
                     */

                    sprite.Declarations.Add(new Declaration {
                        Name = "moth-original-filename", Expression = new Expression {
                            Terms = new List <Term> {
                                new Term {
                                    Value = localPath
                                }
                            }
                        }
                    });

                    term.Value = string.Format("../../resources/sprites/?file={0}&key={1}&type={2}", file, spriteName.Expression, DataUriHelper.GetStringFromImageType(spriteRule.ImageFormat));

                    hash ^= hashing.Hash(bytes);

                    rules.Add(spriteRule);
                }

                // sort sprites
                var blah = rules.ToArray();
                new SpriteAlghoritms().AlgSortByArea(1200, blah);

                rules = blah.ToList();

                // add hash to every call
                foreach (var sprite in spritesFromCss)
                {
                    var term = GetImageTerm(sprite);
                    term.Value += string.Format("&{0}", hash);

                    var spriteRule = rules.First(r => r.Filename == sprite.Declarations.First(f => f.Name == "moth-original-filename").Expression.Terms.First().Value);

                    // set position-x and position-y
                    sprite.Declarations.Add(new Declaration {
                        Name = "background-position-x", Expression = new Expression {
                            Terms = new List <Term> {
                                new Term {
                                    Value = spriteRule.X + "px"
                                }
                            }
                        }
                    });
                    sprite.Declarations.Add(new Declaration {
                        Name = "background-position-y", Expression = new Expression {
                            Terms = new List <Term> {
                                new Term {
                                    Value = -spriteRule.Y + "px"
                                }
                            }
                        }
                    });

                    // remove moth- extensions
                    sprite.Declarations = sprite.Declarations.Where(s => !s.Name.StartsWith("moth-")).ToList();
                }

                // find all URL declarations that aren't already processed by the spriting
                foreach (var uri in doc.RuleSets
                         .SelectMany(d => d.Declarations.SelectMany(r => r.Expression.Terms.Where(t => t.Type == TermType.Url))))
                {
                    MakeUriTagRelative(uri, fullCssPath);
                }


                //foreach(var image in doc.RuleSets
                //    .Where(d => !d.Declarations.Any(r => r.Name.Equals("moth-original-filename", StringComparison.OrdinalIgnoreCase)))
                //    .Where(d=>d.Declarations.Any(r => r.Name.Equals("background-image", StringComparison.OrdinalIgnoreCase))
                //    || d.Declarations.Any(r => r.Name.Equals("background", StringComparison.OrdinalIgnoreCase))).ToList())
                //{
                //    var imageTag = GetImageTerm(image);

                //    // replace the current value of the image tag with a new relative path

                //}

                return new SpriteRuleSet
                {
                    Hash = hash.ToString(),
                    Document = doc,
                    Rules = rules
                };
            }, _provider.CacheDurations.ExternalScript));
        }