feat: file metadata

This commit is contained in:
2026-03-12 11:11:34 +01:00
parent d0f54655b8
commit adaa6256cd
8 changed files with 330 additions and 60 deletions

View File

@@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using CarManagerV3.Classes;
using CarManagerV3.Manager;
namespace CarManagerV3
@@ -56,7 +58,7 @@ namespace CarManagerV3
{
Directory.CreateDirectory(directory);
}
if(!folderOnly) InitializeFile(path);
if (!folderOnly) InitializeFile(path);
}
catch (Exception ex)
{
@@ -95,6 +97,7 @@ namespace CarManagerV3
{
List<Car> cars = new List<Car>();
List<string> failedLines = new List<string>();
FileMeta meta = null;
bool isLegacy = false;
try
{
@@ -103,6 +106,27 @@ namespace CarManagerV3
string line;
while ((line = reader.ReadLine()) != null)
{
if (FileMeta.IsFileMeta(line))
{
meta = FileMeta.FromCSV(line);
if (meta != null)
{
if (FileMeta.NeedsUpdate(meta))
{
if (!StateManager.askForMigration(meta))
{
MessageBox.Show($"The file you are trying to open is from an older version that is no longer supported. Please select a different file.\nRunning:{Updater.GetCurrentVersion()}, File:{meta.Version}", "Unsupported Version", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw new LegacyException();
//Environment.Exit(0);
}
else
{
isLegacy = true;
}
}
}
continue;
}
// Process the line
if (line == "") continue;
if (Car.isLegacyCsvString(line))
@@ -116,8 +140,10 @@ namespace CarManagerV3
else
{
isLegacy = true;
line = FileUpdate.PerformOldLegacyUpdate(line);
}
}
if (isLegacy && meta != null) line = FileUpdate.ApplyUpdates(line, meta.Version);
Car tmp = Car.FromCsvString(line);
if (tmp == null)
{
@@ -166,6 +192,8 @@ namespace CarManagerV3
{
using (StreamWriter writer = new StreamWriter(@path))
{
FileMeta meta = FileMeta.Generate();
writer.WriteLine(meta.ToCSV());
foreach (Car car in cars)
{
writer.WriteLine(car.ToCsvString());
@@ -285,5 +313,31 @@ namespace CarManagerV3
}
}
public static FileMeta GetFileMeta(string path)
{
try
{
using (StreamReader reader = new StreamReader(@path))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (FileMeta.IsFileMeta(line))
{
reader.Close();
return FileMeta.FromCSV(line);
}
}
reader.Close();
}
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error getting file meta: {ex.Message}");
}
return null;
}
}
}