using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using MVR.FileManagementSecure;
using UnityEngine;
/// <summary>
/// Add to a Person atom. Writes the morph UIDs of the person's active gender to four text files in
/// Saves/PluginData/MorphDump: built-in morphs, built-in poses, custom morphs and custom poses.
/// Each line is "gender/uid". "Pose" means VaM's own pose morph flag (isPoseControl).
/// </summary>
public class MorphDump : MVRScript
{
private const string LogPrefix = "MorphDump: ";
private const string DumpFolder = "Saves/PluginData/MorphDump";
private JSONStorableString _status;
public override void Init()
{
try
{
if (containingAtom.type != "Person")
{
SuperController.LogError(LogPrefix + "add this plugin to a Person atom.");
return;
}
_status = new JSONStorableString("Status", "Waiting for morphs to load...");
CreateTextField(_status, true).height = 500f;
CreateButton("Dump Again").button.onClick.AddListener(Dump);
StartCoroutine(DumpWhenLoaded());
}
catch (Exception e)
{
SuperController.LogError(LogPrefix + e);
}
}
private IEnumerator DumpWhenLoaded()
{
yield return null;
while (SuperController.singleton.isLoading)
{
yield return null;
}
Dump();
}
private void Dump()
{
try
{
DAZCharacterSelector selector = containingAtom.GetStorableByID("geometry") as DAZCharacterSelector;
GenerateDAZMorphsControlUI morphsControl = selector != null ? selector.morphsControlUI : null;
List<DAZMorph> morphs = morphsControl != null ? morphsControl.GetMorphs() : null;
if (morphs == null || morphs.Count == 0)
{
Report("No morphs found. Wait for the person to finish loading, then press Dump Again.");
return;
}
string gender = selector.gender == DAZCharacterSelector.Gender.Male ? "male" : "female";
List<string> builtInMorphs = new List<string>();
List<string> builtInPoses = new List<string>();
List<string> customMorphs = new List<string>();
List<string> customPoses = new List<string>();
int skipped = 0;
for (int i = 0; i < morphs.Count; i++)
{
DAZMorph morph = morphs[i];
// Temporary morphs that only came in with a loaded scene or preset.
if (morph.isTransient)
{
skipped++;
continue;
}
// isRuntime = loaded from files (loose or .var), otherwise it ships with VaM.
List<string> target;
if (morph.isRuntime)
{
target = morph.isPoseControl ? customPoses : customMorphs;
}
else
{
target = morph.isPoseControl ? builtInPoses : builtInMorphs;
}
target.Add(gender + "/" + morph.uid);
}
FileManagerSecure.CreateDirectory(DumpFolder);
StringBuilder report = new StringBuilder();
report.Append("Saved to ").Append(DumpFolder).Append('\n');
WriteList(gender + "_BuiltInMorphs.txt", builtInMorphs, report);
WriteList(gender + "_BuiltInPoses.txt", builtInPoses, report);
WriteList(gender + "_CustomMorphs.txt", customMorphs, report);
WriteList(gender + "_CustomPoses.txt", customPoses, report);
if (skipped > 0)
{
report.Append("Skipped temporary morphs: ").Append(skipped).Append('\n');
}
Report(report.ToString());
}
catch (Exception e)
{
Report("Dump failed: " + e);
}
}
private static void WriteList(string fileName, List<string> lines, StringBuilder report)
{
lines.Sort(StringComparer.OrdinalIgnoreCase);
FileManagerSecure.WriteAllText(DumpFolder + "/" + fileName, string.Join("\r\n", lines.ToArray()));
report.Append(fileName).Append(": ").Append(lines.Count).Append('\n');
}
private void Report(string message)
{
_status.val = message;
SuperController.LogMessage(LogPrefix + message);
}
}