Skip to content

Commit 08c0c47

Browse files
🚑 Fix execution on WebGl (#5)
* 🚑 Fix execution on WebGl Using UnityWebRequest when running inside Unity, as HttpClient is not fully supported in WebGL * 🔖 Bump version to 2.6.0.8 * ✨ Add Istruction Account public key
1 parent 44b14fd commit 08c0c47

File tree

5 files changed

+100
-14
lines changed

5 files changed

+100
-14
lines changed

SharedBuildProperties.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<Product>Solana.Unity</Product>
5-
<Version>2.6.0.7</Version>
5+
<Version>2.6.0.8</Version>
66
<Copyright>Copyright 2022 &#169; Solana.Unity</Copyright>
77
<Authors>garbles-dev</Authors>
88
<PublisherName>garbles-dev</PublisherName>

build.cake

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ var testProjectsRelativePaths = new string[]
1212
};
1313

1414
var target = Argument("target", "Pack");
15-
var configuration = Argument("configuration", "Release");
15+
var configuration = Argument("configuration", "Debug");
16+
var configurationRelease = Argument("configuration", "Release");
1617
var solutionFolder = "./";
1718
var artifactsDir = MakeAbsolute(Directory("artifacts"));
1819

@@ -47,6 +48,16 @@ Task("Build")
4748
});
4849
});
4950

51+
Task("BuildRelease")
52+
.IsDependentOn("Clean")
53+
.IsDependentOn("Restore")
54+
.Does(() => {
55+
DotNetCoreBuild(solutionFolder, new DotNetCoreBuildSettings
56+
{
57+
NoRestore = true,
58+
Configuration = configurationRelease
59+
});
60+
});
5061

5162
Task("Test")
5263
.IsDependentOn("Build")
@@ -94,11 +105,12 @@ Task("Report")
94105

95106
Task("Publish")
96107
.IsDependentOn("Report")
108+
.IsDependentOn("BuildRelease")
97109
.Does(() => {
98110
DotNetCorePublish(solutionFolder, new DotNetCorePublishSettings
99111
{
100112
NoRestore = true,
101-
Configuration = configuration,
113+
Configuration = configurationRelease,
102114
NoBuild = true,
103115
OutputDirectory = artifactsDir
104116
});
@@ -110,7 +122,7 @@ Task("Pack")
110122
{
111123
var settings = new DotNetCorePackSettings
112124
{
113-
Configuration = configuration,
125+
Configuration = configurationRelease,
114126
NoBuild = true,
115127
NoRestore = true,
116128
OutputDirectory = packagesDir,

src/Solana.Unity.Programs/SysVars.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,9 @@ public static readonly PublicKey
3030
/// The public key of the Stake History System Variable.
3131
/// </summary>
3232
public static readonly PublicKey StakeHistoryKey = new("SysvarStakeHistory1111111111111111111111111");
33+
/// <summary>
34+
/// The public key of the Instruction Account Variable.
35+
/// </summary>
36+
public static readonly PublicKey InstructionAccount = new("Sysvar1nstructions1111111111111111111111111");
3337
}
3438
}

src/Solana.Unity.Rpc/Core/Http/JsonRpcClient.cs

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
using Newtonsoft.Json.Converters;
77
using Newtonsoft.Json.Serialization;
88
using Solana.Unity.Rpc.Converters;
9+
using System.ComponentModel;
10+
using System.Net;
911
using System.Net.Http;
1012
using System.Text;
1113
using System.Threading.Tasks;
12-
using JsonException = Newtonsoft.Json.JsonException;
14+
using UnityEngine;
15+
using UnityEngine.Networking;
1316

1417
namespace Solana.Unity.Rpc.Core.Http
1518
{
@@ -101,14 +104,12 @@ protected async Task<RequestResult<T>> SendRequest<T>(JsonRpcRequest req)
101104
};
102105

103106
// execute POST
104-
using (var response = await _httpClient.SendAsync(httpReq).ConfigureAwait(false))
107+
using (var response = await SendAsyncRequest(_httpClient, httpReq))
105108
{
106109
var result = await HandleResult<T>(req, response).ConfigureAwait(false);
107110
result.RawRpcRequest = requestJson;
108111
return result;
109112
}
110-
111-
112113
}
113114
catch (HttpRequestException e)
114115
{
@@ -130,10 +131,10 @@ protected async Task<RequestResult<T>> SendRequest<T>(JsonRpcRequest req)
130131
}
131132
return result;
132133
}
133-
134-
134+
135135
}
136136

137+
137138
/// <summary>
138139
/// Handles the result after sending a request.
139140
/// </summary>
@@ -143,7 +144,7 @@ protected async Task<RequestResult<T>> SendRequest<T>(JsonRpcRequest req)
143144
/// <returns>A task that represents the asynchronous operation that holds the request result.</returns>
144145
private async Task<RequestResult<T>> HandleResult<T>(JsonRpcRequest req, HttpResponseMessage response)
145146
{
146-
RequestResult<T> result = new RequestResult<T>(response);
147+
RequestResult<T> result = new(response);
147148
try
148149
{
149150
result.RawRpcResponse = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
@@ -154,9 +155,10 @@ private async Task<RequestResult<T>> HandleResult<T>(JsonRpcRequest req, HttpRes
154155
}
155156
var res = JsonConvert.DeserializeObject<JsonRpcResponse<T>>(result.RawRpcResponse, _serializerOptions);
156157

158+
157159
if (res.Result != null)
158160
{
159-
result.Result = (T)res.Result;
161+
result.Result = res.Result;
160162
result.WasRequestSuccessfullyHandled = true;
161163
}
162164
else
@@ -226,7 +228,7 @@ public async Task<RequestResult<JsonRpcBatchResponse>> SendBatchRequestAsync(Jso
226228
};
227229

228230
// execute POST
229-
using (var response = await _httpClient.SendAsync(httpReq).ConfigureAwait(false))
231+
using (var response = await SendAsyncRequest(_httpClient, httpReq))
230232
{
231233
var result = await HandleBatchResult(reqs, response).ConfigureAwait(false);
232234
result.RawRpcRequest = requestsJson;
@@ -315,7 +317,74 @@ private async Task<RequestResult<JsonRpcBatchResponse>> HandleBatchResult(JsonRp
315317

316318
return result;
317319
}
318-
320+
321+
/// <summary>
322+
/// Return True if running on Unity, False otherwise
323+
/// </summary>
324+
/// <returns>Return True if running on Unity, False otherwise</returns>
325+
private bool IsUnityPlayer()
326+
{
327+
#if NETSTANDARD2_0 && !DEBUG
328+
try
329+
{
330+
if (Application.platform != null)
331+
{
332+
return true;
333+
}
334+
}
335+
catch (Exception)
336+
{
337+
return false;
338+
}
339+
#endif
340+
return false;
341+
}
342+
343+
/// <summary>
344+
/// Send an async request using HttpClient or UnityWebRequest if running on Unity
345+
/// </summary>
346+
/// <param name="httpClient"></param>
347+
/// <param name="httpReq"></param>
348+
/// <returns></returns>
349+
private async Task<HttpResponseMessage> SendAsyncRequest(HttpClient httpClient, HttpRequestMessage httpReq)
350+
{
351+
if (IsUnityPlayer())
352+
{
353+
return await SendUnityWebRequest(httpClient.BaseAddress, httpReq);
354+
}
355+
return await _httpClient.SendAsync(httpReq).ConfigureAwait(false);
356+
}
357+
358+
/// <summary>
359+
/// Convert a httReq to a Unity Web request
360+
/// </summary>
361+
/// <param name="uri">RPC URI</param>
362+
/// <param name="httpReq">The http request</param>
363+
/// <returns>Http response</returns>
364+
/// <exception cref="HttpRequestException"></exception>
365+
private async Task<HttpResponseMessage> SendUnityWebRequest(Uri uri, HttpRequestMessage httpReq)
366+
{
367+
Byte[] buffer = await httpReq.Content.ReadAsByteArrayAsync();
368+
Console.WriteLine($"Send A");
369+
using (var request = new UnityWebRequest(uri, httpReq.Method.ToString()))
370+
{
371+
request.uploadHandler = new UploadHandlerRaw(buffer);
372+
request.downloadHandler = new DownloadHandlerBuffer();
373+
request.SetRequestHeader("Content-Type", "application/json");
374+
request.SendWebRequest();
375+
if (request.result == UnityWebRequest.Result.ConnectionError)
376+
{
377+
throw new HttpRequestException("Error While Sending: " + request.error);
378+
}
379+
while (!request.isDone)
380+
{
381+
await Task.Yield();
382+
}
383+
var response = new HttpResponseMessage(HttpStatusCode.OK);
384+
response.Content = new ByteArrayContent(Encoding.UTF8.GetBytes(request.downloadHandler.text));
385+
return response;
386+
}
387+
}
319388
}
320389

321390
}

src/Solana.Unity.Rpc/Solana.Unity.Rpc.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<PackageReference Include="Newtonsoft.Json" Version="12.0.*" />
1616
<PackageReference Include="System.Collections.Immutable" Version="6.0.*" />
1717
<PackageReference Include="IsExternalInit" Version="1.0.*" PrivateAssets="all" />
18+
<PackageReference Include="Unity3D.SDK" Version="2021.*" />
1819
<ProjectReference Include="..\Solana.Unity.Wallet\Solana.Unity.Wallet.csproj" />
1920
</ItemGroup>
2021

0 commit comments

Comments
 (0)