-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSVWriter.cs
120 lines (108 loc) · 3.74 KB
/
CSVWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Unity.Mathematics;
using Helpers;
[System.Serializable]
public class CSVWriter
{
public string fileName = null;
public string dirName = null;
public bool writeUnixTime = true;
public bool append_zero_to_filename = false;
public List<string> columns;
[SerializeField, ReadOnlyInsp] private string filePath;
private StreamWriter eventWriter;
private List<string> payload = new List<string>();
private bool _is_active = false;
public bool is_active => _is_active;
public bool Initialize() {
string dname = $"{Application.persistentDataPath}/{Helpers.SaveSystemMethods.GetCurrentDateTime()}";
if (dirName != null && dirName.Length > 0) {
dname = $"{Application.persistentDataPath}/{dirName}";
}
Helpers.SaveSystemMethods.CheckOrCreateDirectory(dname);
string fname = (fileName != null && fileName.Length > 0) ? fileName : System.DateTime.Now.ToString("HH-mm-ss");
filePath = (append_zero_to_filename) ? Path.Combine(dname, fname+"_0.csv") : Path.Combine(dname, fname+".csv");
int counter = 1;
while(File.Exists(filePath)) {
filePath = Path.Combine(dname, fname+$"_{counter}.csv");
counter++;
}
eventWriter = new StreamWriter(new FileStream(filePath, FileMode.Create), Encoding.UTF8);
// Header Line, if any columns are added to `columns`
if (columns.Count > 0) {
if (writeUnixTime) columns.Insert(0,"unix_ms");
eventWriter.WriteLine(String.Join(',', columns));
}
_is_active = true;
return true;
}
public bool WriteLine() {
if (payload.Count == 0) return false;
WriteLine(String.Join(",",payload));
payload = new List<string>();
return true;
}
public bool WriteLine(bool add_unix) {
if (payload.Count == 0) return false;
WriteLine(String.Join(",",payload), add_unix);
payload = new List<string>();
return true;
}
public bool WriteLine(string newLine) {
eventWriter.WriteLine(newLine);
return true;
}
public bool WriteLine(string newLine, bool add_unix) {
if (add_unix) eventWriter.WriteLine($"{GetUnixTime()},{newLine}");
else eventWriter.WriteLine(newLine);
return true;
}
public void AddPayload(string to_add) {
payload.Add(to_add);
}
public void AddPayload(int to_add) {
payload.Add(to_add.ToString());
}
public void AddPayload(float to_add) {
payload.Add(to_add.ToString());
}
public void AddPayload(long to_add) {
payload.Add($"{to_add}");
}
public void AddPayload(Vector3 to_add) {
payload.Add(to_add.x.ToString());
payload.Add(to_add.y.ToString());
payload.Add(to_add.z.ToString());
}
public void AddPayload(float3 to_add) {
payload.Add(to_add[0].ToString());
payload.Add(to_add[1].ToString());
payload.Add(to_add[2].ToString());
}
public void AddPayload(Quaternion to_add) {
AddPayload(to_add.eulerAngles);
}
public void AddPayload(List<string> to_add) {
foreach(string a in to_add) payload.Add(a);
}
public void AddPayload(string[] to_add) {
foreach(string a in to_add) payload.Add(a);
}
public static long GetUnixTime() {
DateTime currentTime = DateTime.UtcNow;
return ((DateTimeOffset)currentTime).ToUnixTimeMilliseconds();
}
public void Disable() {
if (_is_active) {
eventWriter.Flush();
eventWriter.Close();
}
_is_active = false;
}
}