ResoniteBridgeLib 1.2.0

dotnet add package ResoniteBridgeLib --version 1.2.0
                    
NuGet\Install-Package ResoniteBridgeLib -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ResoniteBridgeLib" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ResoniteBridgeLib" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="ResoniteBridgeLib" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ResoniteBridgeLib --version 1.2.0
                    
#r "nuget: ResoniteBridgeLib, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ResoniteBridgeLib@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ResoniteBridgeLib&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=ResoniteBridgeLib&version=1.2.0
                    
Install as a Cake Tool

ResoniteBridgeLib

A connector that lets you communicate between Resonite mods and other applications, using MemoryMappedFileIPC

Usage

First, select a folder that'll store all the current connections.

You can just use something like

string serverDirectory = 
	Path.Combine(
		Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), 
		"YourApplication",
		"IPCConnections",
		"Servers"
	);

Also pick a channel name, this should be unique to your mod.

string channelName = "myModChannel";

Resonite side:

Your mod needs to create a server.

ResoniteBridgeServer bridgeServer = new ResoniteBridgeServer(
	channelName,
	serverDirectory,
	(string msg) =>
{
	// this is just for debug messages, if you want them
	// you can also comment this out
	Msg("ResoniteBridgeLib logging:" + msg);
});

Now you can register functions to be called. For example:

string functionLabel = "TestFunction";
static byte[] SimpleTestFunc(byte[] inputBytes) {
	byte[] responseBytes = ...; // make response bytes
	return responseBytes;
}
server.RegisterProcessor(fuctionLabel, SimpleTestFunc);

Other application side

ResoniteBridgeClient bridgeClient = new ResoniteBridgeClient(
	channelName,
	serverDirectory, 
	(string message) => { Debug.Log(message); } // this is for unity, modify this for your own logging
);

Then to send our message

byte[] inputBytes = ...; // make input bytes
int timeout = -1; // for no timeout, take as long as they want
bridgeClient.SendMessageSync(
	"TestFunction",
	encoded,
	timeout,
	out byte[] outBytes,
	out bool isError
	);
if (isError)
{
    throw new Exception(ResoniteBridgeUtils.DecodeString(outBytes));
}
else
{
	// process outBytes
}

Remember to call

bridgeClient.Dispose();

when you are doing using it.

Serialization

This works using byte[], but in practice it's easier to work with objects or strings.

To support this I have a few helper methods in SerializationUtils.

Strings

You can use SerializationUtils.EncodeString and SerializationUtils.DecodeString.

Objects

You can serialize objects using Newtonsoft.Json's bson.

However, I find if you have large arrays of primitive types or structs this can be slow.

To get around this, I implemented my own serialization that is much faster (50x faster than bson for large arrays).

It's faster because it just directly copies the raw bytes of those large arrays using Marshal.

It only works for structs with [StructLayout(LayoutKind.Sequential)] with fields that are either:

  • A: primitive types
  • B: T[] of primitive types
  • C: T[] of struct satisfying the above
  • D: a struct with fields satisfying A-C or D

But this isn't that big of a limitation in practice.

For example, here is a valid struct:

[StructLayout(LayoutKind.Sequential)]
public struct Float3_Example
{
 public float x;
 public float y;
 public float z;
}

To encode, just do

Float3_Example example = new Float3_Example() {
	x=0,
	y=1,
	z=2
};
byte[] inputBytes = SerializationUtils.EncodeObject(exampleObj);

Then to decode:

Float3_Example inputObject = SerializationUtils.DecodeObject<Float3_Example>(inputBytes);

Modifying Resonite

The functions you register will be occuring on a seperate thread than resonite, and will throw an error if you try and do something like add a Slot.

To fix this, I recommend following this pattern.

First, define some helpers:

public class OutputBytesHolder
{
    public byte[] outputBytes;
}

static IEnumerator<FrooxEngine.Context> ActionWrapper(IEnumerator<FrooxEngine.Context> action, System.Threading.Tasks.TaskCompletionSource<bool> completion)
{
    try
    {
        yield return FrooxEngine.Context.WaitFor(action);
    }
    finally
    {
        completion.SetResult(result: true);
    }
}

public static bool RunOnWorldThread(IEnumerator<FrooxEngine.Context> action)
{
    System.Threading.Tasks.TaskCompletionSource<bool> taskCompletionSource = new System.Threading.Tasks.TaskCompletionSource<bool>();
    Engine.Current.WorldManager.FocusedWorld.RootSlot.StartCoroutine(ActionWrapper(action, taskCompletionSource));
    return taskCompletionSource.Task.GetAwaiter().GetResult();
}

Now you can do something like this:

[StructLayout(LayoutKind.Sequential)]
public struct RefID_Example
{
    public ulong id;
}
static IEnumerator<FrooxEngine.Context> AddSlotFuncHelper(byte[] inputBytes, OutputBytesHolder outputBytes)
{
    // move to background thread (optional, useful if you are doing heavy stuff)
    yield return FrooxEngine.Context.ToBackground();
    // move to world thread (necessary if we want to modify the world at all)
    yield return Context.ToWorld();
    string slotName = ResoniteBridgeUtils.DecodeString(inputBytes);
    Slot resultSlot = Engine.Current.WorldManager.FocusedWorld.RootSlot.AddSlot(slotName);
    RefID_Example result = new RefID_Example()
    {
        id = (ulong)resultSlot.ReferenceID
    };
    outputBytes.outputBytes = ResoniteBridgeUtils.EncodeObject(result);
}

// this is the function that we register
public byte[] AddSlotFunc(byte[] inputBytes)
{
    OutputBytesHolder outputHolder = new OutputBytesHolder();
    RunOnWorldThread(AddSlotFuncHelper(inputBytes, outputHolder));
    return outputHolder.outputBytes;
}
Product Compatible and additional computed target framework versions.
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.7.2

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 286 3/7/2025
1.1.6 144 2/26/2025
1.1.5 141 2/26/2025
1.1.4 136 2/26/2025
1.1.3 140 2/26/2025
1.1.2 137 2/25/2025
1.1.1 135 2/25/2025
1.1.0 141 2/25/2025
1.0.10 146 2/25/2025
1.0.9 144 2/25/2025
1.0.8 151 2/20/2025
1.0.7 144 2/6/2025
1.0.6 134 2/5/2025
1.0.5 136 2/5/2025
1.0.4 167 2/3/2025
1.0.3 147 2/2/2025
1.0.2 152 2/2/2025
1.0.1 152 1/30/2025
1.0.0 134 1/27/2025