@quenty/cmdrservice 3.3.1 → 3.5.0

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/CHANGELOG.md CHANGED
@@ -3,6 +3,28 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [3.5.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/cmdrservice@3.4.0...@quenty/cmdrservice@3.5.0) (2021-12-09)
7
+
8
+
9
+ ### Features
10
+
11
+ * CmdrService allows access to cmdr via /cmdr for mobile access ([ee9e233](https://github.com/Quenty/NevermoreEngine/commit/ee9e233862805bcbfc8c3c0a2698f405ba8c65cc))
12
+
13
+
14
+
15
+
16
+
17
+ # [3.4.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/cmdrservice@3.3.1...@quenty/cmdrservice@3.4.0) (2021-12-05)
18
+
19
+
20
+ ### Features
21
+
22
+ * Make CmdrService allow dynamically binding commands ([d4a0de8](https://github.com/Quenty/NevermoreEngine/commit/d4a0de8ccb4b791185ab0020bb7efe23d71501de))
23
+
24
+
25
+
26
+
27
+
6
28
  ## [3.3.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/cmdrservice@3.3.0...@quenty/cmdrservice@3.3.1) (2021-12-04)
7
29
 
8
30
  **Note:** Version bump only for package @quenty/cmdrservice
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/cmdrservice",
3
- "version": "3.3.1",
3
+ "version": "3.5.0",
4
4
  "description": "Bridge between cmdr and Nevermore services using servicebag",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -29,7 +29,8 @@
29
29
  "@quenty/permissionprovider": "^4.3.1",
30
30
  "@quenty/promise": "^3.2.0",
31
31
  "@quenty/remoting": "^3.2.0",
32
- "@quenty/servicebag": "^3.2.1"
32
+ "@quenty/servicebag": "^3.2.1",
33
+ "@quenty/templateprovider": "^3.4.0"
33
34
  },
34
35
  "peerDependencies": {
35
36
  "@quentystudios/cmdr": "^1.8.4-quenty.1"
@@ -37,5 +38,5 @@
37
38
  "publishConfig": {
38
39
  "access": "public"
39
40
  },
40
- "gitHead": "a0583027b100d718bce1603ec6c525ad99adeeac"
41
+ "gitHead": "adc1a4962fffcccc7f55411ebed6c9306d86dac6"
41
42
  }
@@ -1,24 +1,31 @@
1
- ---
1
+ --- Loads cmdr on the client
2
2
  -- @module CmdrServiceClient
3
3
  -- @author Quenty
4
4
 
5
5
  local require = require(script.Parent.loader).load(script)
6
6
 
7
7
  local ReplicatedStorage = game:GetService("ReplicatedStorage")
8
+ local Players = game:GetService("Players")
8
9
 
9
10
  local promiseChild = require("promiseChild")
10
11
  local PromiseUtils = require("PromiseUtils")
11
12
  local PermissionServiceClient = require("PermissionServiceClient")
13
+ local Maid = require("Maid")
14
+ local String = require("String")
12
15
 
13
16
  local CmdrServiceClient = {}
14
17
 
15
18
  function CmdrServiceClient:Init(serviceBag)
16
- assert(serviceBag, "No serviceBag")
19
+ assert(not self._serviceBag, "Already initialized")
20
+ self._serviceBag = assert(serviceBag, "No serviceBag")
17
21
 
22
+ self._maid = Maid.new()
18
23
  self._permissionService = serviceBag:GetService(PermissionServiceClient)
19
24
  end
20
25
 
21
26
  function CmdrServiceClient:Start()
27
+ assert(self._serviceBag, "Not initialized")
28
+
22
29
  PromiseUtils.all({
23
30
  self:PromiseCmdr(),
24
31
  self._permissionService:PromisePermissionProvider()
@@ -28,14 +35,31 @@ function CmdrServiceClient:Start()
28
35
  })
29
36
  :Then(function(cmdr, isAdmin)
30
37
  if isAdmin then
31
- cmdr:SetActivationKeys({ Enum.KeyCode.F2 })
38
+ self:_setBindings(cmdr)
32
39
  else
33
40
  cmdr:SetActivationKeys({})
34
41
  end
35
42
  end)
36
43
  end
37
44
 
45
+ function CmdrServiceClient:_setBindings(cmdr)
46
+ cmdr:SetActivationUnlocksMouse(true)
47
+ cmdr:SetActivationKeys({ Enum.KeyCode.F2 })
48
+
49
+ -- enable activation on mobile
50
+ self._maid:GiveTask(Players.LocalPlayer.Chatted:Connect(function(chat)
51
+ if String.startsWith(chat, "/cmdr") then
52
+ cmdr:Show()
53
+ end
54
+ end))
55
+
56
+ -- Default blink for debugging purposes
57
+ cmdr.Dispatcher:Run("bind", Enum.KeyCode.G.Name, "blink")
58
+ end
59
+
38
60
  function CmdrServiceClient:PromiseCmdr()
61
+ assert(self._serviceBag, "Not initialized")
62
+
39
63
  if self._cmdrPromise then
40
64
  return self._cmdrPromise
41
65
  end
@@ -4,19 +4,30 @@
4
4
 
5
5
  local require = require(script.Parent.loader).load(script)
6
6
 
7
+ local HttpService = game:GetService("HttpService")
8
+
7
9
  local PermissionService = require("PermissionService")
10
+ local CmdrTemplateProviderServer = require("CmdrTemplateProviderServer")
8
11
  local Promise = require("Promise")
9
12
 
10
13
  local CmdrService = {}
11
14
 
15
+ local GLOBAL_REGISTRY = setmetatable({}, {__mode = "kv"})
16
+
12
17
  function CmdrService:Init(serviceBag)
13
18
  assert(not self._cmdr, "Already initialized")
14
19
 
20
+ self._serviceBag = assert(serviceBag, "No serviceBag")
21
+ self._cmdrTemplateProviderServer = self._serviceBag:GetService(CmdrTemplateProviderServer)
22
+
23
+ self._serviceId = HttpService:GenerateGUID(false)
15
24
  self._cmdr = require("Cmdr")
16
- self._cmdr:RegisterDefaultCommands()
17
25
 
18
26
  self._permissionService = serviceBag:GetService(PermissionService)
19
27
 
28
+ self._definitionData = {}
29
+ self._executeData = {}
30
+
20
31
  self._cmdr.Registry:RegisterHook("BeforeRun", function(context)
21
32
  local providerPromise = self._permissionService:PromisePermissionProvider()
22
33
  if providerPromise:IsPending() then
@@ -32,6 +43,12 @@ function CmdrService:Init(serviceBag)
32
43
  return "You don't have permission to run this command"
33
44
  end
34
45
  end)
46
+
47
+ GLOBAL_REGISTRY[self._serviceId] = self
48
+ end
49
+
50
+ function CmdrService:Start()
51
+ self._cmdr:RegisterDefaultCommands()
35
52
  end
36
53
 
37
54
  function CmdrService:PromiseCmdr()
@@ -40,4 +57,63 @@ function CmdrService:PromiseCmdr()
40
57
  return Promise.resolved(self._cmdr)
41
58
  end
42
59
 
60
+ function CmdrService:RegisterCommand(commandData, execute)
61
+ assert(self._cmdr, "Not initialized")
62
+ assert(commandData, "No commandData")
63
+ assert(commandData.Name, "No commandData.Name")
64
+ assert(execute, "No execute")
65
+
66
+ local commandId = ("%s_%s"):format(commandData.Name, HttpService:GenerateGUID(false))
67
+
68
+ self._definitionData[commandId] = commandData
69
+ self._executeData[commandId] = execute
70
+
71
+ local commandServerScript = self._cmdrTemplateProviderServer:Clone("CmdrExecutionTemplate")
72
+ commandServerScript.Name = ("%sServer"):format(commandId)
73
+
74
+ local cmdrServiceTarget = Instance.new("ObjectValue")
75
+ cmdrServiceTarget.Name = "CmdrServiceTarget"
76
+ cmdrServiceTarget.Value = script
77
+ cmdrServiceTarget.Parent = commandServerScript
78
+
79
+ local cmdrServiceId = Instance.new("StringValue")
80
+ cmdrServiceId.Name = "CmdrServiceId"
81
+ cmdrServiceId.Value = self._serviceId
82
+ cmdrServiceId.Parent = commandServerScript
83
+
84
+ local cmdrCommandId = Instance.new("StringValue")
85
+ cmdrCommandId.Name = "CmdrCommandId"
86
+ cmdrCommandId.Value = commandId
87
+ cmdrCommandId.Parent = commandServerScript
88
+
89
+ local commandScript = self._cmdrTemplateProviderServer:Clone("CmdrCommandDefinitionTemplate")
90
+ commandScript.Name = commandId
91
+
92
+ local cmdrJsonCommandData = Instance.new("StringValue")
93
+ cmdrJsonCommandData.Value = HttpService:JSONEncode(commandData)
94
+ cmdrJsonCommandData.Name = "CmdrJsonCommandData"
95
+ cmdrJsonCommandData.Parent = commandScript
96
+
97
+ self._cmdr.Registry:RegisterCommand(commandScript, commandServerScript)
98
+ end
99
+
100
+ function CmdrService:__ExecuteCommand(id, ...)
101
+ assert(type(id) == "string", "Bad serviceId")
102
+ assert(self._cmdr, "Not initialized")
103
+
104
+ local execute = self._executeData[id]
105
+ if not execute then
106
+ error(("[CmdrService] - No command definition for id %q"):format(tostring(id)))
107
+ end
108
+
109
+ return execute(...)
110
+ end
111
+
112
+ -- Global, but only intended for internal use
113
+ function CmdrService:__GetServiceFromId(id)
114
+ assert(type(id) == "string", "Bad serviceId")
115
+
116
+ return GLOBAL_REGISTRY[id]
117
+ end
118
+
43
119
  return CmdrService
@@ -0,0 +1,7 @@
1
+ --- Generic command definition template
2
+ -- @module CmdrCommandDefinitionTemplate
3
+
4
+ local HttpService = game:GetService("HttpService")
5
+
6
+ local value = script:WaitForChild("CmdrJsonCommandData")
7
+ return HttpService:JSONDecode(value.Value)
@@ -0,0 +1,20 @@
1
+ --- Generic command definition template
2
+ -- @module CmdrCommandDefinitionTemplate
3
+
4
+ local function waitForValue(objectValue)
5
+ local value = objectValue.Value
6
+ if value then
7
+ return value
8
+ end
9
+
10
+ return objectValue.Changed:Wait()
11
+ end
12
+
13
+ local cmdrServiceId = waitForValue(script:WaitForChild("CmdrServiceId"))
14
+ local cmdrCommandId = waitForValue(script:WaitForChild("CmdrCommandId"))
15
+ local commandServiceDefinition = require(waitForValue(script:WaitForChild("CmdrServiceTarget")))
16
+ local cmdrService = commandServiceDefinition:__GetServiceFromId(cmdrServiceId)
17
+
18
+ return function(...)
19
+ return cmdrService:__ExecuteCommand(cmdrCommandId, ...)
20
+ end
@@ -0,0 +1,9 @@
1
+ --- Retrieves CmdrTemplateProviderServer
2
+ -- @module CmdrTemplateProviderServer
3
+ -- @author Quenty
4
+
5
+ local require = require(script.Parent.loader).load(script)
6
+
7
+ local TemplateProvider = require("TemplateProvider")
8
+
9
+ return TemplateProvider.new(script)
@@ -1,12 +1,7 @@
1
1
  {
2
- "name": "cmdrservicetest",
2
+ "name": "CmdrServiceTest",
3
3
  "tree": {
4
4
  "$className": "DataModel",
5
- "ReplicatedStorage": {
6
- "Nevermore": {
7
- "$path": "../node_modules/@quenty/loader"
8
- }
9
- },
10
5
  "ServerScriptService": {
11
6
  "cmdrservice": {
12
7
  "$path": ".."
@@ -17,3 +17,29 @@ serviceBag:GetService(require(serverFolder.CmdrService))
17
17
 
18
18
  serviceBag:Init()
19
19
  serviceBag:Start()
20
+
21
+ serviceBag:GetService(require(serverFolder.CmdrService)):RegisterCommand({
22
+ Name = "explode";
23
+ Aliases = { "boom" };
24
+ Description = "Makes players explode";
25
+ Group = "Admin";
26
+ Args = {
27
+ {
28
+ Type = "players";
29
+ Name = "players";
30
+ Description = "Victims";
31
+ },
32
+ };
33
+ }, function(_context, players)
34
+ for _, player in pairs(players) do
35
+ local humanoid = player.Character and player.Character:FindFirstChildWhichIsA("Humanoid")
36
+ local humanoidRootPart = humanoid and humanoid.RootPart
37
+ if humanoidRootPart then
38
+ local explosion = Instance.new("Explosion")
39
+ explosion.Position = humanoidRootPart.Position
40
+ explosion.Parent = humanoidRootPart
41
+ end
42
+ end
43
+
44
+ return "Exploded!"
45
+ end)