forked from Vencord/Installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_discord_windows.go
151 lines (130 loc) · 3.14 KB
/
find_discord_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* SPDX-License-Identifier: GPL-3.0
* Vencord Installer, a cross platform gui/cli app for installing Vencord
* Copyright (c) 2023 Vendicated and Vencord contributors
*/
package main
import (
"errors"
"golang.org/x/sys/windows"
"os"
path "path/filepath"
"strings"
"sync"
"unsafe"
)
var windowsNames = map[string]string{
"stable": "Discord",
"ptb": "DiscordPTB",
"canary": "DiscordCanary",
"dev": "DiscordDevelopment",
}
var killLock sync.Mutex
func ParseDiscord(p, branch string) *DiscordInstall {
entries, err := os.ReadDir(p)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
Log.Warn("Error during readdir "+p+":", err)
}
return nil
}
isPatched := false
appPath := ""
for _, dir := range entries {
if dir.IsDir() && strings.HasPrefix(dir.Name(), "app-") {
resources := path.Join(p, dir.Name(), "resources")
if !ExistsFile(resources) {
continue
}
app := path.Join(resources, "app")
if app > appPath {
appPath = app
isPatched = ExistsFile(path.Join(resources, "_app.asar"))
}
}
}
if appPath == "" {
return nil
}
if branch == "" {
branch = GetBranch(p)
}
return &DiscordInstall{
path: p,
branch: branch,
appPath: appPath,
isPatched: isPatched,
isFlatpak: false,
isSystemElectron: false,
}
}
func FindDiscords() []any {
var discords []any
appData := os.Getenv("LOCALAPPDATA")
if appData == "" {
Log.Error("%LOCALAPPDATA% is empty???????")
return discords
}
for branch, dirname := range windowsNames {
p := path.Join(appData, dirname)
if discord := ParseDiscord(p, branch); discord != nil {
Log.Debug("Found Discord install at ", p)
discords = append(discords, discord)
}
}
return discords
}
func PreparePatch(di *DiscordInstall) {
killLock.Lock()
defer killLock.Unlock()
name := windowsNames[di.branch]
Log.Debug("Trying to kill", name)
pid := findProcessIdByName(name + ".exe")
if pid == 0 {
Log.Debug("Didn't find process matching name")
return
}
proc, err := os.FindProcess(int(pid))
if err != nil {
Log.Warn("Failed to find process with pid", pid)
return
}
err = proc.Kill()
if err != nil {
Log.Warn("Failed to kill", name+":", err)
} else {
Log.Debug("Waiting for", name, "to exit")
_, _ = proc.Wait()
}
}
func FixOwnership(_ string) error {
return nil
}
// https://github.com/Vencord/Installer/issues/9
func CheckScuffedInstall() bool {
username := os.Getenv("USERNAME")
programData := os.Getenv("PROGRAMDATA")
for _, discordName := range windowsNames {
if ExistsFile(path.Join(programData, username, discordName)) || ExistsFile(path.Join(programData, username, discordName)) {
HandleScuffedInstall()
return true
}
}
return false
}
func findProcessIdByName(name string) uint32 {
snapshot, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
if err != nil {
return 0
}
procEntry := windows.ProcessEntry32{Size: uint32(unsafe.Sizeof(windows.ProcessEntry32{}))}
for {
err = windows.Process32Next(snapshot, &procEntry)
if err != nil {
return 0
}
if windows.UTF16ToString(procEntry.ExeFile[:]) == name {
return procEntry.ProcessID
}
}
}