Add game system support

This commit is contained in:
2021-04-15 09:51:46 +02:00
parent 8753a83d93
commit de4a289395
3 changed files with 34 additions and 4 deletions

View File

@@ -1,2 +1,6 @@
## In development
### New features
- Added support for game systems
## 1.0.0 ## 1.0.0
### Initial release ### Initial release

View File

@@ -64,6 +64,17 @@ Call `registerModule` to make socketlib listen for sockets that come in for your
**Return value**: A socket instance is returned, that is used for all further interactions with socketlib. **Return value**: A socket instance is returned, that is used for all further interactions with socketlib.
#### socketlib.registerSystem
```javascript
registerSystem(systemId);
```
Call `registerSystem` to make socketlib listen for sockets that come in for your game system. This is the first function in socketlib that your game system should call.
- **systemId** the id of your game system as specified in your game system's manifest.
**Return value**: A socket instance is returned, that is used for all further interactions with socketlib.
#### socket.register #### socket.register
```javascript ```javascript
socket.register(name, func); socket.register(name, func);

View File

@@ -39,17 +39,32 @@ class Socketlib {
console.error(`socketlib | Failed to register socket for module '${moduleName}'. Please set '"socket":true' in your manifset and restart foundry (you need to reload your world - simply reloading your browser won't do).`); console.error(`socketlib | Failed to register socket for module '${moduleName}'. Please set '"socket":true' in your manifset and restart foundry (you need to reload your world - simply reloading your browser won't do).`);
return undefined; return undefined;
} }
const newSocket = new SocketlibSocket(moduleName); const newSocket = new SocketlibSocket(moduleName, "module");
this.modules.set(moduleName, newSocket); this.modules.set(moduleName, newSocket);
return newSocket; return newSocket;
} }
registerSystem(systemId) {
if (game.system.id !== systemId) {
console.error(`socketlib | Someone tried to register system '${systemId}', but that system isn't active. As a result the registration request has been ignored.`);
return undefined;
}
const existingSocket = this.system;
if (existingSocket)
return existingSocket;
if (!game.system.data.socket) {
console.error(`socketlib | Failed to register socket for system '${systemId}'. Please set '"socket":true' in your manifest and restart foundry (you need to reload your world - simply reloading your browser won't do).`);
}
const newSocket = new SocketlibSocket(systemId, "system");
this.system = newSocket;
return newSocket;
}
} }
class SocketlibSocket { class SocketlibSocket {
constructor(moduleName) { constructor(moduleName, moduleType) {
this.moduleName = moduleName;
this.functions = new Map(); this.functions = new Map();
this.socketName = `module.${moduleName}`; this.socketName = `${moduleType}.${moduleName}`;
this.pendingRequests = new Map(); this.pendingRequests = new Map();
game.socket.on(this.socketName, this._onSocketReceived.bind(this)); game.socket.on(this.socketName, this._onSocketReceived.bind(this));
} }