/
Number.sol
219 lines (180 loc) · 6.96 KB
/
Number.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
// 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";
library Number {
// Hex below is the result of `WorldResourceIdLib.encode({ namespace: "", name: "Number", typeId: RESOURCE_TABLE });`
ResourceId constant _tableId = ResourceId.wrap(0x746200000000000000000000000000004e756d62657200000000000000000000);
FieldLayout constant _fieldLayout =
FieldLayout.wrap(0x0004010004000000000000000000000000000000000000000000000000000000);
/**
* @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[](1);
_keySchema[0] = SchemaType.UINT32;
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[](1);
_valueSchema[0] = SchemaType.UINT32;
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[](1);
keyNames[0] = "key";
}
/**
* @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[](1);
fieldNames[0] = "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 value.
*/
function getValue(uint32 key) internal view returns (uint32 value) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (uint32(bytes4(_blob)));
}
/**
* @notice Get value.
*/
function _getValue(uint32 key) internal view returns (uint32 value) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (uint32(bytes4(_blob)));
}
/**
* @notice Get value.
*/
function get(uint32 key) internal view returns (uint32 value) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (uint32(bytes4(_blob)));
}
/**
* @notice Get value.
*/
function _get(uint32 key) internal view returns (uint32 value) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (uint32(bytes4(_blob)));
}
/**
* @notice Set value.
*/
function setValue(uint32 key, uint32 value) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout);
}
/**
* @notice Set value.
*/
function _setValue(uint32 key, uint32 value) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout);
}
/**
* @notice Set value.
*/
function set(uint32 key, uint32 value) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout);
}
/**
* @notice Set value.
*/
function _set(uint32 key, uint32 value) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout);
}
/**
* @notice 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);
}
/**
* @notice 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);
}
/**
* @notice Tightly pack static (fixed length) data using this table's schema.
* @return The static data, encoded into a sequence of bytes.
*/
function encodeStatic(uint32 value) internal pure returns (bytes memory) {
return abi.encodePacked(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(uint32 value) internal pure returns (bytes memory, PackedCounter, bytes memory) {
bytes memory _staticData = encodeStatic(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 key) internal pure returns (bytes32[] memory) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(key));
return _keyTuple;
}
}