Skip to content

Commit

Permalink
Added: new logic for working with adb, separation of the download fun…
Browse files Browse the repository at this point in the history
…ction into a separate method
  • Loading branch information
Sergey004 committed Jan 2, 2024
1 parent 897ecc0 commit aba5c16
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 89 deletions.
186 changes: 101 additions & 85 deletions Quest2-VRC.Core/Modules/ADB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,90 +24,7 @@ public static bool StartADB(bool sender, bool receiver, string hostip, bool wire
{

Console.WriteLine("Make sure you connect the headset to your computer and turn on the controllers");
if (!AdbServer.Instance.GetStatus().IsRunning)
{
AdbServer server = new AdbServer();
try
{

bool exists = Directory.Exists("platform-tools"); // ADB Auto downloader
if (!exists)
{
Console.WriteLine("ADB directory does not exist, creating...");
Console.WriteLine("Downloading ADB");
var client = new WebClient();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) == true)
{
string uri = "https://dl.google.com/android/repository/platform-tools-latest-windows.zip";
string filename = "platform-tools-latest-windows.zip";
string extractPath = AppDomain.CurrentDomain.BaseDirectory;
client.DownloadFile(uri, filename);
ZipFile.ExtractToDirectory(filename, extractPath);
File.Delete(filename);
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) == true) // Really Mac OS?!
{
string uri = "https://dl.google.com/android/repository/platform-tools-latest-darwin.zip";
string filename = "platform-tools-latest-darwin.zip";
string extractPath = AppDomain.CurrentDomain.BaseDirectory;
client.DownloadFile(uri, filename);
ZipFile.ExtractToDirectory(filename, extractPath);
File.Delete(filename);
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) == true) // Well, I can't imagine who would use this lib under Linux and in Mac OS XD
{
string uri = "https://dl.google.com/android/repository/platform-tools-latest-linux.zip";
string filename = "platform-tools-latest-linux.zip";
string extractPath = AppDomain.CurrentDomain.BaseDirectory;
client.DownloadFile(uri, filename);
ZipFile.ExtractToDirectory(filename, extractPath);
File.Delete(filename);
}
Console.WriteLine("Download completed");
}
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) == true)
{
StartServerResult result = server.StartServer(@"platform-tools\adb.exe", false);
if (result != StartServerResult.Started)
{
Console.WriteLine("Can't start adb server, please try again");

return false;
}
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) == true)
{
StartServerResult result = server.StartServer(@"platform-tools/adb", false);
if (result != StartServerResult.Started)
{
Console.WriteLine("Can't start adb server, please try again");

return false;
}
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) == true)
{
StartServerResult result = server.StartServer(@"platform-tools/adb", false);
if (result != StartServerResult.Started)
{
Console.WriteLine("Can't start adb server, please try again");

return false;
}
}
}
catch (WebException)
{
Console.WriteLine("Unable to download ADB from Google servers, try again or download files manually https://developer.android.com/studio/releases/platform-tools, press any key to exit");

return false;
}

}
else
{
Console.WriteLine("ADB server is already running, no checks are required");
}

client = new AdbClient();
client.Connect(hostip);
Expand Down Expand Up @@ -187,6 +104,18 @@ public static void StartTCPIP()
process.StartInfo = startInfo;
process.Start();

}
public static void ForceKillADB()
{

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C platform-tools\\adb.exe kill-server";
process.StartInfo = startInfo;
process.Start();

}
public static string GetIP()
{
Expand Down Expand Up @@ -226,7 +155,93 @@ public static async Task<string> GetZeroConfIP()
return await Task.FromResult(deviceip);
}

public static void DownLoadADB()
{
if (!AdbServer.Instance.GetStatus().IsRunning)
{
AdbServer server = new AdbServer();
try
{

bool exists = Directory.Exists("platform-tools"); // ADB Auto downloader
if (!exists)
{
Console.WriteLine("ADB directory does not exist, creating...");
Console.WriteLine("Downloading ADB");
var client = new WebClient();

Check warning on line 171 in Quest2-VRC.Core/Modules/ADB.cs

View workflow job for this annotation

GitHub Actions / build

'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)

Check warning on line 171 in Quest2-VRC.Core/Modules/ADB.cs

View workflow job for this annotation

GitHub Actions / build

'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) == true)
{
string uri = "https://dl.google.com/android/repository/platform-tools-latest-windows.zip";
string filename = "platform-tools-latest-windows.zip";
string extractPath = AppDomain.CurrentDomain.BaseDirectory;
client.DownloadFile(uri, filename);
ZipFile.ExtractToDirectory(filename, extractPath);
File.Delete(filename);
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) == true) // Really Mac OS?!
{
string uri = "https://dl.google.com/android/repository/platform-tools-latest-darwin.zip";
string filename = "platform-tools-latest-darwin.zip";
string extractPath = AppDomain.CurrentDomain.BaseDirectory;
client.DownloadFile(uri, filename);
ZipFile.ExtractToDirectory(filename, extractPath);
File.Delete(filename);
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) == true) // Well, I can't imagine who would use this lib under Linux and in Mac OS XD
{
string uri = "https://dl.google.com/android/repository/platform-tools-latest-linux.zip";
string filename = "platform-tools-latest-linux.zip";
string extractPath = AppDomain.CurrentDomain.BaseDirectory;
client.DownloadFile(uri, filename);
ZipFile.ExtractToDirectory(filename, extractPath);
File.Delete(filename);
}
Console.WriteLine("Download completed");
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) == true)
{
StartServerResult result = server.StartServer(@"platform-tools\adb.exe", false);
if (result != StartServerResult.Started)
{
Console.WriteLine("Can't start adb server, please try again");


}
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) == true)
{
StartServerResult result = server.StartServer(@"platform-tools/adb", false);
if (result != StartServerResult.Started)
{
Console.WriteLine("Can't start adb server, please try again");


}
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) == true)
{
StartServerResult result = server.StartServer(@"platform-tools/adb", false);
if (result != StartServerResult.Started)
{
Console.WriteLine("Can't start adb server, please try again");


}
}
}
catch (WebException)
{
Console.WriteLine("Unable to download ADB from Google servers, try again or download files manually https://developer.android.com/studio/releases/platform-tools, press any key to exit");


}

}
else
{
Console.WriteLine("ADB server is already running, no checks are required");
}
}

public static void StopADB()
{
Expand All @@ -235,12 +250,13 @@ public static void StopADB()
try

{
client.KillAdb();

ForceKillADB();
Environment.Exit(1987);
}
catch
{
// IDK how thos works
// IDK how this works
}

}
Expand Down
3 changes: 0 additions & 3 deletions Quest2-VRC.Core/Services/Sender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public class Global
public static int gputempint;
public static float WifiInt;
public static bool LowHMDBat;



}

public static async void Run(bool wirlessmode, bool audioEnadled, bool disableerrmsg, string hostip)
Expand Down
9 changes: 8 additions & 1 deletion Quest2-VRC.GUI/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public MainWindow()
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.DARK;
materialSkinManager.ColorScheme = new ColorScheme(Primary.Amber800, Primary.Amber900, Primary.Cyan500, Accent.Cyan700, TextShade.WHITE);
if (File.Exists("platform-tools\\adb.exe") == false)
{
MessageBox.Show(resources.GetString("ADBNotFound"), resources.GetString("Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
ADB.DownLoadADB();
}

materialLabel5.Text = resources.GetString("Ready");

}
Expand Down Expand Up @@ -336,7 +342,7 @@ private async void materialSwitch1_CheckedChanged(object sender, EventArgs e)
materialSwitch1.Checked = false;
}


}
else if (dialogResult1 == DialogResult.No)
{
Expand All @@ -347,6 +353,7 @@ private async void materialSwitch1_CheckedChanged(object sender, EventArgs e)
ADB.StartTCPIP();
await Task.Delay(3000);
materialTextBox1.Text = ADB.GetIP();
MessageBox.Show(resources.GetString("USBDisCon"), resources.GetString("ADBInTCPIP"), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
Expand Down
6 changes: 6 additions & 0 deletions Quest2-VRC.GUI/MainWindow.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3101,4 +3101,10 @@ ZeroConf requires an additional program to be installed on Quest
<data name="Manualinput" xml:space="preserve">
<value>The input box has been unlocked for manual entry</value>
</data>
<data name="ADBNotFound" xml:space="preserve">
<value>adb.exe is not found in the "platform-tools" directory, adb will be loaded after pressing "OK" button, make sure your device is connected to the internet.</value>
</data>
<data name="USBDisCon" xml:space="preserve">
<value>Disconnect the device from the computer for the wireless connection to work properly.</value>
</data>
</root>
6 changes: 6 additions & 0 deletions Quest2-VRC.GUI/MainWindow.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3643,4 +3643,10 @@
<data name="Manualinput" xml:space="preserve">
<value></value>
</data>
<data name="ADBNotFound" xml:space="preserve">
<value></value>
</data>
<data name="USBDisCon" xml:space="preserve">
<value></value>
</data>
</root>
6 changes: 6 additions & 0 deletions Quest2-VRC.GUI/MainWindow.ru-RU.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3140,4 +3140,10 @@
<data name="Manualinput" xml:space="preserve">
<value>Строка ввода была разблокирована для ручного ввода</value>
</data>
<data name="ADBNotFound" xml:space="preserve">
<value>adb.exe не найден в каталоге "platform-tools", adb будет загружен после нажатия кнопки "OK", убедитесь, что ваше устройство подключено к Интернету.</value>
</data>
<data name="USBDisCon" xml:space="preserve">
<value>Для правильной работы беспроводного соединения отсоедините устройство от компьютера.</value>
</data>
</root>

0 comments on commit aba5c16

Please sign in to comment.