Advertisement
Aussiemon

SwitchCharacter.lua

Feb 27th, 2018
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. local mod_name = "Characters"
  2. --[[
  3. Characters
  4. - Allows you to switch any charter you wish
  5. --]]
  6.  
  7. local profile_synchronizer = Managers.state.network.profile_synchronizer
  8.  
  9. if not SwitchCharacter then
  10. SwitchCharacter = {}
  11. SwitchCharacter.is_updating = false
  12. SwitchCharacter.selected = 1
  13. SwitchCharacter.profile_index = 1
  14. SwitchCharacter.sp_backup = table.clone(SPProfiles)
  15. else
  16. SwitchCharacter.profile_index = 1
  17. end
  18.  
  19. --[[
  20. Looks for a empty profile spot and return the Unit
  21. --]]
  22. local find_empty_unit = function(self)
  23. -- Find empty profile spot
  24. local profile_index = 1
  25. for j = 1, #SPProfiles, 1 do
  26. if profile_synchronizer:owner(j) == nil then
  27. profile_index = j
  28. EchoConsole("Profile " .. tostring(j) .. " avaidable")
  29. end
  30. end
  31.  
  32. -- get unit of profile_index
  33. local profile_unit = nil
  34. for unit, obj in pairs(self.units) do
  35. if obj == profile_index then
  36. return unit
  37. end
  38. end
  39.  
  40. return nil
  41. end
  42.  
  43. --[[
  44. Restore SPProfiles
  45. --]]
  46. local spprofiles_restore = function()
  47. SPProfiles = table.clone(SwitchCharacter.sp_backup)
  48. end
  49.  
  50. local spprofiles_patch = function(index)
  51. SwitchCharacter.profile_index = index
  52.  
  53. for i, profile in ipairs(SPProfiles) do
  54. SPProfiles[i] = table.clone(SwitchCharacter.sp_backup[index])
  55. end
  56. end
  57.  
  58. --[[
  59. Patch FindProfileIndex:
  60. Because we patched the SPProfiles this function try to convert a name to
  61. a index. Because we changed all SPProfiles the filter can't find the name.
  62. This will result to a nil value.
  63. --]]
  64. Mods.hook.set(mod_name, "FindProfileIndex", function(func, profile_name)
  65. local value = func(profile_name)
  66.  
  67. if value == nil then
  68. return SwitchCharacter.profile_index
  69. else
  70. return value
  71. end
  72. end)
  73.  
  74. --- Make all charters selectable
  75. Mods.hook.set(mod_name, "ProfileSynchronizer.owner", function (func, ...)
  76. if SwitchCharacter.is_updating then
  77. return nil
  78. end
  79.  
  80. return func(...)
  81. end)
  82.  
  83. Mods.hook.set(mod_name, "ProfileWorldView.update", function (func, ...)
  84. SwitchCharacter.is_updating = true
  85.  
  86. func(...)
  87.  
  88. SwitchCharacter.is_updating = false
  89. end)
  90.  
  91. Mods.hook.set(mod_name, "ProfileWorldView.select_unit", function (func, self, unit)
  92. SwitchCharacter.selected = self.units[unit]
  93.  
  94. local empty_unit = find_empty_unit(self)
  95.  
  96. func(self, empty_unit)
  97. end)
  98.  
  99. -- Patch Spawn/Despawn
  100. Mods.hook.set(mod_name, "BulldozerPlayer.despawn", function(func, ...)
  101. func(...)
  102.  
  103. spprofiles_patch(SwitchCharacter.selected)
  104. end)
  105.  
  106. Mods.hook.set(mod_name, "BulldozerPlayer.spawn", function(func, ...)
  107. return func(...)
  108. end)
  109.  
  110. -- Patch Profile View
  111. Mods.hook.set(mod_name, "ProfileView.on_enter", function(func, ...)
  112. spprofiles_restore()
  113.  
  114. func(...)
  115. end)
  116.  
  117. Mods.hook.set(mod_name, "ProfileView.on_exit", function(func, ...)
  118. func(...)
  119.  
  120. spprofiles_patch(SwitchCharacter.profile_index)
  121. end)
  122.  
  123.  
  124. Mods.hook.set(mod_name, "MatchmakingStateJoinGame.rpc_matchmaking_request_profile_reply",
  125. function (func, self, sender, client_cookie, host_cookie, profile, ...)
  126. spprofiles_restore()
  127.  
  128. profile = FindProfileIndex(self.selected_hero_name)
  129.  
  130. func(self, sender, client_cookie, host_cookie, profile, ...)
  131.  
  132. spprofiles_patch(SwitchCharacter.profile_index)
  133. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement