From fcd4b3a8d48b22ebf0c9cc37da24fbf09b9f9f89 Mon Sep 17 00:00:00 2001
From: "VKMTHD\\haraldmartini" <harald.martini@student.tugraz.at>
Date: Tue, 14 Mar 2023 17:07:06 +0100
Subject: [PATCH] write overridden files to override.txt

---
 .../Models/Declaration/LACDecisionFactor.cs   |  2 +
 .../Models/Declaration/LookupData.cs          | 53 ++++++++++++++++---
 VectoCore/VectoCore/Models/Declaration/PT1.cs |  1 +
 .../Models/Declaration/StandardBodies.cs      |  1 +
 .../VectoCore/Models/Declaration/Wheels.cs    |  1 +
 5 files changed, 52 insertions(+), 6 deletions(-)

diff --git a/VectoCore/VectoCore/Models/Declaration/LACDecisionFactor.cs b/VectoCore/VectoCore/Models/Declaration/LACDecisionFactor.cs
index c9e6ba0948..0bc4c611ef 100644
--- a/VectoCore/VectoCore/Models/Declaration/LACDecisionFactor.cs
+++ b/VectoCore/VectoCore/Models/Declaration/LACDecisionFactor.cs
@@ -84,6 +84,7 @@ namespace TUGraz.VectoCore.Models.Declaration
 
 			public override double Lookup(MeterPerSecond targetVelocity)
 			{
+				WarnReadFromFile();
 				var section = Data.GetSection(kv => kv.Key < targetVelocity);
 				return VectoMath.Interpolate(section.Item1.Key, section.Item2.Key, section.Item1.Value, section.Item2.Value,
 					targetVelocity);
@@ -132,6 +133,7 @@ namespace TUGraz.VectoCore.Models.Declaration
 
 			public override double Lookup(MeterPerSecond targetVelocity)
 			{
+				WarnReadFromFile();
 				var section = Data.GetSection(kv => kv.Key < targetVelocity);
 				return VectoMath.Interpolate(section.Item1.Key, section.Item2.Key, section.Item1.Value, section.Item2.Value,
 					targetVelocity);
diff --git a/VectoCore/VectoCore/Models/Declaration/LookupData.cs b/VectoCore/VectoCore/Models/Declaration/LookupData.cs
index b90b1ceaac..388fe361b6 100644
--- a/VectoCore/VectoCore/Models/Declaration/LookupData.cs
+++ b/VectoCore/VectoCore/Models/Declaration/LookupData.cs
@@ -35,8 +35,10 @@ using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
+using System.Diagnostics;
 using System.IO;
 using System.Linq;
+using System.Runtime.CompilerServices;
 using TUGraz.VectoCommon.Exceptions;
 using TUGraz.VectoCommon.Models;
 using TUGraz.VectoCommon.Utils;
@@ -50,6 +52,22 @@ namespace TUGraz.VectoCore.Models.Declaration
 		protected abstract string ErrorMessage { get; }
 		protected abstract void ParseData(DataTable table);
 
+#if USE_EXTERNAL_DECLARATION_DATA
+		protected bool _readFromFile = false;
+		private static bool _overrideFileDeleted = false;
+#endif
+		[Conditional("USE_EXTERNAL_DECLARATION_DATA")]
+		protected void WarnReadFromFile()
+		{
+			if (_readFromFile) {
+				//Not displayed in simulation window (don't know why? Just write to file for now)
+				//var tmp = ResourceId?.Replace(DeclarationData.DeclarationDataResourcePrefix + ".", "") ?? "unknown resource";
+				//Log.Warn(string.Format("{0} overridden", tmp));
+			}
+
+		}
+
+
 		protected LookupData()
 		{
 			ReadData();
@@ -58,24 +76,44 @@ namespace TUGraz.VectoCore.Models.Declaration
 		protected void ReadData()
 		{
 			if (!string.IsNullOrWhiteSpace(ResourceId)) {
-				var table = ReadCsvResource(ResourceId, (s) => System.Diagnostics.Debug.WriteLine(s));
+				var table = ReadCsvResource(ResourceId, (s) => {
+					System.Diagnostics.Debug.WriteLine(s);
+					Log.Warn(s);
+				});
 				NormalizeTable(table);
 				ParseData(table);
 			}
 		}
-
-		protected static DataTable ReadCsvResource(string resourceId, Action<string> overrideWarning = null)
+#if USE_EXTERNAL_DECLARATION_DATA
+		[MethodImpl(MethodImplOptions.Synchronized)]
+#endif
+		protected DataTable ReadCsvResource(string resourceId, Action<string> overrideWarning = null)
 		{
 // TODO: MQ 2020-07 Remove in official bus version!
 #if USE_EXTERNAL_DECLARATION_DATA
 			var tmp = resourceId.Replace(DeclarationData.DeclarationDataResourcePrefix + ".", "");
 			var parts = tmp.Split('.');
+			//one dir up
 			var fileName = Path.GetFullPath(Path.Combine(@"Declaration\Override", string.Join(".", parts[parts.Length-2], parts[parts.Length-1])));
+			Console.WriteLine(fileName);
+			
 			if (File.Exists(fileName)) {
 				if (overrideWarning != null) {
 					overrideWarning($"{resourceId} overridden by {fileName}");
 				}
-				return VectoCSVFile.Read(fileName);
+
+				_readFromFile = true;
+				var overrideFileName = "override.txt";
+				if (File.Exists(overrideFileName) && !_overrideFileDeleted) {
+					File.Delete(overrideFileName);
+					_overrideFileDeleted = true;
+				}
+				using (StreamWriter w = File.AppendText(overrideFileName))
+				{
+					w.WriteLine(string.Format("{0}: {1}", DateTime.Now, fileName));
+					w.Flush();
+				}
+                return VectoCSVFile.Read(fileName);
 			}
 #endif
 			return VectoCSVFile.ReadStream(RessourceHelper.ReadStream(resourceId), source: resourceId);
@@ -98,6 +136,7 @@ namespace TUGraz.VectoCore.Models.Declaration
 
 		public virtual TValue Lookup(TKey key)
 		{
+			WarnReadFromFile();
 			try {
 				return Data[key];
 			} catch (KeyNotFoundException) {
@@ -114,7 +153,8 @@ namespace TUGraz.VectoCore.Models.Declaration
 
 		public virtual TValue Lookup(TKey1 key1, TKey2 key2)
 		{
-			try {
+			WarnReadFromFile();
+            try {
 				return Data[Tuple.Create(key1, key2)];
 			} catch (KeyNotFoundException) {
 				throw new VectoException(string.Format(ErrorMessage, key1, key2));
@@ -129,7 +169,8 @@ namespace TUGraz.VectoCore.Models.Declaration
 
 		public virtual TValue Lookup(TKey1 key1, TKey2 key2, TKey3 key3)
 		{
-			try {
+			WarnReadFromFile();
+            try {
 				return Data[Tuple.Create(key1, key2, key3)];
 			} catch (KeyNotFoundException) {
 				throw new VectoException(string.Format(ErrorMessage, key1, key2, key3));
diff --git a/VectoCore/VectoCore/Models/Declaration/PT1.cs b/VectoCore/VectoCore/Models/Declaration/PT1.cs
index 427e8decf1..5ebba7c478 100644
--- a/VectoCore/VectoCore/Models/Declaration/PT1.cs
+++ b/VectoCore/VectoCore/Models/Declaration/PT1.cs
@@ -91,6 +91,7 @@ namespace TUGraz.VectoCore.Models.Declaration
 
 		public override PT1Result Lookup(PerSecond key)
 		{
+			WarnReadFromFile();
 			var extrapolated = key.IsSmaller(_entries[0].Key) || key.IsGreater(_entries.Last().Key);
 
 			var index = _entries.FindIndex(x => x.Key.IsGreater(key));
diff --git a/VectoCore/VectoCore/Models/Declaration/StandardBodies.cs b/VectoCore/VectoCore/Models/Declaration/StandardBodies.cs
index 8287ef4947..a41585e456 100644
--- a/VectoCore/VectoCore/Models/Declaration/StandardBodies.cs
+++ b/VectoCore/VectoCore/Models/Declaration/StandardBodies.cs
@@ -95,6 +95,7 @@ namespace TUGraz.VectoCore.Models.Declaration
 
 		public override StandardBody Lookup(string id)
 		{
+			WarnReadFromFile();
 			return string.IsNullOrWhiteSpace(id) ? Empty : base.Lookup(id);
 		}
 
diff --git a/VectoCore/VectoCore/Models/Declaration/Wheels.cs b/VectoCore/VectoCore/Models/Declaration/Wheels.cs
index 3ae7aae077..60502fad97 100644
--- a/VectoCore/VectoCore/Models/Declaration/Wheels.cs
+++ b/VectoCore/VectoCore/Models/Declaration/Wheels.cs
@@ -48,6 +48,7 @@ namespace TUGraz.VectoCore.Models.Declaration
 
 		public override Entry Lookup(string key)
 		{
+			WarnReadFromFile();
 			return base.Lookup(key.RemoveWhitespace());
 		}
 
-- 
GitLab