C# guide
How to use C# code generated by DeukPack in your project. Applicable to both .NET and Unity.
1. Code generation
Specify --csharp to generate C# from IDL.
npx deukpack ./schema.deuk ./gen --csharp --protocol tbinary
Output goes under gen/csharp/ (or your specified folder) as <stem>_deuk.cs per IDL source (suffix avoids clashing with hand-written types like Player.cs). Shared files such as MetaTableRegistry.g.cs keep their fixed names.
The runtime is required: reference dist/csharp from the npm package or the DeukPack.Protocol project from the DeukPack core repo. Generated code alone will not compile.
2. Adding project references
- Generated
.cs: Include all C# files from the output folder in your project, or reference the containing directory. - Runtime: Add
DeukPack.Protocol(or the equivalent runtime already in your solution) as a project reference.
Example (.csproj):
<ItemGroup>
<Compile Include="..\gen\csharp\**\*.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\path\to\DeukPack.Protocol\DeukPack.Protocol.csproj" />
</ItemGroup>
When using npm only: reference the runtime source at node_modules/deukpack/dist/csharp, or use your team's DeukPack runtime assembly.
3. Serialization & deserialization (read/write)
Generated types serialize via IDeukPackReader / IDeukPackWriter (or protocol-specific Reader/Writer). Select the protocol matching your --protocol tbinary (or tcompact, tjson, pack, json, …) at generation time.
Write example (concept):
var value = new DemoUser { Id = 1, Name = "Alice", Home = new DemoPoint { X = 10, Y = 20 } };
using var stream = new MemoryStream();
var writer = /* create Binary/Compact/JSON Writer (see generated code / runtime API) */;
value.Write(writer);
byte[] bytes = stream.ToArray();
Read example (concept):
using var stream = new MemoryStream(bytes);
var reader = /* create matching protocol Reader */;
var value = DemoUser.Read(reader);
Actual class and method names depend on your generated code and runtime version. For a working end-to-end example, see core repo examples/consumer-csharp.
4. GetSchema()
Generated types include GetSchema() (or similar API) to retrieve schema metadata — fields, types, defaults — at runtime. Useful for Excel, validation, and editor integration.
var schema = DemoUser.GetSchema();
// query field list, types, defaults, etc.
5. ProtocolRegistry & message dispatch
ProtocolRegistry provides message-type ↔ identifier (msgId, etc.) mapping. Used on server and client to dispatch different handlers based on packet type.
- Check types and IDs registered in generated code, read msgId from received bytes, look up the type via ProtocolRegistry, and call Read to deserialize.
- Details: Protocol and core repo docs.
6. Unified Write · extends
Every generated struct includes:
Write(oprot, fieldIds, overrides?)—fieldIdsnull ⇒ all fields;overrides⇒ per-field ID replacement values; combine both for projection + fan-out without cloning.FieldIdnested class —StructName.FieldId.PropertyNamefor compile-time safe field ID references.- struct extends — use
extendsin IDL to auto-merge parent fields into child structs.
Tutorial & comparison: Unified Write tutorial. API tables: API reference. Core spec: DEUKPACK_WRITE_WITH_OVERRIDES_API.md.
7. In Unity
- Include generated C# + DeukPack.Protocol (or Unity runtime) in your Unity project.
- If using asmdef, place generated code and runtime in the same assembly or set up a reference relationship.
- Kits lineup: See the Unity and Web App entries in DeukPack kits lineup for sample paths and setup guidance.
8. Next steps
- Protocol & serialization — Binary/Compact/JSON differences and selection
- API reference — CLI, unified Write, FieldId, GetSchema, ProtocolRegistry
- Unified Write tutorial — overrides, field selection, extends
- Core examples/consumer-csharp — Runnable C# sample