@dcl/protocol 1.0.0-28172947415.commit-c2f6832 → 1.0.0-28173821783.commit-63eb0db

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +0 -201
  2. package/out-js/decentraland/kernel/apis/restricted_actions.gen.d.ts +0 -21
  3. package/out-js/decentraland/kernel/apis/restricted_actions.gen.js +6 -85
  4. package/out-js/decentraland/kernel/apis/restricted_actions.gen.js.map +1 -1
  5. package/out-js/decentraland/kernel/comms/rfc4/comms.gen.d.ts +0 -21
  6. package/out-js/decentraland/kernel/comms/rfc4/comms.gen.js +4 -124
  7. package/out-js/decentraland/kernel/comms/rfc4/comms.gen.js.map +1 -1
  8. package/out-js/decentraland/sdk/components/avatar_shape.gen.d.ts +0 -8
  9. package/out-js/decentraland/sdk/components/avatar_shape.gen.js +1 -35
  10. package/out-js/decentraland/sdk/components/avatar_shape.gen.js.map +1 -1
  11. package/out-ts/decentraland/kernel/apis/restricted_actions.gen.ts +3 -89
  12. package/out-ts/decentraland/kernel/comms/rfc4/comms.gen.ts +2 -166
  13. package/out-ts/decentraland/sdk/components/avatar_shape.gen.ts +0 -34
  14. package/package.json +6 -9
  15. package/proto/buf.yaml +47 -0
  16. package/proto/decentraland/kernel/apis/restricted_actions.proto +0 -7
  17. package/proto/decentraland/kernel/comms/rfc4/comms.proto +0 -11
  18. package/proto/decentraland/sdk/components/avatar_shape.proto +0 -5
  19. package/proto/decentraland/sdk/components/light_source.proto +1 -1
  20. package/proto/decentraland/sdk/components/virtual_camera.proto +0 -2
  21. package/proto/google/LICENSE +27 -0
  22. package/proto/google/README.md +1 -0
  23. package/proto/google/api/annotations.json +83 -0
  24. package/proto/google/api/annotations.proto +11 -0
  25. package/proto/google/api/http.json +86 -0
  26. package/proto/google/api/http.proto +31 -0
  27. package/proto/google/protobuf/api.json +118 -0
  28. package/proto/google/protobuf/api.proto +34 -0
  29. package/proto/google/protobuf/descriptor.json +739 -0
  30. package/proto/google/protobuf/descriptor.proto +286 -0
  31. package/proto/google/protobuf/source_context.json +20 -0
  32. package/proto/google/protobuf/source_context.proto +7 -0
  33. package/proto/google/protobuf/type.json +202 -0
  34. package/proto/google/protobuf/type.proto +89 -0
  35. package/proto/decentraland/common/options.proto +0 -30
  36. package/proto/decentraland/common/quantization_example.proto +0 -132
  37. package/proto/decentraland/pulse/pulse_client.proto +0 -77
  38. package/proto/decentraland/pulse/pulse_server.proto +0 -138
  39. package/proto/decentraland/pulse/pulse_shared.proto +0 -48
  40. package/protoc-gen-bitwise/generator_csharp.js +0 -215
  41. package/protoc-gen-bitwise/options.js +0 -108
  42. package/protoc-gen-bitwise/plugin.js +0 -87
  43. package/protoc-gen-bitwise/runtime/cs/BitReader.cs +0 -112
  44. package/protoc-gen-bitwise/runtime/cs/BitWriter.cs +0 -117
  45. package/protoc-gen-bitwise/runtime/cs/Quantize.cs +0 -35
  46. package/protoc-gen-bitwise/wire.js +0 -239
@@ -1,132 +0,0 @@
1
- syntax = "proto3";
2
-
3
- package decentraland.common;
4
-
5
- import "decentraland/common/options.proto";
6
-
7
- // High-frequency player state messages sent on Channel 1 (unreliable sequenced).
8
- //
9
- // Every message below uses the protoc-gen-bitwise annotations to minimise
10
- // wire size. Wire costs are listed per-message so the trade-offs are clear.
11
- //
12
- // Annotation cheat-sheet:
13
- // [(decentraland.common.quantized) = { min: F, max: F, bits: N }]
14
- // → stores a float as a uint32 quantized to N bits over [min, max].
15
- // → protobuf encodes the uint32 as a varint: values ≤ 2^14-1 cost 2 bytes,
16
- // values ≤ 2^21-1 cost 3 bytes; the generated partial class adds a
17
- // float {Name}Quantized accessor that encodes/decodes transparently.
18
- // [(decentraland.common.bit_packed) = { bits: N }]
19
- // → documents that this uint32 uses at most N bits; protobuf encodes it
20
- // as a varint (same savings, no generated accessor needed).
21
- // (no annotation)
22
- // → standard protobuf encoding (bool/double/etc. at natural width).
23
- //
24
- // Varint byte costs at worst-case (all bits set):
25
- // ≤ 7 bits → 1 byte (max 127)
26
- // ≤ 14 bits → 2 bytes (max 16 383)
27
- // ≤ 21 bits → 3 bytes (max 2 097 151)
28
- // Proto3 omits fields equal to their default value (0), so average cost is lower.
29
-
30
- // ---------------------------------------------------------------------------
31
- // PositionDelta — Δ position relative to last acknowledged full snapshot.
32
- //
33
- // Sent every client tick (~10 Hz) on Channel 1.
34
- //
35
- // Field | Type | Range | Q bits | Wire worst-case
36
- // ------------|--------|----------------|--------|-----------------------
37
- // dx | uint32 | [-100, 100] | 16 | tag 1B + varint 3B = 4B
38
- // dy | uint32 | [-100, 100] | 16 | tag 1B + varint 3B = 4B
39
- // dz | uint32 | [-100, 100] | 16 | tag 1B + varint 3B = 4B
40
- // entity_id | uint32 | [0, 1 048 575] | 20 | tag 1B + varint 3B = 4B
41
- // sequence | uint32 | [0, 4 095] | 12 | tag 1B + varint 2B = 3B
42
- // ---------------------------------------------------------------------------
43
- // Worst-case: 19 B (vs. 20 B raw: 3×float + 2×uint32)
44
- // Step: dx/dy/dz ≈ 0.003 units
45
- // ---------------------------------------------------------------------------
46
- message PositionDelta {
47
- uint32 dx = 1 [(decentraland.common.quantized) = { min: -100.0, max: 100.0, bits: 16 }];
48
- uint32 dy = 2 [(decentraland.common.quantized) = { min: -100.0, max: 100.0, bits: 16 }];
49
- uint32 dz = 3 [(decentraland.common.quantized) = { min: -100.0, max: 100.0, bits: 16 }];
50
- uint32 entity_id = 4 [(decentraland.common.bit_packed) = { bits: 20 }];
51
- uint32 sequence = 5 [(decentraland.common.bit_packed) = { bits: 12 }];
52
- }
53
-
54
- // ---------------------------------------------------------------------------
55
- // PlayerInput — client input snapshot for server-side reconciliation.
56
- //
57
- // Sent every client frame (~30 Hz) on Channel 1.
58
- //
59
- // Field | Type | Range | Q bits | Wire worst-case
60
- // ------------|--------|----------------|--------|-----------------------
61
- // move_x | uint32 | [-1, 1] | 8 | tag 1B + varint 2B = 3B
62
- // move_z | uint32 | [-1, 1] | 8 | tag 1B + varint 2B = 3B
63
- // yaw | uint32 | [-180, 180] | 12 | tag 1B + varint 2B = 3B
64
- // buttons | uint32 | bitmask | 8 | tag 1B + varint 2B = 3B
65
- // sequence | uint32 | [0, 4 095] | 12 | tag 1B + varint 2B = 3B
66
- // ---------------------------------------------------------------------------
67
- // Worst-case: 15 B (vs. 20 B raw: 3×float + 2×uint32)
68
- // Step: move_x/move_z ≈ 0.008; yaw ≈ 0.088°
69
- // ---------------------------------------------------------------------------
70
- message PlayerInput {
71
- // Normalised joystick axes in [-1, 1].
72
- uint32 move_x = 1 [(decentraland.common.quantized) = { min: -1.0, max: 1.0, bits: 8 }];
73
- uint32 move_z = 2 [(decentraland.common.quantized) = { min: -1.0, max: 1.0, bits: 8 }];
74
-
75
- // Horizontal look direction in degrees.
76
- uint32 yaw = 3 [(decentraland.common.quantized) = { min: -180.0, max: 180.0, bits: 12 }];
77
-
78
- // Bitmask of active buttons (see ButtonFlags below).
79
- uint32 buttons = 4 [(decentraland.common.bit_packed) = { bits: 8 }];
80
-
81
- // Rolling input sequence number used by the server for reconciliation.
82
- uint32 sequence = 5 [(decentraland.common.bit_packed) = { bits: 12 }];
83
- }
84
-
85
- // Bitmask values for PlayerInput.buttons.
86
- enum ButtonFlags {
87
- BF_NONE = 0;
88
- BF_JUMP = 1; // bit 0
89
- BF_SPRINT = 2; // bit 1
90
- BF_INTERACT = 4; // bit 2
91
- BF_EMOTE = 8; // bit 3
92
- BF_CROUCH = 16; // bit 4
93
- // bits 5-7 reserved
94
- }
95
-
96
- // ---------------------------------------------------------------------------
97
- // AvatarStateSnapshot — full authoritative state, sent on Channel 0 (reliable)
98
- // or on resync requests. Demonstrates wider ranges and mixed encodings.
99
- //
100
- // Field | Type | Range | Q bits | Wire worst-case
101
- // -----------------|--------|------------------|--------|-----------------------
102
- // x | uint32 | [-4096, 4096] | 16 | tag 1B + varint 3B = 4B
103
- // y | uint32 | [-256, 256] | 14 | tag 1B + varint 2B = 3B
104
- // z | uint32 | [-4096, 4096] | 16 | tag 1B + varint 3B = 4B
105
- // pitch | uint32 | [-90, 90] | 10 | tag 1B + varint 2B = 3B
106
- // yaw | uint32 | [-180, 180] | 12 | tag 1B + varint 2B = 3B
107
- // entity_id | uint32 | [0, 1 048 575] | 20 | tag 1B + varint 3B = 4B
108
- // animation_state | uint32 | [0, 63] | 6 | tag 1B + varint 1B = 2B
109
- // is_grounded | bool | — | — | tag 1B + varint 1B = 2B
110
- // timestamp | double | — | — | tag 1B + fixed64 8B = 9B
111
- // ---------------------------------------------------------------------------
112
- // Worst-case: 34 B (vs. 45 B raw: 5×float + 2×uint32 + bool + double)
113
- // Step: x/z ≈ 0.125 units; y ≈ 0.031 units; pitch ≈ 0.176°; yaw ≈ 0.088°
114
- // ---------------------------------------------------------------------------
115
- message AvatarStateSnapshot {
116
- // World-space position.
117
- uint32 x = 1 [(decentraland.common.quantized) = { min: -4096.0, max: 4096.0, bits: 16 }];
118
- uint32 y = 2 [(decentraland.common.quantized) = { min: -256.0, max: 256.0, bits: 14 }];
119
- uint32 z = 3 [(decentraland.common.quantized) = { min: -4096.0, max: 4096.0, bits: 16 }];
120
-
121
- // View angles.
122
- uint32 pitch = 4 [(decentraland.common.quantized) = { min: -90.0, max: 90.0, bits: 10 }];
123
- uint32 yaw = 5 [(decentraland.common.quantized) = { min: -180.0, max: 180.0, bits: 12 }];
124
-
125
- // Identity and animation state.
126
- uint32 entity_id = 6 [(decentraland.common.bit_packed) = { bits: 20 }];
127
- uint32 animation_state = 7 [(decentraland.common.bit_packed) = { bits: 6 }];
128
-
129
- // Un-annotated fields — encoded at their natural width.
130
- bool is_grounded = 8;
131
- double timestamp = 9; // server epoch milliseconds
132
- }
@@ -1,77 +0,0 @@
1
- syntax = "proto3";
2
-
3
- package decentraland.pulse;
4
-
5
- import "decentraland/common/vectors.proto";
6
- import "decentraland/pulse/pulse_shared.proto";
7
-
8
- message HandshakeRequest {
9
- bytes auth_chain = 1;
10
- int32 profile_version = 2;
11
- optional PlayerInitialState initial_state = 3;
12
- }
13
-
14
- // Describes the initial state of the player if (re-)connected in the middle of the session
15
- message PlayerInitialState {
16
- PlayerState state = 1;
17
- optional string emote_id = 2;
18
- optional uint32 emote_duration_ms = 3;
19
- // Indicates how many milliseconds ago the emote was started
20
- optional uint32 emote_start_offset_ms = 4;
21
- // Non-empty realm identifier. Rejected if empty.
22
- string realm = 5;
23
- optional int32 emote_mask = 6;
24
- }
25
-
26
- // Similarly to the LiveKit pipeline, a peer announces the version of its profile but
27
- // it only does it when it's changed as it's sent reliably and stored on the server for other peers
28
- message ProfileVersionAnnouncement {
29
- int32 version = 1;
30
- }
31
-
32
- // Since the server doesn't simulate the scenes state, it trusts the values from the client
33
- message PlayerStateInput {
34
- PlayerState state = 1;
35
- }
36
-
37
- // Client sends resync request if it has a gap in the known sequences and, thus, can't apply a delta.
38
- // It's sent reliably to prevent further desynchronization
39
- message ResyncRequest {
40
- uint32 subject_id = 1;
41
- // highest seq the client actually has for this subject
42
- uint32 known_seq = 2;
43
- }
44
-
45
- // Client → Server. Client requests to start an emote.
46
- message EmoteStart {
47
- string emote_id = 1;
48
- optional uint32 duration_ms = 2;
49
- PlayerState player_state = 3;
50
- optional int32 mask = 4;
51
- }
52
-
53
- // Client → Server. Client requests to stop a looping emote.
54
- // One-shot emotes are terminated by the server timer.
55
- message EmoteStop {
56
- }
57
-
58
- // Client → Server. Also announces the peer's realm; peers in different realms never see each
59
- // other. Must be the first gameplay message after handshake. Same-realm re-teleports are valid.
60
- message TeleportRequest {
61
- int32 parcel_index = 1;
62
- decentraland.common.Vector3 position = 2;
63
- // Non-empty realm identifier. Rejected if empty.
64
- string realm = 3;
65
- }
66
-
67
- message ClientMessage {
68
- oneof message {
69
- HandshakeRequest handshake = 1;
70
- PlayerStateInput input = 2;
71
- ResyncRequest resync = 3;
72
- ProfileVersionAnnouncement profile_announcement = 4;
73
- EmoteStart emote_start = 5;
74
- EmoteStop emote_stop = 6;
75
- TeleportRequest teleport = 7;
76
- }
77
- }
@@ -1,138 +0,0 @@
1
- syntax = "proto3";
2
-
3
- package decentraland.pulse;
4
-
5
- import "decentraland/common/options.proto";
6
- import "decentraland/pulse/pulse_shared.proto";
7
-
8
- message HandshakeResponse {
9
- bool success = 1;
10
- optional string error = 2;
11
- }
12
-
13
- message PlayerProfileVersionsAnnounced {
14
- uint32 subject_id = 1;
15
- int32 version = 2;
16
- }
17
-
18
- message PlayerStateDeltaTier0 {
19
- uint32 subject_id = 1;
20
-
21
- // Between two consecutive server simulation ticks, the subject may have sent multiple inputs, each incrementing Seq by 1. So
22
- // the delta's NewSeq will naturally jump by more than 1 compared to the previous delta — even with zero packet loss.
23
- // Without the "BaselineSeq" the client has no way to detect the package loss
24
- uint32 baseline_seq = 2;
25
- uint32 new_seq = 3;
26
- uint32 server_tick = 4;
27
-
28
- // While the player doesn't cross the parcel, this field is omitted from diff
29
- optional int32 parcel_index = 5;
30
-
31
- // X position inside the parcel
32
- optional uint32 position_x = 6 [(decentraland.common.quantized) = { min: 0, max: 16, bits: 8 }];
33
-
34
- // Y position
35
- optional uint32 position_y = 7 [(decentraland.common.quantized) = { min: 0, max: 200, bits: 13 }];
36
-
37
- // Z position inside the parcel
38
- optional uint32 position_z = 8 [(decentraland.common.quantized) = { min: 0, max: 16, bits: 8 }];
39
-
40
- optional uint32 velocity_x = 9 [(decentraland.common.quantized) = { min: -50, max: 50, bits: 8 }];
41
- optional uint32 velocity_y = 10 [(decentraland.common.quantized) = { min: -50, max: 50, bits: 8 }];
42
- optional uint32 velocity_z = 11 [(decentraland.common.quantized) = { min: -50, max: 50, bits: 8 }];
43
-
44
- optional uint32 rotation_y = 12 [(decentraland.common.quantized) = { min: 0, max: 360.0, bits: 7 }];
45
- optional uint32 movement_blend = 13 [(decentraland.common.quantized) = { min: 0, max: 3, bits: 5 }];
46
- optional uint32 slide_blend = 14 [(decentraland.common.quantized) = { min: 0, max: 1, bits: 4 }];
47
- optional uint32 head_yaw = 15 [(decentraland.common.quantized) = { min: 0, max: 360.0, bits: 7 }];
48
- optional uint32 head_pitch = 16 [(decentraland.common.quantized) = { min: 0, max: 360.0, bits: 7 }];
49
-
50
- optional uint32 state_flags = 17;
51
- optional GlideState glide_state = 18;
52
-
53
- optional int32 jump_count = 19;
54
-
55
- // Absolute world hit position the player is pointing at.
56
- // Only meaningful when POINTING_AT is set in `state_flags`.
57
- //
58
- // X/Z use 17 bits over the world span (~±3000m, covers GenesisCity + border +
59
- // raycast cutoff) which yields ~0.046m steps — at least as fine as the player's
60
- // own parcel_index + position_x/z combined precision (0.0625m), so no separate
61
- // parcel index is needed for point-at.
62
- //
63
- // Y uses 7 bits over the player altitude range (matches position_y), step ~1.6m.
64
- // Point At is not supposed to change frequently so the wire overhead should be minimal
65
- optional uint32 point_at_x = 20 [(decentraland.common.quantized) = { min: -3000.0, max: 3000.0, bits: 17 }];
66
- optional uint32 point_at_y = 21 [(decentraland.common.quantized) = { min: 0.0, max: 200.0, bits: 7 }];
67
- optional uint32 point_at_z = 22 [(decentraland.common.quantized) = { min: -3000.0, max: 3000.0, bits: 17 }];
68
- }
69
-
70
- // Full State is sent to the client when it is out of sync, and can't recover with a diff only
71
- message PlayerStateFull {
72
- uint32 subject_id = 1;
73
- uint32 sequence = 2;
74
- uint32 server_tick = 3;
75
- PlayerState state = 4;
76
- }
77
-
78
- // Notification to the client, that a peer has joined, it can mean connection or entering the area of interest, it's up to the server to decide
79
- message PlayerJoined {
80
- string user_id = 1;
81
- int32 profile_version = 2;
82
- PlayerStateFull state = 3;
83
- }
84
-
85
- // Notification to the client, that a peer has left, it can mean disconnection or leaving the area of interest
86
- message PlayerLeft {
87
- uint32 subject_id = 1;
88
- }
89
-
90
- enum EmoteStopReason {
91
- COMPLETED = 0; // one-shot timer expired on the server
92
- CANCELLED = 1; // client sent EmoteStop (looping emotes)
93
- }
94
-
95
- // Server → All Observers
96
- // Full player state is piggybacked to ensure the emote is played in the right place.
97
- // Observers use server_tick to scrub animation forward by transit latency.
98
- message EmoteStarted {
99
- uint32 subject_id = 1;
100
- uint32 sequence = 2;
101
- uint32 server_tick = 3;
102
- string emote_id = 4;
103
- PlayerState player_state = 5;
104
- optional int32 mask = 6;
105
- }
106
-
107
- // Server → All Observers
108
- // Client resumes MovementInput only after receiving this.
109
- // Carries full PlayerState so the client can snap to the correct position on resume.
110
- message EmoteStopped {
111
- uint32 subject_id = 1;
112
- uint32 server_tick = 2;
113
- EmoteStopReason reason = 3;
114
- uint32 sequence = 4;
115
- PlayerState player_state = 5;
116
- }
117
-
118
- // Server → All Observers
119
- message TeleportPerformed {
120
- uint32 subject_id = 1;
121
- uint32 sequence = 2;
122
- uint32 server_tick = 3;
123
- PlayerState state = 4;
124
- }
125
-
126
- message ServerMessage {
127
- oneof message {
128
- HandshakeResponse handshake = 1;
129
- PlayerStateFull player_state_full = 2;
130
- PlayerStateDeltaTier0 player_state_delta = 3;
131
- PlayerJoined player_joined = 4;
132
- PlayerLeft player_left = 5;
133
- PlayerProfileVersionsAnnounced player_profile_version_announced = 6;
134
- EmoteStarted emote_started = 7;
135
- EmoteStopped emote_stopped = 8;
136
- TeleportPerformed teleported = 9;
137
- }
138
- }
@@ -1,48 +0,0 @@
1
- syntax = "proto3";
2
-
3
- package decentraland.pulse;
4
-
5
- import "decentraland/common/vectors.proto";
6
-
7
- enum PlayerAnimationFlags {
8
- NONE = 0;
9
- GROUNDED = 1;
10
- LONG_JUMP = 2;
11
- LONG_FALL = 4;
12
- FALLING = 8;
13
- STUNNED = 16;
14
- HEAD_YAW = 32;
15
- HEAD_PITCH = 64;
16
- POINTING_AT = 128;
17
- }
18
-
19
- enum GlideState {
20
- PROP_CLOSED = 0;
21
- OPENING_PROP = 1;
22
- GLIDING = 2;
23
- CLOSING_PROP = 3;
24
- }
25
-
26
- message PlayerState {
27
- int32 parcel_index = 1;
28
-
29
- decentraland.common.Vector3 position = 2;
30
- decentraland.common.Vector3 velocity = 3;
31
-
32
- float rotation_y = 4;
33
-
34
- float movement_blend = 5;
35
- float slide_blend = 6;
36
-
37
- optional float head_yaw = 7;
38
- optional float head_pitch = 8;
39
-
40
- uint32 state_flags = 9;
41
- GlideState glide_state = 10;
42
-
43
- int32 jump_count = 11;
44
-
45
- // Absolute world hit position the player is pointing at.
46
- // Only meaningful when POINTING_AT is set in `state_flags`.
47
- optional decentraland.common.Vector3 point_at = 12;
48
- }
@@ -1,215 +0,0 @@
1
- 'use strict'
2
-
3
- /**
4
- * C# code generator for the protoc-gen-bitwise plugin.
5
- *
6
- * For every proto message that contains at least one uint32 field annotated
7
- * with [(quantized)], this module emits a C# partial class that adds a computed
8
- * float property named {FieldName}Quantized. The getter decodes the stored
9
- * uint32 to a float; the setter encodes a float back to a uint32. Standard
10
- * protobuf handles serialization of the uint32 wire field; this class adds a
11
- * typed float accessor on top.
12
- *
13
- * Only uint32 fields are supported. bit_packed and unannotated fields are
14
- * passed through without generating any accessor.
15
- *
16
- * Port of the original generator_csharp.py — output is intended to be
17
- * byte-for-byte identical.
18
- */
19
-
20
- const { getFieldOptions } = require('./options')
21
-
22
- // FieldDescriptorProto type/label constants.
23
- const TYPE_UINT32 = 13
24
- const LABEL_REPEATED = 3
25
-
26
- // ---------------------------------------------------------------------------
27
- // Helpers
28
- // ---------------------------------------------------------------------------
29
-
30
- /** Mirrors Python str.capitalize(): upper-first, lowercase the rest. */
31
- function capitalize(word) {
32
- if (word.length === 0) return ''
33
- return word[0].toUpperCase() + word.slice(1).toLowerCase()
34
- }
35
-
36
- /** position_x -> PositionX */
37
- function snakeToPascal(name) {
38
- return name.split('_').map(capitalize).join('')
39
- }
40
-
41
- /** decentraland.kernel.comms.v3 -> Decentraland.Kernel.Comms.V3 */
42
- function packageToNamespace(pkg) {
43
- if (!pkg) return 'Generated'
44
- return pkg.split('.').map(capitalize).join('.')
45
- }
46
-
47
- /**
48
- * Format a number with C printf %g semantics at `precision` significant digits:
49
- * shortest of fixed/scientific, with trailing zeros and a trailing dot removed.
50
- * Reproduces Python's `f'{value:.{precision}g}'`.
51
- */
52
- function formatG(value, precision) {
53
- if (precision <= 0) precision = 1
54
- if (value === 0) return '0'
55
- if (!Number.isFinite(value)) return value > 0 ? 'inf' : 'nan'
56
-
57
- const negative = value < 0
58
- const v = Math.abs(value)
59
-
60
- // Correctly-rounded scientific form yields the decimal exponent X (handling
61
- // carry such as 9.999 -> 1.0e+1).
62
- const sci = v.toExponential(precision - 1)
63
- const eIdx = sci.indexOf('e')
64
- const X = parseInt(sci.slice(eIdx + 1), 10)
65
-
66
- let result
67
- if (X >= -4 && X < precision) {
68
- // Fixed notation with (precision - 1 - X) fraction digits.
69
- const fractionDigits = precision - 1 - X
70
- result = v.toFixed(fractionDigits >= 0 ? fractionDigits : 0)
71
- if (result.indexOf('.') !== -1) {
72
- result = result.replace(/0+$/, '').replace(/\.$/, '')
73
- }
74
- } else {
75
- // Scientific notation; printf prints at least two exponent digits.
76
- let mantissa = sci.slice(0, eIdx)
77
- if (mantissa.indexOf('.') !== -1) {
78
- mantissa = mantissa.replace(/0+$/, '').replace(/\.$/, '')
79
- }
80
- const expSign = X < 0 ? '-' : '+'
81
- let expDigits = String(Math.abs(X))
82
- if (expDigits.length < 2) expDigits = '0' + expDigits
83
- result = mantissa + 'e' + expSign + expDigits
84
- }
85
-
86
- return (negative ? '-' : '') + result
87
- }
88
-
89
- /** Format a value as a C# float literal (e.g. -100.0f). */
90
- function formatFloat(value) {
91
- let text = formatG(value, 8)
92
- if (text.indexOf('.') === -1 && text.indexOf('e') === -1 && text.indexOf('E') === -1) {
93
- text += '.0'
94
- }
95
- return text + 'f'
96
- }
97
-
98
- /** Format a quantization step size for a doc comment (e.g. "≈ 0.003"). */
99
- function formatStep(step) {
100
- return '≈ ' + formatG(step, 6)
101
- }
102
-
103
- // ---------------------------------------------------------------------------
104
- // Per-message code generation
105
- // ---------------------------------------------------------------------------
106
-
107
- /**
108
- * Generate a C# partial class for a proto message, or null if it has no
109
- * quantized uint32 fields. Returns an array of lines (no trailing newline).
110
- */
111
- function generateMessage(msgProto, indent) {
112
- const i = indent || ' '
113
- const props = []
114
-
115
- for (const field of msgProto.field) {
116
- // Repeated/map fields are not supported.
117
- if (field.label === LABEL_REPEATED) continue
118
- // Only uint32 fields are candidates for quantized accessors.
119
- if (field.type !== TYPE_UINT32) continue
120
-
121
- const { quantized } = getFieldOptions(field.optionsRaw)
122
- if (quantized === null) continue
123
-
124
- const step = (quantized.max - quantized.min) / ((1 << quantized.bits) - 1)
125
-
126
- props.push({
127
- propName: snakeToPascal(field.name),
128
- mn: formatFloat(quantized.min),
129
- mx: formatFloat(quantized.max),
130
- bits: quantized.bits,
131
- step,
132
- })
133
- }
134
-
135
- if (props.length === 0) return null
136
-
137
- const backings = []
138
- const lines = []
139
- lines.push(`public partial class ${msgProto.name}`)
140
- lines.push('{')
141
-
142
- for (const { propName, mn, mx, bits, step } of props) {
143
- const backing = '_' + propName[0].toLowerCase() + propName.slice(1)
144
- backings.push(backing)
145
- lines.push(`${i}private float? ${backing};`)
146
- lines.push(
147
- `${i}/// <summary>Float accessor for <see cref="${propName}"/>. Range [${mn}, ${mx}], ${bits} bits, step ${formatStep(step)}.</summary>`,
148
- )
149
- lines.push(`${i}public float ${propName}Quantized`)
150
- lines.push(`${i}{`)
151
- lines.push(`${i}${i}get => ${backing} ??= Quantize.Decode(${propName}, ${mn}, ${mx}, ${bits});`)
152
- lines.push(`${i}${i}set { ${backing} = value; ${propName} = Quantize.Encode(value, ${mn}, ${mx}, ${bits}); }`)
153
- lines.push(`${i}}`)
154
- lines.push('')
155
- }
156
-
157
- lines.push(`${i}/// <summary>Clears all cached decoded values. Call after mutating raw uint32 fields directly.</summary>`)
158
- lines.push(`${i}public void ResetDecodedCache()`)
159
- lines.push(`${i}{`)
160
- for (const backing of backings) {
161
- lines.push(`${i}${i}${backing} = null;`)
162
- }
163
- lines.push(`${i}}`)
164
-
165
- lines.push('}')
166
- return lines
167
- }
168
-
169
- // ---------------------------------------------------------------------------
170
- // Per-file code generation (public entry point)
171
- // ---------------------------------------------------------------------------
172
-
173
- /**
174
- * Generate a C# source file for a FileDescriptorProto, or null if the file
175
- * contains no quantized uint32 fields.
176
- * @returns {{name: string, content: string} | null}
177
- */
178
- function generateCsharp(fileProto) {
179
- const namespace = packageToNamespace(fileProto.package)
180
-
181
- const protoFile = fileProto.name.split('/').pop()
182
- const stem = snakeToPascal(protoFile.replace('.proto', ''))
183
- const outName = `${stem}.Bitwise.cs`
184
-
185
- const header = [
186
- '// <auto-generated>',
187
- '// Generated by protoc-gen-bitwise. DO NOT EDIT.',
188
- `// Source: ${fileProto.name}`,
189
- '// </auto-generated>',
190
- '',
191
- 'using Decentraland.Networking.Bitwise;',
192
- '',
193
- `namespace ${namespace}`,
194
- '{',
195
- ]
196
- const footer = ['', `} // namespace ${namespace}`]
197
-
198
- const body = []
199
- for (const msg of fileProto.messageType) {
200
- const msgLines = generateMessage(msg)
201
- if (msgLines === null) continue
202
- // Indent each line by 4 spaces (inside the namespace block).
203
- for (const line of msgLines) {
204
- body.push(line ? ' ' + line : '')
205
- }
206
- body.push('')
207
- }
208
-
209
- if (body.length === 0) return null
210
-
211
- const content = header.concat(body, footer).join('\n') + '\n'
212
- return { name: outName, content }
213
- }
214
-
215
- module.exports = { generateCsharp }