Skip to content

Commit d3d8660

Browse files
committed
Update adb based on DanTheMan ig, I didn't really check
1 parent 341c8b7 commit d3d8660

File tree

6 files changed

+238
-239
lines changed

6 files changed

+238
-239
lines changed

QuestAppVersionSwitcher/Adb/AdbServer.cs

Lines changed: 48 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,126 +3,104 @@
33
using System.IO;
44
using System.Threading;
55
using System.Threading.Tasks;
6-
using ComputerUtils.Android;
7-
using ComputerUtils.Android.Logging;
6+
using Application = Android.App.Application;
87

98
namespace DanTheMan827.OnDeviceADB
109
{
11-
1210
/// <summary>
1311
/// A class for managing the adb server.
1412
/// </summary>
1513
public class AdbServer : IDisposable
1614
{
17-
private static string? FilesDir => AndroidCore.context?.FilesDir?.Path;
18-
private static string? CacheDir => AndroidCore.context?.CacheDir?.Path;
19-
private static string? NativeLibsDir => AndroidCore.context.ApplicationInfo?.NativeLibraryDir;
15+
public static int AdbPort = 5037;
16+
public static AdbServer Instance { get; private set; } = new AdbServer();
17+
private static string FilesDir => Application.Context?.FilesDir?.Path ?? throw new Exception("Unable to determine application files path");
18+
private static string CacheDir => Application.Context?.CacheDir?.Path ?? throw new Exception("Unable to determine application cache path");
19+
private static string NativeLibsDir => Application.Context.ApplicationInfo?.NativeLibraryDir ?? throw new Exception("Unable to determine native libs path");
20+
private CancellationTokenSource? CancelToken { get; set; }
2021
private Process? ServerProcess { get; set; }
21-
public static AdbServer Instance { get; set; }
2222

2323
/// <summary>
2424
/// Path to the adb binary.
2525
/// </summary>
26-
public static string? AdbPath => NativeLibsDir != null ? Path.Combine(NativeLibsDir, "libadb.so") : null;
26+
public static string AdbPath => NativeLibsDir != null ? Path.Combine(NativeLibsDir, "libadb.so") : throw new Exception("Unable to determine adb path");
2727

2828
/// <summary>
2929
/// If the server is running
3030
/// </summary>
3131
public bool IsRunning => ServerProcess != null && !ServerProcess.HasExited;
3232

33-
public AdbServer()
33+
private AdbServer() { }
34+
private async Task StartServer()
3435
{
35-
Debug.Assert(FilesDir != null);
36-
Debug.Assert(CacheDir != null);
37-
Debug.Assert(NativeLibsDir != null);
38-
Debug.Assert(AdbPath != null);
39-
Instance = this;
40-
}
41-
private void StartServer(string arguments)
42-
{
43-
Thread t = new Thread(() =>
44-
{
45-
// Create and configure the ProcessStartInfo.
46-
var adbInfo = new ProcessStartInfo(AdbPath, arguments);
47-
adbInfo.WorkingDirectory = FilesDir;
48-
adbInfo.UseShellExecute = false;
49-
adbInfo.RedirectStandardOutput = true;
50-
adbInfo.RedirectStandardError = true;
51-
adbInfo.EnvironmentVariables["HOME"] = FilesDir;
52-
adbInfo.EnvironmentVariables["TMPDIR"] = CacheDir;
53-
54-
// Start the process
55-
Logger.Log("Starting adb server process from " + adbInfo.FileName + " with arguments " + adbInfo.Arguments);
56-
ServerProcess = Process.Start(adbInfo);
57-
58-
if (ServerProcess == null)
59-
{
60-
Logger.Log("Adb server failed to start", LoggingType.Error);
61-
}
62-
63-
// Wait for the server to exit
64-
while (!ServerProcess.StandardError.EndOfStream)
65-
{
66-
Logger.Log(ServerProcess.StandardError.ReadLine());
67-
}
68-
while (!ServerProcess.StandardOutput.EndOfStream)
69-
{
70-
Logger.Log(ServerProcess.StandardOutput.ReadLine());
71-
}
72-
ServerProcess.WaitForExit();
73-
// Log standard output and error
74-
Logger.Log("Adb server exited", LoggingType.Error);
75-
76-
// Dispose our variables.
77-
DisposeVariables(false);
78-
});
79-
t.Start();
80-
}
36+
// Asserts
37+
Debug.Assert(this.ServerProcess == null);
38+
Debug.Assert(this.CancelToken == null);
8139

82-
private void KillServer()
83-
{
84-
85-
if (ServerProcess != null && !ServerProcess.HasExited)
40+
// Create and configure the ProcessStartInfo.
41+
var adbInfo = new ProcessStartInfo(AdbPath, $"-P {AdbPort} server nodaemon");
42+
adbInfo.WorkingDirectory = FilesDir;
43+
adbInfo.RedirectStandardOutput = true;
44+
adbInfo.RedirectStandardError = true;
45+
adbInfo.EnvironmentVariables["HOME"] = FilesDir;
46+
adbInfo.EnvironmentVariables["TMPDIR"] = CacheDir;
47+
adbInfo.EnvironmentVariables["ADB_MDNS"] = "0";
48+
adbInfo.EnvironmentVariables["ADB_MDNS_AUTO_CONNECT"] = "";
49+
50+
// Start the process
51+
ServerProcess = Process.Start(adbInfo);
52+
53+
if (ServerProcess == null)
8654
{
87-
ServerProcess.Kill();
55+
throw new Exception("adb server failed to start");
8856
}
57+
58+
// Dispose any token source that may exist (there shouldn't be any)
59+
CancelToken?.Dispose();
60+
61+
// Create our CancellationTokenSource and register the process kill action.
62+
CancelToken = new CancellationTokenSource();
63+
CancelToken.Token.Register(this.ServerProcess.Kill);
64+
65+
// Wait for the server to exit
66+
ServerProcess.WaitForExit();
67+
68+
// Dispose our variables.
69+
DisposeVariables(false);
8970
}
9071

9172
private void DisposeVariables(bool attemptKill)
9273
{
9374
// Stop the server
9475
if (attemptKill && ServerProcess != null && !ServerProcess.HasExited)
9576
{
96-
KillServer();
77+
ServerProcess.Kill();
9778
}
9879

9980
// Cleanup the token and process
10081
ServerProcess?.Dispose();
10182
ServerProcess = null;
83+
84+
CancelToken?.Dispose();
85+
CancelToken = null;
10286
}
10387

10488
/// <summary>
10589
/// Starts the server if not already running.
10690
/// </summary>
107-
public void Start()
91+
public async void Start()
10892
{
10993
if (!IsRunning)
11094
{
111-
StartServer("server nodaemon");
95+
await StartServer();
11296
}
11397
}
11498

11599
/// <summary>
116100
/// Stops the server if running.
117101
/// </summary>
118-
public void Stop() => DisposeVariables(true);
102+
public void Stop(bool force = true) => DisposeVariables(force);
119103

120104
public void Dispose() => Stop();
121-
122-
public void Pair(string rPort, string rCode)
123-
{
124-
KillServer();
125-
StartServer("pair 127.0.0.1:" + rPort + " " + rCode);
126-
}
127105
}
128106
}

0 commit comments

Comments
 (0)