Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Dojo sdk integration #156

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Binary file modified .DS_Store
Binary file not shown.
Binary file modified Assets/.DS_Store
Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/Dojo.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Dojo/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Assets/Dojo/Editor/DojoEditor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "DojoEditor",
"rootNamespace": "Dojo",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Assets/Dojo/Editor/DojoEditor.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Dojo/Prefabs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions Assets/Dojo/Prefabs/WorldManager.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &4900589733936057544
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 3269270482660702203}
- component: {fileID: 1648136030876143075}
- component: {fileID: 4477060482053654553}
m_Layer: 0
m_Name: WorldManager
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &3269270482660702203
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4900589733936057544}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1648136030876143075
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4900589733936057544}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 85bbcaeafdf5e41c6a2f48ff65820b7d, type: 3}
m_Name:
m_EditorClassIdentifier:
toriiUrl:
rpcUrl:
relayUrl:
relayWebrtcUrl:
worldAddress:
synchronizationMaster: {fileID: 4477060482053654553}
dojoConfig: {fileID: 11400000, guid: 1d6a5fa48aab79f0084b213f6fc768c5, type: 2}
--- !u!114 &4477060482053654553
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4900589733936057544}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 03336b1725e0144688810398fccab79e, type: 3}
m_Name:
m_EditorClassIdentifier:
worldManager: {fileID: 1648136030876143075}
limit: 100
OnSynchronized:
m_PersistentCalls:
m_Calls: []
OnEntitySpawned:
m_PersistentCalls:
m_Calls: []
7 changes: 7 additions & 0 deletions Assets/Dojo/Prefabs/WorldManager.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Dojo/Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Dojo/Runtime/Config.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Assets/Dojo/Runtime/Config/WorldManagerData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using Dojo.Starknet;
using Dojo.Torii;
using UnityEngine;

namespace Dojo
{
[CreateAssetMenu(fileName = "WorldManagerData", menuName = "ScriptableObjects/WorldManagerData", order = 2)]
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")]
public FieldElement worldAddress;
public Query query = new Query(100, 0);
}
}
11 changes: 11 additions & 0 deletions Assets/Dojo/Runtime/Config/WorldManagerData.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Assets/Dojo/Runtime/Config/WorldManagerLocalConfig.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: faed63f2427f3ed83a42d16bb3e73e6d, type: 3}
m_Name: WorldManagerLocalConfig
m_EditorClassIdentifier:
toriiUrl: http://localhost:8080
rpcUrl: http://localhost:5050
relayUrl: /ip4/127.0.0.1/tcp/9090
relayWebrtcUrl: /ip4/127.0.0.1/udp/9091/webrtc-direct/certhash/uEiAS9CpA5yNwO7iidBM5f9FcPl67PwohiK-1J9BhnFMrHg
worldAddress:
hex: 0x00e2ea9b5dd9804d13903edf712998943b7d5d606c139dd0f13eeb8f5b84da8d
query:
limit: 100
offset: 0
dont_include_hashed_keys: 0
8 changes: 8 additions & 0 deletions Assets/Dojo/Runtime/Config/WorldManagerLocalConfig.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Assets/Dojo/Runtime/DojoRuntime.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "DojoRuntime",
"rootNamespace": "Dojo",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": true,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Assets/Dojo/Runtime/DojoRuntime.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

153 changes: 153 additions & 0 deletions Assets/Dojo/Runtime/ModelInstance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Dojo.Starknet;
using Dojo.Torii;
using dojo_bindings;
using UnityEngine;
using UnityEngine.Events;
using System.Numerics;
using System.Reflection;
using Newtonsoft.Json;

namespace Dojo
{
[AttributeUsage(AttributeTargets.Field)]
public class ModelField : Attribute
{
public string Name { get; }

public ModelField(string name)
{
Name = name;
}
}

// Base class for the definition of a model
public abstract class ModelInstance : MonoBehaviour
{
public UnityEvent OnUpdated = new UnityEvent();
public Model Model { get; private set; }

// Initialize the model instance with the model
// Uses the ModelField attribute to map the model fields to the class fields
// One can override this method to do custom initialization
// If reflection isn't an option.
// Called upon instantiation and model update
public virtual void Initialize(Model model)
{
Model = model;

var fields = GetType().GetFields();
foreach (var field in fields)
{
// Check if the field has the ModelField attribute
var attribute = field.GetCustomAttributes(typeof(ModelField), false);
if (attribute.Length == 0)
{
continue;
}

var modelField = (ModelField)attribute[0];
if (!model.Members.ContainsKey(modelField.Name))
{
Debug.LogWarning($"Model field {modelField.Name} not found in model {model.Name}");
continue;
}

var member = model.Members[modelField.Name];

field.SetValue(this, HandleField(field.FieldType, member));
}
}

// Handles the initialization of a field
// of a model instance. Uses reflection to set the field
// to the value of the model member.
private static object HandleField(Type type, object ty)
{
// if the field is a primitive, we can just set it
// fieldelement is included as a primitive because its a class
// but its already instantiated
if (type.IsPrimitive || type == typeof(FieldElement) || type == typeof(BigInteger) || type == typeof(string))
{
return ty.GetType() != type ? Convert.ChangeType(ty, type) : ty;
}
// handle array
else if (type.IsArray)
{
var elementType = type.GetElementType();
var array = (IList<object>)ty;
var instance = Array.CreateInstance(elementType, array.Count);
for (var i = 0; i < array.Count; i++)
{
instance.SetValue(HandleField(elementType, array[i]), i);
}
return instance;
}
// handle tuple (ValueTuple - (T1, T2))
else if (type.FullName.StartsWith(typeof(ValueTuple).FullName))
{
var tupleTypes = type.GetGenericArguments();
var instance = Activator.CreateInstance(type);
var fields = type.GetFields();
for (var i = 0; i < fields.Length; i++)
{
fields[i].SetValue(instance, HandleField(tupleTypes[i], ((IList<object>)ty)[i]));
}
return instance;
}
// dynamic types
// handle record (rust-like) enums
else if (ty is Model.Enum enumVariant)
{
var variantType = type.GetNestedType(enumVariant.option);
if (variantType == null)
{
throw new Exception($"Could not find variant {enumVariant.option} in enum {type}");
}

if (type.GenericTypeArguments.Length > 0)
{
variantType = variantType.MakeGenericType(type.GenericTypeArguments);
}

List<object> args = new List<object>();
if (variantType.GetProperty("value") is PropertyInfo prop)
{
args.Add(HandleField(prop.PropertyType, enumVariant.value));
}

return Activator.CreateInstance(variantType, args.ToArray());

}
// if the field is a struct/class. we check if our member is a dictionary
// and we go through each of its keys and values and set them to the fields
// of the instantiated struct/class
else if (ty is Model.Struct struct_)
{
var instance = Activator.CreateInstance(type);
var fields = type.GetFields();

foreach (var field in fields)
{
field.SetValue(instance, HandleField(field.FieldType, struct_.members[field.Name]));
}

return instance;
}
else
{
throw new Exception($"Could not handle field of type {type}");
}
}

// Called when the model is updated
public virtual void OnUpdate(Model model)
{
Initialize(model);
OnUpdated.Invoke();
}
}
}
Loading