Skip to content

Commit

Permalink
Rewrite C# codegen to the new Lang infra + fixes (#220)
Browse files Browse the repository at this point in the history
## Description of Changes

This is the companion PR for
clockworklabs/SpacetimeDB#2184, please see the
other PR for full description.

On the client side main changes are:

- Regenerate .NET and Unity test client bindings and test snapshot.
- Remove `IDatabaseRow` since V9 multi-tables splits data types from
actual table definitions, so those "table data types" are no longer
special. Just using `IStructuralReadWrite` in its place now.
- Add base index classes as mentioned in the other PR.
- As a sub-improvement, the non-unique index class actually does
indexing instead of iterating over the entire table like we did before.
- Remove internal-but-not-really `InternalInvokeValueDeleted` and
`InternalInvokeValueInserted` methods in favour of private events.

## API

 - [x] This is an API breaking change to the SDK

Removes some technically-visible but internal APIs.

## Requires SpacetimeDB PRs
clockworklabs/SpacetimeDB#2184

## Testsuite
*If you would like to run the your SDK changes in this PR against a
specific SpacetimeDB branch, specify that here. This can be a branch
name or a link to a PR.*

SpacetimeDB branch name: ingvar/csharp-new-codegen

## Testing
*Write instructions for a test that you performed for this PR*

- [x] Manually tested Blackholio

---------

Co-authored-by: James Gilles <[email protected]>
  • Loading branch information
RReverser and kazimuth authored Feb 5, 2025
1 parent 23d70b6 commit 20c6480
Show file tree
Hide file tree
Showing 113 changed files with 1,763 additions and 1,637 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ jobs:

- name: Check for changes
run: |
git diff --exit-code unity-tests~/client/Assets/Scripts/autogen || {
git diff --exit-code unity-tests~/client-unity/Assets/Scripts/autogen || {
echo "Error: Bindings are dirty. Please generate bindings again and commit them to this branch."
exit 1
}
Expand All @@ -135,7 +135,7 @@ jobs:
bash ./publish.sh
- name: Patch com.clockworklabs.spacetimedbsdk dependency in manifest.json
working-directory: unity-tests~/client/Packages
working-directory: unity-tests~/client-unity/Packages
run: |
# Replace the com.clockworklabs.spacetimedbsdk dependency with the current branch.
# TODO: find out why pointing to a local directory doesn't work - is it because Unity CI action uses Docker?
Expand All @@ -144,15 +144,15 @@ jobs:
- uses: actions/cache@v3
with:
path: unity-tests~/client/Library
path: unity-tests~/client-unity/Library
key: Unity-${{ github.head_ref }}
restore-keys: Unity-

- name: Run Unity tests
uses: game-ci/unity-test-runner@v4
with:
unityVersion: 2022.3.32f1 # Adjust Unity version to a valid tag
projectPath: unity-tests~/client # Path to the Unity project subdirectory
projectPath: unity-tests~/client-unity # Path to the Unity project subdirectory
githubToken: ${{ secrets.GITHUB_TOKEN }}
testMode: playmode
useHostNetwork: true
Expand Down
13 changes: 13 additions & 0 deletions DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,16 @@

To regenerate this namespace, run the `tools~/gen-client-api.sh` or the
`tools~/gen-client-api.bat` script.

## Developing against a local clone of SpacetimeDB
When developing against a local clone of SpacetimeDB, you'll need to ensure that the packages here can find an up-to-date version of the BSATN.Codegen and BSATN.Runtime packages from SpacetimeDB.

To develop against a local clone of SpacetimeDB at `../SpacetimeDB`, run the following command:

```sh
dotnet pack ../SpacetimeDB/crates/bindings-csharp/BSATN.Runtime && ./tools~/write-nuget-config.sh ../SpacetimeDB
```

This will create a (`.gitignore`d) `nuget.config` file that uses the local build of the package, instead of the package on NuGet.

You'll need to rerun this command whenever you update `BSATN.Codegen` or `BSATN.Runtime`.
10 changes: 10 additions & 0 deletions examples~/quickstart/client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Quickstart client
See [SpacetimeDB](https://github.com/clockworklabs/SpacetimeDB)/modules/quickstart-chat

## Regenerating bindings

To regenerate bindings: clone SpacetimeDB next to this repo, then in the root of this repo:

```bash
tools~/gen-quickstart.sh ../SpacetimeDB
```
43 changes: 0 additions & 43 deletions examples~/quickstart/client/module_bindings/Message.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.

#nullable enable

using System;
using SpacetimeDB.ClientApi;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void IdentityConnectedHandler(EventContext ctx);
public event IdentityConnectedHandler? OnIdentityConnected;

public bool InvokeIdentityConnected(EventContext ctx, Reducer.IdentityConnected args)
{
if (OnIdentityConnected == null) return false;
OnIdentityConnected(
ctx
);
return true;
}
}

public abstract partial class Reducer
{
[SpacetimeDB.Type]
[DataContract]
public sealed partial class IdentityConnected : Reducer, IReducerArgs
{
string IReducerArgs.ReducerName => "identity_connected";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.

#nullable enable

using System;
using SpacetimeDB.ClientApi;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void IdentityDisconnectedHandler(EventContext ctx);
public event IdentityDisconnectedHandler? OnIdentityDisconnected;

public bool InvokeIdentityDisconnected(EventContext ctx, Reducer.IdentityDisconnected args)
{
if (OnIdentityDisconnected == null) return false;
OnIdentityDisconnected(
ctx
);
return true;
}
}

public abstract partial class Reducer
{
[SpacetimeDB.Type]
[DataContract]
public sealed partial class IdentityDisconnected : Reducer, IReducerArgs
{
string IReducerArgs.ReducerName => "identity_disconnected";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.

#nullable enable

using System;
using SpacetimeDB.ClientApi;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void SendMessageHandler(EventContext ctx, string text);
public event SendMessageHandler? OnSendMessage;

public void SendMessage(string text)
{
conn.InternalCallReducer(new Reducer.SendMessage(text), this.SetCallReducerFlags.SendMessageFlags);
}

public bool InvokeSendMessage(EventContext ctx, Reducer.SendMessage args)
{
if (OnSendMessage == null) return false;
OnSendMessage(
ctx,
args.Text
);
return true;
}
}

public abstract partial class Reducer
{
[SpacetimeDB.Type]
[DataContract]
public sealed partial class SendMessage : Reducer, IReducerArgs
{
[DataMember(Name = "text")]
public string Text;

public SendMessage(string Text)
{
this.Text = Text;
}

public SendMessage()
{
this.Text = "";
}

string IReducerArgs.ReducerName => "send_message";
}
}

public sealed partial class SetReducerFlags
{
internal CallReducerFlags SendMessageFlags;
public void SendMessage(CallReducerFlags flags) => SendMessageFlags = flags;
}
}
62 changes: 62 additions & 0 deletions examples~/quickstart/client/module_bindings/Reducers/SetName.g.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.

#nullable enable

using System;
using SpacetimeDB.ClientApi;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void SetNameHandler(EventContext ctx, string name);
public event SetNameHandler? OnSetName;

public void SetName(string name)
{
conn.InternalCallReducer(new Reducer.SetName(name), this.SetCallReducerFlags.SetNameFlags);
}

public bool InvokeSetName(EventContext ctx, Reducer.SetName args)
{
if (OnSetName == null) return false;
OnSetName(
ctx,
args.Name
);
return true;
}
}

public abstract partial class Reducer
{
[SpacetimeDB.Type]
[DataContract]
public sealed partial class SetName : Reducer, IReducerArgs
{
[DataMember(Name = "name")]
public string Name;

public SetName(string Name)
{
this.Name = Name;
}

public SetName()
{
this.Name = "";
}

string IReducerArgs.ReducerName => "set_name";
}
}

public sealed partial class SetReducerFlags
{
internal CallReducerFlags SetNameFlags;
public void SetName(CallReducerFlags flags) => SetNameFlags = flags;
}
}
Loading

0 comments on commit 20c6480

Please sign in to comment.