Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SSTRP = SSTRP or {}
- SSTRP.Workshop = SSTRP.Workshop or {}
- SSTRP.Workshop.Items = {}
- SSTRP.Workshop.Subscribed = SSTRP.Workshop.Subscribed or {}
- function SSTRP.Workshop.GetPublishedFileDetails(workshopid, callback)
- local req = {}
- req.method = "POST"
- req.url = "https://api.steampowered.com/ISteamRemoteStorage/GetPublishedFileDetails/v1"
- req.body = "format=json&itemcount=1&publishedfileids[0]=" .. workshopid
- req.type = "application/x-www-form-urlencoded"
- req.success = function (code, body)
- callback(code, body)
- end
- req.failed = function (reason)
- callback(nil, reason)
- end
- HTTP(req)
- end
- function SSTRP.Workshop.ParseURL(s)
- --https://steamcommunity.com/sharedfiles/filedetails/?id=1663048567
- local out = {}
- s = string.Trim(s)
- local idx = s:find("?")
- if !idx then
- return {}
- end
- local substr = s:sub(idx + 1)
- local pairsets = substr:split("&")
- for k, v in pairs(pairsets) do
- local kvp = v:split("=")
- out[kvp[1]] = kvp[2]
- end
- return out
- end
- if CLIENT then
- net.Receive("SSTRP_Workshop_Subscribe", function (len)
- local itm = net.ReadTable()
- local wsid = itm.WorkshopId
- if !SSTRP.Workshop.Subscribed[wsid] then
- ix.util.Notify("(Client) Downloading '" .. itm.Title .. "' from the workshop...")
- timer.Simple(1.0, function ()
- steamworks.FileInfo(wsid, function (r)
- steamworks.Download(r.fileid, true, function (name)
- ix.util.Notify("(Client) Download completed. Mounting (this can lag)...")
- success, files = game.MountGMA(name)
- if success then
- SSTRP.Workshop.Subscribed[wsid] = true
- ix.util.Notify("(Client) '" .. itm.Title .. "' successfully mounted.")
- else
- ix.util.Notify("(Client) '" .. itm.Title .. "': error occurred whilst mounting.")
- end
- end)
- end)
- end)
- else
- print("Skipping download of addon ID " .. wsid)
- end
- end)
- net.Receive("SSTRP_Workshop_Sync", function (len)
- local workshopId = net.ReadString()
- local data = net.ReadTable()
- SSTRP.Workshop.Items[workshopId] = data
- if SSTRP.Workshop.MainPanel and SSTRP.Workshop.MainPanel.DrawItemList then
- SSTRP.Workshop.MainPanel:DrawItemList()
- end
- if (SSTRP.Workshop.PopulatedSpawnMenu and data.SpawnList ~= nil) then
- print("Adding spawn menu for " .. data.Title)
- RunConsoleCommand("spawnmenu_reload")
- end
- print(string.format("Received data [%s]: %s", workshopId, data))
- end)
- net.Receive("SSTRP_Workshop_Remove", function (len)
- local workshopId = net.ReadString()
- if SSTRP.Workshop.Items[workshopId] then
- SSTRP.Workshop.Items[workshopId] = nil
- if SSTRP.Workshop.MainPanel and SSTRP.Workshop.MainPanel.DrawItemList then
- SSTRP.Workshop.MainPanel:DrawItemList()
- end
- end
- end)
- hook.Add("PopulatePropMenu", "SSTRP_Workshop_PopulateSpawnMenu", function ()
- for k, data in pairs(SSTRP.Workshop.Items) do
- if (data.SpawnList ~= nil) then
- spawnmenu.AddPropCategory("WS_" .. data.WorkshopId, data.Title, data.SpawnList, "icon16/plugin.png")
- end
- end
- SSTRP.Workshop.PopulatedSpawnMenu = true
- end)
- end
- if SERVER then
- util.AddNetworkString("SSTRP_Workshop_Sync")
- util.AddNetworkString("SSTRP_Workshop_Remove")
- util.AddNetworkString("SSTRP_Workshop_Subscribe")
- hook.Add("PlayerInitialSpawn", "SSTRP_Workshop_InitialSpawn", function (ply)
- for k, v in pairs(SSTRP.Workshop.Items) do
- SSTRP.Workshop.Transmit(v, ply)
- end
- end)
- function SSTRP.Workshop.Load()
- if file.Exists("workshop_addons.dat", "DATA") then
- local items = util.JSONToTable(file.Read("workshop_addons.dat"))
- for k, v in pairs(items) do
- v.Defer = false
- SSTRP.Workshop.Items[tostring(k)] = v
- if (v.Enabled) then
- --if (v.Server) then
- --SSTRP.Workshop.Subscribe(v)
- --end
- if (v.Client) then
- resource.AddWorkshop(v.WorkshopId)
- --SSTRP.Workshop.BroadcastSubscribe(v)
- end
- end
- SSTRP.Workshop.Transmit(v)
- print(string.format("LOADED WORKSHOP ADDON [%s]: %s", v.WorkshopId, v))
- end
- SSTRP.Workshop.Save()
- end
- end
- function SSTRP.Workshop.Save()
- file.Write("workshop_addons.dat", util.TableToJSON(SSTRP.Workshop.Items, true))
- end
- function SSTRP.Workshop.Add(workshopid, title, server, client, defer, enabled)
- workshopid = string.Trim(tostring(workshopid))
- local itm = {}
- itm.WorkshopId = workshopid
- itm.Title = title
- itm.Server = server
- itm.Client = client
- itm.Defer = defer
- itm.Enabled = enabled
- SSTRP.Workshop.Items[workshopid] = itm
- SSTRP.Workshop.Transmit(itm)
- if (itm.Enabled) then
- SSTRP.Workshop.Subscribe(itm)
- if (itm.Client and !itm.Defer) then
- SSTRP.Workshop.BroadcastSubscribe(itm)
- resource.AddWorkshop(workshopid)
- end
- end
- SSTRP.Workshop.Save()
- end
- function SSTRP.Workshop.Subscribe(item)
- local wsid = item.WorkshopId
- ix.util.Notify("Subscribing server to '" .. item.Title .. "'")
- SSTRP.Workshop.GetPublishedFileDetails(wsid, function (code, body)
- if (!code) then
- print("An error occurred whilst polling the Steam Workshop for " .. wsid)
- return
- end
- local data = util.JSONToTable(body).response.publishedfiledetails[1]
- local url = data["file_url"]
- http.Fetch(url, function (body, size)
- if (!file.Exists("addons", "DATA")) then
- file.CreateDir("addons")
- end
- file.Write("addons/" .. wsid .. ".gma.dat", util.Decompress(body))
- local success, files = game.MountGMA("data/addons/" .. wsid .. ".gma.dat")
- if (success) then
- ix.util.Notify("Successfully subscribed to '" .. item.Title .. "'")
- local contents = {}
- for k, v in pairs(files) do
- if string.EndsWith(v, ".mdl") then
- table.insert(contents,
- {
- type = "model",
- model = v
- })
- end
- if (string.EndsWith(v, ".bsp")) then
- item.Maps = item.Maps or {}
- item.Maps[v:gsub(".bsp", ""):gsub("maps/", "")] = true
- end
- end
- if (#contents > 0) then
- item.SpawnList = contents
- end
- -- Retransmit the item (spawnlists)
- SSTRP.Workshop.Transmit(item)
- end
- end)
- end)
- end
- function SSTRP.Workshop.BroadcastSubscribe(item)
- -- Transmit it to players
- ix.util.Notify("Subscribing clients to '" .. item.Title .. "'")
- net.Start("SSTRP_Workshop_Subscribe")
- net.WriteTable(item)
- net.Broadcast()
- end
- function SSTRP.Workshop.Remove(workshopid)
- workshopid = string.Trim(tostring(workshopid))
- if (SSTRP.Workshop.Items[workshopid]) then
- SSTRP.Workshop.Items[workshopid] = nil
- -- Transmit it to players
- net.Start("SSTRP_Workshop_Remove")
- net.WriteString(workshopid)
- net.Broadcast()
- local path = "data/addons/" .. workshopid .. ".gma.dat"
- if (file.Exists(path, "DATA")) then
- file.Delete(path)
- end
- SSTRP.Workshop.Save()
- end
- end
- function SSTRP.Workshop.Transmit(item, ply)
- -- Transmit it to players
- net.Start("SSTRP_Workshop_Sync")
- net.WriteString(item.WorkshopId)
- net.WriteTable(item)
- if !ply then
- net.Broadcast()
- else
- net.Send(ply)
- end
- end
- -- sstrp_workshop_add url serverside clientside restart active
- concommand.Add("sstrp_workshop_add", function (client, cmd, args)
- local url = args[1]
- local serverside = tonumber(args[2]) == 1
- local clientside = tonumber(args[3]) == 1
- local defer = tonumber(args[4]) == 1
- local enabled = tonumber(args[5]) == 1
- if !url then
- client:PrintMessage(HUD_PRINTTALK, "Missing arguments.")
- return
- end
- if !client:IsSuperAdmin() then
- client:PrintMessage(HUD_PRINTTALK, "You must be an admin to use this command.")
- return
- end
- local params = SSTRP.Workshop.ParseURL(url)
- if (!params.id) then
- client:PrintMessage(HUD_PRINTTALK, "Invalid workshop URL.")
- return
- end
- if (SSTRP.Workshop.Items[params.id] ~= nil) then
- client:PrintMessage(HUD_PRINTTALK, "The server is already subscribed to this addon.")
- return
- end
- SSTRP.Workshop.GetPublishedFileDetails(params.id, function (code, body)
- if (!code) then
- client:PrintMessage(HUD_PRINTTALK, "An error occurred whilst polling the Steam Workshop. Is this a valid workshop link?")
- return
- end
- local tbl = util.JSONToTable(body).response.publishedfiledetails[1]
- if (tonumber(tbl.creator_app_id) ~= 4000) then
- client:PrintMessage(HUD_PRINTTALK, "This isn't a Garry's Mod workshop addon!")
- return
- end
- SSTRP.Workshop.Add(string.Trim(params.id), tbl.title, serverside, clientside, defer, enabled)
- end)
- end)
- concommand.Add("sstrp_workshop_delete", function (client, cmd, args)
- local url = tostring(args[1])
- if !url then
- client:PrintMessage(HUD_PRINTTALK, "Missing arguments.")
- return
- end
- if !client:IsSuperAdmin() then
- client:PrintMessage(HUD_PRINTTALK, "You must be an admin to use this command.")
- return
- end
- local itm = SSTRP.Workshop.Items[url]
- if itm == nil then
- client:PrintMessage(HUD_PRINTTALK, "No addon has been enabled with the workshop ID " .. url)
- return
- end
- SSTRP.Workshop.Remove(url)
- end)
- concommand.Add("sstrp_workshop_setserverside", function (client, cmd, args)
- local url = args[1]
- local value = args[2] and tonumber(args[2]) == 1
- if !url or value == nil then
- client:PrintMessage(HUD_PRINTTALK, "Missing arguments.")
- return
- end
- if !client:IsSuperAdmin() then
- client:PrintMessage(HUD_PRINTTALK, "You must be an admin to use this command.")
- return
- end
- local itm = SSTRP.Workshop.Items[url]
- if !itm then
- client:PrintMessage(HUD_PRINTTALK, "No addon has been enabled with the workshop ID " .. url)
- return
- end
- if (itm.Enabled and !itm.Defer and itm.Server ~= value and value == true) then
- SSTRP.Workshop.Subscribe(itm)
- end
- itm.Server = value
- SSTRP.Workshop.Transmit(itm)
- SSTRP.Workshop.Save()
- end)
- concommand.Add("sstrp_workshop_setclientside", function (client, cmd, args)
- local url = args[1]
- local value = args[2] and tonumber(args[2]) == 1
- if !url or value == nil then
- client:PrintMessage(HUD_PRINTTALK, "Missing arguments.")
- return
- end
- if !client:IsSuperAdmin() then
- client:PrintMessage(HUD_PRINTTALK, "You must be an admin to use this command.")
- return
- end
- local itm = SSTRP.Workshop.Items[url]
- if !itm then
- client:PrintMessage(HUD_PRINTTALK, "No addon has been enabled with the workshop ID " .. url)
- return
- end
- if (itm.Enabled and !itm.Defer and itm.Client ~= value and value == true) then
- SSTRP.Workshop.BroadcastSubscribe(itm)
- end
- itm.Client = value
- SSTRP.Workshop.Transmit(itm)
- SSTRP.Workshop.Save()
- end)
- concommand.Add("sstrp_workshop_setdeferred", function (client, cmd, args)
- local url = args[1]
- local value = args[2] and tonumber(args[2]) == 1
- if !url or value == nil then
- client:PrintMessage(HUD_PRINTTALK, "Missing arguments.")
- return
- end
- if !client:IsSuperAdmin() then
- client:PrintMessage(HUD_PRINTTALK, "You must be an admin to use this command.")
- return
- end
- local itm = SSTRP.Workshop.Items[url]
- if !itm then
- client:PrintMessage(HUD_PRINTTALK, "No addon has been enabled with the workshop ID " .. url)
- return
- end
- if (itm.Enabled and itm.Defer ~= value and value == false) then
- if itm.Server then
- SSTRP.Workshop.Subscribe(itm)
- end
- if itm.Client then
- SSTRP.Workshop.BroadcastSubscribe(itm)
- end
- end
- itm.Defer = value
- SSTRP.Workshop.Transmit(itm)
- SSTRP.Workshop.Save()
- end)
- concommand.Add("sstrp_workshop_setenabled", function (client, cmd, args)
- local url = args[1]
- local value = args[2] and tonumber(args[2]) == 1
- if !url or value == nil then
- client:PrintMessage(HUD_PRINTTALK, "Missing arguments.")
- return
- end
- if !client:IsSuperAdmin() then
- client:PrintMessage(HUD_PRINTTALK, "You must be an admin to use this command.")
- return
- end
- local itm = SSTRP.Workshop.Items[url]
- if !itm then
- client:PrintMessage(HUD_PRINTTALK, "No addon has been enabled with the workshop ID " .. url)
- return
- end
- if (!itm.Defer and itm.Enabled ~= value and value == true) then
- if itm.Server then
- SSTRP.Workshop.Subscribe(itm)
- end
- if itm.Client then
- SSTRP.Workshop.BroadcastSubscribe(itm)
- end
- end
- itm.Enabled = value
- SSTRP.Workshop.Transmit(itm)
- SSTRP.Workshop.Save()
- end)
- concommand.Add("sstrp_workshop_restart_server", function (client, cmd, args)
- if !client:IsSuperAdmin() then
- client:PrintMessage(HUD_PRINTTALK, "You must be an admin to use this command.")
- return
- end
- ix.util.Notify(client:Name() .. " has forced a server restart.")
- timer.Simple(2.0, function ()
- Entity(1):GetAnimInfo(-99999999)
- end)
- end)
- timer.Simple(0.5, SSTRP.Workshop.Load)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement