Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local AutoUpdate = {}
- AutoUpdate.CachedConfigs = {}
- AutoUpdate.CachedSets = {}
- AutoUpdate.SafetyLevel = 0.5
- function GetHighestValue(ValueTable)
- local HighestKey
- local HighestValue
- local Index = 0
- for Key, Value in next, ValueTable do
- if (not HighestKey and not HighestValue) or (Value > HighestValue) then
- HighestKey = Key
- HighestValue = Value
- Index += 1
- end
- end
- return HighestKey, Index
- end
- function GetMostLikelyParent(InstanceTable)
- local Parents = {}
- for _, ThisInstance in next, InstanceTable do
- local Check = Parents[ThisInstance.Parent]
- if Check then
- Parents[ThisInstance.Parent] += 1
- else
- Parents[ThisInstance.Parent] = 1
- end
- end
- for Parent, Value in next, Parents do
- Parents[Parent] = Value / #InstanceTable
- end
- local HighKey, KeyIndex = GetHighestValue(Parents)
- return HighKey
- end
- function AutoUpdate.SetSafetyLevel(SafetyLevel)
- AutoUpdate.SafetyLevel = typeof(SafetyLevel) == "number" and SafetyLevel or AutoUpdate.SafetyLevel
- end
- function TryGetName(Descendant, Dependency)
- local Result, Found = pcall(function()
- return string.find(Descendant.Name:lower(), Dependency:lower()) and true or false
- end)
- return typeof(Found) == "boolean" and Found or false
- end
- function TryGetProperty(Descendant, Property)
- local Result, Property = pcall(function()
- return Descendant[Property]
- end)
- return Result, Property
- end
- function AutoUpdate.GetConfig(ConfigName)
- return AutoUpdate.CachedConfigs[ConfigName] or warn("[AutoUpdate]: '"..ConfigName.."' not found. Cache necessary configs before attempting to access them.")
- end
- function AutoUpdate.GetSet(ConfigName)
- return AutoUpdate.CachedSets[ConfigName] or warn("[AutoUpdate]: '"..ConfigName.."' not found. Cache necessary sets before attempting to access them.")
- end
- function AutoUpdate.AddSet(ConfigName)
- local NewSet = {
- Name = ConfigName,
- Properties = {},
- RequiredLevel = 0.5,
- Candidates = {},
- TopParent = nil,
- Result = nil
- }
- local SetLibrary = {}
- function SetLibrary.Cache()
- AutoUpdate.CachedSets[ConfigName] = NewSet
- end
- function SetLibrary.SetRequiredLevel(RequiredLevel)
- NewSet.RequiredLevel = typeof(RequiredLevel) == "number" and RequiredLevel or NewSet.RequiredLevel
- end
- function SetLibrary.SetProperties(PropertyTable)
- NewSet.Properties = typeof(PropertyTable) == "table" and PropertyTable or NewSet.Properties
- end
- function SetLibrary.Guess(InstanceToCheck)
- local GameDescendants = (InstanceToCheck or game):GetDescendants()
- local Total = 0
- local CurrentConfidence = 0
- local PotentialCandidates = {}
- for _, Descendant in next, GameDescendants do
- local TotalPoints = 0
- local Tries = 0
- for Property, Value in next, NewSet.Properties do
- local Accessible, Property = TryGetProperty(Descendant, Property)
- if Accessible and Property == Value then
- TotalPoints += 1
- end
- Tries += 1
- end
- if (TotalPoints/Tries) >= NewSet.RequiredLevel then
- table.insert(PotentialCandidates, Descendant)
- end
- end
- if #PotentialCandidates > 0 then
- NewSet.Candidates = PotentialCandidates
- NewSet.Result = GetMostLikelyParent(PotentialCandidates)
- local FullName = NewSet.Result:GetFullName():split(".")
- NewSet.TopParent = game[FullName[1]][FullName[2]]
- end
- return NewSet
- end
- return SetLibrary
- end
- function AutoUpdate.AddConfig(ConfigName)
- local NewConfig = {
- Name = ConfigName,
- Dependencies = {},
- ConfidenceLevel = 0,
- Candidates = {},
- TopParent = nil,
- Result = nil
- }
- local ConfigLibrary = {}
- function ConfigLibrary.Cache()
- AutoUpdate.CachedConfigs[ConfigName] = NewConfig
- end
- function ConfigLibrary.AddDependencies(...)
- local DependenciesTable = {...}
- for _, Dependency in next, DependenciesTable do
- table.insert(NewConfig.Dependencies, Dependency)
- end
- end
- function ConfigLibrary.Guess(InstanceToCheck)
- local GameDescendants = (InstanceToCheck or game):GetDescendants()
- local Total = 0
- local CurrentConfidence = 0
- local PotentialCandidates = {}
- for _, Dependency in next, NewConfig.Dependencies do
- for _, Descendant in next, GameDescendants do
- if TryGetName(Descendant, Dependency) then
- Total += 1
- table.insert(PotentialCandidates, Descendant)
- break
- end
- end
- end
- CurrentConfidence = Total / #NewConfig.Dependencies
- NewConfig.ConfidenceLevel = CurrentConfidence
- if CurrentConfidence >= AutoUpdate.SafetyLevel then
- NewConfig.Candidates = PotentialCandidates
- NewConfig.Result = GetMostLikelyParent(PotentialCandidates)
- local FullName = NewConfig.Result:GetFullName():split(".")
- NewConfig.TopParent = game[FullName[1]][FullName[2]]
- end
- return NewConfig
- end
- return ConfigLibrary
- end
- return AutoUpdate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement