Browse Source

Stop BG2 initialization coroutine when exiting MM

habeebweeb 3 years ago
parent
commit
17d34e76ae

+ 108 - 108
MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.Gui.cs

@@ -683,131 +683,131 @@ namespace CM3D2.MultipleMaids.Plugin
                     GUI.Label(new Rect(GetPix(15), GetPix(130), GetPix(90), GetPix(25)), "Initializing...");
                 }
 
-            int iconSize = GetPix(45);
-            Rect position1;
-            Rect viewRect;
-            int bg2ItemCount = slotIndex == 1 ?
-                creativeSortList.Count :
-                (!modItemsOnly && modItemsToggle) ?
-                    numberOfModItems : sortList.Count;
-            if (sceneLevel != 5)
-            {
-                position1 = new Rect(GetPix(7),
-                                      GetPix(138),
-                                      GetPix(44) * 4 + GetPix(20),
-                                      rectWin.height - GetPix(148));
-                viewRect = new Rect(0.0f,
-                                    0.0f,
-                                    position1.width * 0.845f,
-                                    Mathf.Ceil(bg2ItemCount / 4f) * iconSize + GetPix(10));
-            }
-            else
-            {
-                position1 = new Rect(GetPix(7),
-                                      GetPix(138),
-                                      GetPix(44) * 4 + GetPix(20),
-                                      rectWin.height - GetPix(148));
-                viewRect = new Rect(0.0f,
-                                    0.0f,
-                                    position1.width * 0.845f,
-                                    Mathf.Ceil(bg2ItemCount / 4f) * iconSize + GetPix(10));
-            }
-
-            bg2ScrollPos = GUI.BeginScrollView(position1, bg2ScrollPos, viewRect);
-            int bg2Index = 0;
-            List<SortItem> bg2List = slotIndex == 1 ? creativeSortList : sortList;
-            foreach (SortItem sortItem in bg2List)
-            {
-                if (slotIndex > 1 && modItemsToggle && !sortItem.isMod)
+                int iconSize = GetPix(45);
+                Rect position1;
+                Rect viewRect;
+                int bg2ItemCount = slotIndex == 1 ?
+                    creativeSortList.Count :
+                    (!modItemsOnly && modItemsToggle) ?
+                        numberOfModItems : sortList.Count;
+                if (sceneLevel != 5)
                 {
-                    continue;
+                    position1 = new Rect(GetPix(7),
+                                          GetPix(138),
+                                          GetPix(44) * 4 + GetPix(20),
+                                          rectWin.height - GetPix(148));
+                    viewRect = new Rect(0.0f,
+                                        0.0f,
+                                        position1.width * 0.845f,
+                                        Mathf.Ceil(bg2ItemCount / 4f) * iconSize + GetPix(10));
                 }
-                Rect position2 = new Rect(
-                    bg2Index % 4 * iconSize,
-                    bg2Index / 4 * iconSize,
-                    iconSize,
-                    iconSize
-                );
-                if (GUI.Button(position2, ""))
+                else
+                {
+                    position1 = new Rect(GetPix(7),
+                                          GetPix(138),
+                                          GetPix(44) * 4 + GetPix(20),
+                                          rectWin.height - GetPix(148));
+                    viewRect = new Rect(0.0f,
+                                        0.0f,
+                                        position1.width * 0.845f,
+                                        Mathf.Ceil(bg2ItemCount / 4f) * iconSize + GetPix(10));
+                }
+
+                bg2ScrollPos = GUI.BeginScrollView(position1, bg2ScrollPos, viewRect);
+                int bg2Index = 0;
+                List<SortItem> bg2List = slotIndex == 1 ? creativeSortList : sortList;
+                foreach (SortItem sortItem in bg2List)
                 {
-                    GameObject gameObject = null;
-                    if (slotIndex > 1)
+                    if (slotIndex > 1 && modItemsToggle && !sortItem.isMod)
                     {
-                        string menu = sortItem.menu;
-                        byte[] modBuf = null;
-                        bool ready = true;
-                        if (sortItem.isOfficialMod)
+                        continue;
+                    }
+                    Rect position2 = new Rect(
+                        bg2Index % 4 * iconSize,
+                        bg2Index / 4 * iconSize,
+                        iconSize,
+                        iconSize
+                    );
+                    if (GUI.Button(position2, ""))
+                    {
+                        GameObject gameObject = null;
+                        if (slotIndex > 1)
                         {
-                            menu = sortItem.baseMenu;
+                            string menu = sortItem.menu;
+                            byte[] modBuf = null;
+                            bool ready = true;
+                            if (sortItem.isOfficialMod)
+                            {
+                                menu = sortItem.baseMenu;
 
-                            using (FileStream fileStream = new FileStream(sortItem.menu, FileMode.Open))
+                                using (FileStream fileStream = new FileStream(sortItem.menu, FileMode.Open))
+                                {
+                                    if (fileStream == null)
+                                    {
+                                        ready = false;
+                                    }
+                                    else
+                                    {
+                                        modBuf = new byte[fileStream.Length];
+                                        fileStream.Read(modBuf, 0, (int)fileStream.Length);
+                                    }
+                                }
+                            }
+                            byte[] menuBuf = null;
+                            using (AFileBase afileBase = GameUty.FileOpen(menu, null))
                             {
-                                if (fileStream == null)
+                                if (ready = afileBase.IsValid())
                                 {
-                                    ready = false;
+                                    menuBuf = afileBase.ReadAll();
                                 }
-                                else
+                            }
+
+                            if (ready)
+                            {
+                                string[] meshInfo = ProcScriptBin(menuBuf);
+                                gameObject = ImportCM2.LoadSkinMesh_R(meshInfo[0], meshInfo, 1);
+                                gameObject.name = menu;
+                                if (sortItem.isOfficialMod)
                                 {
-                                    modBuf = new byte[fileStream.Length];
-                                    fileStream.Read(modBuf, 0, (int)fileStream.Length);
+                                    gameObject.name += $"#{Path.GetFileName(sortItem.menu)}";
+                                    ProcModScriptBin(modBuf, gameObject);
                                 }
                             }
                         }
-                        byte[] menuBuf = null;
-                        using (AFileBase afileBase = GameUty.FileOpen(menu, null))
+                        else
                         {
-                            if (ready = afileBase.IsValid())
-                            {
-                                menuBuf = afileBase.ReadAll();
-                            }
+                            // My Room Custom content
+                            PlacementData.Data data = PlacementData.GetData(int.Parse(sortItem.menu));
+                            gameObject = Instantiate(data.GetPrefab());
+                            gameObject.name = $"creative_{sortItem.baseMenu}";
                         }
 
-                        if (ready)
+                        if (gameObject != null)
                         {
-                            string[] meshInfo = ProcScriptBin(menuBuf);
-                            gameObject = ImportCM2.LoadSkinMesh_R(meshInfo[0], meshInfo, 1);
-                            gameObject.name = menu;
-                            if (sortItem.isOfficialMod)
-                            {
-                                gameObject.name += $"#{Path.GetFileName(sortItem.menu)}";
-                                ProcModScriptBin(modBuf, gameObject);
-                            }
+                            doguBObject.Add(gameObject);
+                            gameObject.transform.position = new Vector3(0, 0, 0.4f);
+                            doguCnt = doguBObject.Count - 1;
+                            gDogu[doguCnt] = GameObject.CreatePrimitive(PrimitiveType.Cube);
+                            gDogu[doguCnt].GetComponent<Renderer>().material = m_material;
+                            gDogu[doguCnt].layer = 8;
+                            gDogu[doguCnt].GetComponent<Renderer>().enabled = false;
+                            gDogu[doguCnt].SetActive(false);
+                            gDogu[doguCnt].transform.position = gameObject.transform.position;
+                            mDogu[doguCnt] = gDogu[doguCnt].AddComponent<MouseDrag6>();
+                            mDogu[doguCnt].isScale = false;
+                            mDogu[doguCnt].obj = gDogu[doguCnt];
+                            mDogu[doguCnt].maid = gameObject;
+                            mDogu[doguCnt].angles = gameObject.transform.eulerAngles;
+                            gDogu[doguCnt].transform.localScale = new Vector3(cubeSize, cubeSize, cubeSize);
+                            mDogu[doguCnt].ido = 1;
                         }
                     }
-                    else
-                    {
-                        // My Room Custom content
-                        PlacementData.Data data = PlacementData.GetData(int.Parse(sortItem.menu));
-                        gameObject = Instantiate(data.GetPrefab());
-                        gameObject.name = $"creative_{sortItem.baseMenu}";
-                    }
 
-                    if (gameObject != null)
-                    {
-                        doguBObject.Add(gameObject);
-                        gameObject.transform.position = new Vector3(0, 0, 0.4f);
-                        doguCnt = doguBObject.Count - 1;
-                        gDogu[doguCnt] = GameObject.CreatePrimitive(PrimitiveType.Cube);
-                        gDogu[doguCnt].GetComponent<Renderer>().material = m_material;
-                        gDogu[doguCnt].layer = 8;
-                        gDogu[doguCnt].GetComponent<Renderer>().enabled = false;
-                        gDogu[doguCnt].SetActive(false);
-                        gDogu[doguCnt].transform.position = gameObject.transform.position;
-                        mDogu[doguCnt] = gDogu[doguCnt].AddComponent<MouseDrag6>();
-                        mDogu[doguCnt].isScale = false;
-                        mDogu[doguCnt].obj = gDogu[doguCnt];
-                        mDogu[doguCnt].maid = gameObject;
-                        mDogu[doguCnt].angles = gameObject.transform.eulerAngles;
-                        gDogu[doguCnt].transform.localScale = new Vector3(cubeSize, cubeSize, cubeSize);
-                        mDogu[doguCnt].ido = 1;
-                    }
+                    GUI.DrawTexture(position2, sortItem.tex);
+                    bg2Index++;
                 }
 
-                GUI.DrawTexture(position2, sortItem.tex);
-                bg2Index++;
-            }
-
-            GUI.EndScrollView();
+                GUI.EndScrollView();
             }
 
             GUI.enabled = true;
@@ -1174,13 +1174,13 @@ namespace CM3D2.MultipleMaids.Plugin
                 if (!bg2Initialized)
                 {
                     GameMain.Instance.StartCoroutine(InitializeBG2());
-                    }
-                    else
-                    {
+                }
+                else
+                {
                     SetBG2Props();
-                    }
                 }
-                            }
+            }
+        }
 
         private void BGSelectWindow(int winID)
         {

+ 5 - 0
MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.Init.cs

@@ -472,6 +472,11 @@ namespace CM3D2.MultipleMaids.Plugin
 
         public void init()
         {
+            if (bg2Busy)
+            {
+                bg2Busy = false;
+                GameMain.Instance.StopCoroutine(InitializeBG2());
+            }
             isInit = true;
             isVR = GameMain.Instance.VRMode;
             for (int index1 = 0; index1 < GameMain.Instance.CharacterMgr.GetStockMaidCount(); ++index1)

+ 17 - 15
MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.cs

@@ -5601,9 +5601,9 @@ namespace CM3D2.MultipleMaids.Plugin
 
         private class BG2Cache
         {
-            private static JsonSerializerSettings serializationSettings;
+            private static readonly JsonSerializerSettings serializationSettings;
             public static readonly string cachePath = Path.Combine(configPath, "cache.json");
-            private static Dictionary<string, SortItem> bg2Props;
+            private Dictionary<string, SortItem> bg2Props;
             public int Size => bg2Props.Count();
             private bool rebuild = false;
             public SortItem this[string menu]
@@ -5627,8 +5627,6 @@ namespace CM3D2.MultipleMaids.Plugin
 
             public BG2Cache()
             {
-                if (bg2Props != null) return;
-
                 if (!File.Exists(cachePath))
                 {
                     bg2Props = new Dictionary<string, SortItem>();
@@ -5657,15 +5655,17 @@ namespace CM3D2.MultipleMaids.Plugin
         private bool bg2Busy = false;
         private IEnumerator InitializeBG2()
         {
-            foreach (string category in accMpn)
+            if (BG2Acc == null)
             {
-                BG2Acc[category] = new List<SortItem>();
-                BG2AccInit[category] = false;
+                BG2Acc = new Dictionary<string, List<SortItem>>(StringComparer.InvariantCultureIgnoreCase);
+                BG2AccInit = new Dictionary<string, bool>(StringComparer.InvariantCultureIgnoreCase);
+                foreach (string category in accMpn)
+                {
+                    BG2Acc[category] = new List<SortItem>();
+                    BG2AccInit[category] = false;
+                }
             }
 
-            bg2Initialized = true;
-            bg2Busy = true;
-
             BG2Cache cache = new BG2Cache();
 
             if (GameUty.MenuFiles?.Length > 0)
@@ -5698,7 +5698,10 @@ namespace CM3D2.MultipleMaids.Plugin
             }
             else
             {
+                bg2Busy = true;
                 while (!GameMain.Instance.MenuDataBase.JobFinished()) yield return null;
+                if (!bg2Busy) yield break;
+                bg2Busy = false;
 
                 if (!modItemsOnly)
                 {
@@ -5748,7 +5751,8 @@ namespace CM3D2.MultipleMaids.Plugin
                     BG2Acc[item.category].Add(item);
                 }
             }
-            bg2Busy = false;
+
+            bg2Initialized = true;
             SetBG2Props();
         }
 
@@ -5759,10 +5763,8 @@ namespace CM3D2.MultipleMaids.Plugin
             File.WriteAllLines(Path.Combine(modsPath, name), list.ToArray());
         }
 
-        private Dictionary<string, List<SortItem>> BG2Acc
-            = new Dictionary<string, List<SortItem>>(StringComparer.InvariantCultureIgnoreCase);
-        private Dictionary<string, bool> BG2AccInit
-            = new Dictionary<string, bool>(StringComparer.InvariantCultureIgnoreCase);
+        private Dictionary<string, List<SortItem>> BG2Acc;
+        private Dictionary<string, bool> BG2AccInit;
 
         private void SetBG2Props()
         {