I have made a minimalistic multiplayer game netcode framework where client and server code are located in the same classes. Instances of those classes appear on both, the server and the clients. These classes can have properties marked as to be synchronized over the network using a configurable attribute:
[SynchronizeOverNetwork(Authority = Authority.Server)]
public ReconcileMoveData ReconcileMoveData {
// Code should not appear on server, but on client
get => new ReconcileMoveData {
Position = transform.position,
Rotation = transform.rotation.eulerAngles.z,
LinearVelocity = _body.velocity,
AngularVelocity = _body.angularVelocity,
};
// Code should not appear on client, but on server
set {
transform.position = value.Position;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, value.Rotation));
_body.velocity = value.LinearVelocity;
_body.angularVelocity = value.AngularVelocity;
}
}
I find those properties in the initialization phase (when starting the game) to manage them globally and handle differently on client and server.
If I have set Authority
to Authority.Server
the setter of that property is to be executed only on the server and the setter only on the client. If set to Authority.Client
it is the other way around: Setter is called on the client, getter on the server.
To prevent code analysis (The server will never be published) and to reduce client code size I want to exclude server code on client builds and client code on server builds.
Unity has the UNITY_SERVER
directive to marke code for stripping for client:
#if UNITY_SERVER
// Server only code here
#endif
Placing that directives in every getter and setter is easy to do wrong, cumbersome and redundant.
Is it possible to have preprocessor build step (or whatever) that adds those directives when building automatically based on the attributes configuration (without changing the source code files, but the code passed to the compile) or to mark code for stripping otherwise?