Skip to content

Commit

Permalink
Merge pull request #91 from dojoengine/update-1.2.1
Browse files Browse the repository at this point in the history
feat: update to 1.2.1
  • Loading branch information
Larkooo authored Feb 19, 2025
2 parents 555886c + 64b940f commit 759de9b
Show file tree
Hide file tree
Showing 35 changed files with 46,517 additions and 39,168 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
10 changes: 5 additions & 5 deletions Assets/Dojo/Runtime/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public dojo.Policy ToNative()

public unsafe class Controller
{
private dojo.Controller* controller;
private dojo.ControllerAccount* controller;
public FieldElement Address => new FieldElement(dojo.controller_address(controller));
public FieldElement ChainId => new FieldElement(dojo.controller_chain_id(controller));
public string Username => CString.ToString(dojo.controller_username(controller));

private static dojo.FnPtr_ControllerPtr_Void onConnectCallback;
private static dojo.FnPtr_ControllerAccountPtr_Void onConnectCallback;
private static TaskCompletionSource<Controller> connectionTask;

private Controller(dojo.Controller* controller)
private Controller(dojo.ControllerAccount* controller)
{
this.controller = controller;
}
Expand All @@ -55,7 +55,7 @@ public static Controller GetAccount(Policy[] policies, FieldElement chainId)
}

var result = dojo.controller_account(policiesPtr, (UIntPtr)policies.Length, chainId.Inner);
if (result.tag == dojo.ResultController_Tag.ErrController)
if (result.tag == dojo.ResultControllerAccount_Tag.ErrControllerAccount)
{
Debug.LogWarning(result.err.message);
return null;
Expand All @@ -79,7 +79,7 @@ public static Task<Controller> Connect(string rpcUrl, Policy[] policies)
}
}

onConnectCallback = new dojo.FnPtr_ControllerPtr_Void((controllerPtr) =>
onConnectCallback = new dojo.FnPtr_ControllerAccountPtr_Void((controllerPtr) =>
{
var controller = new Controller(controllerPtr);
connectionTask.TrySetResult(controller);
Expand Down
4 changes: 2 additions & 2 deletions Assets/Dojo/Runtime/Torii/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ private object HandleCValue(dojo.Ty ty)
{"high", new BigInteger(MemoryMarshal.Cast<ulong, byte>(ty.primitive.u256).Slice(16, 16).ToArray())},
{"low", new BigInteger(MemoryMarshal.Cast<ulong, byte>(ty.primitive.u256).Slice(0, 16).ToArray())}
}),
dojo.Primitive_Tag.USize => ty.primitive.u_size,
dojo.Primitive_Tag.Felt252 => new FieldElement(ty.primitive.felt252),
dojo.Primitive_Tag.ClassHash => new FieldElement(ty.primitive.class_hash),
dojo.Primitive_Tag.ContractAddress => new FieldElement(ty.primitive.contract_address),
dojo.Primitive_Tag.EthAddress => new FieldElement(ty.primitive.eth_address),
_ => throw new Exception("Unknown primitive type: " + ty.primitive.tag)
},
dojo.Ty_Tag.ByteArray => ty.byte_array,
Expand Down Expand Up @@ -135,11 +135,11 @@ private object HandleWasmValue(WasmValue value)
{"high", new BigInteger(hexStringToByteArray(value.value.ToObject<string>().Substring(2, 32)).Reverse().ToArray())},
{"low", new BigInteger(hexStringToByteArray(value.value.ToObject<string>().Substring(34, 32)).Reverse().ToArray())}
}),
"usize" => value.value.ToObject<uint>(),
// these should be fine
"felt252" => new FieldElement(value.value.ToObject<string>()),
"classhash" => new FieldElement(value.value.ToObject<string>()),
"contractaddress" => new FieldElement(value.value.ToObject<string>()),
"ethaddress" => new FieldElement(value.value.ToObject<string>()),
_ => throw new Exception("Unknown primitive type: " + value.type_name)
},
_ => throw new Exception("Unknown type: " + value.type)
Expand Down
8 changes: 4 additions & 4 deletions Assets/Dojo/Runtime/Torii/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,15 @@ public struct Primitive
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string? U256;
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public uint? USize;
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public bool? Bool;
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public FieldElement? Felt252;
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public FieldElement? ClassHash;
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public FieldElement? ContractAddress;
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public FieldElement? EthAddress;

public dojo.Primitive ToNative()
{
Expand All @@ -371,8 +371,6 @@ public dojo.Primitive ToNative()
return new dojo.Primitive { tag = dojo.Primitive_Tag.U128, u128 = U128.Inner.data };
if (U256 != null)
throw new NotImplementedException("U256 conversion not implemented");
if (USize.HasValue)
return new dojo.Primitive { tag = dojo.Primitive_Tag.USize, u_size = USize.Value };
if (Bool.HasValue)
return new dojo.Primitive { tag = dojo.Primitive_Tag.Bool, bool_ = Bool.Value };
if (Felt252 != null)
Expand All @@ -381,6 +379,8 @@ public dojo.Primitive ToNative()
return new dojo.Primitive { tag = dojo.Primitive_Tag.ClassHash, class_hash = ClassHash.Inner };
if (ContractAddress != null)
return new dojo.Primitive { tag = dojo.Primitive_Tag.ContractAddress, contract_address = ContractAddress.Inner };
if (EthAddress != null)
return new dojo.Primitive { tag = dojo.Primitive_Tag.EthAddress, eth_address = EthAddress.Inner };

throw new InvalidOperationException("Primitive must have one non-null value");
}
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// To disable generating this file set `isEnabledGenerateAssemblyAttributes` to `false` in the config file for generating C# code.
// <auto-generated>
// This code was generated by the following tool on 2025-01-30 11:15:25 GMT+07:00:
// This code was generated by the following tool on 2025-02-19 15:26:36 GMT+08:00:
// https://github.com/bottlenoselabs/c2cs (v0.0.0.0)
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
Expand Down
2 changes: 1 addition & 1 deletion Assets/Dojo/Runtime/bindings/client/Runtime.gen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// To disable generating this file set `isEnabledGeneratingRuntimeCode` to `false` in the config file for generating C# code.

// <auto-generated>
// This code was generated by the following tool on 2025-01-30 11:15:25 GMT+07:00:
// This code was generated by the following tool on 2025-02-19 15:26:36 GMT+08:00:
// https://github.com/bottlenoselabs/c2cs (v0.0.0.0)
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
Expand Down
Loading

0 comments on commit 759de9b

Please sign in to comment.