Client LuaCsForBarotrauma
SetDataAction.cs
1 using System;
2 
3 namespace Barotrauma
4 {
9  {
10  public enum OperationType
11  {
12  Set,
13  Multiply,
14  Add
15  }
16 
17  public SetDataAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element) { }
18 
19  [Serialize(OperationType.Set, IsPropertySaveable.Yes, description: "Do you want to set the metadata to a specific value, multiply it, or add to it.")]
20  public OperationType Operation { get; set; }
21 
22  [Serialize(null, IsPropertySaveable.Yes, description: "Depending on the operation, the value you want to set the metadata to, multiply it with, or add to it.")]
23  public string Value { get; set; } = null!;
24 
25  [Serialize("", IsPropertySaveable.Yes, description: "Identifier of the metadata to set. Can be any arbitrary identifier, e.g. itemscollected, my_custom_event_state, specialnpckilled...")]
26  public Identifier Identifier { get; set; }
27 
28  private bool isFinished;
29 
30  public override bool IsFinished(ref string goTo)
31  {
32  return isFinished;
33  }
34  public override void Reset()
35  {
36  isFinished = false;
37  }
38 
39  public override void Update(float deltaTime)
40  {
41  if (isFinished) { return; }
42 
43  if (GameMain.GameSession?.GameMode is CampaignMode campaign)
44  {
45  object xmlValue = ConvertXMLValue(Value);
46  PerformOperation(campaign.CampaignMetadata, Identifier, xmlValue, Operation);
47  }
48 
49  isFinished = true;
50  }
51 
52  public static void PerformOperation(CampaignMetadata metadata, Identifier identifier, object value, OperationType operation)
53  {
54  if (metadata == null) { return; }
55 
56  object currentValue = metadata.GetValue(identifier);
57 
58  float? originalValue = ConvertValueToFloat(currentValue ?? 0);
59  float? newValue = ConvertValueToFloat(value);
60 
61  if ((originalValue == null || newValue == null) && operation != OperationType.Set)
62  {
63  DebugConsole.ThrowError($"Tried to perform numeric operations to a non number via SetDataAction (Existing: {currentValue?.GetType()}, New: {value.GetType()})");
64  return;
65  }
66 
67  switch (operation)
68  {
69  case OperationType.Set:
70  metadata.SetValue(identifier, value);
71  break;
72  case OperationType.Add:
73  metadata.SetValue(identifier, originalValue + newValue ?? 0);
74  break;
75  case OperationType.Multiply:
76  metadata.SetValue(identifier, originalValue * newValue ?? 0);
77  break;
78  }
79  }
80 
81  private static float? ConvertValueToFloat(object value)
82  {
83  if (value is float || value is int)
84  {
85  return (float?) Convert.ChangeType(value, typeof(float));
86  }
87 
88  return null;
89  }
90 
91  public static object ConvertXMLValue(string value)
92  {
93  if (bool.TryParse(value, out bool b))
94  {
95  return b;
96  }
97 
98  if (float.TryParse(value, out float f))
99  {
100  return f;
101  }
102 
103  return value;
104  }
105 
106  public override string ToDebugString()
107  {
108  return $"{ToolBox.GetDebugSymbol(isFinished)} {nameof(SetDataAction)} -> (Identifier: {Identifier.ColorizeObject()}, Value: {ConvertXMLValue(Value).ColorizeObject()}, Operation: {Operation.ColorizeObject()})";
109  }
110  }
111 }
static GameSession?? GameSession
Definition: GameMain.cs:88
Sets a campaign metadata value. The metadata can be any arbitrary data you want to save: for example,...
Definition: SetDataAction.cs:9
override void Update(float deltaTime)
static object ConvertXMLValue(string value)
SetDataAction(ScriptedEvent parentEvent, ContentXElement element)
override void Reset()
override bool IsFinished(ref string goTo)
Has the action finished.
static void PerformOperation(CampaignMetadata metadata, Identifier identifier, object value, OperationType operation)
override string ToDebugString()
Rich test to display in debugdraw