Comments (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)
- `archetype.first`
- [Idea] Allow `World.add` to take a setup function as an optional second argument
- Offer bound `ECS.useEntities(world => bucket)` API
- `useCurrentEntity` should throw when not run inside an entity context
- <Entity> will destroy and re-create its entity on every rerender
- `Query` (for 2.0)
- Publish `@miniplex/bucket` as `bucketeer`
- Garbage collection for Archetypes HOT 3
- Offer a ref-based capture mechanism alongside the `<Component>`-based one
- Remove all mentions of "archetypes" from the documentation, refer to "queries" instead
- Apply new `onEntityAdded` listeners to entities that are already in the query
- Research how well Miniplex works with class instances as entities (!) HOT 1
- Support setting components directly on `<Entity>`
- Miniplex 2.0 Release Checklist HOT 1
- Accessing Entity Defined in Player Function from App.jsx File HOT 1
- Best way to represent multiple entities? HOT 2
- miniplex + rapier = 💔? HOT 2
- Compared to Bitecs.js's ECS architecture mode, does EC mode perform better?
- Query evaluation against outdated version of entity in nested component removal requests HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from miniplex.