Thomasims

PlayX Sync

Dec 8th, 2017
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.20 KB | None | 0 0
  1. if SERVER then
  2.     util.AddNetworkString("playx_sync")
  3.     AddCSLuaFile()
  4.     local conv = CreateConVar("playx_sync_startdelay", 10, FCVAR_ARCHIVE, "How many seconds to wait before starting the video")
  5.     hook.Add("PlayXMediaBegun","synctime",function(data)
  6.         local handler = data[1]
  7.         local url = data[2]
  8.         SetGlobal2Float("PlayXStartTime",CurTime() + conv:GetFloat())
  9.         print("Starting in " .. conv:GetFloat() .. " seconds")
  10.     end)
  11.     net.Receive("playx_sync",function(len, ply)
  12.         if not PlayX.CurrentMedia then return end
  13.         net.Start("playx_sync")
  14.         net.WriteString(PlayX.CurrentMedia.Handler)
  15.         net.WriteTable(PlayX.CurrentMedia.HandlerArgs)
  16.         net.WriteBool(PlayX.CurrentMedia.LowFramerate)
  17.         net.WriteBool(PlayX.CurrentMedia.ResumeSupported)
  18.         net.WriteString(PlayX.CurrentMedia.URI)
  19.         net.Send(ply)
  20.     end)
  21.     local paused
  22.     concommand.Add("playx_server_pause", function(ply)
  23.         if not ply:IsSuperAdmin() then return end
  24.         paused = CurTime() - GetGlobal2Float("PlayXStartTime")
  25.         SetGlobal2Bool("PlayXPaused", true)
  26.         for k, ply in ipairs(player.GetAll()) do ply:ConCommand("playx_manual_pause") end
  27.     end)
  28.     concommand.Add("playx_server_play", function(ply)
  29.         if not ply:IsSuperAdmin() or not paused then return end
  30.         SetGlobal2Float("PlayXStartTime",CurTime() + paused)
  31.         SetGlobal2Bool("PlayXPaused", false)
  32.         paused = nil
  33.         for k, ply in ipairs(player.GetAll()) do ply:ConCommand("playx_syncme") end
  34.     end)
  35. end
  36. if CLIENT then
  37.    
  38.     local handlers = {}
  39.     handlers.JWVideo = function()
  40.         local playxpanel = ents.FindByClass("gmod_playx")[1].Browser
  41.         playxpanel:SetPos(-playxpanel:GetSize(),0)
  42.         local CONTROLS = {}
  43.         local pending
  44.         playxpanel:AddFunction("ctrl","cb",function(dt) if pending then pending(dt) pending = nil end end)
  45.         CONTROLS.GetTime = function(cb)
  46.             pending = function(a)
  47.                 cb(tonumber(a))
  48.             end
  49.             playxpanel:Call("ctrl.cb(document.getElementsByTagName('video')[0].currentTime);")
  50.         end
  51.         CONTROLS.SetTime = function(time)
  52.             playxpanel:Call("document.getElementsByTagName('video')[0].currentTime = " .. time .. ";")
  53.         end
  54.         CONTROLS.Play = function(time)
  55.             playxpanel:Call("document.getElementsByTagName('video')[0].play();")
  56.         end
  57.         CONTROLS.Pause = function(time)
  58.             playxpanel:Call("document.getElementsByTagName('video')[0].pause();")
  59.         end
  60.         return CONTROLS
  61.     end
  62.     handlers.JW = handlers.JWVideo
  63.     handlers.YoutubeNative = handlers.JWVideo
  64.     handlers.Vimeo = handlers.JWVideo
  65.     handlers["justin.tv"] = handlers.JWVideo
  66.     handlers.Hulu = handlers.JWVideo
  67.     handlers.FlashAPI = handlers.JWVideo --Actually not sure about this one, it's used by the google drive provider so I added it anyway
  68.    
  69.    
  70.     local function getcontrols(handler)
  71.         if not handlers[handler] then print("no controls for handler " .. handler .. ", trying JWVideo") return handlers.JWVideo() end
  72.         return handlers[handler]()
  73.     end
  74.     local currentcontrols
  75.     local function dosync()
  76.         local CONTROLS = getcontrols(PlayX.CurrentMedia.Handler)
  77.         if not CONTROLS then return end
  78.         if GetGlobal2Bool("PlayXPaused", false) then return end
  79.         local start = GetGlobal2Float("PlayXStartTime")
  80.         print("starting in " .. math.max(0, start - CurTime()) .. " seconds")
  81.         timer.Simple(math.max(0, start - CurTime()), function()
  82.             CONTROLS.SetTime(math.max(0, CurTime() - start))
  83.             CONTROLS.Play()
  84.             print("starting at " .. math.max(0, CurTime() - start) .. " seconds")
  85.         end)
  86.     end
  87.     hook.Add("PlayXPlayBegun","synctime",function(data) timer.Simple(3,function()
  88.        
  89.         local CONTROLS = getcontrols(data[1])
  90.         if not CONTROLS then return ErrorNoHalt("couldn't get controls for PlayX Panel!") end
  91.         CONTROLS.Pause()
  92.         currentcontrols = CONTROLS
  93.         dosync()
  94.        
  95.     end) end)
  96.     concommand.Add("playx_syncme", dosync)
  97.     concommand.Add("playx_manual_pause", function() getcontrols(PlayX.CurrentMedia.Handler).Pause() end)
  98.     concommand.Add("playx_manual_play", function() getcontrols(PlayX.CurrentMedia.Handler).Play() end)
  99.     concommand.Add("playx_manual_settime", function(_, _, a) getcontrols(PlayX.CurrentMedia.Handler).SetTime(a[1]) end)
  100.     concommand.Add("playx_manual_gettime", function() getcontrols(PlayX.CurrentMedia.Handler).GetTime(print) end)
  101.     concommand.Add("playx_manual_fixplayer", function()
  102.         if not PlayX.CurrentMedia then
  103.             net.Start("playx_sync")
  104.             net.SendToServer()
  105.             return
  106.         end
  107.         ents.FindByClass("gmod_playx")[1]:Play(
  108.             PlayX.CurrentMedia.Handler,
  109.             PlayX.CurrentMedia.URI,
  110.             CurTime()-GetGlobal2Float("PlayXStartTime"),
  111.             GetConVar("playx_volume"):GetFloat(),
  112.             PlayX.CurrentMedia.HandlerArgs)
  113.         timer.Simple(5, dosync)
  114.     end)
  115.     net.Receive("playx_sync",function(len)
  116.         PlayX.CurrentMedia = {
  117.             Handler = net.ReadString(),
  118.             HandlerArgs = net.ReadTable(),
  119.             LowFramerate = net.ReadBool(),
  120.             ResumeSupported = net.ReadBool(),
  121.             StartTime = CurTime()-GetGlobal2Float("PlayXStartTime"),
  122.             URI = net.ReadString()
  123.         }
  124.         RunConsoleCommand("playx_manual_fixplayer")
  125.     end)
  126. end
Advertisement
Add Comment
Please, Sign In to add comment