瀏覽代碼

Wrap scene save in try catch

habeebweeb 3 年之前
父節點
當前提交
37679cb884
共有 1 個文件被更改,包括 33 次插入23 次删除
  1. 33 23
      COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/MeidoPhotoStudio.cs

+ 33 - 23
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/MeidoPhotoStudio.cs

@@ -78,37 +78,47 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         {
             if (meidoManager.Busy) return null;
 
-            using var memoryStream = new MemoryStream();
-            using var headerWriter = new BinaryWriter(memoryStream, Encoding.UTF8);
+            try
+            {
+                using var memoryStream = new MemoryStream();
+                using var headerWriter = new BinaryWriter(memoryStream, Encoding.UTF8);
 
-            headerWriter.Write(SceneHeader);
+                headerWriter.Write(SceneHeader);
 
-            new SceneMetadata{
-                Version = sceneVersion, Environment = environment, 
-                MaidCount = environment ? kankyoMagic : meidoManager.ActiveMeidoList.Count
-            }.WriteMetadata(headerWriter);
+                new SceneMetadata
+                {
+                    Version = sceneVersion,
+                    Environment = environment,
+                    MaidCount = environment ? kankyoMagic : meidoManager.ActiveMeidoList.Count
+                }.WriteMetadata(headerWriter);
 
-            using var compressionStream = memoryStream.GetCompressionStream();
-            using var dataWriter = new BinaryWriter(compressionStream, Encoding.UTF8);
+                using var compressionStream = memoryStream.GetCompressionStream();
+                using var dataWriter = new BinaryWriter(compressionStream, Encoding.UTF8);
 
-            if (!environment)
-            {
-                Serialization.Get<MeidoManager>().Serialize(meidoManager, dataWriter);
-                Serialization.Get<MessageWindowManager>().Serialize(messageWindowManager, dataWriter);
-                Serialization.Get<CameraManager>().Serialize(cameraManager, dataWriter);
-            }
+                if (!environment)
+                {
+                    Serialization.Get<MeidoManager>().Serialize(meidoManager, dataWriter);
+                    Serialization.Get<MessageWindowManager>().Serialize(messageWindowManager, dataWriter);
+                    Serialization.Get<CameraManager>().Serialize(cameraManager, dataWriter);
+                }
 
-            Serialization.Get<LightManager>().Serialize(lightManager, dataWriter);
-            Serialization.Get<EffectManager>().Serialize(effectManager, dataWriter);
-            Serialization.Get<EnvironmentManager>().Serialize(environmentManager, dataWriter);
-            Serialization.Get<PropManager>().Serialize(propManager, dataWriter);
+                Serialization.Get<LightManager>().Serialize(lightManager, dataWriter);
+                Serialization.Get<EffectManager>().Serialize(effectManager, dataWriter);
+                Serialization.Get<EnvironmentManager>().Serialize(environmentManager, dataWriter);
+                Serialization.Get<PropManager>().Serialize(propManager, dataWriter);
 
-            dataWriter.Write("END");
+                dataWriter.Write("END");
 
-            compressionStream.Close();
+                compressionStream.Close();
 
-            var data = memoryStream.ToArray();
-            return data;
+                var data = memoryStream.ToArray();
+                return data;
+            }
+            catch (Exception e)
+            {
+                Utility.LogError($"Failed to save scene because {e.Message}\n{e.StackTrace}");
+                return null;
+            }
         }
 
         public void LoadScene(byte[] buffer)