using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.IO; using System.Globalization; namespace CSVReader { public class EventCSVReader { private string completeFileName; private char separator = ';'; private long lastLength = 0; public EventCSVReader(string filename) { completeFileName = Path.Combine(".", "Assets", "CSVInput", filename); if (!File.Exists(completeFileName)) { completeFileName = "Err"; Debug.Log("Unable to find Event File " + filename); } } /// /// Reads the entire File that has been found when Constructing the class instance. No streams are left open after the use of this method. /// If instance construction was not successfull, only return null. /// /// /// Returns a field of EventObjects including all entries of the file /// public EventObject[] ReadFile() { if (completeFileName == "Err") { return null; } List lines = new List(); try { using (StreamReader fs = new StreamReader(File.Open(completeFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { if (fs.BaseStream.Length == lastLength) { return new EventObject[0]; } lastLength = fs.BaseStream.Length; string line; while ((line = fs.ReadLine()) != null) lines.Add(line); } } catch { Debug.Log("Unable to Read Lines from Event File"); return null; } EventObject[] events = new EventObject[lines.Count]; for (int i = 0; i < lines.Count; i++) { string[] elems = lines[i].Split(separator); if (elems.Length == 6) { try { events[i] = new EventObject(int.Parse(elems[0]), double.Parse(elems[1].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[2].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[3].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[4].Replace(',', '.'), CultureInfo.InvariantCulture), (EventType)int.Parse(elems[5])); } catch (Exception e) { Debug.Log("Eventfile Error \"" + e.Message + "\" at line " + i); events[i] = null; } } else if (elems.Length == 7) { try { events[i] = new EventObject(int.Parse(elems[0]), double.Parse(elems[1].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[3].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[4].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[5].Replace(',', '.'), CultureInfo.InvariantCulture), (EventType)int.Parse(elems[6])); events[i].TimestampEnd = double.Parse(elems[2].Replace(',', '.'), CultureInfo.InvariantCulture); } catch (Exception e) { Debug.Log("Eventfile Error \"" + e.Message + "\" at line " + i); events[i] = null; } } else if (elems.Length == 9) { try { events[i] = new EventObject(int.Parse(elems[0]), double.Parse(elems[1].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[2].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[3].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[4].Replace(',', '.'), CultureInfo.InvariantCulture), (EventType)int.Parse(elems[8])); events[i].setIdealViewingPosition(float.Parse(elems[5].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[6].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[7].Replace(',', '.'), CultureInfo.InvariantCulture)); } catch (Exception e) { Debug.Log("Eventfile Error \"" + e.Message + "\" at line " + i); events[i] = null; } } else if (elems.Length == 10) { try { events[i] = new EventObject(int.Parse(elems[0]), double.Parse(elems[1].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[3].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[4].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[5].Replace(',', '.'), CultureInfo.InvariantCulture), (EventType)int.Parse(elems[9])); events[i].setIdealViewingPosition(float.Parse(elems[6].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[7].Replace(',', '.'), CultureInfo.InvariantCulture), float.Parse(elems[8].Replace(',', '.'), CultureInfo.InvariantCulture)); events[i].TimestampEnd = double.Parse(elems[2].Replace(',', '.'), CultureInfo.InvariantCulture); } catch (Exception e) { Debug.Log("Eventfile Error \"" + e.Message + "\" at line " + i); events[i] = null; } } else { Debug.Log("Unclear length of line " + i); events[i] = null; } } return events; } } }