Skip to content

Commit

Permalink
Merge pull request #42 from dojoengine/wasm
Browse files Browse the repository at this point in the history
feat: add geteventmessges to wasm asnd remove debug logs
  • Loading branch information
Larkooo authored Apr 26, 2024
2 parents 9145924 + c8954f0 commit 4732ebd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
3 changes: 0 additions & 3 deletions Assets/Dojo/Plugins/WebGL/starknet.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ mergeInto(LibraryManager.library, {
dynCall_vi(cb, buffer);
},
AccountDeployBurner: async function (accountPtr, privateKey, cb) {
console.log(UTF8ToString(privateKey));
const account = wasm_bindgen.Account.__wrap(accountPtr);
const burner = await account.deployBurner(UTF8ToString(privateKey));

Expand Down Expand Up @@ -93,8 +92,6 @@ mergeInto(LibraryManager.library, {
var compactSig =
signature.r.replace("0x", "").padStart(64, "0") +
signature.s.replace("0x", "").padStart(64, "0");
console.log(signature);
console.log(compactSig);
var bufferSize = lengthBytesUTF8(compactSig) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(compactSig, buffer, bufferSize);
Expand Down
18 changes: 16 additions & 2 deletions Assets/Dojo/Plugins/WebGL/torii_c.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,22 @@ mergeInto(LibraryManager.library, {
const client = wasm_bindgen.Client.__wrap(clientPtr);
const query = JSON.parse(UTF8ToString(queryString));
var entities = await client.getEntities(query);
console.log(entities);

// stringify the entities
var entitiesString = JSON.stringify(entities);
// return buffer
var bufferSize = lengthBytesUTF8(entitiesString) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(entitiesString, buffer, bufferSize);

client.__destroy_into_raw();
dynCall_vi(cb, buffer);
},
// Returns a dictionary of all of the eventmessages
GetEventMessages: async function (clientPtr, queryString, cb) {
const client = wasm_bindgen.Client.__wrap(clientPtr);
const query = JSON.parse(UTF8ToString(queryString));
var entities = await client.getEventMessages(query);

// stringify the entities
var entitiesString = JSON.stringify(entities);
Expand Down Expand Up @@ -149,7 +164,6 @@ mergeInto(LibraryManager.library, {
PublishMessage: async function (clientPtr, message, signature, cb) {
var client = wasm_bindgen.Client.__wrap(clientPtr);
const published = await client.publishMessage(UTF8ToString(message), JSON.parse(UTF8ToString(signature)));
console.log(published);
const publishedString = JSON.stringify(Array.from(published));
const bufferSize = lengthBytesUTF8(publishedString) + 1;
const buffer = _malloc(bufferSize);
Expand Down
6 changes: 6 additions & 0 deletions Assets/Dojo/Runtime/Torii/ToriiWasmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public async Task<List<Entity>> Entities(dojo.Query query)
return entities;
}

public async Task<List<Entity>> EventMessages(dojo.Query query)
{
var entities = await ToriiWasmInterop.GetEventMessagesAsync(clientPtr, new Query(query));
return entities;
}

public async Task<byte[]> PublishMessage(TypedData message, Signature signature)
{
return await ToriiWasmInterop.PublishMessageAsync(clientPtr, message, signature);
Expand Down
41 changes: 41 additions & 0 deletions Assets/Dojo/Runtime/Torii/ToriiWasmInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,47 @@ public static Task<List<Entity>> GetEntitiesAsync(IntPtr clientPtr, Query query)
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);
Expand Down

0 comments on commit 4732ebd

Please sign in to comment.