123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 |
- using System;
- using System.IO;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using Ionic.Zlib;
- using BepInEx;
- namespace COM3D2.MeidoPhotoStudio.Plugin
- {
- using Input = InputManager;
- [BepInPlugin(pluginGuid, pluginName, pluginVersion)]
- [BepInDependency("org.bepinex.plugins.unityinjectorloader", BepInDependency.DependencyFlags.SoftDependency)]
- public class MeidoPhotoStudio : BaseUnityPlugin
- {
- private static readonly CameraMain mainCamera = GameMain.Instance.MainCamera;
- private static event EventHandler<ScreenshotEventArgs> ScreenshotEvent;
- private const string pluginGuid = "com.habeebweeb.com3d2.meidophotostudio";
- public const string pluginName = "MeidoPhotoStudio";
- public const string pluginVersion = "1.0.0";
- public const int sceneVersion = 1000;
- public const int kankyoMagic = -765;
- public static string pluginString = $"{pluginName} {pluginVersion}";
- public static bool EditMode => currentScene == Constants.Scene.Edit;
- public static event EventHandler<ScreenshotEventArgs> NotifyRawScreenshot;
- private WindowManager windowManager;
- private SceneManager sceneManager;
- private MeidoManager meidoManager;
- private EnvironmentManager environmentManager;
- private MessageWindowManager messageWindowManager;
- private LightManager lightManager;
- private PropManager propManager;
- private EffectManager effectManager;
- private CameraManager cameraManager;
- private static Constants.Scene currentScene;
- private bool initialized;
- private bool active;
- private bool uiActive;
- static MeidoPhotoStudio()
- {
- Input.Register(MpsKey.Screenshot, KeyCode.S, "Take screenshot");
- Input.Register(MpsKey.Activate, KeyCode.F6, "Activate/deactivate MeidoPhotoStudio");
- }
- private void Awake()
- {
- var harmony = HarmonyLib.Harmony.CreateAndPatchAll(typeof(AllProcPropSeqStartPatcher));
- harmony.PatchAll(typeof(BgMgrPatcher));
- ScreenshotEvent += OnScreenshotEvent;
- DontDestroyOnLoad(this);
- UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;
- UnityEngine.SceneManagement.SceneManager.activeSceneChanged += OnSceneChanged;
- }
- private void Start()
- {
- Constants.Initialize();
- Translation.Initialize(Translation.CurrentLanguage);
- }
- private void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
- {
- currentScene = (Constants.Scene)scene.buildIndex;
- }
- private void OnSceneChanged(Scene current, Scene next)
- {
- if (active) Deactivate(true);
- ResetCalcNearClip();
- }
- public byte[] SerializeScene(bool kankyo = false)
- {
- if (meidoManager.Busy) return null;
- byte[] compressedData;
- using (MemoryStream memoryStream = new MemoryStream())
- using (DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress))
- using (BinaryWriter binaryWriter = new BinaryWriter(deflateStream, System.Text.Encoding.UTF8))
- {
- binaryWriter.Write("MPS_SCENE");
- binaryWriter.Write(sceneVersion);
- binaryWriter.Write(kankyo ? kankyoMagic : meidoManager.ActiveMeidoList.Count);
- effectManager.Serialize(binaryWriter);
- environmentManager.Serialize(binaryWriter);
- lightManager.Serialize(binaryWriter);
- if (!kankyo)
- {
- messageWindowManager.Serialize(binaryWriter);
- // meidomanager has to be serialized before propmanager because reattached props will be in the
- // wrong place after a maid's pose is deserialized.
- meidoManager.Serialize(binaryWriter);
- }
- propManager.Serialize(binaryWriter);
- binaryWriter.Write("END");
- deflateStream.Close();
- compressedData = memoryStream.ToArray();
- }
- return compressedData;
- }
- public static byte[] DecompressScene(string filePath)
- {
- if (!File.Exists(filePath))
- {
- Utility.LogWarning($"Scene file '{filePath}' does not exist.");
- return null;
- }
- byte[] compressedData;
- using (FileStream fileStream = File.OpenRead(filePath))
- {
- if (Utility.IsPngFile(fileStream))
- {
- if (!Utility.SeekPngEnd(fileStream))
- {
- Utility.LogWarning($"'{filePath}' is not a PNG file");
- return null;
- }
- if (fileStream.Position == fileStream.Length)
- {
- Utility.LogWarning($"'{filePath}' contains no scene data");
- return null;
- }
- int dataLength = (int)(fileStream.Length - fileStream.Position);
- compressedData = new byte[dataLength];
- fileStream.Read(compressedData, 0, dataLength);
- }
- else
- {
- compressedData = new byte[fileStream.Length];
- fileStream.Read(compressedData, 0, compressedData.Length);
- }
- }
- return DeflateStream.UncompressBuffer(compressedData);
- }
- public void ApplyScene(string filePath)
- {
- if (meidoManager.Busy) return;
- byte[] sceneBinary = DecompressScene(filePath);
- if (sceneBinary == null) return;
- string header = string.Empty;
- string previousHeader = string.Empty;
- using (MemoryStream memoryStream = new MemoryStream(sceneBinary))
- using (BinaryReader binaryReader = new BinaryReader(memoryStream, System.Text.Encoding.UTF8))
- {
- try
- {
- if (binaryReader.ReadString() != "MPS_SCENE")
- {
- Utility.LogWarning($"'{filePath}' is not a {pluginName} scene");
- return;
- }
- var version = binaryReader.ReadInt32();
- if (version > sceneVersion)
- {
- Utility.LogWarning($"'{filePath}' is newer than the current version"
- + $"Scene's version: {version}, current version: {sceneVersion}");
- return;
- }
- binaryReader.ReadInt32(); // Number of Maids
- while ((header = binaryReader.ReadString()) != "END")
- {
- switch (header)
- {
- case MessageWindowManager.header:
- messageWindowManager.Deserialize(binaryReader);
- break;
- case EnvironmentManager.header:
- environmentManager.Deserialize(binaryReader);
- break;
- case CameraManager.header:
- environmentManager.Deserialize(binaryReader);
- break;
- case MeidoManager.header:
- meidoManager.Deserialize(binaryReader);
- break;
- case PropManager.header:
- propManager.Deserialize(binaryReader);
- break;
- case LightManager.header:
- lightManager.Deserialize(binaryReader);
- break;
- case EffectManager.header:
- effectManager.Deserialize(binaryReader);
- break;
- default: throw new Exception($"Unknown header '{header}'");
- }
- previousHeader = header;
- }
- }
- catch (Exception e)
- {
- Utility.LogError(
- $"Failed to deserialize scene '{filePath}' because {e.Message}"
- + $"\nCurrent header: '{header}'. Last header: '{previousHeader}'"
- );
- Utility.LogError(e.StackTrace);
- return;
- }
- }
- }
- public static void TakeScreenshot(ScreenshotEventArgs args) => ScreenshotEvent?.Invoke(null, args);
- public static void TakeScreenshot(string path = "", int superSize = -1, bool hideMaids = false)
- {
- TakeScreenshot(new ScreenshotEventArgs() { Path = path, SuperSize = superSize, HideMaids = hideMaids });
- }
- private void OnScreenshotEvent(object sender, ScreenshotEventArgs args)
- {
- StartCoroutine(Screenshot(args));
- }
- private void Update()
- {
- if (currentScene == Constants.Scene.Daily || currentScene == Constants.Scene.Edit)
- {
- if (Input.GetKeyDown(MpsKey.Activate))
- {
- if (active) Deactivate();
- else Activate();
- }
- if (active)
- {
- if (!Input.Control && !Input.GetKey(MpsKey.CameraLayer) && Input.GetKeyDown(MpsKey.Screenshot))
- {
- TakeScreenshot();
- }
- meidoManager.Update();
- cameraManager.Update();
- windowManager.Update();
- effectManager.Update();
- sceneManager.Update();
- }
- }
- }
- private IEnumerator Screenshot(ScreenshotEventArgs args)
- {
- // Hide UI and dragpoints
- GameObject gameMain = GameMain.Instance.gameObject;
- GameObject editUI = UTY.GetChildObject(GameObject.Find("UI Root"), "Camera");
- GameObject fpsViewer = UTY.GetChildObject(gameMain, "SystemUI Root/FpsCounter");
- GameObject sysDialog = UTY.GetChildObject(gameMain, "SystemUI Root/SystemDialog");
- GameObject sysShortcut = UTY.GetChildObject(gameMain, "SystemUI Root/SystemShortcut");
- // CameraUtility can hide the edit UI so keep its state for later
- bool editUIWasActive = editUI.activeSelf;
- uiActive = false;
- editUI.SetActive(false);
- fpsViewer.SetActive(false);
- sysDialog.SetActive(false);
- sysShortcut.SetActive(false);
- // Hide maid dragpoints and maids
- List<Meido> activeMeidoList = meidoManager.ActiveMeidoList;
- bool[] isIK = new bool[activeMeidoList.Count];
- bool[] isVisible = new bool[activeMeidoList.Count];
- for (int i = 0; i < activeMeidoList.Count; i++)
- {
- Meido meido = activeMeidoList[i];
- isIK[i] = meido.IK;
- if (meido.IK) meido.IK = false;
- // Hide the maid if needed
- if (args.HideMaids)
- {
- isVisible[i] = meido.Maid.Visible;
- meido.Maid.Visible = false;
- }
- }
- // Hide other drag points
- bool[] isCubeActive = {
- MeidoDragPointManager.CubeActive,
- PropManager.CubeActive,
- LightManager.CubeActive,
- EnvironmentManager.CubeActive
- };
- MeidoDragPointManager.CubeActive = false;
- PropManager.CubeActive = false;
- LightManager.CubeActive = false;
- EnvironmentManager.CubeActive = false;
- // hide gizmos
- GizmoRender.UIVisible = false;
- yield return new WaitForEndOfFrame();
- Texture2D rawScreenshot = null;
- if (args.InMemory)
- {
- // Take a screenshot directly to a Texture2D for immediate processing
- RenderTexture renderTexture = new RenderTexture(Screen.width, Screen.height, 24);
- RenderTexture.active = renderTexture;
- mainCamera.camera.targetTexture = renderTexture;
- mainCamera.camera.Render();
- rawScreenshot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
- rawScreenshot.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0, false);
- rawScreenshot.Apply();
- mainCamera.camera.targetTexture = null;
- RenderTexture.active = null;
- DestroyImmediate(renderTexture);
- }
- else
- {
- // Take Screenshot
- int[] defaultSuperSize = new[] { 1, 2, 4 };
- int selectedSuperSize = args.SuperSize < 1
- ? defaultSuperSize[(int)GameMain.Instance.CMSystem.ScreenShotSuperSize]
- : args.SuperSize;
- string path = string.IsNullOrEmpty(args.Path)
- ? Utility.ScreenshotFilename()
- : args.Path;
- Application.CaptureScreenshot(path, selectedSuperSize);
- }
- GameMain.Instance.SoundMgr.PlaySe("se022.ogg", false);
- yield return new WaitForEndOfFrame();
- // Show UI and dragpoints
- uiActive = true;
- editUI.SetActive(editUIWasActive);
- fpsViewer.SetActive(GameMain.Instance.CMSystem.ViewFps);
- sysDialog.SetActive(true);
- sysShortcut.SetActive(true);
- for (int i = 0; i < activeMeidoList.Count; i++)
- {
- Meido meido = activeMeidoList[i];
- if (isIK[i]) meido.IK = true;
- if (args.HideMaids && isVisible[i]) meido.Maid.Visible = true;
- }
- MeidoDragPointManager.CubeActive = isCubeActive[0];
- PropManager.CubeActive = isCubeActive[1];
- LightManager.CubeActive = isCubeActive[2];
- EnvironmentManager.CubeActive = isCubeActive[3];
- GizmoRender.UIVisible = true;
- if (args.InMemory && rawScreenshot)
- NotifyRawScreenshot?.Invoke(null, new ScreenshotEventArgs() { Screenshot = rawScreenshot });
- }
- private void OnGUI()
- {
- if (uiActive)
- {
- windowManager.DrawWindows();
- if (DropdownHelper.Visible) DropdownHelper.HandleDropdown();
- if (Modal.Visible) Modal.Draw();
- }
- }
- private void Initialize()
- {
- if (initialized) return;
- initialized = true;
- meidoManager = new MeidoManager();
- environmentManager = new EnvironmentManager();
- messageWindowManager = new MessageWindowManager();
- lightManager = new LightManager();
- propManager = new PropManager(meidoManager);
- sceneManager = new SceneManager(this);
- cameraManager = new CameraManager();
- effectManager = new EffectManager();
- effectManager.AddManager<BloomEffectManager>();
- effectManager.AddManager<DepthOfFieldEffectManager>();
- effectManager.AddManager<FogEffectManager>();
- effectManager.AddManager<VignetteEffectManager>();
- effectManager.AddManager<SepiaToneEffectManger>();
- effectManager.AddManager<BlurEffectManager>();
- meidoManager.BeginCallMeidos += (s, a) => uiActive = false;
- meidoManager.EndCallMeidos += (s, a) => uiActive = true;
- MaidSwitcherPane maidSwitcherPane = new MaidSwitcherPane(meidoManager);
- SceneWindow sceneWindow = new SceneWindow(sceneManager);
- windowManager = new WindowManager()
- {
- [Constants.Window.Main] = new MainWindow(meidoManager, propManager, lightManager)
- {
- [Constants.Window.Call] = new CallWindowPane(meidoManager),
- [Constants.Window.Pose] = new PoseWindowPane(meidoManager, maidSwitcherPane),
- [Constants.Window.Face] = new FaceWindowPane(meidoManager, maidSwitcherPane),
- [Constants.Window.BG] = new BGWindowPane(
- environmentManager, lightManager, effectManager, sceneWindow, cameraManager
- ),
- [Constants.Window.BG2] = new BG2WindowPane(meidoManager, propManager),
- [Constants.Window.Settings] = new SettingsWindowPane()
- },
- [Constants.Window.Message] = new MessageWindow(messageWindowManager),
- [Constants.Window.Save] = sceneWindow
- };
- }
- private void Activate()
- {
- if (!GameMain.Instance.SysDlg.IsDecided) return;
- if (!initialized) Initialize();
- else
- {
- meidoManager.Activate();
- environmentManager.Activate();
- cameraManager.Activate();
- propManager.Activate();
- lightManager.Activate();
- effectManager.Activate();
- messageWindowManager.Activate();
- windowManager.Activate();
- }
- SetNearClipPlane();
- uiActive = true;
- active = true;
- if (!EditMode)
- {
- GameObject dailyPanel = GameObject.Find("UI Root")?.transform.Find("DailyPanel")?.gameObject;
- if (dailyPanel) dailyPanel.SetActive(false);
- }
- else meidoManager.CallMeidos();
- }
- private void Deactivate(bool force = false)
- {
- if (meidoManager.Busy || SceneManager.Busy) return;
- SystemDialog sysDialog = GameMain.Instance.SysDlg;
- void exit()
- {
- sysDialog.Close();
- ResetCalcNearClip();
- meidoManager.Deactivate();
- environmentManager.Deactivate();
- cameraManager.Deactivate();
- propManager.Deactivate();
- lightManager.Deactivate();
- effectManager.Deactivate();
- messageWindowManager.Deactivate();
- windowManager.Deactivate();
- Input.Deactivate();
- Modal.Close();
- if (!EditMode)
- {
- GameObject dailyPanel = GameObject.Find("UI Root")?.transform.Find("DailyPanel")?.gameObject;
- dailyPanel?.SetActive(true);
- }
- Configuration.Config.Save();
- }
- if (sysDialog.IsDecided || EditMode || force)
- {
- uiActive = false;
- active = false;
- if (EditMode || force) exit();
- else
- {
- string exitMessage = string.Format(Translation.Get("systemMessage", "exitConfirm"), pluginName);
- sysDialog.Show(exitMessage, SystemDialog.TYPE.OK_CANCEL,
- f_dgOk: exit,
- f_dgCancel: () =>
- {
- sysDialog.Close();
- uiActive = true;
- active = true;
- }
- );
- }
- }
- }
- private void SetNearClipPlane()
- {
- mainCamera.StopAllCoroutines();
- mainCamera.m_bCalcNearClip = false;
- mainCamera.camera.nearClipPlane = 0.01f;
- }
- private void ResetCalcNearClip()
- {
- if (mainCamera.m_bCalcNearClip) return;
- mainCamera.StopAllCoroutines();
- mainCamera.m_bCalcNearClip = true;
- mainCamera.Start();
- }
- }
- public class ScreenshotEventArgs : EventArgs
- {
- public string Path { get; set; } = string.Empty;
- public int SuperSize { get; set; } = -1;
- public bool HideMaids { get; set; }
- public bool InMemory { get; set; } = false;
- public Texture2D Screenshot { get; set; }
- }
- }
|