public static void ReconstructSurvivors() { SurvivorCatalog.survivorMaxCount = Mathf.Max(SurvivorDefinitions.Count, 10); for (int i = 0; i < SurvivorDefinitions.Count; i++) { SurvivorDefinitions[i].survivorIndex = (SurvivorIndex)i; } SurvivorCatalog.idealSurvivorOrder = SurvivorDefinitions.Select(x => x.survivorIndex).ToArray(); survivorDefs.SetValue(null, SurvivorDefinitions.ToArray()); allSurvivorDefs.SetValue(null, SurvivorDefinitions.ToArray()); ViewablesCatalog.Node node = new ViewablesCatalog.Node("/Survivors/", true, null); var existingNode = ViewablesCatalog.FindNode("/Survivors/"); //this essentially deletes an existing node if it exists existingNode?.SetParent(new ViewablesCatalog.Node("dummy", true, null)); for (var i = 0; i < SurvivorDefinitions.Count; i++) { var survivor = SurvivorDefinitions[i]; ViewablesCatalog.Node survivorEntryNode = new ViewablesCatalog.Node(survivor.displayNameToken, false, node); survivorEntryNode.shouldShowUnviewed = userProfile => !userProfile.HasViewedViewable(survivorEntryNode.fullName) && userProfile.HasSurvivorUnlocked(survivor.survivorIndex) && !string.IsNullOrEmpty(survivor.unlockableName); } ViewablesCatalog.AddNodeToRoot(node); }
private static void ReconstructSurvivors() { SurvivorDefinitions.GroupBy(x => x.survivorIndex).Where(x => x.Count() > 1).ToList().ForEach(x => { R2API.Logger.LogError($"{CenterText("!ERROR!")}"); R2API.Logger.LogError($"{CenterText($"One of your mods assigns a duplicate SurvivorIndex for \"{x.Key}\"")}"); R2API.Logger.LogError($"{CenterText("Please ask the author to fix their mod.")}"); }); SurvivorCatalog.survivorMaxCount = Math.Max((int)SurvivorDefinitions.Select(x => x.survivorIndex).Max() + 1, 10); SurvivorCatalog.idealSurvivorOrder = SurvivorDefinitions.Select(x => x.survivorIndex).ToArray(); // Only contains not null survivors typeof(SurvivorCatalog).SetFieldValue("_allSurvivorDefs", SurvivorDefinitions .OrderBy(x => x.survivorIndex) .ToArray() ); // Contains null for index with no survivor typeof(SurvivorCatalog).SetFieldValue("survivorDefs", Enumerable.Range(0, SurvivorCatalog.survivorMaxCount) .Select(i => SurvivorDefinitions.FirstOrDefault(x => x.survivorIndex == (SurvivorIndex)i) ?? new SurvivorDef { survivorIndex = (SurvivorIndex)i }) .OrderBy(x => x.survivorIndex) .Select(x => x.bodyPrefab == null ? null : x) .ToArray() ); var bodyIndexToSurvivorIndex = new SurvivorIndex[BodyCatalog.bodyCount]; var survivorIndexToBodyIndex = new int[SurvivorCatalog.survivorMaxCount]; foreach (var survivorDef in SurvivorDefinitions) { int bodyIndex = survivorDef.bodyPrefab.GetComponent <CharacterBody>().bodyIndex; bodyIndexToSurvivorIndex[bodyIndex] = survivorDef.survivorIndex; survivorIndexToBodyIndex[(int)survivorDef.survivorIndex] = bodyIndex; } typeof(SurvivorCatalog).SetFieldValue("bodyIndexToSurvivorIndex", bodyIndexToSurvivorIndex); typeof(SurvivorCatalog).SetFieldValue("survivorIndexToBodyIndex", survivorIndexToBodyIndex); var parent = ViewablesCatalog.FindNode("/Survivors/") ?? new ViewablesCatalog.Node("Survivors", true); if (parent.parent == null) { ViewablesCatalog.AddNodeToRoot(parent); } foreach (var survivor in SurvivorDefinitions) { var name = survivor.survivorIndex.ToString(); var child = ViewablesCatalog.FindNode(parent.fullName + name) ?? new ViewablesCatalog.Node(name, false, parent); child.shouldShowUnviewed = userProfile => !string.IsNullOrEmpty(survivor.unlockableName) && survivor.survivorIndex < SurvivorIndex.Count && userProfile.HasSurvivorUnlocked(survivor.survivorIndex) && !userProfile.HasViewedViewable(child.fullName) ; } Debug.Log("Re-setting all survivor nodes, duplicates may occur. This is no problem."); ViewablesCatalog.AddNodeToRoot(parent); Debug.Log("Re-setting survivor nodes complete."); // Survivors over the builtin limit will be returned as null from SurvivorCatalog.GetSurvivorDef // This is a quick check if the MonoMod component is installed correctly. var overLimit = SurvivorDefinitions.FirstOrDefault(x => x.survivorIndex >= SurvivorIndex.Count); if (overLimit == null || SurvivorCatalog.GetSurvivorDef(overLimit.survivorIndex) != null) { return; } R2API.Logger.LogError($"{CenterText("!ERROR!")}"); R2API.Logger.LogError($"{CenterText("MonoMod component of R2API is not installed correctly!")}"); R2API.Logger.LogError($"{CenterText("Please copy Assembly-CSharp.R2API.mm.dll to BepInEx/monomod.")}"); }
public static void Init() { SurvivorDefinitions = new ObservableCollection <SurvivorDef>(new List <SurvivorDef> { new SurvivorDef { bodyPrefab = BodyCatalog.FindBodyPrefab("CommandoBody"), displayPrefab = Resources.Load <GameObject>("Prefabs/CharacterDisplays/CommandoDisplay"), descriptionToken = "COMMANDO_DESCRIPTION", primaryColor = new Color(0.929411769f, 0.5882353f, 0.07058824f), survivorIndex = SurvivorIndex.Commando }, new SurvivorDef { bodyPrefab = BodyCatalog.FindBodyPrefab("EngiBody"), displayPrefab = Resources.Load <GameObject>("Prefabs/CharacterDisplays/EngiDisplay"), descriptionToken = "ENGI_DESCRIPTION", primaryColor = new Color(0.372549027f, 0.8862745f, 0.5254902f), unlockableName = "Characters.Engineer", survivorIndex = SurvivorIndex.Engineer }, new SurvivorDef { bodyPrefab = BodyCatalog.FindBodyPrefab("HuntressBody"), displayPrefab = Resources.Load <GameObject>("Prefabs/CharacterDisplays/HuntressDisplay"), primaryColor = new Color(0.8352941f, 0.235294119f, 0.235294119f), descriptionToken = "HUNTRESS_DESCRIPTION", unlockableName = "Characters.Huntress", survivorIndex = SurvivorIndex.Huntress }, new SurvivorDef { bodyPrefab = BodyCatalog.FindBodyPrefab("MageBody"), displayPrefab = Resources.Load <GameObject>("Prefabs/CharacterDisplays/MageDisplay"), descriptionToken = "MAGE_DESCRIPTION", primaryColor = new Color(0.968627453f, 0.75686276f, 0.992156863f), unlockableName = "Characters.Mage", survivorIndex = SurvivorIndex.Mage }, new SurvivorDef { bodyPrefab = BodyCatalog.FindBodyPrefab("MercBody"), displayPrefab = Resources.Load <GameObject>("Prefabs/CharacterDisplays/MercDisplay"), descriptionToken = "MERC_DESCRIPTION", primaryColor = new Color(0.423529416f, 0.819607854f, 0.917647064f), unlockableName = "Characters.Mercenary", survivorIndex = SurvivorIndex.Merc }, new SurvivorDef { bodyPrefab = BodyCatalog.FindBodyPrefab("ToolbotBody"), displayPrefab = Resources.Load <GameObject>("Prefabs/CharacterDisplays/ToolbotDisplay"), descriptionToken = "TOOLBOT_DESCRIPTION", primaryColor = new Color(0.827451f, 0.768627465f, 0.3137255f), unlockableName = "Characters.Toolbot", survivorIndex = SurvivorIndex.Toolbot } }); SurvivorDefinitions.CollectionChanged += (sender, args) => { ReconstructSurvivors(); }; SurvivorCatalogReady?.Invoke(null, null); HasBeenInit = true; SurvivorAPI.survivorDefs.SetValue(null, SurvivorDefinitions.ToArray()); SurvivorAPI.allSurvivorDefs.SetValue(null, SurvivorDefinitions.ToArray()); SurvivorCatalog.idealSurvivorOrder = Enumerable.Range(0, SurvivorDefinitions.Count).Cast <SurvivorIndex>().ToArray(); ViewablesCatalog.Node node = new ViewablesCatalog.Node("Survivors", true, null); for (int i = 0; i < SurvivorDefinitions.Count; i++) { SurvivorDefinitions[i].survivorIndex = (SurvivorIndex)i; } foreach (var survivor in SurvivorDefinitions) { ViewablesCatalog.Node survivorEntryNode = new ViewablesCatalog.Node(survivor.survivorIndex.ToString(), false, node); survivorEntryNode.shouldShowUnviewed = userProfile => !userProfile.HasViewedViewable(survivorEntryNode.fullName) && userProfile.HasSurvivorUnlocked(survivor.survivorIndex) && !string.IsNullOrEmpty(survivor.unlockableName); } ViewablesCatalog.AddNodeToRoot(node); }