Skip to content

Commit

Permalink
Merge pull request #54 from dojoengine/torii-client
Browse files Browse the repository at this point in the history
refactor: clean up torii interop code
  • Loading branch information
Larkooo authored Jun 28, 2024
2 parents 4a2c422 + f0f6613 commit c61a151
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 181 deletions.
140 changes: 129 additions & 11 deletions Assets/Dojo/Runtime/Torii/ToriiWasmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AOT;
using bottlenoselabs.C2CS.Runtime;
using Dojo.Starknet;
using dojo_bindings;
Expand All @@ -26,28 +27,145 @@ public ToriiWasmClient(string toriiUrl, string rpcUrl, string relayUrl, FieldEle
this.world = world;
}

private static class CreateClientHelper
{
public static TaskCompletionSource<IntPtr> Tcs;

[MonoPInvokeCallback(typeof(Action<IntPtr>))]
public static void Callback(IntPtr clientPtr)
{
Tcs.SetResult(clientPtr);
}
}

public async Task CreateClient()
{
clientPtr = await ToriiWasmInterop.CreateClientAsync(rpcUrl, toriiUrl, relayUrl, world.Hex());
ToriiWasmInterop.OnEntityUpdated(clientPtr);
ToriiWasmInterop.OnEventMessageUpdated(clientPtr);
CreateClientHelper.Tcs = new TaskCompletionSource<IntPtr>();
ToriiWasmInterop.CreateClient(new CString(rpcUrl), new CString(toriiUrl), new CString(relayUrl), new CString(world.Hex()), CreateClientHelper.Callback);
clientPtr = await CreateClientHelper.Tcs.Task;

RegisterEntityStateUpdateEvent();
RegisterEventMessageUpdateEvent();
}

private static class GetEntitiesHelper
{
public static TaskCompletionSource<List<Entity>> Tcs;

[MonoPInvokeCallback(typeof(Action<string>))]
public static void Callback(string entities)
{
var parsedEntities = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string, WasmValue>>>>(entities);
var entityList = new List<Entity>();

foreach (var entity in parsedEntities)
{
var models = new Dictionary<string, Model>();
foreach (var model in entity.Value)
{
models.Add(model.Key, new Model(
model.Key,
model.Value.ToDictionary(
m => m.Key,
m => m.Value
)
));
}

entityList.Add(new Entity(new FieldElement(entity.Key), models));
}

Tcs.SetResult(entityList);
}
}

public Task<List<Entity>> Entities(Query query)
{
GetEntitiesHelper.Tcs = new TaskCompletionSource<List<Entity>>();
ToriiWasmInterop.GetEntities(clientPtr, new CString(JsonConvert.SerializeObject(query)), GetEntitiesHelper.Callback);
return GetEntitiesHelper.Tcs.Task;
}

public async Task<List<Entity>> Entities(Query query)
public Task<List<Entity>> EventMessages(Query query)
{
var entities = await ToriiWasmInterop.GetEntitiesAsync(clientPtr, query);
return entities;
GetEntitiesHelper.Tcs = new TaskCompletionSource<List<Entity>>();
ToriiWasmInterop.GetEventMessages(clientPtr, new CString(JsonConvert.SerializeObject(query)), GetEntitiesHelper.Callback);
return GetEntitiesHelper.Tcs.Task;
}

public async Task<List<Entity>> EventMessages(Query query)
private static class OnEntityUpdatedHelper
{
var entities = await ToriiWasmInterop.GetEventMessagesAsync(clientPtr, query);
return entities;

[MonoPInvokeCallback(typeof(Action<string>))]
public static void Callback(string entity)
{
var parsedEntity = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string, WasmValue>>>>(entity).First();
var models = new Dictionary<string, Model>();

foreach (var model in parsedEntity.Value)
{
models.Add(model.Key, new Model(
model.Key,
model.Value
));
}

ToriiEvents.Instance.EntityUpdated(new FieldElement(parsedEntity.Key), models.Values.ToArray());
}
}

public async Task<byte[]> PublishMessage(TypedData message, Signature signature)
public void RegisterEntityStateUpdateEvent(KeysClause? clause = null)
{
return await ToriiWasmInterop.PublishMessageAsync(clientPtr, message, signature);
ToriiWasmInterop.OnEntityUpdated(clientPtr, clause.HasValue ? new CString(JsonConvert.SerializeObject(clause)) : (IntPtr)0, OnEntityUpdatedHelper.Callback);
}

private static class OnEventMessageUpdatedHelper
{

[MonoPInvokeCallback(typeof(Action<string>))]
public static void Callback(string entity)
{
var parsedEntity = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string, WasmValue>>>>(entity).First();
var models = new Dictionary<string, Model>();

foreach (var model in parsedEntity.Value)
{
models.Add(model.Key, new Model(
model.Key,
model.Value
));
}

ToriiEvents.Instance.EventMessageUpdated(new FieldElement(parsedEntity.Key), models.Values.ToArray());
}
}

public void RegisterEventMessageUpdateEvent(KeysClause? clause = null)
{
ToriiWasmInterop.OnEventMessageUpdated(clientPtr, clause.HasValue ? new CString(JsonConvert.SerializeObject(clause)) : (IntPtr)0, OnEventMessageUpdatedHelper.Callback);
}

private static class PublishMessageHelper
{
public static TaskCompletionSource<byte[]> Tcs;

[MonoPInvokeCallback(typeof(Action<string>))]
public static void Callback(string messageId)
{
Tcs.SetResult(JsonConvert.DeserializeObject<byte[]>(messageId));
}
}

public Task<byte[]> PublishMessage(TypedData typedData, Signature signature)
{
PublishMessageHelper.Tcs = new TaskCompletionSource<byte[]>();
ToriiWasmInterop.PublishMessage(clientPtr, new CString(JsonConvert.SerializeObject(typedData)), new CString(JsonConvert.SerializeObject(new
{
r = signature.R().Hex(),
s = signature.S().Hex()
})), PublishMessageHelper.Callback);
return PublishMessageHelper.Tcs.Task;
}

}
}
172 changes: 2 additions & 170 deletions Assets/Dojo/Runtime/Torii/ToriiWasmInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,170 +36,24 @@ public class ToriiWasmInterop : MonoBehaviour
[DllImport("__Internal")]
public static extern void CreateClient(CString rpcUrl, CString toriiUrl, CString relayUrl, CString worldAddress, Action<IntPtr> cb);

private static class CreateClientHelper
{
public static TaskCompletionSource<IntPtr> Tcs;

[MonoPInvokeCallback(typeof(Action<IntPtr>))]
public static void Callback(IntPtr clientPtr)
{
Tcs.SetResult(clientPtr);
}
}

public static Task<IntPtr> CreateClientAsync(string rpcUrl, string toriiUrl, string relayUrl, string worldAddress)
{
CreateClientHelper.Tcs = new TaskCompletionSource<IntPtr>();
CreateClient(new CString(rpcUrl), new CString(toriiUrl), new CString(relayUrl), new CString(worldAddress), CreateClientHelper.Callback);
return CreateClientHelper.Tcs.Task;
}

// Returns a dictionary of all of the entities
[DllImport("__Internal")]
public static extern void GetEntities(IntPtr clientPtr, CString query, Action<string> cb);

private static class GetEntitiesHelper
{
public static TaskCompletionSource<List<Entity>> Tcs;

[MonoPInvokeCallback(typeof(Action<string>))]
public static void Callback(string entities)
{
var parsedEntities = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string, WasmValue>>>>(entities);
var entityList = new List<Entity>();

foreach (var entity in parsedEntities)
{
var models = new Dictionary<string, Model>();
foreach (var model in entity.Value)
{
models.Add(model.Key, new Model(
model.Key,
model.Value.ToDictionary(
m => m.Key,
m => m.Value
)
));
}

entityList.Add(new Entity(new FieldElement(entity.Key), models));
}

Tcs.SetResult(entityList);
}
}

public static Task<List<Entity>> GetEntitiesAsync(IntPtr clientPtr, Query query)
{
GetEntitiesHelper.Tcs = new TaskCompletionSource<List<Entity>>();
GetEntities(clientPtr, new CString(JsonConvert.SerializeObject(query)), GetEntitiesHelper.Callback);
return GetEntitiesHelper.Tcs.Task;
}

[DllImport("__Internal")]
public static extern void GetEventMessages(IntPtr clientPtr, CString query, Action<string> cb);

private static class GetEventMessagesHelper
{
public static TaskCompletionSource<List<Entity>> Tcs;

[MonoPInvokeCallback(typeof(Action<string>))]
public static void Callback(string entities)
{
var parsedEntities = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string, WasmValue>>>>(entities);
var entityList = new List<Entity>();

foreach (var entity in parsedEntities)
{
var models = new Dictionary<string, Model>();
foreach (var model in entity.Value)
{
models.Add(model.Key, new Model(
model.Key,
model.Value.ToDictionary(
m => m.Key,
m => m.Value
)
));
}

entityList.Add(new Entity(new FieldElement(entity.Key), models));
}

Tcs.SetResult(entityList);
}
}

public static Task<List<Entity>> GetEventMessagesAsync(IntPtr clientPtr, Query query)
{
GetEventMessagesHelper.Tcs = new TaskCompletionSource<List<Entity>>();
GetEventMessages(clientPtr, new CString(JsonConvert.SerializeObject(query)), GetEventMessagesHelper.Callback);
return GetEventMessagesHelper.Tcs.Task;
}

// Get the value of a model for a specific set of keys
[DllImport("__Internal")]
public static extern string GetModelValue(IntPtr clientPtr, string model, string keys);

// Calls the callback at [callbackObjectName].[callbackMethodName] on entity updated
[DllImport("__Internal")]
private static extern void OnEntityUpdated(IntPtr clientPtr, IntPtr clause, Action<string> cb);

private static class OnEntityUpdatedHelper
{

[MonoPInvokeCallback(typeof(Action<string>))]
public static void Callback(string entity)
{
var parsedEntity = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string, WasmValue>>>>(entity).First();
var models = new Dictionary<string, Model>();

foreach (var model in parsedEntity.Value)
{
models.Add(model.Key, new Model(
model.Key,
model.Value
));
}

ToriiEvents.Instance.EntityUpdated(new FieldElement(parsedEntity.Key), models.Values.ToArray());
}
}

public static void OnEntityUpdated(IntPtr clientPtr, KeysClause? clause = null)
{
OnEntityUpdated(clientPtr, clause.HasValue ? new CString(JsonConvert.SerializeObject(clause)) : (IntPtr)0, OnEntityUpdatedHelper.Callback);
}
public static extern void OnEntityUpdated(IntPtr clientPtr, IntPtr clause, Action<string> cb);

// Calls the callback at [callbackObjectName].[callbackMethodName] on event mnessage updated
[DllImport("__Internal")]
private static extern void OnEventMessageUpdated(IntPtr clientPtr, IntPtr clause, Action<string> cb);

private static class OnEventMessageUpdatedHelper
{

[MonoPInvokeCallback(typeof(Action<string>))]
public static void Callback(string entity)
{
var parsedEntity = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string, WasmValue>>>>(entity).First();
var models = new Dictionary<string, Model>();

foreach (var model in parsedEntity.Value)
{
models.Add(model.Key, new Model(
model.Key,
model.Value
));
}

ToriiEvents.Instance.EventMessageUpdated(new FieldElement(parsedEntity.Key), models.Values.ToArray());
}
}

public static void OnEventMessageUpdated(IntPtr clientPtr, KeysClause? clause = null)
{
OnEventMessageUpdated(clientPtr, clause.HasValue ? new CString(JsonConvert.SerializeObject(clause)) : (IntPtr)0, OnEventMessageUpdatedHelper.Callback);
}
public static extern void OnEventMessageUpdated(IntPtr clientPtr, IntPtr clause, Action<string> cb);

// Add models to sync
[DllImport("__Internal")]
Expand All @@ -219,27 +73,5 @@ public static void OnEventMessageUpdated(IntPtr clientPtr, KeysClause? clause =

[DllImport("__Internal")]
public static extern void PublishMessage(IntPtr clientPtr, CString typedData, CString signature, Action<string> cb);

private static class PublishMessageHelper
{
public static TaskCompletionSource<byte[]> Tcs;

[MonoPInvokeCallback(typeof(Action<string>))]
public static void Callback(string messageId)
{
Tcs.SetResult(JsonConvert.DeserializeObject<byte[]>(messageId));
}
}

public static Task<byte[]> PublishMessageAsync(IntPtr clientPtr, TypedData typedData, Signature signature)
{
PublishMessageHelper.Tcs = new TaskCompletionSource<byte[]>();
PublishMessage(clientPtr, new CString(JsonConvert.SerializeObject(typedData)), new CString(JsonConvert.SerializeObject(new
{
r = signature.R().Hex(),
s = signature.S().Hex()
})), PublishMessageHelper.Callback);
return PublishMessageHelper.Tcs.Task;
}
}
}

0 comments on commit c61a151

Please sign in to comment.