Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.83 KB | None | 0 0
  1. SSTRP = SSTRP or {}
  2.  
  3. SSTRP.Workshop = SSTRP.Workshop or {}
  4. SSTRP.Workshop.Items = {}
  5. SSTRP.Workshop.Subscribed = SSTRP.Workshop.Subscribed or {}
  6.  
  7.  
  8. function SSTRP.Workshop.GetPublishedFileDetails(workshopid, callback)
  9.  
  10. local req = {}
  11. req.method = "POST"
  12. req.url = "https://api.steampowered.com/ISteamRemoteStorage/GetPublishedFileDetails/v1"
  13. req.body = "format=json&itemcount=1&publishedfileids[0]=" .. workshopid
  14. req.type = "application/x-www-form-urlencoded"
  15.  
  16. req.success = function (code, body)
  17. callback(code, body)
  18. end
  19. req.failed = function (reason)
  20. callback(nil, reason)
  21. end
  22. HTTP(req)
  23.  
  24. end
  25.  
  26. function SSTRP.Workshop.ParseURL(s)
  27. --https://steamcommunity.com/sharedfiles/filedetails/?id=1663048567
  28. local out = {}
  29.  
  30. s = string.Trim(s)
  31. local idx = s:find("?")
  32. if !idx then
  33. return {}
  34. end
  35.  
  36. local substr = s:sub(idx + 1)
  37. local pairsets = substr:split("&")
  38. for k, v in pairs(pairsets) do
  39. local kvp = v:split("=")
  40. out[kvp[1]] = kvp[2]
  41. end
  42. return out
  43. end
  44.  
  45. if CLIENT then
  46. net.Receive("SSTRP_Workshop_Subscribe", function (len)
  47. local itm = net.ReadTable()
  48. local wsid = itm.WorkshopId
  49.  
  50. if !SSTRP.Workshop.Subscribed[wsid] then
  51. ix.util.Notify("(Client) Downloading '" .. itm.Title .. "' from the workshop...")
  52.  
  53. timer.Simple(1.0, function ()
  54. steamworks.FileInfo(wsid, function (r)
  55. steamworks.Download(r.fileid, true, function (name)
  56. ix.util.Notify("(Client) Download completed. Mounting (this can lag)...")
  57. success, files = game.MountGMA(name)
  58.  
  59. if success then
  60. SSTRP.Workshop.Subscribed[wsid] = true
  61. ix.util.Notify("(Client) '" .. itm.Title .. "' successfully mounted.")
  62. else
  63. ix.util.Notify("(Client) '" .. itm.Title .. "': error occurred whilst mounting.")
  64. end
  65. end)
  66. end)
  67. end)
  68. else
  69. print("Skipping download of addon ID " .. wsid)
  70. end
  71. end)
  72.  
  73. net.Receive("SSTRP_Workshop_Sync", function (len)
  74. local workshopId = net.ReadString()
  75. local data = net.ReadTable()
  76.  
  77. SSTRP.Workshop.Items[workshopId] = data
  78.  
  79. if SSTRP.Workshop.MainPanel and SSTRP.Workshop.MainPanel.DrawItemList then
  80. SSTRP.Workshop.MainPanel:DrawItemList()
  81. end
  82.  
  83. if (SSTRP.Workshop.PopulatedSpawnMenu and data.SpawnList ~= nil) then
  84.  
  85. print("Adding spawn menu for " .. data.Title)
  86. RunConsoleCommand("spawnmenu_reload")
  87.  
  88. end
  89.  
  90. print(string.format("Received data [%s]: %s", workshopId, data))
  91. end)
  92.  
  93. net.Receive("SSTRP_Workshop_Remove", function (len)
  94. local workshopId = net.ReadString()
  95. if SSTRP.Workshop.Items[workshopId] then
  96. SSTRP.Workshop.Items[workshopId] = nil
  97.  
  98. if SSTRP.Workshop.MainPanel and SSTRP.Workshop.MainPanel.DrawItemList then
  99. SSTRP.Workshop.MainPanel:DrawItemList()
  100. end
  101. end
  102. end)
  103.  
  104. hook.Add("PopulatePropMenu", "SSTRP_Workshop_PopulateSpawnMenu", function ()
  105. for k, data in pairs(SSTRP.Workshop.Items) do
  106. if (data.SpawnList ~= nil) then
  107. spawnmenu.AddPropCategory("WS_" .. data.WorkshopId, data.Title, data.SpawnList, "icon16/plugin.png")
  108. end
  109. end
  110. SSTRP.Workshop.PopulatedSpawnMenu = true
  111. end)
  112. end
  113.  
  114. if SERVER then
  115.  
  116. util.AddNetworkString("SSTRP_Workshop_Sync")
  117. util.AddNetworkString("SSTRP_Workshop_Remove")
  118. util.AddNetworkString("SSTRP_Workshop_Subscribe")
  119.  
  120. hook.Add("PlayerInitialSpawn", "SSTRP_Workshop_InitialSpawn", function (ply)
  121. for k, v in pairs(SSTRP.Workshop.Items) do
  122. SSTRP.Workshop.Transmit(v, ply)
  123. end
  124. end)
  125.  
  126. function SSTRP.Workshop.Load()
  127. if file.Exists("workshop_addons.dat", "DATA") then
  128. local items = util.JSONToTable(file.Read("workshop_addons.dat"))
  129.  
  130. for k, v in pairs(items) do
  131. v.Defer = false
  132.  
  133. SSTRP.Workshop.Items[tostring(k)] = v
  134.  
  135. if (v.Enabled) then
  136. --if (v.Server) then
  137. --SSTRP.Workshop.Subscribe(v)
  138. --end
  139.  
  140. if (v.Client) then
  141. resource.AddWorkshop(v.WorkshopId)
  142. --SSTRP.Workshop.BroadcastSubscribe(v)
  143. end
  144. end
  145.  
  146. SSTRP.Workshop.Transmit(v)
  147.  
  148. print(string.format("LOADED WORKSHOP ADDON [%s]: %s", v.WorkshopId, v))
  149. end
  150. SSTRP.Workshop.Save()
  151. end
  152. end
  153.  
  154. function SSTRP.Workshop.Save()
  155. file.Write("workshop_addons.dat", util.TableToJSON(SSTRP.Workshop.Items, true))
  156. end
  157.  
  158. function SSTRP.Workshop.Add(workshopid, title, server, client, defer, enabled)
  159.  
  160. workshopid = string.Trim(tostring(workshopid))
  161.  
  162. local itm = {}
  163. itm.WorkshopId = workshopid
  164. itm.Title = title
  165. itm.Server = server
  166. itm.Client = client
  167. itm.Defer = defer
  168. itm.Enabled = enabled
  169. SSTRP.Workshop.Items[workshopid] = itm
  170.  
  171. SSTRP.Workshop.Transmit(itm)
  172.  
  173. if (itm.Enabled) then
  174. SSTRP.Workshop.Subscribe(itm)
  175.  
  176. if (itm.Client and !itm.Defer) then
  177. SSTRP.Workshop.BroadcastSubscribe(itm)
  178. resource.AddWorkshop(workshopid)
  179. end
  180. end
  181.  
  182. SSTRP.Workshop.Save()
  183. end
  184.  
  185. function SSTRP.Workshop.Subscribe(item)
  186.  
  187. local wsid = item.WorkshopId
  188.  
  189. ix.util.Notify("Subscribing server to '" .. item.Title .. "'")
  190. SSTRP.Workshop.GetPublishedFileDetails(wsid, function (code, body)
  191. if (!code) then
  192. print("An error occurred whilst polling the Steam Workshop for " .. wsid)
  193. return
  194. end
  195.  
  196. local data = util.JSONToTable(body).response.publishedfiledetails[1]
  197.  
  198. local url = data["file_url"]
  199.  
  200. http.Fetch(url, function (body, size)
  201. if (!file.Exists("addons", "DATA")) then
  202. file.CreateDir("addons")
  203. end
  204. file.Write("addons/" .. wsid .. ".gma.dat", util.Decompress(body))
  205.  
  206. local success, files = game.MountGMA("data/addons/" .. wsid .. ".gma.dat")
  207.  
  208. if (success) then
  209. ix.util.Notify("Successfully subscribed to '" .. item.Title .. "'")
  210.  
  211. local contents = {}
  212. for k, v in pairs(files) do
  213. if string.EndsWith(v, ".mdl") then
  214. table.insert(contents,
  215. {
  216. type = "model",
  217. model = v
  218. })
  219. end
  220.  
  221. if (string.EndsWith(v, ".bsp")) then
  222. item.Maps = item.Maps or {}
  223. item.Maps[v:gsub(".bsp", ""):gsub("maps/", "")] = true
  224. end
  225. end
  226.  
  227. if (#contents > 0) then
  228. item.SpawnList = contents
  229. end
  230.  
  231. -- Retransmit the item (spawnlists)
  232. SSTRP.Workshop.Transmit(item)
  233. end
  234. end)
  235. end)
  236.  
  237. end
  238.  
  239. function SSTRP.Workshop.BroadcastSubscribe(item)
  240.  
  241. -- Transmit it to players
  242. ix.util.Notify("Subscribing clients to '" .. item.Title .. "'")
  243. net.Start("SSTRP_Workshop_Subscribe")
  244. net.WriteTable(item)
  245. net.Broadcast()
  246.  
  247. end
  248.  
  249.  
  250. function SSTRP.Workshop.Remove(workshopid)
  251.  
  252. workshopid = string.Trim(tostring(workshopid))
  253. if (SSTRP.Workshop.Items[workshopid]) then
  254. SSTRP.Workshop.Items[workshopid] = nil
  255.  
  256. -- Transmit it to players
  257. net.Start("SSTRP_Workshop_Remove")
  258. net.WriteString(workshopid)
  259. net.Broadcast()
  260.  
  261. local path = "data/addons/" .. workshopid .. ".gma.dat"
  262. if (file.Exists(path, "DATA")) then
  263. file.Delete(path)
  264. end
  265.  
  266. SSTRP.Workshop.Save()
  267. end
  268.  
  269. end
  270.  
  271. function SSTRP.Workshop.Transmit(item, ply)
  272.  
  273. -- Transmit it to players
  274. net.Start("SSTRP_Workshop_Sync")
  275. net.WriteString(item.WorkshopId)
  276. net.WriteTable(item)
  277.  
  278. if !ply then
  279. net.Broadcast()
  280. else
  281. net.Send(ply)
  282. end
  283. end
  284.  
  285. -- sstrp_workshop_add url serverside clientside restart active
  286. concommand.Add("sstrp_workshop_add", function (client, cmd, args)
  287. local url = args[1]
  288. local serverside = tonumber(args[2]) == 1
  289. local clientside = tonumber(args[3]) == 1
  290. local defer = tonumber(args[4]) == 1
  291. local enabled = tonumber(args[5]) == 1
  292.  
  293. if !url then
  294. client:PrintMessage(HUD_PRINTTALK, "Missing arguments.")
  295. return
  296. end
  297.  
  298. if !client:IsSuperAdmin() then
  299. client:PrintMessage(HUD_PRINTTALK, "You must be an admin to use this command.")
  300. return
  301. end
  302.  
  303. local params = SSTRP.Workshop.ParseURL(url)
  304. if (!params.id) then
  305. client:PrintMessage(HUD_PRINTTALK, "Invalid workshop URL.")
  306. return
  307. end
  308.  
  309. if (SSTRP.Workshop.Items[params.id] ~= nil) then
  310. client:PrintMessage(HUD_PRINTTALK, "The server is already subscribed to this addon.")
  311. return
  312. end
  313.  
  314. SSTRP.Workshop.GetPublishedFileDetails(params.id, function (code, body)
  315. if (!code) then
  316. client:PrintMessage(HUD_PRINTTALK, "An error occurred whilst polling the Steam Workshop. Is this a valid workshop link?")
  317. return
  318. end
  319.  
  320. local tbl = util.JSONToTable(body).response.publishedfiledetails[1]
  321. if (tonumber(tbl.creator_app_id) ~= 4000) then
  322. client:PrintMessage(HUD_PRINTTALK, "This isn't a Garry's Mod workshop addon!")
  323. return
  324. end
  325.  
  326. SSTRP.Workshop.Add(string.Trim(params.id), tbl.title, serverside, clientside, defer, enabled)
  327. end)
  328.  
  329.  
  330. end)
  331.  
  332. concommand.Add("sstrp_workshop_delete", function (client, cmd, args)
  333. local url = tostring(args[1])
  334.  
  335. if !url then
  336. client:PrintMessage(HUD_PRINTTALK, "Missing arguments.")
  337. return
  338. end
  339.  
  340. if !client:IsSuperAdmin() then
  341. client:PrintMessage(HUD_PRINTTALK, "You must be an admin to use this command.")
  342. return
  343. end
  344.  
  345. local itm = SSTRP.Workshop.Items[url]
  346. if itm == nil then
  347. client:PrintMessage(HUD_PRINTTALK, "No addon has been enabled with the workshop ID " .. url)
  348. return
  349. end
  350.  
  351. SSTRP.Workshop.Remove(url)
  352. end)
  353.  
  354. concommand.Add("sstrp_workshop_setserverside", function (client, cmd, args)
  355. local url = args[1]
  356. local value = args[2] and tonumber(args[2]) == 1
  357.  
  358. if !url or value == nil then
  359. client:PrintMessage(HUD_PRINTTALK, "Missing arguments.")
  360. return
  361. end
  362.  
  363. if !client:IsSuperAdmin() then
  364. client:PrintMessage(HUD_PRINTTALK, "You must be an admin to use this command.")
  365. return
  366. end
  367.  
  368. local itm = SSTRP.Workshop.Items[url]
  369. if !itm then
  370. client:PrintMessage(HUD_PRINTTALK, "No addon has been enabled with the workshop ID " .. url)
  371. return
  372. end
  373.  
  374. if (itm.Enabled and !itm.Defer and itm.Server ~= value and value == true) then
  375. SSTRP.Workshop.Subscribe(itm)
  376. end
  377.  
  378. itm.Server = value
  379. SSTRP.Workshop.Transmit(itm)
  380. SSTRP.Workshop.Save()
  381. end)
  382.  
  383. concommand.Add("sstrp_workshop_setclientside", function (client, cmd, args)
  384. local url = args[1]
  385. local value = args[2] and tonumber(args[2]) == 1
  386.  
  387. if !url or value == nil then
  388. client:PrintMessage(HUD_PRINTTALK, "Missing arguments.")
  389. return
  390. end
  391.  
  392. if !client:IsSuperAdmin() then
  393. client:PrintMessage(HUD_PRINTTALK, "You must be an admin to use this command.")
  394. return
  395. end
  396.  
  397. local itm = SSTRP.Workshop.Items[url]
  398. if !itm then
  399. client:PrintMessage(HUD_PRINTTALK, "No addon has been enabled with the workshop ID " .. url)
  400. return
  401. end
  402.  
  403. if (itm.Enabled and !itm.Defer and itm.Client ~= value and value == true) then
  404. SSTRP.Workshop.BroadcastSubscribe(itm)
  405. end
  406.  
  407. itm.Client = value
  408. SSTRP.Workshop.Transmit(itm)
  409. SSTRP.Workshop.Save()
  410. end)
  411.  
  412. concommand.Add("sstrp_workshop_setdeferred", function (client, cmd, args)
  413. local url = args[1]
  414. local value = args[2] and tonumber(args[2]) == 1
  415.  
  416. if !url or value == nil then
  417. client:PrintMessage(HUD_PRINTTALK, "Missing arguments.")
  418. return
  419. end
  420.  
  421. if !client:IsSuperAdmin() then
  422. client:PrintMessage(HUD_PRINTTALK, "You must be an admin to use this command.")
  423. return
  424. end
  425.  
  426. local itm = SSTRP.Workshop.Items[url]
  427. if !itm then
  428. client:PrintMessage(HUD_PRINTTALK, "No addon has been enabled with the workshop ID " .. url)
  429. return
  430. end
  431.  
  432. if (itm.Enabled and itm.Defer ~= value and value == false) then
  433. if itm.Server then
  434. SSTRP.Workshop.Subscribe(itm)
  435. end
  436.  
  437. if itm.Client then
  438. SSTRP.Workshop.BroadcastSubscribe(itm)
  439. end
  440. end
  441.  
  442. itm.Defer = value
  443. SSTRP.Workshop.Transmit(itm)
  444. SSTRP.Workshop.Save()
  445. end)
  446.  
  447. concommand.Add("sstrp_workshop_setenabled", function (client, cmd, args)
  448. local url = args[1]
  449. local value = args[2] and tonumber(args[2]) == 1
  450.  
  451. if !url or value == nil then
  452. client:PrintMessage(HUD_PRINTTALK, "Missing arguments.")
  453. return
  454. end
  455.  
  456. if !client:IsSuperAdmin() then
  457. client:PrintMessage(HUD_PRINTTALK, "You must be an admin to use this command.")
  458. return
  459. end
  460.  
  461. local itm = SSTRP.Workshop.Items[url]
  462. if !itm then
  463. client:PrintMessage(HUD_PRINTTALK, "No addon has been enabled with the workshop ID " .. url)
  464. return
  465. end
  466.  
  467. if (!itm.Defer and itm.Enabled ~= value and value == true) then
  468. if itm.Server then
  469. SSTRP.Workshop.Subscribe(itm)
  470. end
  471.  
  472. if itm.Client then
  473. SSTRP.Workshop.BroadcastSubscribe(itm)
  474. end
  475. end
  476.  
  477. itm.Enabled = value
  478. SSTRP.Workshop.Transmit(itm)
  479. SSTRP.Workshop.Save()
  480. end)
  481.  
  482. concommand.Add("sstrp_workshop_restart_server", function (client, cmd, args)
  483.  
  484. if !client:IsSuperAdmin() then
  485. client:PrintMessage(HUD_PRINTTALK, "You must be an admin to use this command.")
  486. return
  487. end
  488.  
  489. ix.util.Notify(client:Name() .. " has forced a server restart.")
  490. timer.Simple(2.0, function ()
  491. Entity(1):GetAnimInfo(-99999999)
  492. end)
  493. end)
  494.  
  495. timer.Simple(0.5, SSTRP.Workshop.Load)
  496. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement