Code Monkey home page Code Monkey logo

Comments (1)

clibequilibrium avatar clibequilibrium commented on July 18, 2024 1

Would be good to include a serialization guide like the one in this library: https://github.com/NateTheGreatt/bitECS/blob/master/src/Serialize.js

Hi I played around with it. I use simple JSON.stringify combined with pako delfate

import { World } from 'miniplex';
import { Inflate, deflate, inflate } from 'pako';
import { Entity } from '../components';

interface SerializedWorldData {
	entities: Entity[];
}

class WorldSerializer {
	constructor(public readonly world: World<Entity>) {}

	public toJSON(): SerializedWorldData {
		// eslint-disable-next-line @typescript-eslint/no-explicit-any
		const { entities } = this.world as any;

		return {
			entities: entities
		};
	}

	static fromJSON(json: SerializedWorldData): World<Entity> {
		const instance = new WorldSerializer(new World<Entity>(json.entities));
		return instance.world;
	}
}

/**
 * Serializes the world and chunks the array buffer. Uses pako deflate
 * @param world
 * @param chunkSize
 * @returns array of buffers
 */
export function serializeWorld(world: World<Entity>, chunkSize: number): ArrayBuffer[] {
	const deflated = deflate(JSON.stringify(new WorldSerializer(world)));
	const buffers: ArrayBuffer[] = [];

	for (let i = 0; i < deflated.byteLength; i += chunkSize) {
		const chunk = deflated.slice(i, i + chunkSize);
		buffers.push(chunk);
	}

	return buffers;
}

/**
 * Deserializes the world from chunks. Uses pako inflate from provided chunks
 * @param buffers
 * @param maxEntities
 * @returns world
 */
export function deserializeWorld(buffers: ArrayBuffer[] | undefined): World<Entity> {
	let world: World<Entity>;

	// Use pako to inflate the chunks
	if (buffers && buffers.length !== 0) {
		const inflate = new Inflate({ to: 'string' });

		for (let i = 0; i < buffers.length; i++) {
			const chunk = buffers[i];

			if (i >= buffers.length) {
				inflate.push(chunk, true);
			} else {
				inflate.push(chunk, false);
			}
		}

		if (inflate.err) {
			console.error(inflate.err);
			throw new Error(inflate.msg);
		}

		// Deserialize the world from the inflate buffer
		if (inflate.result) {
			world = WorldSerializer.fromJSON(JSON.parse(inflate.result as string));
		} else {
			throw new Error('Fatal error: Inflate result is null during loading.');
		}
	} else {
		world = new World<Entity>();
	}

	return world;
}

It is optimized for both performance and low memory footprint.

from miniplex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.