-
Notifications
You must be signed in to change notification settings - Fork 0
/
GKCore.lua
225 lines (184 loc) · 6.72 KB
/
GKCore.lua
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
--[[
GameKeeper Core
Version 1.0.0 Alpha
Foundation for the GameKeeper structure.
For more information, visit our GitHub at https://github.com/ynox-studios/gamekeeper
Changelog:
Feb 22, 2016:
- Initial writing
]]
local Data = {}
local Utilities = {} do
local GetService do
local Cache = setmetatable({}, {__mode = "v"})
local Service
function GetService(Name)
if not (type(Name) == "string") then
return error("[Utilities.GetService(string Name)] - arg1 is type ".. type(Name), 2)
end
Service = Cache[Name]
if not Service then
local Success = pcall(function() Service = game:GetService(Name) end)
if Success then
Cache[Name] = Service
else
return error("[Utilities.GetService(string Name)] - Service ".. Name .." is not a valid Service.", 2)
end
end
return Service
end
Utilities.GetService = GetService
end
local function Modify(Object, Properties)
if not (type(Object) == "userdata") then
return error("[Utilities.Modify(userdata Object, table Properties)] - arg1 is type ".. type(Object), 2)
end
if not (type(Properties) == "table") then
return error("[Utilities.Modify(userdata Object, table Properties)] - arg2 is type ".. type(Properties), 2)
end
for Key, Value in next, Properties do
Object[Key] = Value
end
return Object
end
Utilities.Modify = Modify
local function Create(ClassName, Properties)
if not (type(ClassName) == "string") then
return error("[Utilities.Create(string ClassName, table Properties)] - arg1 is type ".. type(ClassName), 2)
end
return Modify(Instance.new(ClassName), Properties)
end
Utilities.Create = Create
local function CreateManagedSignal()
local UnmanagedSignal = Instance.new("BindableEvent")
local Arguments = nil
local NumOfArgs = 0
local this = {} do
function this:Fire(...)
Arguments = {...}
NumOfArgs = select("#", ...)
UnmanagedSignal:Fire()
end
function this:Destroy()
UnmanagedSignal:Destroy()
Arguments = nil
NumOfArgs = nil
this = nil
end
local Event = {} do
function Event:connect(Handler, DisconnectOnError)
if not (type(Handler) == "function") then
return error("[ManagedEvent.Event:connect(function Handler, boolean DisconnectOnError)] - arg1 is type ".. type(Handler), 2)
end
if not (type(DisconnectOnError) == "boolean") then
DisconnectOnError = false
end
local Connection do
if (DisconnectOnError) then
Connection = UnmanagedEvent.Event:connect(function()
local Success = pcall(Handler, unpack(Arguments))
if not Success then
Connection:disconnect()
warn("Disconnected ManagedEvent because of exception.")
end
end)
else
Connection = UnmanagedEvent.Event:connect(function()
Handler(unpack(Arguments))
end)
end
end
return Connection
end
function Event:wait()
UnmanagedEvent.Event:wait()
return unpack(Arguments, NumOfArgs)
end
this.Event = Event
end
end
return this
end
Utilities.CreateManagedSignal = CreateManagedSignal
local ManagedConnect do
Data.ManagedConnections = setmetatable({}, {__mode = "v"})
function ManagedConnect(Event,Handler)
local Connection = Event:connect(Handler)
table.insert(Data.ManagedConnections, Connection)
return Connection
end
Utilities.ManagedConnect = ManagedConnect
end
local HttpService = GetService("HttpService")
local function JSONEncode(Table)
if not (type(Table) == "table") then
return error("[Utilities.JSONEncode(table Table)] - arg1 is type ".. type(Table), 2)
end
local Success = pcall(function() Table = HttpService:JSONEncode(Table) end)
if not Success then
return error("[Utilities.JSONEncode(table Table)] - unable to encode table.", 2)
else
return Table
end
end
Utilities.JSONEncode = JSONEncode
local function JSONDecode(String)
if not (type(String) == "string") then
return error("[Utilities.JSONDecode(string Table) = arg1 is type ".. type(String), 2)
end
local Success = pcall(function() String = HttpService:JSONDecode(String) end)
if not Success then
return error("[Utilities.JSONDecode(string String)] - unable to decode string.", 2)
else
return String
end
end
Utilities.JSONDecode = JSONDecode
local function URLEncode(String)
if not (type(String) == "string") then
return error("[Utilities.URLEncode(string String)] - arg1 is type ".. type(String), 2)
end
local Success = pcall(function() String = HttpService:UrlEncode(String) end)
if not Success then
return error("[Utilities.URLEncode(string String)] - unable to encode string.", 2)
else
return String
end
end
Utilities.URLEncode = URLEncode
local RunService = GetService("RunService")
local function GetPeerStatus()
if (RunService:IsServer() and not RunService:IsClient() and RunService:IsStudio()) then
return "StudioServer"
elseif (RunService:IsServer() and not RunService:IsClient() and not RunService:IsStudio()) then
return "OnlineServer"
elseif (not RunService:IsServer() and RunService:IsClient() and RunService:IsStudio()) then
return "StudioClient"
elseif (not RunService:IsServer() and RunService:IsClient() and not RunService:IsStudio()) then
return "OnlineClient"
else
return "Unknown"
end
end
Utilities.GetPeerStatus = GetPeerStatus
end
local System = {} do
local Peer do
if (Utliities.GetPeerStatus():match("Server")) then
Peer = Utilities.GetService("NetworkServer")
elseif (Utilities.GetPeerStatus():match("Client")) then
Peer = Utilities.GetService("NetworkServer")
else
Peer = "Unknown"
end
System.Peer = Peer
end
System.Version = "1.0"
System.BuildId = "afc1eed"
System.DevelopmentBranch = "core"
System.VersionString = System.Version.. " [Build ".. System.BuildId ..":".. System.DevelopmentBranch .."]"
end
local GameKeeperCore = {}
GameKeeperCore.Utilities = Utilities
GameKeeperCore.System = System
return GameKeeperCore