Skip to content

Commit e1d1d5c

Browse files
committed
- Add --new-instance when another LibreWolf instance is running:
Before, it would add the argument if the launcher wasn't running while any LibreWolf instance was. This caused issues when trying to open a new window while the launcher was waiting for other LW instances to close, while the portable LW instances were already closed. - Initialize properly when this launcher already running - Use DSlash() instead of separate variables for "\" => "\\" - Rename *InstanceRunning() to *LauncherRunning() for clarity
1 parent 8973ebe commit e1d1d5c

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

LibreWolf-Portable.ahk

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; LibreWolf Portable - https://github.com/ltGuillaume/LibreWolf-Portable
2-
;@Ahk2Exe-SetFileVersion 1.4.2
2+
;@Ahk2Exe-SetFileVersion 1.4.3
33

44
;@Ahk2Exe-Bin Unicode 64*
55
;@Ahk2Exe-SetCompanyName LibreWolf Community
@@ -36,33 +36,36 @@ Global _Title := "LibreWolf Portable"
3636
, _MissingDLLs := "You probably don't have msvcp140.dll and vcruntime140.dll present on your system. Put these files in the folder " LibreWolfPath ",`nor install the Visual C++ runtime libraries via https://librewolf.net."
3737
, _FileReadError := "Error reading file for modification:"
3838

39-
If (ThisInstanceRunning()) {
40-
RunLibreWolf()
41-
Exit()
42-
}
4339
Init()
4440
CheckPaths()
4541
CheckArgs()
42+
If (ThisLauncherRunning()) {
43+
RunLibreWolf()
44+
Exit()
45+
}
4646
CheckUpdates()
4747
RegBackup()
4848
UpdateProfile()
4949
RunLibreWolf()
5050
SetTimer, WaitForClose, 5000
5151

52-
OtherInstanceRunning() {
53-
Result := InstanceRunning("Name=""LibreWolf-Portable.exe""")
54-
;MsgBox, OtherInstanceRunning: %Result%
52+
DSlash(Path) {
53+
Return StrReplace(Path, "\", "\\")
54+
}
55+
56+
OtherLauncherRunning() {
57+
Result := LauncherRunning("Name=""LibreWolf-Portable.exe"" and ExecutablePath<>""" DSlash(A_ScriptFullPath) """")
58+
;MsgBox, OtherLauncherRunning: %Result%
5559
Return %Result%
5660
}
5761

58-
ThisInstanceRunning() {
59-
ScriptPathDS := StrReplace(A_ScriptFullPath, "\", "\\")
60-
Result := InstanceRunning("ExecutablePath=""" ScriptPathDS """")
61-
;MsgBox, ThisInstanceRunning: %Result%
62+
ThisLauncherRunning() {
63+
Result := LauncherRunning("ExecutablePath=""" DSlash(A_ScriptFullPath) """")
64+
;MsgBox, ThisLauncherRunning: %Result%
6265
Return %Result%
6366
}
6467

65-
InstanceRunning(Where) {
68+
LauncherRunning(Where) {
6669
Process, Exist ; Put launcher's process id into ErrorLevel
6770
Query := "Select ProcessId from Win32_Process where ProcessId!=" ErrorLevel " and " Where
6871
;MsgBox, Query: %Query%
@@ -164,7 +167,7 @@ RegBackup() {
164167
BackupKeyFound := True
165168
;MsgBox, BackupFound: %BackupKeyFound%
166169
If (BackupKeyFound) {
167-
If (OtherInstanceRunning())
170+
If (OtherLauncherRunning())
168171
Return
169172
MsgBox, 54, %_Title%, %_BackupKeyFound%`n%RegKey%.pbak`n%_BackupFoundActions%
170173
IfMsgBox Cancel
@@ -186,10 +189,8 @@ UpdateProfile() {
186189
Return False
187190

188191
; Adjust absolute profile folder paths to current path
189-
LibreWolfPathDS := StrReplace(LibreWolfPath, "\", "\\")
190192
VarSetCapacity(LibreWolfPathUri, 300*2)
191193
DllCall("shlwapi\UrlCreateFromPathW", "Str", LibreWolfPath, "Str", LibreWolfPathUri, "UInt*", 300, "UInt", 0x00040000) // 0x00040000 = URL_ESCAPE_AS_UTF8
192-
ProfilePathDS := StrReplace(ProfilePath, "\", "\\")
193194
VarSetCapacity(ProfilePathUri, 300*2)
194195
DllCall("shlwapi\UrlCreateFromPathW", "Str", ProfilePath, "Str", ProfilePathUri, "UInt*", 300, "UInt", 0x00040000)
195196
OverridesPath := "user_pref(""autoadmin.global_config_url"", """ ProfilePathUri "/librewolf.overrides.cfg"");"
@@ -232,9 +233,9 @@ ReplacePaths(FilePath) {
232233
If (Count = 0)
233234
File .= OverridesPath
234235
}
235-
File := RegExReplace(File, "i).:\\[^""]+?(\Q\\browser\\features\E)", LibreWolfPathDS "$1")
236+
File := RegExReplace(File, "i).:\\[^""]+?(\Q\\browser\\features\E)", DSlash(LibreWolfPath) "$1")
236237
File := RegExReplace(File, "i)file:\/\/\/[^""]+?(\Q/browser/features\E)", LibreWolfPathUri "$1")
237-
File := RegExReplace(File, "i).:\\[^""]+?(\Q\\extensions\E)", ProfilePathDS "$1")
238+
File := RegExReplace(File, "i).:\\[^""]+?(\Q\\extensions\E)", DSlash(ProfilePath) "$1")
238239
File := RegExReplace(File, "i)file:\/\/\/[^""]+?(\Q/extensions\E)", ProfilePathUri "$1")
239240

240241
If (File = FileOrg)
@@ -247,11 +248,11 @@ ReplacePaths(FilePath) {
247248
}
248249

249250
RunLibreWolf() {
250-
If (!ThisInstanceRunning) {
251+
If (!ThisLauncherRunning)
251252
SetTimer, GetCityHash, 1000
252-
If (LibreWolfRunning())
253-
Args := "--new-instance " Args
254-
}
253+
254+
If (LibreWolfRunning() And !ThisLibreWolfRunning())
255+
Args := "--new-instance " Args
255256

256257
;MsgBox, %LibreWolfExe% -profile "%ProfilePath%" %Args%
257258
RunWait, %LibreWolfExe% -Profile "%ProfilePath%" %Args%,, UseErrorLevel
@@ -280,8 +281,14 @@ WaitForClose() {
280281
SetTimer, CleanUp, 2000
281282
}
282283

283-
LibreWolfRunning() {
284-
For Process in ComObjGet("winmgmts:").ExecQuery("Select ProcessId from Win32_Process where Name=""librewolf.exe""") {
284+
ThisLibreWolfRunning() {
285+
Result := LibreWolfRunning(" and ExecutablePath=""" DSlash(LibreWolfExe) """")
286+
;MsgBox, ThisLibreWolfRunning: %Result%
287+
Return %Result%
288+
}
289+
290+
LibreWolfRunning(Where := "") {
291+
For Process in ComObjGet("winmgmts:").ExecQuery("Select ProcessId from Win32_Process where Name=""librewolf.exe"" " Where) {
285292
Try {
286293
oUser := ComObject(0x400C, &User) ; VT_BYREF
287294
Process.GetOwner(oUser)
@@ -295,19 +302,19 @@ LibreWolfRunning() {
295302
}
296303

297304
CleanUp() {
298-
; Wait until all instances are closed before restoring backed up registry key
299-
If (RegBackedUp And OtherInstanceRunning())
305+
; Wait until all launcher instances are closed before restoring backed up registry key
306+
If (RegBackedUp And OtherLauncherRunning())
300307
Return
301308

302309
SetTimer,, Delete
303310

304-
; Remove files with CityHash of this instance
311+
; Remove files with CityHash of this LibreWolf instance
305312
If (CityHash) {
306313
FileDelete, %MozCommonPath%\*%CityHash%*.*
307314
FileRemoveDir, %MozCommonPath%\updates\%CityHash%, 1
308315
}
309316

310-
If (OtherInstanceRunning())
317+
If (OtherLauncherRunning())
311318
Exit()
312319

313320
; Restore backed up registry key

0 commit comments

Comments
 (0)