From bcdd25edef95fd830b14a0ee06c87eefab661719 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Fri, 29 May 2020 15:35:57 +0200 Subject: [PATCH] Fix addons startup (portapps/phyrox-portable#3) --- CHANGELOG.md | 5 +++ main.go | 111 +++++++++++++++++++-------------------------------- 2 files changed, 47 insertions(+), 69 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24958cb..d0e87b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 68.8.0-37 (2020/05/29) + +* Fix addons startup (portapps/phyrox-portable#3) +* Portapps 2.4.4 + ## 68.8.0-36 (2020/05/10) * Firefox ESR 68.8.0 diff --git a/main.go b/main.go index 1bdbe15..9bd86b0 100644 --- a/main.go +++ b/main.go @@ -8,9 +8,9 @@ import ( "fmt" "html/template" "io/ioutil" - "net/url" "os" "path" + "strings" "github.com/Jeffail/gabs" "github.com/pkg/errors" @@ -66,6 +66,36 @@ func main() { profileFolder, } + // Set env vars + crashreporterFolder := utl.CreateFolder(app.DataPath, "crashreporter") + pluginsFolder := utl.CreateFolder(app.DataPath, "plugins") + utl.OverrideEnv("MOZ_CRASHREPORTER", "0") + utl.OverrideEnv("MOZ_CRASHREPORTER_DATA_DIRECTORY", crashreporterFolder) + utl.OverrideEnv("MOZ_CRASHREPORTER_DISABLE", "1") + utl.OverrideEnv("MOZ_CRASHREPORTER_NO_REPORT", "1") + utl.OverrideEnv("MOZ_DATA_REPORTING", "0") + utl.OverrideEnv("MOZ_MAINTENANCE_SERVICE", "0") + utl.OverrideEnv("MOZ_PLUGIN_PATH", pluginsFolder) + utl.OverrideEnv("MOZ_UPDATER", "0") + + // Create and check mutex + mu, err := mutex.New(app.ID) + defer mu.Release() + if err != nil { + if !cfg.MultipleInstances { + log.Error().Msg("You have to enable multiple instances in your configuration if you want to launch another instance") + if _, err = dialog.MsgBox( + fmt.Sprintf("%s portable", app.Name), + "Other instance detected. You have to enable multiple instances in your configuration if you want to launch another instance.", + dialog.MsgBoxBtnOk|dialog.MsgBoxIconError); err != nil { + log.Error().Err(err).Msg("Cannot create dialog box") + } + return + } else { + log.Warn().Msg("Another instance is already running") + } + } + // Cleanup on exit if cfg.Cleanup { defer func() { @@ -136,36 +166,6 @@ pref("browser.startup.homepage_override.mstone", "ignore"); log.Error().Err(err).Msg("Cannot fix extensions path") } - // Set env vars - crashreporterFolder := utl.CreateFolder(app.DataPath, "crashreporter") - pluginsFolder := utl.CreateFolder(app.DataPath, "plugins") - utl.OverrideEnv("MOZ_CRASHREPORTER", "0") - utl.OverrideEnv("MOZ_CRASHREPORTER_DATA_DIRECTORY", crashreporterFolder) - utl.OverrideEnv("MOZ_CRASHREPORTER_DISABLE", "1") - utl.OverrideEnv("MOZ_CRASHREPORTER_NO_REPORT", "1") - utl.OverrideEnv("MOZ_DATA_REPORTING", "0") - utl.OverrideEnv("MOZ_MAINTENANCE_SERVICE", "0") - utl.OverrideEnv("MOZ_PLUGIN_PATH", pluginsFolder) - utl.OverrideEnv("MOZ_UPDATER", "0") - - // Create and check mutex - mu, err := mutex.New(app.ID) - defer mu.Release() - if err != nil { - if !cfg.MultipleInstances { - log.Error().Msg("You have to enable multiple instances in your configuration if you want to launch another instance") - if _, err = dialog.MsgBox( - fmt.Sprintf("%s portable", app.Name), - "Other instance detected. You have to enable multiple instances in your configuration if you want to launch another instance.", - dialog.MsgBoxBtnOk|dialog.MsgBoxIconError); err != nil { - log.Error().Err(err).Msg("Cannot create dialog box") - } - return - } else { - log.Warn().Msg("Another instance is already running") - } - } - // Copy default shortcut shortcutPath := path.Join(os.Getenv("APPDATA"), "Microsoft", "Windows", "Start Menu", "Programs", "Phyrox ESR Portable.lnk") defaultShortcut, err := assets.Asset("FirefoxESR.lnk") @@ -267,55 +267,28 @@ func checkLocale() (string, error) { } func updateAddonStartup(profileFolder string) error { - asLz4 := path.Join(profileFolder, "addonStartup.json.lz4") - if !utl.Exists(asLz4) { + lz4File := path.Join(profileFolder, "addonStartup.json.lz4") + if !utl.Exists(lz4File) || app.Prev.RootPath == "" { return nil } - decAsLz4, err := mozLz4Decompress(asLz4) + lz4Raw, err := mozLz4Decompress(lz4File) if err != nil { return err } - jsonAs, err := gabs.ParseJSON(decAsLz4) - if err != nil { - return err - } + prevPathLin := strings.Replace(utl.FormatUnixPath(app.Prev.RootPath), ` `, `%20`, -1) + currPathLin := strings.Replace(utl.FormatUnixPath(app.RootPath), ` `, `%20`, -1) + lz4Str := strings.Replace(string(lz4Raw), prevPathLin, currPathLin, -1) - if err := updateAddons("app-global", utl.PathJoin(profileFolder, "extensions"), jsonAs); err != nil { - return err - } - if err := updateAddons("app-profile", utl.PathJoin(profileFolder, "extensions"), jsonAs); err != nil { - return err - } - if err := updateAddons("app-system-defaults", utl.PathJoin(app.AppPath, "browser", "features"), jsonAs); err != nil { - return err - } - log.Debug().Msgf("Updated addonStartup.json: %s", jsonAs.String()) + prevPathWin := strings.Replace(strings.Replace(utl.FormatWindowsPath(app.Prev.RootPath), `\`, `\\`, -1), ` `, `%20`, -1) + currPathWin := strings.Replace(strings.Replace(utl.FormatWindowsPath(app.RootPath), `\`, `\\`, -1), ` `, `%20`, -1) + lz4Str = strings.Replace(lz4Str, prevPathWin, currPathWin, -1) - encAsLz4, err := mozLz4Compress(jsonAs.Bytes()) + lz4Enc, err := mozLz4Compress([]byte(lz4Str)) if err != nil { return err } - return ioutil.WriteFile(asLz4, encAsLz4, 0644) -} - -func updateAddons(field string, basePath string, container *gabs.Container) error { - if _, ok := container.Search(field, "path").Data().(string); !ok { - return nil - } - if _, err := container.Set(basePath, field, "path"); err != nil { - return errors.Wrap(err, fmt.Sprintf("couldn't set %s.path", field)) - } - - addons, _ := container.S(field, "addons").ChildrenMap() - for key, addon := range addons { - _, err := addon.Set(fmt.Sprintf("jar:file:///%s/%s.xpi!/", utl.FormatUnixPath(basePath), url.PathEscape(key)), "rootURI") - if err != nil { - return errors.Wrap(err, fmt.Sprintf("couldn't set %s %s.rootURI", field, key)) - } - } - - return nil + return ioutil.WriteFile(lz4File, lz4Enc, 0644) }