/** * Represents the interface for data type solvers, providing methods for * writing, reading, and determining the length of data. * Now generic to preserve type information! */ export interface DataTypeInterface { /** * Writes a value to the Writer's internal buffer. * @param writer The Writer instance. * @param value The value to write. */ write(writer: Writer, value: T): void; /** * Reads a value from the Reader's internal buffer. * @param reader The Reader instance. * @returns The read value. */ read(reader: Reader): T; /** * Calculates the byte length of a given value when serialized. * @param value The value to calculate the length for. * @returns The length in bytes. */ length(value: T): number; } /** * Represents an Event packet with proper type preservation. */ export interface Event { _solver: DataTypeInterface; _listeners: ((data: T) => void)[]; _packetid: number; _packettype: PacketType; _method: "Event"; /** * Sends data from the client to the server. (Client-only) * @param data The data to send. */ Send: (data: T) => void; /** * Sends data from the server to a specific player. (Server-only) * @param user The player to send the data to. * @param data The data to send. */ SendTo: (user: Player, data: T) => void; /** * Sends data from the server to all players. (Server-only) * @param data The data to send. */ SendToAll: (data: T) => void; /** * Sends data from the server to all players except those in the exclude list. (Server-only) * @param exclude A list of players to exclude. * @param data The data to send. */ SendToAllExcept: (exclude: Player[], data: T) => void; /** * Registers a listener function to be called when the event receives data. * @param listener The function to call with the received data. */ Listen: (listener: (data: T, player?: Player | undefined) => void) => void; } /** * Represents a Function packet with proper type preservation. */ export interface Function { _request: DataTypeInterface; _response: DataTypeInterface; _listener?: (data: RequestT) => ResponseT; _packetid: number; _method: "Function"; /** * Invokes the function from the client, expecting a response from the server. (Client-only) * @param data The request data. * @returns The response data. */ Invoke: (data: RequestT) => ResponseT; /** * Invokes the function on a specific player from the server, expecting a response from the client. (Server-only) * @param user The player to invoke the function on. * @param data The request data. * @returns The response data. */ InvokeTo: (user: Player, data: RequestT) => ResponseT; /** * Registers a listener function to be called when the function is invoked, * which should return the response data. * @param listener The function to call with the request data, returning the response data. */ Listen: (listener: (data: RequestT) => ResponseT) => void; } /** * Represents a Writer for serializing data. */ export interface Writer { _cursor: number; _size: number; _refcursor: number; _references: unknown[]; _data: buffer; } /** * Internal interface for Writer implementation details. */ interface WriterImpl { new (): Writer; Allocate(self: Writer, bytes: number): void; SetPacketID(self: Writer, id: number): void; Dump(self: Writer): [buffer, unknown[]?]; Append(self: Writer, packet: Event, props: T): void; WriteRequest(self: Writer, packet: Function, props: RequestT): void; WriteResponse(self: Writer, packet: Function, props: ResponseT): void; Clear(self: Writer): void; } declare const Writer: WriterImpl; /** * Represents a Reader for deserializing data. */ export interface Reader { _cursor: number; _refcursor: number; _data?: buffer; _references?: unknown[]; } /** * Internal interface for Reader implementation details. */ interface ReaderImpl { new (): Reader; GetPacketID(self: Reader, data: buffer): number; ReadEvent(self: Reader, data: buffer, references?: unknown[]): { packetid: number; value: unknown }[]; ReadRequest(self: Reader, data: buffer, references?: unknown[]): { packetid: number; value: unknown }; ReadResponse(self: Reader, data: buffer, references?: unknown[]): { packetid: number; value: unknown }; Clear(self: Reader): void; } declare const Reader: ReaderImpl; /** * Defines the server-side processing capabilities for events and functions. */ export interface ServerProcesser { /** * Sends an event to a specific player. * @param self The Event instance. * @param user The player to send to. * @param data The data to send. */ SendTo(self: Event, user: Player, data: T): void; /** * Invokes a function on a specific player, returning the response. * @param self The Function instance. * @param user The player to invoke on. * @param data The request data. * @returns The response data. */ InvokeTo(self: Function, user: Player, data: RequestT): ResponseT; } /** * Defines the client-side processing capabilities for events and functions. */ export interface ClientProcesser { /** * Sends an event from the client to the server. * @param self The Event instance. * @param data The data to send. */ Send: (self: Event, data: T) => void; /** * Invokes a function on the server, returning the response. * @param self The Function instance. * @param data The request data. * @returns The response data. */ Invoke: (self: Function, data: RequestT) => ResponseT; } /** * Manages the registration and retrieval of Event and Function packets. */ export interface Registry { /** * Sets (registers) a packet with a given type name and ID. * @param typename The type name ("Event" or "Function"). * @param id The packet ID. * @param packet The Event or Function instance. */ set: { (typename: "Event", id: number, packet: Event): void; (typename: "Event", id: number, packet: Event): void; (typename: "Function", id: number, packet: Function): void; }; /** * Gets a registered packet by its type name and ID. * @param typename The type name ("Event" or "Function"). * @param id The packet ID. * @returns The Event or Function instance, or undefined if not found. */ get: { (typename: "Event", id: number): Event | undefined; (typename: "Function", id: number): Function | undefined; }; } /** * Represents a single packet type for events (Reliable or Unreliable). */ export interface PacketType { Name: "Reliable" | "Unreliable"; } /** * Struct solver type helper for complex objects */ export type StructSolver> = { [K in keyof T]: DataTypeInterface; }; /** * Defines all available data types for serialization with proper typing. */ export interface DataTypes { // Factory methods for creating Events and Functions Event: (packetType: PacketType, solver: DataTypeInterface) => Event; Function: ( request: DataTypeInterface, response: DataTypeInterface, ) => Function; // Numeric data types uint8: DataTypeInterface; uint16: DataTypeInterface; uint32: DataTypeInterface; int8: DataTypeInterface; int16: DataTypeInterface; int32: DataTypeInterface; float32: DataTypeInterface; float64: DataTypeInterface; // Complex data types with proper generic typing struct: >(solvers: StructSolver) => DataTypeInterface; array: (solver: DataTypeInterface) => DataTypeInterface; map: (keySolver: DataTypeInterface, valueSolver: DataTypeInterface) => DataTypeInterface>; // Roblox-specific data types inst: DataTypeInterface; color3: DataTypeInterface; //vector: DataTypeInterface; vector3: DataTypeInterface; vector2: DataTypeInterface; cframe: DataTypeInterface; // Basic data types boolean: DataTypeInterface; string: DataTypeInterface; buffer: DataTypeInterface; // Special data types unknown: DataTypeInterface; none: DataTypeInterface; } /** * Defines the available packet types (Reliable, Unreliable). */ export interface PacketTypes { Reliable: PacketType; Unreliable: PacketType; } // Main LightNet declaration declare const LightNet: { DataTypes: DataTypes; PacketTypes: PacketTypes; }; export declare const DataTypes: DataTypes; export declare const PacketTypes: PacketTypes; export default LightNet;