OSDN Git Service

1st attempt to fix Protocol buffer corruption issue
authormelchior <melchior@users.osdn.me>
Thu, 17 Sep 2020 03:51:03 +0000 (23:51 -0400)
committermelchior <melchior@users.osdn.me>
Thu, 17 Sep 2020 03:51:03 +0000 (23:51 -0400)
Automap/Data/ColumnMeta.cs
Automap/Data/EntitiesOfInterest.cs
Automap/Data/PngMetadataChunk.cs
Automap/Data/PointOfInterest.cs
Automap/Designators/DefaultDesignators.cs
Automap/Subsystems/AutomapSystem.cs
Automap/Subsystems/Snapshot.cs

index 77daaf4..38e83d1 100644 (file)
@@ -1,41 +1,42 @@
 using System;
 using System.Collections.Generic;
 using System.Collections.Specialized;
-
+using System.Diagnostics;
+using System.IO;
+using System.Collections.ObjectModel;
+using System.Text;
 
 using Vintagestory.API.MathTools;
 using Vintagestory.API.Common;
+using Vintagestory.API.Client;
 
 using ProtoBuf;
-using System.IO;
-using System.Collections.ObjectModel;
-using System.Text;
-using Vintagestory.API.Client;
-using Newtonsoft.Json.Linq;
 
 namespace Automap
 {
-       [ProtoContract]
+       [ProtoContract(ImplicitFields = ImplicitFields.None)]
        public struct ColumnMeta
        {
                [ProtoMember(1)]
                public Vec2i Location;
 
                [DisplayName(0, "Coords.")]
+               [ProtoIgnore]
                public string PrettyLocation;
 
                [ProtoMember(2)]
                public TimeSpan ChunkAge;//OLDEST CHUNK. from chunk last edit
 
                [DisplayName(1, "Age")]
+               [ProtoIgnore]
                public string ShortChunkAge { get => ChunkAge.ToString("c"); }
 
-               [ProtoMember(3)]
                [DisplayName(2, "Temp.")]
+               [ProtoMember(3)]
                public float Temperature;// Temperature - surface
 
-               [ProtoMember(4)]
                [DisplayName(3, "Y Max.")]
+               [ProtoMember(4)]
                public ushort YMax;// Y feature height
 
                [ProtoMember(5)]
@@ -56,28 +57,28 @@ namespace Automap
                //}
 
 
-               [ProtoMember(6)]
                [DisplayName(4, "Fert.")]
+               [ProtoMember(6)]
                public float Fertility;
 
-               [ProtoMember(7)]
                //[DisplayName(5, "Forest")]
+               [ProtoMember(7)]
                public float ForestDensity; // not given to client
 
-               [ProtoMember(8)]
                [DisplayName(6, "Rain")]
+               [ProtoMember(8)]
                public float Rainfall;
 
-               [ProtoMember(9)]
                //[DisplayName(7, "Shrub")]
+               [ProtoMember(9)]
                public float ShrubDensity; // not given to client
 
-               [ProtoMember(10)]
                [DisplayName(8, "Air blocks")]
+               [ProtoMember(10)]
                public uint AirBlocks;
 
-               [ProtoMember(11)]
                [DisplayName(9, "Non-air")]
+               [ProtoMember(11)]
                public uint NonAirBlocks;
 
                [ProtoMember(12)]
@@ -176,7 +177,7 @@ namespace Automap
                internal ColumnMeta Reload(ICoreClientAPI clientAPI)
                {
                        this.PrettyLocation = Location.PrettyCoords(clientAPI);
-                       Console.Write(PrettyLocation == null ? "*" : ",");
+                       Debug.Write(PrettyLocation == null ? "*" : ",");
                        return this;
                }
        }
index d22f328..e7b3438 100644 (file)
@@ -17,7 +17,7 @@ namespace Automap
        /// <summary>
        /// Basically the same as a POI but for an entity
        /// </summary>
-       [ProtoContract]
+       [ProtoContract(ImplicitFields = ImplicitFields.None)]
        public struct EntityOfInterest
        {
 
@@ -30,6 +30,7 @@ namespace Automap
                public string Notes;
 
                [DisplayName(1, "Loc.")]
+               [ProtoIgnore]
                public string PrettyLocation;
 
                [ProtoMember(3)]
index c62aadf..132b947 100644 (file)
@@ -1,10 +1,12 @@
 using System;
-
-using Vintagestory.API.Util;
+using System.Diagnostics;
+using System.IO;
 
 using Hjg.Pngcs;
 using Hjg.Pngcs.Chunks;
 
+using ProtoBuf;
+
 namespace Automap
 {
        /// <summary>
@@ -31,17 +33,23 @@ namespace Automap
 
                public override ChunkRaw CreateRawChunk()
                {
-                       var datas = SerializerUtil.Serialize<ColumnMeta>(ChunkMetadata);
+                       using (MemoryStream outputStream = new MemoryStream( )) 
+                       {
+                       Serializer.Serialize<ColumnMeta>(outputStream, this.ChunkMetadata);
 
-                       ChunkRaw rawChunk = createEmptyChunk(datas.Length, true);
-                       rawChunk.Data = datas;
+                       ChunkRaw pngChunk = createEmptyChunk(( int )outputStream.Length, true);
+                       pngChunk.Data = outputStream.ToArray();
 
-                       return rawChunk;
+                       return pngChunk;
+                       }
                }
 
-               public override void ParseFromRaw(ChunkRaw rawChunk)
-               {
-                       this.ChunkMetadata = SerializerUtil.Deserialize<ColumnMeta>(rawChunk.Data);
+               public override void ParseFromRaw(ChunkRaw pngChunk)
+               {                       
+                       using (MemoryStream inputStream = new MemoryStream(pngChunk.Data, false)) 
+                       {
+                       this.ChunkMetadata = Serializer.Deserialize<ColumnMeta>(inputStream);                           
+                       }
                }
 
                public override void CloneDataFromRead(PngChunk other)
index 28bf5d8..c673f7c 100644 (file)
@@ -15,7 +15,7 @@ namespace Automap
        /// <summary>
        /// Actual Physical Point in space - that is interesting.
        /// </summary>
-       [ProtoContract]
+       [ProtoContract(ImplicitFields = ImplicitFields.None)]
        public struct PointOfInterest
        {
                [DisplayName(0, "Name")]
@@ -27,6 +27,7 @@ namespace Automap
                public string Notes;
 
                [DisplayName(1, "Loc.")]
+               [ProtoIgnore]
                public string PrettyLocation;
 
                [ProtoMember(3)]
index d4ac56a..aa13d4e 100644 (file)
@@ -167,14 +167,14 @@ namespace Automap
 
                        if (te != null)
                        {
-
+                               //FIXME: Delayed rescan ?
                                StringBuilder textTarget = new StringBuilder();
                                //translocatorEntity.GetBlockInfo(clientAPI.World.Player, textTarget);
-                               textTarget.Append(te.FullyRepaired ? "Functional " : "Broken ");
-                               textTarget.Append(te.Activated ? "Online " : "Offline ");
-                               textTarget.Append(" Target: ");
-                               textTarget.Append(te.TargetLocation != null ? "Set" : "Invalid");//Or ABS coords?               
-                               textTarget.AppendFormat(" Range ({0} ~ {1})", te.MinTeleporterRangeInBlocks, te.MaxTeleporterRangeInBlocks);
+                               textTarget.Append(te.FullyRepaired ? "Functional, " : "Broken, ");
+                               textTarget.Append(te.Activated ? "Online, " : "Offline, ");
+                               textTarget.Append(" Target: ");
+                               textTarget.Append(te.TargetLocation != null ? "Set ]" : "Invalid ]");//Or ABS coords?           
+                               textTarget.AppendFormat(", Range ({0} ~ {1})", te.MinTeleporterRangeInBlocks, te.MaxTeleporterRangeInBlocks);
                                poi.AddReplace(
                                                        new PointOfInterest
                                                        {
@@ -183,10 +183,9 @@ namespace Automap
                                                                Location = posn.Copy(),
                                                                Notes = textTarget.ToString(),
                                                                Timestamp = DateTime.UtcNow,
-                                                               Destination = te.TargetLocation != null ? new BlockPosJson(te.TargetLocation) : null//FIXME: Delayed rescan
+                                                               Destination = te.TargetLocation != null ? new BlockPosJson(te.TargetLocation.Copy()) : null
                                                        }
                                                        );
-
                        }
                }
 
index 4bab7de..0a85be3 100644 (file)
@@ -3,7 +3,6 @@ using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
-using System.Reflection;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Threading;
@@ -11,8 +10,6 @@ using System.Threading;
 using Hjg.Pngcs;
 using Hjg.Pngcs.Chunks;
 
-using Newtonsoft.Json;
-
 using ProtoBuf;
 
 using Vintagestory.API.Client;
@@ -496,6 +493,11 @@ namespace Automap
                                                Logger.Error("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx);
                                                continue;
                                        }
+                                       catch (ProtoException protoEx) 
+                                       {
+                                               Logger.Error("ProtoBuf invalid! file:'{0}' - Reason: {1}", shardFile.Name, protoEx);
+                                               continue;
+                                       }
                                }
                        }
 
index 507bbec..c997333 100644 (file)
@@ -45,7 +45,7 @@ namespace Automap
                        var t = new Stopwatch();
                        t.Start();
 
-                       Console.WriteLine("snapshot started");
+                       Debug.WriteLine("snapshot started");
 
                        ImageInfo info = new ImageInfo(Width * chunkSize, Height * chunkSize, 8, false);
                        PngWriter snapWriter = FileHelper.CreatePngWriter(fileName, info, true);
@@ -114,9 +114,9 @@ namespace Automap
                        }
                        catch (Exception)
                        {
-                               Console.WriteLine("Snapshot exception!");
+                               Debug.WriteLine("Snapshot exception!");
                        }
-                       Console.WriteLine($"snapshot finished in {t.ElapsedMilliseconds}");
+                       Debug.WriteLine($"snapshot finished in {t.ElapsedMilliseconds}");
                }
 
                private async Task<Dictionary<int, byte[][]>> ReadAllInGroup(IGrouping<int, ColumnMeta> group)
@@ -138,7 +138,7 @@ namespace Automap
                        }
                        catch (Exception e)
                        {
-                               Console.WriteLine(e.Message);
+                               Debug.WriteLine(e.Message);
                                return null;
                        } // do nothing on error
                }