Skip to content

Commit

Permalink
Merge pull request #27 from tcavenezuela/feat/write-method
Browse files Browse the repository at this point in the history
Feat/write method
  • Loading branch information
carl0shd authored Jul 2, 2024
2 parents b92b23a + 92defa1 commit 2357b26
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-countries-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fsuipc.js/api': patch
---

add write method
50 changes: 50 additions & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */

import { FSUIPC, Simulator, Type, FSUIPCError } from 'fsuipc.js';
import { timer, from, Observable, throwError, of } from 'rxjs';
import { switchMap, catchError, map } from 'rxjs/operators';
Expand Down Expand Up @@ -89,6 +91,54 @@ export class FsuipcApi {
);
}

public async write(
offsetList: { offset: string; value: string | number | bigint | ArrayBufferView }[],
terminateOnError = true
): Promise<void> {
if (!this.fsuipc) {
throw new Error('NO_FSUIPC_INSTANCE');
}

for (const offsetData of offsetList) {
const offset: Offset = OFFSETS[offsetData.offset];

if (offset.permission === 'r') {
throw new Error(`${offset.name} offset does not allow writing.`);
}

if (offset.type === Type.ByteArray || offset.type === Type.String) {
const offsetType = offset.type as Type.ByteArray | Type.String;
// @ts-ignore
this.fsuipc.write(offset.value, offsetType, offset.length, offsetData.value);
} else {
const offsetType = offset.type as
| Type.Byte
| Type.SByte
| Type.Int16
| Type.Int32
| Type.Int64
| Type.UInt64
| Type.UInt16
| Type.UInt32
| Type.Double
| Type.Single;

// @ts-ignore
this.fsuipc.write(offset.value, offsetType, offsetData.value);
}
}

try {
await this.fsuipc.process();
} catch (error) {
if (terminateOnError) {
this.fsuipc.close();
}

throw new Error(JSON.stringify(error));
}
}

private watchOffsets(offsetList: string[]): void {
if (this.shouldUpdateCache(offsetList)) {
this.watchedOffsetCache = offsetList;
Expand Down

0 comments on commit 2357b26

Please sign in to comment.