Skip to content

Commit

Permalink
Release 0.1.3 - Small bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
WiredSpast committed Feb 6, 2022
1 parent ecbfae1 commit 844cc6f
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 23 deletions.
36 changes: 18 additions & 18 deletions lib/extension/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { EventEmitter } = require("events");
const net = require("net");
const util = require("util");

const OUTGOING_MESSAGE_IDS = {
const INCOMING_MESSAGE_IDS = {
ONDOUBLECLICK: 1,
INFOREQUEST: 2,
PACKETINTERCEPT: 3,
Expand All @@ -20,7 +20,7 @@ const OUTGOING_MESSAGE_IDS = {
STRINGTOPACKET_RESPONSE: 21
}

const INCOMING_MESSAGE_IDS = {
const OUTGOING_MESSAGE_IDS = {
EXTENSIONINFO: 1,
MANIPULATEDPACKET: 2,
REQUESTFLAGS: 3,
Expand Down Expand Up @@ -77,7 +77,7 @@ exports.Extension = class Extension extends EventEmitter {
return;
}

throw new Error("Invalid constructor arguments, extensionInfo object requires name, description, version and author");
throw new Error("Extension.constructor: extensionInfo object requires name, description, version and author");
}

run() {
Expand Down Expand Up @@ -140,10 +140,10 @@ exports.Extension = class Extension extends EventEmitter {

#onGPacket(packet) {
switch(packet.headerId()) {
case OUTGOING_MESSAGE_IDS.INFOREQUEST:
case INCOMING_MESSAGE_IDS.INFOREQUEST:
let file = this.#getArgument(FILE_FLAG);
let cookie = this.#getArgument(COOKIE_FLAG);
let response = new HPacket(INCOMING_MESSAGE_IDS.EXTENSIONINFO)
let response = new HPacket(OUTGOING_MESSAGE_IDS.EXTENSIONINFO)
.appendString(this.#extensionInfo.name)
.appendString(this.#extensionInfo.author)
.appendString(this.#extensionInfo.version)
Expand All @@ -156,7 +156,7 @@ exports.Extension = class Extension extends EventEmitter {
.appendBoolean(true); // DeleteButtonVisible
this.#gEarthExtensionServer.write(response.toBytes());
break;
case OUTGOING_MESSAGE_IDS.CONNECTIONSTART:
case INCOMING_MESSAGE_IDS.CONNECTIONSTART:
let host = packet.readString();
let connectionPort = packet.readInteger();
let hotelVersion = packet.readString();
Expand All @@ -167,10 +167,10 @@ exports.Extension = class Extension extends EventEmitter {
this.emit('connect', host, connectionPort, hotelVersion, clientIdentifier, client);
this.emit('start');
break;
case OUTGOING_MESSAGE_IDS.CONNECTIONEND:
case INCOMING_MESSAGE_IDS.CONNECTIONEND:
this.emit('end');
break;
case OUTGOING_MESSAGE_IDS.FLAGSCHECK:
case INCOMING_MESSAGE_IDS.FLAGSCHECK:
if(this.#flagRequestCallback !== null && this.#flagRequestCallback !== undefined) {
let arraySize = packet.readInteger();
let gEarthArgs = [];
Expand All @@ -181,20 +181,20 @@ exports.Extension = class Extension extends EventEmitter {
}
this.#flagRequestCallback = null;
break;
case OUTGOING_MESSAGE_IDS.INIT:
case INCOMING_MESSAGE_IDS.INIT:
this.emit('init');
this.#writeToConsole("Extension \"" + this.#extensionInfo.name + "\" successfully initialized", "green", false);
break;
case OUTGOING_MESSAGE_IDS.ONDOUBLECLICK:
case INCOMING_MESSAGE_IDS.ONDOUBLECLICK:
this.emit('click');
break;
case OUTGOING_MESSAGE_IDS.PACKETINTERCEPT:
case INCOMING_MESSAGE_IDS.PACKETINTERCEPT:
let stringMessage = packet.readLongString();
let hMessage = new HMessage(stringMessage);

this.#modifyMessage(hMessage);

let responsePacket = new HPacket(INCOMING_MESSAGE_IDS.MANIPULATEDPACKET);
let responsePacket = new HPacket(OUTGOING_MESSAGE_IDS.MANIPULATEDPACKET);
responsePacket.appendLongString(hMessage.stringify());

this.#gEarthExtensionServer.write(responsePacket.toBytes());
Expand Down Expand Up @@ -254,14 +254,14 @@ exports.Extension = class Extension extends EventEmitter {
if(packet instanceof HPacket) {
return this.#send(packet, HDirection.TOCLIENT);
}
throw new Error("Invalid arguments passed");
throw new Error("Extension.sendToClient: packet must be an instance of HPacket");
}

sendToServer(packet) {
if(packet instanceof HPacket) {
return this.#send(packet, HDirection.TOSERVER);
}
throw new Error("Invalid arguments passed");
throw new Error("Extension.sendToServer: packet must be an instance of HPacket");
}

#send(packet, direction) {
Expand All @@ -270,7 +270,7 @@ exports.Extension = class Extension extends EventEmitter {
if(!packet.isPacketComplete()) packet.completePacket(this.#packetInfoManager);
if(!packet.isPacketComplete()) return false;

let sendingPacket = new HPacket(INCOMING_MESSAGE_IDS.SENDMESSAGE);
let sendingPacket = new HPacket(OUTGOING_MESSAGE_IDS.SENDMESSAGE);
sendingPacket.appendByte(direction);
sendingPacket.appendInt(packet.getBytesLength());
sendingPacket.appendBytes(packet.toBytes());
Expand All @@ -286,7 +286,7 @@ exports.Extension = class Extension extends EventEmitter {
if(this.#flagRequestCallback !== null) return false;
this.#flagRequestCallback = flagRequestCallback;
try {
this.#gEarthExtensionServer.write(new HPacket(INCOMING_MESSAGE_IDS.REQUESTFLAGS).toBytes());
this.#gEarthExtensionServer.write(new HPacket(OUTGOING_MESSAGE_IDS.REQUESTFLAGS).toBytes());
return true;
} catch {
return false;
Expand All @@ -299,7 +299,7 @@ exports.Extension = class Extension extends EventEmitter {
}

if(typeof(colorClass) !== "string" || typeof(s) !== "string") {
throw new Error("Invalid arguments passed")
throw new Error("Extensions.writeToConsole: Both s and colorClass have to be strings")
}

this.#writeToConsole(s, colorClass, true);
Expand All @@ -308,7 +308,7 @@ exports.Extension = class Extension extends EventEmitter {
#writeToConsole(s, colorClass, mentionTitle) {
let text = "[" + colorClass + "]" + (mentionTitle ? this.#extensionInfo.name + " --> " : "") + s;

let packet = new HPacket(INCOMING_MESSAGE_IDS.EXTENSIONCONSOLELOG);
let packet = new HPacket(OUTGOING_MESSAGE_IDS.EXTENSIONCONSOLELOG);
packet.appendString(text);
try {
this.#gEarthExtensionServer.write(packet.toBytes());
Expand Down
2 changes: 1 addition & 1 deletion lib/extension/parsers/hflooritem.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ exports.HFloorItem = class HFloorItem {
return furniture;
}

constructPacket(floorItems, headerId) {
static constructPacket(floorItems, headerId) {
if(!(Array.isArray(floorItems) || !Number.isInteger(headerId))) {
throw new Error("HFlooritem.constructPacket: headerId must be an integer");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/extension/parsers/hinventoryitem.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ exports.HInventoryItem = class HInventoryItem {
.appendInt(this.#roomId);

if(this.#furniType === HFurniType.FLOOR) {
packet.appendString(this.#slotId | "")
packet.appendString(this.#slotId || "")
.appendInt(this.#extra | 0);
}
}
Expand Down
6 changes: 6 additions & 0 deletions lib/extension/parsers/huserprofile.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { HGroup } from "./hgroup";
export class HUserProfile {
constructor(packet: HPacket);

/**
* Construct a packet containing the user profile
* @param headerId Header id to assign to created packet
*/
constructPacket(headerId: number): HPacket;

/**
* Get user id
*/
Expand Down
30 changes: 29 additions & 1 deletion lib/extension/parsers/huserprofile.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,34 @@ exports.HUserProfile = class HUserProfile {
this.#openProfile = packet.readBoolean();
}

constructPacket(headerId) {
if(!Number.isInteger(headerId)) {
throw new Error("HUserProfile.constructPacket: headerId must be an integer");
}

let packet = new HPacket(headerId)
.appendInt(this.#id)
.appendString(this.#username)
.appendString(this.#figure)
.appendString(this.#motto)
.appendString(this.#creationDate)
.appendInt(this.#achievementScore)
.appendInt(this.#friendCount)
.appendBoolean(this.#isFriend)
.appendBoolean(this.#isRequestedFriend)
.appendBoolean(this.#isOnline)
.appendInt(this.#groups.length);

for(let group of this.#groups) {
group.appendToPacket(packet);
}

packet.appendInt(this.#lastAccessSince)
.appendBoolean(this.#openProfile);

return packet;
}

get id() {
return this.#id;
}
Expand Down Expand Up @@ -206,7 +234,7 @@ exports.HUserProfile = class HUserProfile {
throw new Error("HUserProfile.lastAccessSince: must be an integer");
}

this.lastAccessSince = val;
this.#lastAccessSince = val;
}

get openProfile() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gnode-api",
"version": "0.1.1",
"version": "0.1.3",
"description": "Node.js G-Earth extension API",
"main": "index.js",
"types": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { Extension, HDirection, HEntity, HUserProfile} = require('../index');
const extensionInfo = require('./package.json');

const ext = new Extension(extensionInfo);
ext.run();
//ext.run();

ext.interceptByNameOrHash(HDirection.TOCLIENT, 'Users', onUsers);

Expand Down

0 comments on commit 844cc6f

Please sign in to comment.