Skip to content

Commit

Permalink
fix(is_mso): adjust TextStart property value according to the decoded…
Browse files Browse the repository at this point in the history
… string length of player name

fix #43
  • Loading branch information
mkapal committed Jul 16, 2024
1 parent e7d3207 commit 533d107
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/packets/IS_MSO.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ describe('IS_MSO', () => {
});

describe('non-ASCII characters in message', () => {
const size = 32;
const size = 36;

const data: PacketTestData<IS_MSO> = {
ReqI: 0,
Zero: 0,
UCID: 2,
PLID: 4,
UserType: UserType.MSO_USER,
TextStart: 15,
Msg: '^7Player ^7: ^8cršč',
TextStart: 15, // After converting Msg from bytes into UTF-8
Msg: '^7Player ě ^7: ^8cršč',
};

const buffer = new Uint8Array([
Expand All @@ -61,7 +61,7 @@ describe('IS_MSO', () => {
2, // UCID
4, // PLID
1, // UserType
15, // TextStart
17, // TextStart
94, // Msg
55,
80,
Expand All @@ -72,6 +72,10 @@ describe('IS_MSO', () => {
114,
32,
94,
69,
236,
32,
94,
55,
58,
32,
Expand Down
17 changes: 17 additions & 0 deletions src/packets/IS_MSO.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import isArray from 'lodash/isArray';

import { byte, getFormat, stringNull } from '../decorators';
import { InSimError } from '../errors';
import { unpack } from '../lfspack';
Expand Down Expand Up @@ -47,6 +49,21 @@ export class IS_MSO extends Packet {
Msg: `${msgLength}s`,
});

const playerNameUnpacked = unpack(
`<${this.TextStart}s`,
buffer.buffer,
IS_MSO.FIXED_DATA_SIZE,
);

if (
playerNameUnpacked !== null &&
isArray(playerNameUnpacked[0]) &&
playerNameUnpacked[0].length === 2
) {
const [, playerName] = playerNameUnpacked[0];
this.TextStart = playerName.length;
}

return this;
}
}

0 comments on commit 533d107

Please sign in to comment.