Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from Mantas-2155X/main
Browse files Browse the repository at this point in the history
Recode, port to Linux, quest 1 support
  • Loading branch information
AtlasTheProto authored Oct 12, 2021
2 parents 0368568 + 5efcefd commit 8cb7223
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 107 deletions.
13 changes: 0 additions & 13 deletions ADBForwarder.csproj

This file was deleted.

6 changes: 5 additions & 1 deletion ADBForwarder.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31521.260
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ADBForwarder", "ADBForwarder.csproj", "{AC3AABF6-EF23-42CC-B24B-FD0E8EA4640E}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ADBForwarder", "ADBForwarder\ADBForwarder.csproj", "{D61008B2-6278-4C15-9F4B-D4E229573D4E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +15,10 @@ Global
{AC3AABF6-EF23-42CC-B24B-FD0E8EA4640E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC3AABF6-EF23-42CC-B24B-FD0E8EA4640E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC3AABF6-EF23-42CC-B24B-FD0E8EA4640E}.Release|Any CPU.Build.0 = Release|Any CPU
{D61008B2-6278-4C15-9F4B-D4E229573D4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D61008B2-6278-4C15-9F4B-D4E229573D4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D61008B2-6278-4C15-9F4B-D4E229573D4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D61008B2-6278-4C15-9F4B-D4E229573D4E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
88 changes: 88 additions & 0 deletions ADBForwarder/ADBForwarder.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D61008B2-6278-4C15-9F4B-D4E229573D4E}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ADBForwarder</RootNamespace>
<AssemblyName>ADBForwarder</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<LangVersion>8</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="ICSharpCode.SharpZipLib, Version=1.3.3.11, Culture=neutral, PublicKeyToken=1b03e6acf1164f73">
<HintPath>..\packages\SharpZipLib.1.3.3\lib\net45\ICSharpCode.SharpZipLib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Extensions.Logging.Abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60">
<HintPath>..\packages\Microsoft.Extensions.Logging.Abstractions.2.0.0\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="mscorlib" />
<Reference Include="SharpAdbClient, Version=2.3.0.0, Culture=neutral, PublicKeyToken=d728076f5d82a2e8">
<HintPath>..\packages\SharpAdbClient.2.3.23\lib\net461\SharpAdbClient.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51">
<HintPath>..\packages\System.Buffers.4.5.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Drawing.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51">
<HintPath>..\packages\System.Drawing.Common.4.5.0\lib\net461\System.Drawing.Common.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="PostBuildUnix" AfterTargets="PostBuildEvent" Condition=" '$(OS)' == 'Unix' ">
<Exec Command="mkdir $(TargetDir)Libraries" />
<Exec Command="mv $(TargetDir)*.dll $(TargetDir)Libraries" />
<Exec Command="mv $(TargetDir)*.xml $(TargetDir)Libraries" />
<Exec Command="mv $(TargetDir)*.pdb $(TargetDir)Libraries" />
</Target>
<Target Name="PostBuildWindows" AfterTargets="PostBuildEvent" Condition=" '$(OS)' != 'Unix' ">
<Exec Command="mkdir $(TargetDir)Libraries" />
<Exec Command="move $(TargetDir)*.dll $(TargetDir)Libraries" />
<Exec Command="move $(TargetDir)*.xml $(TargetDir)Libraries" />
<Exec Command="move $(TargetDir)*.pdb $(TargetDir)Libraries" />
</Target>
</Project>
11 changes: 11 additions & 0 deletions ADBForwarder/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="Libraries;"/>
</assemblyBinding>
</runtime>
</configuration>
158 changes: 158 additions & 0 deletions ADBForwarder/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;

using SharpAdbClient;
using ICSharpCode.SharpZipLib.Zip;

namespace ADBForwarder
{
internal class Program
{
public const string VERSION = "0.2";

private static readonly string[] deviceNames =
{
"monterey", // Oculus Quest 1
"hollywood" // Oculus Quest 2
};

private static readonly AdbClient client = new AdbClient();
private static readonly AdbServer server = new AdbServer();
private static readonly IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, AdbClient.AdbServerPort);

private static void Main()
{
var currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (currentDirectory == null)
{
Console.WriteLine("Path error!");
return;
}

var adbPath = "adb/platform-tools/{0}";
var downloadUri = "https://dl.google.com/android/repository/platform-tools-latest-{0}.zip";

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Console.WriteLine("Platform: Linux");

adbPath = string.Format(adbPath, "adb");
downloadUri = string.Format(downloadUri, "linux");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Console.WriteLine("Platform: Windows");

adbPath = string.Format(adbPath, "adb.exe");
downloadUri = string.Format(downloadUri, "windows");
}
else
{
Console.WriteLine("Unsupported platform!");
return;
}

var absoluteAdbPath = Path.Combine(currentDirectory, adbPath);
if (!File.Exists(absoluteAdbPath))
{
Console.WriteLine("ADB not found, downloading in the background...");
DownloadADB(downloadUri);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
SetExecutable(absoluteAdbPath);
}

Console.WriteLine("Starting ADB Daemon...");
server.StartServer(absoluteAdbPath, false);

client.Connect(endPoint);

var monitor = new DeviceMonitor(new AdbSocket(endPoint));
monitor.DeviceConnected += Monitor_DeviceConnected;
monitor.DeviceDisconnected += Monitor_DeviceDisconnected;
monitor.Start();

while (true)
{
// Main thread needs to stay alive, 100ms is acceptable idle time
Thread.Sleep(100);
}
}

private static void Monitor_DeviceConnected(object sender, DeviceDataEventArgs e)
{
Console.WriteLine($"Connected device: {e.Device.Serial}");
Forward(e.Device);
}

private static void Monitor_DeviceDisconnected(object sender, DeviceDataEventArgs e)
{
Console.WriteLine($"Disconnected device: {e.Device.Serial}");
}

private static void Forward(DeviceData device)
{
// DeviceConnected calls without product set yet
Thread.Sleep(1000);

foreach (var deviceData in client.GetDevices().Where(deviceData => device.Serial == deviceData.Serial))
{
if (!deviceNames.Contains(deviceData.Product))
{
Console.WriteLine($"Skipped forwarding device: {(string.IsNullOrEmpty(deviceData.Product) ? deviceData.Serial : deviceData.Product)}");
return;
}

client.CreateForward(deviceData, 9943, 9943);
client.CreateForward(deviceData, 9944, 9944);

Console.WriteLine($"Successfully forwarded device: {deviceData.Serial} [{deviceData.Product}]");

return;
}
}

private static void DownloadADB(string downloadUri)
{
using var web = new WebClient();
web.DownloadFile(downloadUri, "adb.zip");
Console.WriteLine("Download successful");

var zip = new FastZip();
zip.ExtractZip("adb.zip", "adb", null);
Console.WriteLine("Extraction successful");

File.Delete("adb.zip");
}

private static void SetExecutable(string fileName)
{
Console.WriteLine("Giving adb executable permissions");

var args = $"chmod u+x {fileName}";
var escapedArgs = args.Replace("\"", "\\\"");

using var process = new Process
{
StartInfo = new ProcessStartInfo
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "/bin/bash",
Arguments = $"-c \"{escapedArgs}\""
}
};

process.Start();
process.WaitForExit();
}
}
}
35 changes: 35 additions & 0 deletions ADBForwarder/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ADBForwarder")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ADBForwarder")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("D61008B2-6278-4C15-9F4B-D4E229573D4E")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion(ADBForwarder.Program.VERSION)]
[assembly: AssemblyFileVersion(ADBForwarder.Program.VERSION)]
8 changes: 8 additions & 0 deletions ADBForwarder/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Extensions.Logging.Abstractions" version="2.0.0" targetFramework="net461" />
<package id="SharpAdbClient" version="2.3.23" targetFramework="net461" />
<package id="SharpZipLib" version="1.3.3" targetFramework="net461" />
<package id="System.Buffers" version="4.5.0" targetFramework="net461" />
<package id="System.Drawing.Common" version="4.5.0" targetFramework="net461" />
</packages>
Loading

0 comments on commit 8cb7223

Please sign in to comment.