Advertisement
Guest User

Untitled

a guest
May 27th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. AddCSLuaFile()
  2.  
  3. if SERVER then
  4. util.AddNetworkString("H.PrepIPly")
  5. util.AddNetworkString("H.IPly")
  6. util.AddNetworkString("H.Ply")
  7. util.AddNetworkString("H.PV")
  8.  
  9. GM.PFields = {}
  10. GM.PVars = {}
  11.  
  12. -- ids aren't registered variables so we have to add them ourselves
  13. GM.GlobalPVars = {id = NW_MEDIUMINT}
  14. GM.SharedPVars = {}
  15. end
  16.  
  17. -- Internals
  18. local VAR_PRIVATE, VAR_SHARED, VAR_GLOBAL = VAR_PRIVATE, VAR_SHARED, VAR_GLOBAL
  19. local meta = FindMetaTable("Player")
  20.  
  21. -- use this for values that are different for each player (e.g. registration date)
  22. -- nwtype must be one of the NW_* enums
  23. -- shared: VAR_SHARED will sync the player, VAR_GLOBAL will sync everyone
  24. function GM:AddPlayerField(name, funcname, default, nwtype, shared)
  25. print("added player field: " .. name .. " (default: " .. tostring(default) .. ")")
  26. if SERVER then
  27. meta["Set" .. funcname] = function(self, val, nosave)
  28. self.PVars = self.PVars or {}
  29. if val == self.PVars[name] then return end
  30. self.PVars[name] = val
  31.  
  32. if shared ~= nil then
  33. print("updating " .. name .. " on " .. self:Nick())
  34. net.Start("SP.PV")
  35. net.WriteUInt(self:EntIndex(), 8)
  36. net.WriteString(name)
  37. net.Write(nwtype, val)
  38. if shared then
  39. net.Broadcast()
  40. else
  41. net.Send(self)
  42. end
  43. end
  44.  
  45. if not nosave then
  46. self:SavePlayerField(name, val)
  47. end
  48. end
  49.  
  50. self.PFields[name] = {Type = type(default), NWType = nwtype, Shared = shared}
  51. if shared == VAR_GLOBAL then
  52. self.GlobalPVars[name] = nwtype
  53. elseif shared == VAR_SHARED then
  54. self.SharedPVars[name] = nwtype
  55. end
  56. end
  57.  
  58. if SERVER or nwtype ~= VAR_PRIVATE then
  59. meta["Get" .. funcname] = function(self)
  60. if not self.PVars or self.PVars[name] == nil then
  61. return default
  62. end
  63. return self.PVars[name]
  64. end
  65. end
  66. end
  67.  
  68. -- Adds a variable as a row in sp_player_vars
  69. -- Should be used for variables that will rarely vary from the default
  70. -- nwtype must be one of the NW_* enums
  71. -- shared: VAR_SHARED will sync the player, VAR_GLOBAL will sync everyone
  72. function GM:AddPlayerVar(name, funcname, default, nwtype, shared)
  73. print("added PV: " .. name .. " (default: " .. tostring(default) .. ")")
  74. if SERVER then
  75. meta["Set" .. funcname] = function(self, val, nosave)
  76. self.PVars = self.PVars or {}
  77. -- local cur = self.PVars[name]
  78. -- if val == cur or (cur == nil and val == default) then
  79. -- return
  80. -- end
  81. if val == self.PVars[name] then return end
  82. self.PVars[name] = val
  83.  
  84. if shared ~= nil then
  85. print("updating " .. name .. " on " .. self:Nick())
  86. net.Start("SP.PV")
  87. net.WriteUInt(self:EntIndex(), 8)
  88. net.WriteString(name)
  89. net.Write(nwtype, val)
  90. if shared then
  91. net.Broadcast()
  92. else
  93. net.Send(self)
  94. end
  95. end
  96.  
  97. if not nosave then
  98. self:SavePlayerVariable(name, val)
  99. end
  100. end
  101.  
  102. self.PVars[name] = {Type = type(default), NWType = nwtype, Shared = shared, Save = true}
  103. if shared == VAR_GLOBAL then
  104. self.GlobalPlayerVars[name] = nwtype
  105. elseif shared == VAR_SHARED then
  106. self.SharedPlayerVars[name] = nwtype
  107. end
  108. else
  109. meta["Set" .. funcname] = function(self, val)
  110. self.PVars = self.PVars or {}
  111.  
  112. end
  113. end
  114.  
  115. if SERVER or nwtype ~= VAR_PRIVATE then
  116. meta["Get" .. funcname] = function(self)
  117. if not self.PVars or self.PVars[name] == nil then
  118. return default
  119. end
  120. return self.PVars[name]
  121. end
  122. end
  123. end
  124.  
  125. -- Adds a temporary player var
  126. -- nwtype must be one of the NW_* enums
  127. -- shared: VAR_SHARED will sync the player, VAR_GLOBAL will sync everyone
  128. function GM:AddTempPlayerVar(name, funcname, default, nwtype, shared)
  129. print("added runtime PV: " .. name .. " (default: " .. tostring(default) .. ")")
  130. if SERVER then
  131. meta["Set" .. funcname] = function(self, val)
  132. self.PVars = self.PVars or {}
  133. -- local cur = self.PVars[name]
  134. -- if val == cur or (cur == nil and val == default) then
  135. -- return
  136. -- end
  137. if val == self.PVars[name] then return end
  138. self.PVars[name] = val
  139.  
  140. if shared ~= nil then
  141. print("updating " .. name .. " on " .. self:Nick())
  142. net.Start("SP.PV")
  143. net.WriteUInt(self:EntIndex(), 8)
  144. net.WriteString(name)
  145. net.Write(nwtype, val)
  146. if shared then
  147. net.Broadcast()
  148. else
  149. net.Send(self)
  150. end
  151. end
  152. end
  153.  
  154. self:PVars[name] = {Type = type(default), NWType = nwtype, Shared = shared, Save = false}
  155. if shared == VAR_GLOBAL then
  156. self:GlobalPlayerVars[name] = nwtype
  157. elseif shared == VAR_SHARED then
  158. self:SharedPlayerVars[name] = nwtype
  159. end
  160. end
  161.  
  162. if SERVER or nwtype ~= VAR_PRIVATE then
  163. meta["Get" .. funcname] = function(self)
  164. if not self.PVars or self.PVars[name] == nil then
  165. return default
  166. end
  167. return self.PVars[name]
  168. end
  169. end
  170. end
  171.  
  172. GM:AddPlayerVar("charlimit", "CharLimit", GM.CharLimit, NW_TINYUINT, VAR_SHARED)
  173. GM:AddPlayerVar("tooltrust", "Tooltrust", GM.Tooltrust, NW_TINYUINT, VAR_SHARED)
  174.  
  175. GM:AddTempPlayerVar("typing", "Typing", false, NW_BOOL, VAR_GLOBAL)
  176. GM:AddTempPlayerVar("holstered", "Holstered", 0, NW_TINYUINT, VAR_GLOBAL)
  177. GM:AddTempPlayerVar("ic", "InCharacter", false, NW_BOOL, VAR_GLOBAL)
  178.  
  179. Player.IsTyping, Player.GetTyping = Player.GetTyping, nil
  180. Player.IsHolstered, Player.GetHolstered = Player.GetHolstered, nil
  181. Player.IsInCharacter, Player.GetInCharacter = Player.GetInCharacter, nil
  182.  
  183. if SERVER then return end
  184.  
  185. local target = {}
  186.  
  187. net.Receive("H.PrepIPly", function(len)
  188. print("INITIAL PLAYER SYNC")
  189. GAMEMODE.LocalIndex = net.ReadUInt(8)
  190. GAMEMODE.IPlayers = net.ReadUInt(8)
  191. end)
  192.  
  193. net.Receive("H.IPly", function(len)
  194. local index = net.ReadUInt(8)
  195. local ply = Entity(index)
  196. print("Initial sync: #" .. index .. " (" .. tostring(ply) .. ")")
  197.  
  198. local vars = {}
  199. for i = 1, net.ReadUInt(8) do
  200. vars[net.ReadString()] = net.Read()
  201. end
  202.  
  203. -- LocalPlayer() is rarely valid at this point
  204. if index == GAMEMODE.LocalIndex then
  205. GAMEMODE.CurrentPlayerVars = vars
  206.  
  207. local flags = {}
  208.  
  209. print("\tLocal player\n\tFlags:")
  210.  
  211. for i = 1, net.ReadUInt(8) do
  212. local id = net.ReadString()
  213. print("\t\t" .. id)
  214. flags[id] = GAMEMODE.PlayerFlags[id]
  215. end
  216. GAMEMODE.CurrentPlayerFlags = flags
  217. end
  218.  
  219. if ply:IsValid() then
  220. ply.PVars = vars
  221. else
  222. GAMEMODE.PVQueue[index] = vars
  223. end
  224.  
  225. GAMEMODE.IPlayers = GAMEMODE.IPlayers - 1
  226. if GAMEMODE.IPlayers == 0 then
  227. GAMEMODE.IPlayers = nil
  228.  
  229. -- merge any newer, non-initial updates.
  230. -- this is very unlikely.
  231. for index, vars in pairs(target) do
  232. local t = GAMEMODE.PVQueue[index]
  233. if t then
  234. table.Merge(t, vars)
  235. else
  236. GAMEMODE.PVQueue[index] = vars
  237. end
  238.  
  239. local ply = Entity(index)
  240. if ply:IsValid() then
  241. ply.PVars = vars
  242. GAMEMODE.PVQueue[index] = nil
  243. end
  244. end
  245.  
  246. -- PV updates are applied normally now
  247. target = GAMEMODE.PVQueue
  248.  
  249. hook.Run("PostReadPlayers")
  250. if GAMEMODE.CharacterList then
  251. hook.Run("PlayerReady")
  252. end
  253. end
  254. end)
  255.  
  256. net.Receive("SP.Ply", function(len)
  257. local index = net.ReadUInt(8)
  258. local ply = Entity(index)
  259. print("Sync: #" .. index .. " (" .. tostring(ply) .. ")")
  260.  
  261. local vars = {}
  262. for i = 1, net.ReadUInt(8) do
  263. vars[net.ReadString()] = net.Read()
  264. end
  265.  
  266. if ply:IsValid() and not SP.IPlayers then
  267. ply.PVars = vars
  268.  
  269. if index == SP.LocalIndex then
  270. SP.CurrentPlayerVars = vars
  271. end
  272. else
  273. target[index] = vars
  274. end
  275. end)
  276.  
  277. net.Receive("SP.PV", function(len)
  278. local index = net.ReadUInt(8)
  279. local ply = Entity(index)
  280.  
  281. if ply:IsValid() and not SP.IPlayers then
  282. ply.PVars[net.ReadString()] = net.Read()
  283. else
  284. local vars = target[index]
  285. if vars then
  286. vars[net.ReadString()] = net.Read()
  287. else
  288. target[index] = {
  289. [net.ReadString()] = net.Read()
  290. }
  291. end
  292. end
  293. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement