UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

101 lines (93 loc) 2.79 kB
/** * Defines how to serialize and deserialize an instance of a given class. * This class is intended to be overridden. * * @template T * * @example * class BookSerializer extends BinaryClassSerializationAdapter{ * klass = Book * version = 0 * * serialize(buffer: BinaryBuffer, book: Book){ * * buffer.writeUTF8String(book.title); * buffer.writeUint32(book.page_count); * buffer.writeUTF8String(book.text); * * } * * deserialize(buffer: BinaryBuffer, book: Book){ * * book.title = buffer.readUTF8String(); * book.page_count = buffer.readUint32(); * book.text = buffer.readUTF8String(); * * } * } * * @author Alex Goldring * @copyright Company Named Limited (c) 2025 */ export class BinaryClassSerializationAdapter { /** * Class that this adapter handles. * NOTE: misspelling of the word "class" is intentional, in JavaScript "class" is a reserved word. * @example * klass = Vector3 * @protected * @type {Class<T>} */ klass = null; /** * Format version number, used to check for format changes and data compatibility * Increment the number if you change the adapter to indicate the change in format * See {@link BinaryClassUpgrader} for details on how to handle support for outdated format versions * @protected * @type {number} */ version = 0; /** * Invoked externally as part of the serialization lifecycle. * Guaranteed to be invoked before the first usage of the adapter. * * @param args */ initialize(...args) { //override as needed } /** * Handle any necessary resource cleanup. * Invoked externally as part of the serialization lifecycle * Guaranteed to be invoked after the last usage of the adapter. * */ finalize() { //override as need } /** * Serialize an instance into the passed buffer. * Will write at the current {@link BinaryBuffer.position}. * * @param {BinaryBuffer} buffer * @param {T} value */ serialize(buffer, value) { //override as necessary } /** * Read value from the buffer, passed in value will change as a result. * Will read from the current {@link BinaryBuffer.position}. * * @param {BinaryBuffer} buffer * @param {T} value */ deserialize(buffer, value) { //override as necessary } } /** * @readonly * @type {boolean} */ BinaryClassSerializationAdapter.prototype.isBinaryClassSerializationAdapter = true;