123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- using System;
- using System.IO;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Xml.Linq;
- using UnityEngine;
- using static TBody;
- namespace COM3D2.MeidoPhotoStudio.Plugin
- {
- internal class Meido
- {
- private const int maxMaids = 12;
- public static readonly PoseInfo DefaultPose =
- new PoseInfo(Constants.PoseGroupList[0], Constants.PoseDict[Constants.PoseGroupList[0]][0]);
- public static readonly string defaultFaceBlendSet = "通常";
- public static readonly string[] faceKeys = new string[24]
- {
- "eyeclose", "eyeclose2", "eyeclose3", "eyebig", "eyeclose6", "eyeclose5", "hitomih",
- "hitomis", "mayuha", "mayuw", "mayuup", "mayuv", "mayuvhalf", "moutha", "mouths",
- "mouthc", "mouthi", "mouthup", "mouthdw", "mouthhe", "mouthuphalf", "tangout",
- "tangup", "tangopen"
- };
- public static readonly string[] faceToggleKeys = new string[12]
- {
-
- "hoho2", "shock", "nosefook", "namida", "yodare", "toothoff",
-
- "tear1", "tear2", "tear3", "hohos", "hoho", "hohol"
- };
- public enum Curl
- {
- front, back, shift
- }
- private bool initialized;
- public event EventHandler<MeidoUpdateEventArgs> UpdateMeido;
- public int StockNo { get; }
- public Maid Maid { get; private set; }
- public TBody Body => Maid.body0;
- public MeidoDragPointManager IKManager { get; private set; }
- public Texture2D Portrait { get; private set; }
- public PoseInfo CachedPose { get; private set; } = DefaultPose;
- public string FaceBlendSet { get; private set; } = defaultFaceBlendSet;
- public int Slot { get; private set; }
- public bool Loading { get; private set; }
- public string FirstName => Maid.status.firstName;
- public string LastName => Maid.status.lastName;
- public bool Busy => Maid.IsBusy && Loading;
- public bool CurlingFront => Maid.IsItemChange("skirt", "めくれスカート")
- || Maid.IsItemChange("onepiece", "めくれスカート");
- public bool CurlingBack => Maid.IsItemChange("skirt", "めくれスカート後ろ")
- || Maid.IsItemChange("onepiece", "めくれスカート後ろ");
- public bool PantsuShift => Maid.IsItemChange("panz", "パンツずらし")
- || Maid.IsItemChange("mizugi", "パンツずらし");
- private bool freeLook;
- public bool FreeLook
- {
- get => freeLook;
- set
- {
- if (this.freeLook == value) return;
- this.freeLook = value;
- Body.trsLookTarget = this.freeLook ? null : GameMain.Instance.MainCamera.transform;
- OnUpdateMeido();
- }
- }
- public bool Stop
- {
- get
- {
- if (!Body.isLoadedBody) return true;
- else return !Maid.GetAnimation().isPlaying;
- }
- set
- {
- if (!Body.isLoadedBody || value == Stop) return;
- else
- {
- if (value) Maid.GetAnimation().Stop();
- else this.SetPose(this.CachedPose.Pose);
- OnUpdateMeido();
- }
- }
- }
- public bool IK
- {
- get => IKManager.Active;
- set
- {
- if (value == IKManager.Active) return;
- else IKManager.Active = value;
- }
- }
- public bool Bone
- {
- get => IKManager.IsBone;
- set
- {
- if (value == Bone) return;
- else IKManager.IsBone = value;
- OnUpdateMeido();
- }
- }
- public float[] BlendValuesBackup { get; private set; }
- public float[] BlendValues { get; private set; }
- public Quaternion DefaultEyeRotL { get; private set; }
- public Quaternion DefaultEyeRotR { get; private set; }
- public Meido(int stockMaidIndex)
- {
- this.StockNo = stockMaidIndex;
- this.Maid = GameMain.Instance.CharacterMgr.GetStockMaid(stockMaidIndex);
- this.Portrait = Maid.GetThumIcon();
- Maid.boAllProcPropBUSY = false;
- IKManager = new MeidoDragPointManager(this);
- IKManager.SelectMaid += (s, args) => OnUpdateMeido((MeidoUpdateEventArgs)args);
- }
- public void BeginLoad()
- {
- FreeLook = false;
- Maid.Visible = true;
- Body.boHeadToCam = true;
- Body.boEyeToCam = true;
- Body.SetBoneHitHeightY(-1000f);
- }
- public void Load(int slot)
- {
- Slot = slot;
- Loading = true;
- if (!Body.isLoadedBody)
- {
- Maid.DutPropAll();
- Maid.AllProcPropSeqStart();
- }
- GameMain.Instance.StartCoroutine(Load());
- }
- private IEnumerator Load()
- {
- while (Maid.IsBusy) yield return null;
- OnBodyLoad();
- }
- public void Unload()
- {
- if (Body.isLoadedBody)
- {
- Body.jbMuneL.enabled = true;
- Body.jbMuneR.enabled = true;
- }
- Body.MuneYureL(1f);
- Body.MuneYureR(1f);
- Body.SetMaskMode(MaskMode.None);
- Body.SetBoneHitHeightY(0f);
- Maid.Visible = false;
- IKManager.Destroy();
- }
- public void Deactivate()
- {
- Unload();
- Maid.SetPos(Vector3.zero);
- Maid.SetRot(Vector3.zero);
- Maid.SetPosOffset(Vector3.zero);
- Body.transform.localScale = Vector3.one;
- Maid.DelPrefabAll();
- Maid.ActiveSlotNo = -1;
- }
- public void SetPose(PoseInfo poseInfo)
- {
- CachedPose = poseInfo;
- SetPose(poseInfo.Pose);
- }
- public void SetPose(string pose)
- {
- if (!Body.isLoadedBody) return;
- if (pose.StartsWith(Constants.customPosePath))
- {
- byte[] poseBuffer = File.ReadAllBytes(pose);
- string hash = Path.GetFileName(pose).GetHashCode().ToString();
- Body.CrossFade(hash, poseBuffer, loop: true, fade: 0f);
- }
- else
- {
- string[] poseComponents = pose.Split(',');
- pose = poseComponents[0] + ".anm";
- Maid.CrossFade(pose, loop: true, val: 0f);
- Maid.GetAnimation().Play();
- if (poseComponents.Length > 1)
- {
- Maid.GetAnimation()[pose].time = float.Parse(poseComponents[1]);
- Maid.GetAnimation()[pose].speed = 0f;
- }
- }
- Maid.SetAutoTwistAll(true);
- SetMune();
- }
- public void CopyPose(Meido fromMeido)
- {
- byte[] poseBinary = fromMeido.SerializePose();
- string tag = $"copy_{fromMeido.Maid.status.guid}";
- Body.CrossFade(tag, poseBinary, false, true, false, 0f);
- Maid.SetAutoTwistAll(true);
- Maid.transform.rotation = fromMeido.Maid.transform.rotation;
- SetMune();
- }
- public void SetMune(bool drag = false)
- {
- bool momiOrPaizuri = CachedPose.Pose.Contains("_momi") || CachedPose.Pose.Contains("paizuri_");
- float onL = (drag || momiOrPaizuri) ? 0f : 1f;
- Body.MuneYureL(onL);
- Body.MuneYureR(onL);
- Body.jbMuneL.enabled = !drag;
- Body.jbMuneR.enabled = !drag;
- }
- public void SetHandPreset(string filename, bool right)
- {
- XDocument handDocument = XDocument.Load(filename);
- XElement handElement = handDocument.Element("FingerData");
- if (handElement.IsEmpty || handElement.Element("GameVersion").IsEmpty
- || handElement.Element("RightData").IsEmpty || handElement.Element("BinaryData").IsEmpty
- ) return;
- Stop = true;
- bool rightData = bool.Parse(handElement.Element("RightData").Value);
- string base64Data = handElement.Element("BinaryData").Value;
- byte[] handData = Convert.FromBase64String(base64Data);
- IKManager.DeserializeHand(handData, right, rightData != right);
- }
- public byte[] SerializePose(bool frameBinary = false)
- {
- CacheBoneDataArray cache = GetCacheBoneData();
- return frameBinary ? cache.GetFrameBinary(true, true) : cache.GetAnmBinary(true, true);
- }
- public void SetFaceBlendSet(string blendSet)
- {
- FaceBlendSet = blendSet;
- Maid.boMabataki = false;
- TMorph morph = Body.Face.morph;
- morph.EyeMabataki = 0f;
- morph.MulBlendValues(blendSet, 1f);
- morph.FixBlendValues_Face();
- OnUpdateMeido();
- }
- public void SetFaceBlendValue(string hash, float value)
- {
- TMorph morph = Body.Face.morph;
- if (hash == "nosefook")
- {
- morph.boNoseFook = value > 0f;
- Maid.boNoseFook = morph.boNoseFook;
- }
- else
- {
- bool gp01FBFace = morph.bodyskin.PartsVersion >= 120;
- float[] blendValues = hash.StartsWith("eyeclose") && !(gp01FBFace && (hash == "eyeclose3"))
- ? this.BlendValuesBackup
- : this.BlendValues;
- hash = Utility.GP01FbFaceHash(morph, hash);
- try
- {
- blendValues[(int)morph.hash[hash]] = value;
- }
- catch { }
- }
- Maid.boMabataki = false;
- morph.EyeMabataki = 0f;
- morph.FixBlendValues_Face();
- }
- public void SetMaskMode(MaskMode maskMode)
- {
- bool invisibleBody = !Body.GetMask(SlotID.body);
- Body.SetMaskMode(maskMode);
- if (invisibleBody) SetBodyMask(false);
- }
- public void SetBodyMask(bool enabled)
- {
- Hashtable table = Utility.GetFieldValue<TBody, Hashtable>(Body, "m_hFoceHide");
- foreach (SlotID bodySlot in MaidDressingPane.bodySlots)
- {
- table[bodySlot] = enabled;
- }
- if (Body.goSlot[19].m_strModelFileName.Contains("melala_body"))
- {
- table[SlotID.accHana] = enabled;
- }
- Body.FixMaskFlag();
- Body.FixVisibleFlag(false);
- }
- public void SetCurling(Curl curling, bool enabled)
- {
- string[] name = curling == Curl.shift
- ? new[] { "panz", "mizugi" }
- : new[] { "skirt", "onepiece" };
- if (enabled)
- {
- string action = curling == Curl.shift
- ? "パンツずらし" : curling == Curl.front
- ? "めくれスカート" : "めくれスカート後ろ";
- Maid.ItemChangeTemp(name[0], action);
- Maid.ItemChangeTemp(name[1], action);
- }
- else
- {
- Maid.ResetProp(name[0]);
- Maid.ResetProp(name[1]);
- }
- Maid.AllProcProp();
- }
- private CacheBoneDataArray GetCacheBoneData()
- {
- CacheBoneDataArray cache = this.Maid.gameObject.GetComponent<CacheBoneDataArray>();
- if (cache == null)
- {
- cache = this.Maid.gameObject.AddComponent<CacheBoneDataArray>();
- cache.CreateCache(this.Maid.body0.GetBone("Bip01"));
- }
- return cache;
- }
- private void OnBodyLoad()
- {
- if (!initialized)
- {
- TMorph faceMorph = Body.Face.morph;
- BlendValuesBackup = Utility.GetFieldValue<TMorph, float[]>(faceMorph, "BlendValuesBackup");
- BlendValues = Utility.GetFieldValue<TMorph, float[]>(faceMorph, "BlendValues");
- DefaultEyeRotL = Body.quaDefEyeL;
- DefaultEyeRotR = Body.quaDefEyeR;
- initialized = true;
- }
- IKManager.Initialize();
- IK = true;
- Stop = false;
- Bone = false;
- Loading = false;
- }
- public void OnUpdateMeido(MeidoUpdateEventArgs args = null)
- {
- this.UpdateMeido?.Invoke(this, args ?? MeidoUpdateEventArgs.Empty);
- }
- }
- public struct PoseInfo
- {
- public string PoseGroup { get; }
- public string Pose { get; }
- public bool CustomPose { get; }
- public PoseInfo(string poseGroup, string pose, bool customPose = false)
- {
- this.PoseGroup = poseGroup;
- this.Pose = pose;
- this.CustomPose = customPose;
- }
- }
- }
|