|
1 |
| -# CsharpBlankProject |
2 |
| -This is a SpatialOS project which can serve as a template for building SpatialOS workers using the C# SDK. |
| 1 | +# SpatialOS C# Blank project |
| 2 | + |
| 3 | +## Quick start |
| 4 | + |
| 5 | +Build the project and start it with the default launch configuration: |
| 6 | +``` |
| 7 | +spatial worker build |
| 8 | +spatial local launch |
| 9 | +``` |
| 10 | + |
| 11 | +Connects a new instance of the C# External worker to a running local |
| 12 | +deployment. |
| 13 | +``` |
| 14 | +spatial local worker launch External local |
| 15 | +``` |
| 16 | + |
| 17 | +See sections below for more details. |
| 18 | + |
| 19 | +## Troubleshooting |
| 20 | + |
| 21 | +If you encounter any errors when trying to build or launch this project, |
| 22 | +firstly make sure that all the dependencies of SpatialOS are met by running |
| 23 | +`spatial diagnose`. |
| 24 | + |
| 25 | +Warnings about missing files from `msbuild` during `spatial worker build` might |
| 26 | +mean you don't have the required C++ build tools in your installation of |
| 27 | +`msbuild` if it came with Visual Studio. You will need to modify your |
| 28 | +installation of Visual Studio to include [C++ build |
| 29 | +tools](http://landinghub.visualstudio.com /visual-cpp-build-tools). |
| 30 | + |
| 31 | +Visual Studio has a weird way to pick default configuration and platform for |
| 32 | +builds. Because of that the default configuration when you open one of the |
| 33 | +solutions for the first time in Visual Studio will be `DebugLinux`. This might |
| 34 | +cause build errors on other platforms so use the dropdown in Visual Studio to |
| 35 | +select a configuration matching your platform. |
| 36 | + |
| 37 | +In case you need further assistance, don't hesitate to ask on the |
| 38 | +[forums](https://forums.improbable.io/c/sup/setup-and-tutorials) and remember |
| 39 | +to attach the contents of your `logs` folder and output from the failing |
| 40 | +commands. |
| 41 | + |
| 42 | +## Build |
| 43 | + |
| 44 | +Use `spatial worker build` as usual. |
| 45 | + |
| 46 | +The current worker configuration is using a modified version of the generated |
| 47 | +`spatialos.csharp.build.json` which replaces `xbuild` with `msbuild` and adds |
| 48 | +some extra steps. On old versions of Mono (before 5.0.0), even though it's best |
| 49 | +to just update, you might want to replace `msbuild` with `xbuild`. |
| 50 | + |
| 51 | +## Local launch |
| 52 | + |
| 53 | +Use `spatial local launch` as usual. |
| 54 | + |
| 55 | +Once the deloyment is running you can connect a worker with: |
| 56 | + |
| 57 | +``` |
| 58 | +spatial local worker launch External local |
| 59 | +``` |
| 60 | + |
| 61 | +`local` is an external launch configuration defined in |
| 62 | +`spatialos.External.worker.json`. Feel free to change it or add more |
| 63 | +configurations for different starting conditions. |
| 64 | + |
| 65 | +When a worker connects the terminal which runs `spatial local launch` should |
| 66 | +output several messages: |
| 67 | + |
| 68 | +The first two confirm that the connection is successful. |
| 69 | +```sh |
| 70 | +[improbable.bridge.oracle.service.BridgeOracleInternalServiceImpl] The worker ExternalLocalWindows registered with SpatialOS successfully. |
| 71 | + |
| 72 | +[improbable.bridge.logging.EngineLogMessageHandler] [Worker: ExternalLocalWindows] Successfully connected using the Receptionist -[WorkerLogger:Startup.cs] |
| 73 | +``` |
| 74 | + |
| 75 | +You could also have configurations which start multiple workers or start |
| 76 | +workers directly from an executable. Look at [External worker launch configuration](https://docs.improbable.io/reference/latest/workers/configuration/launch-configuration) for all the technical details. |
| 77 | + |
| 78 | +## Attaching a debugger |
| 79 | + |
| 80 | +The easiest option is using Visual Studio. Open the project properties and set |
| 81 | +the command-line arguments in `Debug > Start options` to something like |
| 82 | +`receptionist localhost 7777 ExternalDebug`. Then you can start the project |
| 83 | +from Visual Studio. If you're using another IDE there must be a similar way to |
| 84 | +configure and start the project with a visual debugger. |
| 85 | + |
| 86 | +## Project structure |
| 87 | + |
| 88 | +The C# workers which come with this project are called _External_ and _Managed_ |
| 89 | +to reflect the way they are configured and connected and their intended use as |
| 90 | +an external worker which is often used to implement a game client or a managed |
| 91 | +worker used for various tasks ranging from physics simulation to player login |
| 92 | +to inventory management and microservices. Have a look at the [Glossary entry |
| 93 | +for Workers](https://docs.improbable.io/reference/latest/getting-started/concepts/glossary#worker) for a quick intro and links to learning |
| 94 | +resources. |
| 95 | + |
| 96 | +If you're building your own worker out of them, you're strongly encouraged to |
| 97 | +replace the names with something meaningful for your use case. A simple Find |
| 98 | +and Replace in both file contents and file names of the text "External" is |
| 99 | +sufficient to rename the worker named "External" and keep the convention to use |
| 100 | +the worker name in project files and build targets. |
| 101 | + |
| 102 | +``` |
| 103 | ++-- schema/ |
| 104 | ++-- generated_code/csharp/ |
| 105 | ++-- dependencies/worker_sdk |
| 106 | ++-- workers |
| 107 | + |-- External/ |
| 108 | + | |-- External/ |
| 109 | + | |-- BuildTargets.targets |
| 110 | + | |-- External.targets |
| 111 | + | |-- External.csproj |
| 112 | + | |-- GeneratedCode.csproj |
| 113 | + | |-- External.sln |
| 114 | + | |-- spatialos.External.worker.json |
| 115 | + | |-- spatialos_worker_packages.json |
| 116 | + | |-- spatialos.csharp_msbuild.build.json |
| 117 | + | |
| 118 | + |-- Managed/ |
| 119 | + |-- Managed/ |
| 120 | + |-- BuildTargets.targets |
| 121 | + |-- Managed.targets |
| 122 | + |-- Managed.csproj |
| 123 | + |-- GeneratedCode.csproj |
| 124 | + |-- Managed.sln |
| 125 | + |-- spatialos.Managed.worker.json |
| 126 | + |-- spatialos_worker_packages.json |
| 127 | + |-- spatialos.csharp_msbuild.build.json |
| 128 | +``` |
| 129 | + |
| 130 | +The SpatialOS C# Blank project contains two Visual Studio solutions each with |
| 131 | +several C# projects. |
| 132 | + |
| 133 | + - `workers/External/External.sln` contains: |
| 134 | + - `External.csproj` with the worker sources |
| 135 | + - `GeneratedCode.csproj` with C# classes generated from schema sources |
| 136 | + |
| 137 | + - `workers/Managed/Managed.sln` follows the same structure as described for |
| 138 | + `External`. |
| 139 | + |
| 140 | +### More about the worker project structure |
| 141 | + |
| 142 | +The worker project `External.csproj` has its sources located in |
| 143 | +`workers/External/External`, dependencies located in `dependencies/worker_sdk`, |
| 144 | +and build targets for all platforms located in two places: |
| 145 | + |
| 146 | +- `workers/External/External/bin/x64` for the worker executables |
| 147 | +- `build/assembly/worker/External` for the packaged worker zips used by SpatialOS deployments |
| 148 | + |
| 149 | +The `GeneratedCode.csproj` has its sources located in `generated_code/csharp`, |
| 150 | +dependencies located also in `dependencies/worker_sdk`, and builds a |
| 151 | +`GeneratedCode.dll` assembly for each configuration located in |
| 152 | +`workers/External/External/bin/generated_code` and referenced by the worker |
| 153 | +project. |
| 154 | + |
| 155 | +### Why isn't there a single `GeneratedCode.csproj` shared by both solutions? |
| 156 | + |
| 157 | +This might seem reasonable with the current state of the project since all C# |
| 158 | +generated code is included in both projects. However, it's good to have the |
| 159 | +flexibility to include only the code which is actually referenced by the worker |
| 160 | +project. This results in smaller assemblies and faster build times when large |
| 161 | +schemas are present. |
| 162 | + |
| 163 | +## Cloud deployment |
| 164 | + |
| 165 | +As usual set the `project_name` field in `spatialos.json` to match your SpatialOS project name. Then upload and launch: |
| 166 | + |
| 167 | +``` |
| 168 | +spatial cloud upload <AssemblyName> |
| 169 | +spatial cloud launch <AssemblyName> default_launch.json <deploymentname> |
| 170 | +``` |
| 171 | + |
| 172 | +However, the launcher doesn't support using C# workers to start clients yet. You can still connect a client to the deployment by obtaining a login token from the launcher and passing it when starting the `cloud` worker external configuration. |
| 173 | + |
| 174 | +``` |
| 175 | +spatial local worker launch External cloud <deploymentname> <login_token> |
| 176 | +``` |
| 177 | + |
| 178 | +## Cross-platform builds |
| 179 | + |
| 180 | +With the current build configuration for workers (defined in |
| 181 | +`spatialos.csharp_msbuild.build.json`) running `spatial worker build` builds the |
| 182 | +release configurations for each of the 3 supported platforms. You can build for |
| 183 | +a specific platform only by passing the `target` flag: |
| 184 | + |
| 185 | +``` |
| 186 | +spatial worker build --target=ReleaseWindows |
| 187 | +``` |
| 188 | + |
| 189 | +This will skip all steps which have a target defined that is different from |
| 190 | +"ReleaseWindows". The target string is case-insensitive. |
| 191 | + |
| 192 | +If you want to be able to build the Debug configurations with `spatial` from |
| 193 | +the command-line, it's easy to add more steps and define the respective |
| 194 | +targets. You can already build and run all Debug configurations which are |
| 195 | +defined for the projects from Visual Studio or by using `msbuild` on the |
| 196 | +command-line. |
0 commit comments