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;
}
}
}

View File

@@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using CarManagerV3.Classes;
using CarManagerV3.Manager;
namespace CarManagerV3
{
@@ -139,13 +141,19 @@ namespace CarManagerV3
/// This is to prevent multiple annoying popups if the user tries to open multiple files that require migration.
/// </summary>
/// <returns>True if the user has accepted to migrate the file, otherwise False</returns>
public static bool askForMigration()
public static bool askForMigration(FileMeta meta = null)
{
if (hasConfirmedMigration)
{
return true;
}
DialogResult result = MessageBox.Show("The file you are trying to open is in an older format. Do you want to attempt to migrate it to the new format? If you choose not to migrate, the file will not be opened.", "Migration Needed", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
string promptMessage = "The file you are trying to open is in an older format. Do you want to attempt to migrate it to the new format? If you choose not to migrate, the file will not be opened.";
if(meta != null)
{
int nUpdatesNeeded = FileUpdate.GetRequiredUpdates(meta.Version).Count;
promptMessage = $"The file you are trying to open is in an older format ({meta.Version}). It requires {nUpdatesNeeded} update(s) to be migrated to your Version ({Updater.GetCurrentVersion(true)}).\nDo you want to attempt to migrate it? If you choose not to migrate, the file will not be opened.";
}
DialogResult result = MessageBox.Show(promptMessage, "Migration Needed", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
hasConfirmedMigration = result == DialogResult.Yes;
return hasConfirmedMigration;
}