Commit ec34713f authored by nanahira's avatar nanahira

migrate struct.js to ygopro-msg-encode

parent 72fc9542
......@@ -3,17 +3,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.YGOProMessagesHelper = void 0;
const struct_1 = require("./struct");
exports.YGOProMessagesHelper = exports.LegacyStruct = exports.LegacyStructInst = void 0;
const underscore_1 = __importDefault(require("underscore"));
const structs_json_1 = __importDefault(require("./data/structs.json"));
const typedefs_json_1 = __importDefault(require("./data/typedefs.json"));
const proto_structs_json_1 = __importDefault(require("./data/proto_structs.json"));
const load_constants_1 = __importDefault(require("./load-constants"));
const ygopro_msg_encode_1 = require("ygopro-msg-encode");
const ygopro_msg_struct_compat_1 = require("./ygopro-msg-struct-compat");
const proto_structs_json_1 = __importDefault(require("./data/proto_structs.json"));
class Handler {
constructor(handler, synchronous) {
this.handler = handler;
this.synchronous = synchronous || false;
this.synchronous = synchronous;
}
async handle(buffer, info, datas, params) {
if (this.synchronous) {
......@@ -27,8 +26,50 @@ class Handler {
}
}
}
class LegacyStructInst {
constructor(cls) {
this.cls = cls;
}
_setBuff(buff) {
this.buffer = buff;
}
set(field, value) {
if (!this.buffer || !this.cls)
return;
const inst = (0, ygopro_msg_struct_compat_1.applyYGOProMsgStructCompat)(new this.cls().fromPayload(this.buffer));
inst[field] = value;
const parsed = Buffer.from(inst.toPayload());
if (parsed.length >= this.buffer.length) {
// slice it down
parsed.copy(this.buffer, 0, 0, this.buffer.length);
}
else {
// copy a small part only
parsed.copy(this.buffer, 0, 0, parsed.length);
}
}
}
exports.LegacyStructInst = LegacyStructInst;
class LegacyStruct {
constructor(helper) {
this.helper = helper;
this.protoClasses = new Map();
for (const [direction, list] of Object.entries(proto_structs_json_1.default)) {
for (const [protoStr, structName] of Object.entries(list)) {
if (!structName)
continue;
this.protoClasses.set(structName, this.helper.getProtoClass(protoStr, direction));
}
}
}
get(structName) {
return new LegacyStructInst(this.protoClasses.get(structName));
}
}
exports.LegacyStruct = LegacyStruct;
class YGOProMessagesHelper {
constructor(singleHandleLimit) {
constructor(singleHandleLimit = 1000) {
this.singleHandleLimit = singleHandleLimit;
this.handlers = {
STOC: [new Map(),
new Map(),
......@@ -43,56 +84,8 @@ class YGOProMessagesHelper {
new Map(),
]
};
this.initDatas();
this.initStructs();
if (singleHandleLimit) {
this.singleHandleLimit = singleHandleLimit;
}
else {
this.singleHandleLimit = 1000;
}
}
initDatas() {
this.structs_declaration = structs_json_1.default;
this.typedefs = typedefs_json_1.default;
this.proto_structs = proto_structs_json_1.default;
this.constants = (0, load_constants_1.default)();
}
initStructs() {
this.structs = new Map();
for (let name in this.structs_declaration) {
const declaration = this.structs_declaration[name];
let result = (0, struct_1.Struct)();
for (let field of declaration) {
if (field.encoding) {
switch (field.encoding) {
case "UTF-16LE":
result.chars(field.name, field.length * 2, field.encoding);
break;
default:
throw `unsupported encoding: ${field.encoding}`;
}
}
else {
let type = field.type;
if (this.typedefs[type]) {
type = this.typedefs[type];
}
if (field.length) {
result.array(field.name, field.length, type); //不支持结构体
}
else {
if (this.structs.has(type)) {
result.struct(field.name, this.structs.get(type));
}
else {
result[type](field.name);
}
}
}
}
this.structs.set(name, result);
}
this.structs = new LegacyStruct(this);
}
getDirectionAndProto(protoStr) {
const protoStrMatch = protoStr.match(/^(STOC|CTOS)_([_A-Z]+)$/);
......@@ -117,11 +110,27 @@ class YGOProMessagesHelper {
}
return parseInt(translatedProto);
}
getProtoClass(proto, direction) {
const identifier = typeof proto === 'number' ? proto : this.translateProto(proto, direction);
const registry = direction === 'CTOS' ? ygopro_msg_encode_1.YGOProCtos : direction === 'STOC' ? ygopro_msg_encode_1.YGOProStoc : null;
if (!registry) {
throw `Invalid direction: ${direction}`;
}
return registry.get(identifier);
}
classToProtoStr(cls) {
const registry = cls.prototype instanceof ygopro_msg_encode_1.YGOProCtosBase ? ygopro_msg_encode_1.YGOProCtos : cls.prototype instanceof ygopro_msg_encode_1.YGOProStocBase ? ygopro_msg_encode_1.YGOProStoc : null;
if (!registry) {
throw `Invalid class: ${cls.name}`;
}
const identifier = cls.identifier;
const direction = cls.prototype instanceof ygopro_msg_encode_1.YGOProCtosBase ? 'CTOS' : 'STOC';
return `${direction}_${this.constants[direction][identifier]}`;
}
prepareMessage(protostr, info) {
const { direction, proto } = this.getDirectionAndProto(protostr);
const translatedProto = this.translateProto(proto, direction);
let buffer;
//console.log(proto, this.proto_structs[direction][proto]);
//const directionProtoList = this.constants[direction];
if (typeof info === 'undefined') {
buffer = null;
}
......@@ -129,12 +138,12 @@ class YGOProMessagesHelper {
buffer = info;
}
else {
let struct = this.structs.get(this.proto_structs[direction][proto]);
struct.allocate();
struct.set(info);
buffer = struct.buffer();
const protoCls = this.getProtoClass(translatedProto, direction);
if (!protoCls) {
throw `No proto class for ${protostr}`;
}
buffer = Buffer.from((0, ygopro_msg_struct_compat_1.fromPartialCompat)(protoCls, info).toPayload());
}
const translatedProto = this.translateProto(proto, direction);
let sendBuffer = Buffer.allocUnsafe(3 + (buffer ? buffer.length : 0));
if (buffer) {
sendBuffer.writeUInt16LE(buffer.length + 1, 0);
......@@ -231,13 +240,11 @@ class YGOProMessagesHelper {
}
const handlerCollection = this.handlers[direction][priority];
if (proto && handlerCollection.has(bufferProto)) {
let struct = this.structs.get(this.proto_structs[direction][proto]);
for (const handler of handlerCollection.get(bufferProto)) {
let info = null;
if (struct) {
struct._setBuff(buffer);
info = underscore_1.default.clone(struct.fields);
}
const protoCls = this.getProtoClass(bufferProto, direction);
const info = protoCls
? (0, ygopro_msg_struct_compat_1.applyYGOProMsgStructCompat)(new protoCls().fromPayload(buffer))
: null;
cancel = await handler.handle(buffer, info, datas, params);
if (cancel) {
if (Buffer.isBuffer(cancel)) {
......
import { Struct } from "./struct";
import _ from "underscore";
import structs_declaration from "./data/structs.json";
import typedefs from "./data/typedefs.json";
import proto_structs from "./data/proto_structs.json";
import loadConstants from "./load-constants";
import net from "net";
import { YGOProCtos, YGOProCtosBase, YGOProStoc, YGOProStocBase } from "ygopro-msg-encode";
import { applyYGOProMsgStructCompat, fromPartialCompat } from "./ygopro-msg-struct-compat";
import legacyProtoStructs from "./data/proto_structs.json";
class Handler {
private handler: (buffer: Buffer, info: any, datas: Buffer[], params: any) => Promise<boolean | string | Buffer>;
synchronous: boolean;
constructor(handler: (buffer: Buffer, info: any, datas: Buffer[], params: any) => Promise<boolean | string | Buffer>, synchronous: boolean) {
this.handler = handler;
this.synchronous = synchronous || false;
}
async handle(buffer: Buffer, info: any, datas: Buffer[], params: any): Promise<boolean | string | Buffer> {
constructor(
private handler: (buffer: Buffer, info: YGOProStocBase | YGOProCtosBase, datas: Buffer[], params: any) => Promise<boolean | string | Buffer>,
public synchronous: boolean
) {}
async handle(buffer: Buffer, info: YGOProStocBase | YGOProCtosBase, datas: Buffer[], params: any): Promise<boolean | string | Buffer> {
if (this.synchronous) {
return await this.handler(buffer, info, datas, params);
} else {
......@@ -31,12 +28,21 @@ interface HandlerList {
CTOS: Map<number, Handler[]>[];
}
interface DirectionBase {
STOC: typeof YGOProStocBase;
CTOS: typeof YGOProCtosBase;
}
type DirectionToBase<T>
= T extends `${infer K extends keyof DirectionBase}_${string}` ? DirectionBase[K] : DirectionBase[keyof DirectionBase];
interface DirectionAndProto {
direction: string;
direction: keyof HandlerList;
proto: string;
}
export interface Feedback{
export interface Feedback {
type: string;
message: string;
}
......@@ -64,18 +70,48 @@ export interface Constants {
MSG: Record<string, string>;
}
export class YGOProMessagesHelper {
export class LegacyStructInst {
buffer: Buffer;
constructor(private cls?: typeof YGOProCtosBase | typeof YGOProStocBase) {}
handlers: HandlerList;
structs: Map<string, Struct>;
structs_declaration: Record<string, Struct>;
typedefs: Record<string, string>;
proto_structs: Record<'CTOS' | 'STOC', Record<string, string>>;
constants: Constants;
singleHandleLimit: number;
_setBuff(buff: Buffer) {
this.buffer = buff;
}
constructor(singleHandleLimit?: number) {
this.handlers = {
set(field: string, value: any) {
if (!this.buffer || !this.cls) return;
const inst = applyYGOProMsgStructCompat(new this.cls().fromPayload(this.buffer));
inst[field] = value;
const parsed = Buffer.from(inst.toPayload());
if (parsed.length >= this.buffer.length) {
// slice it down
parsed.copy(this.buffer, 0, 0, this.buffer.length);
} else {
// copy a small part only
parsed.copy(this.buffer, 0, 0, parsed.length);
}
}
}
export class LegacyStruct {
private protoClasses = new Map<string, typeof YGOProCtosBase | typeof YGOProStocBase>();
constructor(private helper: YGOProMessagesHelper) {
for (const [direction, list] of Object.entries(legacyProtoStructs)) {
for (const [protoStr, structName] of Object.entries(list)) {
if(!structName) continue;
this.protoClasses.set(structName, this.helper.getProtoClass(protoStr, direction as keyof HandlerList));
}
}
}
get(structName: string) {
return new LegacyStructInst(this.protoClasses.get(structName));
}
}
export class YGOProMessagesHelper {
handlers: HandlerList = {
STOC: [new Map(),
new Map(),
new Map(),
......@@ -88,56 +124,12 @@ export class YGOProMessagesHelper {
new Map(),
new Map(),
]
}
this.initDatas();
this.initStructs();
if (singleHandleLimit) {
this.singleHandleLimit = singleHandleLimit;
} else {
this.singleHandleLimit = 1000;
}
}
};
constants = loadConstants();
initDatas() {
this.structs_declaration = structs_declaration;
this.typedefs = typedefs;
this.proto_structs = proto_structs;
this.constants = loadConstants();
}
constructor(public singleHandleLimit = 1000) {}
initStructs() {
this.structs = new Map();
for (let name in this.structs_declaration) {
const declaration = this.structs_declaration[name];
let result = Struct();
for (let field of declaration) {
if (field.encoding) {
switch (field.encoding) {
case "UTF-16LE":
result.chars(field.name, field.length * 2, field.encoding);
break;
default:
throw `unsupported encoding: ${field.encoding}`;
}
} else {
let type = field.type;
if (this.typedefs[type]) {
type = this.typedefs[type];
}
if (field.length) {
result.array(field.name, field.length, type); //不支持结构体
} else {
if (this.structs.has(type)) {
result.struct(field.name, this.structs.get(type));
} else {
result[type](field.name);
}
}
}
}
this.structs.set(name, result);
}
}
structs = new LegacyStruct(this);
getDirectionAndProto(protoStr: string): DirectionAndProto {
const protoStrMatch = protoStr.match(/^(STOC|CTOS)_([_A-Z]+)$/);
......@@ -145,13 +137,12 @@ export class YGOProMessagesHelper {
throw `Invalid proto string: ${protoStr}`
}
return {
direction: protoStrMatch[1].toUpperCase(),
direction: protoStrMatch[1].toUpperCase() as keyof HandlerList,
proto: protoStrMatch[2].toUpperCase()
}
}
translateProto(proto: string | number, direction: string): number {
translateProto(proto: string | number, direction: keyof HandlerList): number {
const directionProtoList = this.constants[direction];
if (typeof proto !== "string") {
return proto;
......@@ -165,25 +156,43 @@ export class YGOProMessagesHelper {
return parseInt(translatedProto);
}
getProtoClass<T extends string | number>(proto: T, direction: keyof HandlerList): DirectionToBase<T> {
const identifier = typeof proto === 'number' ? proto : this.translateProto(proto, direction);
const registry = direction === 'CTOS' ? YGOProCtos : direction === 'STOC' ? YGOProStoc : null;
if (!registry) {
throw `Invalid direction: ${direction}`;
}
return registry.get(identifier) as DirectionToBase<T>;
}
classToProtoStr(cls: typeof YGOProCtosBase | typeof YGOProStocBase): string {
const registry = cls.prototype instanceof YGOProCtosBase ? YGOProCtos : cls.prototype instanceof YGOProStocBase ? YGOProStoc : null;
if (!registry) {
throw `Invalid class: ${cls.name}`;
}
const identifier = cls.identifier;
const direction = cls.prototype instanceof YGOProCtosBase ? 'CTOS' : 'STOC';
return `${direction}_${this.constants[direction][identifier]}`;
}
prepareMessage(protostr: string, info?: string | Buffer | any): Buffer {
const {
direction,
proto
} = this.getDirectionAndProto(protostr);
const translatedProto = this.translateProto(proto, direction);
let buffer: Buffer;
//console.log(proto, this.proto_structs[direction][proto]);
//const directionProtoList = this.constants[direction];
if (typeof info === 'undefined') {
buffer = null;
} else if (Buffer.isBuffer(info)) {
buffer = info;
} else {
let struct = this.structs.get(this.proto_structs[direction][proto]);
struct.allocate();
struct.set(info);
buffer = struct.buffer();
const protoCls = this.getProtoClass(translatedProto, direction);
if (!protoCls) {
throw `No proto class for ${protostr}`;
}
buffer = Buffer.from(fromPartialCompat(protoCls, info).toPayload());
}
const translatedProto = this.translateProto(proto, direction);
let sendBuffer = Buffer.allocUnsafe(3 + (buffer ? buffer.length : 0));
if (buffer) {
sendBuffer.writeUInt16LE(buffer.length + 1, 0);
......@@ -214,7 +223,7 @@ export class YGOProMessagesHelper {
return this.send(socket, sendBuffer);
}
addHandler(protostr: string, handler: (buffer: Buffer, info: any, datas: Buffer[], params: any) => Promise<boolean | string>, synchronous: boolean, priority: number) {
addHandler<T extends string>(protostr: T, handler: (buffer: Buffer, info: InstanceType<DirectionToBase<T>>, datas: Buffer[], params: any) => Promise<boolean | string>, synchronous: boolean, priority: number) {
if (priority < 0 || priority > 4) {
throw "Invalid priority: " + priority;
}
......@@ -232,7 +241,7 @@ export class YGOProMessagesHelper {
handlerCollection.get(translatedProto).push(handlerObj);
}
async handleBuffer(messageBuffer: Buffer, direction: string, protoFilter?: string[], params?: any, preconnect = false): Promise<HandleResult> {
async handleBuffer(messageBuffer: Buffer, direction: keyof HandlerList, protoFilter?: string[], params?: any, preconnect = false): Promise<HandleResult> {
let feedback: Feedback = null;
let messageLength = 0;
let bufferProto = 0;
......@@ -281,13 +290,11 @@ export class YGOProMessagesHelper {
}
const handlerCollection: Map<number, Handler[]> = this.handlers[direction][priority];
if (proto && handlerCollection.has(bufferProto)) {
let struct = this.structs.get(this.proto_structs[direction][proto]);
for (const handler of handlerCollection.get(bufferProto)) {
let info = null;
if (struct) {
struct._setBuff(buffer);
info = _.clone(struct.fields);
}
const protoCls = this.getProtoClass(bufferProto, direction);
const info = protoCls
? applyYGOProMsgStructCompat(new protoCls().fromPayload(buffer))
: null;
cancel = await handler.handle(buffer, info, datas, params);
if (cancel) {
if (Buffer.isBuffer(cancel)) {
......
{
"HostInfo": [
{"name": "lflist", "type": "unsigned int"},
{"name": "rule", "type": "unsigned char"},
{"name": "mode", "type": "unsigned char"},
{"name": "duel_rule", "type": "unsigned char"},
{"name": "no_check_deck", "type": "bool"},
{"name": "no_shuffle_deck", "type": "bool"},
{"name": "start_lp", "type": "unsigned int"},
{"name": "start_hand", "type": "unsigned char"},
{"name": "draw_count", "type": "unsigned char"},
{"name": "time_limit", "type": "unsigned short"}
],
"HostPacket": [
{"name": "identifier", "type": "unsigned short"},
{"name": "version", "type": "unsigned short"},
{"name": "port", "type": "unsigned short"},
{"name": "ipaddr", "type": "unsigned int"},
{"name": "name", "type": "unsigned short", "length": 20, "encoding": "UTF-16LE"},
{"name": "host", "type": "HostInfo"}
],
"HostRequest": [
{"name": "identifier", "type": "unsigned short"}
],
"CTOS_HandResult": [
{"name": "res", "type": "unsigned char"}
],
"CTOS_TPResult": [
{"name": "res", "type": "unsigned char"}
],
"CTOS_PlayerInfo": [
{"name": "name", "type": "unsigned short", "length": 20, "encoding": "UTF-16LE"}
],
"CTOS_CreateGame": [
{"name": "info", "type": "HostInfo"},
{"name": "name", "type": "unsigned short", "length": 20, "encoding": "UTF-16LE"},
{"name": "pass", "type": "unsigned short", "length": 20, "encoding": "UTF-16LE"}
],
"CTOS_JoinGame": [
{"name": "version", "type": "unsigned short"},
{"name": "align", "type": "unsigned short"},
{"name": "gameid", "type": "unsigned int"},
{"name": "pass", "type": "unsigned short", "length": 20, "encoding": "UTF-16LE"}
],
"CTOS_ExternalAddress": [
{"name": "real_ip", "type": "unsigned int"},
{"name": "hostname", "type": "unsigned short", "length":"256", "encoding": "UTF-16LE"}
],
"CTOS_Kick": [
{"name": "pos", "type": "unsigned char"}
],
"STOC_ErrorMsg": [
{"name": "msg", "type": "unsigned char"},
{"name": "align1", "type": "unsigned char"},
{"name": "align2", "type": "unsigned char"},
{"name": "align3", "type": "unsigned char"},
{"name": "code", "type": "unsigned int"}
],
"STOC_HandResult": [
{"name": "res1", "type": "unsigned char"},
{"name": "res2", "type": "unsigned char"}
],
"STOC_CreateGame": [
{"name": "gameid", "type": "unsigned int"}
],
"STOC_JoinGame": [
{"name": "info", "type": "HostInfo"}
],
"STOC_TypeChange": [
{"name": "type", "type": "unsigned char"}
],
"STOC_ExitGame": [
{"name": "pos", "type": "unsigned char"}
],
"STOC_TimeLimit": [
{"name": "player", "type": "unsigned char"},
{"name": "left_time", "type": "unsigned short"}
],
"STOC_Chat": [
{"name": "player", "type": "unsigned short"},
{"name": "msg", "type": "unsigned short", "length": 255, "encoding": "UTF-16LE"}
],
"STOC_HS_PlayerEnter": [
{"name": "name", "type": "unsigned short", "length": 20, "encoding": "UTF-16LE"},
{"name": "pos", "type": "unsigned char"},
{"name": "padding", "type": "unsigned char"}
],
"STOC_HS_PlayerChange": [
{"name": "status", "type": "unsigned char"}
],
"STOC_HS_WatchChange": [
{"name": "watch_count", "type": "unsigned short"}
],
"GameMsg_Hint_Card_only": [
{"name": "curmsg", "type": "word8Ule"},
{"name": "type", "type": "word8"},
{"name": "player", "type": "word8"},
{"name": "data", "type": "word32Ule"}
],
"deck": [
{"name": "mainc", "type": "unsigned int"},
{"name": "sidec", "type": "unsigned int"},
{"name": "deckbuf", "type": "unsigned int", "length": 90}
],
"chat": [
{"name": "msg", "type": "unsigned short", "length":"255", "encoding": "UTF-16LE"}
],
"STOC_DeckCount": [
{"name": "mainc_s", "type": "unsigned short"},
{"name": "sidec_s", "type": "unsigned short"},
{"name": "extrac_s", "type": "unsigned short"},
{"name": "mainc_o", "type": "unsigned short"},
{"name": "sidec_o", "type": "unsigned short"},
{"name": "extrac_o", "type": "unsigned short"}
]
}
{
"unsigned int": "word32Ule",
"unsigned short": "word16Ule",
"unsigned char": "word8",
"bool": "bool2"
}
\ No newline at end of file
......@@ -136,3 +136,6 @@ const loadConstants = () => {
};
exports.loadConstants = loadConstants;
exports.default = exports.loadConstants;
if (require.main === module) {
console.log(JSON.stringify((0, exports.loadConstants)(), null, 2));
}
......@@ -171,3 +171,7 @@ export const loadConstants = () => {
};
export default loadConstants;
if (require.main === module) {
console.log(JSON.stringify(loadConstants(), null, 2));
}
// a special version of node-struct by xdenser
// https://github.com/xdenser/node-struct/tree/f843487d6768cd0bf20c2ce7803dde2d92df5694
function byteField(p, offset) {
this.length = 1;
this.get = function () {
return p.buf[offset];
}
this.set = function (val) {
p.buf[offset] = val;
}
}
function boolField(p, offset, length) {
this.length = length;
this.get = function () {
return (p.buf[offset] > 0 );
}
this.set = function (val) {
p.buf[offset] = val ? 1 : 0;
}
}
function intField(p, offset, length, le, signed) {
this.length = length;
function bec(cb) {
for (var i = 0; i < length; i++)
cb(i, length - i - 1);
}
function lec(cb) {
for (var i = 0; i < length; i++)
cb(i, i);
}
function getUVal(bor) {
var val = 0;
bor(function (i, o) {
val += Math.pow(256, o) * p.buf[offset + i];
})
return val;
}
function getSVal(bor) {
var val = getUVal(bor);
if ((p.buf[offset + ( le ? (length - 1) : 0)] & 0x80) == 0x80) {
val -= Math.pow(256, length);
}
return val;
}
function setVal(bor, val) {
bor(function (i, o) {
p.buf[offset + i] = Math.floor(val / Math.pow(256, o)) & 0xff;
});
}
this.get = function () {
var bor = le ? lec : bec;
return ( signed ? getSVal(bor) : getUVal(bor));
}
this.set = function (val) {
var bor = le ? lec : bec;
setVal(bor, val);
}
}
function charField(p, offset, length, encoding) {
this.length = length;
this.encoding = encoding;
this.get = function () {
var result = p.buf.toString(this.encoding, offset, offset + length);
var strlen = result.indexOf("\0");
if (strlen == -1) {
return result;
} else {
return result.slice(0, strlen);
}
}
this.set = function (val) {
val += "\0";
if (val.length > length)
val = val.substring(0, length);
p.buf.write(val, offset, this.encoding);
}
}
function structField(p, offset, struct) {
this.length = struct.length();
this.get = function () {
return struct;
}
this.set = function (val) {
struct.set(val);
}
this.allocate = function () {
struct._setBuff(p.buf.slice(offset, offset + struct.length()));
}
}
function arrayField(p, offset, len, type) {
var as = Struct();
var args = [].slice.call(arguments, 4);
args.unshift(0);
for (var i = 0; i < len; i++) {
if (type instanceof Struct) {
as.struct(i, type.clone());
} else if (type in as) {
args[0] = i;
as[type].apply(as, args);
}
}
this.length = as.length();
this.allocate = function () {
as._setBuff(p.buf.slice(offset, offset + as.length()));
}
this.get = function () {
return as;
}
this.set = function (val) {
as.set(val);
}
}
function Struct() {
if (!(this instanceof Struct))
return new Struct;
var priv = {
buf: {},
allocated: false,
len: 0,
fields: {},
closures: []
}, self = this;
function checkAllocated() {
if (priv.allocated)
throw new Error('Cant change struct after allocation');
}
this.word8 = function (key) {
checkAllocated();
priv.closures.push(function (p) {
p.fields[key] = new byteField(p, p.len);
p.len++;
});
return this;
};
// Create handlers for various Bool Field Variants
[1, 2, 3, 4].forEach(function (n) {
self['bool' + (n == 1 ? '' : n)] = function (key) {
checkAllocated();
priv.closures.push(function (p) {
p.fields[key] = new boolField(p, p.len, n);
p.len += n;
});
return this;
}
});
// Create handlers for various Integer Field Variants
[1, 2, 3, 4, 6, 8].forEach(function (n) {
[true, false].forEach(function (le) {
[true, false].forEach(function (signed) {
var name = 'word' + (n * 8) + ( signed ? 'S' : 'U') + ( le ? 'le' : 'be');
self[name] = function (key) {
checkAllocated();
priv.closures.push(function (p) {
p.fields[key] = new intField(p, p.len, n, le, signed);
p.len += n;
});
return this;
};
});
});
});
this.chars = function (key, length, encoding) {
checkAllocated();
priv.closures.push(function (p) {
p.fields[key] = new charField(p, p.len, length, encoding || 'ascii');
p.len += length;
});
return this;
}
this.struct = function (key, struct) {
checkAllocated();
priv.closures.push(function (p) {
p.fields[key] = new structField(p, p.len, struct.clone());
p.len += p.fields[key].length;
});
return this;
}
function construct(constructor, args) {
function F() {
return constructor.apply(this, args);
}
F.prototype = constructor.prototype;
return new F();
}
this.array = function (key, length, type) {
checkAllocated();
var args = [].slice.call(arguments, 1);
args.unshift(null);
args.unshift(null);
priv.closures.push(function (p) {
args[0] = p;
args[1] = p.len;
p.fields[key] = construct(arrayField, args);
p.len += p.fields[key].length;
});
return this;
}
var beenHere = false;
function applyClosures(p) {
if (beenHere)
return;
p.closures.forEach(function (el) {
el(p);
});
beenHere = true;
}
function allocateFields() {
for (var key in priv.fields) {
if ('allocate' in priv.fields[key])
priv.fields[key].allocate();
}
}
this._setBuff = function (buff) {
priv.buf = buff;
applyClosures(priv);
allocateFields();
priv.allocated = true;
}
this.allocate = function () {
applyClosures(priv);
priv.buf = Buffer.alloc(priv.len);
allocateFields();
priv.allocated = true;
return this;
}
this._getPriv = function () {
return priv;
}
this.clone = function () {
var c = new Struct;
var p = c._getPriv();
p.closures = priv.closures;
return c;
}
this.length = function () {
applyClosures(priv);
return priv.len;
}
this.get = function (key) {
if (key in priv.fields) {
return priv.fields[key].get();
} else
throw new Error('Can not find field ' + key);
}
this.set = function (key, val) {
if (arguments.length == 2) {
if (key in priv.fields) {
priv.fields[key].set(val);
} else
throw new Error('Can not find field ' + key);
} else if (Buffer.isBuffer(key)) {
this._setBuff(key);
} else {
for (var k in key) {
this.set(k, key[k]);
}
}
}
this.buffer = function () {
return priv.buf;
}
function getFields() {
var fields = {};
Object.keys(priv.fields).forEach(function (key) {
Object.defineProperty(fields, key, {
get: function () {
var res = self.get(key);
if (res instanceof Struct) return res.fields;
else return res;
},
set: function (newVal) {
self.set(key, newVal);
},
enumerable: true
});
});
return fields;
};
var _fields;
Object.defineProperty(this, 'fields', {
get: function () {
if (_fields) return _fields;
return (_fields = getFields());
},
enumerable: true,
configurable: true
});
}
exports.Struct = Struct;
......@@ -3383,8 +3383,6 @@ ygopro.ctos_follow 'CHAT', true, (buffer, info, client, server, datas)->
else
ygopro.stoc_send_chat(client, "${get_chat_color_default}", ygopro.constants.COLORS.BABYBLUE)
#when '/test'
# ygopro.stoc_send_hint_card_to_room(room, 2333365)
if (msg.length>100)
log.warn "SPAM WORD", client.name, client.ip, msg
client.abuse_count=client.abuse_count+2 if client.abuse_count
......
......@@ -4530,8 +4530,6 @@
}
}
}
//when '/test'
// ygopro.stoc_send_hint_card_to_room(room, 2333365)
if (msg.length > 100) {
log.warn("SPAM WORD", client.name, client.ip, msg);
if (client.abuse_count) {
......
......@@ -2,7 +2,6 @@ _ = require 'underscore'
_.str = require 'underscore.string'
_.mixin(_.str.exports())
Struct = require('./struct.js').Struct
loadJSON = require('load-json-file').sync
@i18ns = loadJSON './data/i18n.json'
......@@ -23,9 +22,6 @@ YGOProMessagesHelper = require("./YGOProMessages.js").YGOProMessagesHelper # 为
@helper = new YGOProMessagesHelper(9000)
@structs = @helper.structs
@structs_declaration = @helper.structs_declaration
@typedefs = @helper.typedefs
@proto_structs = @helper.proto_structs
@constants = @helper.constants
translateHandler = (handler) ->
......@@ -91,26 +87,6 @@ translateHandler = (handler) ->
room.recordChatMessage(msg, player)
return
@stoc_send_hint_card_to_room = (room, card)->
if !room
console.log "err stoc_send_hint_card_to_room"
return
for client in room.players
@stoc_send client, 'GAME_MSG', {
curmsg: 2,
type: 10,
player: 0,
data: card
} if client
for client in room.watchers
@stoc_send client, 'GAME_MSG', {
curmsg: 2,
type: 10,
player: 0,
data: card
} if client
return
@stoc_die = (client, msg)->
await @stoc_send_chat(client, msg, @constants.COLORS.RED)
await @stoc_send client, 'ERROR_MSG', {
......
// Generated by CoffeeScript 2.7.0
(function() {
var Struct, YGOProMessagesHelper, _, loadJSON, translateHandler;
var YGOProMessagesHelper, _, loadJSON, translateHandler;
_ = require('underscore');
......@@ -8,8 +8,6 @@
_.mixin(_.str.exports());
Struct = require('./struct.js').Struct;
loadJSON = require('load-json-file').sync;
this.i18ns = loadJSON('./data/i18n.json');
......@@ -47,12 +45,6 @@
this.structs = this.helper.structs;
this.structs_declaration = this.helper.structs_declaration;
this.typedefs = this.helper.typedefs;
this.proto_structs = this.helper.proto_structs;
this.constants = this.helper.constants;
translateHandler = function(handler) {
......@@ -153,38 +145,6 @@
room.recordChatMessage(msg, player);
};
this.stoc_send_hint_card_to_room = function(room, card) {
var client, i, j, len, len1, ref, ref1;
if (!room) {
console.log("err stoc_send_hint_card_to_room");
return;
}
ref = room.players;
for (i = 0, len = ref.length; i < len; i++) {
client = ref[i];
if (client) {
this.stoc_send(client, 'GAME_MSG', {
curmsg: 2,
type: 10,
player: 0,
data: card
});
}
}
ref1 = room.watchers;
for (j = 0, len1 = ref1.length; j < len1; j++) {
client = ref1[j];
if (client) {
this.stoc_send(client, 'GAME_MSG', {
curmsg: 2,
type: 10,
player: 0,
data: card
});
}
}
};
this.stoc_die = async function(client, msg) {
await this.stoc_send_chat(client, msg, this.constants.COLORS.RED);
if (client) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment