2021-02-27 04:12:09 +01:00
|
|
|
using System;
|
2020-08-13 14:40:27 +02:00
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
2019-11-21 16:37:15 -08:00
|
|
|
using Robust.Shared.GameObjects;
|
2021-07-12 01:32:10 -07:00
|
|
|
using Robust.Shared.GameStates;
|
2019-11-21 16:37:15 -08:00
|
|
|
using Robust.Shared.IoC;
|
2021-11-28 22:24:09 +11:00
|
|
|
using Robust.Shared.Log;
|
2019-11-21 16:37:15 -08:00
|
|
|
using Robust.Shared.Prototypes;
|
|
|
|
|
using Robust.Shared.Serialization;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2021-11-28 22:24:09 +11:00
|
|
|
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
|
2019-11-21 16:37:15 -08:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Shared.Cargo.Components
|
2019-11-21 16:37:15 -08:00
|
|
|
{
|
2021-07-12 01:32:10 -07:00
|
|
|
[NetworkedComponent()]
|
2022-02-16 00:23:23 -07:00
|
|
|
public abstract class SharedGalacticMarketComponent : Component, IEnumerable<CargoProductPrototype>, ISerializationHooks
|
2019-11-21 16:37:15 -08:00
|
|
|
{
|
2021-11-28 22:24:09 +11:00
|
|
|
[DataField("products", customTypeSerializer: typeof(PrototypeIdListSerializer<CargoProductPrototype>))]
|
2021-03-05 01:08:38 +01:00
|
|
|
protected List<string> _productIds = new();
|
|
|
|
|
|
|
|
|
|
protected readonly List<CargoProductPrototype> _products = new();
|
2019-11-21 16:37:15 -08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A read-only list of products.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IReadOnlyList<CargoProductPrototype> Products => _products;
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
void ISerializationHooks.AfterDeserialization()
|
|
|
|
|
{
|
|
|
|
|
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
|
|
|
|
|
|
2021-03-05 10:25:21 +01:00
|
|
|
_products.Clear();
|
2021-03-05 01:08:38 +01:00
|
|
|
|
|
|
|
|
foreach (var id in _productIds)
|
|
|
|
|
{
|
|
|
|
|
if (!prototypeManager.TryIndex(id, out CargoProductPrototype? product))
|
|
|
|
|
{
|
2021-11-28 22:24:09 +11:00
|
|
|
Logger.ErrorS("cargo", $"Unable to find {nameof(CargoProductPrototype)} for {id}");
|
2021-03-05 01:08:38 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_products.Add(product);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ISerializationHooks.BeforeSerialization()
|
|
|
|
|
{
|
2021-03-05 10:25:21 +01:00
|
|
|
_productIds.Clear();
|
2021-03-05 01:08:38 +01:00
|
|
|
|
|
|
|
|
foreach (var product in _products)
|
|
|
|
|
{
|
|
|
|
|
_productIds.Add(product.ID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-21 16:37:15 -08:00
|
|
|
public IEnumerator<CargoProductPrototype> GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
return _products.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
return GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a product from the string id;
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Product</returns>
|
2021-02-27 04:12:09 +01:00
|
|
|
public CargoProductPrototype? GetProduct(string productId)
|
2019-11-21 16:37:15 -08:00
|
|
|
{
|
|
|
|
|
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
|
2021-02-27 04:12:09 +01:00
|
|
|
if (!prototypeManager.TryIndex(productId, out CargoProductPrototype? product) || !_products.Contains(product))
|
2019-11-21 16:37:15 -08:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return product;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a list with the IDs of all products.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A list of product IDs</returns>
|
|
|
|
|
public List<string> GetProductIdList()
|
|
|
|
|
{
|
2021-03-05 01:08:38 +01:00
|
|
|
var productIds = new List<string>();
|
2019-11-21 16:37:15 -08:00
|
|
|
|
|
|
|
|
foreach (var product in _products)
|
|
|
|
|
{
|
|
|
|
|
productIds.Add(product.ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return productIds;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Serializable, NetSerializable]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class GalacticMarketState : ComponentState
|
2019-11-21 16:37:15 -08:00
|
|
|
{
|
|
|
|
|
public List<string> Products;
|
2021-07-12 01:32:10 -07:00
|
|
|
public GalacticMarketState(List<string> technologies)
|
2019-11-21 16:37:15 -08:00
|
|
|
{
|
|
|
|
|
Products = technologies;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|