OSDN Git Service

1/2 Fix; Truncation of Protocol-buffer POI data
authormelchior <melchior@users.osdn.me>
Fri, 18 Jun 2021 01:19:09 +0000 (21:19 -0400)
committermelchior <melchior@users.osdn.me>
Fri, 18 Jun 2021 01:19:09 +0000 (21:19 -0400)
Auto-assigned Designators, Teleport tracking...

Automap/Automap.csproj
Automap/Data/BlockDesignator.cs
Automap/Data/EntityDesignator.cs
Automap/Data/PersistedConfiguration.cs
Automap/Designators/DefaultDesignators.cs
Automap/Helpers.cs
Automap/Subsystems/AutomapSystem.cs
Automap/modinfo.json

index bb756e8..d864e9d 100644 (file)
       <HintPath>VS_libs\Newtonsoft.Json.dll</HintPath>
       <Private>False</Private>
     </Reference>
+    <Reference Include="0Harmony">
+      <HintPath>VS_libs\0Harmony.dll</HintPath>
+      <Private>False</Private>
+    </Reference>
   </ItemGroup>
   <ItemGroup>
     <Compile Include="AutomapMod.cs" />
index 8511308..224b1f6 100644 (file)
@@ -3,6 +3,9 @@ using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Drawing;
 using System.Runtime.Serialization;
+using System.Linq;
+
+using HarmonyLib;
 
 using Newtonsoft.Json;
 
@@ -63,31 +66,20 @@ namespace Automap
 
                public override string ToString()
                {
-                       return Pattern.ToShortString() + "|" + OverwriteColor.Name + "|" + Material ?? "";
+                       return $"{Pattern.ToShortString()}  | {OverwriteColor.Name} | {(Material.HasValue ? Material.ToString() : "?")} | [{SpecialActionName}]";
                }
 
                [OnDeserialized]
                public void RelinkDesignator(StreamingContext sCtx )
-               {
-                       //TODO: properly Via reflection - and support for external designators?
+               {                       
                        if (SpecialAction == null && !String.IsNullOrEmpty(SpecialActionName)) {
-                               switch (SpecialActionName) {
-
-                                       case "DecodeSign":
-                                       SpecialAction = DefaultDesignators.DecodeSign;
-                                       break;
-                               
-                                       case "DecodePostSign":
-                                       SpecialAction = DefaultDesignators.DecodePostSign;
-                                       break;
-                               
-                                       case "DecodeTranslocator":
-                                       SpecialAction = DefaultDesignators.DecodeTranslocator;
-                                       break;
-
-                               }
-                       }
+                       //TODO: still not truely dynamic...                     
 
+                       BlockDesignator delegateTarget = DefaultDesignators.DefaultBlockDesignators.FirstOrDefault((BlockDesignator arg) => arg.SpecialActionName == SpecialActionName);                
+                       
+                       if (delegateTarget != null)     this.SpecialAction = delegateTarget?.SpecialAction;
+
+                       }
                }
        }
 }
index 50dd7a8..93bdf24 100644 (file)
@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Drawing;
 using System.Runtime.Serialization;
+using System.Linq;
 
 using Newtonsoft.Json;
 
@@ -68,19 +69,18 @@ namespace Automap
                [OnDeserialized]
                public void RelinkDesignator(StreamingContext sCtx)
                {
-               //TODO: properly Via reflection - and support for external designators?
-               if (SpecialAction == null && !String.IsNullOrEmpty(SpecialActionName)) {
-               switch (SpecialActionName) {
+               
+               if (SpecialAction == null && !String.IsNullOrEmpty(SpecialActionName)) 
+                       {
+                       //TODO: still not truely dynamic...                     
 
-               case "KeepTrackOfMerchant":
-                       SpecialAction = DefaultDesignators.KeepTrackOfMerchant;
-                       break;
+                       EntityDesignator delegateTarget = DefaultDesignators.DefaultEntityDesignators.FirstOrDefault((EntityDesignator arg) => arg.SpecialActionName == SpecialActionName);
 
-               
-               }
-               }
+                       if (delegateTarget != null) this.SpecialAction = delegateTarget?.SpecialAction;
 
+                       }
                }
+
        }
 }
 
index 23bce7d..782107b 100644 (file)
@@ -35,8 +35,8 @@ namespace Automap
                {
 
                if (defaults) {
-               BlockDesignators = DefaultDesignators.DefaultBlockDesignators( );
-               EntityDesignators= DefaultDesignators.DefaultEntityDesignators( );
+               BlockDesignators = DefaultDesignators.DefaultBlockDesignators;
+               EntityDesignators= DefaultDesignators.DefaultEntityDesignators;
                }
 
                }
index 102de1b..4bdcaab 100644 (file)
@@ -3,7 +3,7 @@ using System.Collections.Generic;
 using System.Drawing;
 using System.Text;
 using System.Text.RegularExpressions;
-
+using HarmonyLib;
 using Vintagestory.API.Client;
 using Vintagestory.API.Common;
 using Vintagestory.API.Common.Entities;
@@ -14,7 +14,9 @@ using Vintagestory.GameContent;
 namespace Automap
 {
        public static class DefaultDesignators
-       {               
+       {
+               #region Defaults
+
                public static BlockDesignator Roads =
                         new BlockDesignator(
                                new AssetLocation("game", "stonepath"),
@@ -54,6 +56,14 @@ namespace Automap
                                DecodeTranslocator
                        );
 
+               public static BlockDesignator Teleporters =
+                        new BlockDesignator(
+                               new AssetLocation("game", "teleporterbase"),
+                               Color.SeaGreen,
+                               EnumBlockMaterial.Wood,
+                               DecodeTeleport
+                       );
+
                public static EntityDesignator Traders =
                         new EntityDesignator(
                                new AssetLocation("game", "humanoid-trader"),
@@ -66,163 +76,200 @@ namespace Automap
                /// Not just blocks, but block-entities as well!
                /// </summary>
                /// <returns>The block designators.</returns>
-               public static List<BlockDesignator> DefaultBlockDesignators()
+               public static List<BlockDesignator> DefaultBlockDesignators
                {
-                       return new List<BlockDesignator>{
-                               DefaultDesignators.Roads,
-                               DefaultDesignators.GroundSigns,
-                               DefaultDesignators.WallSigns,
-                               DefaultDesignators.PostSigns,
-                               DefaultDesignators.Translocators,
-                               };
+                       get
+                       {
+                               return new List<BlockDesignator>{
+                                       DefaultDesignators.Roads,
+                                       DefaultDesignators.GroundSigns,
+                                       DefaultDesignators.WallSigns,
+                                       DefaultDesignators.PostSigns,
+                                       DefaultDesignators.Translocators,
+                                       DefaultDesignators.Teleporters,
+                                       };
+                       }
                }
 
-               public static List<EntityDesignator> DefaultEntityDesignators()
+               public static List<EntityDesignator> DefaultEntityDesignators
                {
-                       return new List<EntityDesignator>{
-                               DefaultDesignators.Traders,
-               };
+                       get
+                       {
+                               return new List<EntityDesignator>{
+                                               DefaultDesignators.Traders,
+                               };
+                       }
                }
 
+               #endregion
+
+
                #region Designators
 
                internal static void DecodeSign(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
                {
 #if DEBUG
-                       clientAPI.Logger.VerboseDebug("Sign Designator Invoked!");
+               clientAPI.Logger.VerboseDebug("Sign Designator Invoked!");
 #endif
-                       //sign Text into a POI field...
-                       BlockEntitySign signEntity = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntitySign;
+               //sign Text into a POI field...
+               BlockEntitySign signEntity = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntitySign;
 
-                       if (signEntity != null && !String.IsNullOrEmpty(signEntity.text))
-                       {
+               if (signEntity != null && !String.IsNullOrEmpty(signEntity.text)) {
 
-                               poi.AddReplace(
-                                                       new PointOfInterest
-                                                       {
-                                                               Name = "Sign",
-                                                               PrettyLocation = posn.PrettyCoords(clientAPI),
-                                                               Location = posn.Copy(),
-                                                               Notes = signEntity.text,
-                                                               Timestamp = DateTime.UtcNow,
-                                                       }
-                                                       );
-                       }
+               poi.AddReplace(
+                                       new PointOfInterest {
+                                               Name = "Sign",
+                                               PrettyLocation = posn.PrettyCoords(clientAPI),
+                                               Location = posn.Copy( ),
+                                               Notes = signEntity.text,
+                                               Timestamp = DateTime.UtcNow,
+                                       }
+                                       );
+               }
                }
 
 
                internal static void DecodePostSign(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
                {
 #if DEBUG
-                       clientAPI.Logger.VerboseDebug("Post-sign Designator Invoked!");
+               clientAPI.Logger.VerboseDebug("Post-sign Designator Invoked!");
 #endif
-                       //sign post Text into a POI field...
-                       BlockEntitySignPost signEntity = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntitySignPost;
+               //sign post Text into a POI field...
+               BlockEntitySignPost signEntity = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntitySignPost;
 
-                       if (signEntity != null && signEntity.textByCardinalDirection?.Length > 0)
-                       {
+               if (signEntity != null && signEntity.textByCardinalDirection?.Length > 0) {
 
-                               poi.AddReplace(
-                                                       new PointOfInterest
-                                                       {
-                                                               Name = "Signpost",
-                                                               PrettyLocation = posn.PrettyCoords(clientAPI),
-                                                               Location = posn.Copy(),
-                                                               Notes = string.Join(",", signEntity.textByCardinalDirection),
-                                                               Timestamp = DateTime.UtcNow,
-                                                       }
-                                                       );
-                       }
+               poi.AddReplace(
+                                       new PointOfInterest {
+                                               Name = "Signpost",
+                                               PrettyLocation = posn.PrettyCoords(clientAPI),
+                                               Location = posn.Copy( ),
+                                               Notes = string.Join(",", signEntity.textByCardinalDirection),
+                                               Timestamp = DateTime.UtcNow,
+                                       }
+                                       );
+               }
                }
 
                internal static void KeepTrackOfMerchant(ICoreClientAPI clientAPI, EntitiesOfInterest poi, BlockPos posn, Entity entity)
                {
-                       //clientAPI.Logger.VerboseDebug("Trader: {0} @ {1}", entity.GetName(), posn);
-
-                       var traderJoe = entity as EntityTrader;
-                       var traderName = entity.GetBehavior<EntityBehaviorNameTag>()?.DisplayName;
-                       string code;
-                       // this makes me ill
-                       switch (entity.Code.Path) {
-                               case "humanoid-trader-artisan":
-                                       code = "{0} the artisan";
-                                       break;
-                               case "humanoid-trader-treasurehunter":
-                                       code = "{0} the treasure hunter";
-                                       break;
-                               case "humanoid-trader-buildmaterials":
-                                       code = "{0} the building materials trader";
-                                       break;
-                               case "humanoid-trader-clothing":
-                                       code = "{0} the clothing merchant";
-                                       break;
-                               case "humanoid-trader-commodities":
-                                       code = "{0} the commodities merchant";
-                                       break;
-                               case "humanoid-trader-foods":
-                                       code = "{0} the foods supplier";
-                                       break;
-                               case "humanoid-trader-furniture":
-                                       code = "{0} the furniture trader";
-                                       break;
-                               case "humanoid-trader-luxuries":
-                                       code = "{0} the luxuries merchant";
-                                       break;
-                               case "humanoid-trader-survivalgoods":
-                                       code = "{0} the survival goods supplier";
-                                       break;
-                               default:
-                                       code = "";
-                                       break;
-                       }
-                       var message = string.Format(code, traderName);
-                       if (traderJoe.TradeProps != null)
-                       {
-                               message += $" - Gears: {traderJoe.TradeProps.Money}, ";
-                       }
-                       poi.AddReplace(new EntityOfInterest
-                       {
-                               Name = "Trader",
-                               PrettyLocation = posn.PrettyCoords(clientAPI),
-                               Location = posn.Copy(),
-                               Notes = message,
-                               Timestamp = DateTime.UtcNow,
-                               EntityId = entity.EntityId
-                       });
+               //clientAPI.Logger.VerboseDebug("Trader: {0} @ {1}", entity.GetName(), posn);
+
+               var traderJoe = entity as EntityTrader;
+               var traderName = entity.GetBehavior<EntityBehaviorNameTag>( )?.DisplayName;
+               string code;
+               // this makes me ill
+               switch (entity.Code.Path) {
+               case "humanoid-trader-artisan":
+                       code = "{0} the artisan";
+                       break;
+               case "humanoid-trader-treasurehunter":
+                       code = "{0} the treasure hunter";
+                       break;
+               case "humanoid-trader-buildmaterials":
+                       code = "{0} the building materials trader";
+                       break;
+               case "humanoid-trader-clothing":
+                       code = "{0} the clothing merchant";
+                       break;
+               case "humanoid-trader-commodities":
+                       code = "{0} the commodities merchant";
+                       break;
+               case "humanoid-trader-foods":
+                       code = "{0} the foods supplier";
+                       break;
+               case "humanoid-trader-furniture":
+                       code = "{0} the furniture trader";
+                       break;
+               case "humanoid-trader-luxuries":
+                       code = "{0} the luxuries merchant";
+                       break;
+               case "humanoid-trader-survivalgoods":
+                       code = "{0} the survival goods supplier";
+                       break;
+               default:
+                       code = "";
+                       break;
+               }
+               var message = string.Format(code, traderName);
+               if (traderJoe.TradeProps != null) {
+               message += $" - Gears: {traderJoe.TradeProps.Money}, ";
+               }
+               poi.AddReplace(new EntityOfInterest {
+                       Name = "Trader",
+                       PrettyLocation = posn.PrettyCoords(clientAPI),
+                       Location = posn.Copy( ),
+                       Notes = message,
+                       Timestamp = DateTime.UtcNow,
+                       EntityId = entity.EntityId
+               });
                }
 
                internal static void DecodeTranslocator(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
                {
-               #if DEBUG
+#if DEBUG
                clientAPI.Logger.VerboseDebug("TRANSLOCATOR Designator Invoked!");
+#endif
+               //Where to? and from!
+
+               BlockEntityStaticTranslocator te = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityStaticTranslocator;
+
+               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);
+               poi.AddReplace(
+                                       new PointOfInterest {
+                                               Name = "Translocator",
+                                               PrettyLocation = posn.PrettyCoords(clientAPI),
+                                               Location = posn.Copy( ),
+                                               Notes = textTarget.ToString( ),
+                                               Timestamp = DateTime.UtcNow,
+                                               Destination = te.TargetLocation != null ? new BlockPosJson(te.TargetLocation.Copy( )) : null
+                                       }
+                                       );
+               }
+               }
+
+
+               internal static void DecodeTeleport(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
+               {
+               #if DEBUG
+               clientAPI.Logger.VerboseDebug("Teleport Designator Invoked!");
                #endif
-                       //Where to? and from!
+               
 
-                       BlockEntityStaticTranslocator te = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityStaticTranslocator;
+               BlockEntityTeleporter tele = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityTeleporter;
 
-                       if (te != null)
+               if (tele != 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);
-                               poi.AddReplace(
-                                                       new PointOfInterest
-                                                       {
-                                                               Name = "Translocator",
-                                                               PrettyLocation = posn.PrettyCoords(clientAPI),
-                                                               Location = posn.Copy(),
-                                                               Notes = textTarget.ToString(),
-                                                               Timestamp = DateTime.UtcNow,
-                                                               Destination = te.TargetLocation != null ? new BlockPosJson(te.TargetLocation.Copy()) : null
-                                                       }
-                                                       );
+                       //TeleporterManager teleManager = clientAPI.ModLoader.GetModSystem<TeleporterManager>();
+                       TeleporterLocation location = AccessTools.FieldRefAccess<BlockEntityTeleporter, TeleporterLocation>(tele, @"tpLocation");//TeleporterLocation tpLocation;
+
+                       if (location != null) 
+                       {
+                       StringBuilder textTarget = new StringBuilder( );
+                       textTarget.Append(" Target: [ ");
+                       textTarget.Append($" '{location.SourceName}' @ {location.SourcePos?.PrettyCoords(clientAPI)} -> '{location.TargetName}' @ {location.TargetPos?.PrettyCoords(clientAPI)}  ");//Or ABS coords?            
+                       textTarget.Append(" ]");
+                       poi.AddReplace(
+                                               new PointOfInterest {
+                                                       Name = "Teleport",
+                                                       PrettyLocation = posn.PrettyCoords(clientAPI),
+                                                       Location = posn.Copy( ),
+                                                       Notes = textTarget.ToString( ),
+                                                       Timestamp = DateTime.UtcNow,
+                                                       Destination = location.TargetPos != null ? new BlockPosJson(location.TargetPos.Copy( )) : null
+                                               }
+                                               );
                        }
                }
+               }
 
                #endregion
        }
index 842a648..5dacf09 100644 (file)
@@ -116,7 +116,7 @@ namespace Automap
             {
 
 #if DEBUG
-                CoreApi.World.Logger.VerboseDebug(" World Blocks [Count: {0}]", CoreApi.World.Blocks.Count);
+                //CoreApi.World.Logger.VerboseDebug(" World Blocks [Count: {0}]", CoreApi.World.Blocks.Count);
 #endif
                 //If Brute force won't work; use GROOT FORCE!
                 //var theBlock = ClientApi.World.BlockAccessor.GetBlock(0);
@@ -159,7 +159,7 @@ namespace Automap
                 }
 
 #if DEBUG
-                CoreApi.World.Logger.VerboseDebug("Block gaps: {0}", emptyCount);
+                //CoreApi.World.Logger.VerboseDebug("Block gaps: {0}", emptyCount);
 #endif
             }
 
index 593bdde..3481d1e 100644 (file)
@@ -399,7 +399,7 @@ namespace Automap
 
                        if (this.POIs.Count > 0)
                        {
-                               using (var poiFile = File.OpenWrite(poiPath))
+                               using (var poiFile = File.Open(poiPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                                {
                                        Serializer.Serialize<PointsOfInterest>(poiFile, this.POIs);
                                }
@@ -407,7 +407,7 @@ namespace Automap
 
                        if (this.EOIs.Count > 0)
                        {
-                               using (var eoiFile = File.OpenWrite(eoiPath))
+                               using (var eoiFile = File.Open(eoiPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                                {
                                        Serializer.Serialize<EntitiesOfInterest>(eoiFile, this.EOIs);
                                }
@@ -626,10 +626,10 @@ namespace Automap
 
                                        foreach (var blockEnt in worldChunk.BlockEntities)
                                        {
-                                               if (blockEnt.Value != null && blockEnt.Value.Block != null && BlockID_Designators.ContainsKey(blockEnt.Value.Block.BlockId))
+                                               if (blockEnt.Key != null && blockEnt.Value != null && blockEnt.Value.Block != null && BlockID_Designators.ContainsKey(blockEnt.Value.Block.BlockId))
                                                {
                                                        var designator = BlockID_Designators[blockEnt.Value.Block.BlockId];
-                                                       designator.SpecialAction(ClientAPI, POIs, blockEnt.Value.Pos.Copy(), blockEnt.Value.Block);
+                                                       designator?.SpecialAction(ClientAPI, POIs, blockEnt.Value.Pos.Copy(), blockEnt.Value.Block);
                                                }
                                        }
                                }
index 6ff2b19..b648cc8 100644 (file)
@@ -9,5 +9,5 @@
   "dependencies": { 
        "game": "1.14.10"       
   },
-  "website": "http://nowebsite.nope"
+  "website": "http://automap.osdn.io/"
 }
\ No newline at end of file