-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNexus.lua
More file actions
409 lines (308 loc) · 11.9 KB
/
Nexus.lua
File metadata and controls
409 lines (308 loc) · 11.9 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
if Nexus then Nexus:Stop() end
if not game:IsLoaded() then
task.delay(60, function()
if NoShutdown then return end
if not game:IsLoaded() then
return game:Shutdown()
end
local Code = game:GetService'GuiService':GetErrorCode().Value
if Code >= Enum.ConnectionError.DisconnectErrors.Value then
return game:Shutdown()
end
end)
game.Loaded:Wait()
end
local Nexus = {}
local WSConnect = syn and syn.websocket.connect or Krnl and Krnl.WebSocket.connect or WebSocket and WebSocket.connect
if not WSConnect then return end
local TeleportService = game:GetService'TeleportService'
local InputService = game:GetService'UserInputService'
local HttpService = game:GetService'HttpService'
local RunService = game:GetService'RunService'
local GuiService = game:GetService'GuiService'
local Players = game:GetService'Players'
local LocalPlayer = Players.LocalPlayer if not LocalPlayer then repeat LocalPlayer = Players.LocalPlayer task.wait() until LocalPlayer end task.wait(0.5)
local UGS = UserSettings():GetService'UserGameSettings'
local OldVolume = UGS.MasterVolume
LocalPlayer.OnTeleport:Connect(function(State)
if State == Enum.TeleportState.Started and Nexus.IsConnected then
Nexus:Stop() -- Apparently doesn't disconnect websockets on teleport so this has to be here
end
end)
local Signal = {} do
Signal.__index = Signal
function Signal.new()
local self = setmetatable({ _BindableEvent = Instance.new'BindableEvent' }, Signal)
return self
end
function Signal:Connect(Callback)
assert(typeof(Callback) == 'function', 'function expected, got ' .. typeof(Callback))
return self._BindableEvent.Event:Connect(Callback)
end
function Signal:Fire(...)
self._BindableEvent:Fire(...)
end
function Signal:Wait()
return self._BindableEvent.Event:Wait()
end
function Signal:Disconnect()
if self._BindableEvent then
self._BindableEvent:Destroy()
end
end
end
do -- Nexus
local BTN_CLICK = 'ButtonClicked:'
Nexus.Connected = Signal.new()
Nexus.Disconnected = Signal.new()
Nexus.MessageReceived = Signal.new()
Nexus.Commands = {}
Nexus.Connections = {}
Nexus.ShutdownTime = 45
Nexus.ShutdownOnTeleportError = true
function Nexus:Send(Command, Payload)
assert(self.Socket ~= nil, 'websocket is nil')
assert(self.IsConnected, 'websocket not connected')
assert(typeof(Command) == 'string', 'Command must be a string, got ' .. typeof(Command))
if Payload then
assert(typeof(Payload) == 'table', 'Payload must be a table, got ' .. typeof(Payload))
end
local Message = HttpService:JSONEncode {
Name = Command,
Payload = Payload
}
self.Socket:Send(Message)
end
function Nexus:SetAutoRelaunch(Enabled)
self:Send('SetAutoRelaunch', { Content = Enabled and 'true' or 'false' })
end
function Nexus:SetPlaceId(PlaceId)
self:Send('SetPlaceId', { Content = PlaceId })
end
function Nexus:SetJobId(JobId)
self:Send('SetJobId', { Content = JobId })
end
function Nexus:Echo(Message)
self:Send('Echo', { Content = Message })
end
function Nexus:Log(...)
local T = {}
for Index, Value in pairs{ ... } do
table.insert(T, tostring(Value))
end
self:Send('Log', {
Content = table.concat(T, ' ')
})
end
function Nexus:CreateElement(ElementType, Name, Content, Size, Margins, Table)
assert(typeof(Name) == 'string', 'string expected on argument #1, got ' .. typeof(Name))
assert(typeof(Content) == 'string', 'string expected on argument #2, got ' .. typeof(Content))
assert(Name:find'%W' == nil, 'argument #1 cannot contain whitespace')
if Size then assert(typeof(Size) == 'table' and #Size == 2, 'table with 2 arguments expected on argument #3, got ' .. typeof(Size)) end
if Margins then assert(typeof(Margins) == 'table' and #Margins == 4, 'table with 4 arguments expected on argument #4, got ' .. typeof(Margins)) end
local Payload = {
Name = Name,
Content = Content,
Size = Size and table.concat(Size, ','),
Margin = Margins and table.concat(Margins, ',')
}
if Table then
for Index, Value in pairs(Table) do
Payload[Index] = Value
end
end
self:Send(ElementType, Payload)
end
function Nexus:CreateButton(...)
return self:CreateElement('CreateButton', ...)
end
function Nexus:CreateTextBox(...)
return self:CreateElement('CreateTextBox', ...)
end
function Nexus:CreateNumeric(Name, Value, DecimalPlaces, Increment, Size, Margins)
return self:CreateElement('CreateNumeric', Name, tostring(Value), Size, Margins, { DecimalPlaces = DecimalPlaces, Increment = Increment })
end
function Nexus:CreateLabel(...)
return self:CreateElement('CreateLabel', ...)
end
function Nexus:NewLine(...)
return self:Send('NewLine')
end
function Nexus:GetText(Name)
return self:WaitForMessage('ElementText:', 'GetText', { Name = Name })
end
function Nexus:SetRelaunch(Seconds)
self:Send('SetRelaunch', { Seconds = Seconds })
end
function Nexus:WaitForMessage(Header, Message, Payload)
if Message then
task.defer(self.Send, self, Message, Payload)
end
local Message
while true do
Message = self.MessageReceived:Wait()
if Message:sub(1, #Header) == Header then
break
end
end
return Message:sub(#Header + 1)
end
function Nexus:Connect(Host)
while true do
for Index, Connection in pairs(self.Connections) do
Connection:Disconnect()
end
table.clear(self.Connections)
if self.IsConnected then
self.IsConnected = false
self.Socket = nil
self.Disconnected:Fire()
end
if self.Terminated then break end
if not Host then
Host = 'localhost:5242'
end
local Success, Socket = pcall(WSConnect, ('ws://%s/Nexus?name=%s&id=%s&jobId=%s'):format(Host, LocalPlayer.Name, LocalPlayer.UserId, game.JobId))
if not Success then task.wait(12) continue end
self.Socket = Socket
self.IsConnected = true
table.insert(self.Connections, Socket.OnMessage:Connect(function(Message)
self.MessageReceived:Fire(Message)
end))
table.insert(self.Connections, Socket.OnClose:Connect(function()
self.IsConnected = false
self.Disconnected:Fire()
end))
self.Connected:Fire()
while self.IsConnected do
local Success, Error = pcall(self.Send, self, 'ping')
if not Success or self.Terminated then
break
end
task.wait(1)
end
end
end
function Nexus:Stop()
self.IsConnected = false
self.Terminated = true
self.Disconnected:Fire()
if self.Socket then
pcall(function() self.Socket:Close() end)
end
end
function Nexus:AddCommand(Name, Function)
self.Commands[Name] = Function
end
function Nexus:RemoveCommand(Name)
self.Commands[Name] = nil
end
function Nexus:OnButtonClick(Name, Function)
self:AddCommand('ButtonClicked:' .. Name, Function)
end
Nexus.MessageReceived:Connect(function(Message)
local S = Message:find(' ')
if S then
local Command, Message = Message:sub(1, S - 1):lower(), Message:sub(S + 1)
if Nexus.Commands[Command] then
local Success, Error = pcall(Nexus.Commands[Command], Message)
if not Success and Error then
Nexus:Log(('Error with command `%s`: %s'):format(Command, Error))
end
end
elseif Nexus.Commands[Message] then
local Success, Error = pcall(Nexus.Commands[Message], Message)
if not Success and Error then
Nexus:Log(('Error with command `%s`: %s'):format(Message, Error))
end
end
end)
end
do -- Default Commands
Nexus:AddCommand('execute', function(Message)
local Function, Error = loadstring(Message)
if Function then
local Env = getfenv(Function)
Env.Player = LocalPlayer
Env.print = function(...)
local T = {}
for Index, Value in pairs{ ... } do
table.insert(T, tostring(Value))
end
Nexus:Log(table.concat(T, ' '))
end
local S, E = pcall(Function)
if not S then
Nexus:Log(E)
end
else
Nexus:Log(Error)
end
end)
Nexus:AddCommand('teleport', function(Message)
local S = Message:find(' ')
local PlaceId, JobId = S and Message:sub(1, S - 1) or Message, S and Message:sub(S + 1)
if JobId then
TeleportService:TeleportToPlaceInstance(tonumber(PlaceId), JobId)
else
TeleportService:Teleport(tonumber(PlaceId))
end
end)
Nexus:AddCommand('rejoin', function(Message)
TeleportService:TeleportToPlaceInstance(game.PlaceId, game.JobId)
end)
Nexus:AddCommand('mute', function()
if (UGS.MasterVolume - OldVolume) > 0.01 then
OldVolume = UGS.MasterVolume
end
UGS.MasterVolume = 0
end)
Nexus:AddCommand('unmute', function()
UGS.MasterVolume = OldVolume
end)
Nexus:AddCommand('performance', function(Message)
if _PERF then return end
_PERF = true
_TARGETFPS = 15
if Message and tonumber(Message) then
_TARGETFPS = tonumber(Message)
end
local OldLevel = settings().Rendering.QualityLevel
RunService:Set3dRenderingEnabled(false)
settings().Rendering.QualityLevel = 1
game:GetService("ReplicatedFirst"):RemoveDefaultLoadingScreen()
InputService.WindowFocused:Connect(function()
game:GetService("ReplicatedFirst"):RemoveDefaultLoadingScreen()
RunService:Set3dRenderingEnabled(false)
settings().Rendering.QualityLevel = 1
setfpscap(_TARGETFPS)
end)
InputService.WindowFocusReleased:Connect(function()
OldLevel = settings().Rendering.QualityLevel
game:GetService("ReplicatedFirst"):RemoveDefaultLoadingScreen()
RunService:Set3dRenderingEnabled(false)
settings().Rendering.QualityLevel = 1
setfpscap(_TARGETFPS)
end)
setfpscap(_TARGETFPS)
end)
end
do -- Connections
GuiService.ErrorMessageChanged:Connect(function()
if NoShutdown then return end
local Code = GuiService:GetErrorCode().Value
if Code >= Enum.ConnectionError.DisconnectErrors.Value then
if not Nexus.ShutdownOnTeleportError and Code > Enum.ConnectionError.PlacelaunchOtherError.Value then
return
end
task.delay(Nexus.ShutdownTime, game.Shutdown, game)
end
end)
end
local GEnv = getgenv()
GEnv.Nexus = Nexus
GEnv.performance = Nexus.Commands.performance -- fix the sirmeme error so that people stop being annoying saying "omg performance() doesnt work" (https://youtu.be/vVfg9ym2MNs?t=389)
if not Nexus_Version then
Nexus:Connect()
Nexus.Connected:Wait()
end