-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogFilePlayer.cs
More file actions
128 lines (100 loc) · 2.94 KB
/
LogFilePlayer.cs
File metadata and controls
128 lines (100 loc) · 2.94 KB
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
121
122
123
124
125
126
127
128
using UnityEngine;
using System.Collections;
using System.IO;
using System.Linq;
public class LogFilePlayer : MonoBehaviour
{
[Header("Folder Paths")]
public string logFolderPath = @"C:\Users\judit\OneDrive\Desktop\ML_agents\CSV\CSV";
public string recordingFolder = @"C:\Users\judit\Videos"; // <-- Update in Inspector to match OBS
private string[] logFiles;
private int currentFileIndex = 0;
private OBSController obs;
private PathReplayer pathReplayer;
IEnumerator Start()
{
obs = FindObjectOfType<OBSController>();
pathReplayer = FindObjectOfType<PathReplayer>();
logFiles = Directory.GetFiles(logFolderPath, "*.csv");
if (logFiles.Length == 0)
{
Debug.LogWarning("No log files found in: " + logFolderPath);
yield break;
}
if (obs != null)
{
obs.Connect();
yield return new WaitForSeconds(1f); // Give OBS time to connect
}
yield return StartCoroutine(ReplayAndRecord());
}
IEnumerator ReplayAndRecord()
{
while (currentFileIndex < logFiles.Length)
{
string logFilePath = logFiles[currentFileIndex];
string fileName = Path.GetFileNameWithoutExtension(logFilePath) + ".mkv";
Debug.Log("Starting replay: " + logFilePath);
if (obs != null)
{
obs.StartRecording(fileName);
}
if (pathReplayer != null)
{
yield return StartCoroutine(pathReplayer.LoadAndPlay(logFilePath));
}
if (obs != null)
{
obs.StopRecording();
yield return new WaitForSeconds(2f); // Give OBS time to finalize the file
// Rename based on original log filename
yield return StartCoroutine(RenameLastRecording(fileName));
}
yield return new WaitForSeconds(1f); // Optional buffer
currentFileIndex++;
}
Debug.Log("All replays finished.");
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
}
IEnumerator RenameLastRecording(string newName)
{
const int maxRetries = 10;
const float retryDelay = 0.5f;
if (!Directory.Exists(recordingFolder))
{
Debug.LogError($"Recording folder not found: {recordingFolder}");
yield break;
}
var dir = new DirectoryInfo(recordingFolder);
var lastFile = dir.GetFiles("*.mkv")
.OrderByDescending(f => f.LastWriteTime)
.FirstOrDefault();
if (lastFile == null)
{
Debug.LogWarning("No recent recording found to rename.");
yield break;
}
string newPath = Path.Combine(recordingFolder, newName);
for (int i = 0; i < maxRetries; i++)
{
try
{
if (File.Exists(newPath))
{
File.Delete(newPath);
}
File.Move(lastFile.FullName, newPath);
Debug.Log("Renamed recording to: " + newName);
yield break;
}
catch (IOException ex)
{
Debug.LogWarning($"Rename attempt {i + 1} failed: {ex.Message}");
}
yield return new WaitForSeconds(retryDelay);
}
Debug.LogError("Failed to rename recording after multiple attempts.");
}
}