refactor: auto packet defgen

This commit is contained in:
cha0s 2019-04-14 21:32:48 -05:00
parent d635918cfd
commit 506bfec19b
12 changed files with 41 additions and 22 deletions

View File

@ -14,7 +14,9 @@ import {Room, RoomView} from '@avocado/topdown';
import {World} from '@avocado/physics/matter/world';
import {clearAnimation, setAnimation, Ticker} from '@avocado/timing';
// 1st party.
import {InputPacket, KeysPacket, StatePacket} from '../common/packet';
import {InputPacket} from '../common/packet/input.packet';
import {KeysPacket} from '../common/packet/keys.packet';
import {StatePacket} from '../common/packet/state.packet';
import {augmentParserWithThroughput} from '../common/parser-throughput';
import {WorldTime} from '../common/world-time';
import {App} from './app';

View File

@ -1,12 +0,0 @@
import {registerPacket} from '@avocado/packet';
import {InputPacket} from './input';
registerPacket(InputPacket);
import {KeysPacket} from './keys';
registerPacket(KeysPacket);
import {StatePacket} from './state';
registerPacket(StatePacket);
export {InputPacket, KeysPacket, StatePacket};

View File

@ -43,9 +43,9 @@ module.exports = function(source) {
// Validate schema.
validateOptions(schema, options, 'Avocado defgen');
// Extract options.
const {paths, registrar, type} = options;
const {classTransformer: transformer, paths, registrar, type} = options;
// Search avocado.
options.paths.push(path.resolve(
paths.push(path.resolve(
__dirname, 'node_modules', '@avocado',
));
// Get all trait source paths.
@ -79,16 +79,18 @@ module.exports = function(source) {
const rest = part.substr(1).toLowerCase();
return className + firstLetter + rest;
}, '');
const transformed = transformer ? transformer(className) : className;
// Import and register.
return [
`import {${className}} from '${importPath}';`,
`${registrar.function}(${className});`,
`import {${transformed}} from '${importPath}';`,
`${registrar.function}(${transformed});`,
].join('\n');
});
// Import trait registry first.
return [
const output = [
`import {${registrar.function}} from '${registrar.module}'`,
'',
...importDefinitions
].join('\n');
return output;
}

0
register-packets.js Normal file
View File

View File

@ -6,7 +6,7 @@ import {performance} from 'perf_hooks';
import {Synchronizer} from '@avocado/state';
import {Ticker} from '@avocado/timing';
// 1st party.
import {InputPacket} from '../common/packet';
import {InputPacket} from '../common/packet/input.packet';
import {WorldTime} from '../common/world-time';
import {createEntityForConnection} from './create-entity-for-connection';
import {createRoom} from './create-server-room';

View File

@ -7,8 +7,8 @@ import {Trait} from '@avocado/entity';
import {Rectangle, Vector} from '@avocado/math';
import {Packer, Synchronizer} from '@avocado/state';
import {KeysPacket} from '../../common/packet/keys';
import {StatePacket} from '../../common/packet/state';
import {KeysPacket} from '../../common/packet/keys.packet';
import {StatePacket} from '../../common/packet/state.packet';
const decorate = compose(
);

View File

@ -7,6 +7,7 @@ const config = require('./webpack.common.config');
config.entry = {
client: [
'@babel/polyfill',
path.join(__dirname, 'register-packets.js'),
path.join(__dirname, 'register-traits.js'),
'@avocado/behavior/item/initialize',
path.join(__dirname, 'client', 'index.js'),
@ -37,6 +38,9 @@ config.devtool = 'eval-source-map';
config.module.rules[1].use.options.paths.push(
path.resolve(__dirname, 'client'),
);
config.module.rules[2].use.options.paths.push(
path.resolve(__dirname, 'client'),
);
config.node = {
fs: 'empty',
path: 'empty',

View File

@ -16,6 +16,25 @@ const config = {
loader: 'babel-loader',
},
},
{
test: /register-packets\.js/,
use: {
loader: './defgen',
options: {
classTransformer: (Packet) => {
return `${Packet}Packet`;
},
paths: [
path.resolve(__dirname, 'common'),
],
registrar: {
function: 'registerPacket',
module: '@avocado/packet',
},
type: 'packet',
},
},
},
{
test: /register-traits\.js/,
use: {
@ -26,7 +45,7 @@ const config = {
],
registrar: {
function: 'registerTrait',
module: '@avocado/entity/trait-registry',
module: '@avocado/entity',
},
type: 'trait',
},

View File

@ -9,6 +9,7 @@ config.entry = {
server: [
'source-map-support/register',
'@babel/polyfill',
path.join(__dirname, 'register-packets.js'),
path.join(__dirname, 'register-traits.js'),
'@avocado/behavior/item/initialize',
path.join(__dirname, 'server', 'index.js'),
@ -22,6 +23,9 @@ config.externals = [
config.module.rules[1].use.options.paths.push(
path.resolve(__dirname, 'server'),
);
config.module.rules[2].use.options.paths.push(
path.resolve(__dirname, 'server'),
);
const nodeArgs = [];
if (process.argv.find((arg) => '--prof' === arg)) {
nodeArgs.push('--prof');