Unstable 0.17.0.0
This commit is contained in:
@@ -10,7 +10,7 @@ namespace Barotrauma
|
||||
{
|
||||
public CampaignMode Campaign { get; }
|
||||
|
||||
private readonly Dictionary<string, object> data = new Dictionary<string, object>();
|
||||
private readonly Dictionary<Identifier, object> data = new Dictionary<Identifier, object>();
|
||||
|
||||
public CampaignMetadata(CampaignMode campaign)
|
||||
{
|
||||
@@ -21,15 +21,15 @@ namespace Barotrauma
|
||||
{
|
||||
Campaign = campaign;
|
||||
|
||||
foreach (XElement subElement in element.Elements())
|
||||
foreach (var subElement in element.Elements())
|
||||
{
|
||||
if (string.Equals(subElement.Name.ToString(), "data", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
string identifier = subElement.GetAttributeString("key", string.Empty).ToLowerInvariant();
|
||||
Identifier identifier = subElement.GetAttributeIdentifier("key", Identifier.Empty);
|
||||
string value = subElement.GetAttributeString("value", string.Empty);
|
||||
string valueType = subElement.GetAttributeString("type", string.Empty);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(identifier) || string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(valueType))
|
||||
if (identifier.IsEmpty || string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(valueType))
|
||||
{
|
||||
DebugConsole.ThrowError("Unable to load value because one or more of the required attributes are empty.\n" +
|
||||
$"key: \"{identifier}\", value: \"{value}\", type: \"{valueType}\"");
|
||||
@@ -43,28 +43,20 @@ namespace Barotrauma
|
||||
DebugConsole.ThrowError($"Type for {identifier} not found ({valueType}).");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (type == typeof(float))
|
||||
else if (type == typeof(Identifier))
|
||||
{
|
||||
if (!float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out float floatValue))
|
||||
{
|
||||
DebugConsole.ThrowError($"Error in campaign metadata: could not parse \"{value}\" as a float.");
|
||||
continue;
|
||||
}
|
||||
data.Add(identifier, floatValue);
|
||||
data.Add(identifier, value.ToIdentifier());
|
||||
}
|
||||
else
|
||||
{
|
||||
data.Add(identifier, Convert.ChangeType(value, type));
|
||||
data.Add(identifier, Convert.ChangeType(value, type, NumberFormatInfo.InvariantInfo));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetValue(string identifier, object value)
|
||||
public void SetValue(Identifier identifier, object value)
|
||||
{
|
||||
identifier = identifier.ToLowerInvariant();
|
||||
|
||||
DebugConsole.Log($"Set the value \"{identifier}\" to {value}");
|
||||
|
||||
if (!data.ContainsKey(identifier))
|
||||
@@ -76,33 +68,32 @@ namespace Barotrauma
|
||||
data[identifier] = value;
|
||||
}
|
||||
|
||||
public float GetFloat(string identifier, float? defaultValue = null)
|
||||
public float GetFloat(Identifier identifier, float? defaultValue = null)
|
||||
{
|
||||
return (float)GetTypeOrDefault(identifier, typeof(float), defaultValue ?? 0f);
|
||||
}
|
||||
|
||||
public int GetInt(string identifier, int? defaultValue = null)
|
||||
public int GetInt(Identifier identifier, int? defaultValue = null)
|
||||
{
|
||||
return (int)GetTypeOrDefault(identifier, typeof(int), defaultValue ?? 0);
|
||||
}
|
||||
|
||||
public bool GetBoolean(string identifier, bool? defaultValue = null)
|
||||
public bool GetBoolean(Identifier identifier, bool? defaultValue = null)
|
||||
{
|
||||
return (bool)GetTypeOrDefault(identifier, typeof(bool), defaultValue ?? false);
|
||||
}
|
||||
|
||||
public string GetString(string identifier, string? defaultValue = null)
|
||||
public string GetString(Identifier identifier, string? defaultValue = null)
|
||||
{
|
||||
return (string)GetTypeOrDefault(identifier, typeof(string), defaultValue ?? string.Empty);
|
||||
}
|
||||
|
||||
public bool HasKey(string identifier)
|
||||
public bool HasKey(Identifier identifier)
|
||||
{
|
||||
identifier = identifier.ToLowerInvariant();
|
||||
return data.ContainsKey(identifier);
|
||||
}
|
||||
|
||||
private object GetTypeOrDefault(string identifier, Type type, object defaultValue)
|
||||
private object GetTypeOrDefault(Identifier identifier, Type type, object defaultValue)
|
||||
{
|
||||
object? value = GetValue(identifier);
|
||||
|
||||
@@ -122,7 +113,7 @@ namespace Barotrauma
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public object? GetValue(string identifier)
|
||||
public object? GetValue(Identifier identifier)
|
||||
{
|
||||
return data.ContainsKey(identifier) ? data[identifier] : null;
|
||||
}
|
||||
@@ -133,16 +124,16 @@ namespace Barotrauma
|
||||
|
||||
foreach (var (key, value) in data)
|
||||
{
|
||||
string valueStr = value?.ToString() ?? "";
|
||||
if (value?.GetType() == typeof(float))
|
||||
string valueStr = value.ToString() ?? throw new NullReferenceException();
|
||||
if (value is float f)
|
||||
{
|
||||
valueStr = ((float)value).ToString("G", CultureInfo.InvariantCulture);
|
||||
valueStr = f.ToString("G", CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
element.Add(new XElement("Data",
|
||||
new XAttribute("key", key),
|
||||
new XAttribute("value", valueStr),
|
||||
new XAttribute("type", value?.GetType())));
|
||||
new XAttribute("type", value.GetType())));
|
||||
}
|
||||
#if DEBUG
|
||||
DebugConsole.Log(element.ToString());
|
||||
|
||||
@@ -18,16 +18,14 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
internal class FactionPrefab : IDisposable
|
||||
internal class FactionPrefab : Prefab
|
||||
{
|
||||
public static List<FactionPrefab> Prefabs { get; set; }
|
||||
public readonly static PrefabCollection<FactionPrefab> Prefabs = new PrefabCollection<FactionPrefab>();
|
||||
|
||||
public string Name { get; }
|
||||
public LocalizedString Name { get; }
|
||||
|
||||
public string Description { get; }
|
||||
public string ShortDescription { get; }
|
||||
|
||||
public string Identifier { get; }
|
||||
public LocalizedString Description { get; }
|
||||
public LocalizedString ShortDescription { get; }
|
||||
|
||||
/// <summary>
|
||||
/// How low the reputation can drop on this faction
|
||||
@@ -52,17 +50,16 @@ namespace Barotrauma
|
||||
public Color IconColor { get; }
|
||||
#endif
|
||||
|
||||
private FactionPrefab(XElement element)
|
||||
public FactionPrefab(ContentXElement element, FactionsFile file) : base(file, element.GetAttributeIdentifier("identifier", string.Empty))
|
||||
{
|
||||
Identifier = element.GetAttributeString("identifier", string.Empty);
|
||||
MinReputation = element.GetAttributeInt("minreputation", -100);
|
||||
MaxReputation = element.GetAttributeInt("maxreputation", 100);
|
||||
InitialReputation = element.GetAttributeInt("initialreputation", 0);
|
||||
Name = element.GetAttributeString("name", null) ?? TextManager.Get($"faction.{Identifier}", returnNull: true) ?? "Unnamed";
|
||||
Description = element.GetAttributeString("description", null) ?? TextManager.Get($"faction.{Identifier}.description", returnNull: true) ?? "";
|
||||
ShortDescription = element.GetAttributeString("shortdescription", null) ?? TextManager.Get($"faction.{Identifier}.shortdescription", returnNull: true) ?? "";
|
||||
Name = element.GetAttributeString("name", null) ?? TextManager.Get($"faction.{Identifier}").Fallback("Unnamed");
|
||||
Description = element.GetAttributeString("description", null) ?? TextManager.Get($"faction.{Identifier}.description").Fallback("");
|
||||
ShortDescription = element.GetAttributeString("shortdescription", null) ?? TextManager.Get($"faction.{Identifier}.shortdescription").Fallback("");
|
||||
#if CLIENT
|
||||
foreach (XElement subElement in element.Elements())
|
||||
foreach (var subElement in element.Elements())
|
||||
{
|
||||
|
||||
if (subElement.Name.ToString().Equals("icon", StringComparison.OrdinalIgnoreCase))
|
||||
@@ -78,64 +75,12 @@ namespace Barotrauma
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void LoadFactions()
|
||||
{
|
||||
Prefabs?.ForEach(set => set.Dispose());
|
||||
Prefabs = new List<FactionPrefab>();
|
||||
IEnumerable<ContentFile> files = GameMain.Instance.GetFilesOfType(ContentType.Factions);
|
||||
foreach (ContentFile file in files)
|
||||
{
|
||||
XDocument doc = XMLExtensions.TryLoadXml(file.Path);
|
||||
XElement? rootElement = doc?.Root;
|
||||
|
||||
if (doc == null || rootElement == null) { continue; }
|
||||
|
||||
if (doc.Root.IsOverride())
|
||||
{
|
||||
Prefabs.Clear();
|
||||
DebugConsole.NewMessage($"Overriding all factions with '{file.Path}'", Color.Yellow);
|
||||
}
|
||||
|
||||
foreach (XElement element in rootElement.Elements())
|
||||
{
|
||||
bool isOverride = element.IsOverride();
|
||||
XElement sourceElement = isOverride ? element.FirstElement() : element;
|
||||
string elementName = sourceElement.Name.ToString().ToLowerInvariant();
|
||||
string identifier = sourceElement.GetAttributeString("identifier", null);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(identifier))
|
||||
{
|
||||
DebugConsole.ThrowError($"No identifier defined for the faction config '{elementName}' in file '{file.Path}'");
|
||||
continue;
|
||||
}
|
||||
|
||||
var existingParams = Prefabs.Find(set => set.Identifier == identifier);
|
||||
if (existingParams != null)
|
||||
{
|
||||
if (isOverride)
|
||||
{
|
||||
DebugConsole.NewMessage($"Overriding faction config '{identifier}' using the file '{file.Path}'", Color.Yellow);
|
||||
Prefabs.Remove(existingParams);
|
||||
}
|
||||
else
|
||||
{
|
||||
DebugConsole.ThrowError($"Duplicate faction config: '{identifier}' defined in {elementName} of '{file.Path}'");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
Prefabs.Add(new FactionPrefab(element));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
public override void Dispose()
|
||||
{
|
||||
#if CLIENT
|
||||
Icon?.Remove();
|
||||
Icon = null;
|
||||
#endif
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,13 +13,13 @@ namespace Barotrauma
|
||||
public const float MinReputationLossPerStolenItem = 0.5f;
|
||||
public const float MaxReputationLossPerStolenItem = 10.0f;
|
||||
|
||||
public string Identifier { get; }
|
||||
public Identifier Identifier { get; }
|
||||
public int MinReputation { get; }
|
||||
public int MaxReputation { get; }
|
||||
public int InitialReputation { get; }
|
||||
public CampaignMetadata Metadata { get; }
|
||||
|
||||
private readonly string metaDataIdentifier;
|
||||
private readonly Identifier metaDataIdentifier;
|
||||
|
||||
/// <summary>
|
||||
/// Reputation value normalized to the range of 0-1
|
||||
@@ -46,8 +46,8 @@ namespace Barotrauma
|
||||
if (increase != 0 && Character.Controlled != null)
|
||||
{
|
||||
Character.Controlled.AddMessage(
|
||||
TextManager.GetWithVariable("reputationgainnotification", "[reputationname]", Location?.Name ?? Faction.Prefab.Name),
|
||||
increase > 0 ? GUI.Style.Green : GUI.Style.Red,
|
||||
TextManager.GetWithVariable("reputationgainnotification", "[reputationname]", Location?.Name ?? Faction.Prefab.Name).Value,
|
||||
increase > 0 ? GUIStyle.Green : GUIStyle.Red,
|
||||
playSound: true, Identifier, increase, lifetime: 5.0f);
|
||||
}
|
||||
#endif
|
||||
@@ -80,23 +80,23 @@ namespace Barotrauma
|
||||
public readonly Location Location;
|
||||
|
||||
|
||||
public Reputation(CampaignMetadata metadata, Location location, string identifier, int minReputation, int maxReputation, int initialReputation)
|
||||
public Reputation(CampaignMetadata metadata, Location location, Identifier identifier, int minReputation, int maxReputation, int initialReputation)
|
||||
: this(metadata, null, location, identifier, minReputation, maxReputation, initialReputation)
|
||||
{
|
||||
}
|
||||
|
||||
public Reputation(CampaignMetadata metadata, Faction faction, int minReputation, int maxReputation, int initialReputation)
|
||||
: this(metadata, faction, null, $"faction.{faction.Prefab.Identifier}", minReputation, maxReputation, initialReputation)
|
||||
: this(metadata, faction, null, $"faction.{faction.Prefab.Identifier}".ToIdentifier(), minReputation, maxReputation, initialReputation)
|
||||
{
|
||||
}
|
||||
|
||||
private Reputation(CampaignMetadata metadata, Faction faction, Location location, string identifier, int minReputation, int maxReputation, int initialReputation)
|
||||
private Reputation(CampaignMetadata metadata, Faction faction, Location location, Identifier identifier, int minReputation, int maxReputation, int initialReputation)
|
||||
{
|
||||
System.Diagnostics.Debug.Assert(metadata != null);
|
||||
System.Diagnostics.Debug.Assert(faction != null || location != null);
|
||||
Metadata = metadata;
|
||||
Identifier = identifier.ToLowerInvariant();
|
||||
metaDataIdentifier = $"reputation.{Identifier}";
|
||||
Identifier = identifier;
|
||||
metaDataIdentifier = $"reputation.{Identifier}".ToIdentifier();
|
||||
MinReputation = minReputation;
|
||||
MaxReputation = maxReputation;
|
||||
InitialReputation = initialReputation;
|
||||
@@ -104,12 +104,12 @@ namespace Barotrauma
|
||||
Location = location;
|
||||
}
|
||||
|
||||
public string GetReputationName()
|
||||
public LocalizedString GetReputationName()
|
||||
{
|
||||
return GetReputationName(NormalizedValue);
|
||||
}
|
||||
|
||||
public static string GetReputationName(float normalizedValue)
|
||||
public static LocalizedString GetReputationName(float normalizedValue)
|
||||
{
|
||||
if (normalizedValue < HostileThreshold)
|
||||
{
|
||||
@@ -135,36 +135,36 @@ namespace Barotrauma
|
||||
{
|
||||
if (normalizedValue < HostileThreshold)
|
||||
{
|
||||
return GUI.Style.ColorReputationVeryLow;
|
||||
return GUIStyle.ColorReputationVeryLow;
|
||||
}
|
||||
else if (normalizedValue < 0.4f)
|
||||
{
|
||||
return GUI.Style.ColorReputationLow;
|
||||
return GUIStyle.ColorReputationLow;
|
||||
}
|
||||
else if (normalizedValue < 0.6f)
|
||||
{
|
||||
return GUI.Style.ColorReputationNeutral;
|
||||
return GUIStyle.ColorReputationNeutral;
|
||||
}
|
||||
else if (normalizedValue < 0.8f)
|
||||
{
|
||||
return GUI.Style.ColorReputationHigh;
|
||||
return GUIStyle.ColorReputationHigh;
|
||||
}
|
||||
return GUI.Style.ColorReputationVeryHigh;
|
||||
return GUIStyle.ColorReputationVeryHigh;
|
||||
}
|
||||
public string GetFormattedReputationText(bool addColorTags = false)
|
||||
public LocalizedString GetFormattedReputationText(bool addColorTags = false)
|
||||
{
|
||||
return GetFormattedReputationText(NormalizedValue, Value, addColorTags);
|
||||
}
|
||||
|
||||
public static string GetFormattedReputationText(float normalizedValue, float value, bool addColorTags = false)
|
||||
public static LocalizedString GetFormattedReputationText(float normalizedValue, float value, bool addColorTags = false)
|
||||
{
|
||||
string reputationName = GetReputationName(normalizedValue);
|
||||
string formattedReputation = TextManager.GetWithVariables("reputationformat",
|
||||
new string[] { "[reputationname]", "[reputationvalue]" },
|
||||
new string[] { reputationName, ((int)Math.Round(value)).ToString() });
|
||||
LocalizedString reputationName = GetReputationName(normalizedValue);
|
||||
LocalizedString formattedReputation = TextManager.GetWithVariables("reputationformat",
|
||||
("[reputationname]", reputationName),
|
||||
("[reputationvalue]", ((int)Math.Round(value)).ToString()));
|
||||
if (addColorTags)
|
||||
{
|
||||
formattedReputation = $"‖color:{XMLExtensions.ColorToString(GetReputationColor(normalizedValue))}‖{formattedReputation}‖end‖";
|
||||
formattedReputation = $"‖color:{XMLExtensions.ColorToString(GetReputationColor(normalizedValue))}‖"+ formattedReputation+"‖end‖";
|
||||
}
|
||||
return formattedReputation;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user