Skip to content

Commit e2d2208

Browse files
committed
fix #52
1 parent 0df932e commit e2d2208

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

Assets/UXF/Scripts/DataHandling/FileSaver.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ public override string HandleDataTable(UXFDataTable table, string experiment, st
143143
Directory.CreateDirectory(directory);
144144
string savePath = Path.Combine(directory, string.Format("{0}.csv", dataName));
145145

146-
if (verboseDebug) Debug.LogFormat("Queuing save of file: {0}", savePath);
146+
if (verboseDebug) Debug.LogFormat("Queuing save of file: {0}", savePath);
147147

148148
ManageInWorker(() => { File.WriteAllLines(savePath, lines); });
149-
return savePath;
149+
return GetRelativePath(storagePath, savePath);;
150150
}
151151

152152
public override string HandleJSONSerializableObject(List<object> serializableObject, string experiment, string ppid, int sessionNum, string dataName, UXFDataType dataType, int optionalTrialNum = 0)
@@ -163,7 +163,7 @@ public override string HandleJSONSerializableObject(List<object> serializableObj
163163
if (verboseDebug) Debug.LogFormat("Queuing save of file: {0}", savePath);
164164

165165
ManageInWorker(() => { File.WriteAllText(savePath, text); });
166-
return savePath;
166+
return GetRelativePath(storagePath, savePath);;
167167
}
168168

169169
public override string HandleJSONSerializableObject(Dictionary<string, object> serializableObject, string experiment, string ppid, int sessionNum, string dataName, UXFDataType dataType, int optionalTrialNum = 0)
@@ -177,10 +177,10 @@ public override string HandleJSONSerializableObject(Dictionary<string, object> s
177177
Directory.CreateDirectory(directory);
178178
string savePath = Path.Combine(directory, string.Format("{0}.json", dataName));
179179

180-
if (verboseDebug) Debug.LogFormat("Queuing save of file: {0}", savePath);
180+
if (verboseDebug) Debug.LogFormat("Queuing save of file: {0}", savePath);
181181

182182
ManageInWorker(() => { File.WriteAllText(savePath, text); });
183-
return savePath;
183+
return GetRelativePath(storagePath, savePath);;
184184
}
185185

186186
public override string HandleText(string text, string experiment, string ppid, int sessionNum, string dataName, UXFDataType dataType, int optionalTrialNum = 0)
@@ -192,10 +192,10 @@ public override string HandleText(string text, string experiment, string ppid, i
192192
Directory.CreateDirectory(directory);
193193
string savePath = Path.Combine(directory, string.Format("{0}.txt", dataName));
194194

195-
if (verboseDebug) Debug.LogFormat("Queuing save of file: {0}", savePath);
195+
if (verboseDebug) Debug.LogFormat("Queuing save of file: {0}", savePath);
196196

197197
ManageInWorker(() => { File.WriteAllText(savePath, text); });
198-
return savePath;
198+
return GetRelativePath(storagePath, savePath);;
199199
}
200200

201201
public override string HandleBytes(byte[] bytes, string experiment, string ppid, int sessionNum, string dataName, UXFDataType dataType, int optionalTrialNum = 0)
@@ -207,10 +207,10 @@ public override string HandleBytes(byte[] bytes, string experiment, string ppid,
207207
Directory.CreateDirectory(directory);
208208
string savePath = Path.Combine(directory, string.Format("{0}.txt", dataName));
209209

210-
if (verboseDebug) Debug.LogFormat("Queuing save of file: {0}", savePath);
210+
if (verboseDebug) Debug.LogFormat("Queuing save of file: {0}", savePath);
211211

212212
ManageInWorker(() => { File.WriteAllBytes(savePath, bytes); });
213-
return savePath;
213+
return GetRelativePath(storagePath, savePath);
214214
}
215215

216216

@@ -243,6 +243,20 @@ public override void CleanUp()
243243
bq.Enqueue(doNothing); // ensures bq breaks from foreach loop
244244
parallelThread.Join();
245245
}
246+
247+
public static string GetRelativePath(string relativeToDirectory, string path)
248+
{
249+
relativeToDirectory = Path.GetFullPath(relativeToDirectory);
250+
if (!relativeToDirectory.EndsWith("\\")) relativeToDirectory += "\\";
251+
252+
path = Path.GetFullPath(path);
253+
254+
Uri path1 = new Uri(relativeToDirectory);
255+
Uri path2 = new Uri(path);
256+
257+
Uri diff = path1.MakeRelativeUri(path2);
258+
return diff.OriginalString;
259+
}
246260
}
247261

248262

Assets/UXF/Tests/Editor/TestFileIOManager.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public void SetUp()
2323
{
2424
var gameObject = new GameObject();
2525
fileSaver = gameObject.AddComponent<FileSaver>();
26-
fileSaver.storagePath = "example_output";
2726
fileSaver.verboseDebug = true;
2827
}
2928

@@ -38,6 +37,7 @@ public void TearDown()
3837
[Test]
3938
public void WriteManyFiles()
4039
{
40+
fileSaver.storagePath = "example_output";
4141
fileSaver.SetUp();
4242

4343
// generate a large dictionary
@@ -69,14 +69,15 @@ public void WriteManyFiles()
6969
// cleanup files
7070
foreach (var fpath in fpaths)
7171
{
72-
System.IO.File.Delete(fpath);
72+
System.IO.File.Delete(Path.Combine(fileSaver.storagePath, fpath));
7373
}
7474
}
7575

7676

7777
[Test]
7878
public void EarlyExit()
7979
{
80+
fileSaver.storagePath = "example_output";
8081
fileSaver.SetUp();
8182
fileSaver.CleanUp();
8283

@@ -89,7 +90,44 @@ public void EarlyExit()
8990
fileSaver.SetUp();
9091
fileSaver.ManageInWorker(() => Debug.Log("Code enqueued after FileSaver re-opened"));
9192
fileSaver.CleanUp();
93+
}
94+
95+
[Test]
96+
public void AbsolutePath()
97+
{
98+
fileSaver.storagePath = "C:/example_output";
99+
fileSaver.SetUp();
100+
101+
string outString = fileSaver.HandleText("abc", experiment, ppid, sessionNum, "test", UXFDataType.OtherSessionData);
102+
103+
Assert.AreEqual(outString, @"fileSaver_test/test_ppid/S001/othersessiondata/test.txt");
104+
105+
fileSaver.CleanUp();
106+
}
107+
108+
[Test]
109+
public void FileSaverRelPath()
110+
{
92111

112+
Assert.AreEqual(
113+
FileSaver.GetRelativePath("C:\\base", "C:\\base\\123"),
114+
"123"
115+
);
116+
117+
Assert.AreEqual(
118+
FileSaver.GetRelativePath("base", "base\\123"),
119+
"123"
120+
);
121+
122+
Assert.AreEqual(
123+
FileSaver.GetRelativePath("base/", "base\\123"),
124+
"123"
125+
);
126+
127+
Assert.AreEqual(
128+
FileSaver.GetRelativePath("C:/base/", "C:/base\\123"),
129+
"123"
130+
);
93131
}
94132

95133
}

0 commit comments

Comments
 (0)