From 0b37f16000719cc18b887b6812d992ba10d2fec3 Mon Sep 17 00:00:00 2001 From: Carlos Villalobos Date: Mon, 1 Jul 2024 22:38:24 -0500 Subject: [PATCH 1/2] chore: add write method --- packages/api/src/index.ts | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts index d2e8c61..7885ca9 100644 --- a/packages/api/src/index.ts +++ b/packages/api/src/index.ts @@ -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'; @@ -82,6 +84,54 @@ export class FsuipcApi { ); } + public async write( + offsetList: { offset: string; value: string | number | bigint | ArrayBufferView }[], + terminateOnError = true + ): Promise { + 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; From 92defa1616425781e0bfb6854904fc8a82f766f9 Mon Sep 17 00:00:00 2001 From: Carlos Villalobos Date: Mon, 1 Jul 2024 22:44:27 -0500 Subject: [PATCH 2/2] chore: add changeset file --- .changeset/curvy-countries-rule.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/curvy-countries-rule.md diff --git a/.changeset/curvy-countries-rule.md b/.changeset/curvy-countries-rule.md new file mode 100644 index 0000000..60da29b --- /dev/null +++ b/.changeset/curvy-countries-rule.md @@ -0,0 +1,5 @@ +--- +'@fsuipc.js/api': patch +--- + +add write method