/// <summary> /// Saves the specified collection. /// </summary> /// <param name="collection">The collection.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> /// <exception cref="ArgumentNullException">collection</exception> public virtual bool Save(IModCollection collection) { if (collection == null || string.IsNullOrWhiteSpace(collection.Game)) { throw new ArgumentNullException(nameof(collection)); } var game = GameService.GetSelected(); if (game == null) { return(false); } lock (serviceLock) { var collections = StorageProvider.GetModCollections().ToList(); if (collections.Count > 0) { var existing = collections.FirstOrDefault(p => p.Name.Equals(collection.Name, StringComparison.OrdinalIgnoreCase) && p.Game.Equals(collection.Game)); if (existing != null) { collections.Remove(existing); } if (collection.IsSelected) { foreach (var item in collections.Where(p => p.Game.Equals(collection.Game) && p.IsSelected)) { item.IsSelected = false; } } } collections.Add(collection); return(StorageProvider.SetModCollections(collections)); } }
public IModCollection LoadMods() { try { this.MessageService.PushMessage( $"Started loading mods from [{this.HomeSettings.ModDirectory}]...", MessageType.Info); var stopwatch = new Stopwatch(); stopwatch.Start(); this.mainModel.IsBusy = true; var modCollection = this.modService.LoadCollectionFromPath( @"C:\Games\Steam\steamapps\common\BATTLETECH", this.HomeSettings.ModDirectory, this.HomeSettings.ModCollectionName); stopwatch.Stop(); this.MessageService.PushMessage( $"Loading mods from disk took {stopwatch.ElapsedMilliseconds}ms.", MessageType.Info); this.ModCollection = modCollection; this.OnPropertyChanged(nameof(this.ModCollection)); return(this.ModCollection); } catch (Exception ex) { this.MessageService.PushMessage(ex.Message, MessageType.Error); return(null); } finally { this.mainModel.IsBusy = false; } }
/// <summary> /// Exports the asynchronous. /// </summary> /// <param name="file">The file.</param> /// <param name="modCollection">The mod collection.</param> /// <param name="exportOrderOnly">if set to <c>true</c> [export order only].</param> /// <param name="exportMods">if set to <c>true</c> [export mods].</param> /// <returns>Task<System.Boolean>.</returns> public virtual Task <bool> ExportAsync(string file, IModCollection modCollection, bool exportOrderOnly = false, bool exportMods = false) { var game = GameService.GetSelected(); if (game == null || modCollection == null) { return(Task.FromResult(false)); } var collection = Mapper.Map <IModCollection>(modCollection); if (string.IsNullOrWhiteSpace(collection.MergedFolderName) && exportMods) { collection.MergedFolderName = collection.Name.GenerateValidFileName(); } var path = GetPatchModDirectory(game, modCollection); var parameters = new ModCollectionExporterParams() { File = file, Mod = collection, ModDirectory = path, ExportModOrderOnly = exportOrderOnly }; if (exportMods) { parameters.ExportMods = GetCollectionMods(collectionName: modCollection.Name); } return(modCollectionExporter.ExportAsync(parameters)); }
/// <summary> /// Maps the import result. /// </summary> /// <param name="modCollection">The mod collection.</param> /// <param name="importResult">The import result.</param> protected virtual void MapImportResult(IModCollection modCollection, ICollectionImportResult importResult) { if (!string.IsNullOrWhiteSpace(importResult.Game)) { var collectionGame = GameService.Get().FirstOrDefault(p => p.Type.Equals(importResult.Game)); if (collectionGame == null) { collectionGame = GameService.Get().FirstOrDefault(p => p.ParadoxGameId.Equals(importResult.Game)); } if (collectionGame != null) { modCollection.Game = collectionGame.Type; } } modCollection.IsSelected = importResult.IsSelected; modCollection.MergedFolderName = importResult.MergedFolderName; modCollection.ModNames = importResult.ModNames; modCollection.Mods = importResult.Descriptors; modCollection.Name = importResult.Name; modCollection.PatchModEnabled = importResult.PatchModEnabled; if (importResult.ModIds != null && importResult.ModIds.Any()) { var mods = GetInstalledModsInternal(modCollection.Game, false); if (mods.Any()) { var collectionMods = mods.Where(p => importResult.ModIds.Contains(p.RemoteId.ToString())); modCollection.Mods = collectionMods.Select(p => p.DescriptorFile).ToList(); modCollection.ModNames = collectionMods.Select(p => p.Name).ToList(); } } }
/// <summary> /// set collection data as an asynchronous operation. /// </summary> /// <param name="modCollection">The mod collection.</param> protected async Task SetCollectionDataAsync(IModCollection modCollection) { this.modCollection = modCollection; CollectionName = modCollection.Name; IsOpenVisible = await modService.PatchModExistsAsync(modCollection.Name); IsPatchModEnabled = modCollection.PatchModEnabled; OpenClass = IsPatchModEnabled ? ActiveClass : InactiveClass; SetOpenCaption(); }
/// <summary> /// Sets the parameters. /// </summary> /// <param name="modCollection">The mod collection.</param> public virtual void SetParameters(IModCollection modCollection) { if (modCollection != null) { SetCollectionDataAsync(modCollection).ConfigureAwait(false); } else { IsOpenVisible = false; } }
/// <summary> /// Exports the asynchronous. /// </summary> /// <param name="file">The file.</param> /// <param name="modCollection">The mod collection.</param> /// <param name="exportOrderOnly">if set to <c>true</c> [export order only].</param> /// <returns>Task<System.Boolean>.</returns> public virtual Task <bool> ExportAsync(string file, IModCollection modCollection, bool exportOrderOnly = false) { var game = GameService.GetSelected(); if (game == null || modCollection == null) { return(Task.FromResult(false)); } var path = GetModDirectory(game, modCollection); return(modCollectionExporter.ExportAsync(new ModCollectionExporterParams() { File = file, Mod = modCollection, ModDirectory = path, ExportModOrderOnly = exportOrderOnly })); }
public void ProcessModCollectionLanceSlotEligibility(IModCollection modCollection) { this.logger.Info("Processing Lance Slot Eligibility..."); using (var scopedStopwatch = new ScopedStopwatch(this.logger)) { var lanceSlots = modCollection.GetReferenceableObjects() .Where(o => o.ObjectType == ObjectType.LanceDef).Cast <LanceDefObjectDefinition>() .SelectMany(definition => definition.LanceSlots); lanceSlots //.AsParallel().ForAll( .ToList().ForEach( o => { this.logger.Debug($"Processing lance slot eligibility for Lance [{o.LanceDefObjectDefinition.Id}] - Slot [{o.LanceSlotNumber}]..."); o.LoadEligibleUnitsAndPilots(modCollection); }); } }
public long ProcessModCollectionReferences(IModCollection modCollection) { var allReferences = modCollection.GetReferenceableObjects(); var sw = new Stopwatch(); sw.Start(); allReferences.AsParallel().ForAll( // allReferences.ToList().ForEach( o => { // allReferences.ForEach(o => var references = this.GetObjectReferences(o); }); sw.Stop(); return(sw.ElapsedMilliseconds); }
/// <summary> /// Exports the paradox launcher json asynchronous. /// </summary> /// <param name="file">The file.</param> /// <param name="modCollection">The mod collection.</param> /// <returns>Task<System.Boolean>.</returns> public virtual Task <bool> ExportParadoxLauncherJsonAsync(string file, IModCollection modCollection) { var game = GameService.GetSelected(); if (game == null || modCollection == null) { return(Task.FromResult(false)); } var collection = Mapper.Map <IModCollection>(modCollection); var parameters = new ModCollectionExporterParams() { File = file, Mod = collection, ExportMods = GetCollectionMods(collectionName: modCollection.Name), Game = game }; return(modCollectionExporter.ExportParadoxLauncherJsonAsync(parameters)); }
public long ProcessModCollectionReferences(IModCollection modCollection) { var allReferences = modCollection.GetReferenceableObjects(); var baseReferences = modCollection.Mods.FirstOrDefault(mod => mod.IsBattleTech)?.GetReferenceableObjects(); var sw = new Stopwatch(); sw.Start(); allReferences.AsParallel().ForAll( //allReferences.ToList().ForEach( o => { var references = this.GetObjectReferences(o, baseReferences); }); sw.Stop(); this.ProcessModCollectionLanceSlotEligibility(modCollection); return(sw.ElapsedMilliseconds); }
public ModCollectionNode( IModCollection modCollection, MtmTreeViewItem parent, IReferenceFinderService referenceFinderService, bool preProcessRelationships = false) : base(parent, modCollection, referenceFinderService) { this.ModCollection = modCollection; this.ModCollection.Mods.OrderBy(mod => mod.Name).ToList().ForEach( mod => { var modNode = new ModNode(mod, this, referenceFinderService); this.Children.Add(modNode); modNode.PropertyChanged += (sender, args) => { this.OnPropertyChanged(nameof(this.TotalMods)); this.OnPropertyChanged(nameof(this.TotalModSize)); this.OnPropertyChanged(nameof(this.TotalModObjectCount)); this.OnPropertyChanged(nameof(this.TotalMechCount)); this.OnPropertyChanged(nameof(this.TotalVehicleCount)); this.OnPropertyChanged(nameof(this.TotalTurretCount)); this.OnPropertyChanged(nameof(this.TotalWeaponCount)); this.OnPropertyChanged(nameof(this.TotalUpgradeCount)); this.OnPropertyChanged(nameof(this.TotalCoolingCount)); }; }); // Hook up interdependent properties // When Pilots/Units are selected, notify lances // TODO: Formalize this into a modeled relationship so we can refactor properly // and reuse the concept. var lanceDependencyProperties = new List <ObjectType> { ObjectType.MechDef, ObjectType.TurretDef, ObjectType.VehicleDef, ObjectType.PilotDef }; var sourceObjects = this.AllChildren.Where( item => item.Object is IObjectDefinition obj && lanceDependencyProperties.Contains(obj.ObjectType)); var targetObjects = this.AllChildren.Where(item => item is LanceDefNode obj).Cast <LanceDefNode>() .SelectMany(node => node.LanceSlots); targetObjects.AsParallel().ForAll( // targetObjects.ToList().ForEach( target => { target.LoadEligibleUnitsAndPilots(); sourceObjects.AsParallel().ForAll( source => { var propertyName = nameof(LanceSlotModel.SelectedEligibleUnits); if (((IObjectDefinition)source.Object).ObjectType == ObjectType.PilotDef) { propertyName = nameof(LanceSlotModel.SelectedEligiblePilots); } source.PropertyChanged += (sender, args) => { if (args.PropertyName == nameof(IMtmTreeViewItem.IsChecked)) { target.OnPropertyChanged(propertyName); target.OnPropertyChanged(nameof(LanceSlotModel.ObjectStatus)); } }; }); }); this.IsExpanded = true; }
/// <summary> /// Gets the mod directory. /// </summary> /// <param name="game">The game.</param> /// <param name="modCollection">The mod collection.</param> /// <returns>System.String.</returns> protected virtual string GetModDirectory(IGame game, IModCollection modCollection) { return(GetModDirectory(game, GenerateCollectionPatchName(modCollection.Name))); }
/// <summary> /// Exports the mods asynchronous. /// </summary> /// <param name="enabledMods">The mods.</param> /// <param name="regularMods">The regular mods.</param> /// <param name="modCollection">The mod collection.</param> /// <returns>Task<System.Boolean>.</returns> public virtual async Task <bool> ExportModsAsync(IReadOnlyCollection <IMod> enabledMods, IReadOnlyCollection <IMod> regularMods, IModCollection modCollection) { var game = GameService.GetSelected(); if (game == null || enabledMods == null || regularMods == null || modCollection == null) { return(false); } var allMods = GetInstalledModsInternal(game, false); var mod = GeneratePatchModDescriptor(allMods, game, GenerateCollectionPatchName(modCollection.Name)); var applyModParams = new ModWriterParameters() { OtherMods = regularMods.Where(p => !enabledMods.Any(m => m.DescriptorFile.Equals(p.DescriptorFile))).ToList(), EnabledMods = enabledMods, RootDirectory = game.UserDirectory }; if (await ModWriter.ModDirectoryExistsAsync(new ModWriterParameters() { RootDirectory = mod.FullPath })) { if (modCollection.PatchModEnabled && enabledMods.Any()) { if (await ModWriter.WriteDescriptorAsync(new ModWriterParameters() { Mod = mod, RootDirectory = game.UserDirectory, Path = mod.DescriptorFile, LockDescriptor = CheckIfModShouldBeLocked(game, mod) }, IsPatchModInternal(mod))) { applyModParams.TopPriorityMods = new List <IMod>() { mod }; Cache.Invalidate(new CacheInvalidateParameters() { Region = ModsCacheRegion, Prefix = game.Type, Keys = new List <string> { GetModsCacheKey(true), GetModsCacheKey(false) } }); } } } else { // Remove left over descriptor if (allMods.Any(p => p.Name.Equals(mod.Name))) { await DeleteDescriptorsInternalAsync(new List <IMod>() { mod }); } } return(await ModWriter.ApplyModsAsync(applyModParams)); }