/// <summary> /// File loading coroutine, loads all relevant files into memory /// </summary> /// <param name="files">Specific files to load</param> /// <returns>Enumerator returns loading instructions (continue/break out)</returns> IEnumerator <LoaderInstruction> ILoader.LoadAll(IList <FileInfo> files) { if (this.Loaded) { yield break; } List <T> objects = new List <T>(); Dictionary <string, T[]> paths = new Dictionary <string, T[]>(); this.current = -1; LoaderInstruction inst = CONTINUE; foreach (FileInfo file in files) { this.current++; this.status = $"[{this.Name}]: Loading {file.FullName}"; string data; try { data = file.ReadFile(); } catch (Exception e) { Debug.LogError($"[{this.Name}]: Encountered an exception reading file {file.FullName}.\n{e.GetType().Name}\n{e.StackTrace}"); data = null; inst = BREAK; } yield return(inst); T[] array; try { array = JsonConvert.DeserializeObject <T[]>(data); } catch (Exception e) { Debug.LogError($"[{this.Name}JsonLoader]: Encountered an exception loading object {file.FullName}.\n{e.GetType().Name}\n{e.StackTrace}"); array = null; inst = BREAK; } yield return(inst); paths.Add(file.GetLocalPath(), array); objects.AddRange(array); } this.LoadedObjects = new JsonLoaderList <T>(objects, paths); this.Loaded = true; }
/// <summary> /// File loading coroutine, loads all relevant files into memory /// </summary> /// <param name="files">Specific files to load</param> /// <returns>Enumerator returns loading instructions (continue/break out)</returns> IEnumerator <LoaderInstruction> ILoader.LoadAll(IList <FileInfo> files) { if (this.Loaded) { yield break; } List <T> objects = new List <T>(); Dictionary <string, T> paths = new Dictionary <string, T>(); this.current = -1; LoaderInstruction inst = CONTINUE; foreach (FileInfo file in files) { this.current++; this.status = $"[{this.Name}]: Loading {file.FullName}"; T obj; try { obj = LoadObject(file); } catch (Exception e) { Debug.LogError($"[{this.Name}]: Encountered an exception loading file {file.FullName}.\n{e.GetType().Name}\n{e.StackTrace}"); obj = default(T); inst = BREAK; } yield return(inst); if (obj != null) { paths.Add(file.GetLocalPath(), obj); objects.Add(obj); } else { Debug.LogWarning($"[{this.Name}]: File {file.FullName} loaded a null object"); } } this.LoadedObjects = new LoaderList <T>(objects, paths); this.Loaded = true; }