Giter VIP home page Giter VIP logo

unity-sdk's Introduction

Loom Network SDK for Unity

This repo contains the SDK code and a Unity 2017.4 project that provides examples.

The SDK currently supports the following Unity targets:

  • Desktop Win/macOS/Linux
  • Android
  • iOS
  • WebGL

Requirements

  • Unity 2017.4 or later.
  • Build Settings -> Player Settings -> Configuration set as follows:
    • Scripting Runtime Version: Experimental (.NET 4.6 Equivalent)
    • API Compatibility Level: .NET 4.6

Overview

LoomChainClient provides the means to communicate with a Loom DAppChain from a Unity game, while the Contract class provides a more convenient abstraction that streamlines invoking methods on a specific smart contract running on a DAppChain.

If you haven't done so already now would be a good time to read through the Unity SDK Quickstart.

Adding new transaction types

Create a new .proto file in the Assets\Protobuf directory, refer to Google Protocol Buffers for syntax details etc. You'll need to download the protoc compiler to generate C# classes from the .proto files.

The relevant command will look something like this:

protoc \
  -I<install path>/protoc-3.5.1/include \
  -I<project path>/Assets/Protobuf \
  --csharp_out=<project path>/Assets/Protobuf \
  <project path>/Assets/Protobuf/sample.proto

Samples

The sample authSample scene expects a local Loom DAppChain node to be running on localhost, if you decide to change the default host/ports the node runs on you'll need to update the host/ports in authSample.cs to match.

When you run the sample scene you will see three buttons that are hooked up to call the corresponding methods in Assets/authSample.cs, these must be pressed in the correct order:

  1. Press the Sign In button to generate a new random identity, once you've signed up/in you should see the text above the button change to Signed in as ....
  2. Once the textbox indicates you're signed in you can press the Call SetMsg button to call the SetMsg method in the BluePrint smart contract, this method will store a key/value in the smart contract state. If the method executes without error the textbox should change to Smart contract method finished executing.. You can press the Call SetMsg button again to make another call. Each call to the smart contract actually requires a new transaction to be generated, signed, and then validated by the DAppChain.
  3. Press the Call SetMsgEcho button to call the SetMsgEcho method in the BluePrint smart contract, similarly to SetMsg this method will store a key/value in the smart contract, and return the key/value it stored.
  4. You can press the Call GetMsg button to send a simple query to the BluePrint contract.

Building the SDK

BuildScripts folder contains scripts for building loom-unity-sdk.unitypackage for Windows and macOS. Scripts use the UNITY_PATH environment variable to determine the Unity executable path, if it is set; alternatively, the path can be passed as an argument. As a fallback, standard Unity installation directory might be used. The package is built to Assets\~NonVersioned\loom-unity-sdk.unitypackage.

Dependencies

Here are some notes on the dependencies used and how to update them.

Chaos.NaCl

Download the latest Chaos.NaCl package from NuGet, open as a ZIP archive, use the lib\net40\Chaos.NaCl.dll assembly.

Google Protocol Buffers

Download the latest Google.Protobuf package from NuGet, open as a ZIP archive, use the lib\net45\Google.Protobuf.dll assembly.

Websocket-Sharp

Clone https://github.com/sta/websocket-sharp, build the Release configuration, the output assembly is bin\Release\websocket-sharp.dll.

Nethereum

Download the latest net461dllsAOT.zip release artifact from https://github.com/Nethereum/Nethereum/releases. Only those assemblies are used by the SDK:

Nethereum.ABI.dll
Nethereum.Contracts.dll
Nethereum.Hex.dll
Nethereum.JsonRpc.Client.dll
Nethereum.Model.dll
Nethereum.RLP.dll
Nethereum.RPC.dll
Nethereum.Util.dll
BouncyCastle.Crypto.dll
Common.Logging.Core.dll
Newtonsoft.Json.dll

Dependecy Namespace Prefixing

The SDK contains quite a bit of dependencies, so there is a high chance that a Unity project will contain another version of a dependency for other purposes. Since Unity doesn't have any dependency management for third-party SDKs yet, all dependencies have Loom. prefix added to their namespaces. To do this, AssemblyNamespaceChanger tool is used. The relevant version is already present n the repo.

  1. Acquire/build all dependency assemblies.
  2. Copy them into the BuildScripts\PrefixDependencies\Original folder.
  3. Run the BuildScripts\PrefixDependencies\prefix-dependencies.cmd script. It will prefix the assemblies and put them into the BuildScripts\PrefixDependencies\Processed folder.
  4. Copy the prefixed assemblies from BuildScripts\PrefixDependencies\Processed to UnityProject\Assets\LoomSDK\Plugins.

unity-sdk's People

Contributors

dziknsk avatar enlight avatar eriiond avatar mattkanwisher avatar zimm-lostpolygon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unity-sdk's Issues

No way to get the logs

Currently, it is only possible to get new events that are triggered during a connection session. However, it is not possible to get logs of previous events.

Rename `Contract` to `PluginContract`

Since we move from plugin-based contracts more towards EVM-based contracts, I think it makes sense to rename Contract into a more specific PluginContract, because otherwise it can be assumed that plugin-based contracts are some kind of "recommended default".

Golang events are not parsed at all.

This is Contract.cs method which is used to pre-process event before emitting it to listeners.

protected override ChainEventArgs TransformChainEvent(RawChainEventArgs e) {
    string jsonRpcEventString = Encoding.UTF8.GetString(e.Data);
    JsonRpcEvent jsonRpcEvent = JsonConvert.DeserializeObject<JsonRpcEvent>(jsonRpcEventString);
    byte[] eventData = Encoding.UTF8.GetBytes(jsonRpcEvent.Data);

    return new ChainEventArgs(
         e.ContractAddress,
         e.CallerAddress,
         e.BlockHeight,
         eventData,
         jsonRpcEvent.Method
    );
}

When i emit event in Go either using ctx.Emit() or ctx.EmitTopics() i have same result:

jsonRpcEventString has serialized json from Emit for instance

type AccountCreatedEvt struct {
	Test  string
	Test2 int
}

Will result as {"Test": "test", "Test2": 1488}. So the next line fails to deserialize
JsonRpcEvent jsonRpcEvent = JsonConvert.DeserializeObject<JsonRpcEvent>(jsonRpcEventString);

Resulting in converting null to byte[]

I wonder whether it is problem of Loom Node emitting event in the wrong format or something else.

Revert back to System.Numerics.BigInteger instead of Org.BouncyCastle.Math.BigInteger

For EVM contracts, Unity SDK currently uses Org.BouncyCastle.Math.BigInteger instead of System.Numerics.BigInteger, since due to a Unity bug, System.Numerics is not referenced in generated C# projects. This is a major inconvenience, since Nethereum had to be modified to use Org.BouncyCastle.Math.BigInteger, and, to be honest, Org.BouncyCastle.Math.BigInteger is much worse compared to the native .NET System.Numerics.BigInteger in terms of interface and convenience.

However, I accidentally found out that Unity has a callback that is called after C# project are generated. This allows to add System.Numerics reference by trivially modifying the projects:
https://gist.github.com/ZimM-LostPolygon/13c4a196f241072939d477998278948c

If we add this to the SDK, we won't have to maintain a fork of Nethereum that uses Org.BouncyCastle.Math.BigInteger, and everyone will be able to use System.Numerics.BigInteger, which is much nicer. The downside, obviously, is that the any existing C# code that uses Org.BouncyCastle.Math.BigInteger will break. This is unfortunate, but I believe using the native System.Numerics.BigInteger type is beneficial enough to justify that.

@enlight What do you think?

SDK is imported into every sample repo

Currently, samples just include a copy of the SDK, which works, but is messy. People seem to just download the sample and rip the SDK out of it, even if it is outdated already.

We need to have a way to easily keep the SDK up-to-date in samples. Proposal: remove the SDK from samples, and instead have a script that will bootstrap the SDK - download the latest package from GitHub releases (or any other place we can host it) and import it into the sample project.

The downside is that the sample code might be using some APIs that are not present in the latest SDK anymore, but I think it's fair to say this is not going to happen often, and we have to keep the samples working with the latest SDK, otherwise there's little use for them in terms of learning how to use the SDK.

Here is the current version of the bootstrapper:
https://raw.githubusercontent.com/loomnetwork/unity3d-sdk/sample-bootstrapper/UnityProject/Assets/LoomSDKBootstrapper/Editor/LoomSdkBootstrapper.cs

It downloads the latest .unitypackage from GitHub releases of this repo (we don't have any ATM) and imports it into the project. Since the bootstrapper should be distributed as a .dll, rather than source code, Unity will load even if the compilation of sample fails, which it will, since there is initially no SDK in the samples. The .dll can be grabbed from \UnityProject\Library\ScriptAssemblies\LoomSDKBootstrapper.dll.

Implement CreateFilterAsync and GetFilterChanges

It would be very helpful to be able to query the logs of past events.
Possibly duplicated issue with #12

Would love something like this:

var event = contract.GetEvent("EventName");

 // Make some filters
var grabAll = await multiplyEvent.CreateFilterAsync();
var filter = await multiplyEvent.CreateFilterAsync(indexedEventFieldValue);

// Get the logs
var allLogs = await multiplyEvent.GetFilterChanges<EventType>(grabAll);
var logs = await multiplyEvent.GetFilterChanges<EventType>(filter);`

Which is exactly as it is in here:
https://nethereum.readthedocs.io/en/latest/contracts/calling-transactions-events/
Under the 'Events, filters and topics' section.

It would be great if we could at least grab the logs without filtering.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.