Skip to content

Commit 8e5bcb4

Browse files
committed
Add LogicGateReceiver component
1 parent 4a32e68 commit 8e5bcb4

7 files changed

+478
-0
lines changed

docs/LogicGateReceiverComponent.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Class: `LogicGateReceiverComponent`
2+
3+
- [Types](#types)
4+
- [`<ComponentProps>`](#componentprops)
5+
- [`<LogicGateReceiverComponentProps>`](#logicgatereceivercomponentprops)
6+
- [Constructors](#constructors)
7+
- [`new LogicGateReceiverComponent(props)`](#new-logicgatereceivercomponentprops)
8+
- [`LogicGateReceiverComponent.fromBinary(reader, version)`](#logicgatereceivercomponentfrombinaryreader-version)
9+
- [Properties](#properties)
10+
- [`hash`](#hash)
11+
- [`name`](#name)
12+
- [`version`](#version)
13+
- [`senders`](#senders)
14+
- [`operationType`](#operationtype)
15+
- [`isInversedOutputSaved`](#isinversedoutputsaved)
16+
- [Methods](#methods)
17+
- [`toBinary(version?)`](#tobinaryversion)
18+
- [`write(writer, version?)`](#writewriter-version)
19+
20+
## Types
21+
22+
### `<ComponentProps>`
23+
24+
See [`<ComponentProps>`](./Component.md#componentprops)
25+
26+
---
27+
28+
### `<LogicGateReceiverComponentProps>`
29+
30+
A portion of the configuration object to pass to the `LogicGateReceiverComponent` constructor.
31+
32+
```ts
33+
type LogicGateReceiverComponentPropsV2 = {
34+
senders?: number[] | undefined;
35+
operationType?: LogicOperator | undefined;
36+
isInversedOutputSaved?: boolean | undefined;
37+
};
38+
39+
type LogicGateReceiverComponentProps = LogicGateReceiverComponentPropsV2;
40+
```
41+
42+
#### Since v2
43+
44+
- `senders` (optional, default `[]`) `<Array<number>>`
45+
- `operationType` (optional, default `0`) [`<LogicOperator>`](../src/types/LogicOperator.ts)
46+
- `isInversedOutputSaved` (optional, default `false`) `<boolean>`
47+
48+
## Constructors
49+
50+
### `new LogicGateReceiverComponent(props)`
51+
52+
Creates a versioned `LogicGateReceiver` component.
53+
54+
- `props` [`<ComponentProps & LogicGateReceiverComponentProps>`](#types) Configuration of the component to create.
55+
- Returns: `<LogicGateReceiverComponent>`
56+
57+
```ts
58+
import { LogicGateReceiverComponent } from 'att-string-transcoder';
59+
60+
const componentVersion = 2;
61+
const component = new LogicGateReceiverComponent({ version: componentVersion });
62+
```
63+
64+
---
65+
66+
### `LogicGateReceiverComponent.fromBinary(reader, version)`
67+
68+
See [`Component.fromBinary(reader, version)`](./Component.md#componentfrombinaryreader-version)
69+
70+
## Properties
71+
72+
Note that the following properties are sorted in order of appearance when decoding component binary data.
73+
74+
### `hash`
75+
76+
See [`Component.hash`](./Component.md#hash)
77+
78+
---
79+
80+
### `name`
81+
82+
See [`Component.name`](./Component.md#name)
83+
84+
---
85+
86+
### `version`
87+
88+
See [`Component.version`](./Component.md#version)
89+
90+
---
91+
92+
### `senders`
93+
94+
The identifiers of the `Logic*Sender` components that are connected to this `LogicGateReceiver`.
95+
96+
- Since: `v2`
97+
- `<Array<number>>`
98+
99+
---
100+
101+
### `operationType`
102+
103+
The type of operation to perform on the received inputs.
104+
105+
- Since: `v2`
106+
- [`<LogicOperator>`](../src/types/LogicOperator.ts)
107+
108+
```ts
109+
import { LogicGateReceiverComponent } from 'att-string-transcoder';
110+
111+
const componentVersion = 2;
112+
const component = new LogicGateReceiverComponent({ version: componentVersion });
113+
114+
const operationType = component.operationType;
115+
// `operationType` is `0` (AND)
116+
```
117+
118+
---
119+
120+
### `isInversedOutputSaved`
121+
122+
Whether to inverse the output of this gate.
123+
124+
- Since: `v2`
125+
- `<boolean>`
126+
127+
```ts
128+
import { LogicGateReceiverComponent } from 'att-string-transcoder';
129+
130+
const componentVersion = 2;
131+
const component = new LogicGateReceiverComponent({ version: componentVersion });
132+
133+
const isInversedOutputSaved = component.isInversedOutputSaved;
134+
// `isInversedOutputSaved` is `false`
135+
```
136+
137+
## Methods
138+
139+
### `toBinary(version?)`
140+
141+
See [`Component.toBinary(version?)`](./Component.md#tobinaryversion)
142+
143+
---
144+
145+
### `write(writer, version?)`
146+
147+
See [`Component.write(writer, version?)`](./Component.md#writewriter-version)

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- [`LiquidContainerComponent`](./LiquidContainerComponent.md)
2121
- [`LogicBoolReceiverComponent`](./LogicBoolReceiverComponent.md)
2222
- [`LogicFloatReceiverComponent`](./LogicFloatReceiverComponent.md)
23+
- [`LogicGateReceiverComponent`](./LogicGateReceiverComponent.md)
2324
- [`LogicIntReceiverComponent`](./LogicIntReceiverComponent.md)
2425
- [`NetworkRigidbodyComponent`](./NetworkRigidbodyComponent.md)
2526
- [`PhysicalMaterialPartComponent`](./PhysicalMaterialPartComponent.md)
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import { BinaryData } from '../BinaryData.js';
2+
import { BinaryReader } from '../BinaryReader.js';
3+
import { BinaryWriter } from '../BinaryWriter.js';
4+
import { Prefab } from '../Prefab.js';
5+
import { ComponentHash } from '../types/ComponentHash.js';
6+
import { LogicOperator } from '../types/LogicOperator.js';
7+
8+
import { LogicGateReceiverComponent } from './LogicGateReceiverComponent.js';
9+
10+
const componentHash = ComponentHash.LogicGateReceiver;
11+
const componentName = 'LogicGateReceiver';
12+
const componentVersion = 2;
13+
const componentProps = {
14+
senders: [69, 420, 1337],
15+
operationType: LogicOperator.Xor,
16+
isInversedOutputSaved: true
17+
};
18+
19+
describe('new LogicGateReceiverComponent()', () => {
20+
describe('when given the required props', () => {
21+
it('returns an instance of the LogicGateReceiverComponent class', () => {
22+
const component = new LogicGateReceiverComponent({ version: componentVersion });
23+
24+
expect(component).toBeInstanceOf(LogicGateReceiverComponent);
25+
expect(component.hash).toStrictEqual(componentHash);
26+
expect(component.name).toStrictEqual(componentName);
27+
expect(component.version).toStrictEqual(componentVersion);
28+
});
29+
});
30+
31+
describe('when given additional props', () => {
32+
it('returns an instance of the LogicGateReceiverComponent class', () => {
33+
const component = new LogicGateReceiverComponent({ version: componentVersion, ...componentProps });
34+
35+
expect(component).toBeInstanceOf(LogicGateReceiverComponent);
36+
expect(component.hash).toStrictEqual(componentHash);
37+
expect(component.name).toStrictEqual(componentName);
38+
expect(component.version).toStrictEqual(componentVersion);
39+
expect(component.senders).toStrictEqual(componentProps.senders);
40+
expect(component.operationType).toStrictEqual(componentProps.operationType);
41+
expect(component.isInversedOutputSaved).toStrictEqual(componentProps.isInversedOutputSaved);
42+
});
43+
});
44+
});
45+
46+
describe('LogicGateReceiverComponent.fromBinary()', () => {
47+
let fastForwardedReader: BinaryReader;
48+
49+
const prefab = new Prefab('MRK_Small_Lever', {
50+
components: {
51+
LogicGateReceiver: new LogicGateReceiverComponent({ version: componentVersion, ...componentProps })
52+
}
53+
});
54+
55+
beforeEach(() => {
56+
const saveString = prefab.toSaveString();
57+
const [dataString] = saveString.split('|');
58+
59+
if (typeof dataString === 'undefined') {
60+
throw new Error('Invalid test data.');
61+
}
62+
63+
const unsignedIntegers = dataString.split(',').map(Number);
64+
65+
const data = BinaryData.fromUnsignedIntegerArray(unsignedIntegers);
66+
const reader = new BinaryReader(data.toBinaryString());
67+
68+
reader.readUnsignedInteger(); // Root prefab hash.
69+
reader.readUnsignedInteger(); // SaveString bytes.
70+
reader.readUnsignedInteger(); // Parent prefab hash.
71+
reader.readFloat(); // Parent position X.
72+
reader.readFloat(); // Parent position Y.
73+
reader.readFloat(); // Parent position Z.
74+
reader.readFloat(); // Parent rotation X.
75+
reader.readFloat(); // Parent rotation Y.
76+
reader.readFloat(); // Parent rotation Z.
77+
reader.readFloat(); // Parent rotation W.
78+
reader.readFloat(); // Parent scale.
79+
reader.readUnsignedInteger(); // LogicGateReceiverComponent hash.
80+
reader.readUnsignedInteger(); // LogicGateReceiverComponent data length.
81+
82+
fastForwardedReader = reader;
83+
});
84+
85+
it('returns an instance of the LogicGateReceiverComponent class', () => {
86+
const component = LogicGateReceiverComponent.fromBinary(fastForwardedReader, componentVersion);
87+
88+
expect(component).toBeInstanceOf(LogicGateReceiverComponent);
89+
expect(component.hash).toStrictEqual(componentHash);
90+
expect(component.name).toStrictEqual(componentName);
91+
expect(component.version).toStrictEqual(componentVersion);
92+
expect(component.senders).toStrictEqual(componentProps.senders);
93+
expect(component.operationType).toStrictEqual(componentProps.operationType);
94+
expect(component.isInversedOutputSaved).toStrictEqual(componentProps.isInversedOutputSaved);
95+
});
96+
});
97+
98+
describe('LogicGateReceiverComponent.toBinary()', () => {
99+
it('returns a BinaryString representation of the component', () => {
100+
const component = new LogicGateReceiverComponent({ version: componentVersion, ...componentProps });
101+
102+
const data = component.toBinary();
103+
104+
const expectedData =
105+
'00000000000000000000000000000011000000000000000000000000010001010000000000000000000000011010010000000000000000000000010100111001100000000000000000000000000000101';
106+
107+
expect(data).toStrictEqual(expectedData);
108+
});
109+
});
110+
111+
describe('LogicGateReceiverComponent.write()', () => {
112+
it('writes a BinaryString representation of the component to the given BinaryWriter, including component hash and data length', () => {
113+
const component = new LogicGateReceiverComponent({ version: componentVersion, ...componentProps });
114+
115+
const writer = new BinaryWriter();
116+
component.write(writer);
117+
118+
const data = writer.flush();
119+
120+
const expectedData =
121+
'010100101111001111111000000011100000000000000000000000001010000100000000000000000000000000000011000000000000000000000000010001010000000000000000000000011010010000000000000000000000010100111001100000000000000000000000000000101';
122+
123+
expect(data).toStrictEqual(expectedData);
124+
125+
const reader = new BinaryReader(data);
126+
127+
const hash = reader.readUnsignedInteger();
128+
reader.readUnsignedInteger(); // LogicGateReceiverComponent data length.
129+
reader.readUnsignedInteger(); // Senders array length.
130+
const sender1 = reader.readUnsignedInteger();
131+
const sender2 = reader.readUnsignedInteger();
132+
const sender3 = reader.readUnsignedInteger();
133+
const operationType = reader.readSignedInteger();
134+
const isInversedOutputSaved = reader.readBoolean();
135+
136+
expect(hash).toStrictEqual(componentHash);
137+
expect(sender1).toStrictEqual(componentProps.senders[0]);
138+
expect(sender2).toStrictEqual(componentProps.senders[1]);
139+
expect(sender3).toStrictEqual(componentProps.senders[2]);
140+
expect(operationType).toStrictEqual(componentProps.operationType);
141+
expect(isInversedOutputSaved).toStrictEqual(componentProps.isInversedOutputSaved);
142+
});
143+
});

0 commit comments

Comments
 (0)