Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement limitsets #454

Open
wants to merge 26 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lua/acf/core/globals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ do
return Float
end
end

function ACF.StringDataCallback()
local SettingData = ACF.GetWorkingSetting()
SettingData.Type = "String"

return function(_, Value)
return tostring(Value)
end
end
end

do -- ACF global vars
Expand All @@ -117,6 +126,8 @@ do -- ACF global vars
ACF.ModelData = ACF.ModelData or { Models = {} }

-- General Settings
ACF.DefineSetting("SelectedLimitset", "none", "The current limitset has been set to %s.", ACF.StringDataCallback())

ACF.DefineSetting("AllowAdminData", false, "Admin server data access has been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("RestrictInfo", true, "Entity information restrictions have been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("LegalChecks", true, "Legality checks for ACF entities has been %s.", ACF.BooleanDataCallback())
Expand Down
4 changes: 2 additions & 2 deletions lua/acf/core/networking/data_vars/data_vars_sh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ do -- Data persisting
UpdateData(Key)
end)

hook.Add("Initialize", "ACF Load Persisted Data", function()
hook.Add("ACF_OnLoadAddon", "ACF Load Persisted Data", function()
local Saved = ACF.LoadFromFile(Folder, File)
local SetFunction = ACF["Set" .. Realm .. "Data"]

Expand All @@ -140,7 +140,7 @@ do -- Data persisting
end

hook.Run("ACF_OnLoadPersistedData")
hook.Remove("Initialize", "ACF Load Persisted Data")
hook.Remove("ACF_OnLoadAddon", "ACF Load Persisted Data")
end)
end

Expand Down
7 changes: 5 additions & 2 deletions lua/acf/core/networking/data_vars/data_vars_sv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,18 @@ do -- Data syncronization

if not Data then return end

local Hook = "ACF_OnUpdate" .. Type .. "Data"
local HookUpdate = "ACF_OnUpdate" .. Type .. "Data"
local HookUpload = "ACF_OnUpload" .. Type .. "Data"
marchc1 marked this conversation as resolved.
Show resolved Hide resolved

for K, V in pairs(Data) do
if Values[K] ~= V then
Values[K] = V

hook.Run(Hook, Player, K, V)
hook.Run(HookUpdate, Player, K, V)
end

hook.Run(HookUpload, Player, K, V)

Data[K] = nil
end
end
Expand Down
19 changes: 19 additions & 0 deletions lua/acf/hooks/hooks_sh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ Hooks.Add("ACF_Base_Shared", function(Gamemode)
function Gamemode:ACF_OnUpdateClientData()
end

--- Called when a server data variable value gets uploaded. Is fucntionally equivalent to ACF_OnUploadServerData, but is called after it and is called regardless if the value was truly changed or not.
--- @param Player entity The player that triggered the server data variable change.
--- On the clientside, if the upload was done by the client then this will always be the local player.
--- On the serverside, if the upload was done by the server then this will always be nil.
--- @param Key string The name of the affected server data variable.
--- @param Value any The new value assigned to the server data variable.
function Gamemode:ACF_OnUploadServerData()
end

--- Called when a client data variable value gets uploaded. Is fucntionally equivalent to ACF_OnUploadClientData, but is called after it and is called regardless if the value was truly changed or not.
--- On the clientside, this will be called every time the client uploads something to the data var.
--- This means that this hook can be called multiple times on the same tick for the same data variable.
--- On the serverside, this will be called once per tick when the value gets networked.
--- @param Player entity The player that triggered the client data variable change.
--- @param Key string The name of the affected client data variable.
--- @param Value any The new value assigned to the client data variable.
function Gamemode:ACF_OnUploadClientData()
end

--- Called after the Think hook is called.
--- The only difference with the Think hook are the convenience arguments provided by this one.
--- @param CurTime number Returns the uptime of the server.
Expand Down
91 changes: 91 additions & 0 deletions lua/acf/limitsets/limitset.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
local OldRegistered = ACF.LimitSets and ACF.LimitSets.Registered or nil

local LimitSets = {
Registered = OldRegistered or {},
__PostInitLimitSet = false
}
ACF.LimitSets = LimitSets

if SERVER then
LimitSets.acf_has_limitset_notice_been_shown = CreateConVar("__acf_has_limitset_notice_been_shown", 0, FCVAR_ARCHIVE + FCVAR_REPLICATED + FCVAR_UNREGISTERED, "Internal limitset flag", 0, 1)
end

function LimitSets.Create(Name)
local Object = {
ServerData = {}
}

LimitSets.Registered[Name] = Object

Object.Name = Name

function Object:WithAuthor(Author) self.Author = Author end
function Object:WithDescription(Description) self.Description = Description end

function Object:SetServerData(Key, Value)
self.ServerData[Key] = Value
end

-- This allows hot-reloading
if Name == ACF.ServerData.SelectedLimitset and LimitSets.__PostInitLimitSet then
timer.Simple(0, function()
LimitSets.Execute(Name)
end)
end

return Object
end

function LimitSets.Get(Name)
return LimitSets.Registered[Name]
end

function LimitSets.GetAll()
return table.GetKeys(LimitSets.Registered)
end

function LimitSets.Execute(Name)
if CLIENT then return end
if Name == "none" then

return true, "OK"
end
if not isstring(Name) then return false, "Argument #1 (Name) must be a string." end

local LimitSet = LimitSets.Registered[Name]
if not LimitSet then return false, "No limitset with the name '" .. Name .. "'." end

ACF.Utilities.Messages.PrintLog("Info", "Loading the limitset '" .. Name .. "' by " .. (LimitSet.Author or "<no author>") .. "...")

for Key, Value in pairs(LimitSet.ServerData) do
ACF.SetServerData(Key, Value)
end

return true, "OK"
end

local function UpdateLimitSet()
local SelectedLimitsetName = ACF.ServerData.SelectedLimitset or "none"
local SelectedLimitset = ACF.LimitSets.Registered[SelectedLimitsetName]

if SelectedLimitset then
LimitSets.Execute(SelectedLimitsetName)
else
ACF.Utilities.Messages.PrintLog("Info", "No limitset loaded.")
end

ACF.LimitSets.__PostInitLimitSet = true
end

hook.Add("ACF_OnLoadPersistedData", "ACF_LimitSets_Setup", function()
if CLIENT then return end

UpdateLimitSet()

hook.Add("ACF_OnUploadServerData", "ACF_LimitSets_WatchForKey", function(_, Key, _)
if Key ~= "SelectedLimitset" then return end
UpdateLimitSet()

LimitSets.acf_has_limitset_notice_been_shown:SetBool(true)
end)
end)
Loading
Loading