Skip to content

Commit

Permalink
Decent loader patch, recommended to replace require & removed import
Browse files Browse the repository at this point in the history
  • Loading branch information
Mullets-Gavin committed Dec 19, 2020
1 parent 7682ef4 commit ce5ab31
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 256 deletions.
7 changes: 3 additions & 4 deletions src/DataSync/Methods.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ local Methods = {}
Methods._Occupants = {}
Methods._MaxRetries = 5

local Loader = require(game:GetService('ReplicatedStorage'):WaitForChild('Loader'))
local Manager = Loader('Manager')
local DataStoreService = Loader['DataStoreService']
local require = require(game:GetService('ReplicatedStorage'):WaitForChild('Loader'))
local Manager = require('Manager')
local DataStoreService = game:GetService('DataStoreService')

--[=[
Load DataStore
Expand Down Expand Up @@ -45,7 +45,6 @@ function Methods.LoadData(key: string, index: string, file: table): table & bool
end

if last == nil then
print('LOADED DEEP COPY')
last = Manager.DeepCopy(file)
elseif typeof(last) == 'table' then
for index,value in pairs(file) do
Expand Down
54 changes: 27 additions & 27 deletions src/DataSync/Subscribe.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
]=]

local Subscribe = {}
Subscribe.Cache = {}
Subscribe.All = {}
Subscribe.Remotes = {
Subscribe._Cache = {}
Subscribe._All = {}
Subscribe._Remotes = {
['Download'] = '_DOWNLOAD';
['Upload'] = '_UPLOAD';
['Subscribe'] = '_SUBSCRIBE';
}

local Loader = require(game:GetService('ReplicatedStorage'):WaitForChild('Loader'))
local Manager = Loader('Manager')
local Network = Loader('Network')
local Players = Loader['Players']
local RunService = Loader['RunService']
local require = require(game:GetService('ReplicatedStorage'):WaitForChild('Loader'))
local Manager = require('Manager')
local Network = require('Network')
local Players = game:GetService('Players')
local RunService = game:GetService('RunService')

--[=[
Get a player from an index
Expand Down Expand Up @@ -53,22 +53,22 @@ end
@private
]=]
local function GetCache(key: string, index: string, value: string): table
if not Subscribe.Cache[key] then
Subscribe.Cache[key] = {}
if not Subscribe._Cache[key] then
Subscribe._Cache[key] = {}
end

if not Subscribe.Cache[key][index] then
Subscribe.Cache[key][index] = {}
if not Subscribe._Cache[key][index] then
Subscribe._Cache[key][index] = {}
end

if not Subscribe.Cache[key][index][value] then
Subscribe.Cache[key][index][value] = {
if not Subscribe._Cache[key][index][value] then
Subscribe._Cache[key][index][value] = {
['Clients'] = {};
['Code'] = {};
}
end

return Subscribe.Cache[key][index][value]
return Subscribe._Cache[key][index][value]
end

--[=[
Expand All @@ -80,18 +80,18 @@ end
@private
]=]
local function GetAll(key: string, index: string): table
if not Subscribe.All[key] then
Subscribe.All[key] = {}
if not Subscribe._All[key] then
Subscribe._All[key] = {}
end

if not Subscribe.All[key][index] then
Subscribe.All[key][index] = {
if not Subscribe._All[key][index] then
Subscribe._All[key][index] = {
['Clients'] = {};
['Code'] = {};
}
end

return Subscribe.All[key][index]
return Subscribe._All[key][index]
end

--[=[
Expand All @@ -117,14 +117,14 @@ function Subscribe.FireSubscription(key: any, index: any, value: any, data: any)
for count,client in pairs(cache['Clients']) do
Manager.wrap(function()
table.insert(sent,client)
Network:FireClient(Subscribe.Remotes.Download,client,key,index,value,data)
Network:FireClient(Subscribe._Remotes.Download,client,key,index,value,data)
end)
end

for count,client in pairs(all['Clients']) do
Manager.wrap(function()
if not table.find(sent,client) then
Network:FireClient(Subscribe.Remotes.Download,client,key,index,value,data)
Network:FireClient(Subscribe._Remotes.Download,client,key,index,value,data)
end
end)
end
Expand Down Expand Up @@ -191,14 +191,14 @@ function Subscribe.ConnectSubscription(info: Instance | any, key: any, index: an
end
end

Subscribe.Cache[key][index][value] = cache
Subscribe._Cache[key][index][value] = cache

if all then
Subscribe.All[key][index] = all
Subscribe._All[key][index] = all
end

if Manager.IsClient then
Network:FireServer(Subscribe.Remotes.Subscribe,key,index,value)
Network:FireServer(Subscribe._Remotes.Subscribe,key,index,value)
end
end

Expand Down Expand Up @@ -245,10 +245,10 @@ function Subscribe.DisconnectSubscription(info: Instance | any, key: any, index:
end
end

Subscribe.Cache[key][index][value] = nil
Subscribe._Cache[key][index][value] = nil

if all then
Subscribe.All[key][index] = all
Subscribe._All[key][index] = all
end
end

Expand Down
61 changes: 34 additions & 27 deletions src/DataSync/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
local DataSync = {}
DataSync.__index = DataSync
DataSync._Name = string.upper(script.Name)
DataSync._Error = '['.. DataSync._Name ..']: '
DataSync._ShuttingDown = false
DataSync._Private = '__'
DataSync._Cache = {}
Expand All @@ -95,13 +94,13 @@ DataSync.AutoSaveTimer = 30 -- how often, in seconds, a DataFile autosaves
DataSync.FailProof = true -- kick the player if the datastore failed loading player-based data
DataSync.All = 'all' -- the 'all' variable for streamlining data types

local Loader = require(game:GetService('ReplicatedStorage'):WaitForChild('Loader'))
local Manager = Loader('Manager')
local Network = Loader('Network')
local Methods = Loader(script:WaitForChild('Methods'))
local Subscribe = Loader(script:WaitForChild('Subscribe'))
local Players = Loader['Players']
local RunService = Loader['RunService']
local require = require(game:GetService('ReplicatedStorage'):WaitForChild('Loader'))
local Manager = require('Manager')
local Network = require('Network')
local Methods = require(script:WaitForChild('Methods'))
local Subscribe = require(script:WaitForChild('Subscribe'))
local Players = game:GetService('Players')
local RunService = game:GetService('RunService')

--[=[
Get the Player from either the instance or UserId
Expand Down Expand Up @@ -167,18 +166,18 @@ end
@param filter? boolean -- if true, only save these keys, if false, don't save those keys
]=]
function DataSync:FilterKeys(keys: table, filter: boolean?): typeof(DataSync.GetStore())
assert(self._key,DataSync._Error.."':FilterKeys' can only be used with a store")
assert(self._key,"':FilterKeys' can only be used with a store")

if not DataSync._Defaults[self._key] then
warn(DataSync._Error..'Unable to set filter, no Default Data table found')
warn('Unable to set filter, no Default Data table found')
return self
end

DataSync._Filters[self._key] = {
['Keys'] = keys;
['Type'] = filter and 'Whitelist' or 'Blacklist'
}
print(self._key,DataSync._Filters[self._key])

return self
end

Expand All @@ -189,7 +188,7 @@ end
@return DataFileObject
]=]
function DataSync:GetFile(index: string | number | nil): typeof(DataSync:GetFile())
assert(self._key,DataSync._Error.."':GetFile' can only be used with a store")
assert(self._key,"':GetFile' can only be used with a store")

if not index and Manager.IsClient and Players.LocalPlayer then
index = tostring(Players.LocalPlayer.UserId)
Expand All @@ -214,6 +213,14 @@ function DataSync:GetFile(index: string | number | nil): typeof(DataSync:GetFile
if not DataSync._Cache[self._key][index] and Manager.IsServer and not self._sesh then
self._sesh = true

if not DataSync._Defaults[self._key] then
while not DataSync._Files[index] do
Manager.wait()
end

return DataSync._Files[index]
end

local load,success = Methods.LoadData(self._key,index,DataSync._Defaults[self._key])
if not success then
if load == '__OCCUPIED' or DataSync._Sessions[index] then
Expand All @@ -224,10 +231,10 @@ function DataSync:GetFile(index: string | number | nil): typeof(DataSync:GetFile
return DataSync._Files[index]
end

warn(DataSync._Error..'DataStores are currently down; returning default data')
warn('DataStores are currently down; returning default data')

if player and not Manager.IsStudio and DataSync.FailProof then
player:Kick('\n'.. DataSync._Error ..'DataStores are currently down, please try again later')
player:Kick('\n'..'DataStores are currently down, please try again later')
return nil
end

Expand Down Expand Up @@ -296,7 +303,7 @@ end
@return DataValue | DataFile?
]=]
function DataSync:GetData(value: string | number | nil): any? | table
assert(self._file,DataSync._Error.."':GetData' can only be used with a data file")
assert(self._file,"':GetData' can only be used with a data file")

local file = DataSync._Cache[self._key][self._file]

Expand Down Expand Up @@ -326,7 +333,7 @@ end
@return DataFileObject
]=]
function DataSync:UpdateData(value: string, data: any?): typeof(DataSync:GetFile())
assert(self._file,DataSync._Error.."':UpdateData' can only be used with a data file")
assert(self._file,"':UpdateData' can only be used with a data file")

local file = DataSync._Cache[self._key][self._file]
if data == nil and DataSync._Defaults[self._key][value] ~= nil then
Expand Down Expand Up @@ -372,13 +379,13 @@ end
@return DataFileObject
]=]
function DataSync:IncrementData(value: string, num: number): typeof(DataSync:GetFile())
assert(self._file,DataSync._Error.."':UpdateData' can only be used with a data file")
assert(self._file,"':UpdateData' can only be used with a data file")

local current = self:GetData(value)
if typeof(current) == 'number' then
self:UpdateData(value,current + num)
else
error(DataSync._Error.."':IncrementData' failed, tried to increment a non-number")
error("':IncrementData' failed, tried to increment a non-number")
end

return self
Expand All @@ -390,7 +397,7 @@ end
@return DataFileObject
]=]
function DataSync:SaveData(override: boolean?): typeof(DataSync:GetFile())
assert(self._file,DataSync._Error.."':SaveData' can only be used with a data file")
assert(self._file,"':SaveData' can only be used with a data file")
assert(Manager.IsServer,"':SaveData' only works on the server")

if (DataSync._ShuttingDown and not override) or self._sesh then
Expand Down Expand Up @@ -419,7 +426,7 @@ function DataSync:SaveData(override: boolean?): typeof(DataSync:GetFile())

local load,success = Methods.SaveData(self._key,self._file,clone)
if not success then
warn(DataSync._Error.."!URGENT! Failed to save file '"..self._file.."' on store '"..self._key.."'")
warn("!URGENT! Failed to save file '"..self._file.."' on store '"..self._key.."'")
end

if DataSync._Cache[self._key][self._file] then
Expand All @@ -437,7 +444,7 @@ end
@return DestroyedDataFileObject
]=]
function DataSync:RemoveData(override: boolean?): typeof(DataSync:RemoveData())
assert(self._file,DataSync._Error.."':RemoveData' can only be used with a data file")
assert(self._file,"':RemoveData' can only be used with a data file")
if self._sesh then return self end

if DataSync._ShuttingDown and not override then
Expand Down Expand Up @@ -468,7 +475,7 @@ end
@Return DataFileObject
]=]
function DataSync:WipeData(): typeof(DataSync:GetFile())
assert(self._file,DataSync._Error.."':WipeData' can only be used with a data file")
assert(self._file,"':WipeData' can only be used with a data file")
assert(Manager.IsServer,"':SaveData' only works on the server")

Methods.WipeData(self._key,self._file)
Expand All @@ -484,7 +491,7 @@ end
@param code function -- the function which to be called whenever the value changes
]=]
function DataSync:Subscribe(index: string | number | Player, value: string, code: (any) -> nil, _sent: Player?): typeof(DataSync:Subscribe())
assert(self._key,DataSync._Error.."':Subscribe' can only be used with a store")
assert(self._key,"':Subscribe' can only be used with a store")

local index,player = tostring(GetPlayer(index))
local player = Manager.IsClient and Players.LocalPlayer or Manager.IsServer and _sent
Expand All @@ -509,8 +516,8 @@ end
@return SubscriptionObject
]=]
function DataSync:Unsubscribe(): typeof(DataSync:Unsubscribe())
assert(self._key,DataSync._Error.."':Unsubscribe' can only be used with a store")
assert(self._subscription,DataSync._Error.."':Unsubscribe' can only be used with a subscription created with ':Subscribe'")
assert(self._key,"':Unsubscribe' can only be used with a store")
assert(self._subscription,"':Unsubscribe' can only be used with a subscription created with ':Subscribe'")

if not DataSync._Subscriptions[self._subscription.index .. self._subscription.value] then
return self
Expand All @@ -531,7 +538,7 @@ end
@return nil
]=]
function DataSync:_FireSubscriptions(index: string, value: string, data: any?): nil
assert(self._key,DataSync._Error.."':Subscribe' can only be used with a store")
assert(self._key,"':Subscribe' can only be used with a store")

if string.sub(tostring(tostring(value)),1,#DataSync._Private) == DataSync._Private then
return true
Expand All @@ -549,7 +556,7 @@ if Manager.IsServer then
return
end

print(DataSync._Error..'Shutting down and saving DataSync files')
print('Shutting down and saving DataSync files')

for index,file in pairs(DataSync._Files) do
file:SaveData(true):RemoveData(true)
Expand Down
Loading

0 comments on commit ce5ab31

Please sign in to comment.