using System; using System.Collections; using System.Collections.Generic; using Content.Shared.Research; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Prototypes; using Robust.Shared.Serialization; namespace Content.Shared.GameObjects.Components.Research { public class SharedLatheDatabaseComponent : Component, IEnumerable { public override string Name => "LatheDatabase"; public override uint? NetID => ContentNetIDs.LATHE_DATABASE; private List _recipes = new List(); /// /// Removes all recipes from the database if it's not static. /// /// Whether it could clear the database or not. public virtual void Clear() { _recipes.Clear(); } /// /// Adds a recipe to the database if it's not static. /// /// The recipe to be added. /// Whether it could be added or not public virtual void AddRecipe(LatheRecipePrototype recipe) { if(!Contains(recipe)) _recipes.Add(recipe); } /// /// Removes a recipe from the database if it's not static. /// /// The recipe to be removed. /// Whether it could be removed or not public virtual bool RemoveRecipe(LatheRecipePrototype recipe) { return _recipes.Remove(recipe); } /// /// Returns whether the database contains the recipe or not. /// /// The recipe to check /// Whether the database contained the recipe or not. public virtual bool Contains(LatheRecipePrototype recipe) { return _recipes.Contains(recipe); } /// /// Returns whether the database contains the recipe or not. /// /// The recipe id to check /// Whether the database contained the recipe or not. public virtual bool Contains(string id) { foreach (var recipe in _recipes) { if (recipe.ID == id) return true; } return false; } public override void ExposeData(ObjectSerializer serializer) { base.ExposeData(serializer); if (serializer.Reading) { var recipes = serializer.ReadDataField("recipes", new List()); var prototypeManager = IoCManager.Resolve(); foreach (var id in recipes) { if (!prototypeManager.TryIndex(id, out LatheRecipePrototype recipe)) continue; _recipes.Add(recipe); } } else if (serializer.Writing) { var recipes = GetRecipeIdList(); serializer.DataField(ref recipes, "recipes", new List()); } } public List GetRecipeIdList() { var list = new List(); foreach (var recipe in this) { list.Add(recipe.ID); } return list; } public IEnumerator GetEnumerator() { return _recipes.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } [NetSerializable, Serializable] public class LatheDatabaseState : ComponentState { public readonly List Recipes; public LatheDatabaseState(List recipes) : base(ContentNetIDs.LATHE_DATABASE) { Recipes = recipes; } } }