Program.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. [BepInPlugin(pluginGuid, pluginName, pluginVersion)]
  14. public class SceneConverter : BaseUnityPlugin
  15. {
  16. private const string pluginGuid = "com.habeebweeb.com3d2.meidophotostudio.converter";
  17. public const string pluginName = "MeidoPhotoStudio Converter";
  18. public const string pluginVersion = "0.0.0";
  19. private readonly byte[] noThumb = {
  20. 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00,
  21. 0x00, 0x32, 0x00, 0x00, 0x00, 0x32, 0x08, 0x02, 0x00, 0x00, 0x00, 0x91, 0x5D, 0x1F, 0xE6, 0x00, 0x00, 0x00,
  22. 0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xAE, 0xCE, 0x1C, 0xE9, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, 0x4D, 0x41,
  23. 0x00, 0x00, 0xB1, 0x8F, 0x0B, 0xFC, 0x61, 0x05, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00,
  24. 0x0E, 0xC3, 0x00, 0x00, 0x0E, 0xC3, 0x01, 0xC7, 0x6F, 0xA8, 0x64, 0x00, 0x00, 0x01, 0x4E, 0x49, 0x44, 0x41,
  25. 0x54, 0x58, 0x47, 0xDD, 0xD2, 0x5B, 0x8E, 0x83, 0x30, 0x10, 0x44, 0xD1, 0x2C, 0x84, 0x4F, 0xF6, 0xBF, 0xB3,
  26. 0xAC, 0x21, 0xA9, 0xA8, 0x90, 0x01, 0x63, 0xEC, 0x7E, 0xD9, 0x46, 0xB9, 0xCA, 0xCF, 0x44, 0x72, 0xD7, 0x89,
  27. 0x34, 0xAF, 0xF7, 0xB2, 0x3E, 0xF0, 0xB3, 0xB1, 0x3E, 0x8F, 0x69, 0x67, 0xF1, 0x0F, 0x7E, 0x3B, 0xB7, 0x84,
  28. 0xD9, 0x58, 0xE9, 0xAB, 0x89, 0x1D, 0x25, 0x3B, 0x0B, 0x4D, 0x94, 0x65, 0x8C, 0x13, 0x0B, 0x4D, 0x91, 0x5D,
  29. 0x0D, 0x39, 0x0B, 0x0D, 0x96, 0x15, 0x01, 0x05, 0x16, 0x1A, 0x26, 0xBB, 0x5B, 0x2F, 0xB3, 0xD0, 0x00, 0x59,
  30. 0x65, 0xFA, 0x96, 0x85, 0xBA, 0xCA, 0xEA, 0xBB, 0x35, 0x16, 0xEA, 0x24, 0x6B, 0x8E, 0x36, 0x58, 0x28, 0x5C,
  31. 0x26, 0x59, 0x6C, 0xB3, 0x50, 0xA0, 0x4C, 0x38, 0x27, 0x62, 0xA1, 0x10, 0x99, 0x7C, 0x4B, 0xCA, 0x42, 0x4E,
  32. 0x99, 0x6A, 0x48, 0xC1, 0x42, 0x66, 0x99, 0x76, 0x45, 0xC7, 0x42, 0x06, 0x99, 0x61, 0x42, 0xCD, 0x42, 0xAA,
  33. 0x27, 0xB6, 0xFB, 0x16, 0x16, 0x12, 0xBE, 0x32, 0x1F, 0x37, 0xB2, 0x50, 0xF3, 0xA1, 0xE7, 0xB2, 0x9D, 0x85,
  34. 0x2A, 0x6F, 0x9D, 0x67, 0x5D, 0x2C, 0x54, 0x7C, 0xEE, 0xBF, 0xE9, 0x65, 0xA1, 0xEC, 0x42, 0xC8, 0xC1, 0x00,
  35. 0x16, 0x4A, 0x47, 0xA2, 0xAE, 0xC5, 0xB0, 0x10, 0xEE, 0x04, 0x9E, 0xFA, 0x6B, 0x56, 0x3A, 0x12, 0x75, 0xED,
  36. 0x4F, 0xFF, 0xE5, 0x8B, 0xCF, 0xFD, 0x37, 0x5D, 0xAC, 0xCA, 0x5B, 0xE7, 0x59, 0x3B, 0xAB, 0xF9, 0xD0, 0x73,
  37. 0xD9, 0xC8, 0x12, 0xBE, 0x32, 0x1F, 0xB7, 0xB0, 0x54, 0x4F, 0x6C, 0xF7, 0xD5, 0xAC, 0xDE, 0x3F, 0x03, 0xA9,
  38. 0x59, 0x06, 0x13, 0xD3, 0xAE, 0x28, 0x58, 0x66, 0x13, 0x53, 0x0D, 0x49, 0x59, 0x4E, 0x13, 0x93, 0x6F, 0x89,
  39. 0x58, 0x21, 0x26, 0x26, 0x9C, 0x6B, 0xB3, 0x02, 0x4D, 0x4C, 0xB2, 0xD8, 0x60, 0x85, 0x9B, 0x58, 0x73, 0xB4,
  40. 0xC6, 0xEA, 0x64, 0x62, 0xF5, 0xDD, 0x5B, 0x56, 0x57, 0x13, 0xAB, 0x4C, 0x97, 0x59, 0x03, 0x4C, 0xEC, 0x6E,
  41. 0xBD, 0xC0, 0x1A, 0x66, 0x62, 0x45, 0x40, 0xCE, 0x1A, 0x6C, 0x62, 0x57, 0xC3, 0x89, 0x35, 0xC5, 0xC4, 0x32,
  42. 0xC6, 0xCE, 0x9A, 0x68, 0x62, 0x47, 0xC9, 0xC6, 0x9A, 0x6E, 0x62, 0x09, 0xF3, 0x63, 0x3D, 0xC4, 0xC4, 0xE8,
  43. 0xD9, 0x58, 0xCF, 0xFA, 0x2C, 0xEB, 0x17, 0xC3, 0x4F, 0x1F, 0x0A, 0xB7, 0x49, 0x8A, 0x9E, 0x00, 0x00, 0x00,
  44. 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82
  45. };
  46. private const string converterDirectoryName = "Converter";
  47. private static Dictionary<string, PlacementData.Data> myrAssetNameToData
  48. = new Dictionary<string, PlacementData.Data>(StringComparer.InvariantCultureIgnoreCase);
  49. private static readonly string[] faceKeys = {
  50. "eyeclose", "eyeclose2", "eyeclose3", "eyeclose6", "hitomih", "hitomis", "mayuha",
  51. "mayuup", "mayuv", "mayuvhalf", "moutha", "mouths", "mouthdw", "mouthup", "tangout",
  52. "tangup", "eyebig", "eyeclose5", "mayuw", "mouthhe", "mouthc", "mouthi", "mouthuphalf",
  53. "tangopen",
  54. "namida", "tear1", "tear2", "tear3", "shock", "yodare", "hoho", "hoho2", "hohos", "hohol",
  55. "toothoff", "nosefook"
  56. };
  57. private static readonly string[] mpnAttachProps = {
  58. /* "", "", "", "", "", "", "", "", "", */
  59. "kousokuu_tekaseone_i_.menu", "kousokuu_tekasetwo_i_.menu", "kousokul_ashikaseup_i_.menu",
  60. "kousokuu_tekasetwo_i_.menu", "kousokul_ashikasedown_i_.menu", "kousokuu_tekasetwodown_i_.menu",
  61. "kousokuu_ushirode_i_.menu", "kousokuu_smroom_haritsuke_i_.menu"
  62. };
  63. private static readonly int[] bodyRotations =
  64. {
  65. 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
  66. };
  67. public const int sceneVersion = 1000;
  68. public const int meidoDataVersion = 1000;
  69. public const int kankyoMagic = -765;
  70. private static BepInEx.Logging.ManualLogSource Log;
  71. private static readonly int faceToggleIndex = Array.IndexOf(faceKeys, "tangopen") + 1;
  72. private static string configPath = Path.Combine(Paths.ConfigPath, converterDirectoryName);
  73. private bool active = false;
  74. private Rect windowRect = new Rect(30f, 30f, 300f, 200f);
  75. private void Awake()
  76. {
  77. DontDestroyOnLoad(this);
  78. if (!Directory.Exists(configPath)) Directory.CreateDirectory(configPath);
  79. Log = Logger;
  80. }
  81. private void Start()
  82. {
  83. UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;
  84. List<PlacementData.Data> dataList = PlacementData.GetAllDatas(false);
  85. foreach (var data in dataList)
  86. {
  87. string assetName = string.IsNullOrEmpty(data.assetName) ? data.resourceName : data.assetName;
  88. myrAssetNameToData[assetName] = data;
  89. }
  90. }
  91. private void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
  92. {
  93. int index = scene.buildIndex;
  94. active = index == 9 || index == 3;
  95. }
  96. private void OnGUI()
  97. {
  98. if (active)
  99. {
  100. windowRect.width = 300f;
  101. windowRect.height = 200f;
  102. windowRect.x = UnityEngine.Mathf.Clamp(windowRect.x, 0, Screen.width - windowRect.width);
  103. windowRect.y = UnityEngine.Mathf.Clamp(windowRect.y, 0, Screen.height - windowRect.height);
  104. windowRect = GUI.Window(0xEA4040, windowRect, GUIFunc, "MeidoPhotoStudio Converter");
  105. }
  106. }
  107. private void GUIFunc(int id)
  108. {
  109. if (GUILayout.Button("Convert ModifiedMM")) ProcessModifedMM();
  110. if (GUILayout.Button("Convert ModifiedMM (Scene Manager)")) ProcessModifiedMMPng();
  111. GUILayout.Space(30f);
  112. if (GUILayout.Button("Convert modifedMM quickSave")) ProcessQuickSave();
  113. // GUILayout.Button("Convert MultipleMaids");
  114. }
  115. private void ProcessModifiedMMPng()
  116. {
  117. string modPath = Path.Combine(Paths.GameRootPath, "Mod");
  118. string scenePath = Path.Combine(modPath, "MultipleMaidsScene");
  119. string kankyoPath = Path.Combine(modPath, "MultipleMaidsScene");
  120. }
  121. private string GetModifiedMMSceneData(string pngPath)
  122. {
  123. return String.Empty;
  124. }
  125. private void ProcessQuickSave()
  126. {
  127. string sybarisPath = Path.Combine(Paths.GameRootPath, "Sybaris");
  128. string iniPath = BepInEx.Utility.CombinePaths(sybarisPath, "UnityInjector", "Config", "MultipleMaids.ini");
  129. IniFile mmIniFile = IniFile.FromFile(iniPath);
  130. IniSection sceneSection = mmIniFile.GetSection("scene");
  131. if (sceneSection != null)
  132. {
  133. if (sceneSection.HasKey("s9999"))
  134. {
  135. string sceneData = sceneSection["s9999"].Value;
  136. if (!string.IsNullOrEmpty(sceneData))
  137. {
  138. byte[] convertedSceneData = ProcessScene(sceneData, false);
  139. string path = Path.Combine(configPath, $"mmtempscene{GetMMDateString(sceneData)}.png");
  140. SaveSceneToFile(path, convertedSceneData, noThumb);
  141. }
  142. }
  143. }
  144. }
  145. private void ProcessModifedMM()
  146. {
  147. string sybarisPath = Path.Combine(Paths.GameRootPath, "Sybaris");
  148. string iniPath = BepInEx.Utility.CombinePaths(sybarisPath, "UnityInjector", "Config", "MultipleMaids.ini");
  149. IniFile mmIniFile = IniFile.FromFile(iniPath);
  150. IniSection sceneSection = mmIniFile.GetSection("scene");
  151. if (sceneSection != null)
  152. {
  153. foreach (IniKey key in sceneSection.Keys)
  154. {
  155. if (key.Key.StartsWith("ss")) continue;
  156. bool kankyo = int.Parse(key.Key.Substring(1)) >= 10000;
  157. string sceneData = key.Value;
  158. if (!string.IsNullOrEmpty(sceneData))
  159. {
  160. byte[] convertedSceneData = ProcessScene(sceneData, kankyo);
  161. string prefix = kankyo ? "mmkankyo" : "mmscene";
  162. string path = Path.Combine(configPath, $"{prefix}_{key.Key}{GetMMDateString(sceneData)}.png");
  163. byte[] thumbnail = noThumb;
  164. string screenshotKey = $"s{key.Key}";
  165. if (sceneSection.HasKey(screenshotKey))
  166. {
  167. string screenshotBase64 = sceneSection[screenshotKey].Value;
  168. if (!string.IsNullOrEmpty(screenshotBase64))
  169. {
  170. thumbnail = Convert.FromBase64String(screenshotBase64);
  171. }
  172. }
  173. SaveSceneToFile(path, convertedSceneData, thumbnail);
  174. }
  175. }
  176. }
  177. }
  178. public static void SaveSceneToFile(string path, byte[] sceneData, byte[] thumbnailData)
  179. {
  180. using (FileStream fileStream = File.Create(path))
  181. {
  182. fileStream.Write(thumbnailData, 0, thumbnailData.Length);
  183. fileStream.Write(sceneData, 0, sceneData.Length);
  184. }
  185. }
  186. public static string GetMMDateString(string sceneData)
  187. {
  188. string dateString = sceneData.Split('_')[0].Split(',')[0];
  189. DateTime date = DateTime.Parse(dateString);
  190. return $"{date:yyyyMMddHHmm}";
  191. }
  192. public static byte[] ProcessScene(string sceneData, bool kankyo)
  193. {
  194. string[] strArray1 = sceneData.Split('_');
  195. string[] strArray2 = strArray1[1].Split(';');
  196. string[] strArray3 = strArray1[0].Split(',');
  197. string[] strArray4 = null;
  198. string[] strArray5 = null;
  199. string[] strArray6 = null;
  200. string[] strArray7 = null;
  201. if (strArray1.Length >= 5)
  202. {
  203. strArray4 = strArray1[2].Split(',');
  204. strArray5 = strArray1[3].Split(';');
  205. strArray6 = strArray1[4].Split(';');
  206. }
  207. if (strArray1.Length >= 6)
  208. {
  209. strArray7 = strArray1[5].Split(';');
  210. }
  211. using (MemoryStream memoryStream = new MemoryStream())
  212. using (DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress))
  213. using (BinaryWriter binaryWriter = new BinaryWriter(deflateStream, System.Text.Encoding.UTF8))
  214. {
  215. binaryWriter.Write("MPS_SCENE");
  216. binaryWriter.Write(sceneVersion);
  217. binaryWriter.Write(kankyo ? kankyoMagic : int.Parse(strArray3[1]));
  218. SerializeEnvironment(strArray3, binaryWriter, kankyo);
  219. SerializeLights(strArray3, strArray4, strArray5, strArray7, binaryWriter);
  220. SerializeEffect(strArray4, binaryWriter);
  221. SerializeProp(strArray3, strArray6, binaryWriter);
  222. if (!kankyo)
  223. {
  224. SerializeMessage(strArray3, binaryWriter);
  225. SerializeMaid(strArray2, binaryWriter);
  226. }
  227. binaryWriter.Write("END");
  228. deflateStream.Close();
  229. return memoryStream.ToArray();
  230. }
  231. }
  232. private static void SerializeMaid(string[] strArray2, BinaryWriter binaryWriter)
  233. {
  234. binaryWriter.Write("MEIDO");
  235. // MM scene converted to MPS
  236. binaryWriter.Write(true);
  237. binaryWriter.Write(meidoDataVersion);
  238. int numberOfMaids = strArray2.Length;
  239. binaryWriter.Write(numberOfMaids);
  240. /*
  241. TODO: Investigate why serialized maid data may only have 64 items.
  242. https://git.coder.horse/meidomustard/modifiedMM/src/master/MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.Update.cs#L3745
  243. The difference affects whether or not rotations are local or world.
  244. Certain body rotations would be missing as well particularly the toes.
  245. Other data like free look and attached items like hand/vag/anl would be missing.
  246. */
  247. for (int i = 0; i < numberOfMaids; i++)
  248. {
  249. using (MemoryStream memoryStream = new MemoryStream())
  250. using (BinaryWriter tempWriter = new BinaryWriter(memoryStream))
  251. {
  252. string[] maidData = strArray2[i].Split(':');
  253. tempWriter.WriteVector3(Utility.Vector3String(maidData[59])); // position
  254. tempWriter.WriteQuaternion(Utility.EulerString(maidData[58])); // rotation
  255. tempWriter.WriteVector3(Utility.Vector3String(maidData[60])); // scale
  256. // fingers
  257. for (int j = 0; j < 40; j++)
  258. {
  259. tempWriter.WriteQuaternion(Utility.EulerString(maidData[j]));
  260. }
  261. // toes
  262. for (int k = 0; k < 2; k++)
  263. {
  264. for (int j = 72 + k; j < 90; j += 2)
  265. {
  266. tempWriter.WriteQuaternion(Utility.EulerString(maidData[j]));
  267. }
  268. }
  269. // the rest of the limbs
  270. foreach (int j in bodyRotations)
  271. {
  272. tempWriter.WriteQuaternion(Utility.EulerString(maidData[j]));
  273. }
  274. tempWriter.WriteVector3(Utility.Vector3String(maidData[96])); // hip position
  275. // cached pose stuff
  276. tempWriter.Write("normal");
  277. tempWriter.Write("pose_taiki_f");
  278. tempWriter.Write(false);
  279. // eye rotation delta
  280. // MM saves the rotations directly so just save the identity
  281. tempWriter.WriteQuaternion(Quaternion.identity);
  282. tempWriter.WriteQuaternion(Quaternion.identity);
  283. string[] freeLookData = maidData[64].Split(',');
  284. bool isFreeLook = int.Parse(freeLookData[0]) == 1;
  285. tempWriter.Write(isFreeLook);
  286. if (isFreeLook)
  287. {
  288. tempWriter.WriteVector3(new Vector3(
  289. float.Parse(freeLookData[2]), 1f, float.Parse(freeLookData[1])
  290. ));
  291. }
  292. string[] faceValues = maidData[63].Split(',');
  293. tempWriter.Write("MPS_FACE");
  294. for (int j = 0; j < faceKeys.Length - 2; j++)
  295. {
  296. tempWriter.Write(faceKeys[j]);
  297. if (j >= faceToggleIndex) tempWriter.Write(float.Parse(faceValues[j]) > 0f);
  298. else tempWriter.Write(float.Parse(faceValues[j]));
  299. }
  300. if (faceValues.Length > 65)
  301. {
  302. tempWriter.Write(faceKeys[faceKeys.Length - 1]);
  303. tempWriter.Write(float.Parse(faceValues[faceValues.Length - 1]) > 0f);
  304. }
  305. tempWriter.Write("END_FACE");
  306. tempWriter.Write(true); // body visible
  307. // MM does not serialize clothing
  308. for (int j = 0; j < 29; j++) tempWriter.Write(true);
  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 += 1;
  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. }
  351. private static void SerializeProp(string[] strArray3, string[] strArray6, BinaryWriter binaryWriter)
  352. {
  353. binaryWriter.Write("PROP");
  354. bool hasWProp = strArray3.Length > 37 && !string.IsNullOrEmpty(strArray3[37]);
  355. int numberOfProps = hasWProp ? 1 : 0;
  356. numberOfProps += strArray6 == null ? 0 : strArray6.Length - 1;
  357. binaryWriter.Write(numberOfProps);
  358. if (hasWProp)
  359. {
  360. // For the prop that spawns when you push (shift +) W
  361. binaryWriter.Write(strArray3[37].Replace(' ', '_'));
  362. SerializeAttachPoint(binaryWriter);
  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. }
  373. if (strArray6 != null)
  374. {
  375. for (int i = 0; i < strArray6.Length - 1; i++)
  376. {
  377. string[] assetParts = strArray6[i].Split(',');
  378. string assetName = assetParts[0].Replace(' ', '_');
  379. if (assetName.StartsWith("creative_"))
  380. {
  381. // modifiedMM my room creative prop
  382. // modifiedMM serializes the prefabName rather than the ID.
  383. assetName = assetName.Replace("creative_", String.Empty);
  384. assetName = $"MYR_{myrAssetNameToData[assetName].ID}#{assetName}";
  385. }
  386. else if (assetName.StartsWith("MYR_"))
  387. {
  388. // MM 23.0+ my room creative prop
  389. int assetID = int.Parse(assetName.Replace("MYR_", string.Empty));
  390. PlacementData.Data data = PlacementData.GetData(assetID);
  391. string asset = string.IsNullOrEmpty(data.assetName) ? data.resourceName : data.assetName;
  392. assetName = $"{assetName}#{asset}";
  393. }
  394. else if (assetName.Contains('#'))
  395. {
  396. if (assetName.Contains(".menu"))
  397. {
  398. // modifiedMM official mod prop
  399. string[] modComponents = assetParts[0].Split('#');
  400. string baseMenuFile = modComponents[0].Replace(' ', '_');
  401. string modItem = modComponents[1].Replace(' ', '_');
  402. assetName = $"{modItem}#{baseMenuFile}";
  403. }
  404. else
  405. {
  406. assetName = assetName.Split('#')[1].Replace(' ', '_');
  407. }
  408. }
  409. else if (assetName.StartsWith("BGOdogu", StringComparison.InvariantCultureIgnoreCase))
  410. {
  411. // I don't know why multiplemaids even prepends BG
  412. assetName = assetName.Substring(2);
  413. }
  414. binaryWriter.Write(assetName);
  415. SerializeAttachPoint(binaryWriter);
  416. binaryWriter.WriteVector3(new Vector3(
  417. float.Parse(assetParts[4]), float.Parse(assetParts[5]), float.Parse(assetParts[6])
  418. ));
  419. binaryWriter.WriteQuaternion(Quaternion.Euler(
  420. float.Parse(assetParts[1]), float.Parse(assetParts[2]), float.Parse(assetParts[3])
  421. ));
  422. binaryWriter.WriteVector3(new Vector3(
  423. float.Parse(assetParts[7]), float.Parse(assetParts[8]), float.Parse(assetParts[9])
  424. ));
  425. }
  426. }
  427. }
  428. private static void SerializeEffect(string[] strArray4, BinaryWriter binaryWriter)
  429. {
  430. binaryWriter.Write("EFFECT");
  431. if (strArray4 != null)
  432. {
  433. // bloom
  434. binaryWriter.Write("EFFECT_BLOOM");
  435. binaryWriter.Write(float.Parse(strArray4[2])); // intensity
  436. binaryWriter.Write((int)float.Parse(strArray4[3])); // blur iterations
  437. binaryWriter.WriteColour(new Color( // bloom threshold colour
  438. float.Parse(strArray4[4]), float.Parse(strArray4[5]), float.Parse(strArray4[6]), 1f
  439. ));
  440. binaryWriter.Write(int.Parse(strArray4[7]) == 1); // hdr
  441. binaryWriter.Write(int.Parse(strArray4[1]) == 1); // active
  442. // vignetting
  443. binaryWriter.Write("EFFECT_VIGNETTE");
  444. binaryWriter.Write(float.Parse(strArray4[9])); // intensity
  445. binaryWriter.Write(float.Parse(strArray4[10])); // blur
  446. binaryWriter.Write(float.Parse(strArray4[11])); // blur spread
  447. binaryWriter.Write(float.Parse(strArray4[12])); // chromatic aberration
  448. binaryWriter.Write(int.Parse(strArray4[8]) == 1); // active
  449. // bokashi
  450. // TODO: implement bokashi in MPS
  451. float bokashi = float.Parse(strArray4[13]);
  452. // TODO: implement sepia in MPS too
  453. if (strArray4.Length > 15)
  454. {
  455. binaryWriter.Write("EFFECT_DOF");
  456. binaryWriter.Write(float.Parse(strArray4[16])); // focal length
  457. binaryWriter.Write(float.Parse(strArray4[17])); // focal size
  458. binaryWriter.Write(float.Parse(strArray4[18])); // aperture
  459. binaryWriter.Write(float.Parse(strArray4[19])); // max blur size
  460. binaryWriter.Write(int.Parse(strArray4[20]) == 1); // visualize focus
  461. binaryWriter.Write(int.Parse(strArray4[15]) == 1); // active
  462. binaryWriter.Write("EFFECT_FOG");
  463. binaryWriter.Write(float.Parse(strArray4[22])); // fog distance
  464. binaryWriter.Write(float.Parse(strArray4[23])); // density
  465. binaryWriter.Write(float.Parse(strArray4[24])); // height scale
  466. binaryWriter.Write(float.Parse(strArray4[25])); // height
  467. binaryWriter.WriteColour(new Color( // fog colour
  468. float.Parse(strArray4[26]), float.Parse(strArray4[27]), float.Parse(strArray4[28]), 1f
  469. ));
  470. binaryWriter.Write(int.Parse(strArray4[21]) == 1); // active
  471. }
  472. }
  473. binaryWriter.Write("END_EFFECT");
  474. }
  475. private static void SerializeMessage(string[] strArray3, BinaryWriter binaryWriter)
  476. {
  477. binaryWriter.Write("TEXTBOX");
  478. bool showingMessage = false;
  479. string name = "Maid";
  480. string message = "Hello world";
  481. if (strArray3.Length > 16)
  482. {
  483. showingMessage = int.Parse(strArray3[34]) == 1;
  484. name = strArray3[35];
  485. message = strArray3[36].Replace("&kaigyo", "\n");
  486. // MM does not serialize message font size
  487. }
  488. binaryWriter.Write(showingMessage);
  489. binaryWriter.Write(25);
  490. binaryWriter.WriteNullableString(name);
  491. binaryWriter.WriteNullableString(message);
  492. }
  493. private static void SerializeLights(string[] strArray3, string[] strArray4, string[] strArray5, string[] strArray7, BinaryWriter binaryWriter)
  494. {
  495. // Lights
  496. binaryWriter.Write("LIGHT");
  497. int numberOfLights = 1;
  498. numberOfLights += strArray5 == null ? 0 : strArray5.Length - 1;
  499. binaryWriter.Write(numberOfLights);
  500. if (strArray3.Length > 16)
  501. {
  502. // Main Light
  503. /*
  504. 0 = Directional
  505. 1 = Spot
  506. 2 = Point
  507. 3 = Directional (Colour Mode)
  508. */
  509. int lightType = int.Parse(strArray3[17]);
  510. Color lightColour = new Color(
  511. float.Parse(strArray3[18]), float.Parse(strArray3[19]), float.Parse(strArray3[20]), 1f
  512. );
  513. Quaternion lightRotation = Quaternion.Euler(
  514. float.Parse(strArray3[21]), float.Parse(strArray3[22]), float.Parse(strArray3[23])
  515. );
  516. // MM uses spotAngle for both range and spotAngle based on which light type is used
  517. float intensity = float.Parse(strArray3[24]);
  518. float spotAngle = float.Parse(strArray3[25]);
  519. float range = lightType == 2 ? spotAngle / 5f : spotAngle; ;
  520. float shadowStrength = 0.098f;
  521. if (strArray4 != null) shadowStrength = float.Parse(strArray4[0]);
  522. for (int i = 0; i < 3; i++)
  523. {
  524. if (i == lightType || (i == 0 && lightType == 3))
  525. {
  526. SerializeLightProperty(
  527. binaryWriter, lightRotation, lightColour, intensity, spotAngle, range, shadowStrength
  528. );
  529. }
  530. else SerializeDefaultLight(binaryWriter);
  531. }
  532. if (strArray7 != null)
  533. {
  534. binaryWriter.WriteVector3(Utility.Vector3String(strArray7[0]));
  535. }
  536. else binaryWriter.WriteVector3(new Vector3(0f, 1.9f, 0.4f));
  537. binaryWriter.Write(lightType == 3 ? 0 : lightType);
  538. binaryWriter.Write(lightType == 3);
  539. binaryWriter.Write(false);
  540. // lightKage[0] is the only value that's serialized
  541. }
  542. if (strArray5 != null)
  543. {
  544. int otherLights = strArray5.Length - 1;
  545. for (int i = 0; i < otherLights; i++)
  546. {
  547. string[] lightProperties = strArray5[i].Split(',');
  548. int lightType = int.Parse(lightProperties[0]);
  549. Color lightColour = new Color(
  550. float.Parse(lightProperties[1]), float.Parse(lightProperties[2]),
  551. float.Parse(lightProperties[3]), 1f
  552. );
  553. Quaternion lightRotation = Quaternion.Euler(
  554. float.Parse(lightProperties[4]), float.Parse(lightProperties[5]), 18f
  555. );
  556. float intensity = float.Parse(lightProperties[6]);
  557. float spotAngle = float.Parse(lightProperties[7]);
  558. float range = lightType == 2 ? spotAngle / 5f : spotAngle;
  559. float shadowStrength = 0.098f;
  560. for (int j = 0; j < 3; j++)
  561. {
  562. if (j == lightType)
  563. {
  564. SerializeLightProperty(
  565. binaryWriter, lightRotation, lightColour, intensity, spotAngle, range, shadowStrength
  566. );
  567. }
  568. else SerializeDefaultLight(binaryWriter);
  569. }
  570. if (strArray7 != null)
  571. {
  572. binaryWriter.WriteVector3(Utility.Vector3String(strArray7[i + 1]));
  573. }
  574. else binaryWriter.WriteVector3(new Vector3(0f, 1.9f, 0.4f));
  575. binaryWriter.Write(lightType == 3 ? 0 : lightType);
  576. binaryWriter.Write(false);
  577. binaryWriter.Write(lightType == 3);
  578. }
  579. }
  580. }
  581. private static void SerializeEnvironment(string[] data, BinaryWriter binaryWriter, bool kankyo)
  582. {
  583. binaryWriter.Write("ENVIRONMENT");
  584. int bgIndex;
  585. string bgAsset = "Theater";
  586. if (!int.TryParse(data[2], out bgIndex))
  587. {
  588. bgAsset = data[2].Replace(" ", "_");
  589. }
  590. binaryWriter.Write(bgAsset);
  591. binaryWriter.WriteVector3(new Vector3(
  592. float.Parse(data[6]), float.Parse(data[7]), float.Parse(data[8])
  593. ));
  594. binaryWriter.WriteQuaternion(Quaternion.Euler(
  595. float.Parse(data[3]), float.Parse(data[4]), float.Parse(data[5])
  596. ));
  597. binaryWriter.WriteVector3(new Vector3(
  598. float.Parse(data[9]), float.Parse(data[10]), float.Parse(data[11])
  599. ));
  600. binaryWriter.Write(kankyo);
  601. Vector3 cameraTargetPos = new Vector3(0f, 0.9f, 0f);
  602. float cameraDistance = 3f;
  603. Quaternion cameraRotation = Quaternion.identity;
  604. if (data.Length > 16)
  605. {
  606. cameraTargetPos = new Vector3(
  607. float.Parse(data[27]), float.Parse(data[28]), float.Parse(data[29])
  608. );
  609. cameraDistance = float.Parse(data[30]);
  610. cameraRotation = Quaternion.Euler(
  611. float.Parse(data[31]), float.Parse(data[32]), float.Parse(data[33])
  612. );
  613. }
  614. binaryWriter.WriteVector3(cameraTargetPos);
  615. binaryWriter.Write(cameraDistance);
  616. binaryWriter.WriteQuaternion(cameraRotation);
  617. }
  618. public static void SerializeAttachPoint(BinaryWriter binaryWriter)
  619. {
  620. binaryWriter.Write(0);
  621. binaryWriter.Write(-1);
  622. }
  623. public static void SerializeDefaultLight(BinaryWriter binaryWriter)
  624. {
  625. SerializeLightProperty(binaryWriter, Quaternion.Euler(40f, 180f, 0f), Color.white);
  626. }
  627. public static void SerializeLightProperty(
  628. BinaryWriter binaryWriter,
  629. Quaternion rotation, Color colour, float intensity = 0.95f, float range = 50f,
  630. float spotAngle = 50f, float shadowStrength = 0.1f
  631. )
  632. {
  633. binaryWriter.WriteQuaternion(rotation);
  634. binaryWriter.Write(intensity);
  635. binaryWriter.Write(range);
  636. binaryWriter.Write(spotAngle);
  637. binaryWriter.Write(shadowStrength);
  638. binaryWriter.WriteColour(colour);
  639. }
  640. }
  641. public static class BinaryExtensions
  642. {
  643. public static void WriteVector3(this BinaryWriter binaryWriter, Vector3 vector3)
  644. {
  645. binaryWriter.Write(vector3.x);
  646. binaryWriter.Write(vector3.y);
  647. binaryWriter.Write(vector3.z);
  648. }
  649. public static void WriteQuaternion(this BinaryWriter binaryWriter, Quaternion quaternion)
  650. {
  651. binaryWriter.Write(quaternion.x);
  652. binaryWriter.Write(quaternion.y);
  653. binaryWriter.Write(quaternion.z);
  654. binaryWriter.Write(quaternion.w);
  655. }
  656. public static void WriteColour(this BinaryWriter binaryWriter, UnityEngine.Color colour)
  657. {
  658. binaryWriter.Write(colour.r);
  659. binaryWriter.Write(colour.g);
  660. binaryWriter.Write(colour.b);
  661. binaryWriter.Write(colour.a);
  662. }
  663. public static void WriteNullableString(this BinaryWriter binaryWriter, string str)
  664. {
  665. binaryWriter.Write(str != null);
  666. if (str != null) binaryWriter.Write(str);
  667. }
  668. }
  669. public static class Utility
  670. {
  671. public static Quaternion EulerString(string euler)
  672. {
  673. string[] data = euler.Split(',');
  674. return Quaternion.Euler(
  675. float.Parse(data[0]), float.Parse(data[1]), float.Parse(data[2])
  676. );
  677. }
  678. public static Vector3 Vector3String(string vector3)
  679. {
  680. string[] data = vector3.Split(',');
  681. return new Vector3(
  682. float.Parse(data[0]), float.Parse(data[1]), float.Parse(data[2])
  683. );
  684. }
  685. }
  686. }