private static void AddRequirementErrors(ModValidationResult result, ContentFile modFile) { //we already loaded and used XSD validation on mod.xml annd exited if it had malformed XML, so it's safe to just load it here. //However we are not sure their "expansion" attributes exist or are integers. using (var modXml = modFile.OpenRead()) { XDocument doc = XDocument.Load(modXml); var requireElements = doc.Root.Elements("require", true); //Need nullables to support left joins. List <int?> statedRequirements = new List <int?>(); List <int?> knownRequirements = result.ExpansionNumbersUsed.Select(p => new Nullable <int>(p)).ToList(); foreach (var element in requireElements) { var expansion = element.GetIntAttribute("expansion"); if (expansion != null) { statedRequirements.Add(expansion.Value); } } //distinct list in case they repeat themselves. statedRequirements = statedRequirements.Distinct().ToList(); //left join to find missing requirements. var missingRequirements = from knownRequirement in knownRequirements join statedRequirement in statedRequirements on knownRequirement equals statedRequirement into statedRequirementGroup from statedRequirement in statedRequirementGroup.DefaultIfEmpty() where statedRequirement == null select knownRequirement; result.ModErrors.AddRange( missingRequirements.Select( p => String.Format("Expansion {0} is used by your mod, but you didn't include <require expansion=\"{0}\"/> in mod.xml", p) ) ); //the opposite left join to find overstated requirements. var overstatedRequirements = from overStatedRequirement in statedRequirements join knownRequirement in knownRequirements on overStatedRequirement equals knownRequirement into knownRequirementGroup from knownRequirement in knownRequirementGroup.DefaultIfEmpty() where knownRequirement == null select overStatedRequirement; result.ModErrors.AddRange( overstatedRequirements.Select( p => String.Format("You have <require expansion=\"{0}\"/> in mod.xml, but expansion {0} is not used by your mod.", p) ) ); } }
protected XmlResourceValidator(ContentFile file, GameResources resources, GameResources expansionResourcesUsed) { this.resources = resources; this.path = file.FullName; this.expansionResourcesUsed = expansionResourcesUsed; using (Stream xmlStream = file.OpenRead()) { this.doc = XDocument.Load(xmlStream, LoadOptions.SetLineInfo); } this.isCoreGame = file.Directory.IsCoreGame; this.expansionNumber = file.ExpansionNumber; this.result = new XmlFileValidationErrorList { Path = this.path, Errors = new List <XmlValidationError>() }; }
private static void AddRequirementErrors( ModValidationResult result, ContentFile modFile ) { //we already loaded and used XSD validation on mod.xml annd exited if it had malformed XML, so it's safe to just load it here. //However we are not sure their "expansion" attributes exist or are integers. using ( var modXml = modFile.OpenRead() ) { XDocument doc = XDocument.Load( modXml ); var requireElements = doc.Root.Elements( "require", true ); //Need nullables to support left joins. List<int?> statedRequirements = new List<int?>(); List<int?> knownRequirements = result.ExpansionNumbersUsed.Select( p => new Nullable<int>( p ) ).ToList(); foreach ( var element in requireElements ) { var expansion = element.GetIntAttribute( "expansion" ); if ( expansion != null ) { statedRequirements.Add( expansion.Value ); } } //distinct list in case they repeat themselves. statedRequirements = statedRequirements.Distinct().ToList(); //left join to find missing requirements. var missingRequirements = from knownRequirement in knownRequirements join statedRequirement in statedRequirements on knownRequirement equals statedRequirement into statedRequirementGroup from statedRequirement in statedRequirementGroup.DefaultIfEmpty() where statedRequirement == null select knownRequirement; result.ModErrors.AddRange( missingRequirements.Select( p => String.Format( "Expansion {0} is used by your mod, but you didn't include <require expansion=\"{0}\"/> in mod.xml", p ) ) ); //the opposite left join to find overstated requirements. var overstatedRequirements = from overStatedRequirement in statedRequirements join knownRequirement in knownRequirements on overStatedRequirement equals knownRequirement into knownRequirementGroup from knownRequirement in knownRequirementGroup.DefaultIfEmpty() where knownRequirement == null select overStatedRequirement; result.ModErrors.AddRange( overstatedRequirements.Select( p => String.Format( "You have <require expansion=\"{0}\"/> in mod.xml, but expansion {0} is not used by your mod.", p ) ) ); } }
protected XmlResourceValidator( ContentFile file, GameResources resources, GameResources expansionResourcesUsed ) { this.resources = resources; this.path = file.FullName; this.expansionResourcesUsed = expansionResourcesUsed; using ( Stream xmlStream = file.OpenRead() ) { this.doc = XDocument.Load( xmlStream, LoadOptions.SetLineInfo ); } this.isCoreGame = file.Directory.IsCoreGame; this.expansionNumber = file.ExpansionNumber; this.result = new XmlFileValidationErrorList { Path = this.path, Errors = new List<XmlValidationError>() }; }