Skip to content

Commit

Permalink
Merge Unity files into this repo
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Aug 5, 2024
1 parent 7d94b10 commit 6859594
Show file tree
Hide file tree
Showing 67 changed files with 673 additions and 171 deletions.
192 changes: 66 additions & 126 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,137 +1,77 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/main/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/

# MemoryCaptures can get excessive in size.
# They also could contain extremely sensitive data
/[Mm]emoryCaptures/

# Recordings can get excessive in size
/[Rr]ecordings/

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
/[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.unityproj
*.suo
*.user
*.sln.docstates

# Build results

[Dd]ebug/
[Rr]elease/
x64/
[Bb]in/
[Oo]bj/

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*

*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.user
*.userprefs
*.pidb
*.log
*.svclog
*.scc

# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile

# Visual Studio profiler
*.psess
*.vsp
*.vspx

# Guidance Automation Toolkit
*.gpState

# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user

# Click-Once directory
publish/

# Publish Web Output
*.Publish.xml
*.pubxml
*.azurePubxml

# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
packages/
## TODO: If the tool you use requires repositories.config, also uncomment the next line
!packages/repositories.config

# Windows Azure Build Output
csx/
*.build.csdef

# Windows Store app package directory
AppPackages/

# Others
sql/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
![Ss]tyle[Cc]op.targets
~$*
*~
*.dbmdl
*.[Pp]ublish.xml
.vs

*.publishsettings

# RIA/Silverlight projects
Generated_Code/

# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# SQL Server files
App_Data/*.mdf
App_Data/*.ldf
# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# =========================
# Windows detritus
# =========================
# Unity3D generated file on crash reports
sysinfo.txt

# Windows image file caches
Thumbs.db
ehthumbs.db
# Builds
*.apk
*.aab
*.unitypackage
*.app

# Folder config file
Desktop.ini
# Crashlytics generated file
crashlytics-build.properties

# Recycle Bin used on file shares
$RECYCLE.BIN/
# Packed Addressables
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*

# Mac desktop service store files
.DS_Store
# Temporary auto-generated Android Assets
/[Aa]ssets/[Ss]treamingAssets/aa.meta
/[Aa]ssets/[Ss]treamingAssets/aa/*

_NCrunch*
bin
obj

# JetBrains Ignore
.idea/
# Note: some DLLs are explicitly added via `git add -f <file>` to avoid being ignored by this rule.
# Those are required by Unity to stay in the Git repo.
/packages
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}
133 changes: 133 additions & 0 deletions README.dotnet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# SpacetimeDB SDK for C#

## Overview

This repository contains the [C#](https://learn.microsoft.com/en-us/dotnet/csharp/) SDK for SpacetimeDB. The SDK allows to interact with the database server and is prepared to work with code generated from a SpacetimeDB backend code.

## Documentation

The C# SDK has a [Quick Start](https://spacetimedb.com/docs/client-languages/csharp/csharp-sdk-quickstart-guide) guide and a [Reference](https://spacetimedb.com/docs/client-languages/csharp/csharp-sdk-reference).

## Installation

The SDK is available as a [NuGet Package](https://www.nuget.org/packages/SpacetimeDB.ClientSDK). To install it, follow these steps:

1. Open the NuGet package manager in Visual Studio.
2. Search for `SpacetimeDB.ClientSDK`.
3. Click the install button.

Alternatively, it can be installed on the command line using the `dotnet` command:

```bash
dotnet add package SpacetimeDB.ClientSDK
```

## Usage

### Access the SpacetimeDB Client

The SpacetimeDB client is created automatically as a singleton and accessible via the `SpacetimeDBClient.instance` property.

### Connecting to SpacetimeDB

To connect to SpacetimeDB, you need to call the `Connect` method on the `SpacetimeDBClient` class. The `Connect` method takes the following parameters:

- `token`: The authentication token to use to connect to SpacetimeDB. This token is generated by the backend code and is used to authenticate the client.
- `hostName`: The hostname of the SpacetimeDB server. This is the same hostname that you use to connect to the SpacetimeDB web interface.
- `moduleAddress`: The address of the module to connect to. This is the same address that you use to connect to the SpacetimeDB web interface.
- `sslEnabled`: Whether to use SSL to connect to SpacetimeDB. This is the same value that you use to connect to the SpacetimeDB web interface.

Example:

```csharp
using SpacetimeDB;

SpacetimeDBClient.instance.Connect(TOKEN, HOST, DBNAME, SSL_ENABLED);
```

### AuthToken optional helper class

The `AuthToken` class is a helper class that can be used to store the local client's authentication token locally to your user's home directory.

```csharp
using SpacetimeDB;

AuthToken.Init(".spacetime_csharp_quickstart");

SpacetimeDBClient.instance.Connect(AuthToken.Token, HOST, DBNAME, SSL_ENABLED);

void OnIdentityReceived(string authToken, Identity identity)
{
local_identity = identity;
AuthToken.SaveToken(authToken);
}
SpacetimeDBClient.instance.onIdentityReceived += OnIdentityReceived;
```

### Subscribing to tables

To subscribe to a table, you need to call the `Subscribe` method on the `SpacetimeDBClient` class. The `Subscribe` method takes a list of queries as a parameter. The queries are the same queries that you use to subscribe to tables in the SpacetimeDB web interface.

### Listening to events

To listen to events, you need to register callbacks on the `SpacetimeDBClient` class. The following callbacks are available:

- `onConnect`: Called when the client connects to SpacetimeDB.
- `onConnectError`: Called when the client fails to connect to SpacetimeDB.
- `onDisconnect`: Called when the client disconnects from SpacetimeDB.
- `onIdentityReceived`: Called when the client receives its identity from SpacetimeDB.
- `onSubscriptionApplied`: Called when the client receives the initial data from SpacetimeDB after subscribing to tables.

You can register for row update events on a table. To do this, you need to register callbacks on the table class. The following callbacks are available:

- `OnInsert`: Called when a row is inserted into the table.
- `OnUpdate`: Called when a row is updated in the table.
- `OnBeforeDelete`: Called before a row is deleted from the table.
- `OnDelete`: Called when a row is deleted from the table.

Example:

```csharp
using SpacetimeDB.Types;

PlayerComponent.OnInsert += PlayerComponent_OnInsert;
PlayerComponent.OnUpdate += PlayerComponent_OnUpdate;
PlayerComponent.OnDelete += PlayerComponent_OnDelete;
PlayerComponent.OnBeforeDelete += PlayerComponent_OnBeforeDelete;
```

You can register for reducer call updates as well.

- `OnREDUCEREvent`: Called when a reducer call is received from SpacetimeDB. (If a) you are subscribed to the table that the reducer modifies or b) You called the reducer and it failed)

Example:

```csharp
using SpacetimeDB.Types;

Reducer.OnMovePlayerEvent += Reducer_OnMovePlayerEvent;
```

### Accessing the client cache

The client cache is a local cache of the data that the client has received from SpacetimeDB. The client cache is automatically updated when the client receives updates from SpacetimeDB.

When you run the CLI generate command, SpacetimeDB will automatically generate a class for each table in your database. These classes are generated in the `SpacetimeDB.Types` namespace. Each class contains a set of static methods that allow you to query the client cache. The following methods are available:

- `int Count()`: Returns the number of rows in the table.
- `IEnumerable<TableRow> Iter()`: Returns an iterator over the table.
- `IEnumerable<TableRow> FilterByCOLUMN(ColumnValue)`: Filters the table by the specified column value.
- `TableRow? FindByCOLUMN(ColumnValue)`: Finds a single item by the specifed column value.
- `IEnumerable<TableRow> Query(Func<TableRow, bool>)`: Filters the table with the specified predicate.

### Calling Reducers

To call a reducer, you need to call the autogenerated method on the `Reducer` class. The autogenerated method takes the reducer arguments as parameters. The reducer arguments are the same arguments that are expected in your server module.

Example:

```csharp
using SpacetimeDB.Types;

Reducer.MovePlayer(new StdbVector2(0.0f, 0.0f), new StdbVector2(1.0f, 1.0f));
```
Loading

0 comments on commit 6859594

Please sign in to comment.