123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using BepInEx.Configuration;
- using UnityEngine;
- using static MeidoPhotoStudio.Plugin.ModelUtility;
- using Object = UnityEngine.Object;
- namespace MeidoPhotoStudio.Plugin;
- public class PropManager : IManager
- {
- public const string Header = "PROP";
- private static readonly ConfigEntry<bool> ModItemsOnlyValue;
- private static bool cubeActive = true;
- private static Dictionary<string, string> modFileToFullPath;
- private static bool cubeSmall;
- private readonly List<DragPointProp> propList = new();
- private readonly MeidoManager meidoManager;
- private int currentPropIndex;
- static PropManager() =>
- ModItemsOnlyValue = Configuration.Config.Bind(
- "Prop", "ModItemsOnly", false, "Disable waiting for and loading base game clothing");
- public PropManager(MeidoManager meidoManager)
- {
- this.meidoManager = meidoManager;
- meidoManager.BeginCallMeidos += OnBeginCallMeidos;
- meidoManager.EndCallMeidos += OnEndCallMeidos;
- Activate();
- }
- public event EventHandler PropSelectionChange;
- public event EventHandler FromPropSelect;
- public event EventHandler PropListChange;
- private static event EventHandler CubeActiveChange;
- private static event EventHandler CubeSmallChange;
- public static bool CubeActive
- {
- get => cubeActive;
- set
- {
- if (value == cubeActive)
- return;
- cubeActive = value;
- CubeActiveChange?.Invoke(null, EventArgs.Empty);
- }
- }
- public static bool CubeSmall
- {
- get => cubeSmall;
- set
- {
- if (value == cubeSmall)
- return;
- cubeSmall = value;
- CubeSmallChange?.Invoke(null, EventArgs.Empty);
- }
- }
- public static bool ModItemsOnly =>
- ModItemsOnlyValue.Value;
- public DragPointProp CurrentProp =>
- PropCount is 0 ? null : propList[CurrentPropIndex];
- public string[] PropNameList =>
- propList.Count is 0
- ? new[] { Translation.Get("systemMessage", "noProps") }
- : propList.Select(prop => prop.Name).ToArray();
- public int PropCount =>
- propList.Count;
- public int CurrentPropIndex
- {
- get => currentPropIndex;
- set
- {
- if (PropCount is 0)
- {
- currentPropIndex = 0;
- return;
- }
- if ((uint)value >= (uint)PropCount)
- throw new ArgumentOutOfRangeException(nameof(value));
- if (currentPropIndex == value)
- return;
- currentPropIndex = value;
- PropSelectionChange?.Invoke(this, EventArgs.Empty);
- }
- }
- private static Dictionary<string, string> ModFileToFullPath
- {
- get
- {
- if (modFileToFullPath is not null)
- return modFileToFullPath;
- var modFiles = Menu.GetModFiles();
- modFileToFullPath = new(modFiles.Length, StringComparer.OrdinalIgnoreCase);
- foreach (var mod in modFiles)
- {
- var key = Path.GetFileName(mod);
- if (!modFileToFullPath.ContainsKey(key))
- modFileToFullPath[key] = mod;
- }
- return modFileToFullPath;
- }
- }
- public bool AddFromPropInfo(PropInfo propInfo)
- {
- switch (propInfo.Type)
- {
- case PropInfo.PropType.Mod:
- ModItem modItem;
- if (!string.IsNullOrEmpty(propInfo.SubFilename))
- {
- modItem = ModItem.OfficialMod(ModFileToFullPath[propInfo.Filename]);
- modItem.BaseMenuFile = propInfo.SubFilename;
- }
- else
- {
- modItem = ModItem.Mod(propInfo.Filename);
- }
- return AddModProp(modItem);
- case PropInfo.PropType.MyRoom:
- return AddMyRoomProp(new() { ID = propInfo.MyRoomID, PrefabName = propInfo.Filename });
- case PropInfo.PropType.Bg:
- return AddBgProp(propInfo.Filename);
- case PropInfo.PropType.Odogu:
- return AddGameProp(propInfo.Filename);
- default:
- throw new ArgumentOutOfRangeException();
- }
- }
- public bool AddModProp(ModItem modItem)
- {
- var model = LoadMenuModel(modItem);
- if (!model)
- return false;
- var name = modItem.MenuFile;
- if (modItem.IsOfficialMod)
- name = Path.GetFileName(name) + ".menu"; // add '.menu' for partsedit support
- model.name = name;
- var dragPoint = AttachDragPoint(model);
- dragPoint.Info = PropInfo.FromModItem(modItem);
- AddProp(dragPoint);
- return true;
- }
- public bool AddMyRoomProp(MyRoomItem myRoomItem)
- {
- var model = LoadMyRoomModel(myRoomItem);
- if (!model)
- return false;
- model.name = Translation.Get("myRoomPropNames", myRoomItem.PrefabName);
- var dragPoint = AttachDragPoint(model);
- dragPoint.Info = PropInfo.FromMyRoom(myRoomItem);
- AddProp(dragPoint);
- return true;
- }
- public bool AddBgProp(string assetName)
- {
- var model = LoadBgModel(assetName);
- if (!model)
- return false;
- model.name = Translation.Get("bgNames", assetName);
- var dragPoint = AttachDragPoint(model);
- dragPoint.Info = PropInfo.FromBg(assetName);
- AddProp(dragPoint);
- return true;
- }
- public bool AddGameProp(string assetName)
- {
- var isMenu = assetName.EndsWith(".menu");
- var model = isMenu ? LoadMenuModel(assetName) : LoadGameModel(assetName);
- if (!model)
- return false;
- model.name = Translation.Get("propNames", isMenu ? Utility.HandItemToOdogu(assetName) : assetName, !isMenu);
- var dragPoint = AttachDragPoint(model);
- dragPoint.Info = PropInfo.FromGameProp(assetName);
- AddProp(dragPoint);
- return true;
- }
- public void CopyProp(int propIndex)
- {
- if ((uint)propIndex >= (uint)PropCount)
- throw new ArgumentOutOfRangeException(nameof(propIndex));
- AddFromPropInfo(propList[propIndex].Info);
- }
- public void DeleteAllProps()
- {
- foreach (var prop in propList)
- DestroyProp(prop);
- propList.Clear();
- CurrentPropIndex = 0;
- EmitPropListChange();
- }
- public void RemoveProp(int index)
- {
- if ((uint)index >= (uint)PropCount)
- throw new ArgumentOutOfRangeException(nameof(index));
- DestroyProp(propList[index]);
- propList.RemoveAt(index);
- CurrentPropIndex = Utility.Bound(CurrentPropIndex, 0, PropCount - 1);
- EmitPropListChange();
- }
- public void AttachProp(DragPointProp prop, AttachPoint point, int index)
- {
- if ((uint)index >= (uint)meidoManager.ActiveMeidoList.Count)
- return;
- var meido = meidoManager.ActiveMeidoList[index];
- prop.AttachTo(meido, point);
- }
- public void Update()
- {
- }
- public void Activate()
- {
- CubeSmallChange += OnCubeSmall;
- CubeActiveChange += OnCubeActive;
- }
- public void Deactivate()
- {
- DeleteAllProps();
- CubeSmallChange -= OnCubeSmall;
- CubeActiveChange -= OnCubeActive;
- }
- private DragPointProp AttachDragPoint(GameObject model)
- {
- var dragPoint = DragPoint.Make<DragPointProp>(PrimitiveType.Cube, Vector3.one * 0.12f);
- dragPoint.Initialize(() => model.transform.position, () => Vector3.zero);
- dragPoint.Set(model.transform);
- dragPoint.AddGizmo(0.45f, CustomGizmo.GizmoMode.World);
- dragPoint.ConstantScale = true;
- dragPoint.DragPointScale = CubeSmall ? DragPointGeneral.SmallCube : 1f;
- dragPoint.Delete += OnDeleteProp;
- dragPoint.Select += OnSelectProp;
- return dragPoint;
- }
- private void AddProp(DragPointProp dragPoint)
- {
- propList.Add(dragPoint);
- EmitPropListChange();
- }
- private void DestroyProp(DragPointProp prop)
- {
- if (!prop)
- return;
- prop.Delete -= OnDeleteProp;
- prop.Select -= OnSelectProp;
- Object.Destroy(prop.gameObject);
- }
- private void EmitPropListChange() =>
- PropListChange?.Invoke(this, EventArgs.Empty);
- private void OnBeginCallMeidos(object sender, EventArgs args)
- {
- foreach (var prop in propList.Where(p => p.AttachPointInfo.AttachPoint is not AttachPoint.None))
- prop.DetachTemporary();
- }
- private void OnEndCallMeidos(object sender, EventArgs args)
- {
- foreach (var prop in propList.Where(p => p.AttachPointInfo.AttachPoint is not AttachPoint.None))
- {
- var info = prop.AttachPointInfo;
- var meido = meidoManager.GetMeido(info.MaidGuid);
- prop.AttachTo(meido, info.AttachPoint, meido is null);
- }
- }
- private void OnDeleteProp(object sender, EventArgs args) =>
- RemoveProp(propList.IndexOf((DragPointProp)sender));
- private void OnSelectProp(object sender, EventArgs args)
- {
- CurrentPropIndex = propList.IndexOf((DragPointProp)sender);
- FromPropSelect?.Invoke(this, EventArgs.Empty);
- }
- private void OnCubeSmall(object sender, EventArgs args)
- {
- foreach (var dragPoint in propList)
- dragPoint.DragPointScale = CubeSmall ? DragPointGeneral.SmallCube : 1f;
- }
- private void OnCubeActive(object sender, EventArgs args)
- {
- foreach (var dragPoint in propList)
- dragPoint.gameObject.SetActive(CubeActive);
- }
- }
|