152 lines
No EOL
5.6 KiB
C#
152 lines
No EOL
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using JD.LogService;
|
|
using TeaBox;
|
|
using File = System.IO.File;
|
|
|
|
namespace JD.Timeline.View.SnipeView
|
|
{
|
|
/// <summary>
|
|
/// This Class handles Loading and Saving of the csv files for sniping data
|
|
/// Csv files are stored per move with sniping info for that particular move.
|
|
/// </summary>
|
|
public class SnipingDataFilesProcessor
|
|
{
|
|
/// <summary>
|
|
/// Gives Snipe info for all the moves and examples listed in each csv file in the array
|
|
/// </summary>
|
|
/// <param name="csvFiles"></param>
|
|
/// <param name="logger"></param>
|
|
/// <returns></returns>
|
|
public static List<MoveSnipingData> CreateSnipingDatabaseFromAllCsvFiles(string[] csvFiles, ILogger logger)
|
|
{
|
|
var moveSnipingDatabase = new List<MoveSnipingData>();
|
|
foreach (var csvFileName in csvFiles)
|
|
{
|
|
var csvFileNameOnly = Path.GetFileNameWithoutExtension(csvFileName);
|
|
|
|
if (csvFileNameOnly == null)
|
|
throw new Exception("csvFileName was null.");
|
|
|
|
var literals = csvFileNameOnly.Split('.');
|
|
|
|
if (literals.Length != 2)
|
|
{
|
|
throw new NotImplementedException("Invalid CSV File Name.");
|
|
}
|
|
|
|
string moveName = literals[0];
|
|
|
|
MoveSnipingData moveSnipeData = LoadSnipingInfoFromFile(moveName, Path.GetFullPath(csvFileName));
|
|
|
|
if (!moveSnipingDatabase.Contains(moveSnipeData))
|
|
{
|
|
moveSnipingDatabase.Add(moveSnipeData);
|
|
}
|
|
else
|
|
{
|
|
logger.LogError("Move already added in database! Duplicate found.");
|
|
}
|
|
}
|
|
|
|
return moveSnipingDatabase;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Saves sniping information in given csv file (Location stored in the info only)
|
|
/// </summary>
|
|
/// <param name="snipeData"></param>
|
|
/// <param name="logger"></param>
|
|
/// <returns></returns>
|
|
public static bool SaveSnipeDataToCsvFile(MoveSnipingData snipeData, ILogger logger)
|
|
{
|
|
var fileLocation = snipeData.FileLocation;
|
|
|
|
if (File.Exists(fileLocation))
|
|
{
|
|
var cloneFileName = $"{Path.GetFileNameWithoutExtension(fileLocation)}-original.csv";
|
|
File.Copy(fileLocation, cloneFileName, true);
|
|
File.Delete(fileLocation);
|
|
}
|
|
|
|
try
|
|
{
|
|
StreamWriter writer = new StreamWriter(fileLocation, false);
|
|
|
|
foreach (var metaDataLine in snipeData.MetaDataReadFromFile)
|
|
writer.WriteLine(metaDataLine);
|
|
|
|
foreach (var snipeItem in snipeData.TaggedExamplesList)
|
|
{
|
|
string snipeLabel = snipeItem.SnipeLabel.GetLabelString();
|
|
|
|
var lineToWrite = $"{snipeItem.MoveName},{snipeItem.MoveName}\\{snipeItem.FileNameOnly}.e,{snipeLabel}";
|
|
|
|
writer.WriteLine(lineToWrite);
|
|
}
|
|
|
|
logger.LogMessage("Saved Edited Sniping Data for " + snipeData.MoveName);
|
|
|
|
writer.Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError("Error Saving File : " + snipeData.FileLocation + " Error : " + ex.Message);
|
|
File.Copy($"{Path.GetFileNameWithoutExtension(fileLocation)}-original.csv", fileLocation);
|
|
}
|
|
|
|
File.Delete($"{Path.GetFileNameWithoutExtension(fileLocation)}-original.csv");
|
|
|
|
return true;
|
|
}
|
|
|
|
#region Private Methods
|
|
|
|
private static MoveSnipingData LoadSnipingInfoFromFile(string moveName, string fileName)
|
|
{
|
|
MoveSnipingData moveSnipeData = new MoveSnipingData(moveName, fileName);
|
|
|
|
using (var reader = new StreamReader(fileName))
|
|
{
|
|
while (!reader.EndOfStream)
|
|
{
|
|
var readLine = reader.ReadLine();
|
|
if (readLine == null)
|
|
continue;
|
|
|
|
var values = readLine.Split(',');
|
|
|
|
if (values[0].ToLower() == "example meta data" || values[0].ToLower() == "move name")
|
|
{
|
|
moveSnipeData.MetaDataReadFromFile.Add(readLine);
|
|
continue;
|
|
}
|
|
string snipeInfoMoveName = values[0];
|
|
string moveSessionNameWithExtension = values[1].Split('\\')[1];
|
|
string moveSessionName = moveSessionNameWithExtension.Split('.')[0];
|
|
string snipeLabel = values[2];
|
|
|
|
switch (snipeLabel)
|
|
{
|
|
case "Positive":
|
|
moveSnipeData.TaggedExamplesList.Add(new SnipeInfo(snipeInfoMoveName, moveSessionName, SnipeLabel.Positive));
|
|
break;
|
|
case "Negative":
|
|
moveSnipeData.TaggedExamplesList.Add(new SnipeInfo(snipeInfoMoveName, moveSessionName, SnipeLabel.Negative));
|
|
break;
|
|
case "Ignore":
|
|
moveSnipeData.TaggedExamplesList.Add(new SnipeInfo(snipeInfoMoveName, moveSessionName, SnipeLabel.Ignored));
|
|
break;
|
|
default:
|
|
throw new NotImplementedException(snipeLabel);
|
|
}
|
|
}
|
|
}
|
|
|
|
return moveSnipeData;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |