/
Vector.sol
351 lines (275 loc) · 11.8 KB
/
Vector.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
/* Autogenerated file. Do not edit manually. */
// Import schema type
import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol";
// Import store internals
import { IStore } from "@latticexyz/store/src/IStore.sol";
import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol";
import { StoreCore } from "@latticexyz/store/src/StoreCore.sol";
import { Bytes } from "@latticexyz/store/src/Bytes.sol";
import { Memory } from "@latticexyz/store/src/Memory.sol";
import { SliceLib } from "@latticexyz/store/src/Slice.sol";
import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol";
import { FieldLayout, FieldLayoutLib } from "@latticexyz/store/src/FieldLayout.sol";
import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol";
import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol";
import { ResourceId } from "@latticexyz/store/src/ResourceId.sol";
import { RESOURCE_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol";
ResourceId constant _tableId = ResourceId.wrap(
bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14(""), bytes16("Vector")))
);
ResourceId constant VectorTableId = _tableId;
FieldLayout constant _fieldLayout = FieldLayout.wrap(
0x0008020004040000000000000000000000000000000000000000000000000000
);
struct VectorData {
int32 x;
int32 y;
}
library Vector {
/** Get the table values' field layout */
function getFieldLayout() internal pure returns (FieldLayout) {
return _fieldLayout;
}
/** Get the table's key schema */
function getKeySchema() internal pure returns (Schema) {
SchemaType[] memory _keySchema = new SchemaType[](1);
_keySchema[0] = SchemaType.UINT32;
return SchemaLib.encode(_keySchema);
}
/** Get the table's value schema */
function getValueSchema() internal pure returns (Schema) {
SchemaType[] memory _valueSchema = new SchemaType[](2);
_valueSchema[0] = SchemaType.INT32;
_valueSchema[1] = SchemaType.INT32;
return SchemaLib.encode(_valueSchema);
}
/** Get the table's key names */
function getKeyNames() internal pure returns (string[] memory keyNames) {
keyNames = new string[](1);
keyNames[0] = "key";
}
/** Get the table's field names */
function getFieldNames() internal pure returns (string[] memory fieldNames) {
fieldNames = new string[](2);
fieldNames[0] = "x";
fieldNames[1] = "y";
}
/** Register the table with its config */
function register() internal {
StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames());
}
/** Register the table with its config */
function _register() internal {
StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames());
}
/** Register the table with its config (using the specified store) */
function register(IStore _store) internal {
_store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames());
}
/** Get x */
function getX(uint32 key) internal view returns (int32 x) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (int32(uint32(bytes4(_blob))));
}
/** Get x */
function _getX(uint32 key) internal view returns (int32 x) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (int32(uint32(bytes4(_blob))));
}
/** Get x (using the specified store) */
function getX(IStore _store, uint32 key) internal view returns (int32 x) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (int32(uint32(bytes4(_blob))));
}
/** Set x */
function setX(uint32 key, int32 x) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout);
}
/** Set x */
function _setX(uint32 key, int32 x) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout);
}
/** Set x (using the specified store) */
function setX(IStore _store, uint32 key, int32 x) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
_store.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout);
}
/** Get y */
function getY(uint32 key) internal view returns (int32 y) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 1, _fieldLayout);
return (int32(uint32(bytes4(_blob))));
}
/** Get y */
function _getY(uint32 key) internal view returns (int32 y) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout);
return (int32(uint32(bytes4(_blob))));
}
/** Get y (using the specified store) */
function getY(IStore _store, uint32 key) internal view returns (int32 y) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 1, _fieldLayout);
return (int32(uint32(bytes4(_blob))));
}
/** Set y */
function setY(uint32 key, int32 y) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout);
}
/** Set y */
function _setY(uint32 key, int32 y) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
StoreCore.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout);
}
/** Set y (using the specified store) */
function setY(IStore _store, uint32 key, int32 y) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
_store.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout);
}
/** Get the full data */
function get(uint32 key) internal view returns (VectorData memory _table) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
(bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreSwitch.getRecord(
_tableId,
_keyTuple,
_fieldLayout
);
return decode(_staticData, _encodedLengths, _dynamicData);
}
/** Get the full data */
function _get(uint32 key) internal view returns (VectorData memory _table) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
(bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreCore.getRecord(
_tableId,
_keyTuple,
_fieldLayout
);
return decode(_staticData, _encodedLengths, _dynamicData);
}
/** Get the full data (using the specified store) */
function get(IStore _store, uint32 key) internal view returns (VectorData memory _table) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
(bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = _store.getRecord(
_tableId,
_keyTuple,
_fieldLayout
);
return decode(_staticData, _encodedLengths, _dynamicData);
}
/** Set the full data using individual values */
function set(uint32 key, int32 x, int32 y) internal {
bytes memory _staticData = encodeStatic(x, y);
PackedCounter _encodedLengths;
bytes memory _dynamicData;
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout);
}
/** Set the full data using individual values */
function _set(uint32 key, int32 x, int32 y) internal {
bytes memory _staticData = encodeStatic(x, y);
PackedCounter _encodedLengths;
bytes memory _dynamicData;
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout);
}
/** Set the full data using individual values (using the specified store) */
function set(IStore _store, uint32 key, int32 x, int32 y) internal {
bytes memory _staticData = encodeStatic(x, y);
PackedCounter _encodedLengths;
bytes memory _dynamicData;
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
_store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout);
}
/** Set the full data using the data struct */
function set(uint32 key, VectorData memory _table) internal {
set(key, _table.x, _table.y);
}
/** Set the full data using the data struct */
function _set(uint32 key, VectorData memory _table) internal {
set(key, _table.x, _table.y);
}
/** Set the full data using the data struct (using the specified store) */
function set(IStore _store, uint32 key, VectorData memory _table) internal {
set(_store, key, _table.x, _table.y);
}
/**
* Decode the tightly packed blob of static data using this table's field layout
* Undefined behaviour for invalid blobs
*/
function decodeStatic(bytes memory _blob) internal pure returns (int32 x, int32 y) {
x = (int32(uint32(Bytes.slice4(_blob, 0))));
y = (int32(uint32(Bytes.slice4(_blob, 4))));
}
/**
* Decode the tightly packed blob using this table's field layout.
* Undefined behaviour for invalid blobs.
*/
function decode(
bytes memory _staticData,
PackedCounter,
bytes memory
) internal pure returns (VectorData memory _table) {
(_table.x, _table.y) = decodeStatic(_staticData);
}
/** Tightly pack static data using this table's schema */
function encodeStatic(int32 x, int32 y) internal pure returns (bytes memory) {
return abi.encodePacked(x, y);
}
/** Tightly pack full data using this table's field layout */
function encode(int32 x, int32 y) internal pure returns (bytes memory, PackedCounter, bytes memory) {
bytes memory _staticData = encodeStatic(x, y);
PackedCounter _encodedLengths;
bytes memory _dynamicData;
return (_staticData, _encodedLengths, _dynamicData);
}
/** Encode keys as a bytes32 array using this table's field layout */
function encodeKeyTuple(uint32 key) internal pure returns (bytes32[] memory) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
return _keyTuple;
}
/* Delete all data for given keys */
function deleteRecord(uint32 key) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout);
}
/* Delete all data for given keys */
function _deleteRecord(uint32 key) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout);
}
/* Delete all data for given keys (using the specified store) */
function deleteRecord(IStore _store, uint32 key) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
_store.deleteRecord(_tableId, _keyTuple, _fieldLayout);
}
}