Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions Runtime/codebase/Web3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,17 +319,18 @@ public static async UniTask UpdateNFTs(Commitment commitment = Commitment.Confir
/// </summary>
/// <param name="loadTexture"></param>
/// <param name="notifyRegisteredListeners">If true, notify the register listeners</param>
/// <param name="requestsMillisecondsDelay">Add a delay between requests</param>
/// <param name="commitment"></param>
public static async UniTask<List<Nft.Nft>> LoadNFTs(
bool loadTexture = true,
bool notifyRegisteredListeners = true,
int requestsMillisecondsDelay = 0,
Commitment commitment = Commitment.Confirmed)
{
loadTexture = LoadNftsTextureByDefault ?? loadTexture;
if(Wallet == null) return null;
var tokens = (await Wallet.GetTokenAccounts(commitment))?
.ToList()
.FindAll(m => m.Account.Data.Parsed.Info.TokenAmount.AmountUlong == 1);
.ToList();
if(tokens == null) return null;

// Remove tokens not owned anymore
Expand Down Expand Up @@ -360,13 +361,19 @@ public static async UniTask UpdateNFTs(Commitment commitment = Commitment.Confir
if (tokens is {Count: > 0})
{
var toFetch = tokens
.Where(item => item.Account.Data.Parsed.Info.TokenAmount.AmountUlong == 1)
.Where(item => nfts
.All(t => t.metaplexData.data.mint!= item.Account.Data.Parsed.Info.Mint)).ToArray();
total = nfts.Count + toFetch.Length;

foreach (var item in toFetch)
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
// If we are on WebGL, we need to add a min delay between requests
requestsMillisecondsDelay = Mathf.Max(requestsMillisecondsDelay, 100);
}
if (requestsMillisecondsDelay > 0) await UniTask.Delay(requestsMillisecondsDelay);

var tNft = Nft.Nft.TryGetNftData(item.Account.Data.Parsed.Info.Mint, Rpc, loadTexture: loadTexture).AsUniTask();
loadingTasks.Add(tNft);
tNft.ContinueWith(nft =>
Expand Down
1 change: 0 additions & 1 deletion Runtime/codebase/nft/Nft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public static Nft TryLoadNftFromLocal(string mint)
/// <summary>
/// Load the texture of the NFT
/// </summary>
/// <param name="nft"></param>
/// <param name="imageHeightAndWidth"></param>
public async Task LoadTexture(int imageHeightAndWidth = 256)
{
Expand Down
82 changes: 19 additions & 63 deletions Runtime/codebase/utility/FileDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
Expand Down Expand Up @@ -85,20 +84,13 @@ public static async Task<T> LoadFile<T>(string path, string optionalName = "")
return await LoadTexture<T>(path);
}
}
else
{
#if UNITY_WEBGL && !UNITY_EDITOR
return await LoadJsonWebRequest<T>(path);
#else
return await LoadJson<T>(path);
#endif
}
throw new NotImplementedException();
}

private static async Task<T> LoadTexture<T>(string filePath, CancellationToken token = default)
{
using var uwr = UnityWebRequestTexture.GetTexture(filePath);
uwr.SendWebRequest();
await uwr.SendWebRequest();

while (!uwr.isDone && !token.IsCancellationRequested)
{
Expand All @@ -111,14 +103,15 @@ private static async Task<T> LoadTexture<T>(string filePath, CancellationToken t
return default;
}
var texture = DownloadHandlerTexture.GetContent(uwr);
Object.Destroy(((DownloadHandlerTexture) uwr.downloadHandler).texture);
DestroyTexture(((DownloadHandlerTexture)uwr.downloadHandler).texture);
return (T)Convert.ChangeType(texture, typeof(T));
}

private static async UniTask<T> LoadGif<T>(string path, CancellationToken token = default)
{
using UnityWebRequest uwr = UnityWebRequest.Get(path);
uwr.SendWebRequest();
await uwr.SendWebRequest();

while (!uwr.isDone && !token.IsCancellationRequested)
{
await Task.Yield();
Expand Down Expand Up @@ -174,56 +167,6 @@ private static Texture2D GetTextureFromGifByteStream(byte[] bytes)
return null;
}

private static async Task<T> LoadJsonWebRequest<T>(string path)
{
using var uwr = UnityWebRequest.Get(path);
uwr.downloadHandler = new DownloadHandlerBuffer();
uwr.SendWebRequest();

while (!uwr.isDone)
{
await Task.Yield();
}

var json = uwr.downloadHandler.text;
if (uwr.result is UnityWebRequest.Result.ConnectionError or UnityWebRequest.Result.ProtocolError)
{
Debug.Log(uwr.error);
return default;
}

Debug.Log(json);
try
{
var data = JsonConvert.DeserializeObject<T>(json);
return data;
}
catch
{
return default;
}
}

private static async Task<T> LoadJson<T>(string path)
{
var client = new HttpClient();

try
{
var response = await client.GetAsync(path);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(responseBody);
client.Dispose();
return data;
}
catch
{
client.Dispose();
return default;
}
}

public static T LoadFileFromLocalPath<T>(string path)
{
if (!File.Exists(path))
Expand Down Expand Up @@ -298,8 +241,21 @@ public static Texture2D Resize(Texture texture2D, int targetX, int targetY)
Texture2D result = new Texture2D(targetX, targetY);
result.ReadPixels(new Rect(0, 0, targetX, targetY), 0, 0);
result.Apply();
DestroyTexture(texture2D);
return result;
}

private static void DestroyTexture(Texture texture)
{
if (Application.isPlaying)
{
Object.Destroy(texture);
}
else
{
Object.DestroyImmediate(texture);
}
}

}

Expand Down