Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. -- There are bugs here, tread lightly
  2.  
  3. if SERVER then
  4. util.AddNetworkString( "RSWorkshop" )
  5.  
  6. local collection = CreateConVar( "wsdl_collection", "", { FCVAR_ARCHIVE, FCVAR_REPLICATED } )
  7. local overrideRSWorkshop = CreateConVar( "wsdl_overrideresource", 0, { FCVAR_ARCHIVE }, "Expriemental: Override resource.AddWorkshop. This will completly stop the client from downloading any addons on join, even if it's resource.AddWorkshop." )
  8.  
  9. if overrideRSWorkshop then
  10. local addWorkshops = {}
  11.  
  12. function resource.AddWorkshop( id )
  13. addWorkshops[] = true
  14. end
  15.  
  16. hook.Add( "PlayerInitialSpawn", "OverrideRSWorkshop", function( ply )
  17. net.Start( "RSWorkshop" )
  18. net.WriteTable( addWorkshops )
  19. net.Send( ply )
  20. end )
  21. end
  22. else
  23. local collection = CreateConVar( "wsdl_collection", "", { FCVAR_REPLICATED, FCVAR_SERVER_CAN_EXECUTE } )
  24.  
  25. local toDownload = {}
  26. local currentAddon = 1
  27.  
  28. local function DownloadSteamworksAddon( id, callback )
  29. steamworks.FileInfo( id, function( dat )
  30. if not dat then
  31. print( "Couldn't download addon! Skipping..." )
  32. callback()
  33. return
  34. end
  35.  
  36. notification.AddProgress( "wsdl_notify_" .. id, "Downloading " .. dat.title .. " via workshop..." )
  37.  
  38. --print( dat.fileid )
  39. --print( dat.previewid )
  40.  
  41. --[[for k,v in pairs( dat ) do
  42. print( tostring( k ) .. " = " .. tostring( v ) )
  43. end]]
  44.  
  45. steamworks.Download( dat.fileid, true, function( path )
  46. print( path )
  47. game.MountGMA( path )
  48. notification.Kill( "wsdl_notify_" .. id )
  49. callback()
  50. end )
  51. end )
  52. end
  53.  
  54. local function DownloadLoop()
  55. currentAddon = currentAddon + 1
  56. print( currentAddon, #table.GetKeys( toDownload ), currentAddon < #toDownload )
  57. -- If 2 is less than or equal to 3 then this should happen
  58. -- If 3 is less than or equal to 3 then this should happen
  59. if currentAddon <= #table.GetKeys( toDownload ) then
  60. DownloadSteamworksAddon( table.GetKeys( toDownload )[ currentAddon ], DownloadLoop )
  61. else
  62. -- Step 5: Inform the user of how great I am
  63. chat.AddText( "Workshop download done!" )
  64. print( "In-Game WorkshopDL was created by meharryp (http://steamcommunity.com/id/meharryp)" )
  65. print( "Get the addon for your server here: http://soon.tm" )
  66. end
  67. end
  68.  
  69. function GetWorkshopDownloader()
  70. -- Stage 1: Figure out what's in the workshop collection via the shittest way possible
  71. timer.Simple( 5, function() -- Don't want it starting straight away
  72. print( collection:GetString() )
  73. local strCollection = string.Replace( collection:GetString(), "http://steamcommunity.com/sharedfiles/filedetails/?id=", "" )
  74. strCollection = string.Replace( strCollection, "https://steamcommunity.com/sharedfiles/filedetails/?id=", "" )
  75.  
  76. --print( strCollection )
  77.  
  78. local html = vgui.Create( "DHTML" )
  79. html:SetSize( 0, 0 )
  80. html:SetPos( 0, 0 )
  81. html:OpenURL( "http://meharryp.xyz/wsapi.html#endpoint=ISteamRemoteStorage/GetCollectionDetails/v1/&format=json&collectioncount=1&publishedfileids[0]=" .. strCollection )
  82.  
  83. -- Stage 2: Extract list from responce
  84. timer.Simple( 5, function() -- This is the hackiest way of doing it, but it works.
  85. html:AddFunction( "wsdl", "GetDetails", function( str )
  86. str = string.Replace( str, [[<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">]], "" )
  87. str = string.Replace( str, [[</pre></body></html>]], "" )
  88.  
  89. local res = util.JSONToTable( str )
  90. --print( str )
  91.  
  92. if not res.response then
  93. print( "Couldn't get a response! Retrying in 90 seconds..." )
  94. timer.Simple( 90, function()
  95. GetWorkshopDownloader()
  96. end )
  97. return
  98. end
  99.  
  100. -- Stage 3: Add the addons to the toDownload table for later use
  101. for k,v in pairs( res.response.collectiondetails[ 1 ].children ) do
  102. toDownload[ v.publishedfileid ] = true
  103. end
  104.  
  105. -- Stage 4: Download the addons
  106. timer.Simple( 5, function() -- Now we download the addons
  107. DownloadSteamworksAddon( table.GetKeys( toDownload )[ currentAddon ], DownloadLoop )
  108. end )
  109. end )
  110.  
  111. -- Step 2a: Why is DHTML:GetHTML() not a function
  112. html:RunJavascript( "wsdl.GetDetails( document.documentElement.outerHTML );" )
  113. end )
  114. end )
  115. end
  116.  
  117. hook.Add( "InitPostEntity", "WDSL_Start", GetWorkshopDownloader )
  118.  
  119. -- Extra stage: resource.AddWorkshop overwriting
  120. -- The toDownload[ key ] = true part will stop duplicate adddons being added.
  121. net.Receive( "RSWorkshop", function()
  122. local tab = net.ReadTable()
  123. for k,v in pairs( tab ) do
  124. toDownload[ k ] = true
  125. end
  126. end )
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement