Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Items/Components/Holdable/IdCard.cs
1 using Barotrauma.IO;
2 using Microsoft.Xna.Framework;
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 
8 {
9  partial class IdCard
10  {
11  public struct OwnerAppearance
12  {
13  public Sprite Portrait;
14  public Vector2 SheetIndex;
16  public List<WearableSprite> Attachments;
17  public Color HairColor;
18  public Color FacialHairColor;
19  public Color SkinColor;
20 
21  public void ExtractJobPrefab(IReadOnlyDictionary<Identifier, string> tags)
22  {
23  if (!tags.TryGetValue("jobid".ToIdentifier(), out string jobId)) { return; }
24  if (!jobId.IsNullOrEmpty())
25  {
26  JobPrefab = JobPrefab.Get(jobId.ToIdentifier());
27  }
28  }
29 
30  public void ExtractAppearance(CharacterInfo characterInfo, IdCard idCard)
31  {
32  int disguisedHairIndex = idCard.OwnerHairIndex;
33  int disguisedBeardIndex = idCard.OwnerBeardIndex;
34  int disguisedMoustacheIndex = idCard.OwnerMoustacheIndex;
35  int disguisedFaceAttachmentIndex = idCard.OwnerFaceAttachmentIndex;
36  Color hairColor = idCard.OwnerHairColor;
37  Color facialHairColor = idCard.OwnerFacialHairColor;
38  Color skinColor = idCard.OwnerSkinColor;
39  var tags = idCard.OwnerTagSet;
40 
41  if ((characterInfo.HasSpecifierTags && !tags.Any()))
42  {
43  Portrait = null;
44  Attachments = null;
45  return;
46  }
47 
48  if (characterInfo.Ragdoll.MainElement?.Elements() is { } limbElements)
49  {
50  foreach (ContentXElement limbElement in limbElements)
51  {
52  if (!limbElement.GetAttributeString("type", "").Equals("head", StringComparison.OrdinalIgnoreCase)) { continue; }
53 
54  ContentXElement spriteElement = limbElement.GetChildElement("sprite");
55  if (spriteElement == null) { continue; }
56 
57  ContentPath contentPath = spriteElement.GetAttributeContentPath("texture");
58 
59  string spritePath = characterInfo.ReplaceVars(contentPath.Value);
60  string fileName = Path.GetFileNameWithoutExtension(spritePath);
61 
62  //go through the files in the directory to find a matching sprite
63  foreach (string file in Directory.GetFiles(Path.GetDirectoryName(spritePath)))
64  {
65  if (!file.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
66  {
67  continue;
68  }
69  string fileWithoutTags = Path.GetFileNameWithoutExtension(file);
70  fileWithoutTags = fileWithoutTags.Split('[', ']').First();
71  if (fileWithoutTags != fileName) { continue; }
72  Portrait = new Sprite(spriteElement, "", file) { RelativeOrigin = Vector2.Zero };
73  break;
74  }
75 
76  break;
77  }
78  }
79 
80  if (characterInfo.Wearables != null)
81  {
82  float baldnessChance = 0.1f;
83 
84  List<ContentXElement> createElementList(WearableType wearableType, float emptyCommonness = 1.0f)
86  characterInfo.FilterElements(characterInfo.Wearables, tags, wearableType),
87  wearableType, emptyCommonness);
88 
89  var disguisedHairs = createElementList(WearableType.Hair, baldnessChance);
90  var disguisedBeards = createElementList(WearableType.Beard);
91  var disguisedMoustaches = createElementList(WearableType.Moustache);
92  var disguisedFaceAttachments = createElementList(WearableType.FaceAttachment);
93 
94  ContentXElement getElementFromList(List<ContentXElement> list, int index)
95  => CharacterInfo.IsValidIndex(index, list)
96  ? list[index]
97  : null;
98 
99  var disguisedHairElement = getElementFromList(disguisedHairs, disguisedHairIndex);
100  var disguisedBeardElement = getElementFromList(disguisedBeards, disguisedBeardIndex);
101  var disguisedMoustacheElement = getElementFromList(disguisedMoustaches, disguisedMoustacheIndex);
102  var disguisedFaceAttachmentElement = getElementFromList(disguisedFaceAttachments, disguisedFaceAttachmentIndex);
103 
104  Attachments = new List<WearableSprite>();
105 
106  void loadAttachments(List<WearableSprite> attachments, ContentXElement element, WearableType wearableType)
107  {
108  foreach (var s in element?.GetChildElements("sprite") ?? Enumerable.Empty<ContentXElement>())
109  {
110  attachments.Add(new WearableSprite(s, wearableType));
111  }
112  }
113 
114  loadAttachments(Attachments, disguisedFaceAttachmentElement, WearableType.FaceAttachment);
115  loadAttachments(Attachments, disguisedBeardElement, WearableType.Beard);
116  loadAttachments(Attachments, disguisedMoustacheElement, WearableType.Moustache);
117  loadAttachments(Attachments, disguisedHairElement, WearableType.Hair);
118  }
119 
120  HairColor = hairColor;
121  FacialHairColor = facialHairColor;
122  SkinColor = skinColor;
123  }
124  }
125 
127  }
128 }
Stores information about the Character that is needed between rounds in the menu etc....
IEnumerable< ContentXElement > FilterElements(IEnumerable< ContentXElement > elements, ImmutableHashSet< Identifier > tags, WearableType? targetType=null)
static List< ContentXElement > AddEmpty(IEnumerable< ContentXElement > elements, WearableType type, float commonness=1)
static bool IsValidIndex(int index, List< ContentXElement > list)
string???????????? Value
Definition: ContentPath.cs:27
string? GetAttributeString(string key, string? def)
IEnumerable< ContentXElement > Elements()
ContentPath? GetAttributeContentPath(string key)
ContentXElement? GetChildElement(string name)
virtual ContentXElement? MainElement