Advertisement
XZTablets

AutoUpdateConcept

Jul 29th, 2020
2,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.76 KB | None | 0 0
  1. local AutoUpdate = {}
  2.  
  3. AutoUpdate.CachedConfigs = {}
  4. AutoUpdate.CachedSets = {}
  5. AutoUpdate.SafetyLevel = 0.5
  6.  
  7. function GetHighestValue(ValueTable)
  8.     local HighestKey
  9.     local HighestValue
  10.     local Index = 0
  11.     for Key, Value in next, ValueTable do
  12.         if (not HighestKey and not HighestValue) or (Value > HighestValue) then
  13.             HighestKey = Key
  14.             HighestValue = Value
  15.             Index += 1
  16.         end
  17.     end
  18.     return HighestKey, Index
  19. end
  20.  
  21. function GetMostLikelyParent(InstanceTable)
  22.     local Parents = {}
  23.     for _, ThisInstance in next, InstanceTable do
  24.         local Check = Parents[ThisInstance.Parent]
  25.         if Check then
  26.             Parents[ThisInstance.Parent] += 1
  27.         else
  28.             Parents[ThisInstance.Parent] = 1
  29.         end
  30.     end
  31.     for Parent, Value in next, Parents do
  32.         Parents[Parent] = Value / #InstanceTable
  33.     end
  34.     local HighKey, KeyIndex = GetHighestValue(Parents)
  35.     return HighKey
  36. end
  37.  
  38. function AutoUpdate.SetSafetyLevel(SafetyLevel)
  39.     AutoUpdate.SafetyLevel = typeof(SafetyLevel) == "number" and SafetyLevel or AutoUpdate.SafetyLevel
  40. end
  41.  
  42. function TryGetName(Descendant, Dependency)
  43.     local Result, Found = pcall(function()
  44.         return string.find(Descendant.Name:lower(), Dependency:lower()) and true or false
  45.     end)
  46.     return typeof(Found) == "boolean" and Found or false
  47. end
  48.  
  49. function TryGetProperty(Descendant, Property)
  50.     local Result, Property = pcall(function()
  51.         return Descendant[Property]
  52.     end)
  53.     return Result, Property
  54. end
  55.  
  56. function AutoUpdate.GetConfig(ConfigName)
  57.     return AutoUpdate.CachedConfigs[ConfigName] or warn("[AutoUpdate]: '"..ConfigName.."' not found. Cache necessary configs before attempting to access them.")
  58. end
  59.  
  60. function AutoUpdate.GetSet(ConfigName)
  61.     return AutoUpdate.CachedSets[ConfigName] or warn("[AutoUpdate]: '"..ConfigName.."' not found. Cache necessary sets before attempting to access them.")
  62. end
  63.  
  64. function AutoUpdate.AddSet(ConfigName)
  65.     local NewSet = {
  66.         Name = ConfigName,
  67.         Properties = {},
  68.         RequiredLevel = 0.5,
  69.         Candidates = {},
  70.         TopParent = nil,
  71.         Result = nil
  72.     }
  73.    
  74.     local SetLibrary = {}
  75.    
  76.     function SetLibrary.Cache()
  77.         AutoUpdate.CachedSets[ConfigName] = NewSet
  78.     end
  79.    
  80.     function SetLibrary.SetRequiredLevel(RequiredLevel)
  81.         NewSet.RequiredLevel = typeof(RequiredLevel) == "number" and RequiredLevel or NewSet.RequiredLevel
  82.     end
  83.    
  84.     function SetLibrary.SetProperties(PropertyTable)
  85.         NewSet.Properties = typeof(PropertyTable) == "table" and PropertyTable or NewSet.Properties
  86.     end
  87.    
  88.     function SetLibrary.Guess(InstanceToCheck)
  89.         local GameDescendants = (InstanceToCheck or game):GetDescendants()
  90.         local Total = 0
  91.         local CurrentConfidence = 0
  92.         local PotentialCandidates = {}
  93.         for _, Descendant in next, GameDescendants do
  94.             local TotalPoints = 0
  95.             local Tries = 0
  96.             for Property, Value in next, NewSet.Properties do
  97.                 local Accessible, Property = TryGetProperty(Descendant, Property)
  98.                 if Accessible and Property == Value then
  99.                     TotalPoints += 1
  100.                 end
  101.                 Tries += 1
  102.             end
  103.             if (TotalPoints/Tries) >= NewSet.RequiredLevel then
  104.                 table.insert(PotentialCandidates, Descendant)
  105.             end
  106.         end
  107.         if #PotentialCandidates > 0 then
  108.             NewSet.Candidates = PotentialCandidates
  109.             NewSet.Result = GetMostLikelyParent(PotentialCandidates)
  110.             local FullName = NewSet.Result:GetFullName():split(".")
  111.             NewSet.TopParent = game[FullName[1]][FullName[2]]
  112.         end
  113.         return NewSet
  114.     end
  115.    
  116.     return SetLibrary
  117. end
  118.  
  119. function AutoUpdate.AddConfig(ConfigName)
  120.     local NewConfig = {
  121.         Name = ConfigName,
  122.         Dependencies = {},
  123.         ConfidenceLevel = 0,
  124.         Candidates = {},
  125.         TopParent = nil,
  126.         Result = nil
  127.     }
  128.    
  129.     local ConfigLibrary = {}
  130.    
  131.     function ConfigLibrary.Cache()
  132.         AutoUpdate.CachedConfigs[ConfigName] = NewConfig
  133.     end
  134.    
  135.     function ConfigLibrary.AddDependencies(...)
  136.         local DependenciesTable = {...}
  137.         for _, Dependency in next, DependenciesTable do
  138.             table.insert(NewConfig.Dependencies, Dependency)
  139.         end
  140.     end
  141.    
  142.     function ConfigLibrary.Guess(InstanceToCheck)
  143.         local GameDescendants = (InstanceToCheck or game):GetDescendants()
  144.         local Total = 0
  145.         local CurrentConfidence = 0
  146.         local PotentialCandidates = {}
  147.         for _, Dependency in next, NewConfig.Dependencies do
  148.             for _, Descendant in next, GameDescendants do
  149.                 if TryGetName(Descendant, Dependency) then
  150.                     Total += 1
  151.                     table.insert(PotentialCandidates, Descendant)
  152.                     break
  153.                 end
  154.             end
  155.         end
  156.         CurrentConfidence = Total / #NewConfig.Dependencies
  157.         NewConfig.ConfidenceLevel = CurrentConfidence
  158.         if CurrentConfidence >= AutoUpdate.SafetyLevel then
  159.             NewConfig.Candidates = PotentialCandidates
  160.             NewConfig.Result = GetMostLikelyParent(PotentialCandidates)
  161.             local FullName = NewConfig.Result:GetFullName():split(".")
  162.             NewConfig.TopParent = game[FullName[1]][FullName[2]]
  163.         end
  164.         return NewConfig
  165.     end
  166.    
  167.     return ConfigLibrary
  168. end
  169.  
  170. return AutoUpdate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement