/// <summary> /// Add a <see cref="CustomPieceTable"/> to the game.<br /> /// Checks if the custom piece table is valid and unique and adds it to the list of custom piece tables. /// </summary> /// <param name="customPieceTable">The custom piece table to add.</param> /// <returns>true if the custom piece table was added to the manager.</returns> public bool AddPieceTable(CustomPieceTable customPieceTable) { // Assert if (!customPieceTable.IsValid()) { Logger.LogWarning($"Custom piece {customPieceTable} is not valid"); return(false); } if (PieceTables.Contains(customPieceTable)) { Logger.LogWarning($"Piece table {customPieceTable} already added"); return(false); } // Add the prefab to the PrefabManager if (!PrefabManager.Instance.AddPrefab(customPieceTable.PieceTablePrefab, customPieceTable.SourceMod)) { return(false); } // Create all custom categories on that table foreach (var category in customPieceTable.Categories) { AddPieceCategory(customPieceTable.ToString(), category); } // Add the custom table to the PieceManager PieceTables.Add(customPieceTable); PieceTableMap.Add(customPieceTable.ToString(), customPieceTable.PieceTable); return(true); }
/// <summary> /// Create piece categories per table if custom categories were added. /// </summary> private void CreatePieceTableCategories() { if (!PieceCategories.Any()) { return; } //Logger.LogInfo($"Adding {PieceCategories.Count} custom piece table categories"); // All piece tables using categories foreach (var table in PieceTableMap.Values.Where(x => x.m_useCategories)) { try { // Create category map if not present PieceTableCategoriesMap.TryGetValue(table.name, out var categories); if (categories == null) { categories = new PieceTableCategories(); PieceTableCategoriesMap.Add(table.name, categories); } // Remap shortcut categories to actual table (e.g. categories added to "Hammer" must be added to "_HammerPieceTable") if (PieceTableNameMap.ContainsValue(table.name)) { string tableItemName = PieceTableNameMap.FirstOrDefault(x => x.Value.Equals(table.name)).Key; if (PieceTableCategoriesMap.ContainsKey(tableItemName)) { foreach (var cat in PieceTableCategoriesMap[tableItemName]) { if (!categories.ContainsKey(cat.Key)) { categories.Add(cat.Key, cat.Value); } } PieceTableCategoriesMap.Remove(tableItemName); } } // Add vanilla categories for vanilla tables CustomPieceTable customTable = PieceTables.FirstOrDefault(x => x.PieceTable.name.Equals(table.name)); if (customTable == null) { for (int i = 0; i < (int)Piece.PieceCategory.Max; i++) { string categoryName = Enum.GetName(typeof(Piece.PieceCategory), i); if (!categories.ContainsKey(categoryName)) { categories.Add(categoryName, (Piece.PieceCategory)i); } } } // Add empty lists up to the max categories count if (table.m_availablePieces.Count < (int)PieceCategoryMax) { for (int i = table.m_availablePieces.Count; i < (int)PieceCategoryMax; i++) { table.m_availablePieces.Add(new List <Piece>()); } } // Resize selectedPiece array Array.Resize(ref table.m_selectedPiece, table.m_availablePieces.Count); // Set first available category table.m_selectedCategory = categories.Values.Min(); Logger.LogDebug($"Added categories for table {table.name}"); } catch (Exception ex) { Logger.LogError($"Error while adding categories for table {table.name}: {ex}"); } } }