PlotFileWriter.cs 909 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using JetBrains.Annotations;
  8. using UnityEngine;
  9. namespace Plotting
  10. {
  11. public class PlotFileWriter
  12. {
  13. private readonly StreamWriter writer;
  14. private Task previousWriteTask;
  15. public PlotFileWriter(string path)
  16. {
  17. var fileInfo = new FileInfo(path);
  18. if (!fileInfo.Directory?.Exists ?? false)
  19. {
  20. fileInfo.Directory?.Create();
  21. }
  22. writer = new StreamWriter(path) {AutoFlush = true};
  23. }
  24. public async Task WriteDataLine<T>(T xValue, IEnumerable<T> values)
  25. {
  26. await writer.WriteLineAsync($"{xValue}\t{string.Join("\t", values)}");
  27. }
  28. public void Dispose()
  29. {
  30. writer?.Close();
  31. }
  32. }
  33. }