-
Notifications
You must be signed in to change notification settings - Fork 7
/
Resources.lua
287 lines (226 loc) · 9.71 KB
/
Resources.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
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
-- The core resource manager and library loader for RoStrap
-- It is designed to increase organization and streamline the retrieval and networking of resources.
-- @documentation https://rostrap.github.io/Resources/
-- @source https://github.com/RoStrap/Resources/
-- @author Validark
local RunService = game:GetService("RunService")
local Metatable = {}
local Resources = setmetatable({}, Metatable)
local Caches = {} -- All cached data within Resources is accessible through Resources:GetLocalTable()
local Instance_new, type, require = Instance.new, type, require
local LocalResourcesLocation
local SERVER_SIDE = RunService:IsServer()
local UNINSTANTIABLE_INSTANCES = setmetatable({
Folder = false; RemoteEvent = false; BindableEvent = false;
RemoteFunction = false; BindableFunction = false; Library = true;
}, {
__index = function(self, InstanceType)
local Instantiable, GeneratedInstance = pcall(Instance_new, InstanceType)
local Uninstantiable
if Instantiable and GeneratedInstance then
GeneratedInstance:Destroy()
Uninstantiable = false
else
Uninstantiable = true
end
self[InstanceType] = Uninstantiable
return Uninstantiable
end;
})
function Resources:GetLocalTable(TableName) -- Returns a cached table by TableName, generating if non-existant
TableName = self ~= Resources and self or TableName
local Table = Caches[TableName]
if not Table then
Table = {}
Caches[TableName] = Table
end
return Table
end
local function GetFirstChild(Folder, InstanceName, InstanceType)
local Object = Folder:FindFirstChild(InstanceName)
if not Object then
if UNINSTANTIABLE_INSTANCES[InstanceType] then error("[Resources] " .. InstanceType .. " \"" .. InstanceName .. "\" is not installed within " .. Folder:GetFullName() .. ".", 2) end
Object = Instance_new(InstanceType)
Object.Name = InstanceName
Object.Parent = Folder
end
return Object
end
function Metatable:__index(MethodName)
if type(MethodName) ~= "string" then error("[Resources] Attempt to index Resources with invalid key: string expected, got " .. typeof(MethodName), 2) end
if MethodName:sub(1, 3) ~= "Get" then error("[Resources] Methods should begin with \"Get\"", 2) end
local InstanceType = MethodName:sub(4)
-- Set CacheName to ["RemoteEvent" .. "s"], or ["Librar" .. "ies"]
local a, b = InstanceType:byte(-2, -1) -- this is a simple gimmick but works well enough for all Roblox ClassNames :D
local CacheName = b == 121 and a ~= 97 and a ~= 101 and a ~= 105 and a ~= 111 and a ~= 117 and InstanceType:sub(1, -2) .. "ies" or InstanceType .. "s"
local IsLocal = InstanceType:sub(1, 5) == "Local"
local Cache, Folder, FolderGetter -- Function Constants
if IsLocal then -- Determine whether a method is local
InstanceType = InstanceType:sub(6)
if InstanceType == "Folder" then
FolderGetter = function() return GetFirstChild(LocalResourcesLocation, "Resources", "Folder") end
else
FolderGetter = Resources.GetLocalFolder
end
else
if InstanceType == "Folder" then
FolderGetter = function() return script end
else
FolderGetter = Resources.GetFolder
end
end
local function GetFunction(this, InstanceName)
InstanceName = this ~= self and this or InstanceName
if type(InstanceName) ~= "string" then error("[Resources] " .. MethodName .. " expected a string parameter, got " .. typeof(InstanceName), 2) end
if not Folder then
Cache = Caches[CacheName]
Folder = FolderGetter(IsLocal and CacheName:sub(6) or CacheName)
if not Cache then
Cache = Folder:GetChildren() -- Cache children of Folder into Table
Caches[CacheName] = Cache
for i = 1, #Cache do
local Child = Cache[i]
Cache[Child.Name] = Child
Cache[i] = nil
end
end
end
local Object = Cache[InstanceName]
if not Object then
if SERVER_SIDE or IsLocal then
Object = GetFirstChild(Folder, InstanceName, InstanceType)
else
Object = Folder:WaitForChild(InstanceName, 5)
if not Object then
local Caller = getfenv(0).script
if Caller and Caller.Parent and Caller.Parent.Parent == script then
warn("[Resources] Make sure a Script in ServerScriptService calls `Resources:LoadLibrary(\"" .. Caller.Name .. "\")`")
else
if InstanceType == "Library" then
warn("[Resources] Did you forget to install " .. InstanceName .. "?")
elseif InstanceType == "Folder" then
warn("[Resources] Make sure a Script in ServerScriptService calls `require(ReplicatedStorage:WaitForChild(\"Resources\"))`")
end
end
Object = Folder:WaitForChild(InstanceName)
end
end
Cache[InstanceName] = Object
end
return Object
end
Resources[MethodName] = GetFunction
return GetFunction
end
if not SERVER_SIDE then
local LocalPlayer repeat LocalPlayer = game:GetService("Players").LocalPlayer until LocalPlayer or not wait()
repeat LocalResourcesLocation = LocalPlayer:FindFirstChildOfClass("PlayerScripts") until LocalResourcesLocation or not wait()
else
LocalResourcesLocation = game:GetService("ServerStorage")
local LibraryRepository = LocalResourcesLocation:FindFirstChild("Repository") or game:GetService("ServerScriptService"):FindFirstChild("Repository")
local function CacheLibrary(Storage, Library, StorageName)
if Storage[Library.Name] then
error("[Resources] Duplicate " .. StorageName .. " Found:\n\t"
.. Storage[Library.Name]:GetFullName() .. " and \n\t"
.. Library:GetFullName()
.. "\nOvershadowing is only permitted when a server-only library overshadows a replicated library"
, 0)
else
Storage[Library.Name] = Library
end
end
if LibraryRepository then
-- If Folder `Repository` exists, move all Libraries over to ReplicatedStorage
-- unless if they have "Server" in their name or in the name of a parent folder
local ServerLibraries = {}
local ReplicatedLibraries = Resources:GetLocalTable("Libraries")
local FoldersToHandle = {}
local FolderChildren, ExclusivelyServer = LibraryRepository:GetChildren(), false
while FolderChildren do
FoldersToHandle[FolderChildren] = nil
for i = 1, #FolderChildren do
local Child = FolderChildren[i]
local ClassName = Child.ClassName
local ServerOnly = ExclusivelyServer or (Child.Name:find("Server", 1, true) and true or false)
if ClassName == "ModuleScript" then
if ServerOnly then
Child.Parent = Resources:GetLocalFolder("Libraries")
CacheLibrary(ServerLibraries, Child, "ServerLibraries")
else
-- ModuleScripts which are not descendants of ServerOnly folders and do not have "Server" in name should be moved to Libraries
-- if there are descendants of the ModuleScript with "Server" in the name, we should copy the original for use on the server
-- and replicate a version with everything with "Server" in the name deleted
local ModuleDescendants = Child:GetDescendants()
local TemplateObject
-- Iterate through the ModuleScript's Descendants, deleting those with "Server" in the Name
for j = 1, #ModuleDescendants do
local Descendant = ModuleDescendants[j]
if Descendant.Name:find("Server", 1, true) then
if not TemplateObject then -- Before the first deletion, clone Child
TemplateObject = Child:Clone()
end
Descendant:Destroy()
end
end
if TemplateObject then -- If we want to replicate an object with Server descendants, move the server-version to LocalLibraries
TemplateObject.Parent = Resources:GetLocalFolder("Libraries")
CacheLibrary(ServerLibraries, TemplateObject, "ServerLibraries")
end
Child.Parent = Resources:GetFolder("Libraries") -- Replicate Child which may have had things deleted
CacheLibrary(ReplicatedLibraries, Child, "ReplicatedLibraries")
end
elseif ClassName == "Folder" then
FoldersToHandle[Child:GetChildren()] = ServerOnly
else
error("[Resources] Instances within your Repository must be either a ModuleScript or a Folder, found: " .. ClassName .. " " .. Child:GetFullName(), 0)
end
end
FolderChildren, ExclusivelyServer = next(FoldersToHandle)
end
for Name, Library in next, ServerLibraries do
ReplicatedLibraries[Name] = Library
end
LibraryRepository:Destroy()
end
end
local LoadedLibraries = Resources:GetLocalTable("LoadedLibraries")
local CurrentlyLoading = {} -- This is a hash which LoadLibrary uses as a kind of linked-list history of [Script who Loaded] -> Library
function Resources:LoadLibrary(LibraryName)
LibraryName = self ~= Resources and self or LibraryName
local Data = LoadedLibraries[LibraryName]
if Data == nil then
local Caller = getfenv(0).script or {Name = "Command bar"} -- If called from command bar, use table as a reference (never concatenated)
local Library = Resources:GetLibrary(LibraryName)
CurrentlyLoading[Caller] = Library
-- Check to see if this case occurs:
-- Library -> Stuff1 -> Stuff2 -> Library
-- WHERE CurrentlyLoading[Library] is Stuff1
-- and CurrentlyLoading[Stuff1] is Stuff2
-- and CurrentlyLoading[Stuff2] is Library
local Current = Library
local Count = 0
while Current do
Count = Count + 1
Current = CurrentlyLoading[Current]
if Current == Library then
local String = Current.Name -- Get the string traceback
for _ = 1, Count do
Current = CurrentlyLoading[Current]
String = String .. " -> " .. Current.Name
end
error("[Resources] Circular dependency chain detected: " .. String)
end
end
Data = require(Library)
if CurrentlyLoading[Caller] == Library then -- Thread-safe cleanup!
CurrentlyLoading[Caller] = nil
end
if Data == nil then
error("[Resources] " .. LibraryName .. " must return a non-nil value. Return false instead.")
end
LoadedLibraries[LibraryName] = Data -- Cache by name for subsequent calls
end
return Data
end
Metatable.__call = Resources.LoadLibrary
return Resources