返回 DeepSeek-Reasonix
version.go
1 //go:build windows && !native_webview2loader
2
3 package webviewloader
4
5 import (
6 "errors"
7 "fmt"
8 "strconv"
9 "strings"
10 )
11
12 // UsingGoWebview2Loader is set to true when the go webview2loader is used.
13 var UsingGoWebview2Loader bool
14
15 // CompareBrowserVersions will compare the 2 given versions and return:
16 //
17 // -1 = v1 < v2
18 // 0 = v1 == v2
19 // 1 = v1 > v2
20 func CompareBrowserVersions(v1 string, v2 string) (int, error) {
21 v, err := parseVersion(v1)
22 if err != nil {
23 return 0, fmt.Errorf("v1 invalid: %w", err)
24 }
25
26 w, err := parseVersion(v2)
27 if err != nil {
28 return 0, fmt.Errorf("v2 invalid: %w", err)
29 }
30
31 return v.compare(w), nil
32 }
33
34 // GetAvailableCoreWebView2BrowserVersionString get the browser version info including channel name
35 // if it is the WebView2 Runtime.
36 // Channel names are Beta, Dev, and Canary.
37 func GetAvailableCoreWebView2BrowserVersionString(browserExecutableFolder string) (string, error) {
38 if browserExecutableFolder != "" {
39 clientPath, err := findEmbeddedClientDll(browserExecutableFolder)
40 if errors.Is(err, errNoClientDLLFound) {
41 // WebView2 is not found
42 return "", nil
43 } else if err != nil {
44 return "", err
45 }
46
47 return findEmbeddedBrowserVersion(clientPath)
48 }
49
50 _, version, err := findInstalledClientDll(false)
51 if errors.Is(err, errNoClientDLLFound) {
52 return "", nil
53 } else if err != nil {
54 return "", err
55 }
56
57 return version.String(), nil
58 }
59
60 type version struct {
61 major int
62 minor int
63 patch int
64 build int
65
66 channel string
67 }
68
69 func (v version) String() string {
70 vv := fmt.Sprintf("%d.%d.%d.%d", v.major, v.minor, v.patch, v.build)
71 if v.channel != "" {
72 vv += " " + v.channel
73 }
74
75 return vv
76 }
77
78 func (v version) compare(o version) int {
79 if c := compareInt(v.major, o.major); c != 0 {
80 return c
81 }
82 if c := compareInt(v.minor, o.minor); c != 0 {
83 return c
84 }
85 if c := compareInt(v.patch, o.patch); c != 0 {
86 return c
87 }
88 return compareInt(v.build, o.build)
89 }
90
91 func parseVersion(v string) (version, error) {
92 var p version
93
94 // Split away channel information...
95 if i := strings.Index(v, " "); i > 0 {
96 p.channel = v[i+1:]
97 v = v[:i]
98 }
99
100 vv := strings.Split(v, ".")
101 if len(vv) > 4 {
102 return p, fmt.Errorf("too many version parts")
103 }
104
105 var err error
106 vv, p.major, err = parseInt(vv)
107 if err != nil {
108 return p, fmt.Errorf("bad major version: %w", err)
109 }
110
111 vv, p.minor, err = parseInt(vv)
112 if err != nil {
113 return p, fmt.Errorf("bad minor version: %w", err)
114 }
115
116 vv, p.patch, err = parseInt(vv)
117 if err != nil {
118 return p, fmt.Errorf("bad patch version: %w", err)
119 }
120
121 _, p.build, err = parseInt(vv)
122 if err != nil {
123 return p, fmt.Errorf("bad build version: %w", err)
124 }
125
126 return p, nil
127 }
128
129 func parseInt(v []string) ([]string, int, error) {
130 if len(v) == 0 {
131 return nil, 0, nil
132 }
133
134 p, err := strconv.ParseInt(v[0], 10, 32)
135 if err != nil {
136 return nil, 0, err
137 }
138 return v[1:], int(p), nil
139 }
140
141 func compareInt(v1, v2 int) int {
142 if v1 == v2 {
143 return 0
144 }
145 if v1 < v2 {
146 return -1
147 } else {
148 return +1
149 }
150 }
151
151 lines GO