Skip to content

Commit 6e5af34

Browse files
committed
Revert "If the update fails during the upgrade, the update will be retried."
This reverts commit 9748fbb.
1 parent 8d1853e commit 6e5af34

File tree

3 files changed

+60
-86
lines changed

3 files changed

+60
-86
lines changed

v2rayN/AmazTool/Program.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,20 @@ private static void Main(string[] args)
77
{
88
if (args.Length == 0)
99
{
10-
Utils.WriteLine(Resx.Resource.Guidelines);
11-
Utils.Waiting(5);
10+
Console.WriteLine(Resx.Resource.Guidelines);
11+
Thread.Sleep(5000);
1212
return;
1313
}
1414

1515
var argData = Uri.UnescapeDataString(string.Join(" ", args));
1616
if (argData.Equals("rebootas"))
1717
{
18-
Utils.Waiting(1);
18+
Thread.Sleep(1000);
1919
Utils.StartV2RayN();
2020
return;
2121
}
22-
23-
var tryTimes = 0;
24-
UpgradeApp.Init();
25-
while (tryTimes++ < 3)
26-
{
27-
if (!UpgradeApp.Upgrade(argData))
28-
{
29-
continue;
30-
}
3122

32-
Utils.WriteLine(Resx.Resource.Restartv2rayN);
33-
Utils.Waiting(3);
34-
Utils.StartV2RayN();
35-
break;
36-
}
23+
UpgradeApp.Upgrade(argData);
3724
}
3825
}
3926
}

v2rayN/AmazTool/UpgradeApp.cs

Lines changed: 54 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,44 @@ namespace AmazTool
66
{
77
internal class UpgradeApp
88
{
9-
public static bool Upgrade(string fileName)
9+
public static void Upgrade(string fileName)
1010
{
11-
Utils.WriteLine($"{Resx.Resource.StartUnzipping}\n{fileName}");
11+
Console.WriteLine($"{Resx.Resource.StartUnzipping}\n{fileName}");
12+
13+
Utils.Waiting(5);
1214

1315
if (!File.Exists(fileName))
1416
{
15-
Utils.WriteLine(Resx.Resource.UpgradeFileNotFound);
16-
return false;
17+
Console.WriteLine(Resx.Resource.UpgradeFileNotFound);
18+
return;
1719
}
1820

19-
Utils.Waiting(5);
20-
21-
KillV2rayN();
21+
Console.WriteLine(Resx.Resource.TryTerminateProcess);
22+
try
23+
{
24+
var existing = Process.GetProcessesByName(Utils.V2rayN);
25+
foreach (var pp in existing)
26+
{
27+
var path = pp.MainModule?.FileName ?? "";
28+
if (path.StartsWith(Utils.GetPath(Utils.V2rayN)))
29+
{
30+
pp?.Kill();
31+
pp?.WaitForExit(1000);
32+
}
33+
}
34+
}
35+
catch (Exception ex)
36+
{
37+
// Access may be denied without admin right. The user may not be an administrator.
38+
Console.WriteLine(Resx.Resource.FailedTerminateProcess + ex.StackTrace);
39+
}
2240

23-
Utils.WriteLine(Resx.Resource.StartUnzipping);
41+
Console.WriteLine(Resx.Resource.StartUnzipping);
2442
StringBuilder sb = new();
2543
try
2644
{
45+
var thisAppOldFile = $"{Utils.GetExePath()}.tmp";
46+
File.Delete(thisAppOldFile);
2747
var splitKey = "/";
2848

2949
using var archive = ZipFile.OpenRead(fileName);
@@ -36,7 +56,7 @@ public static bool Upgrade(string fileName)
3656
continue;
3757
}
3858

39-
Utils.WriteLine(entry.FullName);
59+
Console.WriteLine(entry.FullName);
4060

4161
var lst = entry.FullName.Split(splitKey);
4262
if (lst.Length == 1)
@@ -45,80 +65,53 @@ public static bool Upgrade(string fileName)
4565
}
4666

4767
var fullName = string.Join(splitKey, lst[1..lst.Length]);
68+
69+
if (string.Equals(Utils.GetExePath(), Utils.GetPath(fullName), StringComparison.OrdinalIgnoreCase))
70+
{
71+
File.Move(Utils.GetExePath(), thisAppOldFile);
72+
}
73+
4874
var entryOutputPath = Utils.GetPath(fullName);
4975
Directory.CreateDirectory(Path.GetDirectoryName(entryOutputPath)!);
5076
//In the bin folder, if the file already exists, it will be skipped
5177
if (fullName.StartsWith("bin") && File.Exists(entryOutputPath))
5278
{
5379
continue;
5480
}
55-
entry.ExtractToFile(entryOutputPath, true);
5681

57-
Utils.WriteLine(entryOutputPath);
82+
try
83+
{
84+
entry.ExtractToFile(entryOutputPath, true);
85+
}
86+
catch
87+
{
88+
Thread.Sleep(1000);
89+
entry.ExtractToFile(entryOutputPath, true);
90+
}
91+
92+
Console.WriteLine(entryOutputPath);
5893
}
5994
catch (Exception ex)
6095
{
61-
sb.Append(ex.Message);
6296
sb.Append(ex.StackTrace);
6397
}
6498
}
6599
}
66100
catch (Exception ex)
67101
{
68-
sb.Append(Resx.Resource.FailedUpgrade + ex.StackTrace);
102+
Console.WriteLine(Resx.Resource.FailedUpgrade + ex.StackTrace);
103+
//return;
69104
}
70-
71-
if (sb.Length <= 0)
105+
if (sb.Length > 0)
72106
{
73-
return true;
107+
Console.WriteLine(Resx.Resource.FailedUpgrade + sb.ToString());
108+
//return;
74109
}
75110

76-
Utils.WriteLine(sb.ToString());
77-
Utils.WriteLine(Resx.Resource.FailedUpgrade);
78-
return false;
79-
}
80-
81-
public static bool Init()
82-
{
83-
//Process temporary files generated by the last update
84-
var files = Directory.GetFiles(Utils.GetPath(""), "*.tmp");
85-
foreach (var file in files)
86-
{
87-
if (file.Contains(Utils.AmazTool))
88-
{
89-
File.Delete(file);
90-
}
91-
}
92-
93-
var destFileName = $"{Utils.GetExePath()}{Guid.NewGuid().ToString("N")[..8]}.tmp";
94-
File.Move(Utils.GetExePath(), destFileName);
95-
96-
return true;
97-
}
98-
99-
private static bool KillV2rayN()
100-
{
101-
Utils.WriteLine(Resx.Resource.TryTerminateProcess);
102-
try
103-
{
104-
var existing = Process.GetProcessesByName(Utils.V2rayN);
105-
foreach (var pp in existing)
106-
{
107-
var path = pp.MainModule?.FileName ?? "";
108-
if (path.StartsWith(Utils.GetPath(Utils.V2rayN)))
109-
{
110-
pp?.Kill();
111-
pp?.WaitForExit(1000);
112-
}
113-
}
114-
}
115-
catch (Exception ex)
116-
{
117-
// Access may be denied without admin right. The user may not be an administrator.
118-
Utils.WriteLine(Resx.Resource.FailedTerminateProcess + ex.StackTrace);
119-
}
111+
Console.WriteLine(Resx.Resource.Restartv2rayN);
112+
Utils.Waiting(2);
120113

121-
return true;
114+
Utils.StartV2RayN();
122115
}
123116
}
124117
}

v2rayN/AmazTool/Utils.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static string StartupPath()
1414
return AppDomain.CurrentDomain.BaseDirectory;
1515
}
1616

17-
public static string GetPath(string? fileName)
17+
public static string GetPath(string fileName)
1818
{
1919
var startupPath = StartupPath();
2020
if (string.IsNullOrEmpty(fileName))
@@ -25,7 +25,6 @@ public static string GetPath(string? fileName)
2525
}
2626

2727
public static string V2rayN => "v2rayN";
28-
public static string AmazTool => "AmazTool";
2928

3029
public static void StartV2RayN()
3130
{
@@ -45,14 +44,9 @@ public static void Waiting(int second)
4544
{
4645
for (var i = second; i > 0; i--)
4746
{
48-
Utils.WriteLine(i);
47+
Console.WriteLine(i);
4948
Thread.Sleep(1000);
5049
}
5150
}
52-
53-
public static void WriteLine(object obj)
54-
{
55-
Console.WriteLine(obj);
56-
}
5751
}
5852
}

0 commit comments

Comments
 (0)