Add Entity serialization/deserialization

This commit is contained in:
micah 2019-03-06 15:01:37 -05:00
parent ca36777813
commit 6114418264
3 changed files with 82 additions and 7 deletions

View File

@ -5,8 +5,8 @@
"main": "dist/rage-rpc.min.js", "main": "dist/rage-rpc.min.js",
"types": "dist/rage-rpc.d.ts", "types": "dist/rage-rpc.d.ts",
"scripts": { "scripts": {
"watch": "webpack-cli --config ./webpack.config.js --watch", "watch": "webpack-cli --config ./webpack.config.js --mode=development --watch",
"build": "webpack-cli --config ./webpack.config.js", "build": "webpack-cli --config ./webpack.config.js --mode=production",
"type-check": "tsc" "type-check": "tsc"
}, },
"files": [ "files": [

View File

@ -1,3 +1,34 @@
enum MpTypes {
Blip = 'b',
Checkpoint = 'cp',
Colshape = 'c',
Label = 'l',
Marker = 'm',
Object = 'o',
Pickup = 'p',
Player = 'pl',
Vehicle = 'v'
}
function isObjectMpType(obj: any, type: MpTypes){
const client = getEnvironment() === 'client';
if(obj && typeof obj === 'object' && typeof obj.id !== 'undefined'){
const test = (type, collection, mpType) => client ? obj.type === type && collection.at(obj.id) === obj : obj instanceof mpType;
switch(type){
case MpTypes.Blip: return test('blip', mp.blips, mp.Blip);
case MpTypes.Checkpoint: return test('checkpoint', mp.checkpoints, mp.Checkpoint);
case MpTypes.Colshape: return test('colshape', mp.colshapes, mp.Colshape);
case MpTypes.Label: return test('textlabel', mp.labels, mp.TextLabel);
case MpTypes.Marker: return test('marker', mp.markers, mp.Marker);
case MpTypes.Object: return test('object', mp.objects, mp.Object);
case MpTypes.Pickup: return test('pickup', mp.pickups, mp.Pickup);
case MpTypes.Player: return test('player', mp.players, mp.Player);
case MpTypes.Vehicle: return test('vehicle', mp.vehicles, mp.Vehicle);
}
}
return false;
}
export function uid(): string { export function uid(): string {
const first = (Math.random() * 46656) | 0; const first = (Math.random() * 46656) | 0;
const second = (Math.random() * 46656) | 0; const second = (Math.random() * 46656) | 0;
@ -13,11 +44,55 @@ export function getEnvironment(): string {
} }
export function stringifyData(data: any): string { export function stringifyData(data: any): string {
return JSON.stringify(data); const env = getEnvironment();
return JSON.stringify(data, (_, value) => {
if(env === 'client' || env === 'server' && value && typeof value === 'object'){
let type;
if(isObjectMpType(value, MpTypes.Blip)) type = MpTypes.Blip;
else if(isObjectMpType(value, MpTypes.Checkpoint)) type = MpTypes.Checkpoint;
else if(isObjectMpType(value, MpTypes.Colshape)) type = MpTypes.Colshape;
else if(isObjectMpType(value, MpTypes.Marker)) type = MpTypes.Marker;
else if(isObjectMpType(value, MpTypes.Object)) type = MpTypes.Object;
else if(isObjectMpType(value, MpTypes.Pickup)) type = MpTypes.Pickup;
else if(isObjectMpType(value, MpTypes.Player)) type = MpTypes.Player;
else if(isObjectMpType(value, MpTypes.Vehicle)) type = MpTypes.Vehicle;
if(type) return {
__t: type,
i: value.remoteId || value.id
};
}
return value;
});
} }
export function parseData(data: string): any { export function parseData(data: string): any {
return JSON.parse(data); const env = getEnvironment();
return JSON.parse(data, (_, value) => {
if((env === 'client' || env === 'server') && value && typeof value === 'object' && typeof value['__t'] === 'string' && typeof value.i === 'number' && Object.keys(value).length === 2){
const id = value.i;
const type = value['__t'];
let collection;
switch(type){
case MpTypes.Blip: collection = mp.blips; break;
case MpTypes.Checkpoint: collection = mp.checkpoints; break;
case MpTypes.Colshape: collection = mp.colshapes; break;
case MpTypes.Label: collection = mp.labels; break;
case MpTypes.Marker: collection = mp.markers; break;
case MpTypes.Object: collection = mp.objects; break;
case MpTypes.Pickup: collection = mp.pickups; break;
case MpTypes.Player: collection = mp.players; break;
case MpTypes.Vehicle: collection = mp.vehicles; break;
}
if(collection) return collection[env === 'client' ? 'atRemoteId' : 'at'](id);
}
return value;
});
} }
export function promiseResolve(result: any): Promise<any> { export function promiseResolve(result: any): Promise<any> {

View File

@ -4,9 +4,9 @@ const ReplaceInFileWebpackPlugin = require('replace-in-file-webpack-plugin');
const LIBRARY_NAME = 'rpc'; const LIBRARY_NAME = 'rpc';
const OUTPUT_FILE = 'rage-rpc.min.js'; const OUTPUT_FILE = 'rage-rpc.min.js';
module.exports = { module.exports = mode => ({
entry: './src/index.ts', entry: './src/index.ts',
mode: 'production', mode,
module: { module: {
rules: [ rules: [
{ {
@ -38,4 +38,4 @@ module.exports = {
}] }]
}]) }])
] ]
}; });