forked from StupidityDB/VencordPlusInstaller
-
Notifications
You must be signed in to change notification settings - Fork 1
/
openasar.go
122 lines (100 loc) · 2.41 KB
/
openasar.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
/*
* 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 (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"os"
path "path/filepath"
"strconv"
)
const OpenAsarDownloadLink = "https://github.com/GooseMod/OpenAsar/releases/download/nightly/app.asar"
func FindAsarFile(dir string) (*os.File, error) {
for _, file := range []string{"app.asar", "_app.asar"} {
f, err := os.Open(path.Join(dir, file))
if err != nil {
continue
}
stats, err := f.Stat()
if err == nil && !stats.IsDir() {
return f, nil
}
_ = f.Close()
}
return nil, errors.New("Install at " + dir + " has no asar file")
}
func (di *DiscordInstall) IsOpenAsar() (retBool bool) {
if di.isOpenAsar != nil {
return *di.isOpenAsar
}
defer func() {
fmt.Println("Checking if", di.path, "is using OpenAsar:", retBool)
di.isOpenAsar = &retBool
}()
asarFile, err := FindAsarFile(path.Join(di.appPath, ".."))
if err != nil {
fmt.Println(err)
return false
}
b, err := io.ReadAll(asarFile)
_ = asarFile.Close()
if err != nil {
fmt.Println(err)
return false
}
if bytes.Contains(b, []byte("OpenAsar")) {
return true
}
return false
}
func (di *DiscordInstall) InstallOpenAsar() error {
PreparePatch(di)
dir := path.Join(di.appPath, "..")
asarFile, err := FindAsarFile(dir)
if err != nil {
return err
}
_ = asarFile.Close()
if err = os.Rename(asarFile.Name(), path.Join(dir, "app.asar.original")); err != nil {
return err
}
res, err := http.Get(OpenAsarDownloadLink)
if err != nil {
return err
} else if res.StatusCode >= 300 {
return errors.New("Failed to fetch OpenAsar - " + strconv.Itoa(res.StatusCode) + ": " + res.Status)
}
outFile, err := os.Create(asarFile.Name())
if err != nil {
return err
}
if _, err = io.Copy(outFile, res.Body); err != nil {
return err
}
di.isOpenAsar = Ptr(true)
return nil
}
func (di *DiscordInstall) UninstallOpenAsar() error {
PreparePatch(di)
dir := path.Join(di.appPath, "..")
originalAsar := path.Join(dir, "app.asar.original")
if !ExistsFile(originalAsar) {
return errors.New("No app.asar.original. Reinstall Discord")
}
asarFile, err := FindAsarFile(dir)
if err != nil {
return err
}
_ = asarFile.Close()
if err = os.Rename(originalAsar, asarFile.Name()); err != nil {
return err
}
di.isOpenAsar = Ptr(false)
return nil
}