mirror of
https://github.com/Facepunch/Facepunch.Steamworks.git
synced 2024-12-25 14:15:47 +03:00
Web Api boilerplate
This commit is contained in:
parent
657c04b399
commit
270595a540
@ -71,6 +71,10 @@
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.9.0.2-beta1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
</ItemGroup>
|
||||
<Choose>
|
||||
@ -96,6 +100,7 @@
|
||||
<Compile Include="Server\Inventory.cs" />
|
||||
<Compile Include="Server\Server.cs" />
|
||||
<Compile Include="Server\Stats.cs" />
|
||||
<Compile Include="Web\WebSteamApps.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Facepunch.Steamworks\Facepunch.Steamworks.csproj">
|
||||
@ -107,6 +112,9 @@
|
||||
<Compile Include="Client\Serverlist.cs" />
|
||||
<Compile Include="Client\Stats.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Choose>
|
||||
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
|
||||
<ItemGroup>
|
||||
|
58
Facepunch.Steamworks.Test/Web/WebSteamApps.cs
Normal file
58
Facepunch.Steamworks.Test/Web/WebSteamApps.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace Facepunch.Steamworks.Test
|
||||
{
|
||||
[TestClass]
|
||||
public partial class WebSteamApps
|
||||
{
|
||||
[TestInitialize]
|
||||
public void Init()
|
||||
{
|
||||
//
|
||||
// For the sake of tests, we store our Key in an environment variable
|
||||
// (so we don't end up shipping it to github and exposing it to everyone)
|
||||
//
|
||||
Facepunch.SteamApi.Config.Key = Environment.GetEnvironmentVariable( "SteamWebApi", EnvironmentVariableTarget.User );
|
||||
|
||||
//
|
||||
// We're going to be using Newtonsoft to deserialize our json
|
||||
//
|
||||
Facepunch.SteamApi.Config.DeserializeJson = ( str, type ) =>
|
||||
{
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject( str, type );
|
||||
};
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GetAppBetas()
|
||||
{
|
||||
foreach ( var beta in Facepunch.SteamApi.ISteamApps.GetAppBetas( 252490 ).betas )
|
||||
{
|
||||
Console.WriteLine( beta.Key );
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GetAppBuilds()
|
||||
{
|
||||
foreach ( var build in Facepunch.SteamApi.ISteamApps.GetAppBuilds( 252490, 10 ).builds )
|
||||
{
|
||||
Console.WriteLine( build.Key );
|
||||
Console.WriteLine( " Desc: " + build.Value.Description );
|
||||
Console.WriteLine( " Accnt:" + build.Value.AccountIDCreator );
|
||||
|
||||
foreach ( var depot in build.Value.depots )
|
||||
{
|
||||
Console.WriteLine( " Depot" + depot.Value.DepotId + ":" );
|
||||
Console.WriteLine( " GID: " + depot.Value.DepotVersionGID );
|
||||
Console.WriteLine( " Bytes: " + depot.Value.TotalOriginalBytes );
|
||||
Console.WriteLine( " Compressed: " + depot.Value.TotalCompressedBytes );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
4
Facepunch.Steamworks.Test/packages.config
Normal file
4
Facepunch.Steamworks.Test/packages.config
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Newtonsoft.Json" version="9.0.2-beta1" targetFramework="net452" />
|
||||
</packages>
|
@ -144,6 +144,10 @@
|
||||
<Compile Include="Server\Auth.cs" />
|
||||
<Compile Include="Server\Query.cs" />
|
||||
<Compile Include="Server\Stats.cs" />
|
||||
<Compile Include="SteamNative\SteamApi.cs" />
|
||||
<Compile Include="SteamNative\SteamApi.Config.cs" />
|
||||
<Compile Include="SteamNative\SteamApi.Response.cs" />
|
||||
<Compile Include="SteamNative\SteamApi.Utility.cs" />
|
||||
<Compile Include="SteamNative\SteamNative.Helpers.cs" />
|
||||
<Compile Include="SteamNative\SteamNative.Constants.cs" />
|
||||
<Compile Include="SteamNative\SteamNative.Enums.cs" />
|
||||
|
36
Facepunch.Steamworks/SteamNative/SteamApi.Config.cs
Normal file
36
Facepunch.Steamworks/SteamNative/SteamApi.Config.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Facepunch
|
||||
{
|
||||
namespace SteamApi
|
||||
{
|
||||
/// <summary>
|
||||
/// Static class to configure SteamApi
|
||||
/// </summary>
|
||||
public static class Config
|
||||
{
|
||||
/// <summary>
|
||||
/// Your Steam Api Key is secret.
|
||||
/// Don't ship it with your game.
|
||||
/// Treat it like a password.
|
||||
/// </summary>
|
||||
public static string Key;
|
||||
|
||||
/// <summary>
|
||||
/// Plug your json deserializer in here. We don't force one on you, this is left
|
||||
/// up to the implementation. We've only tested Newtonsoft.Json though. Here's an example of
|
||||
/// how to set it up.
|
||||
/// </summary>
|
||||
/// <example>
|
||||
///
|
||||
/// Facepunch.SteamApi.Config.DeserializeJson = ( str, type ) =>
|
||||
/// {
|
||||
/// return Newtonsoft.Json.JsonConvert.DeserializeObject( str, type );
|
||||
/// };
|
||||
///
|
||||
/// </example>
|
||||
public static Func<string, Type, object> DeserializeJson;
|
||||
}
|
||||
}
|
||||
}
|
54
Facepunch.Steamworks/SteamNative/SteamApi.Response.cs
Normal file
54
Facepunch.Steamworks/SteamNative/SteamApi.Response.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace Facepunch.SteamApi
|
||||
{
|
||||
internal class ApiResponse<T>
|
||||
{
|
||||
public T Response { get; set; }
|
||||
}
|
||||
|
||||
public partial class ISteamApps
|
||||
{
|
||||
public class GetAppBetasResponse
|
||||
{
|
||||
public class Beta
|
||||
{
|
||||
public ulong BuildId { get; set; }
|
||||
public string Description { get; set; }
|
||||
public bool ReqPassword { get; set; }
|
||||
public bool ReqLocalCS { get; set; }
|
||||
}
|
||||
|
||||
public Dictionary<string, Beta> betas;
|
||||
}
|
||||
|
||||
public class GetAppBuildsResponse
|
||||
{
|
||||
public class Build
|
||||
{
|
||||
public ulong BuildId { get; set; }
|
||||
public string Description { get; set; }
|
||||
public uint CreationTime { get; set; }
|
||||
public ulong AccountIDCreator { get; set; }
|
||||
|
||||
public class Depot
|
||||
{
|
||||
public ulong DepotId { get; set; }
|
||||
public ulong DepotVersionGID { get; set; }
|
||||
public ulong TotalOriginalBytes { get; set; }
|
||||
public ulong TotalCompressedBytes { get; set; }
|
||||
}
|
||||
|
||||
public Dictionary<string, Depot> depots;
|
||||
}
|
||||
|
||||
public Dictionary<string, Build> builds;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
35
Facepunch.Steamworks/SteamNative/SteamApi.Utility.cs
Normal file
35
Facepunch.Steamworks/SteamNative/SteamApi.Utility.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace Facepunch.SteamApi
|
||||
{
|
||||
public class Utility
|
||||
{
|
||||
public static T WebGet<T>( string url )
|
||||
{
|
||||
var www = new System.Net.WebClient();
|
||||
var data = www.DownloadString( url );
|
||||
|
||||
if ( typeof( T ) == typeof( string ) )
|
||||
return (T)(object)data;
|
||||
|
||||
var response = DeserializeJson<ApiResponse<T>>( data );
|
||||
|
||||
www.Dispose();
|
||||
|
||||
return response.Response;
|
||||
}
|
||||
|
||||
private static T DeserializeJson<T>( string data )
|
||||
{
|
||||
return (T)Facepunch.SteamApi.Config.DeserializeJson( data, typeof( T ) );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
1895
Facepunch.Steamworks/SteamNative/SteamApi.cs
Normal file
1895
Facepunch.Steamworks/SteamNative/SteamApi.cs
Normal file
File diff suppressed because it is too large
Load Diff
@ -11,10 +11,12 @@ namespace Generator
|
||||
public partial class CodeWriter
|
||||
{
|
||||
private SteamApiDefinition def;
|
||||
private WebApiDefinition webdef;
|
||||
|
||||
public CodeWriter( SteamApiDefinition def )
|
||||
public CodeWriter( SteamApiDefinition def, WebApiDefinition webdef )
|
||||
{
|
||||
this.def = def;
|
||||
this.webdef = webdef;
|
||||
|
||||
WorkoutTypes();
|
||||
}
|
||||
@ -101,6 +103,14 @@ public void ToFolder( string folder )
|
||||
System.IO.File.WriteAllText( $"{folder}SteamNative.Platform.Mac.cs", sb.ToString() );
|
||||
}
|
||||
|
||||
{
|
||||
sb = new StringBuilder();
|
||||
Header( "Facepunch.SteamApi" );
|
||||
WebApi();
|
||||
Footer();
|
||||
System.IO.File.WriteAllText( $"{folder}SteamApi.cs", sb.ToString() );
|
||||
}
|
||||
|
||||
{
|
||||
GenerateClasses( $"{folder}SteamNative." );
|
||||
}
|
||||
@ -147,12 +157,12 @@ private List<Argument> BuildArguments( SteamApiDefinition.MethodDef.ParamType[]
|
||||
return args;
|
||||
}
|
||||
|
||||
private void Header()
|
||||
private void Header( string NamespaceName = "SteamNative" )
|
||||
{
|
||||
WriteLine( "using System;" );
|
||||
WriteLine( "using System.Runtime.InteropServices;" );
|
||||
WriteLine();
|
||||
StartBlock( "namespace SteamNative" );
|
||||
StartBlock( "namespace " + NamespaceName );
|
||||
}
|
||||
|
||||
private void Footer()
|
||||
|
116
Generator/CodeWriter/WebApi.cs
Normal file
116
Generator/CodeWriter/WebApi.cs
Normal file
@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Generator
|
||||
{
|
||||
public partial class CodeWriter
|
||||
{
|
||||
public static string[] HasReturnTypes = new string[]
|
||||
{
|
||||
"ISteamApps.GetAppBetas",
|
||||
"ISteamApps.GetAppBuilds",
|
||||
};
|
||||
|
||||
private void WebApi()
|
||||
{
|
||||
foreach ( var o in webdef.apilist.interfaces )
|
||||
{
|
||||
//
|
||||
// Skip any that end in numbers
|
||||
if ( char.IsNumber( o.name.Last() ) )
|
||||
continue;
|
||||
|
||||
WriteLine( $"/// <summary>" );
|
||||
WriteLine( $"/// {o.name}" );
|
||||
WriteLine( $"/// </summary>" );
|
||||
StartBlock( $"public partial class {o.name}" );
|
||||
{
|
||||
WriteLine( $"private const string Url = \"http://api.steampowered.com/{o.name}/\";" );
|
||||
WriteLine();
|
||||
|
||||
|
||||
foreach ( var m in o.methods.OrderBy( x => x.name ) )
|
||||
{
|
||||
var LatestVersion = o.methods
|
||||
.Where( x => x.name == m.name )
|
||||
.Max( x => x.version );
|
||||
|
||||
// Skip this method if it's not the latest
|
||||
if ( LatestVersion != m.version )
|
||||
continue;
|
||||
|
||||
WebApiMethod( o.name, m );
|
||||
}
|
||||
}
|
||||
EndBlock();
|
||||
WriteLine();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void WebApiMethod( string parent, WebApiDefinition.ApiList.Interface.Method m )
|
||||
{
|
||||
var pars = m.parameters.Where ( x => x.name != "key" );
|
||||
|
||||
var parameters = string.Join( ", ", pars.Select( x => FormatParameter( x ) ).Where( x => x != null ) );
|
||||
|
||||
foreach ( var p in pars )
|
||||
{
|
||||
WriteLine( $"/// <param name=\"{p.name.Replace( "[0]", "[]" ) }\">{p.description}</param>" );
|
||||
}
|
||||
|
||||
var returnType = "string";
|
||||
|
||||
if ( HasReturnTypes.Contains( $"{parent}.{m.name}" ) )
|
||||
{
|
||||
returnType = m.name + "Response";
|
||||
}
|
||||
|
||||
StartBlock( $"public static {returnType} {m.name}({parameters})" );
|
||||
{
|
||||
if ( m.httpmethod == "GET" )
|
||||
{
|
||||
var getParameters = string.Join( "&", pars.Select( x => FormatGetParameter( x ) ).Where( x => x != null ) );
|
||||
WriteLine( $"string url = $\"{{Url}}{m.name}/v{m.version.ToString( "0000" )}/?key={{Facepunch.SteamApi.Config.Key}}&format=json&{getParameters}\";" );
|
||||
WriteLine( $"return Utility.WebGet<{returnType}>( url );" );
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteLine( $"string url = $\"{{Url}}/{m.name}/v{m.version.ToString( "0000" )}/\";" );
|
||||
WriteLine( $"return url;" );
|
||||
}
|
||||
|
||||
}
|
||||
EndBlock();
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
private object FormatGetParameter( WebApiDefinition.ApiList.Interface.Method.Parameter x )
|
||||
{
|
||||
return $"{x.name}={{{x.name}}}";
|
||||
}
|
||||
|
||||
private object FormatParameter( WebApiDefinition.ApiList.Interface.Method.Parameter x )
|
||||
{
|
||||
var name = x.name;
|
||||
|
||||
var type = ToManagedType( x.type );
|
||||
|
||||
if ( type == "{message}" ) type = "string";
|
||||
if ( type == "rawbinary" ) type = "byte[]";
|
||||
|
||||
if ( name.EndsWith( "[0]" ) )
|
||||
{
|
||||
name = name.Replace( "[0]", "" );
|
||||
type += "[]";
|
||||
}
|
||||
|
||||
return $"{type} {name}";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -51,6 +51,7 @@
|
||||
<Compile Include="CodeParser\CodeParser.cs" />
|
||||
<Compile Include="CodeWriter\Class.cs" />
|
||||
<Compile Include="CodeWriter\Constants.cs" />
|
||||
<Compile Include="CodeWriter\WebApi.cs" />
|
||||
<Compile Include="CodeWriter\Writing.cs" />
|
||||
<Compile Include="CodeWriter\CodeWriter.cs" />
|
||||
<Compile Include="CodeWriter\Enums.cs" />
|
||||
@ -60,6 +61,7 @@
|
||||
<Compile Include="CodeWriter\Interface.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="WebApiDefinition.cs" />
|
||||
<Compile Include="SteamApiDefinition.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
@ -14,13 +14,16 @@ static void Main( string[] args )
|
||||
var content = System.IO.File.ReadAllText( "steam_sdk/steam_api.json" );
|
||||
var def = Newtonsoft.Json.JsonConvert.DeserializeObject<SteamApiDefinition>( content );
|
||||
|
||||
var webcontent = System.IO.File.ReadAllText( "steam_sdk/web_api.json" );
|
||||
var webdef = Newtonsoft.Json.JsonConvert.DeserializeObject<WebApiDefinition>( webcontent );
|
||||
|
||||
AddExtras( def );
|
||||
|
||||
var parser = new CodeParser( @"steam_sdk" );
|
||||
|
||||
parser.ExtendDefinition( def );
|
||||
|
||||
var generator = new CodeWriter( def );
|
||||
var generator = new CodeWriter( def, webdef );
|
||||
|
||||
generator.ToFolder( "../Facepunch.Steamworks/SteamNative/" );
|
||||
}
|
||||
|
43
Generator/WebApiDefinition.cs
Normal file
43
Generator/WebApiDefinition.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Generator
|
||||
{
|
||||
public class WebApiDefinition
|
||||
{
|
||||
public class ApiList
|
||||
{
|
||||
public Interface[] interfaces { get; set; }
|
||||
|
||||
public class Interface
|
||||
{
|
||||
public string name { get; set; }
|
||||
|
||||
public Method[] methods { get; set; }
|
||||
|
||||
public class Method
|
||||
{
|
||||
public string name { get; set; }
|
||||
public int version { get; set; }
|
||||
public string httpmethod { get; set; }
|
||||
|
||||
public Parameter[] parameters { get; set; }
|
||||
|
||||
public class Parameter
|
||||
{
|
||||
public string name { get; set; }
|
||||
public string type { get; set; }
|
||||
public bool optional { get; set; }
|
||||
public string description { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ApiList apilist { get; set; }
|
||||
}
|
||||
}
|
8091
Generator/steam_sdk/web_api.json
Normal file
8091
Generator/steam_sdk/web_api.json
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user