using System; using System.Collections.Generic; using System.Threading; namespace NScript.Install { public class Installer { private List _toInstall; private bool _isRunning = false; private bool _isCompleted = false; public bool Completed => _isCompleted; private string _currentItemName = null; public string CurrentlyInstalling => _currentItemName; private Thread _worker = null; public Installer() { _toInstall = new List(); } public Installer(IEnumerable toInstall) { _toInstall = new List(toInstall); } public void AddInstallable(IInstallable i) { if (_isRunning) throw new Exception("Cannot add IInstallable when already installing"); _toInstall.Add(i); } public void InstallEverything() { if (_worker != null) _worker.Join(); _worker = new Thread(InstallWorker); _worker.Start(); } public void WaitForCompletion() { if (_worker != null) _worker.Join(); _worker = null; } private void InstallWorker() { _isRunning = true; foreach (var i in _toInstall) { _currentItemName = i.Name; if (!i.IsInstalled()) { try { i.Install(); } catch (Exception e) { GamecraftModdingAPI.Utility.Logging.LogWarning( $"Failed to install {_currentItemName}: {e.Message}"); } } } _isRunning = false; _isCompleted = true; _currentItemName = null; } } }