Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization & Buffer Support #96

Open
Vorlias opened this issue Sep 4, 2024 · 3 comments
Open

Serialization & Buffer Support #96

Vorlias opened this issue Sep 4, 2024 · 3 comments
Labels
Enhancement New feature or request Planned A planned change RFC request for comments

Comments

@Vorlias
Copy link
Member

Vorlias commented Sep 4, 2024

I have an prototype working version of Serialization and buffer encoding using static type declarations. Will likely port it to RbxNet soon.

@Vorlias Vorlias added Enhancement New feature or request Planned A planned change RFC request for comments labels Sep 4, 2024
@Vorlias
Copy link
Member Author

Vorlias commented Sep 4, 2024

Example:

const TestStaticEvent = Net.Remote(NetType.Array(NetType.Int16)) 
// would type as number[] but effectively be short[]

by default RN not using buffers

However, if you want a more efficient way:

const TestStaticEvent = Net.Remote(NetType.Array(NetType.Int16)).SetUseBuffer(true)
// This would end up converting signals into a buffer like

[ u32 size, ...i16 data ] (where data is the length of size)

This is a bit like tools like zap where you define the static types, but through the net API.

So if we had

type NetworkInventorySlot = struct {
    SlotIndex: u8,
    ItemId: u16,
    Amount: u16,
}

type NetworkInventoryData = struct {
    InventoryId: u8,
    InventorySize: u8,
    InventoryDto: NetworkInventorySlot[],
}

event NetworkPlayerInventoryUpdated = {
    from: Server,
    type: Reliable,
    call: ManyAsync,
    data: NetworkInventoryData[],
}

In Net we could do a similar setup:

const NetworkInventorySlot = NetType.Interface({
    SlotIndex: NetType.UInt8,
    ItemId: NetType.UInt16,
    Amount: NetType.UInt16,
})

const NetworkInventoryData = NetType.Interface({
    InventoryId: NetType.UInt8,
    InventorySize: NetType.UInt8,
    InventoryDto: NetType.Array(NetworkInventorySlot),
})

const NetworkPlayerInventoryUpdated = Net.Remote(NetType.Array(NetworkInventoryData)).SetUseBuffer(true)

export default Net.Create()
    .AddServer("NetworkPlayerInventoryUpdated", NetworkPlayerInventoryUpdated)
    .Build();

@Vorlias
Copy link
Member Author

Vorlias commented Sep 4, 2024

here's an example of a custom serialized type (a Roblox player)

	/**
	 * The player network type
	 */
	export const Player: NetworkSerializer<Player, number> = {
		Name: "Player",
		Message: "Expected a Player",
		NetworkBuffer: NetEncoding.f64, // Roblox UserIds are uuuge
		$serializer: true,
		Validate(value): value is Player {
			return typeIs(value, "Instance") && value.IsA("Player");
		},
		Serialize(value) {
			return value.UserId;
		},
		Deserialize(value) {
			const player = Players.GetPlayerByUserId(value);
			assert(player);
			return player;
		},
	};

If buffers are used, it will use the NetworkBuffer for the buffer translation, otherwise you will see a double number instead.

@Vorlias
Copy link
Member Author

Vorlias commented Sep 4, 2024

Feel free to comment feedback on this - it's been a bit of a process to figure this all out. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request Planned A planned change RFC request for comments
Projects
None yet
Development

No branches or pull requests

1 participant