/// <inheritdoc/> public void SafetyCheck() { Plex.Objects.Logger.Log("Updating drive database..."); var noEntityDrives = _drives.Find(x => _entityBackend.GetEntity(x.EntityId) == null); foreach (var drive in noEntityDrives) { Plex.Objects.Logger.Log($"Removing drive: //{drive.EntityId}/{drive.Mountpoint}. Entity not found."); var mount = _mounts.FirstOrDefault(x => x.DriveNumber == drive.Mountpoint && x.VolumeLabel == drive.VolumeLabel && x.SessionID == drive.EntityId); if (mount != null) { mount.Dispose(); _mounts.Remove(mount); } File.Delete(Path.Combine(_drivePath, drive.ImagePath)); _drives.Delete(x => x.Id == drive.Id); } Plex.Objects.Logger.Log("Mounting newly-created drives..."); foreach (var drive in _drives.Find(x => _mounts.FirstOrDefault(y => y.DriveNumber == x.Mountpoint && y.SessionID == x.EntityId && y.VolumeLabel == x.VolumeLabel) == null)) { Plex.Objects.Logger.Log($"Mounting {drive.ImagePath} to //{drive.EntityId}/{drive.Mountpoint}..."); var fat = new PlexFATDriveMount(new MountInformation { DriveNumber = drive.Mountpoint, ImageFilePath = Path.Combine(_drivePath, drive.ImagePath), Specification = DriveSpec.PlexFAT, VolumeLabel = drive.VolumeLabel }, drive.EntityId); fat.EnsureDriveExistence(); _mounts.Add(fat); } }
/// <inheritdoc/> public void Initiate() { _drivePath = Path.Combine(_backend.RootDirectory, "drives"); if (!System.IO.Directory.Exists(_drivePath)) { Plex.Objects.Logger.Log("Creating drive directory..."); System.IO.Directory.CreateDirectory(_drivePath); Plex.Objects.Logger.Log("Done."); } Plex.Objects.Logger.Log("Loading and mounting entity drives..."); this._drives = _database.Database.GetCollection <EntityMount>("entity_drives"); _drives.EnsureIndex(x => x.Id); var noFSCount = _drives.Delete(x => !File.Exists(Path.Combine(_drivePath, x.ImagePath))); var noEntityDrives = _drives.Find(x => _entityBackend.GetEntity(x.EntityId) == null); foreach (var drive in noEntityDrives) { Plex.Objects.Logger.Log($"Removing drive: //{drive.EntityId}/{drive.Mountpoint}. Entity not found."); File.Delete(Path.Combine(_drivePath, drive.ImagePath)); _drives.Delete(x => x.Id == drive.Id); } Plex.Objects.Logger.Log($"{noFSCount} drives deleted from database due to missing PlexFAT images."); Plex.Objects.Logger.Log($"{noEntityDrives.Count()} drives deleted from database due to missing NPC or player entities."); Plex.Objects.Logger.Log($"{_drives.Count()} drives loaded from database. Mounting..."); foreach (var drive in _drives.FindAll()) { Plex.Objects.Logger.Log($"Mounting {drive.ImagePath} to //{drive.EntityId}/{drive.Mountpoint}..."); var fat = new PlexFATDriveMount(new MountInformation { DriveNumber = drive.Mountpoint, ImageFilePath = Path.Combine(_drivePath, drive.ImagePath), Specification = DriveSpec.PlexFAT, VolumeLabel = drive.VolumeLabel }, drive.EntityId); fat.EnsureDriveExistence(); _mounts.Add(fat); } Plex.Objects.Logger.Log("Done loading filesystems..."); _entityBackend.EntitySpawned += (id, entity) => { //Create a drive for the entity if they don't have one. if (CreateFS(id, 0, "Peacegate OS")) { Plex.Objects.Logger.Log($"Created new 'Peacegate OS' drive at //{id}/0."); } }; _backend.PlayerJoined += (id, player) => { if (CreateFS(_entityBackend.GetPlayerEntityId(id), 0, "Peacegate OS")) { Plex.Objects.Logger.Log($"Created new 'Peacegate OS' drive at //{id}/0."); } }; }