/
Multi.sol
428 lines (362 loc) · 14 KB
/
Multi.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
/* 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";
// Hex below is the result of `WorldResourceIdLib.encode({ namespace: "", name: "Multi", typeId: RESOURCE_TABLE });`
ResourceId constant _tableId = ResourceId.wrap(0x746200000000000000000000000000004d756c74690000000000000000000000);
ResourceId constant MultiTableId = _tableId;
FieldLayout constant _fieldLayout = FieldLayout.wrap(
0x0021020020010000000000000000000000000000000000000000000000000000
);
struct MultiData {
int256 num;
bool value;
}
library Multi {
/**
* @notice Get the table values' field layout.
* @return _fieldLayout The field layout for the table.
*/
function getFieldLayout() internal pure returns (FieldLayout) {
return _fieldLayout;
}
/**
* @notice Get the table's key schema.
* @return _keySchema The key schema for the table.
*/
function getKeySchema() internal pure returns (Schema) {
SchemaType[] memory _keySchema = new SchemaType[](4);
_keySchema[0] = SchemaType.UINT32;
_keySchema[1] = SchemaType.BOOL;
_keySchema[2] = SchemaType.UINT256;
_keySchema[3] = SchemaType.INT120;
return SchemaLib.encode(_keySchema);
}
/**
* @notice Get the table's value schema.
* @return _valueSchema The value schema for the table.
*/
function getValueSchema() internal pure returns (Schema) {
SchemaType[] memory _valueSchema = new SchemaType[](2);
_valueSchema[0] = SchemaType.INT256;
_valueSchema[1] = SchemaType.BOOL;
return SchemaLib.encode(_valueSchema);
}
/**
* @notice Get the table's key field names.
* @return keyNames An array of strings with the names of key fields.
*/
function getKeyNames() internal pure returns (string[] memory keyNames) {
keyNames = new string[](4);
keyNames[0] = "a";
keyNames[1] = "b";
keyNames[2] = "c";
keyNames[3] = "d";
}
/**
* @notice Get the table's value field names.
* @return fieldNames An array of strings with the names of value fields.
*/
function getFieldNames() internal pure returns (string[] memory fieldNames) {
fieldNames = new string[](2);
fieldNames[0] = "num";
fieldNames[1] = "value";
}
/**
* @notice Register the table with its config.
*/
function register() internal {
StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames());
}
/**
* @notice Register the table with its config.
*/
function _register() internal {
StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames());
}
/**
* @notice Get num.
*/
function getNum(uint32 a, bool b, uint256 c, int120 d) internal view returns (int256 num) {
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (int256(uint256(bytes32(_blob))));
}
/**
* @notice Get num.
*/
function _getNum(uint32 a, bool b, uint256 c, int120 d) internal view returns (int256 num) {
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (int256(uint256(bytes32(_blob))));
}
/**
* @notice Set num.
*/
function setNum(uint32 a, bool b, uint256 c, int120 d, int256 num) internal {
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((num)), _fieldLayout);
}
/**
* @notice Set num.
*/
function _setNum(uint32 a, bool b, uint256 c, int120 d, int256 num) internal {
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((num)), _fieldLayout);
}
/**
* @notice Get value.
*/
function getValue(uint32 a, bool b, uint256 c, int120 d) internal view returns (bool value) {
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 1, _fieldLayout);
return (_toBool(uint8(bytes1(_blob))));
}
/**
* @notice Get value.
*/
function _getValue(uint32 a, bool b, uint256 c, int120 d) internal view returns (bool value) {
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout);
return (_toBool(uint8(bytes1(_blob))));
}
/**
* @notice Set value.
*/
function setValue(uint32 a, bool b, uint256 c, int120 d, bool value) internal {
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
StoreSwitch.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((value)), _fieldLayout);
}
/**
* @notice Set value.
*/
function _setValue(uint32 a, bool b, uint256 c, int120 d, bool value) internal {
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
StoreCore.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((value)), _fieldLayout);
}
/**
* @notice Get the full data.
*/
function get(uint32 a, bool b, uint256 c, int120 d) internal view returns (MultiData memory _table) {
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
(bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreSwitch.getRecord(
_tableId,
_keyTuple,
_fieldLayout
);
return decode(_staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Get the full data.
*/
function _get(uint32 a, bool b, uint256 c, int120 d) internal view returns (MultiData memory _table) {
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
(bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreCore.getRecord(
_tableId,
_keyTuple,
_fieldLayout
);
return decode(_staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Set the full data using individual values.
*/
function set(uint32 a, bool b, uint256 c, int120 d, int256 num, bool value) internal {
bytes memory _staticData = encodeStatic(num, value);
PackedCounter _encodedLengths;
bytes memory _dynamicData;
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Set the full data using individual values.
*/
function _set(uint32 a, bool b, uint256 c, int120 d, int256 num, bool value) internal {
bytes memory _staticData = encodeStatic(num, value);
PackedCounter _encodedLengths;
bytes memory _dynamicData;
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout);
}
/**
* @notice Set the full data using the data struct.
*/
function set(uint32 a, bool b, uint256 c, int120 d, MultiData memory _table) internal {
bytes memory _staticData = encodeStatic(_table.num, _table.value);
PackedCounter _encodedLengths;
bytes memory _dynamicData;
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Set the full data using the data struct.
*/
function _set(uint32 a, bool b, uint256 c, int120 d, MultiData memory _table) internal {
bytes memory _staticData = encodeStatic(_table.num, _table.value);
PackedCounter _encodedLengths;
bytes memory _dynamicData;
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout);
}
/**
* @notice Decode the tightly packed blob of static data using this table's field layout.
*/
function decodeStatic(bytes memory _blob) internal pure returns (int256 num, bool value) {
num = (int256(uint256(Bytes.slice32(_blob, 0))));
value = (_toBool(uint8(Bytes.slice1(_blob, 32))));
}
/**
* @notice Decode the tightly packed blobs using this table's field layout.
* @param _staticData Tightly packed static fields.
*
*
*/
function decode(
bytes memory _staticData,
PackedCounter,
bytes memory
) internal pure returns (MultiData memory _table) {
(_table.num, _table.value) = decodeStatic(_staticData);
}
/**
* @notice Delete all data for given keys.
*/
function deleteRecord(uint32 a, bool b, uint256 c, int120 d) internal {
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
StoreSwitch.deleteRecord(_tableId, _keyTuple);
}
/**
* @notice Delete all data for given keys.
*/
function _deleteRecord(uint32 a, bool b, uint256 c, int120 d) internal {
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout);
}
/**
* @notice Tightly pack static (fixed length) data using this table's schema.
* @return The static data, encoded into a sequence of bytes.
*/
function encodeStatic(int256 num, bool value) internal pure returns (bytes memory) {
return abi.encodePacked(num, value);
}
/**
* @notice Encode all of a record's fields.
* @return The static (fixed length) data, encoded into a sequence of bytes.
* @return The lengths of the dynamic fields (packed into a single bytes32 value).
* @return The dynamic (variable length) data, encoded into a sequence of bytes.
*/
function encode(int256 num, bool value) internal pure returns (bytes memory, PackedCounter, bytes memory) {
bytes memory _staticData = encodeStatic(num, value);
PackedCounter _encodedLengths;
bytes memory _dynamicData;
return (_staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Encode keys as a bytes32 array using this table's field layout.
*/
function encodeKeyTuple(uint32 a, bool b, uint256 c, int120 d) internal pure returns (bytes32[] memory) {
bytes32[] memory _keyTuple = new bytes32[](4);
_keyTuple[0] = bytes32(uint256(a));
_keyTuple[1] = _boolToBytes32(b);
_keyTuple[2] = bytes32(uint256(c));
_keyTuple[3] = bytes32(uint256(int256(d)));
return _keyTuple;
}
}
/**
* @notice Cast a value to a bool.
* @dev Boolean values are encoded as uint8 (1 = true, 0 = false), but Solidity doesn't allow casting between uint8 and bool.
* @param value The uint8 value to convert.
* @return result The boolean value.
*/
function _toBool(uint8 value) pure returns (bool result) {
assembly {
result := value
}
}
/**
* @notice Cast a bool to a bytes32.
* @dev The boolean value is casted to a bytes32 value with 0 or 1 at the least significant bit.
*/
function _boolToBytes32(bool value) pure returns (bytes32 result) {
assembly {
result := value
}
}