@latticexyz/cli 2.0.0-alpha.94 → 2.0.0-main-0d434816
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-LFIOIY67.js +30 -0
- package/dist/chunk-LFIOIY67.js.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/mud.js +15 -8340
- package/dist/mud.js.map +1 -0
- package/package.json +45 -43
- package/src/commands/deploy.ts +49 -0
- package/src/commands/dev-contracts.ts +176 -0
- package/src/commands/devnode.ts +7 -2
- package/src/commands/faucet.ts +1 -1
- package/src/commands/index.ts +18 -13
- package/src/commands/set-version.ts +38 -10
- package/src/commands/tablegen.ts +5 -4
- package/src/commands/{test-v2.ts → test.ts} +5 -6
- package/src/commands/trace.ts +108 -0
- package/src/commands/tsgen.ts +4 -3
- package/src/commands/worldgen.ts +29 -28
- package/src/index.ts +2 -1
- package/src/modules.d.ts +11 -0
- package/src/mud.ts +10 -4
- package/src/render-ts/recsV1TableOptions.ts +4 -4
- package/src/render-ts/renderRecsV1Tables.ts +3 -3
- package/src/render-ts/schemaTypesToRecsTypeStrings.ts +33 -33
- package/src/render-ts/tsgen.ts +4 -4
- package/src/utils/{deploy-v2.ts → deploy.ts} +207 -123
- package/src/utils/deployHandler.ts +87 -0
- package/src/utils/errors.ts +1 -1
- package/src/utils/getExistingContracts.ts +12 -0
- package/src/utils/index.ts +7 -5
- package/src/utils/printMUD.ts +14 -0
- package/src/utils/worldtypes.ts +21 -0
- package/dist/chunk-6V563IAZ.js +0 -283
- package/dist/chunk-FPG73MVN.js +0 -71
- package/dist/chunk-L4YLJHLJ.js +0 -30
- package/dist/chunk-SKNB74MT.js +0 -726
- package/dist/chunk-T2YHSEJD.js +0 -3842
- package/dist/chunk-VQTZJIFF.js +0 -353
- package/dist/chunk-WZFXLDPK.js +0 -761
- package/dist/chunk-YL4GJLLL.js +0 -26139
- package/dist/index.d.ts +0 -2
- package/dist/mud.d.ts +0 -1
- package/dist/mud2.d.ts +0 -1
- package/dist/mud2.js +0 -25
- package/dist/render-solidity/index.d.ts +0 -171
- package/dist/render-solidity/index.js +0 -49
- package/dist/render-ts/index.d.ts +0 -26
- package/dist/render-ts/index.js +0 -14
- package/dist/utils/deprecated/index.d.ts +0 -76
- package/dist/utils/deprecated/index.js +0 -36
- package/dist/utils/index.d.ts +0 -88
- package/dist/utils/index.js +0 -39
- package/src/commands/deploy-v2.ts +0 -112
- package/src/commands/deprecated/bulkupload.ts +0 -50
- package/src/commands/deprecated/call-system.ts +0 -70
- package/src/commands/deprecated/codegen-libdeploy.ts +0 -29
- package/src/commands/deprecated/deploy-contracts.ts +0 -120
- package/src/commands/deprecated/index.ts +0 -22
- package/src/commands/deprecated/system-types.ts +0 -29
- package/src/commands/deprecated/test.ts +0 -53
- package/src/commands/deprecated/trace.ts +0 -81
- package/src/commands/deprecated/types.ts +0 -33
- package/src/commands/gas-report.ts +0 -219
- package/src/contracts/BulkUpload.sol +0 -174
- package/src/contracts/Deploy.sol +0 -23
- package/src/contracts/LibDeploy.ejs +0 -121
- package/src/contracts/LibDeploy.sol +0 -23
- package/src/contracts/LibDeployStub.sol +0 -23
- package/src/mud2.ts +0 -29
- package/src/render-solidity/common.ts +0 -138
- package/src/render-solidity/field.ts +0 -160
- package/src/render-solidity/index.ts +0 -10
- package/src/render-solidity/record.ts +0 -158
- package/src/render-solidity/renderSystemInterface.ts +0 -31
- package/src/render-solidity/renderTable.ts +0 -164
- package/src/render-solidity/renderTableIndex.ts +0 -15
- package/src/render-solidity/renderTypeHelpers.ts +0 -99
- package/src/render-solidity/renderTypes.ts +0 -19
- package/src/render-solidity/renderTypesFromConfig.ts +0 -13
- package/src/render-solidity/renderWorld.ts +0 -24
- package/src/render-solidity/tableOptions.ts +0 -107
- package/src/render-solidity/tablegen.ts +0 -35
- package/src/render-solidity/types.ts +0 -111
- package/src/render-solidity/userType.ts +0 -131
- package/src/render-solidity/worldgen.ts +0 -59
- package/src/utils/contractToInterface.ts +0 -132
- package/src/utils/deprecated/build.ts +0 -55
- package/src/utils/deprecated/codegen.ts +0 -42
- package/src/utils/deprecated/constants.ts +0 -2
- package/src/utils/deprecated/deploy.ts +0 -111
- package/src/utils/deprecated/exec.ts +0 -18
- package/src/utils/deprecated/findLog.ts +0 -8
- package/src/utils/deprecated/hsr.ts +0 -71
- package/src/utils/deprecated/ids.ts +0 -14
- package/src/utils/deprecated/index.ts +0 -8
- package/src/utils/deprecated/typegen.ts +0 -133
- package/src/utils/execLog.ts +0 -22
- package/src/utils/format.ts +0 -39
- package/src/utils/formatAndWrite.ts +0 -21
- package/src/utils/foundry.ts +0 -103
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
import type { CommandModule } from "yargs";
|
|
2
|
-
import { readFileSync, writeFileSync, rmSync } from "fs";
|
|
3
|
-
import { execa } from "execa";
|
|
4
|
-
import chalk from "chalk";
|
|
5
|
-
import { table, getBorderCharacters } from "table";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Print the gas report to the console, save it to a file and compare it to a previous gas report if provided.
|
|
9
|
-
* Requires forge to be installed, and gas test files including `// !gasreport` comments, like this:
|
|
10
|
-
*
|
|
11
|
-
* ```solidity
|
|
12
|
-
* contract GasTest is DSTestPlus {
|
|
13
|
-
* function testBuffer() public pure {
|
|
14
|
-
* // !gasreport allocate a buffer
|
|
15
|
-
* Buffer buffer = Buffer_.allocate(32);
|
|
16
|
-
*
|
|
17
|
-
* bytes32 value = keccak256("some data");
|
|
18
|
-
*
|
|
19
|
-
* // !gasreport append 32 bytes to a buffer
|
|
20
|
-
* buffer.append(value);
|
|
21
|
-
* }
|
|
22
|
-
* }
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
type Options = {
|
|
27
|
-
path: string[];
|
|
28
|
-
save?: string;
|
|
29
|
-
compare?: string;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
type GasReportEntry = {
|
|
33
|
-
source: string;
|
|
34
|
-
name: string;
|
|
35
|
-
functionCall: string;
|
|
36
|
-
gasUsed: number;
|
|
37
|
-
prevGasUsed?: number;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
type GasReport = GasReportEntry[];
|
|
41
|
-
|
|
42
|
-
const tempFileSuffix = "MudGasReport";
|
|
43
|
-
|
|
44
|
-
const commandModule: CommandModule<Options, Options> = {
|
|
45
|
-
command: "gas-report",
|
|
46
|
-
|
|
47
|
-
describe: "Create a gas report",
|
|
48
|
-
|
|
49
|
-
builder(yargs) {
|
|
50
|
-
return yargs.options({
|
|
51
|
-
path: { type: "array", string: true, default: ["Gas.t.sol"], desc: "File containing the gas tests" },
|
|
52
|
-
save: { type: "string", desc: "Save the gas report to a file" },
|
|
53
|
-
compare: { type: "string", desc: "Compare to an existing gas report" },
|
|
54
|
-
});
|
|
55
|
-
},
|
|
56
|
-
|
|
57
|
-
async handler({ path: files, save, compare }) {
|
|
58
|
-
const validFiles = files.filter((file) => file.endsWith(".t.sol"));
|
|
59
|
-
const tempFiles = await Promise.all(validFiles.map((file) => createGasReport(file)));
|
|
60
|
-
|
|
61
|
-
process.once("SIGINT", () => {
|
|
62
|
-
console.log("caught sigint, deleting temp files");
|
|
63
|
-
tempFiles.forEach((file) => rmSync(file));
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
let gasReport: GasReport;
|
|
67
|
-
try {
|
|
68
|
-
gasReport = await runGasReport();
|
|
69
|
-
} catch {
|
|
70
|
-
setTimeout(() => process.exit());
|
|
71
|
-
return;
|
|
72
|
-
} finally {
|
|
73
|
-
// Delete the temporary files
|
|
74
|
-
tempFiles.forEach((file) => rmSync(file));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// If this gas report should be compared to an existing one, load the existing one
|
|
78
|
-
if (compare) {
|
|
79
|
-
try {
|
|
80
|
-
const compareGasReport: GasReport = JSON.parse(readFileSync(compare, "utf8"));
|
|
81
|
-
// Merge the previous gas report with the new one
|
|
82
|
-
gasReport = gasReport.map((entry) => {
|
|
83
|
-
const prevEntry = compareGasReport.find(
|
|
84
|
-
(e) => e.name === entry.name && e.functionCall === entry.functionCall
|
|
85
|
-
);
|
|
86
|
-
return { ...entry, prevGasUsed: prevEntry?.gasUsed };
|
|
87
|
-
});
|
|
88
|
-
} catch {
|
|
89
|
-
console.log(chalk.red(`Gas report to compare not found: ${compare}`));
|
|
90
|
-
compare = undefined;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Print gas report
|
|
95
|
-
printGasReport(gasReport, compare);
|
|
96
|
-
|
|
97
|
-
// Save gas report to file if requested
|
|
98
|
-
if (save) saveGasReport(gasReport, save);
|
|
99
|
-
|
|
100
|
-
process.exit(0);
|
|
101
|
-
},
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
export default commandModule;
|
|
105
|
-
|
|
106
|
-
async function createGasReport(filename: string): Promise<string> {
|
|
107
|
-
console.log("Creating gas report for", chalk.bold(filename));
|
|
108
|
-
|
|
109
|
-
// Parse the given test file, and add gas reporting wherever requested by a `// !gasreport` comment
|
|
110
|
-
const fileContents = readFileSync(filename, "utf8");
|
|
111
|
-
let newFile = fileContents;
|
|
112
|
-
|
|
113
|
-
// Use a regex to find first line of each function
|
|
114
|
-
const functionRegex = new RegExp(/function (.*){/g);
|
|
115
|
-
// Insert a line to declare the _gasreport variable at the start of each function
|
|
116
|
-
let functionMatch;
|
|
117
|
-
while ((functionMatch = functionRegex.exec(fileContents)) !== null) {
|
|
118
|
-
const functionSignature = functionMatch[0];
|
|
119
|
-
newFile = newFile.replace(functionSignature, `${functionSignature}\nuint256 _gasreport;`);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// A gasreport comment has a name (written after the comment) and a function call (written on the next line)
|
|
123
|
-
// Create a regex to extract both the name and the function call
|
|
124
|
-
const regex = new RegExp(/\/\/ !gasreport (.*)\n(.*)/g);
|
|
125
|
-
|
|
126
|
-
// Apply the regex and loop through the matches,
|
|
127
|
-
// and create a new file with the gasreport comments replaced by the gas report
|
|
128
|
-
let match;
|
|
129
|
-
while ((match = regex.exec(fileContents)) !== null) {
|
|
130
|
-
const name = match[1];
|
|
131
|
-
const functionCall = match[2].trim();
|
|
132
|
-
|
|
133
|
-
newFile = newFile.replace(
|
|
134
|
-
match[0],
|
|
135
|
-
`
|
|
136
|
-
_gasreport = gasleft();
|
|
137
|
-
${functionCall}
|
|
138
|
-
_gasreport = _gasreport - gasleft();
|
|
139
|
-
console.log("GAS REPORT(${filename}): ${name} [${functionCall.replaceAll('"', '\\"')}]:", _gasreport);`
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Remove all occurrences of `pure` with `view`
|
|
144
|
-
newFile = newFile.replace(/pure/g, "view");
|
|
145
|
-
|
|
146
|
-
// Write the new file to disk (temporarily)
|
|
147
|
-
// Create the temp file by replacing the previous file name with MudGasReport
|
|
148
|
-
const tempFileName = filename.replace(/\.t\.sol$/, `${tempFileSuffix}.t.sol`);
|
|
149
|
-
writeFileSync(tempFileName, newFile);
|
|
150
|
-
|
|
151
|
-
return tempFileName;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
async function runGasReport(): Promise<GasReport> {
|
|
155
|
-
console.log("Running gas report");
|
|
156
|
-
const gasReport: GasReport = [];
|
|
157
|
-
|
|
158
|
-
// Extract the logs from the child process
|
|
159
|
-
let logs = "";
|
|
160
|
-
try {
|
|
161
|
-
// Run the generated file using forge
|
|
162
|
-
const child = execa("forge", ["test", "--match-path", `*${tempFileSuffix}*`, "-vvv"], {
|
|
163
|
-
stdio: ["inherit", "pipe", "inherit"],
|
|
164
|
-
});
|
|
165
|
-
logs = (await child).stdout;
|
|
166
|
-
} catch (error: any) {
|
|
167
|
-
console.log(error.stdout ?? error);
|
|
168
|
-
console.log(chalk.red("\n-----------\nError while running the gas report (see above)"));
|
|
169
|
-
throw error;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// Extract the gas reports from the logs
|
|
173
|
-
|
|
174
|
-
// Create a regex to find all lines starting with `GAS REPORT:` and extract the name, function call and gas used
|
|
175
|
-
const gasReportRegex = new RegExp(/GAS REPORT\((.*)\): (.*) \[(.*)\]: (.*)/g);
|
|
176
|
-
|
|
177
|
-
// Loop through the matches and print the gas report
|
|
178
|
-
let gasReportMatch;
|
|
179
|
-
while ((gasReportMatch = gasReportRegex.exec(logs)) !== null) {
|
|
180
|
-
const source = gasReportMatch[1];
|
|
181
|
-
const name = gasReportMatch[2];
|
|
182
|
-
const functionCall = gasReportMatch[3].replace(";", "");
|
|
183
|
-
const gasUsed = parseInt(gasReportMatch[4]);
|
|
184
|
-
gasReport.push({ source, name, functionCall, gasUsed });
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
gasReport.sort((a, b) => a.source.localeCompare(b.source));
|
|
188
|
-
|
|
189
|
-
return gasReport;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function printGasReport(gasReport: GasReport, compare?: string) {
|
|
193
|
-
if (compare) console.log(chalk.bold(`Gas report compared to ${compare}`));
|
|
194
|
-
|
|
195
|
-
const headers = [
|
|
196
|
-
chalk.bold("Source"),
|
|
197
|
-
chalk.bold("Name"),
|
|
198
|
-
chalk.bold("Function call"),
|
|
199
|
-
chalk.bold("Gas used"),
|
|
200
|
-
...(compare ? [chalk.bold("Prev gas used"), chalk.bold("Difference")] : []),
|
|
201
|
-
];
|
|
202
|
-
|
|
203
|
-
const values = gasReport.map((entry) => {
|
|
204
|
-
const diff = entry.prevGasUsed ? entry.gasUsed - entry.prevGasUsed : 0;
|
|
205
|
-
const diffEntry = diff > 0 ? chalk.red(`+${diff}`) : diff < 0 ? chalk.green(`${diff}`) : diff;
|
|
206
|
-
const compareColumns = compare ? [entry.prevGasUsed ?? "n/a", diffEntry] : [];
|
|
207
|
-
const gasUsedEntry = diff > 0 ? chalk.red(entry.gasUsed) : diff < 0 ? chalk.green(entry.gasUsed) : entry.gasUsed;
|
|
208
|
-
return [entry.source, entry.name, entry.functionCall, gasUsedEntry, ...compareColumns];
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
const rows = [headers, ...values];
|
|
212
|
-
|
|
213
|
-
console.log(table(rows, { border: getBorderCharacters("norc") }));
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
function saveGasReport(gasReport: GasReport, path: string) {
|
|
217
|
-
console.log(chalk.bold(`Saving gas report to ${path}`));
|
|
218
|
-
writeFileSync(path, `${JSON.stringify(gasReport, null, 2)}\n`);
|
|
219
|
-
}
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity >=0.8.0;
|
|
3
|
-
|
|
4
|
-
import { Script } from "forge-std/Script.sol";
|
|
5
|
-
import { console } from "forge-std/console.sol";
|
|
6
|
-
|
|
7
|
-
import { BulkSetStateSystem, ID as BulkSetStateSystemID, ECSEvent } from "std-contracts/systems/BulkSetStateSystem.sol";
|
|
8
|
-
import { World } from "solecs/World.sol";
|
|
9
|
-
import { System } from "solecs/System.sol";
|
|
10
|
-
import { getAddressById } from "solecs/utils.sol";
|
|
11
|
-
import { Set } from "solecs/Set.sol";
|
|
12
|
-
|
|
13
|
-
struct ParsedState {
|
|
14
|
-
string[] componentIds;
|
|
15
|
-
string[] entities;
|
|
16
|
-
ParsedECSEvent[] state;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
struct ParsedECSEvent {
|
|
20
|
-
uint8 component;
|
|
21
|
-
uint32 entity;
|
|
22
|
-
string value;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
struct State {
|
|
26
|
-
uint256[] componentIds;
|
|
27
|
-
uint256[] entities;
|
|
28
|
-
ECSEvent[] state;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Usage:
|
|
33
|
-
* forge script --sig "run(string, address)" --rpc-url http://localhost:8545 src/contracts/BulkUpload.sol:BulkUpload path/to/ecs-map-test.json <WORLD_ADDRESS>
|
|
34
|
-
*/
|
|
35
|
-
contract BulkUpload is Script {
|
|
36
|
-
function run(string memory path, address worldAddress, uint256 eventsPerTx) public {
|
|
37
|
-
vmSafe.startBroadcast();
|
|
38
|
-
|
|
39
|
-
// Read JSON
|
|
40
|
-
console.log(path);
|
|
41
|
-
string memory json = vmSafe.readFile(path);
|
|
42
|
-
|
|
43
|
-
console.log(worldAddress);
|
|
44
|
-
|
|
45
|
-
// Parse JSON
|
|
46
|
-
ParsedState memory parsedState = abi.decode(vmSafe.parseJson(json), (ParsedState));
|
|
47
|
-
|
|
48
|
-
// Convert component ids
|
|
49
|
-
uint256[] memory componentIds = new uint256[](parsedState.componentIds.length);
|
|
50
|
-
for (uint256 i; i < componentIds.length; i++) {
|
|
51
|
-
componentIds[i] = hexToUint256(parsedState.componentIds[i]);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Convert entitiy ids
|
|
55
|
-
uint256[] memory entities = new uint256[](parsedState.entities.length);
|
|
56
|
-
for (uint256 i; i < entities.length; i++) {
|
|
57
|
-
entities[i] = hexToUint256(parsedState.entities[i]);
|
|
58
|
-
}
|
|
59
|
-
World world = World(worldAddress);
|
|
60
|
-
System bulkSetStateSystem = System(getAddressById(world.systems(), BulkSetStateSystemID));
|
|
61
|
-
|
|
62
|
-
// Convert state
|
|
63
|
-
ECSEvent[] memory allEvents = new ECSEvent[](parsedState.state.length);
|
|
64
|
-
for (uint256 i; i < parsedState.state.length; i++) {
|
|
65
|
-
ParsedECSEvent memory p = parsedState.state[i];
|
|
66
|
-
|
|
67
|
-
// Convert value hex string to bytes
|
|
68
|
-
bytes memory value = hexToBytes(substring(p.value, 2, bytes(p.value).length));
|
|
69
|
-
allEvents[i] = ECSEvent(p.component, p.entity, value);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
uint256 numTxs = allEvents.length / eventsPerTx;
|
|
73
|
-
|
|
74
|
-
for (uint256 i = 0; i < numTxs; i++) {
|
|
75
|
-
ECSEvent[] memory events = new ECSEvent[](eventsPerTx);
|
|
76
|
-
for (uint256 j = 0; j < eventsPerTx; j++) {
|
|
77
|
-
events[j] = allEvents[i * eventsPerTx + j];
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
(uint256[] memory newEntities, ECSEvent[] memory newEvents) = transformEventsToOnlyUseNeededEntities(
|
|
81
|
-
entities,
|
|
82
|
-
events
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
bulkSetStateSystem.execute(abi.encode(componentIds, newEntities, newEvents));
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// overflow tx
|
|
89
|
-
uint256 overflowEventCount = allEvents.length - numTxs * eventsPerTx;
|
|
90
|
-
|
|
91
|
-
ECSEvent[] memory overflowEvents = new ECSEvent[](overflowEventCount);
|
|
92
|
-
for (uint256 j = 0; j < overflowEventCount; j++) {
|
|
93
|
-
overflowEvents[j] = allEvents[numTxs * eventsPerTx + j];
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
(
|
|
97
|
-
uint256[] memory newOverflowEntities,
|
|
98
|
-
ECSEvent[] memory newOverflowEvents
|
|
99
|
-
) = transformEventsToOnlyUseNeededEntities(entities, overflowEvents);
|
|
100
|
-
|
|
101
|
-
bulkSetStateSystem.execute(abi.encode(componentIds, newOverflowEntities, newOverflowEvents));
|
|
102
|
-
|
|
103
|
-
vmSafe.stopBroadcast();
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function transformEventsToOnlyUseNeededEntities(
|
|
108
|
-
uint256[] memory entities,
|
|
109
|
-
ECSEvent[] memory events
|
|
110
|
-
) returns (uint256[] memory, ECSEvent[] memory) {
|
|
111
|
-
Set uniqueEntityIndices = new Set();
|
|
112
|
-
|
|
113
|
-
// Find unique entity indices
|
|
114
|
-
for (uint256 i = 0; i < events.length; i++) {
|
|
115
|
-
ECSEvent memory e = events[i];
|
|
116
|
-
|
|
117
|
-
uniqueEntityIndices.add(e.entity);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// Grab the Entity IDs from the big entities array and put them into our new array
|
|
121
|
-
uint256[] memory relevantEntities = new uint256[](uniqueEntityIndices.size());
|
|
122
|
-
for (uint256 i = 0; i < uniqueEntityIndices.size(); i++) {
|
|
123
|
-
relevantEntities[i] = entities[uniqueEntityIndices.getItems()[i]];
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Re-assign event entity indices to point to our new array
|
|
127
|
-
for (uint256 i = 0; i < events.length; i++) {
|
|
128
|
-
(, uint256 index) = uniqueEntityIndices.getIndex(events[i].entity);
|
|
129
|
-
events[i].entity = uint32(index);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return (relevantEntities, events);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function hexToUint8(bytes1 b) pure returns (uint8 res) {
|
|
136
|
-
if (b >= "0" && b <= "9") {
|
|
137
|
-
return uint8(b) - uint8(bytes1("0"));
|
|
138
|
-
} else if (b >= "A" && b <= "F") {
|
|
139
|
-
return 10 + uint8(b) - uint8(bytes1("A"));
|
|
140
|
-
} else if (b >= "a" && b <= "f") {
|
|
141
|
-
return 10 + uint8(b) - uint8(bytes1("a"));
|
|
142
|
-
}
|
|
143
|
-
return uint8(b); // or return error ...
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function hexToUint256(string memory str) pure returns (uint256 value) {
|
|
147
|
-
bytes memory b = bytes(str);
|
|
148
|
-
uint256 number = 0;
|
|
149
|
-
for (uint256 i = 0; i < b.length; i++) {
|
|
150
|
-
number = number << 4; // or number = number * 16
|
|
151
|
-
number |= hexToUint8(b[i]); // or number += numberFromAscII(b[i]);
|
|
152
|
-
}
|
|
153
|
-
return number;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Convert an hexadecimal string to raw bytes
|
|
157
|
-
function hexToBytes(string memory s) pure returns (bytes memory) {
|
|
158
|
-
bytes memory ss = bytes(s);
|
|
159
|
-
require(ss.length % 2 == 0); // length must be even
|
|
160
|
-
bytes memory r = new bytes(ss.length / 2);
|
|
161
|
-
for (uint256 i = 0; i < ss.length / 2; ++i) {
|
|
162
|
-
r[i] = bytes1(hexToUint8(ss[2 * i]) * 16 + hexToUint8(ss[2 * i + 1]));
|
|
163
|
-
}
|
|
164
|
-
return r;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function substring(string memory str, uint256 start, uint256 end) pure returns (string memory) {
|
|
168
|
-
bytes memory strBytes = bytes(str);
|
|
169
|
-
bytes memory result = new bytes(end - start);
|
|
170
|
-
for (uint256 i = start; i < end; i++) {
|
|
171
|
-
result[i - start] = strBytes[i];
|
|
172
|
-
}
|
|
173
|
-
return string(result);
|
|
174
|
-
}
|
package/src/contracts/Deploy.sol
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity >=0.8.0;
|
|
3
|
-
|
|
4
|
-
// Foundry
|
|
5
|
-
import { Script } from "forge-std/Script.sol";
|
|
6
|
-
import { console } from "forge-std/console.sol";
|
|
7
|
-
|
|
8
|
-
// Libraries
|
|
9
|
-
import { LibDeploy, DeployResult } from "./LibDeploy.sol";
|
|
10
|
-
|
|
11
|
-
contract Deploy is Script {
|
|
12
|
-
function broadcastDeploy(
|
|
13
|
-
address _deployer,
|
|
14
|
-
address _world,
|
|
15
|
-
bool _reuseComponents
|
|
16
|
-
) public returns (address world, uint256 initialBlockNumber) {
|
|
17
|
-
vmSafe.startBroadcast(_deployer);
|
|
18
|
-
initialBlockNumber = block.number;
|
|
19
|
-
DeployResult memory result = LibDeploy.deploy(_deployer, _world, _reuseComponents);
|
|
20
|
-
vmSafe.stopBroadcast();
|
|
21
|
-
world = address(result.world);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity >=0.8.0;
|
|
3
|
-
|
|
4
|
-
// NOTE: This file is autogenerated via `mud codegen-libdeploy` from `deploy.json`. Do not edit manually.
|
|
5
|
-
|
|
6
|
-
// Foundry
|
|
7
|
-
import { console } from "forge-std/console.sol";
|
|
8
|
-
|
|
9
|
-
// Solecs
|
|
10
|
-
import { IWorld } from "solecs/interfaces/IWorld.sol";
|
|
11
|
-
import { World } from "solecs/World.sol";
|
|
12
|
-
import { IComponent } from "solecs/interfaces/IComponent.sol";
|
|
13
|
-
import { getAddressById } from "solecs/utils.sol";
|
|
14
|
-
import { IUint256Component } from "solecs/interfaces/IUint256Component.sol";
|
|
15
|
-
import { ISystem } from "solecs/interfaces/ISystem.sol";
|
|
16
|
-
import { SystemStorage } from "solecs/SystemStorage.sol";
|
|
17
|
-
|
|
18
|
-
// Components (requires 'components=...' remapping in project's remappings.txt)
|
|
19
|
-
<% components.forEach(component => { -%>
|
|
20
|
-
import { <%= component %>, ID as <%= component %>ID } from "components/<%- component %>.sol";
|
|
21
|
-
<% }); -%>
|
|
22
|
-
|
|
23
|
-
// Systems (requires 'systems=...' remapping in project's remappings.txt)
|
|
24
|
-
<% systems.forEach(system => { -%>
|
|
25
|
-
import { <%= system.name %>, ID as <%= system.name %>ID } from "systems/<%- system.name %>.sol";
|
|
26
|
-
<% }); -%>
|
|
27
|
-
|
|
28
|
-
<% if (initializers.length > 0) { -%>
|
|
29
|
-
// Initializer libraries (requires 'libraries=...' remapping in project's remappings.txt)
|
|
30
|
-
<% initializers.forEach((initializer) => { -%>
|
|
31
|
-
import { <%= initializer %> } from "libraries/<%- initializer %>.sol";
|
|
32
|
-
<% }); -%>
|
|
33
|
-
<% } -%>
|
|
34
|
-
|
|
35
|
-
struct DeployResult {
|
|
36
|
-
IWorld world;
|
|
37
|
-
address deployer;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
library LibDeploy {
|
|
41
|
-
function deploy(
|
|
42
|
-
address _deployer,
|
|
43
|
-
address _world,
|
|
44
|
-
bool _reuseComponents
|
|
45
|
-
) internal returns (DeployResult memory result) {
|
|
46
|
-
result.deployer = _deployer;
|
|
47
|
-
|
|
48
|
-
// ------------------------
|
|
49
|
-
// Deploy
|
|
50
|
-
// ------------------------
|
|
51
|
-
|
|
52
|
-
// Deploy world
|
|
53
|
-
result.world = _world == address(0) ? new World() : IWorld(_world);
|
|
54
|
-
if (_world == address(0)) result.world.init(); // Init if it's a fresh world
|
|
55
|
-
|
|
56
|
-
// Deploy components
|
|
57
|
-
if (!_reuseComponents) {
|
|
58
|
-
IComponent comp;
|
|
59
|
-
<% components.forEach(component => { -%>
|
|
60
|
-
|
|
61
|
-
console.log("Deploying <%= component %>");
|
|
62
|
-
comp = new <%= component %>(address(result.world));
|
|
63
|
-
console.log(address(comp));
|
|
64
|
-
<% });-%>
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Deploy systems
|
|
68
|
-
deploySystems(address(result.world), true);
|
|
69
|
-
|
|
70
|
-
// Call initializer libraries
|
|
71
|
-
if (!_reuseComponents) {
|
|
72
|
-
// Allow initializers to utilize SystemStorage
|
|
73
|
-
SystemStorage.init(result.world, result.world.components());
|
|
74
|
-
|
|
75
|
-
<% initializers.forEach((name) => { -%>
|
|
76
|
-
<%= name -%>.init(result.world);
|
|
77
|
-
<% }); -%>
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function authorizeWriter(
|
|
82
|
-
IUint256Component components,
|
|
83
|
-
uint256 componentId,
|
|
84
|
-
address writer
|
|
85
|
-
) internal {
|
|
86
|
-
IComponent(getAddressById(components, componentId)).authorizeWriter(writer);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Deploy systems to the given world.
|
|
91
|
-
* If `init` flag is set, systems with `initialize` setting in `deploy.json` will be executed.
|
|
92
|
-
*/
|
|
93
|
-
function deploySystems(
|
|
94
|
-
address _world,
|
|
95
|
-
bool init
|
|
96
|
-
) internal {
|
|
97
|
-
IWorld world = IWorld(_world);
|
|
98
|
-
// Deploy systems
|
|
99
|
-
ISystem system;
|
|
100
|
-
IUint256Component components = world.components();
|
|
101
|
-
<% systems.forEach(system => { -%>
|
|
102
|
-
|
|
103
|
-
console.log("Deploying <%= system.name %>");
|
|
104
|
-
system = new <%= system.name %>(world, address(components));
|
|
105
|
-
world.registerSystem(address(system), <%= system.name %>ID);
|
|
106
|
-
<% system.writeAccess?.forEach(component => { -%>
|
|
107
|
-
<% if (component === "*") { -%>
|
|
108
|
-
<% components.forEach(comp=> { -%>
|
|
109
|
-
authorizeWriter(components, <%= comp %>ID, address(system));
|
|
110
|
-
<% });-%>
|
|
111
|
-
<% } else { -%>
|
|
112
|
-
authorizeWriter(components, <%= component %>ID, address(system));
|
|
113
|
-
<% } -%>
|
|
114
|
-
<% });-%>
|
|
115
|
-
<% if (system.initialize) { -%>
|
|
116
|
-
if(init) system.execute(<%= system.initialize -%>);
|
|
117
|
-
<% } -%>
|
|
118
|
-
console.log(address(system));
|
|
119
|
-
<% });-%>
|
|
120
|
-
}
|
|
121
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity >=0.8.0;
|
|
3
|
-
|
|
4
|
-
// NOTE: THIS IS A STUB TO MAKE IMPORTS WORK.
|
|
5
|
-
// The real LibDeploy.sol is generated automatically
|
|
6
|
-
// when calling `mud test` or `mud deploy-contracts`.
|
|
7
|
-
// To manually generate the real LibDeploy.sol use
|
|
8
|
-
// `mud codegen-libdeploy`.
|
|
9
|
-
|
|
10
|
-
import { IWorld } from "solecs/interfaces/IWorld.sol";
|
|
11
|
-
|
|
12
|
-
struct DeployResult {
|
|
13
|
-
IWorld world;
|
|
14
|
-
address deployer;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
library LibDeploy {
|
|
18
|
-
function deploy(
|
|
19
|
-
address _deployer,
|
|
20
|
-
address _world,
|
|
21
|
-
bool _reuseComponents
|
|
22
|
-
) internal returns (DeployResult memory result) {}
|
|
23
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity >=0.8.0;
|
|
3
|
-
|
|
4
|
-
// NOTE: THIS IS A STUB TO MAKE IMPORTS WORK.
|
|
5
|
-
// The real LibDeploy.sol is generated automatically
|
|
6
|
-
// when calling `mud test` or `mud deploy-contracts`.
|
|
7
|
-
// To manually generate the real LibDeploy.sol use
|
|
8
|
-
// `mud codegen-libdeploy`.
|
|
9
|
-
|
|
10
|
-
import { IWorld } from "solecs/interfaces/IWorld.sol";
|
|
11
|
-
|
|
12
|
-
struct DeployResult {
|
|
13
|
-
IWorld world;
|
|
14
|
-
address deployer;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
library LibDeploy {
|
|
18
|
-
function deploy(
|
|
19
|
-
address _deployer,
|
|
20
|
-
address _world,
|
|
21
|
-
bool _reuseComponents
|
|
22
|
-
) internal returns (DeployResult memory result) {}
|
|
23
|
-
}
|
package/src/mud2.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import yargs from "yargs";
|
|
4
|
-
import { hideBin } from "yargs/helpers";
|
|
5
|
-
import { commands } from "./commands/index.js";
|
|
6
|
-
import { logError } from "./utils/errors.js";
|
|
7
|
-
|
|
8
|
-
// Load .env file into process.env
|
|
9
|
-
import * as dotenv from "dotenv";
|
|
10
|
-
dotenv.config();
|
|
11
|
-
|
|
12
|
-
yargs(hideBin(process.argv))
|
|
13
|
-
// Explicit name to display in help (by default it's the entry file, which may not be "mud" for e.g. ts-node)
|
|
14
|
-
.scriptName("mud")
|
|
15
|
-
// Use the commands directory to scaffold
|
|
16
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- command array overload isn't typed, see https://github.com/yargs/yargs/blob/main/docs/advanced.md#esm-hierarchy
|
|
17
|
-
.command(commands as any)
|
|
18
|
-
// Enable strict mode.
|
|
19
|
-
.strict()
|
|
20
|
-
// Custom error handler
|
|
21
|
-
.fail((msg, err) => {
|
|
22
|
-
console.log("");
|
|
23
|
-
logError(err);
|
|
24
|
-
console.log("");
|
|
25
|
-
|
|
26
|
-
process.exit(1);
|
|
27
|
-
})
|
|
28
|
-
// Useful aliases.
|
|
29
|
-
.alias({ h: "help" }).argv;
|