public void TestRandomGenerateAppNumber() { string[] appNames = new[] { "App-A", "App-B", "App-C", "App-D", "App-E" }; double[] appRatio = new[] { 0.5, 0.2, 0.1, 0.1, 0.1 }; IRandomGenerator randomGenerator = new EntryGenerator("whatever", appNames, appRatio); int total = 10000; int[] counts = new int[appNames.Length]; for (int i = 0; i < total; i++) { int numOfApp = randomGenerator.RandomGenerateAppNumber(appNames.Length); counts[numOfApp - 1]++; } int totalRun = counts.Sum(); Assert.True(totalRun == total); for (int i = 0; i < appNames.Length - 1; i++) { double a = (double)counts[i] / counts[i + 1]; double b = 1 / EntryGenerator.APP_NUMBER_RATIO; Assert.True(Math.Abs(a - b) < 0.2); } }
public void TestRandomGenerateApplicationName() { string[] appNames = new[] { "App-A", "App-B", "App-C", "App-D", "App-E" }; double[] appRatio = new[] { 0.5, 0.2, 0.1, 0.1, 0.1 }; EntryGenerator randomGenerator = new EntryGenerator("whatever", appNames, appRatio); int total = 10000; Dictionary <string, int> counts = new Dictionary <string, int>(); foreach (string name in appNames) { counts[name] = 0; } for (int i = 0; i < total; i++) { string name = randomGenerator.RandomGenerateApplicationName(appNames, randomGenerator.ApplicationMap); counts[name]++; } Assert.True(counts.Values.Sum() == total); for (int i = 0; i < appNames.Length - 1; i++) { double a = (double)counts[appNames[i]] / counts[appNames[i + 1]]; double b = appRatio[i] / appRatio[i + 1]; Assert.True(Math.Abs(a - b) < 0.2); } }
private static void TryAddEntryToMatchedConjunctions(Object lookAt, int lookForInstanceId, EntryAddSettings settings) { var lookAtGameObject = lookAt as GameObject; for (var i = 0; i < currentAssetConjunctions.conjunctions.Count; i++) { var conjunction = currentAssetConjunctions.conjunctions[i]; var referencedAssetObjects = conjunction.referencedAsset.GetAllAssetObjects(); var match = false; for (var j = 0; j < referencedAssetObjects.Length; j++) { if (referencedAssetObjects[j] != lookForInstanceId) { continue; } match = true; break; } if (!match) { continue; } var newEntry = EntryGenerator.CreateNewReferenceEntry(EntryFinder.currentLocation, lookAt, lookAtGameObject, settings); conjunction.referencedAtInfo.AddNewEntry(newEntry); } }
private static GUIContent[] BuildPopupEntries(UnityEngine.Object[] objects, EntryGenerator func, out int selection, out int invalidSelections) { selection = -1; invalidSelections = 0; List <string> entries = null; foreach (UnityEngine.Object o in objects) { int newSelection; bool invalidSelection; List <string> newEntries = func(o, objects.Count() > 1, out newSelection, out invalidSelection); if (newEntries != null) { if (invalidSelection) { ++invalidSelections; } List <string> mergedEntries = entries == null ? newEntries : new List <string>(entries.Intersect(newEntries)); selection = entries == null ? newSelection : selection < 0 || newSelection < 0 || entries[selection] != newEntries[newSelection] ? -1 : mergedEntries.IndexOf(entries[selection]); entries = mergedEntries; } } if (entries == null) { entries = new List <string>(); } return(entries.Select(x => new GUIContent(x)).ToArray()); }
public void TestGenerate35() { const int targetNumEntries = 3; const int targetMaxNum = 5; var generated = EntryGenerator.Generate(targetNumEntries, targetMaxNum); Assert.That(generated.Count == targetNumEntries); generated.ForEach(x => Assert.That(x <= 5)); }
private static bool LookForObjectsReferencesInHierarchy(Object[] objects, List <HierarchyReferenceItem> results) { var foundReferences = new Dictionary <int, Dictionary <ExactReferenceData, List <ExactReferenceData> > >(objects.Length); var objectInstanceIds = CSObjectTools.GetObjectsInstanceIDs(objects); var canceled = HierarchyEntryFinder.FillHierarchyReferenceEntries(objects, (lookAt, lookForInstanceId, settings) => { for (var i = 0; i < objectInstanceIds.Length; i++) { var instanceId = objectInstanceIds[i]; if (instanceId == lookForInstanceId) { ExactReferenceData referenced; if (!foundReferences.ContainsKey(instanceId)) { foundReferences.Add(instanceId, new Dictionary <ExactReferenceData, List <ExactReferenceData> >()); referenced = ObjectToReferencingEntry(objects[i]); foundReferences[instanceId].Add(referenced, new List <ExactReferenceData>()); } else { referenced = foundReferences[instanceId].Keys.First(); } var assetPath = CSObjectTools.TryGetObjectAssetPath(lookAt); var reference = EntryGenerator.CreateNewReferenceEntry(EntryFinder.currentLocation, lookAt, lookAt as GameObject, settings); var referenceData = new ExactReferenceData { assetPath = assetPath, reference = reference }; foundReferences[instanceId][referenced].Add(referenceData); } } }); if (!canceled && foundReferences.Count > 0) { BuildTree(foundReferences, results); } if (canceled) { HierarchyReferencesTab.AutoSelectHierarchyReference = null; } return(canceled); }
private static ExactReferenceData ObjectToReferencingEntry(Object target) { var referencedObjectAsComponent = target as Component; var referencedObjectGameObject = target as GameObject; if (referencedObjectAsComponent != null) { referencedObjectGameObject = referencedObjectAsComponent.gameObject; } if (referencedObjectGameObject == null) { Debug.LogError(Maintainer.ConstructError("Couldn't find referenced Game Object from object " + target)); return(new ExactReferenceData()); } var referencedSettings = new EntryAddSettings { componentName = referencedObjectAsComponent != null?CSComponentTools.GetComponentName(referencedObjectAsComponent) : null, componentIndex = referencedObjectAsComponent != null?CSComponentTools.GetComponentIndex(referencedObjectAsComponent) : -1, componentInstanceId = referencedObjectAsComponent != null?referencedObjectAsComponent.GetInstanceID() : -1, }; var reference = EntryGenerator.CreateNewReferenceEntry(EntryFinder.currentLocation, referencedObjectGameObject, referencedObjectGameObject, referencedSettings); var assetPath = CSObjectTools.TryGetObjectAssetPath(target); var result = new ExactReferenceData { reference = reference, assetPath = assetPath }; return(result); }
public static HierarchyReferenceItem[] FindHierarchyObjectsReferences(Object[] allObjects, Object[] newObjects, bool showResults = true) { var results = new List <HierarchyReferenceItem>(); try { var sw = Stopwatch.StartNew(); CSEditorTools.lastRevealSceneOpenResult = null; EntryGenerator.ResetCachedObjects(); var searchCanceled = LookForObjectsReferencesInHierarchy(allObjects, results); sw.Stop(); EditorUtility.ClearProgressBar(); if (!searchCanceled) { var referencesFound = 0; foreach (var result in results) { if (result.depth == 1) { referencesFound++; } } if (referencesFound == 0) { HierarchyReferencesTab.AutoSelectHierarchyReference = null; MaintainerWindow.ShowNotification("Nothing found!"); } else { if (newObjects != null && newObjects.Length > 0) { var totalFoundFromNew = GetFoundObjects(newObjects, results); if (totalFoundFromNew.Count == 0) { HierarchyReferencesTab.AutoSelectHierarchyReference = null; MaintainerWindow.ShowNotification("Nothing found!"); } else { if (HierarchyReferencesTab.AutoSelectHierarchyReference == null) { if (totalFoundFromNew.Count == 1) { var firstFound = totalFoundFromNew[0]; var entry = ObjectToReferencingEntry(firstFound); HierarchyReferencesTab.AutoSelectHierarchyReference = entry.reference; } } MaintainerWindow.ClearNotification(); } } else { MaintainerWindow.ClearNotification(); } } Debug.Log(Maintainer.LogPrefix + ReferencesFinder.ModuleName + " results: " + referencesFound + " references found in " + sw.Elapsed.TotalSeconds.ToString("0.000", CultureInfo.InvariantCulture) + " seconds."); } else { Debug.Log(Maintainer.LogPrefix + ReferencesFinder.ModuleName + "Search canceled by user!"); } } catch (Exception e) { Debug.LogError(Maintainer.LogPrefix + ReferencesFinder.ModuleName + ": " + e); EditorUtility.ClearProgressBar(); } var foundObjects = GetFoundObjects(allObjects, results); SaveLastSearched(foundObjects); EntryGenerator.ResetCachedObjects(); SearchResultsStorage.HierarchyReferencesSearchResults = results.ToArray(); if (showResults) { MaintainerWindow.ShowObjectReferences(); } return(results.ToArray()); }
public static ProjectReferenceItem[] FindAssetsReferences(FilterItem[] allTargetAssets, FilterItem[] newTargetAssets, bool showResults = true) { var results = new List <ProjectReferenceItem>(); ConjunctionInfoList.Clear(); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); try { var sw = Stopwatch.StartNew(); CSEditorTools.lastRevealSceneOpenResult = null; EntryGenerator.ResetCachedObjects(); var searchCanceled = LookForAssetsReferences(allTargetAssets, results); sw.Stop(); EditorUtility.ClearProgressBar(); if (!searchCanceled) { var resultsCount = results.Count; if (resultsCount <= 1) { ProjectReferencesTab.AutoSelectPath = null; MaintainerWindow.ShowNotification("Nothing found!"); } else { if (newTargetAssets != null && newTargetAssets.Length > 0) { var found = false; foreach (var result in results) { if (result.depth == 0 && CSFilterTools.IsValueMatchesAnyFilter(result.assetPath, newTargetAssets)) { found = true; break; } } if (!found) { ProjectReferencesTab.AutoSelectPath = null; MaintainerWindow.ShowNotification("Nothing found!"); } else { MaintainerWindow.ClearNotification(); } } else { MaintainerWindow.ClearNotification(); } } Debug.Log(Maintainer.LogPrefix + ReferencesFinder.ModuleName + " results: " + (resultsCount - 1) + " items found in " + sw.Elapsed.TotalSeconds.ToString("0.000", CultureInfo.InvariantCulture) + " seconds."); } else { Debug.Log(Maintainer.LogPrefix + ReferencesFinder.ModuleName + "Search canceled by user!"); } } catch (Exception e) { Debug.LogError(Maintainer.LogPrefix + ReferencesFinder.ModuleName + ": " + e); EditorUtility.ClearProgressBar(); } SaveLastSearched(results); EntryGenerator.ResetCachedObjects(); SearchResultsStorage.ProjectReferencesSearchResults = results.ToArray(); if (showResults) { MaintainerWindow.ShowAssetReferences(); } return(results.ToArray()); }