Browse Source

Add ini to png conversion on first boot

habeebweeb 4 years ago
parent
commit
974d85c0e0
1 changed files with 67 additions and 2 deletions
  1. 67 2
      MultipleMaids/SaveManager.cs

+ 67 - 2
MultipleMaids/SaveManager.cs

@@ -1,6 +1,6 @@
 using UnityEngine;
+using ExIni;
 using System;
-using System.Linq;
 using System.Collections.Generic;
 using System.Text;
 using System.IO;
@@ -11,7 +11,6 @@ namespace CM3D2.MultipleMaids.Plugin
     public partial class MultipleMaids
     {
         internal static readonly byte[] pngEnd = Encoding.ASCII.GetBytes("IEND");
-        internal static readonly int[] border = { -1, 0, 0, 0, 0 };
         internal static readonly byte[] kankyoHeader = Encoding.ASCII.GetBytes("KANKYO");
         internal const string sceneDirectoryName = "< Scene Root >";
         internal const string kankyoDirectoryName = "< Kankyo Root >";
@@ -51,6 +50,7 @@ namespace CM3D2.MultipleMaids.Plugin
             frame = MakeTex(2, 2, Color.white);
             infoHighlight = MakeTex(2, 2, new Color(0f, 0f, 0f, 0.8f));
 
+
             selectedButtonStyle = new GUIStyle("button");
             selectedButtonStyle.normal.background = MakeTex(1, 1, new Color(0.5f, 0.5f, 0.5f, 0.4f));
             selectedButtonStyle.normal.textColor = Color.white;
@@ -58,6 +58,14 @@ namespace CM3D2.MultipleMaids.Plugin
             currentDirectory = kankyoModeFlag ? kankyoDirectoryName : sceneDirectoryName;
 
             GetSceneDirectories();
+
+            if (Preferences["scenemanager"]["converted"].Value != "true")
+            {
+                ConvertIni();
+                Preferences["scenemanager"]["converted"].Value = "true";
+                SaveConfig();
+            }
+
             GetScenes(currentDirectory);
             sceneManagerInitialize = true;
         }
@@ -355,6 +363,63 @@ namespace CM3D2.MultipleMaids.Plugin
             return true;
         }
 
+        private void ConvertIni()
+        {
+            HashSet<int> saveSceneEntries = new HashSet<int>();
+
+            IniSection MMScene = Preferences["scene"];
+
+            foreach (IniKey iniKey in MMScene.Keys)
+            {
+                string keyName = iniKey.Key;
+
+                if (keyName[0] == 's')
+                {
+                    int index = Int32.Parse(keyName.Substring(1 + (keyName[1] == 's' ? 1 : 0)));
+                    if (!saveSceneEntries.Contains(index))
+                    {
+                        saveSceneEntries.Add(index);
+                        ConvertSceneToPng(index, MMScene);
+                    }
+                }
+            }
+        }
+
+        private void ConvertSceneToPng(int index, IniSection MMScene)
+        {
+            byte[] sceneBuffer;
+            byte[] screenshotBuffer = frame.EncodeToPNG();
+            string sceneString = MMScene.GetKey($"s{index}")?.RawValue;
+            string screenshotString = MMScene.GetKey($"ss{index}")?.RawValue;
+            bool kankyo = index >= 10000;
+
+            if (index == 9999 || String.IsNullOrEmpty(sceneString)) return;
+
+            using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(sceneString)))
+            {
+                sceneBuffer = LZMA.Compress(stream);
+            }
+
+            DateTime dateSaved = DateTime.Parse(sceneString.Split(',')[0]);
+
+            if (!String.IsNullOrEmpty(screenshotString))
+                screenshotBuffer = Convert.FromBase64String(screenshotString);
+
+            string sceneType = kankyo ? "mmkankyo" : "mmscene";
+            string savePngFilename = $"{sceneType}{index}_{dateSaved.ToString("yyyyMMddHHmm")}.png";
+            string outPath = Path.Combine(kankyo ? kankyoScenePath : saveScenePath, savePngFilename);
+
+            using (BinaryWriter stream = new BinaryWriter(File.Create(outPath)))
+            {
+                stream.Write(screenshotBuffer);
+                if (kankyo) stream.Write(kankyoHeader);
+                stream.Write(sceneBuffer);
+            }
+
+            File.SetCreationTime(outPath, dateSaved);
+            File.SetLastWriteTime(outPath, dateSaved);
+        }
+
         private class ScenePng
         {
             public FileInfo info { get; }