Browse Source

Fix issue where bg2 cannot load menu files

habeebweeb 3 years ago
parent
commit
714a07e388

+ 10 - 144
MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.Gui.cs

@@ -1,4 +1,4 @@
-using ExIni;
+using ExIni;
 using MyRoomCustom;
 using System;
 using System.Collections;
@@ -1167,109 +1167,27 @@ namespace CM3D2.MultipleMaids.Plugin
             {
                 if (itemDataList.Count == 0)
                 {
-                    #region menu files
-                    HashSet<string> modMenus = null;
                     if (!modItemsOnly)
                     {
-                        modMenus = new HashSet<string>(GameUty.ModOnlysMenuFiles);
-                    }
-
-                    string[] menuFiles = modItemsOnly ? GameUty.ModOnlysMenuFiles : GameUty.MenuFiles;
-                    foreach (string menuFile in menuFiles)
+                        for (int i = 0; i < GameMain.Instance.MenuDataBase.GetDataSize(); i++)
                     {
-                        if (menuFile.EndsWith("_del.menu"))
-                        {
-                            continue;
+                            ItemData item = new ItemData();
+                            if (ParseNativeMenuFile(i, item)) itemDataList.Add(item);
                         }
-                        byte[] buf;
-                        using (AFileBase aFileBase = GameUty.FileOpen(menuFile))
-                        {
-                            if (!aFileBase.IsValid())
-                            {
-                                continue;
                             }
-                            buf = aFileBase.ReadAll();
-                        }
 
+                    foreach (string modMenuFile in GameUty.ModOnlysMenuFiles)
+                    {
                         ItemData item = new ItemData()
                         {
-                            menu = menuFile,
-                            isMod = modItemsOnly || modMenus.Contains(menuFile)
+                            menu = modMenuFile,
+                            isMod = true
                         };
-                        BinaryReader binaryReader = new BinaryReader(new MemoryStream(buf), Encoding.UTF8);
-                        if (binaryReader.ReadString() != "CM3D2_MENU")
-                        {
-                            binaryReader.Close();
-                        }
-                        else
-                        {
-                            try
-                            {
-                                binaryReader.ReadInt32();
-                                binaryReader.ReadString();
-                                item.name = binaryReader.ReadString();
-                                item.category = binaryReader.ReadString();
-                                binaryReader.ReadString();
-                                binaryReader.ReadInt32();
-                                bool run = true;
-                                do
-                                {
-                                    int size;
-                                    do
-                                    {
-                                        size = binaryReader.ReadByte();
-                                    } while (size == 0);
-
-                                    for (int index = 0; index < size; ++index)
-                                    {
-                                        string header = binaryReader.ReadString();
-                                        if (header == "icons" || header == "icon")
-                                        {
-                                            run = false;
-                                            item.icon = binaryReader.ReadString();
-                                            break;
-                                        }
-
-                                        if (header == "priority")
-                                        {
-                                            int.TryParse(binaryReader.ReadString(), out item.priority);
-                                            break;
-                                        }
-                                    }
-                                } while (run);
-
-                                itemDataList.Add(item);
-                            }
-                            catch { }
-
-                            binaryReader.Close();
-                        }
+                        if (ParseMenuFile(modMenuFile, item)) itemDataList.Add(item);
                     }
-                    #endregion
 
-                    #region mod files
                     foreach (string modFile in Menu.GetModFiles())
                     {
-                        byte[] buf = null;
-                        try
-                        {
-                            using (FileStream fileStream = new FileStream(modFile, FileMode.Open))
-                            {
-                                if (fileStream == null)
-                                {
-                                    continue;
-                                }
-                                buf = new byte[fileStream.Length];
-                                fileStream.Read(buf, 0, (int)fileStream.Length);
-                            }
-                        }
-                        catch { }
-
-                        if (buf == null)
-                        {
-                            continue;
-                        }
-
                         ItemData item = new ItemData()
                         {
                             isMod = true,
@@ -1277,61 +1195,9 @@ namespace CM3D2.MultipleMaids.Plugin
                             menu = modFile,
                             priority = 1000
                         };
-
-                        BinaryReader binaryReader = new BinaryReader(new MemoryStream(buf), Encoding.UTF8);
-                        if (binaryReader.ReadString() != "CM3D2_MOD")
-                        {
-                            binaryReader.Close();
+                        if (ParseModMenuFile(modFile, item)) itemDataList.Add(item);
                         }
-                        else
-                        {
-                            try
-                            {
-                                binaryReader.ReadInt32();
-                                string iconName = binaryReader.ReadString();
-                                string baseItemPath = binaryReader.ReadString().Replace(":", " ");
-                                item.baseItem = Path.GetFileName(baseItemPath);
-                                item.name = binaryReader.ReadString();
-                                item.category = binaryReader.ReadString();
-                                binaryReader.ReadString();
-                                string mpnValue = binaryReader.ReadString();
-                                MPN mpn = MPN.null_mpn;
-                                try
-                                {
-                                    mpn = (MPN)Enum.Parse(typeof(MPN), mpnValue, true);
-                                }
-                                catch
-                                {
-                                    binaryReader.Close();
-                                    continue;
-                                }
-                                if (mpn != MPN.null_mpn)
-                                {
-                                    binaryReader.ReadString();
-                                }
-                                binaryReader.ReadString();
-                                int size = binaryReader.ReadInt32();
-                                for (int i = 0; i < size; i++)
-                                {
-                                    string key = binaryReader.ReadString();
-                                    int count = binaryReader.ReadInt32();
-                                    byte[] data = binaryReader.ReadBytes(count);
-                                    if (string.Equals(key, iconName, StringComparison.InvariantCultureIgnoreCase))
-                                    {
-                                        Texture2D tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
-                                        tex.LoadImage(data);
-                                        item.tex = tex;
-                                        break;
-                                    }
                                 }
-                                itemDataList.Add(item);
-                            }
-                            catch { }
-                            binaryReader.Close();
-                        }
-                    }
-                    #endregion
-                }
 
                 foreach (ItemData itemData in itemDataList)
                 {

+ 141 - 0
MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.cs

@@ -5577,6 +5577,147 @@ namespace CM3D2.MultipleMaids.Plugin
             }
         }
 
+        private static bool ParseNativeMenuFile(int menuIndex, ItemData itemData)
+        {
+            MenuDataBase menuDataBase = GameMain.Instance.MenuDataBase;
+            menuDataBase.SetIndex(menuIndex);
+            if (menuDataBase.GetBoDelOnly()) return false;
+            itemData.menu = menuDataBase.GetMenuFileName().ToLower();
+            itemData.name = menuDataBase.GetMenuName();
+            itemData.category = menuDataBase.GetCategoryMpnText();
+            itemData.icon = menuDataBase.GetIconS();
+            itemData.priority = (int)menuDataBase.GetPriority();
+            return true;
+        }
+
+        private static bool ParseMenuFile(string menuFile, ItemData itemData)
+        {
+            if (menuFile.EndsWith("_del.menu")) return false;
+
+            byte[] buf;
+            using (AFileBase aFileBase = GameUty.FileOpen(menuFile))
+            {
+                if (!aFileBase.IsValid()) return false;
+                buf = aFileBase.ReadAll();
+            }
+
+            using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buf), Encoding.UTF8))
+            {
+                if (binaryReader.ReadString() != "CM3D2_MENU") return false;
+                else
+                {
+                    try
+                    {
+                        binaryReader.ReadInt32();
+                        binaryReader.ReadString();
+                        itemData.name = binaryReader.ReadString();
+                        itemData.category = binaryReader.ReadString();
+                        binaryReader.ReadString();
+                        binaryReader.ReadInt32();
+                        bool run = true;
+                        do
+                        {
+                            int size;
+                            do
+                            {
+                                size = binaryReader.ReadByte();
+                            } while (size == 0);
+
+                            for (int index = 0; index < size; ++index)
+                            {
+                                string header = binaryReader.ReadString();
+                                if (header == "icons" || header == "icon")
+                                {
+                                    run = false;
+                                    itemData.icon = binaryReader.ReadString();
+                                    break;
+                                }
+
+                                if (header == "priority")
+                                {
+                                    int.TryParse(binaryReader.ReadString(), out itemData.priority);
+                                    break;
+                                }
+                            }
+                        } while (run);
+                    }
+                    catch
+                    {
+                        return false;
+                    }
+                }
+            }
+            return true;
+        }
+
+        private static bool ParseModMenuFile(string modMenuFile, ItemData modItemData)
+        {
+            byte[] buf = null;
+            try
+            {
+                using (FileStream fileStream = new FileStream(modMenuFile, FileMode.Open))
+                {
+                    if (fileStream == null) return false;
+                    buf = new byte[fileStream.Length];
+                    fileStream.Read(buf, 0, (int)fileStream.Length);
+                }
+            }
+            catch { }
+
+            if (buf == null) return false;
+
+            using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buf), Encoding.UTF8))
+            {
+                if (binaryReader.ReadString() != "CM3D2_MOD") return false;
+                else
+                {
+                    try
+                    {
+                        binaryReader.ReadInt32();
+                        string iconName = binaryReader.ReadString();
+                        string baseItemPath = binaryReader.ReadString().Replace(":", " ");
+                        modItemData.baseItem = Path.GetFileName(baseItemPath);
+                        modItemData.name = binaryReader.ReadString();
+                        modItemData.category = binaryReader.ReadString();
+                        binaryReader.ReadString();
+                        string mpnValue = binaryReader.ReadString();
+                        MPN mpn = MPN.null_mpn;
+                        try
+                        {
+                            mpn = (MPN)Enum.Parse(typeof(MPN), mpnValue, true);
+                        }
+                        catch
+                        {
+                            return false;
+                        }
+                        if (mpn != MPN.null_mpn)
+                        {
+                            binaryReader.ReadString();
+                        }
+                        binaryReader.ReadString();
+                        int size = binaryReader.ReadInt32();
+                        for (int i = 0; i < size; i++)
+                        {
+                            string key = binaryReader.ReadString();
+                            int count = binaryReader.ReadInt32();
+                            byte[] data = binaryReader.ReadBytes(count);
+                            if (string.Equals(key, iconName, StringComparison.InvariantCultureIgnoreCase))
+                            {
+                                Texture2D tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
+                                tex.LoadImage(data);
+                                modItemData.tex = tex;
+                                break;
+                            }
+                        }
+                    }
+                    catch
+                    {
+                        return false;
+                    }
+                }
+            }
+            return true;
+        }
         private static string PluginString()
         {
             return $"{PluginName} {PluginVersion}";