Skip to content

Commit

Permalink
torii client create
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Feb 19, 2025
1 parent a33e843 commit 1c74415
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 19 deletions.
4 changes: 0 additions & 4 deletions Assets/Dojo/Plugins/WebGL/torii_c.jslib
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
mergeInto(LibraryManager.library, {
// Creates a new client and returns the pointer to it
CreateClient: async function (
rpcUrl,
toriiUrl,
relayUrl,
worldAddress,
// callbackObjectName,
// callbackMethodName
cb
) {
let client = await wasm_bindgen.createClient({
rpcUrl: UTF8ToString(rpcUrl),
toriiUrl: UTF8ToString(toriiUrl),
relayUrl: UTF8ToString(relayUrl),
worldAddress: UTF8ToString(worldAddress),
Expand Down
1 change: 0 additions & 1 deletion Assets/Dojo/Runtime/Config/WorldManagerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class WorldManagerData : ScriptableObject
{
[Header("RPC")]
public string toriiUrl = "http://localhost:8080";
public string rpcUrl = "http://localhost:5050";
public string relayUrl = "/ip4/127.0.0.1/tcp/9090";
public string relayWebrtcUrl;
[Header("World")]
Expand Down
13 changes: 8 additions & 5 deletions Assets/Dojo/Runtime/Torii/ToriiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ public unsafe class ToriiClient
private dojo.Subscription* entitySubscription;
private dojo.Subscription* eventMessagesSubscription;

public ToriiClient(string toriiUrl, string rpcUrl, string relayUrl, FieldElement worldAddress, bool dispatchEventsToMainThread = true)
public ToriiClient(string toriiUrl, string relayUrl, FieldElement worldAddress, bool dispatchEventsToMainThread = true)
{
CString ctoriiUrl = CString.FromString(toriiUrl);
CString crpcUrl = CString.FromString(rpcUrl);
CString crelayUrl = CString.FromString(relayUrl);

var result = dojo.client_new(ctoriiUrl, crpcUrl, crelayUrl, worldAddress.Inner);
var result = dojo.client_new(ctoriiUrl, crelayUrl, worldAddress.Inner);
if (result.tag == dojo.ResultToriiClient_Tag.ErrToriiClient)
{
throw new Exception(result.err.message);
Expand All @@ -48,9 +47,13 @@ public ToriiClient(string toriiUrl, string rpcUrl, string relayUrl, FieldElement
public dojo.WorldMetadata WorldMetadata()
{
// TODO: implement a managed type for WorldMetadata too
dojo.WorldMetadata worldMetadata = dojo.client_metadata(client);
dojo.ResultWorldMetadata result = dojo.client_metadata(client);
if (result.tag == dojo.ResultWorldMetadata_Tag.ErrWorldMetadata)
{
throw new Exception(result.err.message);
}

return worldMetadata;
return result.ok;
}

// NOT USED? potentially deprecated
Expand Down
6 changes: 2 additions & 4 deletions Assets/Dojo/Runtime/Torii/ToriiWasmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ namespace Dojo.Torii
public class ToriiWasmClient
{
private string toriiUrl;
private string rpcUrl;
private string relayUrl;
private FieldElement world;
public IntPtr clientPtr;

IntPtr entitySubscription;
IntPtr eventMessageSubscription;

public ToriiWasmClient(string toriiUrl, string rpcUrl, string relayUrl, FieldElement world)
public ToriiWasmClient(string toriiUrl, string relayUrl, FieldElement world)
{
this.toriiUrl = toriiUrl;
this.rpcUrl = rpcUrl;
this.relayUrl = relayUrl;
this.world = world;
}
Expand All @@ -44,7 +42,7 @@ public static void Callback(IntPtr clientPtr)
public async Task CreateClient()
{
CreateClientHelper.Tcs = new TaskCompletionSource<IntPtr>();
ToriiWasmInterop.CreateClient(new CString(rpcUrl), new CString(toriiUrl), new CString(relayUrl), new CString(world.Hex()), CreateClientHelper.Callback);
ToriiWasmInterop.CreateClient(new CString(toriiUrl), new CString(relayUrl), new CString(world.Hex()), CreateClientHelper.Callback);
clientPtr = await CreateClientHelper.Tcs.Task;

entitySubscription = await RegisterEntityStateUpdateEvent(new KeysClause[] { });
Expand Down
2 changes: 1 addition & 1 deletion Assets/Dojo/Runtime/Torii/ToriiWasmInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ToriiWasmInterop : MonoBehaviour
{
// Creates a new client and returns the pointer to it
[DllImport("__Internal")]
public static extern void CreateClient(CString rpcUrl, CString toriiUrl, CString relayUrl, CString worldAddress, Action<IntPtr> cb);
public static extern void CreateClient(CString toriiUrl, CString relayUrl, CString worldAddress, Action<IntPtr> cb);

// Returns a dictionary of all of the entities
[DllImport("__Internal")]
Expand Down
6 changes: 2 additions & 4 deletions Assets/Dojo/Runtime/WorldManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ public class WorldManager : MonoBehaviour
async void Awake()
{
#if UNITY_WEBGL && !UNITY_EDITOR
wasmClient = new ToriiWasmClient(dojoConfig.toriiUrl, dojoConfig.rpcUrl,
dojoConfig.relayWebrtcUrl, dojoConfig.worldAddress);
wasmClient = new ToriiWasmClient(dojoConfig.toriiUrl, dojoConfig.relayWebrtcUrl, dojoConfig.worldAddress);
await wasmClient.CreateClient();
#else
toriiClient = new ToriiClient(dojoConfig.toriiUrl, dojoConfig.rpcUrl,
dojoConfig.relayUrl, dojoConfig.worldAddress);
toriiClient = new ToriiClient(dojoConfig.toriiUrl, dojoConfig.relayUrl, dojoConfig.worldAddress);
#endif

/* fetch entities from the world
Expand Down

0 comments on commit 1c74415

Please sign in to comment.