Skip to content
This repository was archived by the owner on Sep 25, 2020. It is now read-only.

update to DSharpPlus 3.X.X #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions Emzi0767.AndroidBot/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.EventArgs;
using DSharpPlus.Net.WebSocket;
using ws4net = WebSocket4Net;
using s = System;

namespace DSharpPlus.Net.WebSocket
{
public class WebSocket4NetClient : BaseWebSocketClient
{
internal static UTF8Encoding UTF8 { get; } = new UTF8Encoding(false);
internal ws4net.WebSocket _socket;

public WebSocket4NetClient()
{
this._connect = new AsyncEvent(this.EventErrorHandler, "WS_CONNECT");
this._disconnect = new AsyncEvent<SocketCloseEventArgs>(this.EventErrorHandler, "WS_DISCONNECT");
this._message = new AsyncEvent<SocketMessageEventArgs>(this.EventErrorHandler, "WS_MESSAGE");
this._error = new AsyncEvent<SocketErrorEventArgs>(null, "WS_ERROR");
}

public override Task<BaseWebSocketClient> ConnectAsync(string uri)
{
_socket = new ws4net.WebSocket(uri);

_socket.Opened += (sender, e) => _connect.InvokeAsync().GetAwaiter().GetResult();

_socket.Closed += (sender, e) =>
{
var sock = (SocketCloseEventArgs)Activator.CreateInstance(typeof(SocketCloseEventArgs), null);

if (e is ws4net.ClosedEventArgs ea)
{
typeof(SocketCloseEventArgs).GetProperty("CloseCode").SetValue(sock, ea.Code);
typeof(SocketCloseEventArgs).GetProperty("CloseMessage").SetValue(sock, ea.Reason);
_disconnect.InvokeAsync(sock).GetAwaiter().GetResult();
}
else
{
typeof(SocketCloseEventArgs).GetProperty("CloseCode").SetValue(sock, -1);
typeof(SocketCloseEventArgs).GetProperty("CloseMessage").SetValue(sock, "unknown");
_disconnect.InvokeAsync(sock).GetAwaiter().GetResult();
}
};

_socket.MessageReceived += (sender, e) =>
{
var sock = (SocketMessageEventArgs)Activator.CreateInstance(typeof(SocketMessageEventArgs), null);
typeof(SocketCloseEventArgs).GetProperty("Message").SetValue(sock, e.Message);
_message.InvokeAsync(sock).GetAwaiter().GetResult();
};

_socket.DataReceived += (sender, e) =>
{
var msg = "";

using (var ms1 = new MemoryStream(e.Data, 2, e.Data.Length - 2))
using (var ms2 = new MemoryStream())
{
using (var zlib = new DeflateStream(ms1, CompressionMode.Decompress))
zlib.CopyTo(ms2);

msg = UTF8.GetString(ms2.ToArray(), 0, (int)ms2.Length);
}

var sock = (SocketMessageEventArgs)Activator.CreateInstance(typeof(SocketMessageEventArgs), null);
typeof(SocketCloseEventArgs).GetProperty("Message").SetValue(sock, msg);
_message.InvokeAsync(sock).GetAwaiter().GetResult();
};

_socket.Open();

return Task.FromResult<BaseWebSocketClient>(this);
}

public override Task InternalDisconnectAsync(SocketCloseEventArgs e)
{
if (_socket.State != ws4net.WebSocketState.Closed)
_socket.Close();
return Task.Delay(0);
}

public override Task<BaseWebSocketClient> OnConnectAsync()
{
return Task.FromResult<BaseWebSocketClient>(this);
}

public override Task<BaseWebSocketClient> OnDisconnectAsync(SocketCloseEventArgs e)
{
return Task.FromResult<BaseWebSocketClient>(this);
}

public override void SendMessage(string message)
{
if (_socket.State == ws4net.WebSocketState.Open)
_socket.Send(message);
}

public override event AsyncEventHandler OnConnect
{
add => this._connect.Register(value);
remove => this._connect.Unregister(value);
}
private AsyncEvent _connect;

public override event AsyncEventHandler<SocketCloseEventArgs> OnDisconnect
{
add => this._disconnect.Register(value);
remove => this._disconnect.Unregister(value);
}
private AsyncEvent<SocketCloseEventArgs> _disconnect;

public override event AsyncEventHandler<SocketMessageEventArgs> OnMessage
{
add => this._message.Register(value);
remove => this._message.Unregister(value);
}
private AsyncEvent<SocketMessageEventArgs> _message;

public override event AsyncEventHandler<SocketErrorEventArgs> OnError
{
add => this._error.Register(value);
remove => this._error.Unregister(value);
}
private AsyncEvent<SocketErrorEventArgs> _error;

private void EventErrorHandler(string evname, Exception ex)
{
if (evname.ToLowerInvariant() == "ws_error")
{
Console.WriteLine($"WSERROR: {ex.GetType()} in {evname}!");
}
else
{
var sock = (SocketErrorEventArgs)Activator.CreateInstance(typeof(SocketErrorEventArgs), null);
typeof(SocketCloseEventArgs).GetProperty("Exception").SetValue(sock, ex);
this._error.InvokeAsync(sock).GetAwaiter().GetResult();
}
}
}
}
27 changes: 18 additions & 9 deletions Emzi0767.AndroidBot/Emzi0767.AndroidBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v7.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v6.0</TargetFrameworkVersion>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>True</DebugSymbols>
Expand Down Expand Up @@ -52,20 +54,20 @@
<AndroidEnableSGenConcurrent>true</AndroidEnableSGenConcurrent>
</PropertyGroup>
<ItemGroup>
<Reference Include="DSharpPlus, Version=2.1.9.8, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DSharpPlus.2.1.9-rc-00008\lib\netstandard1.3\DSharpPlus.dll</HintPath>
<Reference Include="DSharpPlus, Version=3.2.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DSharpPlus.3.2.3\lib\netstandard2.0\DSharpPlus.dll</HintPath>
</Reference>
<Reference Include="DSharpPlus.CommandsNext, Version=2.1.9.8, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DSharpPlus.CommandsNext.2.1.9-rc-00008\lib\netstandard1.3\DSharpPlus.CommandsNext.dll</HintPath>
<Reference Include="DSharpPlus.CommandsNext, Version=3.2.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DSharpPlus.CommandsNext.3.2.3\lib\netstandard2.0\DSharpPlus.CommandsNext.dll</HintPath>
</Reference>
<Reference Include="DSharpPlus.Interactivity, Version=2.1.9.8, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DSharpPlus.Interactivity.2.1.9-rc-00008\lib\netstandard1.3\DSharpPlus.Interactivity.dll</HintPath>
<Reference Include="DSharpPlus.Interactivity, Version=3.2.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DSharpPlus.Interactivity.3.2.3\lib\netstandard2.0\DSharpPlus.Interactivity.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Mono.Android" />
<Reference Include="mscorlib" />
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.2\lib\netstandard1.3\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\netstandard1.3\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -78,12 +80,12 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="LogItemAdapter.cs" />
<Compile Include="MainActivity.cs" />
<Compile Include="PortableCommands.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WebSocket4NetClient.cs" />
</ItemGroup>
<ItemGroup>
<None Include="GettingStarted.Xamarin" />
Expand Down Expand Up @@ -145,6 +147,13 @@
<Folder Include="Resources\drawable\" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<Import Project="..\packages\NETStandard.Library.2.0.1\build\netstandard2.0\NETStandard.Library.targets" Condition="Exists('..\packages\NETStandard.Library.2.0.1\build\netstandard2.0\NETStandard.Library.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\NETStandard.Library.2.0.1\build\netstandard2.0\NETStandard.Library.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NETStandard.Library.2.0.1\build\netstandard2.0\NETStandard.Library.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
155 changes: 155 additions & 0 deletions Emzi0767.AndroidBot/Emzi0767.AndroidBot.csproj.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{8852D3A1-6D88-4C7D-9E08-41D5EA98DB2A}</ProjectGuid>
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Emzi0767.AndroidBot</RootNamespace>
<AssemblyName>Emzi0767.AndroidBot</AssemblyName>
<FileAlignment>512</FileAlignment>
<AndroidApplication>True</AndroidApplication>
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v7.1</TargetFrameworkVersion>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>True</DebugSymbols>
<DebugType>Full</DebugType>
<Optimize>False</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidUseSharedRuntime>true</AndroidUseSharedRuntime>
<AndroidLinkMode>None</AndroidLinkMode>
<EmbedAssembliesIntoApk>false</EmbedAssembliesIntoApk>
<AndroidEnableSGenConcurrent>true</AndroidEnableSGenConcurrent>
<AotAssemblies>false</AotAssemblies>
<EnableLLVM>false</EnableLLVM>
<BundleAssemblies>false</BundleAssemblies>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>PdbOnly</DebugType>
<DebugSymbols>True</DebugSymbols>
<Optimize>True</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
<AndroidLinkMode>SdkOnly</AndroidLinkMode>
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
<AotAssemblies>false</AotAssemblies>
<EnableLLVM>false</EnableLLVM>
<BundleAssemblies>false</BundleAssemblies>
<AndroidEnableSGenConcurrent>true</AndroidEnableSGenConcurrent>
</PropertyGroup>
<ItemGroup>
<Reference Include="DSharpPlus, Version=2.1.9.8, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DSharpPlus.2.1.9-rc-00008\lib\netstandard1.3\DSharpPlus.dll</HintPath>
</Reference>
<Reference Include="DSharpPlus.CommandsNext, Version=2.1.9.8, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DSharpPlus.CommandsNext.2.1.9-rc-00008\lib\netstandard1.3\DSharpPlus.CommandsNext.dll</HintPath>
</Reference>
<Reference Include="DSharpPlus.Interactivity, Version=2.1.9.8, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DSharpPlus.Interactivity.2.1.9-rc-00008\lib\netstandard1.3\DSharpPlus.Interactivity.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Mono.Android" />
<Reference Include="mscorlib" />
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.2\lib\netstandard1.3\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Xml" />
<Reference Include="WebSocket4Net, Version=0.14.1.0, Culture=neutral, PublicKeyToken=eb4e154b696bf72a, processorArchitecture=MSIL">
<HintPath>..\packages\WebSocket4Net.0.14.1\lib\monoandroid23\WebSocket4Net.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="LogItemAdapter.cs" />
<Compile Include="MainActivity.cs" />
<Compile Include="PortableCommands.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WebSocket4NetClient.cs" />
</ItemGroup>
<ItemGroup>
<None Include="GettingStarted.Xamarin" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<None Include="Resources\AboutResources.txt" />
<None Include="Assets\AboutAssets.txt" />
<AndroidResource Include="Resources\layout\LogItem.axml">
<SubType>Designer</SubType>
</AndroidResource>
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\layout\Main.axml">
<SubType>Designer</SubType>
</AndroidResource>
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\values\Strings.xml">
<SubType>Designer</SubType>
</AndroidResource>
</ItemGroup>
<ItemGroup>
<None Include="Properties\AndroidManifest.xml">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-hdpi\Icon.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-mdpi\Icon.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-xhdpi\Icon.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-xxhdpi\Icon.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-xxxhdpi\Icon.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-hdpi\Iconmini.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-mdpi\Iconmini.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-xhdpi\Iconmini.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-xxhdpi\Iconmini.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-xxxhdpi\Iconmini.png" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\drawable\" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Loading