Browse Source

Add better error handling

ghorsington 4 years ago
parent
commit
8260641ff8
1 changed files with 33 additions and 9 deletions
  1. 33 9
      COM3D2.CacheEditMenu/Hooks.cs

+ 33 - 9
COM3D2.CacheEditMenu/Hooks.cs

@@ -4,6 +4,7 @@ using System.Diagnostics;
 using System.IO;
 using System.Reflection;
 using System.Reflection.Emit;
+using Debug = UnityEngine.Debug;
 
 namespace COM3D2.CacheEditMenu
 {
@@ -141,8 +142,16 @@ namespace COM3D2.CacheEditMenu
             };
             getTexFileIDDic().TryGetValue(mi.m_nMenuFileRID, out menuInfo.texName);
             infoCache[menuFileName] = menuInfo;
-            
-            menuInfo.Serialize(cacheWriter);
+
+            try
+            {
+                menuInfo.Serialize(cacheWriter);
+            }
+            catch (Exception e)
+            {
+                Debug.Log($"Failed to serialize menu file {menuFileName}: {e.Message}. The cache may be corrupted and will be rebuilt on next game run.");
+            }
+            cacheWriter.Flush();
             
             return true;
         }
@@ -154,6 +163,8 @@ namespace COM3D2.CacheEditMenu
             var cacheDir = Path.Combine(Patcher.Patcher.SybarisPath, "EditMenuCache");
             var cachePath = Path.Combine(cacheDir, "EditMenuCache.dat");
 
+            var rebuildCache = false;
+
             Directory.CreateDirectory(cacheDir);
 
             if (File.Exists(cachePath))
@@ -168,13 +179,26 @@ namespace COM3D2.CacheEditMenu
                             infoCache[menuInfo.key] = menuInfo;
                         }
                     }
+                    catch (FormatException e)
+                    {
+                        Debug.Log($"Failed to deserialize cache because {e.Message}. Rebuilding the cache...");
+                        rebuildCache = true;
+                    }
                     catch (EndOfStreamException)
                     {
                         // End of file, no need to read more
                     }
                 }
 
-            cacheWriter = new BinaryWriter(File.OpenWrite(cachePath));
+            var stream = rebuildCache ? File.Create(cachePath) : File.OpenWrite(cachePath);
+            cacheWriter = new BinaryWriter(stream);
+
+            if (rebuildCache)
+            {
+                foreach (var keyValuePair in infoCache)
+                    keyValuePair.Value.Serialize(cacheWriter);
+                cacheWriter.Flush();
+            }
             cacheWriter.BaseStream.Seek(0, SeekOrigin.End);
         }
 
@@ -186,14 +210,14 @@ namespace COM3D2.CacheEditMenu
 
             public void Deserialize(BinaryReader br)
             {
-                key = br.ReadString();
+                key = br.ReadNullableString();
                 texName = br.ReadNullableString();
                 mi = new SceneEdit.SMenuItem
                 {
-                    m_strMenuName = br.ReadString(),
+                    m_strMenuName = br.ReadNullableString(),
                     m_strInfo = br.ReadNullableString(),
                     m_mpn = (MPN) br.ReadInt32(),
-                    m_strCateName = br.ReadString(),
+                    m_strCateName = br.ReadNullableString(),
                     m_eColorSetMPN = (MPN) br.ReadInt32(),
                     m_strMenuNameInColorSet = br.ReadNullableString(),
                     m_pcMultiColorID = (MaidParts.PARTS_COLOR) br.ReadInt32(),
@@ -205,12 +229,12 @@ namespace COM3D2.CacheEditMenu
 
             public void Serialize(BinaryWriter bw)
             {
-                bw.Write(key);
+                bw.WriteNullableString(key);
                 bw.WriteNullableString(texName);
-                bw.Write(mi.m_strMenuName);
+                bw.WriteNullableString(mi.m_strMenuName);
                 bw.WriteNullableString(mi.m_strInfo);
                 bw.Write((int) mi.m_mpn);
-                bw.Write(mi.m_strCateName);
+                bw.WriteNullableString(mi.m_strCateName);
                 bw.Write((int) mi.m_eColorSetMPN);
                 bw.WriteNullableString(mi.m_strMenuNameInColorSet);
                 bw.Write((int) mi.m_pcMultiColorID);