Skip to content

Commit 72e388a

Browse files
committed
Update VRCGuid, attempt to login all clients, update sample code.
1 parent 6e474e7 commit 72e388a

File tree

8 files changed

+26
-6
lines changed

8 files changed

+26
-6
lines changed

VRChat.API.Client/VRCGuid.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public static VRCGuid NewGuid(VRCKind kind = VRCKind.File) =>
7171
/// <returns>A <see cref="bool"/> value representing the success of the operation</returns>
7272
public static bool TryParse(string incoming, out VRCGuid vrcid)
7373
{
74+
if(incoming == null)
75+
throw new ArgumentNullException(nameof(incoming));
76+
7477
vrcid = VRCGuid.Empty;
7578
string[] parts = incoming.Split('_');
7679

VRChat.API.Extensions.Hosting/DefaultVRChatClientFactory.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
5+
using System.Threading.Tasks;
56
using VRChat.API.Client;
67

78
namespace VRChat.API.Extensions.Hosting
@@ -15,6 +16,16 @@ public DefaultVRChatClientFactory() =>
1516

1617
internal bool IsDefaultRegistered => _builders.ContainsKey("vrc_default"); // Not sure where I was going with this, but I'll keep it in here for now
1718

19+
public async Task AttemptLoginForAllClients()
20+
{
21+
foreach(var client in _builders.Keys)
22+
await this.LoginClientAsync(client);
23+
}
24+
25+
public Task LoginClientAsync(string name = "vrc_default") =>
26+
this.CreateClient(name).TryLoginAsync(); // There may be a fatal flaw and we should probably start storing actual IVRChat instances to
27+
// prevent logins and authtokens from being cleared out by the GC or if it's even being stored in the first place. Only one real way to find out.
28+
1829
public IVRChat CreateClient() // Maybe we should throw an exception if the default was not registered?
1930
// It'll ensure that users of the library don't end up accidentally using a client that isn't registered (inconsistent library design)
2031
{
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
using VRChat.API.Client;
1+
using System.Threading.Tasks;
2+
using VRChat.API.Client;
23

34
namespace VRChat.API.Extensions.Hosting
45
{
56
public interface IVRChatClientFactory
67
{
78
IVRChat CreateClient();
89
IVRChat CreateClient(string name);
10+
11+
Task AttemptLoginForAllClients();
12+
Task LoginClientAsync(string name = "vrc_default");
913
}
1014
}

VRChat.API.Extensions.Hosting/VRChatServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static IServiceCollection AddVRChat(this IServiceCollection services, str
6969
TryFillVRChatFromEnv(ref vcb); // If they don't specify a config action, we'll just use env vars to try and set it up
7070

7171
services.TryAddSingleton<DefaultVRChatClientFactory>();
72-
services.TryAddSingleton<IVRChatClientFactory>(srv =>
72+
services.AddSingleton<IVRChatClientFactory>(srv => // Had to change to AddSingleton to make it call multiple times
7373
{
7474
IVRChat client = vcb.Build();
7575
var factory = srv.GetRequiredService<DefaultVRChatClientFactory>();

VRChat.API.UnitSample.AspNetCore/Controllers/SampleController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public SampleController(ILogger<SampleController> logger, IVRChat vrchat)
1717
_logger = logger;
1818
}
1919

20-
[HttpGet("/{id}")]
20+
[HttpGet("/{userId}")]
2121
public async Task<IActionResult> GetCurrentUserAsync(string userId)
2222
{
2323
if (!VRCGuid.TryParse(userId, out VRCGuid id))

VRChat.API.UnitSample.AspNetCore/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"commandName": "Project",
66
"dotnetRunMessages": "true",
77
"launchBrowser": true,
8-
"launchUrl": "",
8+
"launchUrl": "usr_c1644b5b-3ca4-45b4-97c6-a2a0de70d469",
99
"applicationUrl": "http://localhost:5000",
1010
"environmentVariables": {
1111
"ASPNETCORE_ENVIRONMENT": "Development"

VRChat.API.UnitSample.AspNetCore/Startup.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.Extensions.Configuration;
44
using Microsoft.Extensions.DependencyInjection;
55
using Microsoft.Extensions.Hosting;
6+
using Microsoft.VisualBasic;
67
using VRChat.API.Extensions.Hosting;
78

89
namespace VRChat.API.UnitSample.AspNetCore
@@ -24,7 +25,7 @@ public void ConfigureServices(IServiceCollection services)
2425
}
2526

2627
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
27-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
28+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IVRChatClientFactory factory)
2829
{
2930
if (env.IsDevelopment())
3031
app.UseDeveloperExceptionPage();
@@ -36,6 +37,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3637
{
3738
endpoints.MapControllers();
3839
});
40+
41+
factory.AttemptLoginForAllClients().Wait(); // This will try to login all the VRChat clients
3942
}
4043
}
4144
}

VRChat.API.UnitSample/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public static async Task Main(string[] args)
1414
var client = new VRChatClientBuilder()
1515
.WithUsername(username)
1616
.WithPassword(password)
17-
.WithTimeout(TimeSpan.FromSeconds(10))
1817
.Build();
1918

2019
var user = await client.Authentication.GetCurrentUserAsync();

0 commit comments

Comments
 (0)