Browse Source

Add more checks to menu file parsing

Add check for empty menu files.
Wrap menu file parsing in try/catch
habeebweeb 3 years ago
parent
commit
563d2cf357
1 changed files with 50 additions and 24 deletions
  1. 50 24
      MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.cs

+ 50 - 24
MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.cs

@@ -5698,13 +5698,13 @@ namespace CM3D2.MultipleMaids.Plugin
             }
             else
             {
-                bg2Busy = true;
-                while (!GameMain.Instance.MenuDataBase.JobFinished()) yield return null;
-                if (!bg2Busy) yield break;
-                bg2Busy = false;
-
                 if (!modItemsOnly)
                 {
+                    bg2Busy = true;
+                    while (!GameMain.Instance.MenuDataBase.JobFinished()) yield return null;
+                    if (!bg2Busy) yield break;
+                    bg2Busy = false;
+
                     for (int i = 0; i < GameMain.Instance.MenuDataBase.GetDataSize(); i++)
                     {
                         SortItem item = new SortItem();
@@ -5855,19 +5855,34 @@ namespace CM3D2.MultipleMaids.Plugin
         {
             if (!validBG2Menu(menuFile)) return false;
 
-            byte[] buf;
-            using (AFileBase aFileBase = GameUty.FileOpen(menuFile))
+            byte[] buf = null;
+            try
+            {
+                using (AFileBase aFileBase = GameUty.FileOpen(menuFile))
+                {
+                    if (!aFileBase.IsValid()) return false;
+                    if (aFileBase.GetSize() == 0)
+                    {
+                        Util.Logger.Log(Util.LogLevel.Error, $"Menu file '{menuFile}' is empty");
+                        return false;
+                    }
+                    buf = aFileBase.ReadAll();
+                }
+            }
+            catch
             {
-                if (!aFileBase.IsValid()) return false;
-                buf = aFileBase.ReadAll();
+                Util.Logger.Log(Util.LogLevel.Error, $"Could not read menu file '{menuFile}'");
+                return false;
             }
 
+            if (buf == null) return false;
+
             using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buf), Encoding.UTF8))
             {
-                if (binaryReader.ReadString() != "CM3D2_MENU") return false;
-                else
+                try
                 {
-                    try
+                    if (binaryReader.ReadString() != "CM3D2_MENU") return false;
+                    else
                     {
                         binaryReader.ReadInt32();
                         binaryReader.ReadString();
@@ -5903,10 +5918,11 @@ namespace CM3D2.MultipleMaids.Plugin
                             }
                         } while (run);
                     }
-                    catch
-                    {
-                        return false;
-                    }
+                }
+                catch
+                {
+                    Util.Logger.Log(Util.LogLevel.Error, $"Could not parse menu file '{menuFile}'");
+                    return false;
                 }
             }
             return true;
@@ -5922,20 +5938,29 @@ namespace CM3D2.MultipleMaids.Plugin
                 using (FileStream fileStream = new FileStream(modMenuFile, FileMode.Open))
                 {
                     if (fileStream == null) return false;
+                    if (fileStream.Length == 0L)
+                    {
+                        Util.Logger.Log(Util.LogLevel.Error, $"Mod menu file '{modMenuFile}' is empty");
+                        return false;
+                    }
                     buf = new byte[fileStream.Length];
                     fileStream.Read(buf, 0, (int)fileStream.Length);
                 }
             }
-            catch { }
+            catch
+            {
+                Util.Logger.Log(Util.LogLevel.Error, $"Could not read mod menu file '{modMenuFile}'");
+                return false;
+            }
 
             if (buf == null) return false;
 
             using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buf), Encoding.UTF8))
             {
-                if (binaryReader.ReadString() != "CM3D2_MOD") return false;
-                else
+                try
                 {
-                    try
+                    if (binaryReader.ReadString() != "CM3D2_MOD") return false;
+                    else
                     {
                         binaryReader.ReadInt32();
                         string iconName = binaryReader.ReadString();
@@ -5975,10 +6000,11 @@ namespace CM3D2.MultipleMaids.Plugin
                             }
                         }
                     }
-                    catch
-                    {
-                        return false;
-                    }
+                }
+                catch
+                {
+                    Util.Logger.Log(Util.LogLevel.Error, $"Could not parse mod menu file '{modMenuFile}'");
+                    return false;
                 }
             }
             return true;