Program.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Ionic.Zlib;
  6. using ExIni;
  7. using BepInEx;
  8. using UnityEngine;
  9. using UnityEngine.SceneManagement;
  10. using MyRoomCustom;
  11. namespace COM3D2.MeidoPhotoStudio.Converter
  12. {
  13. using static Plugin.BinaryExtensions;
  14. [BepInPlugin(pluginGuid, pluginName, pluginVersion)]
  15. public class SceneConverter : BaseUnityPlugin
  16. {
  17. private const string pluginGuid = "com.habeebweeb.com3d2.meidophotostudio.converter";
  18. public const string pluginName = "MeidoPhotoStudio Converter";
  19. public const string pluginVersion = "0.0.0";
  20. private readonly byte[] noThumb = Convert.FromBase64String(
  21. "iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7D" +
  22. "AcdvqGQAAAFOSURBVFhH3dJbjoMwEETRLIRP9r+zrCGpqJABY+x+2Ua5ys9EcteJNK/3sj7ws7E+j2ln8Q9+O7eE2Vjpq4kdJTsLTZRl" +
  23. "jBMLTZFdDTkLDZYVAQUWGia7Wy+z0ABZZfqWhbrK6rs1Fuoka442WChcJllss1CgTDgnYqEQmXxLykJOmWpIwUJmmXZFx0IGmWFCzUKq" +
  24. "J7b7FhYSvjIfN7JQ86Hnsp2FKm+dZ10sVHzuv+lloexCyMEAFkpHoq7FsBDuBJ76a1Y6EnXtT//li8/9N12sylvnWTur+dBz2cgSvjIf" +
  25. "t7BUT2z31azePwOpWQYT064oWGYTUw1JWU4Tk2+JWCEmJpxrswJNTLLYYIWbWHO0xupkYvXdW1ZXE6tMl1kDTOxuvcAaZmJFQM4abGJX" +
  26. "w4k1xcQyxs6aaGJHycaabmIJ82M9xMTo2VjP+izrF8NPHwq3SYqeAAAAAElFTkSuQmCC"
  27. );
  28. private static readonly Dictionary<string, PlacementData.Data> myrAssetNameToData
  29. = new Dictionary<string, PlacementData.Data>(StringComparer.InvariantCultureIgnoreCase);
  30. private static readonly string[] faceKeys = {
  31. "eyeclose", "eyeclose2", "eyeclose3", "eyeclose6", "hitomih", "hitomis", "mayuha",
  32. "mayuup", "mayuv", "mayuvhalf", "moutha", "mouths", "mouthdw", "mouthup", "tangout",
  33. "tangup", "eyebig", "eyeclose5", "mayuw", "mouthhe", "mouthc", "mouthi", "mouthuphalf",
  34. "tangopen",
  35. "namida", "tear1", "tear2", "tear3", "shock", "yodare", "hoho", "hoho2", "hohos", "hohol",
  36. "toothoff", "nosefook"
  37. };
  38. private static readonly string[] mpnAttachProps = {
  39. /* "", "", "", "", "", "", "", "", "", */
  40. "kousokuu_tekaseone_i_.menu", "kousokuu_tekasetwo_i_.menu", "kousokul_ashikaseup_i_.menu",
  41. "kousokuu_tekasetwo_i_.menu", "kousokul_ashikasedown_i_.menu", "kousokuu_tekasetwodown_i_.menu",
  42. "kousokuu_ushirode_i_.menu", "kousokuu_smroom_haritsuke_i_.menu"
  43. };
  44. private static readonly int[] bodyRotations =
  45. {
  46. 71, 44, 40, 41, 42, 43, 57, 68, 69, 46, 49, 47, 50, 52, 55, 53, 56, 92, 94, 93, 95, 45, 48, 51, 54
  47. };
  48. private static BepInEx.Logging.ManualLogSource Log;
  49. private static readonly string scenesPath = Plugin.Constants.scenesPath;
  50. private static readonly Vector3 DefaultSoftG = new Vector3(0f, -3f / 1000f, 0f);
  51. private bool active;
  52. private Rect windowRect = new Rect(30f, 30f, 300f, 200f);
  53. private void Awake()
  54. {
  55. DontDestroyOnLoad(this);
  56. if (!Directory.Exists(scenesPath)) Directory.CreateDirectory(scenesPath);
  57. Log = Logger;
  58. }
  59. private void Start()
  60. {
  61. SceneManager.sceneLoaded += OnSceneLoaded;
  62. foreach (var data in PlacementData.GetAllDatas(false))
  63. {
  64. string assetName = string.IsNullOrEmpty(data.assetName) ? data.resourceName : data.assetName;
  65. myrAssetNameToData[assetName] = data;
  66. }
  67. }
  68. private void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
  69. {
  70. int index = scene.buildIndex;
  71. active = index == 9 || index == 3;
  72. }
  73. private void OnGUI()
  74. {
  75. if (active)
  76. {
  77. windowRect.width = 300f;
  78. windowRect.height = 200f;
  79. windowRect.x = Mathf.Clamp(windowRect.x, 0, Screen.width - windowRect.width);
  80. windowRect.y = Mathf.Clamp(windowRect.y, 0, Screen.height - windowRect.height);
  81. windowRect = GUI.Window(0xEA4040, windowRect, GUIFunc, pluginName);
  82. }
  83. }
  84. private void GUIFunc(int id)
  85. {
  86. if (GUILayout.Button("Convert ModifiedMM")) ProcessModifedMM();
  87. if (GUILayout.Button("Convert ModifiedMM (Scene Manager)")) ProcessModifiedMMPng();
  88. GUILayout.Space(30f);
  89. if (GUILayout.Button("Convert modifedMM quickSave")) ProcessQuickSave();
  90. GUILayout.FlexibleSpace();
  91. if (GUILayout.Button("Close (Reopening requires restart)")) Destroy(this);
  92. GUI.DragWindow();
  93. }
  94. private void ProcessModifiedMMPng()
  95. {
  96. string modPath = Path.Combine(Paths.GameRootPath, "Mod");
  97. string scenePath = Path.Combine(modPath, "MultipleMaidsScene");
  98. string kankyoPath = Path.Combine(modPath, "MultipleMaidsScene");
  99. }
  100. private string GetModifiedMMSceneData(string pngPath)
  101. {
  102. return string.Empty;
  103. }
  104. private void ProcessQuickSave()
  105. {
  106. string sybarisPath = Path.Combine(Paths.GameRootPath, "Sybaris");
  107. string iniPath = BepInEx.Utility.CombinePaths(sybarisPath, "UnityInjector", "Config", "MultipleMaids.ini");
  108. IniFile mmIniFile = IniFile.FromFile(iniPath);
  109. IniSection sceneSection = mmIniFile.GetSection("scene");
  110. if (sceneSection != null)
  111. {
  112. if (sceneSection.HasKey("s9999"))
  113. {
  114. string sceneData = sceneSection["s9999"].Value;
  115. if (!string.IsNullOrEmpty(sceneData))
  116. {
  117. byte[] convertedSceneData = ProcessScene(sceneData, false);
  118. string path = Path.Combine(scenesPath, $"mmtempscene_{GetMMDateString(sceneData)}.png");
  119. SaveSceneToFile(path, convertedSceneData, noThumb);
  120. }
  121. }
  122. }
  123. }
  124. private void ProcessModifedMM()
  125. {
  126. string sybarisPath = Path.Combine(Paths.GameRootPath, "Sybaris");
  127. string iniPath = BepInEx.Utility.CombinePaths(sybarisPath, "UnityInjector", "Config", "MultipleMaids.ini");
  128. IniFile mmIniFile = IniFile.FromFile(iniPath);
  129. IniSection sceneSection = mmIniFile.GetSection("scene");
  130. if (sceneSection != null)
  131. {
  132. foreach (IniKey iniKey in sceneSection.Keys)
  133. {
  134. if (iniKey.Key.StartsWith("ss")) continue;
  135. int sceneIndex = int.Parse(iniKey.Key.Substring(1));
  136. bool kankyo = sceneIndex >= 10000;
  137. string sceneData = iniKey.Value;
  138. if (!string.IsNullOrEmpty(sceneData))
  139. {
  140. byte[] convertedSceneData = ProcessScene(sceneData, kankyo);
  141. string prefix = kankyo
  142. ? "mmkankyo"
  143. : sceneIndex == 9999
  144. ? "mmtempscene" : $"mmscene{sceneIndex}";
  145. string path = Path.Combine(scenesPath, $"{prefix}_{GetMMDateString(sceneData)}.png");
  146. byte[] thumbnail = noThumb;
  147. string screenshotKey = $"s{iniKey.Key}";
  148. if (sceneSection.HasKey(screenshotKey))
  149. {
  150. string screenshotBase64 = sceneSection[screenshotKey].Value;
  151. if (!string.IsNullOrEmpty(screenshotBase64))
  152. {
  153. thumbnail = Convert.FromBase64String(screenshotBase64);
  154. }
  155. }
  156. SaveSceneToFile(path, convertedSceneData, thumbnail);
  157. }
  158. }
  159. }
  160. }
  161. public static void SaveSceneToFile(string path, byte[] sceneData, byte[] thumbnailData)
  162. {
  163. using (FileStream fileStream = File.Create(path))
  164. {
  165. fileStream.Write(thumbnailData, 0, thumbnailData.Length);
  166. fileStream.Write(sceneData, 0, sceneData.Length);
  167. }
  168. }
  169. public static string GetMMDateString(string sceneData)
  170. {
  171. string dateString = sceneData.Split('_')[0].Split(',')[0];
  172. DateTime date = DateTime.Parse(dateString);
  173. return $"{date:yyyyMMddHHmm}";
  174. }
  175. public static byte[] ProcessScene(string sceneData, bool kankyo)
  176. {
  177. string[] strArray1 = sceneData.Split('_');
  178. string[] strArray2 = strArray1[1].Split(';');
  179. string[] strArray3 = strArray1[0].Split(',');
  180. string[] strArray4 = null;
  181. string[] strArray5 = null;
  182. string[] strArray6 = null;
  183. string[] strArray7 = null;
  184. if (strArray1.Length >= 5)
  185. {
  186. strArray4 = strArray1[2].Split(',');
  187. strArray5 = strArray1[3].Split(';');
  188. strArray6 = strArray1[4].Split(';');
  189. }
  190. if (strArray1.Length >= 6)
  191. {
  192. strArray7 = strArray1[5].Split(';');
  193. }
  194. using (MemoryStream memoryStream = new MemoryStream())
  195. using (DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress))
  196. using (BinaryWriter binaryWriter = new BinaryWriter(deflateStream, System.Text.Encoding.UTF8))
  197. {
  198. binaryWriter.Write("MPS_SCENE");
  199. binaryWriter.Write(Plugin.MeidoPhotoStudio.sceneVersion);
  200. binaryWriter.Write(kankyo ? Plugin.MeidoPhotoStudio.kankyoMagic : int.Parse(strArray3[1]));
  201. SerializeEnvironment(strArray3, binaryWriter, kankyo);
  202. SerializeLights(strArray3, strArray4, strArray5, strArray7, binaryWriter);
  203. SerializeEffect(strArray4, binaryWriter);
  204. SerializeProp(strArray3, strArray6, binaryWriter);
  205. if (!kankyo)
  206. {
  207. SerializeMessage(strArray3, binaryWriter);
  208. SerializeMaid(strArray2, strArray3, binaryWriter);
  209. }
  210. binaryWriter.Write("END");
  211. deflateStream.Close();
  212. return memoryStream.ToArray();
  213. }
  214. }
  215. private static void SerializeMaid(string[] strArray2, string[] strArray3, BinaryWriter binaryWriter)
  216. {
  217. binaryWriter.Write(Plugin.MeidoManager.header);
  218. // MM scene converted to MPS
  219. binaryWriter.Write(true);
  220. binaryWriter.Write(Plugin.Meido.meidoDataVersion);
  221. int numberOfMaids = strArray2.Length;
  222. binaryWriter.Write(numberOfMaids);
  223. /*
  224. TODO: Investigate why serialized maid data may only have 64 items.
  225. https://git.coder.horse/meidomustard/modifiedMM/src/master/MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.Update.cs#L3745
  226. The difference affects whether or not rotations are local or world.
  227. Certain body rotations would be missing as well particularly the toes.
  228. Other data like free look and attached items like hand/vag/anl would be missing.
  229. */
  230. bool gravityEnabled = false;
  231. for (int i = 0; i < numberOfMaids; i++)
  232. {
  233. using (MemoryStream memoryStream = new MemoryStream())
  234. using (BinaryWriter tempWriter = new BinaryWriter(memoryStream))
  235. {
  236. string[] maidData = strArray2[i].Split(':');
  237. tempWriter.WriteVector3(Utility.Vector3String(maidData[59])); // position
  238. tempWriter.WriteQuaternion(Utility.EulerString(maidData[58])); // rotation
  239. tempWriter.WriteVector3(Utility.Vector3String(maidData[60])); // scale
  240. // fingers
  241. for (int j = 0; j < 40; j++)
  242. {
  243. tempWriter.WriteQuaternion(Utility.EulerString(maidData[j]));
  244. }
  245. // toes
  246. for (int k = 0; k < 2; k++)
  247. {
  248. for (int j = 72 + k; j < 90; j += 2)
  249. {
  250. tempWriter.WriteQuaternion(Utility.EulerString(maidData[j]));
  251. }
  252. }
  253. // the rest of the limbs
  254. foreach (int j in bodyRotations)
  255. {
  256. tempWriter.WriteQuaternion(Utility.EulerString(maidData[j]));
  257. }
  258. tempWriter.WriteVector3(Utility.Vector3String(maidData[96])); // hip position
  259. // cached pose stuff
  260. tempWriter.Write("normal");
  261. tempWriter.Write("maid_stand01");
  262. tempWriter.Write(false);
  263. // eye rotation delta
  264. // MM saves the rotations directly so just save the identity
  265. tempWriter.WriteQuaternion(Quaternion.identity);
  266. tempWriter.WriteQuaternion(Quaternion.identity);
  267. string[] freeLookData = maidData[64].Split(',');
  268. bool isFreeLook = int.Parse(freeLookData[0]) == 1;
  269. tempWriter.Write(isFreeLook);
  270. if (isFreeLook)
  271. {
  272. tempWriter.WriteVector3(new Vector3(
  273. float.Parse(freeLookData[2]), 1f, float.Parse(freeLookData[1])
  274. ));
  275. }
  276. // head/eye to camera
  277. // MM does not changes these values so they're always true
  278. tempWriter.Write(true);
  279. tempWriter.Write(true);
  280. string[] faceValues = maidData[63].Split(',');
  281. tempWriter.Write("MPS_FACE");
  282. for (int j = 0; j < faceKeys.Length - 2; j++)
  283. {
  284. tempWriter.Write(faceKeys[j]);
  285. tempWriter.Write(float.Parse(faceValues[j]));
  286. }
  287. if (faceValues.Length > 65)
  288. {
  289. tempWriter.Write(faceKeys[faceKeys.Length - 1]);
  290. tempWriter.Write(float.Parse(faceValues[faceValues.Length - 1]));
  291. }
  292. tempWriter.Write("END_FACE");
  293. tempWriter.Write(true); // body visible
  294. // MM does not serialize clothing
  295. for (int j = 0; j < 29; j++) tempWriter.Write(true);
  296. Vector3 softG = new Vector3(
  297. float.Parse(strArray3[12]), float.Parse(strArray3[13]), float.Parse(strArray3[14])
  298. );
  299. bool hairGravityActive = softG != DefaultSoftG;
  300. tempWriter.Write(hairGravityActive);
  301. if (hairGravityActive)
  302. {
  303. // MM gravity affects all maids
  304. gravityEnabled = true;
  305. tempWriter.WriteVector3(softG * 90f);
  306. }
  307. // MM doesn't serialize skirt gravity
  308. tempWriter.Write(false);
  309. // MM does not serialize curling
  310. tempWriter.Write(false);
  311. tempWriter.Write(false);
  312. tempWriter.Write(false);
  313. string kousokuUpperMenu = string.Empty;
  314. string kousokuLowerMenu = string.Empty;
  315. int mpnIndex = int.Parse(maidData[65].Split(',')[0]);
  316. // MM can attach accvag, accanl and handitem stuff as well as kousoku_upper/lower
  317. // MPS attach prop is preferred for non kousoku_upper/lower props because unlike kousoku_upper/lower
  318. // props, accvag etc. props attach only to a single place.
  319. if (mpnIndex >= 9 && mpnIndex <= 16)
  320. {
  321. int actualIndex = mpnIndex - 9;
  322. if (mpnIndex == 12)
  323. {
  324. kousokuUpperMenu = mpnAttachProps[actualIndex];
  325. kousokuLowerMenu = mpnAttachProps[actualIndex - 1];
  326. }
  327. else if (mpnIndex == 13)
  328. {
  329. kousokuUpperMenu = mpnAttachProps[actualIndex + 1];
  330. kousokuLowerMenu = mpnAttachProps[actualIndex];
  331. }
  332. else
  333. {
  334. if (mpnIndex > 13) actualIndex++;
  335. string kousokuMenu = mpnAttachProps[actualIndex];
  336. if (mpnAttachProps[actualIndex][7] == 'u') kousokuUpperMenu = kousokuMenu;
  337. else kousokuLowerMenu = kousokuMenu;
  338. }
  339. }
  340. bool kousokuUpper = !string.IsNullOrEmpty(kousokuUpperMenu);
  341. tempWriter.Write(kousokuUpper);
  342. if (kousokuUpper) tempWriter.Write(kousokuUpperMenu);
  343. bool kousokuLower = !string.IsNullOrEmpty(kousokuLowerMenu);
  344. tempWriter.Write(kousokuLower);
  345. if (kousokuLower) tempWriter.Write(kousokuLowerMenu);
  346. binaryWriter.Write(memoryStream.Length);
  347. binaryWriter.Write(memoryStream.ToArray());
  348. }
  349. }
  350. binaryWriter.Write(gravityEnabled);
  351. }
  352. private static void SerializeProp(string[] strArray3, string[] strArray6, BinaryWriter binaryWriter)
  353. {
  354. binaryWriter.Write(Plugin.PropManager.header);
  355. binaryWriter.Write(Plugin.PropManager.propDataVersion);
  356. bool hasWProp = strArray3.Length > 37 && !string.IsNullOrEmpty(strArray3[37]);
  357. int numberOfProps = hasWProp ? 1 : 0;
  358. numberOfProps += strArray6 == null ? 0 : strArray6.Length - 1;
  359. binaryWriter.Write(numberOfProps);
  360. if (hasWProp)
  361. {
  362. // For the prop that spawns when you push (shift +) W
  363. binaryWriter.WriteVector3(new Vector3(
  364. float.Parse(strArray3[41]), float.Parse(strArray3[42]), float.Parse(strArray3[43])
  365. ));
  366. binaryWriter.WriteQuaternion(Quaternion.Euler(
  367. float.Parse(strArray3[38]), float.Parse(strArray3[39]), float.Parse(strArray3[40])
  368. ));
  369. binaryWriter.WriteVector3(new Vector3(
  370. float.Parse(strArray3[44]), float.Parse(strArray3[45]), float.Parse(strArray3[46])
  371. ));
  372. SerializeAttachPoint(binaryWriter);
  373. binaryWriter.Write(false); // shadow casting
  374. binaryWriter.Write(strArray3[37].Replace(' ', '_'));
  375. }
  376. if (strArray6 != null)
  377. {
  378. for (int i = 0; i < strArray6.Length - 1; i++)
  379. {
  380. string[] assetParts = strArray6[i].Split(',');
  381. string assetName = assetParts[0].Replace(' ', '_');
  382. bool shadowCasting = assetName.EndsWith(".menu");
  383. if (assetName.StartsWith("creative_"))
  384. {
  385. // modifiedMM my room creative prop
  386. // modifiedMM serializes the prefabName rather than the ID.
  387. assetName = assetName.Replace("creative_", String.Empty);
  388. assetName = $"MYR_{myrAssetNameToData[assetName].ID}#{assetName}";
  389. }
  390. else if (assetName.StartsWith("MYR_"))
  391. {
  392. // MM 23.0+ my room creative prop
  393. int assetID = int.Parse(assetName.Replace("MYR_", string.Empty));
  394. PlacementData.Data data = PlacementData.GetData(assetID);
  395. string asset = string.IsNullOrEmpty(data.assetName) ? data.resourceName : data.assetName;
  396. assetName = $"{assetName}#{asset}";
  397. }
  398. else if (assetName.Contains('#'))
  399. {
  400. if (assetName.Contains(".menu"))
  401. {
  402. // modifiedMM official mod prop
  403. string[] modComponents = assetParts[0].Split('#');
  404. string baseMenuFile = modComponents[0].Replace(' ', '_');
  405. string modItem = modComponents[1].Replace(' ', '_');
  406. assetName = $"{modItem}#{baseMenuFile}";
  407. }
  408. else
  409. {
  410. assetName = assetName.Split('#')[1].Replace(' ', '_');
  411. }
  412. }
  413. else if (assetName.StartsWith("BGOdogu", StringComparison.InvariantCultureIgnoreCase))
  414. {
  415. // I don't know why multiplemaids even prepends BG
  416. assetName = assetName.Substring(2);
  417. }
  418. binaryWriter.WriteVector3(new Vector3(
  419. float.Parse(assetParts[4]), float.Parse(assetParts[5]), float.Parse(assetParts[6])
  420. ));
  421. binaryWriter.WriteQuaternion(Quaternion.Euler(
  422. float.Parse(assetParts[1]), float.Parse(assetParts[2]), float.Parse(assetParts[3])
  423. ));
  424. binaryWriter.WriteVector3(new Vector3(
  425. float.Parse(assetParts[7]), float.Parse(assetParts[8]), float.Parse(assetParts[9])
  426. ));
  427. SerializeAttachPoint(binaryWriter);
  428. binaryWriter.Write(shadowCasting);
  429. binaryWriter.Write(assetName);
  430. }
  431. }
  432. }
  433. private static void SerializeEffect(string[] strArray4, BinaryWriter binaryWriter)
  434. {
  435. binaryWriter.Write(Plugin.EffectManager.header);
  436. if (strArray4 != null)
  437. {
  438. // bloom
  439. binaryWriter.Write(Plugin.BloomEffectManager.header);
  440. binaryWriter.Write(float.Parse(strArray4[2])); // intensity
  441. binaryWriter.Write((int)float.Parse(strArray4[3])); // blur iterations
  442. binaryWriter.WriteColour(new Color( // bloom threshold colour
  443. float.Parse(strArray4[4]), float.Parse(strArray4[5]), float.Parse(strArray4[6]), 1f
  444. ));
  445. binaryWriter.Write(int.Parse(strArray4[7]) == 1); // hdr
  446. binaryWriter.Write(int.Parse(strArray4[1]) == 1); // active
  447. // vignetting
  448. binaryWriter.Write(Plugin.VignetteEffectManager.header);
  449. binaryWriter.Write(float.Parse(strArray4[9])); // intensity
  450. binaryWriter.Write(float.Parse(strArray4[10])); // blur
  451. binaryWriter.Write(float.Parse(strArray4[11])); // blur spread
  452. binaryWriter.Write(float.Parse(strArray4[12])); // chromatic aberration
  453. binaryWriter.Write(int.Parse(strArray4[8]) == 1); // active
  454. // bokashi
  455. binaryWriter.Write(Plugin.BlurEffectManager.header);
  456. float blurSize = float.Parse(strArray4[13]);
  457. binaryWriter.Write(blurSize);
  458. binaryWriter.Write(blurSize > 0f);
  459. binaryWriter.Write(Plugin.SepiaToneEffectManger.header);
  460. binaryWriter.Write(int.Parse(strArray4[29]) == 1);
  461. if (strArray4.Length > 15)
  462. {
  463. binaryWriter.Write(Plugin.DepthOfFieldEffectManager.header);
  464. binaryWriter.Write(float.Parse(strArray4[16])); // focal length
  465. binaryWriter.Write(float.Parse(strArray4[17])); // focal size
  466. binaryWriter.Write(float.Parse(strArray4[18])); // aperture
  467. binaryWriter.Write(float.Parse(strArray4[19])); // max blur size
  468. binaryWriter.Write(int.Parse(strArray4[20]) == 1); // visualize focus
  469. binaryWriter.Write(int.Parse(strArray4[15]) == 1); // active
  470. binaryWriter.Write(Plugin.FogEffectManager.header);
  471. binaryWriter.Write(float.Parse(strArray4[22])); // fog distance
  472. binaryWriter.Write(float.Parse(strArray4[23])); // density
  473. binaryWriter.Write(float.Parse(strArray4[24])); // height scale
  474. binaryWriter.Write(float.Parse(strArray4[25])); // height
  475. binaryWriter.WriteColour(new Color( // fog colour
  476. float.Parse(strArray4[26]), float.Parse(strArray4[27]), float.Parse(strArray4[28]), 1f
  477. ));
  478. binaryWriter.Write(int.Parse(strArray4[21]) == 1); // active
  479. }
  480. }
  481. binaryWriter.Write(Plugin.EffectManager.footer);
  482. }
  483. private static void SerializeMessage(string[] strArray3, BinaryWriter binaryWriter)
  484. {
  485. binaryWriter.Write(Plugin.MessageWindowManager.header);
  486. bool showingMessage = false;
  487. string name = "Maid";
  488. string message = "Hello world";
  489. if (strArray3.Length > 16)
  490. {
  491. showingMessage = int.Parse(strArray3[34]) == 1;
  492. name = strArray3[35];
  493. message = strArray3[36].Replace("&kaigyo", "\n");
  494. // MM does not serialize message font size
  495. }
  496. binaryWriter.Write(showingMessage);
  497. binaryWriter.Write(25);
  498. binaryWriter.WriteNullableString(name);
  499. binaryWriter.WriteNullableString(message);
  500. }
  501. private static void SerializeLights(string[] strArray3, string[] strArray4, string[] strArray5, string[] strArray7, BinaryWriter binaryWriter)
  502. {
  503. // Lights
  504. binaryWriter.Write(Plugin.LightManager.header);
  505. int numberOfLights = 1;
  506. numberOfLights += strArray5 == null ? 0 : strArray5.Length - 1;
  507. binaryWriter.Write(numberOfLights);
  508. if (strArray3.Length > 16)
  509. {
  510. // Main Light
  511. /*
  512. 0 = Directional
  513. 1 = Spot
  514. 2 = Point
  515. 3 = Directional (Colour Mode)
  516. */
  517. int lightType = int.Parse(strArray3[17]);
  518. Color lightColour = new Color(
  519. float.Parse(strArray3[18]), float.Parse(strArray3[19]), float.Parse(strArray3[20]), 1f
  520. );
  521. Quaternion lightRotation = Quaternion.Euler(
  522. float.Parse(strArray3[21]), float.Parse(strArray3[22]), float.Parse(strArray3[23])
  523. );
  524. // MM uses spotAngle for both range and spotAngle based on which light type is used
  525. float intensity = float.Parse(strArray3[24]);
  526. float spotAngle = float.Parse(strArray3[25]);
  527. float range = lightType == 2 ? spotAngle / 5f : spotAngle; ;
  528. float shadowStrength = 0.098f;
  529. if (strArray4 != null) shadowStrength = float.Parse(strArray4[0]);
  530. for (int i = 0; i < 3; i++)
  531. {
  532. if (i == lightType || (i == 0 && lightType == 3))
  533. {
  534. SerializeLightProperty(
  535. binaryWriter, lightRotation, lightColour, intensity, spotAngle, range, shadowStrength
  536. );
  537. }
  538. else SerializeDefaultLight(binaryWriter);
  539. }
  540. if (strArray7 != null)
  541. {
  542. binaryWriter.WriteVector3(Utility.Vector3String(strArray7[0]));
  543. }
  544. else binaryWriter.WriteVector3(new Vector3(0f, 1.9f, 0.4f));
  545. binaryWriter.Write(lightType == 3 ? 0 : lightType);
  546. binaryWriter.Write(lightType == 3);
  547. binaryWriter.Write(false);
  548. // lightKage[0] is the only value that's serialized
  549. }
  550. if (strArray5 != null)
  551. {
  552. int otherLights = strArray5.Length - 1;
  553. for (int i = 0; i < otherLights; i++)
  554. {
  555. string[] lightProperties = strArray5[i].Split(',');
  556. int lightType = int.Parse(lightProperties[0]);
  557. Color lightColour = new Color(
  558. float.Parse(lightProperties[1]), float.Parse(lightProperties[2]),
  559. float.Parse(lightProperties[3]), 1f
  560. );
  561. Quaternion lightRotation = Quaternion.Euler(
  562. float.Parse(lightProperties[4]), float.Parse(lightProperties[5]), 18f
  563. );
  564. float intensity = float.Parse(lightProperties[6]);
  565. float spotAngle = float.Parse(lightProperties[7]);
  566. float range = lightType == 2 ? spotAngle / 5f : spotAngle;
  567. float shadowStrength = 0.098f;
  568. for (int j = 0; j < 3; j++)
  569. {
  570. if (j == lightType)
  571. {
  572. SerializeLightProperty(
  573. binaryWriter, lightRotation, lightColour, intensity, spotAngle, range, shadowStrength
  574. );
  575. }
  576. else SerializeDefaultLight(binaryWriter);
  577. }
  578. if (strArray7 != null)
  579. {
  580. binaryWriter.WriteVector3(Utility.Vector3String(strArray7[i + 1]));
  581. }
  582. else binaryWriter.WriteVector3(new Vector3(0f, 1.9f, 0.4f));
  583. binaryWriter.Write(lightType == 3 ? 0 : lightType);
  584. binaryWriter.Write(false);
  585. binaryWriter.Write(lightType == 3);
  586. }
  587. }
  588. }
  589. private static void SerializeEnvironment(string[] data, BinaryWriter binaryWriter, bool kankyo)
  590. {
  591. binaryWriter.Write(Plugin.EnvironmentManager.header);
  592. string bgAsset = "Theater";
  593. if (!int.TryParse(data[2], out _))
  594. {
  595. bgAsset = data[2].Replace(" ", "_");
  596. }
  597. binaryWriter.Write(bgAsset);
  598. binaryWriter.WriteVector3(new Vector3(
  599. float.Parse(data[6]), float.Parse(data[7]), float.Parse(data[8])
  600. ));
  601. binaryWriter.WriteQuaternion(Quaternion.Euler(
  602. float.Parse(data[3]), float.Parse(data[4]), float.Parse(data[5])
  603. ));
  604. binaryWriter.WriteVector3(new Vector3(
  605. float.Parse(data[9]), float.Parse(data[10]), float.Parse(data[11])
  606. ));
  607. binaryWriter.Write(kankyo);
  608. Vector3 cameraTargetPos = new Vector3(0f, 0.9f, 0f);
  609. float cameraDistance = 3f;
  610. Quaternion cameraRotation = Quaternion.identity;
  611. if (data.Length > 16)
  612. {
  613. cameraTargetPos = new Vector3(
  614. float.Parse(data[27]), float.Parse(data[28]), float.Parse(data[29])
  615. );
  616. cameraDistance = float.Parse(data[30]);
  617. cameraRotation = Quaternion.Euler(
  618. float.Parse(data[31]), float.Parse(data[32]), float.Parse(data[33])
  619. );
  620. }
  621. binaryWriter.WriteVector3(cameraTargetPos);
  622. binaryWriter.Write(cameraDistance);
  623. binaryWriter.WriteQuaternion(cameraRotation);
  624. }
  625. public static void SerializeAttachPoint(BinaryWriter binaryWriter)
  626. {
  627. binaryWriter.Write(0);
  628. binaryWriter.Write(-1);
  629. }
  630. public static void SerializeDefaultLight(BinaryWriter binaryWriter)
  631. {
  632. SerializeLightProperty(binaryWriter, Quaternion.Euler(40f, 180f, 0f), Color.white);
  633. }
  634. public static void SerializeLightProperty(
  635. BinaryWriter binaryWriter,
  636. Quaternion rotation, Color colour, float intensity = 0.95f, float range = 50f,
  637. float spotAngle = 50f, float shadowStrength = 0.1f
  638. )
  639. {
  640. binaryWriter.WriteQuaternion(rotation);
  641. binaryWriter.Write(intensity);
  642. binaryWriter.Write(range);
  643. binaryWriter.Write(spotAngle);
  644. binaryWriter.Write(shadowStrength);
  645. binaryWriter.WriteColour(colour);
  646. }
  647. }
  648. public static class Utility
  649. {
  650. public static Quaternion EulerString(string euler)
  651. {
  652. string[] data = euler.Split(',');
  653. return Quaternion.Euler(
  654. float.Parse(data[0]), float.Parse(data[1]), float.Parse(data[2])
  655. );
  656. }
  657. public static Vector3 Vector3String(string vector3)
  658. {
  659. string[] data = vector3.Split(',');
  660. return new Vector3(
  661. float.Parse(data[0]), float.Parse(data[1]), float.Parse(data[2])
  662. );
  663. }
  664. }
  665. }