Browse Source

Make conversion more defensive

habeebweeb 3 years ago
parent
commit
0ae9606882

+ 16 - 2
src/MeidoPhotoStudio.Converter/Converters/MMConverter.cs

@@ -60,8 +60,22 @@ namespace MeidoPhotoStudio.Converter.Converters
         {
         {
             var background = int.Parse(key.Key.Substring(1)) >= 10000;
             var background = int.Parse(key.Key.Substring(1)) >= 10000;
 
 
-            var convertedData = MMSceneConverter.Convert(key.Value, background);
-            var sceneMetadata = MMSceneConverter.GetSceneMetadata(key.Value, background);
+            byte[] convertedData;
+            MeidoPhotoStudio.Plugin.SceneMetadata sceneMetadata;
+
+            try
+            {
+                convertedData = MMSceneConverter.Convert(key.Value, background);
+                sceneMetadata = MMSceneConverter.GetSceneMetadata(key.Value, background);
+            }
+            catch (Exception e)
+            {
+                if (Plugin.Instance == null)
+                    return;
+
+                Plugin.Instance.Logger.LogError($"Could not convert {Path.GetFileName(filePath)} scene because {e}");
+                return;
+            }
 
 
             var screenshotKey = $"s{key.Key}"; // ex. ss100=thumb_base64
             var screenshotKey = $"s{key.Key}"; // ex. ss100=thumb_base64
             string? screenshotBase64 = null;
             string? screenshotBase64 = null;

+ 18 - 5
src/MeidoPhotoStudio.Converter/Converters/MMPngConverter.cs

@@ -44,7 +44,7 @@ namespace MeidoPhotoStudio.Converter.Converters
         {
         {
             var fileStream = File.OpenRead(pngFile);
             var fileStream = File.OpenRead(pngFile);
 
 
-            var thumbnailData = PngUtility.ExtractPng(fileStream);
+            var thumbnailData = PngUtility.ExtractPng(fileStream) ?? MPSSceneSerializer.NoThumb;
 
 
             var kankyo = new byte[KankyoHeader.Length];
             var kankyo = new byte[KankyoHeader.Length];
             fileStream.Read(kankyo, 0, KankyoHeader.Length);
             fileStream.Read(kankyo, 0, KankyoHeader.Length);
@@ -70,8 +70,7 @@ namespace MeidoPhotoStudio.Converter.Converters
                 if (Plugin.Instance == null)
                 if (Plugin.Instance == null)
                     return;
                     return;
 
 
-                var logger = Plugin.Instance.Logger;
-                logger.LogWarning($"Could not decompress scene data from {pngFile} because {e}");
+                Plugin.Instance.Logger.LogWarning($"Could not decompress scene data from {pngFile} because {e}");
 
 
                 return;
                 return;
             }
             }
@@ -79,8 +78,22 @@ namespace MeidoPhotoStudio.Converter.Converters
             if (string.IsNullOrEmpty(sceneData))
             if (string.IsNullOrEmpty(sceneData))
                 return;
                 return;
 
 
-            var convertedData = MMSceneConverter.Convert(sceneData, background);
-            var sceneMetadata = MMSceneConverter.GetSceneMetadata(sceneData, background);
+            byte[] convertedData;
+            MeidoPhotoStudio.Plugin.SceneMetadata sceneMetadata;
+
+            try
+            {
+                convertedData = MMSceneConverter.Convert(sceneData, background);
+                sceneMetadata = MMSceneConverter.GetSceneMetadata(sceneData, background);
+            }
+            catch (Exception e)
+            {
+                if (Plugin.Instance == null)
+                    return;
+
+                Plugin.Instance.Logger.LogError($"Could not convert {pngFile} because {e}");
+                return;
+            }
 
 
             MPSSceneSerializer.SaveToFile(outputFilename, sceneMetadata, convertedData, thumbnailData);
             MPSSceneSerializer.SaveToFile(outputFilename, sceneMetadata, convertedData, thumbnailData);
         }
         }

+ 10 - 11
src/MeidoPhotoStudio.Converter/PluginCore.cs

@@ -17,21 +17,20 @@ namespace MeidoPhotoStudio.Converter
 
 
         public void Convert()
         public void Convert()
         {
         {
-            if (!Directory.Exists(WorkingDirectory))
-                Directory.CreateDirectory(WorkingDirectory);
+            Directory.CreateDirectory(WorkingDirectory);
 
 
-            try
+            foreach (var converter in converters)
             {
             {
-                foreach (var converter in converters)
+                try
+                {
                     converter.Convert(WorkingDirectory);
                     converter.Convert(WorkingDirectory);
-            }
-            catch (Exception e)
-            {
-                if (Plugin.Instance is not null)
+                }
+                catch (Exception e)
                 {
                 {
-                    var logger = Plugin.Instance.Logger;
-                    logger.LogWarning($"Could not convert data because {e.Message}");
-                    logger.LogMessage(e.StackTrace);
+                    if (Plugin.Instance == null)
+                        continue;
+
+                    Plugin.Instance.Logger.LogError($"Could not convert data because {e}");
                 }
                 }
             }
             }
         }
         }

+ 28 - 21
src/MeidoPhotoStudio.Converter/Utility/PngUtility.cs

@@ -24,28 +24,35 @@ namespace MeidoPhotoStudio.Converter.Utility
             var fourByteBuffer = new byte[4];
             var fourByteBuffer = new byte[4];
             var chunkBuffer = new byte[1024];
             var chunkBuffer = new byte[1024];
 
 
-            do
+            try
             {
             {
-                // chunk length
-                var read = stream.Read(fourByteBuffer, 0, 4);
-                memoryStream.Write(fourByteBuffer, 0, read);
-
-                if (BitConverter.IsLittleEndian)
-                    Array.Reverse(fourByteBuffer);
-
-                var length = BitConverter.ToUInt32(fourByteBuffer, 0);
-
-                // chunk type
-                read = stream.Read(fourByteBuffer, 0, 4);
-                memoryStream.Write(fourByteBuffer, 0, read);
-
-                if (chunkBuffer.Length < length + 4L)
-                    chunkBuffer = new byte[length + 4L];
-
-                // chunk data + CRC
-                read = stream.Read(chunkBuffer, 0, (int)(length + 4L));
-                memoryStream.Write(chunkBuffer, 0, read);
-            } while (!MeidoPhotoStudio.Plugin.Utility.BytesEqual(fourByteBuffer, PngEnd));
+                do
+                {
+                    // chunk length
+                    var read = stream.Read(fourByteBuffer, 0, 4);
+                    memoryStream.Write(fourByteBuffer, 0, read);
+
+                    if (BitConverter.IsLittleEndian)
+                        Array.Reverse(fourByteBuffer);
+
+                    var length = BitConverter.ToUInt32(fourByteBuffer, 0);
+
+                    // chunk type
+                    read = stream.Read(fourByteBuffer, 0, 4);
+                    memoryStream.Write(fourByteBuffer, 0, read);
+
+                    if (chunkBuffer.Length < length + 4L)
+                        chunkBuffer = new byte[length + 4L];
+
+                    // chunk data + CRC
+                    read = stream.Read(chunkBuffer, 0, (int)(length + 4L));
+                    memoryStream.Write(chunkBuffer, 0, read);
+                } while (!MeidoPhotoStudio.Plugin.Utility.BytesEqual(fourByteBuffer, PngEnd));
+            }
+            catch
+            {
+                return null;
+            }
 
 
             return memoryStream.ToArray();
             return memoryStream.ToArray();
         }
         }