generated from MinecraftJS/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ add support for custom methods
- Loading branch information
1 parent
22015ed
commit 293225f
Showing
2 changed files
with
82 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const BufWrapper = require('../dist').default; | ||
const { assert } = require('chai'); | ||
|
||
const CStringPlugin = { | ||
/** | ||
* @param {BufWrapper} buf | ||
* @param {String} string | ||
*/ | ||
writeCString(buf, string) { | ||
const buffer = Buffer.alloc(Buffer.byteLength(string) + 1); | ||
buffer.write(string); | ||
buf.writeToBuffer(buffer); | ||
}, | ||
|
||
/** | ||
* @param {BufWrapper} buf | ||
*/ | ||
readCString(buf) { | ||
let cursor = buf.offset; | ||
let length = 0; | ||
while (buf.buffer[cursor] !== 0x00) { | ||
length++; | ||
cursor++; | ||
} | ||
const bytes = buf.buffer.slice(buf.offset, length); | ||
buf.offset += length + 1; | ||
return bytes.toString(); | ||
}, | ||
}; | ||
|
||
describe('Plugins', () => { | ||
it('CString Write', () => { | ||
const buf = new BufWrapper(null, { | ||
plugins: { CStringPlugin }, | ||
}); | ||
|
||
buf.plugins.CStringPlugin.writeCString(buf, 'Hello World!'); | ||
|
||
assert.equal(buf.buffer.toString('hex'), '48656c6c6f20576f726c642100'); | ||
}); | ||
|
||
it('CString Read', () => { | ||
const buf = new BufWrapper( | ||
Buffer.from('48656c6c6f20576f726c642100', 'hex'), | ||
{ | ||
plugins: { CStringPlugin }, | ||
} | ||
); | ||
|
||
assert.equal(buf.plugins.CStringPlugin.readCString(buf), 'Hello World!'); | ||
}); | ||
}); |