diff --git a/packages/world-modules/src/modules/uniqueentity/getUniqueEntity.sol b/packages/world-modules/src/modules/uniqueentity/getUniqueEntity.sol index 5764ce7343..62b99f6e8c 100644 --- a/packages/world-modules/src/modules/uniqueentity/getUniqueEntity.sol +++ b/packages/world-modules/src/modules/uniqueentity/getUniqueEntity.sol @@ -4,8 +4,9 @@ pragma solidity >=0.8.21; import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld.sol"; import { IUniqueEntitySystem } from "../../interfaces/IUniqueEntitySystem.sol"; +import { UniqueEntitySystem } from "./UniqueEntitySystem.sol"; -import { SystemSwitch } from "../utils/SystemSwitch.sol"; +import { SystemSwitch } from "../../utils/SystemSwitch.sol"; import { SYSTEM_ID } from "./constants.sol"; /** @@ -15,7 +16,7 @@ import { SYSTEM_ID } from "./constants.sol"; * For usage outside of a World, use the overload that takes an explicit store argument. */ function getUniqueEntity() returns (bytes32 uniqueEntity) { - return abi.decode(SystemSwitch.call(SYSTEM_ID, new bytes(0)), (bytes32)); + return abi.decode(SystemSwitch.call(SYSTEM_ID, abi.encodeCall(UniqueEntitySystem.getUniqueEntity, ())), (bytes32)); } /** diff --git a/packages/world-modules/src/modules/utils/SystemSwitch.sol b/packages/world-modules/src/utils/SystemSwitch.sol similarity index 100% rename from packages/world-modules/src/modules/utils/SystemSwitch.sol rename to packages/world-modules/src/utils/SystemSwitch.sol diff --git a/packages/world-modules/test/SystemSwitch.t.sol b/packages/world-modules/test/SystemSwitch.t.sol index f0d2a94a4d..cce9879230 100644 --- a/packages/world-modules/test/SystemSwitch.t.sol +++ b/packages/world-modules/test/SystemSwitch.t.sol @@ -11,7 +11,7 @@ import { CoreModule } from "@latticexyz/world/src/modules/core/CoreModule.sol"; import { ResourceId, WorldResourceIdLib } from "@latticexyz/world/src/WorldResourceId.sol"; import { RESOURCE_SYSTEM } from "@latticexyz/world/src/worldResourceTypes.sol"; import { ROOT_NAMESPACE } from "@latticexyz/world/src/constants.sol"; -import { SystemSwitch } from "../src/modules/utils/SystemSwitch.sol"; +import { SystemSwitch } from "../src/utils/SystemSwitch.sol"; contract EchoSystem is System { function msgSender() public view returns (address) { diff --git a/packages/world-modules/test/UniqueEntityModule.t.sol b/packages/world-modules/test/UniqueEntityModule.t.sol index 0f5c4e3bb6..c5f12dfb2e 100644 --- a/packages/world-modules/test/UniqueEntityModule.t.sol +++ b/packages/world-modules/test/UniqueEntityModule.t.sol @@ -7,6 +7,8 @@ import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol"; import { World } from "@latticexyz/world/src/World.sol"; import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld.sol"; import { IWorldErrors } from "@latticexyz/world/src/IWorldErrors.sol"; +import { System } from "@latticexyz/world/src/System.sol"; +import { RESOURCE_SYSTEM } from "@latticexyz/world/src/worldResourceTypes.sol"; import { CoreModule } from "@latticexyz/world/src/modules/core/CoreModule.sol"; import { UniqueEntityModule } from "../src/modules/uniqueentity/UniqueEntityModule.sol"; @@ -17,6 +19,13 @@ import { NAMESPACE, TABLE_NAME } from "../src/modules/uniqueentity/constants.sol import { ResourceId, WorldResourceIdLib, WorldResourceIdInstance } from "@latticexyz/world/src/WorldResourceId.sol"; import { RESOURCE_TABLE } from "@latticexyz/world/src/worldResourceTypes.sol"; +contract UniqueEntityTestSystem is System { + function echoUniqueEntity() public returns (bytes32) { + // Execute `getUniqueEntity` from the context of a World + return getUniqueEntity(); + } +} + contract UniqueEntityModuleTest is Test, GasReporter { using WorldResourceIdInstance for ResourceId; @@ -91,4 +100,32 @@ contract UniqueEntityModuleTest is Test, GasReporter { ); UniqueEntity.set(world, tableId, 123); } + + function testAccessInWorldContext() public { + world.installModule(uniqueEntityModule, new bytes(0)); + + // Set up a system that calls `getUniqueEntity` from the context of a World + UniqueEntityTestSystem uniqueEntityTestSystem = new UniqueEntityTestSystem(); + ResourceId uniqueEntityTestSystemId = WorldResourceIdLib.encode({ + typeId: RESOURCE_SYSTEM, + namespace: "somens", + name: "echoUniqueEntity" + }); + world.registerSystem(uniqueEntityTestSystemId, uniqueEntityTestSystem, true); + + // Execute `getUniqueEntity` from the context of a World + bytes32 uniqueEntity1 = abi.decode( + world.call(uniqueEntityTestSystemId, abi.encodeCall(UniqueEntityTestSystem.echoUniqueEntity, ())), + (bytes32) + ); + bytes32 uniqueEntity2 = abi.decode( + world.call(uniqueEntityTestSystemId, abi.encodeCall(UniqueEntityTestSystem.echoUniqueEntity, ())), + (bytes32) + ); + bytes32 uniqueEntity3 = getUniqueEntity(world); + + // The next entity must be incremented + assertEq(uint256(uniqueEntity2), uint256(uniqueEntity1) + 1); + assertEq(uint256(uniqueEntity3), uint256(uniqueEntity2) + 1); + } }