diff --git a/.gitignore b/.gitignore index 9020994b..8383394b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,10 +4,11 @@ builds/ target/ release/ bin/ +.vs/ deps/craftbukkit.jar *~ *.lock *.DS_Store *.swp *.out -!*.dll +!*.elt diff --git a/sdk/DotNet Core/JsonApi.sln b/sdk/DotNet Core/JsonApi.sln new file mode 100644 index 00000000..ff2ca322 --- /dev/null +++ b/sdk/DotNet Core/JsonApi.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26430.4 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonApi", "JsonApi\JsonApi.csproj", "{7FCD017D-A07B-499B-936B-C37F50BD99C1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonApiTest", "JsonApiTest\JsonApiTest.csproj", "{52A0D2EC-33DA-49A1-9AC0-96D477F30327}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7FCD017D-A07B-499B-936B-C37F50BD99C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7FCD017D-A07B-499B-936B-C37F50BD99C1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7FCD017D-A07B-499B-936B-C37F50BD99C1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7FCD017D-A07B-499B-936B-C37F50BD99C1}.Release|Any CPU.Build.0 = Release|Any CPU + {52A0D2EC-33DA-49A1-9AC0-96D477F30327}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {52A0D2EC-33DA-49A1-9AC0-96D477F30327}.Debug|Any CPU.Build.0 = Debug|Any CPU + {52A0D2EC-33DA-49A1-9AC0-96D477F30327}.Release|Any CPU.ActiveCfg = Release|Any CPU + {52A0D2EC-33DA-49A1-9AC0-96D477F30327}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/sdk/DotNet Core/JsonApi/ApiHelper.cs b/sdk/DotNet Core/JsonApi/ApiHelper.cs new file mode 100644 index 00000000..837fe052 --- /dev/null +++ b/sdk/DotNet Core/JsonApi/ApiHelper.cs @@ -0,0 +1,143 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; +using System.Security.Cryptography; +using System.Text; + +namespace JsonApi +{ + public class ApiHelper + { + private const string UrlFormat = "http://{0}:{1}/api/2/call?json={2}"; + + public ApiHelper(string host, int port, string userName, string password, string salt = "") + { + _host = host; + Port = port; + _userName = userName; + _password = password; + _salt = salt; + } + + private readonly string _host; + private readonly string _userName; + private readonly string _password; + private readonly string _salt; + public readonly int Port; + + private string GetKey(string method) + { + return BitConverter.ToString( + SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(_userName + method + _password + _salt))) + .Replace("-", "").ToLower(); + } + + private string MakeApiUrl(string method, object[] args) + { + return string.Format(UrlFormat, _host, Port, UrlEncode(ConstructCall(method, args))); + } + + private string MakeApiUrl(string[] method, string[][] args) + { + return string.Format(UrlFormat, _host, Port, UrlEncode(ConstructCall(method, args))); + } + + private string ConstructCall(string method, object[] args) + { + var json = new + { + name = method, + arguments = args, + key = GetKey(method), + username = _userName + }; + + return JsonConvert.SerializeObject(json); + } + + private string ConstructCall(string[] method, string[][] args) + { + var allCalls = new List(); + for (var i = 0; i < method.Length; i++) + { + allCalls.Add( + new + { + name = method[i], + arguments = args[i], + key = GetKey(method[i]), + username = _userName + } + ); + } + + return JsonConvert.SerializeObject(allCalls); + } + + /// + /// Call a method on the server. + /// + /// The method to call. + /// Arguments to pass to the server. + /// + public string Call(string method, object[] args) + { + var url = MakeApiUrl(method, args); + + return Get(url); + } + + /// + /// Call multiple methods on the server. + /// + /// A string[] of methods to call on the server. + /// Arguments to pass to the server. + /// + public string Call(string[] method, string[][] args) + { + if (method.Length != args.Length) + { + throw new ArgumentOutOfRangeException(); + } + + var url = MakeApiUrl(method, args); + + return Get(url); + } + + private static string Get(string url) + { + var request = (HttpWebRequest)WebRequest.Create(url); + request.Method = "GET"; + request.ContentType = "text/html;charset=UTF-8"; + + var response = (HttpWebResponse)request.GetResponseAsync().Result; + var myResponseStream = response.GetResponseStream(); + var myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); + var retString = myStreamReader.ReadToEnd(); + myStreamReader.Dispose(); + myResponseStream.Dispose(); + + return retString; + } + + /// + /// UrlEncode + /// + /// + /// + private static string UrlEncode(string str) + { + var sb = new StringBuilder(); + var byStr = Encoding.UTF8.GetBytes(str); + for (var i = 0; i < byStr.Length; i++) + { + sb.Append(@"%" + Convert.ToString(i, 16)); + } + + return (sb.ToString()); + } + } +} diff --git a/sdk/DotNet Core/JsonApi/JsonApi.csproj b/sdk/DotNet Core/JsonApi/JsonApi.csproj new file mode 100644 index 00000000..de58f5f3 --- /dev/null +++ b/sdk/DotNet Core/JsonApi/JsonApi.csproj @@ -0,0 +1,15 @@ + + + + netcoreapp1.1 + + + + + + + + + + + \ No newline at end of file diff --git a/sdk/DotNet Core/JsonApiTest/JsonApiTest.csproj b/sdk/DotNet Core/JsonApiTest/JsonApiTest.csproj new file mode 100644 index 00000000..76de12df --- /dev/null +++ b/sdk/DotNet Core/JsonApiTest/JsonApiTest.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp1.1 + + + + + + + \ No newline at end of file diff --git a/sdk/DotNet Core/JsonApiTest/Program.cs b/sdk/DotNet Core/JsonApiTest/Program.cs new file mode 100644 index 00000000..7ea61b85 --- /dev/null +++ b/sdk/DotNet Core/JsonApiTest/Program.cs @@ -0,0 +1,14 @@ +using JsonApi; +using System; + +namespace JsonApiTest +{ + class Program + { + static void Main(string[] args) + { + var j = new ApiHelper("192.168.0.197", 25565, "admin", "changeme"); + Console.WriteLine(j.Call("players.name", new object[] { "Tuisku" })); + } + } +} \ No newline at end of file