Skip to content

Commit

Permalink
Add precache directive
Browse files Browse the repository at this point in the history
  • Loading branch information
thegrb93 authored Oct 28, 2024
1 parent 70af1fc commit 314a548
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lua/starfall/instance.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,18 @@ function SF.Instance.Compile(code, mainfile, player, entity)
return false, { message = "", traceback = err }
end

local modelsPrecached = 0
for filename, fdata in pairs(ppdata.files) do
if fdata.datafile then continue end -- Don't compile data files
if SERVER then
for _, model in pairs(fdata.precachemodels) do
if modelsPrecached==16 then
return false, { message = "The max precached models is 16!", traceback = "" }
end
modelsPrecached = modelsPrecached + 1
util.PrecacheModel(model)
end
end
if CLIENT and fdata.owneronly and LocalPlayer() ~= player then continue end -- Don't compile owner-only files if not owner
local serverorclient = fdata.serverorclient
if (serverorclient == "server" and CLIENT) or (serverorclient == "client" and SERVER) then continue end -- Don't compile files for other realm
Expand Down
5 changes: 5 additions & 0 deletions lua/starfall/libs_sh/builtins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,11 @@ end
-- @class directive
-- @param model String of the model

--- Precaches models that may take a while to load (max 16). --@precachemodel models/props_junk/watermelon01.mdl
-- @name precachemodel
-- @class directive
-- @param model String of the model to precache

--- Set the current file to only run on the server. Shared is default. --@server
-- @name server
-- @class directive
Expand Down
6 changes: 6 additions & 0 deletions lua/starfall/preprocessor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ SF.PreprocessData = {
self.model = args
end,

precachemodel = function(self, args)
if #args == 0 then return "Empty precachemodel directive" end
self.precachemodels[#self.precachemodels + 1] = args
end,

name = function(self, args) self.scriptname = string.sub(args, 1, 64) end,
author = function(self, args) self.scriptauthor = string.sub(args, 1, 64) end,
server = function(self, args) self.serverorclient = "server" end,
Expand Down Expand Up @@ -80,6 +85,7 @@ SF.PreprocessData = {
includedirs = {},
includesdata = {},
httpincludes = {},
precachemodels = {},
}, t)
end
}
Expand Down

0 comments on commit 314a548

Please sign in to comment.