コード例 #1
0
        public static OperaPatch FindWithHeuristics(Settings settings, string pakFileName)
        {
            PakFile pakFile = new PakFile();

            pakFile.Load(pakFileName);
            string exeFileName         = Path.ChangeExtension(pakFileName, ".exe");
            string originalExeFileName = exeFileName + OperaPatch.BackupExtension;

            byte[] exeFile = File.ReadAllBytes(File.Exists(originalExeFileName) ? originalExeFileName : exeFileName);

            int offset = -1;

            byte[] original = null;
            byte[] patched  = null;

            if (settings.PatchOperaExe)
            {
                for (int n = 0; n < HeuristicPatches.Length; n++)
                {
                    HeuristicPatch patch = HeuristicPatches[n];
                    offset = ByteArray.Find(exeFile, patch.Find);
                    if (offset >= 0)
                    {
                        original = patch.Original;
                        patched  = patch.Patched;
                        offset  += ByteArray.Find(patch.Find, original);
                        break;
                    }
                }

                if (offset < 0)
                {
                    return(null);
                }
            }

            int speeddialLayoutJs        = FindInPakFile(pakFile, "var SpeeddialObject = function(");
            int startPageHtml            = FindInPakFile(pakFile, "<div class=\"view\" data-view-id=\"speeddial\"></div>");
            int preinstalledSpeeddialsJs = FindInPakFile(pakFile, "var PreinstalledSpeeddials = function(");
            int speeddialSuggestionsJs   = FindInPakFile(pakFile, "this.add_dial_dialog = function(");
            int toolsCss  = FindInPakFile(pakFile, "This file holds CSS that brings Opera 12 style to Opera");
            int filterCss = FindInPakFile(pakFile, ".filter-active .filter.animated");

            if (speeddialLayoutJs < 0 || startPageHtml < 0 || preinstalledSpeeddialsJs < 0 ||
                toolsCss < 0 || filterCss < 0)
            {
                return(null);
            }

            return(new OperaPatch(
                       "0.0.0.0", "0.0.0.0",
                       speeddialLayoutJs, startPageHtml, preinstalledSpeeddialsJs, speeddialSuggestionsJs, toolsCss, filterCss,
                       offset >= 0 ? new ExePatch(offset, original, patched) : null));
        }
コード例 #2
0
ファイル: OperaPatches.cs プロジェクト: Hanngman/opera-tools
        public static OperaPatch FindWithHeuristics(Settings settings, string pakFileName)
        {
            PakFile pakFile = new PakFile();

            pakFile.Load(pakFileName);
            string exeFileName         = Path.ChangeExtension(pakFileName, ".exe");
            string originalExeFileName = exeFileName + OperaPatch.BackupExtension;

            byte[] exeFile = File.ReadAllBytes(File.Exists(originalExeFileName) ? originalExeFileName : exeFileName);

            int operaPakHashOffset = -1;

            if (settings.PatchOperaExe)
            {
                string originalPakFileName = pakFileName + OperaPatch.BackupExtension;
                operaPakHashOffset = ByteArray.Find(exeFile, GetPakFileHash(File.Exists(originalPakFileName) ? originalPakFileName : pakFileName));
                if (operaPakHashOffset < 0)
                {
                    return(null);
                }
            }

            int speeddialLayoutJs        = FindInPakFile(pakFile, "var SpeeddialObject = function(");
            int startPageHtml            = FindInPakFile(pakFile, "<div class=\"view\" data-view-id=\"speeddial\"></div>");
            int preinstalledSpeeddialsJs = FindInPakFile(pakFile, "var PreinstalledSpeeddials = function(");
            int speeddialSuggestionsJs   = FindInPakFile(pakFile, "this.add_dial_dialog = function(");
            int toolsCss  = FindInPakFile(pakFile, "This file holds CSS that brings Opera 12 style to Opera");
            int filterCss = FindInPakFile(pakFile, ".filter-active .filter.animated");

            if (speeddialLayoutJs < 0 || startPageHtml < 0 || preinstalledSpeeddialsJs < 0 ||
                toolsCss < 0 || filterCss < 0)
            {
                return(null);
            }

            return(new OperaPatch(
                       speeddialLayoutJs, startPageHtml, preinstalledSpeeddialsJs, speeddialSuggestionsJs, toolsCss, filterCss,
                       operaPakHashOffset));
        }
コード例 #3
0
ファイル: OperaPatch.cs プロジェクト: bono851212/opera-tools
        public void Apply(Settings settings, string pakFileName)
        {
            string     originalFileName = pakFileName + BackupExtension;
            CssPatches cssPatches       = settings.CssPatches;

            PakFile pakFile = new PakFile();

            ColoredConsole.WriteLine("Reading ~W{0}~N ...", pakFileName);

            if (!File.Exists(originalFileName))
            {
                File.Copy(pakFileName, originalFileName);
            }
            else
            {
                File.Copy(originalFileName, pakFileName, true);
            }

            pakFile.Load(pakFileName);

            const string TEXT_MAX_X_COUNT                     = "  var MAX_X_COUNT = ";
            const string TEXT_MARGIN_X                        = "  var MARGIN_X = ";
            const string TEXT_MARGIN_Y                        = "  var MARGIN_Y = ";
            const string TEXT_DIAL_WIDTH                      = "SpeeddialObject.DIAL_WIDTH = ";
            const string TEXT_DIAL_HEIGHT                     = "SpeeddialObject.DIAL_HEIGHT = ";
            const string TEXT_THUMBNAIL_URL1                  = "    \"size=\" + this.DIAL_WIDTH + \"x\" + this.DIAL_HEIGHT +";
            const string TEXT_THUMBNAIL_URL2                  = "    \"size=\" + (({0:F4} * this.DIAL_WIDTH + 0.5) >> 0) + \"x\" + (({0:F4} * this.DIAL_HEIGHT + 0.5) >> 0) +";
            const string TEXT_CSS_WIDTH                       = "  width: ";
            const string TEXT_CSS_HEIGHT                      = "  height: ";
            const string TEXT_CSS_TOP                         = "  top: ";
            const string TEXT_CSS_LEFT                        = "  left: ";
            const string TEXT_CSS_BACKGROUND_POSITION         = "  background-position: ";
            const string TEXT_PREINSTALLED_CHECK_URL_FUNCTION = "  this.checkURL = function(URL)";
            const string TEXT_PREINSTALLED_CHECK_URL_NEXTLINE = "  {";
            const string TEXT_PREINSTALLED_LAST_LINE          = "}).apply(PreinstalledSpeeddials);";
            const string TEXT_STYLE           = "</style>";
            const string TEXT_DIAL_URL        = "SpeeddialObject.DIAL_URL + ";
            const string TEXT_DIAL_RELOAD_URL = "SpeeddialObject.DIAL_RELOAD_URL +";
            const string TEXT_ADD_DIAL_URL    = "var sdURL = sd_obj.sdURL || SpeeddialObject.DIAL_URL + ";

            string[] lines = pakFile.GetItem(SpeeddialLayoutJs);
            for (int n = 0; n < lines.Length; n++)
            {
                string line = lines[n];
                if (line.StartsWith(TEXT_MAX_X_COUNT))
                {
                    lines[n] = TEXT_MAX_X_COUNT + settings.SpeedDial.Columns.ToString() + ";";
                }
                else if (line.StartsWith(TEXT_MARGIN_X))
                {
                    lines[n] = TEXT_MARGIN_X + settings.SpeedDial.MarginX.ToString() + ";";
                }
                else if (line.StartsWith(TEXT_MARGIN_Y))
                {
                    lines[n] = TEXT_MARGIN_Y + settings.SpeedDial.MarginY.ToString() + ";";
                }
                else if (line.StartsWith(TEXT_DIAL_WIDTH))
                {
                    lines[n] = TEXT_DIAL_WIDTH + settings.SpeedDial.ThumbnailWidth.ToString() + ";";
                }
                else if (line.StartsWith(TEXT_DIAL_HEIGHT))
                {
                    lines[n] = TEXT_DIAL_HEIGHT + settings.SpeedDial.ThumbnailHeight.ToString() + ";";
                }
                else if (settings.SpeedDial.CropPageForThumbnail && line.StartsWith(TEXT_THUMBNAIL_URL1))
                {
                    lines[n] = String.Format(CultureInfo.InvariantCulture, TEXT_THUMBNAIL_URL2, 100.0 / settings.SpeedDial.CropArea);
                }
                else if (settings.SpeedDial.AddCustomThumbnails && line.Contains(TEXT_DIAL_URL))
                {
                    lines[n] = "          PreinstalledSpeeddials.transformURL(this.navigateURL) || (SpeeddialObject.DIAL_URL + encodeURIComponent(this.navigateURL))";
                }
                else if (settings.SpeedDial.AddCustomThumbnails && line.Contains(TEXT_DIAL_RELOAD_URL))
                {
                    lines[n]     = "          PreinstalledSpeeddials.transformURL(this.navigateURL) || (SpeeddialObject.DIAL_RELOAD_URL + encodeURIComponent(this.navigateURL))";
                    lines[n + 1] = "";
                }
            }
            pakFile.SetItem(SpeeddialLayoutJs, lines);

            if (settings.SpeedDial.AddCustomThumbnails)
            {
                lines = pakFile.GetItem(SpeeddialSuggestionsJs);
                for (int n = 0; n < lines.Length; n++)
                {
                    if (lines[n].Contains(TEXT_ADD_DIAL_URL))
                    {
                        lines[n] = "    var sdURL = sd_obj.sdURL || PreinstalledSpeeddials.transformURL(sd_obj.url) || SpeeddialObject.DIAL_URL + encodeURIComponent(sd_obj.url);";
                        break;
                    }
                }
                pakFile.SetItem(SpeeddialSuggestionsJs, lines);
            }

            lines = pakFile.GetItem(StartPageHtml);
            for (int n = 0; n < lines.Length; n++)
            {
                string line = lines[n];
                if (line == ".speeddial")
                {
                    ApplyCssPixelRule(lines, n, TEXT_CSS_WIDTH, settings.SpeedDial.ThumbnailWidth);
                    ApplyCssPixelRule(lines, n, TEXT_CSS_HEIGHT, settings.SpeedDial.ThumbnailHeight);
                    if (settings.SpeedDial.CropPageForThumbnail)
                    {
                        ApplyCssRule(lines, n, TEXT_CSS_BACKGROUND_POSITION, "0% 0%");
                    }
                }
                else if (line == ".dial-thumbnail")
                {
                    ApplyCssPixelRule(lines, n, TEXT_CSS_WIDTH, (settings.SpeedDial.ThumbnailWidth - 36) / 2);
                    ApplyCssPixelRule(lines, n, TEXT_CSS_HEIGHT, (settings.SpeedDial.ThumbnailHeight - 28) / 2);
                }
                else if (line == ".dial-thumbnail:nth-child(2)")
                {
                    ApplyCssPixelRule(lines, n, TEXT_CSS_LEFT, settings.SpeedDial.ThumbnailWidth / 2);
                }
                else if (line == ".dial-thumbnail:nth-child(3)")
                {
                    ApplyCssPixelRule(lines, n, TEXT_CSS_TOP, settings.SpeedDial.ThumbnailHeight / 2 - 1);
                }
                else if (line == ".dial-thumbnail:nth-child(4)")
                {
                    ApplyCssPixelRule(lines, n, TEXT_CSS_LEFT, settings.SpeedDial.ThumbnailWidth / 2);
                    ApplyCssPixelRule(lines, n, TEXT_CSS_TOP, settings.SpeedDial.ThumbnailHeight / 2 - 1);
                }
                else if (line.StartsWith(TEXT_STYLE))
                {
                    lines[n] = TEXT_STYLE + cssPatches.Apply("<style>", "</style>", CssPatchFile.StartPageHtml);
                }
            }
            pakFile.SetItem(StartPageHtml, lines);

            lines = pakFile.GetItem(ToolsCss);
            lines[lines.Length - 1] = cssPatches.Apply("", "", CssPatchFile.ToolsCss);
            pakFile.SetItem(ToolsCss, lines);

            lines = pakFile.GetItem(FilterCss);
            lines[lines.Length - 1] = cssPatches.Apply("", "", CssPatchFile.FilterCss);
            pakFile.SetItem(FilterCss, lines);

            lines = pakFile.GetItem(PreinstalledSpeeddialsJs);
            for (int n = 0; n < lines.Length; n++)
            {
                string line = lines[n];
                if (line == TEXT_PREINSTALLED_CHECK_URL_FUNCTION && n < lines.Length - 1 &&
                    lines[n + 1].StartsWith(TEXT_PREINSTALLED_CHECK_URL_NEXTLINE))
                {
                    lines[n + 1] = TEXT_PREINSTALLED_CHECK_URL_NEXTLINE + (settings.SpeedDial.DisableBuiltInThumbnails ? "return null;" : "");
                }
                else if (settings.SpeedDial.AddCustomThumbnails && line == TEXT_PREINSTALLED_LAST_LINE)
                {
                    StringBuilder sb = new StringBuilder("var _transformData = { re: null, getRe: function(dict) { return new RegExp(\"^.{4,5}:\\\\/\\\\/(?:[^.]+\\\\.)?(\" + _arr_to_regexp(Object.keys(dict)) + \")(?:\\\\/|$)\"); }, thumbnails: { ");
                    bool          f  = false;

                    foreach (KeyValuePair <string, string> kvp in settings.SpeedDial.CustomThumbnails)
                    {
                        if (f)
                        {
                            sb.Append(',');
                        }
                        sb.AppendFormat("\"{0}\": \"{1}\"", kvp.Key, kvp.Value);
                        f = true;
                    }

                    sb.Append("} }; this.transformURL = function(URL) { if (!_transformData.re) _transformData.re = _transformData.getRe(_transformData.thumbnails); var match = _transformData.re.exec(URL); return match ? _transformData.thumbnails[match[1]] : null; };");
                    lines[n] = sb.ToString() + TEXT_PREINSTALLED_LAST_LINE;
                }
            }
            pakFile.SetItem(PreinstalledSpeeddialsJs, lines);

            ColoredConsole.WriteLine("Writing ~W{0}~N ...", pakFileName);

            pakFile.Save(pakFileName + ".temp");
            File.Delete(pakFileName);
            File.Move(pakFileName + ".temp", pakFileName);

            string contentFileName         = Path.Combine(Path.GetDirectoryName(pakFileName), "resources\\default_partner_content.json");
            string originalContentFileName = contentFileName + BackupExtension;

            if (!File.Exists(originalContentFileName) && File.Exists(contentFileName))
            {
                File.Copy(contentFileName, originalContentFileName);
            }
            else if (File.Exists(originalContentFileName))
            {
                File.Copy(originalContentFileName, contentFileName, true);
            }

            if (settings.Search.DeletePartnerSearchEngines)
            {
                File.Delete(contentFileName);
            }

            if (ExePatch != null && settings.PatchOperaExe)
            {
                ExePatch.Apply(Path.ChangeExtension(pakFileName, ".exe"), settings);
            }
        }