using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
public static class PersonGender
{
const string _atomsKey = "atoms";
const string _idKey = "id";
const string _atomTypeKey = "type";
const string _storablesKey = "storables";
const string _characterKey = "character";
const string _personType = "Person";
const string _geometryStorableId = "geometry";
const string _presetLabel = "(preset)";
const string _male = "Male";
const string _female = "Female";
const string _basisStock = "stock list";
const string _basisStorables = "storables (weaker)";
// Storables only a female body has. Add more here if you find reliable ones.
// Note: They're not on every save, this is just an accuracy step because it depends what is actually stored.
static readonly string[] _femaleOnlyStorables =
[
"BreastPhysicsMesh",
"BreastInOut"
];
static readonly string[] _femaleBaseStock =
[
"Female Dummy",
"Female Custom",
"Candy",
"Evey",
"Janie",
"Kayla",
"Lexi",
"Maria",
"Mia",
"Simone",
"Tara",
"Tina"
];
static readonly string[] _maleBaseStock =
[
"Male Dummy",
"Male Custom",
"Lee",
"Julian"
];
static readonly string[] _futaBaseStock =
[
"Futa Lexi",
"Futa Maria",
"Futa Mia",
"Futa Simone",
"Futa Kayla"
];
static readonly string[] _femaleNumberedStock = Numbered("Female ", 1, 17);
static readonly string[] _maleNumberedStock = Numbered("Male ", 1, 9);
// Display names of VaM's stock characters, taken from the in-game character list.
// Futa characters live in the male container, so they count as male.
// Static fields initialise in textual order: keep these sets below the arrays they splat,
// or they are built from null.
static readonly HashSet<string> _maleStock =
new(
[
.. _maleBaseStock,
.. _maleNumberedStock,
.. _futaBaseStock
],
StringComparer.OrdinalIgnoreCase
);
static readonly HashSet<string> _femaleStock =
new(
[
.. _femaleBaseStock,
.. _femaleNumberedStock
],
StringComparer.OrdinalIgnoreCase
);
public static int Main(string[] args)
{
if (args.Length != 1)
{
Console.Error.WriteLine("Usage: PersonGender <scene.json | preset.vap>");
return 1;
}
using JsonDocument scene = JsonDocument.Parse(File.ReadAllText(args[0]));
foreach (JsonElement atom in PersonAtoms(scene.RootElement))
{
(string gender, string character, string basis) = GenderOf(atom);
string label = ReadString(atom, _idKey);
Console.WriteLine($"{(label.Length > 0 ? label : _presetLabel)}: {gender} (character '{character}', by {basis})");
}
return 0;
}
static IEnumerable<JsonElement> PersonAtoms(JsonElement scene)
{
// An appearance preset (.vap) has no atoms: its root is the Person's storables.
if (scene.TryGetProperty(_storablesKey, out _))
{
return [scene];
}
return Items(scene, _atomsKey)
.Where(atom => ReadString(atom, _atomTypeKey) == _personType);
}
static (string Gender, string Character, string Basis) GenderOf(JsonElement personAtom)
{
List<JsonElement> storables = [.. Items(personAtom, _storablesKey)];
JsonElement geometry = storables.FirstOrDefault(s => ReadString(s, _idKey) == _geometryStorableId);
string character = geometry.ValueKind == JsonValueKind.Object
? ReadString(geometry, _characterKey)
: string.Empty;
if (_maleStock.Contains(character))
{
return (_male, character, _basisStock);
}
if (_femaleStock.Contains(character))
{
return (_female, character, _basisStock);
}
HashSet<string> ids = [.. storables.Select(s => ReadString(s, _idKey))];
bool hasFemaleStorable = _femaleOnlyStorables.Any(ids.Contains);
string gender = hasFemaleStorable ? _female : _male;
return (gender, character, _basisStorables);
}
static string[] Numbered(string prefix, int first, int count)
{
return [.. Enumerable.Range(first, count).Select(i => prefix + i)];
}
/// <summary>The elements of an array property, or nothing when it is missing or not an array.</summary>
static IEnumerable<JsonElement> Items(JsonElement parent, string name)
{
if (parent.TryGetProperty(name, out JsonElement array) && array.ValueKind == JsonValueKind.Array)
{
return array.EnumerateArray();
}
return [];
}
/// <summary>A string property, or an empty string when it is missing or not a string.</summary>
static string ReadString(JsonElement parent, string name)
{
if (parent.ValueKind == JsonValueKind.Object
&& parent.TryGetProperty(name, out JsonElement value)
&& value.ValueKind == JsonValueKind.String)
{
return value.GetString() ?? string.Empty;
}
return string.Empty;
}
}