Commit c954a538 authored by ZeRo's avatar ZeRo

Add project files.

parent a9bc72d2

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27428.2037
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiscordRPCHTTP", "DiscordRPCHTTP\DiscordRPCHTTP.csproj", "{5FC02F80-F251-4EB1-8B02-FBD901F5BA8A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5FC02F80-F251-4EB1-8B02-FBD901F5BA8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5FC02F80-F251-4EB1-8B02-FBD901F5BA8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5FC02F80-F251-4EB1-8B02-FBD901F5BA8A}.Debug|x64.ActiveCfg = Debug|x64
{5FC02F80-F251-4EB1-8B02-FBD901F5BA8A}.Debug|x64.Build.0 = Debug|x64
{5FC02F80-F251-4EB1-8B02-FBD901F5BA8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5FC02F80-F251-4EB1-8B02-FBD901F5BA8A}.Release|Any CPU.Build.0 = Release|Any CPU
{5FC02F80-F251-4EB1-8B02-FBD901F5BA8A}.Release|x64.ActiveCfg = Release|x64
{5FC02F80-F251-4EB1-8B02-FBD901F5BA8A}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {652BACA0-7B55-44AE-B7C2-E708F256C44C}
EndGlobalSection
EndGlobal
<?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">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DiscordRpc;
using System.Threading;
using System.Diagnostics;
namespace DiscordRPCHTTP
{
class DiscordRPC
{
private Program program;
private static RichPresence Presence;
private string ClientID = null;
private bool Cancel = false;
public DiscordRPC(Program parent)
{
this.program = parent;
}
public void SetActivity(string clientid, json.RPCCommand command)
{
if (Presence == null || this.ClientID != clientid)
{
if (Presence != null)
{
Presence.Shutdown();
Thread.Sleep(3000);
Presence.Dispose();
}
this.ClientID = clientid;
Presence = new RichPresence();
Presence.Initialize(clientid);
Presence.Ready += () => { Presence_Ready(command); };
Presence.Disconnected += Presence_Disconnected;
Presence.Errored += Presence_Errored;
var callbackTask = RunCallbacks();
return;
}
Presence_Ready(command);
}
private void Presence_Errored(int errorCode, string message)
{
}
private void Presence_Disconnected(int errorCode, string message)
{
}
private void Presence_Ready(json.RPCCommand command)
{
try
{
RichPresenceBuilder builder = new RichPresenceBuilder();
DateTimeOffset start = DateTimeOffset.FromUnixTimeSeconds(command.args.activity.timestamps.start);
DateTimeOffset end = DateTimeOffset.FromUnixTimeSeconds(command.args.activity.timestamps.end);
builder.WithState(command.args.activity.state, command.args.activity.details);
builder.WithArtwork(command.args.activity.assets.large_image, command.args.activity.assets.large_text, command.args.activity.assets.small_image, command.args.activity.assets.small_text);
builder.WithTimeInfo(start.UtcDateTime, end.UtcDateTime);
Presence.Update(builder);
Log.WriteLog("Updated Presence");
}
catch (Exception e)
{
Log.WriteLog("Failed to update presence: " + e.Message);
}
}
private async Task RunCallbacks()
{
await Task.Run(() =>
{
while (!Cancel)
{
Presence.RunCallbacks();
Thread.Sleep(1000);
}
});
}
public void RPCShutdown()
{
//This is a hack because RPC takes THIS PID and we can't specify the games...
//I call it SHITSCORD for a reason.
Thread thr = new Thread(new ThreadStart(() =>
{
Thread.Sleep(1000);
Environment.Exit(0x543);
}));
thr.Start();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" 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>{5FC02F80-F251-4EB1-8B02-FBD901F5BA8A}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>DiscordRPCHTTP</RootNamespace>
<AssemblyName>DiscordRPCHTTP</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</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>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="DiscordRpc">
<HintPath>lib\DiscordRpc.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="RegexMatcher, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\RegexMatcher.1.0.0\lib\RegexMatcher.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Core" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="Watson, Version=1.2.11.1, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Watson.1.2.11\lib\Watson.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="DiscordRPC.cs" />
<Compile Include="GmodJsonConvert.cs" />
<Compile Include="HTTP.cs" />
<Compile Include="json\RPCCommand.cs" />
<Compile Include="json\StandardResponse.cs" />
<Compile Include="Log.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Service.cs">
<SubType>Component</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace DiscordRPCHTTP
{
//Copied from WarZone Inventory server, because according to Willox, integers should be converted to floats for JSON? :thinking: This library disagrees.
class GmodJsonConvert : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JValue jsonValue = serializer.Deserialize<JValue>(reader);
if (jsonValue.Type == JTokenType.Float)
{
return Convert.ToInt32(jsonValue.Value<double>());
}
else if (jsonValue.Type == JTokenType.Integer)
{
return jsonValue.Value<int>();
}
throw new FormatException();
}
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(int));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using WatsonWebserver;
using Newtonsoft.Json;
using System.Collections.Specialized;
namespace DiscordRPCHTTP
{
class RPCHTTP
{
private Program program;
private Server HTTPD;
public RPCHTTP(Program parent)
{
this.program = parent;
}
public void Start()
{
Log.WriteLog("Starting Discord RPC over HTTP");
HTTPD = new Server("127.0.0.1", 9000, false, DefaultRoute, false);
HTTPD.AddStaticRoute("post", "/rpc", RPCHandler);
HTTPD.AddStaticRoute("get", "/shutdown", ShutdownHandler);
}
private HttpResponse DefaultRoute(HttpRequest req)
{
json.StandardResponse resp = new json.StandardResponse();
resp.code = 0;
resp.message = "Authorization Required";
string reply = JsonConvert.SerializeObject(resp);
return new HttpResponse(req, false, 404, null, "application/json", reply, true);
}
private HttpResponse RPCHandler(HttpRequest req)
{
try
{
Dictionary<string, string> GETVars = req.QuerystringEntries;
if (!GETVars.ContainsKey("v"))
{
return new HttpResponse(req, false, 500, null, "application/json", StandardResponse(10, "RPC Version not specified"), true);
}
if (!GETVars.ContainsKey("client_id"))
{
return new HttpResponse(req, false, 500, null, "application/json", StandardResponse(10, "client_id not specified"), true);
}
string ClientID = GETVars["client_id"];
Console.WriteLine("Starting RPC with: {0}", ClientID);
byte[] data = req.Data;
string content = Encoding.UTF8.GetString(data);
JsonSerializerSettings settings = new JsonSerializerSettings
{
Converters = new List<JsonConverter> { new GmodJsonConvert() }
};
json.RPCCommand command = JsonConvert.DeserializeObject<json.RPCCommand>(content, settings);
program.DiscordRPC.SetActivity(ClientID, command);
return new HttpResponse(req, false, 500, null, "application/json", StandardResponse(200, "Done"), true);
}
catch (Exception e)
{
Log.WriteLog("Failed to process RPC Handler: " + e.Message);
return null;
}
}
private HttpResponse ShutdownHandler(HttpRequest req)
{
try
{
program.DiscordRPC.RPCShutdown();
return new HttpResponse(req, false, 500, null, "application/json", StandardResponse(200, "Done"), true);
}
catch (Exception e)
{
Log.WriteLog("Failed to process Shutdown Handler: " + e.Message);
return null;
}
}
private string StandardResponse(int code, string message)
{
json.StandardResponse resp = new json.StandardResponse();
resp.code = code;
resp.message = message;
string reply = JsonConvert.SerializeObject(resp);
return reply;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace DiscordRPCHTTP
{
public class Log
{
public static void WriteLog(string text)
{
string line = string.Format("[Discord RPC HTTP] {0} - {1}", DateTime.Now, text);
Console.WriteLine(line);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceProcess;
using System.Threading;
namespace DiscordRPCHTTP
{
class Program
{
public const string ServiceName = "DiscordRPCHTTP";
public RPCHTTP HTTPServer;
public DiscordRPC DiscordRPC;
public ManualResetEvent mre = new ManualResetEvent(false);
//"Let's remove HTTP because it didn't quite work properly"
//Fuck off.
static void Main(string[] args)
{
if (Environment.UserInteractive)
{
Console.Title = "Discord RPC over HTTP";
Log.WriteLog("Running in User Interactive Mode");
Program program = new Program();
program.Start();
}
else
{
using (Service svc = new Service())
{
ServiceBase.Run(svc);
}
}
}
public static void ServiceMain(string[] args)
{
Log.WriteLog("Running in Service Mode");
Thread svcThread = new Thread(new ThreadStart(() =>
{
Program program = new Program();
program.Start();
}));
svcThread.Start();
}
public static void ServiceStop()
{
}
public void Start()
{
HTTPServer = new RPCHTTP(this);
HTTPServer.Start();
DiscordRPC = new DiscordRPC(this);
mre.WaitOne();
}
public void Stop()
{
}
}
}
using System.Reflection;
using System.Runtime.CompilerServices;
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("DiscordRPCHTTP")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DiscordRPCHTTP")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[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("5fc02f80-f251-4eb1-8b02-fbd901f5ba8a")]
// 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("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceProcess;
namespace DiscordRPCHTTP
{
class Service: ServiceBase
{
public Service()
{
ServiceName = Program.ServiceName;
}
protected override void OnStart(string[] args)
{
Program.ServiceMain(args);
}
protected override void OnStop()
{
Program.ServiceStop();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DiscordRPCHTTP.json
{
class RPCCommand
{
public string cmd = "NONE";
public RPCCommandArgs args = new RPCCommandArgs();
public float nonce = 0;
}
class RPCCommandArgs
{
public int pid = 0;
public RPCActivity activity = new RPCActivity();
}
class RPCActivity
{
public string details = "";
public string state = "";
public RPCTimestamps timestamps = new RPCTimestamps();
public RPCAssets assets = new RPCAssets();
}
class RPCAssets
{
public string large_image = "";
public string large_text = "";
public string small_image = "";
public string small_text = "";
}
class RPCTimestamps
{
public int start = 0;
public int end = 0;
}
/*class RPCResponse
{
string cmd = "NONE";
StandardResponse data = new StandardResponse();
float nonce = 0;
}*/
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DiscordRPCHTTP.json
{
class StandardResponse
{
public int code = 0;
public string message = "None";
}
}
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net461" />
<package id="RegexMatcher" version="1.0.0" targetFramework="net461" />
<package id="Watson" version="1.2.11" targetFramework="net461" />
</packages>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment