|
| 1 | +//go:generate go install -v github.com/josephspurrier/goversioninfo/cmd/goversioninfo |
| 2 | +//go:generate goversioninfo -icon=res/papp.ico |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + |
| 8 | + . "github.com/portapps/portapps" |
| 9 | + "github.com/portapps/portapps/pkg/registry" |
| 10 | + "github.com/portapps/portapps/pkg/utl" |
| 11 | +) |
| 12 | + |
| 13 | +type config struct { |
| 14 | + Verbose string `yaml:"verbose" mapstructure:"verbose"` |
| 15 | +} |
| 16 | + |
| 17 | +var ( |
| 18 | + app *App |
| 19 | + cfg *config |
| 20 | +) |
| 21 | + |
| 22 | +func init() { |
| 23 | + var err error |
| 24 | + |
| 25 | + // Default config |
| 26 | + cfg = &config{ |
| 27 | + Verbose: "1", |
| 28 | + } |
| 29 | + |
| 30 | + // Init app |
| 31 | + if app, err = NewWithCfg("vlc-portable", "VLC", cfg); err != nil { |
| 32 | + Log.Fatal().Err(err).Msg("Cannot initialize application. See log file for more info.") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func main() { |
| 37 | + utl.CreateFolder(app.DataPath) |
| 38 | + app.Process = utl.PathJoin(app.AppPath, "vlc.exe") |
| 39 | + app.Args = []string{ |
| 40 | + "--vlm-conf=" + utl.PathJoin(app.DataPath, "vlcrc"), |
| 41 | + "--config=" + utl.PathJoin(app.DataPath, "vlcrc"), |
| 42 | + "--no-plugins-cache", |
| 43 | + "--no-qt-updates-notif", |
| 44 | + } |
| 45 | + |
| 46 | + // VLC paths |
| 47 | + vlcRoamingPath := utl.PathJoin(utl.RoamingPath(), "vlc") |
| 48 | + vlcTmpPath := utl.CreateFolder(app.AppPath, "tmp") |
| 49 | + |
| 50 | + // Set env vars |
| 51 | + utl.OverrideEnv("VLC_PLUGIN_PATH", utl.CreateFolder(app.DataPath, "plugins")) |
| 52 | + utl.OverrideEnv("VLC_VERBOSE", cfg.Verbose) |
| 53 | + utl.OverrideEnv("TEMP", vlcTmpPath) |
| 54 | + |
| 55 | + // VLC volatile files |
| 56 | + dataDvdcssPath := utl.PathJoin(app.DataPath, "dvdcss") |
| 57 | + dataMlXspf := utl.PathJoin(app.DataPath, "ml.xspf") |
| 58 | + dataVlcQtInterface := utl.PathJoin(app.DataPath, "vlc-qt-interface.ini") |
| 59 | + roamingDvdcssPath := utl.PathJoin(utl.RoamingPath(), "dvdcss") |
| 60 | + roamingMlXspf := utl.PathJoin(vlcRoamingPath, "ml.xspf") |
| 61 | + roamingVlcQtInterface := utl.PathJoin(vlcRoamingPath, "vlc-qt-interface.ini") |
| 62 | + |
| 63 | + // Copy existing files from data to roaming folder for the current user |
| 64 | + utl.CreateFolder(vlcRoamingPath) |
| 65 | + if _, err := os.Stat(dataMlXspf); err == nil { |
| 66 | + utl.CopyFile(dataMlXspf, roamingMlXspf) |
| 67 | + } |
| 68 | + if _, err := os.Stat(dataVlcQtInterface); err == nil { |
| 69 | + utl.CopyFile(dataVlcQtInterface, roamingVlcQtInterface) |
| 70 | + } |
| 71 | + |
| 72 | + // Handle reg key |
| 73 | + regsPath := utl.CreateFolder(app.RootPath, "reg") |
| 74 | + regFile := utl.PathJoin(regsPath, "VLC.reg") |
| 75 | + regKey := registry.ExportImport{ |
| 76 | + Key: `HKCU\Software\VideoLAN\VLC`, |
| 77 | + Arch: "32", |
| 78 | + File: regFile, |
| 79 | + } |
| 80 | + if err := registry.ImportKey(regKey); err != nil { |
| 81 | + Log.Warn().Err(err).Msg("Cannot import registry key") |
| 82 | + } |
| 83 | + |
| 84 | + // On exit |
| 85 | + defer func() { |
| 86 | + // Copy back to data |
| 87 | + if _, err := os.Stat(dataDvdcssPath); err == nil { |
| 88 | + if err = utl.CopyFolder(dataDvdcssPath, roamingDvdcssPath); err != nil { |
| 89 | + Log.Warn().Err(err).Msgf("Cannot copy back %s", dataDvdcssPath) |
| 90 | + } |
| 91 | + } |
| 92 | + if _, err := os.Stat(roamingMlXspf); err == nil { |
| 93 | + if err = utl.CopyFile(roamingMlXspf, dataMlXspf); err != nil { |
| 94 | + Log.Warn().Err(err).Msgf("Cannot copy back %s", roamingMlXspf) |
| 95 | + } |
| 96 | + } |
| 97 | + if _, err := os.Stat(roamingVlcQtInterface); err == nil { |
| 98 | + if err = utl.CopyFile(roamingVlcQtInterface, dataVlcQtInterface); err != nil { |
| 99 | + Log.Warn().Err(err).Msgf("Cannot copy back %s", roamingVlcQtInterface) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Export registry key |
| 104 | + os.Remove(regFile) |
| 105 | + if err := registry.ExportKey(regKey); err != nil { |
| 106 | + Log.Warn().Err(err).Msg("Cannot export registry key") |
| 107 | + } |
| 108 | + |
| 109 | + // Remove tmp and roaming path |
| 110 | + os.RemoveAll(vlcTmpPath) |
| 111 | + os.RemoveAll(vlcRoamingPath) |
| 112 | + }() |
| 113 | + |
| 114 | + // Launch |
| 115 | + app.Launch(os.Args[1:]) |
| 116 | +} |
0 commit comments