public static string GetProcedurename(ScenarioElementType typeChanged, string elementValue, string prefixGiven, string prefixWhen, string prefixThen) { TextInfo info = CultureInfo.CurrentCulture.TextInfo; string elementValueTitleCase = info.ToTitleCase(elementValue); elementValueTitleCase = Regex.Replace(elementValueTitleCase, @"[^\w]", ""); string prefix; switch (typeChanged) { case ScenarioElementType.GIVEN: prefix = prefixGiven; break; case ScenarioElementType.WHEN: prefix = prefixWhen; break; case ScenarioElementType.THEN: prefix = prefixThen; break; default: throw new Exception("Expected a new value for Given, When or Then."); } return(string.Format("{0}{1}", prefix, elementValueTitleCase)); }
/// <summary> /// Loads the costs data of a scenario element type from the given element. /// </summary> /// <param name="costsDataElem">The XML element to load from.</param> /// <param name="elementType">The scenario element type being constructed.</param> private static void LoadCostsData(XElement costsDataElem, ScenarioElementType elementType) { XElement buildTimeElem = costsDataElem.Element(XmlMetadataConstants.COSTS_BUILDTIME_ELEM); XElement supplyUsedElem = costsDataElem.Element(XmlMetadataConstants.COSTS_SUPPLYUSED_ELEM); XElement supplyProvidedElem = costsDataElem.Element(XmlMetadataConstants.COSTS_SUPPLYPROVIDED_ELEM); XElement mineralCostElem = costsDataElem.Element(XmlMetadataConstants.COSTS_MINERALCOST_ELEM); XElement gasCostElem = costsDataElem.Element(XmlMetadataConstants.COSTS_GASCOST_ELEM); if (buildTimeElem != null) { elementType.SetBuildTime(XmlHelper.LoadInt(buildTimeElem.Value)); } if (supplyUsedElem != null) { elementType.SetSupplyUsed(XmlHelper.LoadInt(supplyUsedElem.Value)); } if (supplyProvidedElem != null) { elementType.SetSupplyProvided(XmlHelper.LoadInt(supplyProvidedElem.Value)); } if (mineralCostElem != null) { elementType.SetMineralCost(XmlHelper.LoadInt(mineralCostElem.Value)); } if (gasCostElem != null) { elementType.SetGasCost(XmlHelper.LoadInt(gasCostElem.Value)); } }
/// <summary> /// Loads the general data of a scenario element type from the given element. /// </summary> /// <param name="genDataElem">The XML element to load from.</param> /// <param name="elementType">The scenario element type being constructed.</param> private static void LoadGeneralData(XElement genDataElem, ScenarioElementType elementType) { XElement areaElem = genDataElem.Element(XmlMetadataConstants.GENDATA_AREA_ELEM); XElement obstacleAreaElem = genDataElem.Element(XmlMetadataConstants.GENDATA_OBSTACLEAREA_ELEM); XElement armorElem = genDataElem.Element(XmlMetadataConstants.GENDATA_ARMOR_ELEM); XElement maxEnergyElem = genDataElem.Element(XmlMetadataConstants.GENDATA_MAXENERGY_ELEM); XElement maxHPElem = genDataElem.Element(XmlMetadataConstants.GENDATA_MAXHP_ELEM); XElement sightRangeElem = genDataElem.Element(XmlMetadataConstants.GENDATA_SIGHTRANGE_ELEM); XElement sizeElem = genDataElem.Element(XmlMetadataConstants.GENDATA_SIZE_ELEM); XElement speedElem = genDataElem.Element(XmlMetadataConstants.GENDATA_SPEED_ELEM); if (areaElem != null) { elementType.SetArea(XmlHelper.LoadNumRectangle(areaElem.Value)); } if (obstacleAreaElem != null) { elementType.SetObstacleArea(XmlHelper.LoadIntRectangle(obstacleAreaElem.Value)); } if (armorElem != null) { elementType.SetArmor(XmlHelper.LoadInt(armorElem.Value)); } if (maxEnergyElem != null) { elementType.SetMaxEnergy(XmlHelper.LoadInt(maxEnergyElem.Value)); } if (maxHPElem != null) { elementType.SetMaxHP(XmlHelper.LoadInt(maxHPElem.Value)); } if (sightRangeElem != null) { elementType.SetSightRange(XmlHelper.LoadInt(sightRangeElem.Value)); } if (speedElem != null) { elementType.SetSpeed(XmlHelper.LoadNum(speedElem.Value)); } if (sizeElem != null) { SizeEnum size; if (!EnumMap <SizeEnum, string> .TryDemap(sizeElem.Value, out size)) { throw new SimulatorException(string.Format("Unexpected size '{0}' defined in general data!", sizeElem.Value)); } elementType.SetSize(size); } }
/// <summary> /// Loads the shadow data of a scenario element type from the given element. /// </summary> /// <param name="shadowDataElem">The XML element to load from.</param> /// <param name="elementType">The scenario element type being constructed.</param> private static void LoadShadowData(XElement shadowDataElem, ScenarioElementType elementType) { XElement spriteNameElem = shadowDataElem.Element(XmlMetadataConstants.SHADOWDATA_SPRITENAME_ELEM); XElement offsetElem = shadowDataElem.Element(XmlMetadataConstants.SHADOWDATA_OFFSET_ELEM); if (spriteNameElem == null) { throw new SimulatorException("Shadow sprite not defined!"); } if (offsetElem == null) { throw new SimulatorException("Shadow offset not defined!"); } elementType.SetShadowData(spriteNameElem.Value, XmlHelper.LoadNumVector(offsetElem.Value)); }
public bool checkSaveChanges(MessageUpdate msg, Configurations config) { bool procedureCanBeRemovedAfterwards = false; if (msg.State == MessageState.Deleted) { ScenarioElementType scenarioElementType = Library.ALMethodHelper.ToEnum <ScenarioElementType>(msg.Type.ToString().ToUpper()); string procedureNameOfElement = Library.ALMethodHelper.GetProcedurename(scenarioElementType, msg.NewValue, config.prefixGiven, config.prefixWhen, config.prefixThen); List <string> fileContent = File.ReadAllLines(msg.FsPath).ToList(); Range? range = Library.Helpers.ALTestCodeunitHelper.FindProcedureDeclarationRange(fileContent, procedureNameOfElement); if (range.HasValue) { int[] usagesOfProcedure = Library.Helpers.ALTestCodeunitHelper.FindLinesWhereProcedureIsCalled(fileContent, procedureNameOfElement); procedureCanBeRemovedAfterwards = usagesOfProcedure.Length <= 1; } } return(procedureCanBeRemovedAfterwards); }
/// <see cref="IComponent.Start"/> public void Start() { /// Load the simulation metadata files from the configured directory DirectoryInfo rootDir = new DirectoryInfo(Constants.METADATA_DIR); this.metadata = new ScenarioMetadata(); if (rootDir.Exists) { FileInfo[] metadataFiles = rootDir.GetFiles("*.xml", SearchOption.AllDirectories); foreach (FileInfo metadataFile in metadataFiles) { /// TODO: this is a hack! Later we will have binary metadata format. string xmlStr = File.ReadAllText(metadataFile.FullName); string imageDir = metadataFile.DirectoryName; XmlMetadataReader.Read(xmlStr, imageDir, this.metadata); } } /// Register the entity constraints to the corresponding entity types. foreach (KeyValuePair <string, List <EntityPlacementConstraint> > item in this.entityConstraints) { ScenarioElementType elementType = this.metadata.GetElementTypeImpl(item.Key); foreach (EntityPlacementConstraint constraint in item.Value) { elementType.AddPlacementConstraint(constraint); } } /// Register the suggestion providers to the corresponding building types. foreach (KeyValuePair <string, List <BuildingPlacementSuggestionProvider> > item in this.suggestionProviders) { BuildingType buildingType = this.metadata.GetBuildingTypeImpl(item.Key); foreach (BuildingPlacementSuggestionProvider provider in item.Value) { buildingType.AddPlacementSuggestionProvider(provider); } } /// Finalize the metadata. this.metadata.CheckAndFinalize(); }
/// <summary> /// Loads the necessary data of a scenario element type. /// </summary> /// <param name="elementTypeElem">The XML node to load from.</param> /// <param name="elementType">The scenario element type being constructed.</param> /// <param name="metadata">The metadata object.</param> private static void LoadScenarioElementType(XElement elementTypeElem, ScenarioElementType elementType, ScenarioMetadata metadata) { /// Load the displayed name of the element type. XAttribute displayedNameAttr = elementTypeElem.Attribute(XmlMetadataConstants.TYPE_DISPLAYEDNAME_ATTR); if (displayedNameAttr != null) { elementType.SetDisplayedName(displayedNameAttr.Value); } /// Load the has owner flag of the element type. XAttribute hasOwnerAttr = elementTypeElem.Attribute(XmlMetadataConstants.TYPE_HASOWNER_ATTR); elementType.SetHasOwner(hasOwnerAttr != null && XmlHelper.LoadBool(hasOwnerAttr.Value)); /// Load the sprite palette of the element type. XElement spritePaletteElem = elementTypeElem.Element(XmlMetadataConstants.SPRITEPALETTE_ELEM); ISpritePalette <MapDirection> spritePalette = null; if (spritePaletteElem != null) { spritePalette = XmlHelper.LoadSpritePalette(spritePaletteElem, MapDirection.Undefined, tmpImageDir); elementType.SetSpritePalette(spritePalette); } /// Load the HP indicator icon palette of this element type. XElement hpIconPaletteElem = elementTypeElem.Element(XmlMetadataConstants.HPICONPALETTE_ELEM); if (hpIconPaletteElem != null) { ISpritePalette hpIconPalette = XmlHelper.LoadSpritePalette(hpIconPaletteElem, tmpImageDir); elementType.SetHPIconPalette(hpIconPalette); } /// Load the animation palette of the element type. XElement animPaletteElem = elementTypeElem.Element(XmlMetadataConstants.ANIMPALETTE_ELEM); if (animPaletteElem != null) { if (spritePalette == null) { throw new SimulatorException("Animation palette definition requires a sprite palette definition!"); } elementType.SetAnimationPalette(LoadAnimationPalette(animPaletteElem, spritePalette, metadata)); } /// Load the cost data of the element type. XElement costsDataElem = elementTypeElem.Element(XmlMetadataConstants.COSTS_ELEM); if (costsDataElem != null) { LoadCostsData(costsDataElem, elementType); } /// Load the general data of the element type. XElement genDataElem = elementTypeElem.Element(XmlMetadataConstants.GENERALDATA_ELEM); if (genDataElem != null) { LoadGeneralData(genDataElem, elementType); } /// Load the shadow data of the element type. XElement shadowDataElem = elementTypeElem.Element(XmlMetadataConstants.SHADOWDATA_ELEM); if (shadowDataElem != null) { LoadShadowData(shadowDataElem, elementType); } /// Load the ground weapon definitions of the element type. foreach (XElement gndWeaponElem in elementTypeElem.Elements(XmlMetadataConstants.GROUNDWEAPON_ELEM)) { elementType.AddWeapon(LoadWeaponData(gndWeaponElem, metadata)); } /// Load the air weapon definitions of the element type. foreach (XElement airWeaponElem in elementTypeElem.Elements(XmlMetadataConstants.AIRWEAPON_ELEM)) { elementType.AddWeapon(LoadWeaponData(airWeaponElem, metadata)); } /// Load the air-ground weapon definitions of the element type. foreach (XElement airGroundWeaponElem in elementTypeElem.Elements(XmlMetadataConstants.AIRGROUNDWEAPON_ELEM)) { elementType.AddWeapon(LoadWeaponData(airGroundWeaponElem, metadata)); } /// Load the custom weapon definitions of the element type. foreach (XElement customWeaponElem in elementTypeElem.Elements(XmlMetadataConstants.CUSTOMWEAPON_ELEM)) { elementType.AddWeapon(LoadWeaponData(customWeaponElem, metadata)); } /// Load the requirements of the element type. XElement requiresElem = elementTypeElem.Element(XmlMetadataConstants.REQUIRES_ELEM); if (requiresElem != null) { string reqListStr = requiresElem.Value.Trim(); string[] requirementStrings = reqListStr.Split(','); foreach (string reqStr in requirementStrings) { Tuple <string, string> buildingAddonPair = ParseBuildingAddonStr(reqStr); elementType.AddRequirement(new Requirement(buildingAddonPair.Item1, buildingAddonPair.Item2, metadata)); } } }
/// <summary> /// Reads metadata from the given XML document and loads it to the given metadata object. /// </summary> /// <param name="xmlStr">The string that contains the XML document to read.</param> /// <param name="imageDir">The directory where the referenced images can be found. (TODO: this is a hack!)</param> /// <param name="metadata">Reference to the metadata object being constructed.</param> public static void Read(string xmlStr, string imageDir, ScenarioMetadata metadata) { if (xmlStr == null) { throw new ArgumentNullException("xmlStr"); } if (imageDir == null) { throw new ArgumentNullException("imageDir"); } if (metadata == null) { throw new ArgumentNullException("metadata"); } tmpImageDir = imageDir; /// Load the XML document. XDocument xmlDoc = XDocument.Parse(xmlStr); /// Load the scenario element type definitions. foreach (XElement metadataElement in xmlDoc.Root.Elements()) { if (metadataElement.Name == XmlMetadataConstants.BUILDINGTYPE_ELEM) { LoadBuildingType(metadataElement, metadata); } else if (metadataElement.Name == XmlMetadataConstants.UNITTYPE_ELEM) { LoadUnitType(metadataElement, metadata); } else if (metadataElement.Name == XmlMetadataConstants.ADDONTYPE_ELEM) { LoadAddonType(metadataElement, metadata); } else if (metadataElement.Name == XmlMetadataConstants.UPGRADETYPE_ELEM) { LoadUpgradeType(metadataElement, metadata); } else if (metadataElement.Name == XmlMetadataConstants.MISSILETYPE_ELEM) { LoadMissileType(metadataElement, metadata); } else if (metadataElement.Name == XmlMetadataConstants.CUSTOMTYPE_ELEM) { XAttribute nameAttr = metadataElement.Attribute(XmlMetadataConstants.TYPE_NAME_ATTR); if (nameAttr == null) { throw new SimulatorException("Custom type name not defined!"); } ScenarioElementType elementType = new ScenarioElementType(nameAttr.Value, metadata); LoadScenarioElementType(metadataElement, elementType, metadata); metadata.AddCustomType(elementType); } else if (metadataElement.Name == XmlMetadataConstants.SHADOWPALETTE_ELEM) { metadata.SetShadowPalette(XmlHelper.LoadSpritePalette(metadataElement, imageDir)); } } }
private void InitializeSaveChanges(MessageUpdate msg, Configurations config, out TestALMethod testALMethod, out int scenarioLine, out int elementLine, bool elementOfNewValue, out List <string> fileContent, out ScenarioElementType scenarioElementType, out string oldProcedureNameOfElement, out string newProcedureNameOfElement) { string scenarioName = msg.Scenario; string fsPath = msg.FsPath; string elementOldValue = msg.OldValue; string elementNewValue = msg.NewValue; TypeChanged typeChanged = msg.Type; ALTestCodeunitReader alTestCodeunitReader = new ALTestCodeunitReader(); TestALCodeunit testCodeunit = (TestALCodeunit)alTestCodeunitReader.ReadSingle(fsPath); testALMethod = testCodeunit.Methods.First(m => (m.Scenario != null) && (m.Scenario.Name == scenarioName)); scenarioElementType = Library.ALMethodHelper.ToEnum <ScenarioElementType>(typeChanged.ToString().ToUpper()); if (elementOldValue == null) { oldProcedureNameOfElement = ""; } else { oldProcedureNameOfElement = Library.ALMethodHelper.GetProcedurename(scenarioElementType, elementOldValue, config.prefixGiven, config.prefixWhen, config.prefixThen); } if (elementNewValue == null) { newProcedureNameOfElement = ""; } else { newProcedureNameOfElement = Library.ALMethodHelper.GetProcedurename(scenarioElementType, elementNewValue, config.prefixGiven, config.prefixWhen, config.prefixThen); } fileContent = File.ReadAllLines(fsPath).ToList(); elementLine = 0; scenarioLine = Library.Helpers.ALTestCodeunitHelper.FindScenarioLine(testALMethod.Scenario.Name, fileContent); if (scenarioLine == 0) { return; } if (elementOfNewValue) { elementLine = Library.Helpers.ALTestCodeunitHelper.FindElementLine(fileContent, scenarioElementType, elementNewValue, scenarioLine); } else { elementLine = Library.Helpers.ALTestCodeunitHelper.FindElementLine(fileContent, scenarioElementType, elementOldValue, scenarioLine); } if (elementLine == 0) { return; } }
public static int FindElementLine(List <string> fileContent, ScenarioElementType scenarioElementType, string elementValue, int startingAtLine = 0) { return(FindLineUsingRegexPattern(fileContent, string.Format(@"\s*\[\s*{0}\s*\]\s*{1}", scenarioElementType.ToString(), elementValue), true, startingAtLine)); }
public static int FindLineToInsertElement(TestALMethod testALMethod, List <string> fileContent, int scenarioLine, ScenarioElementType elementToAdd) { int lineToInsert = 0; IEnumerable <ITestScenarioElement> elementsOfType = testALMethod.Scenario.Elements.Where(e => e.Type == elementToAdd); if (elementsOfType.Count() == 0) { if (elementToAdd == ScenarioElementType.GIVEN) { IEnumerable <ITestScenarioElement> otherElements = testALMethod.Scenario.Elements.Where(e => e.Type == ScenarioElementType.WHEN || e.Type == ScenarioElementType.THEN); if (otherElements.Count() > 0) { ITestScenarioElement element = otherElements.First(); for (int lineNo = scenarioLine; lineNo < fileContent.Count; lineNo++) { if (fileContent[lineNo].Trim() == element.LineText.Trim()) { return(lineNo); } if (Regex.IsMatch(fileContent[lineNo], @"\s{4}end;", RegexOptions.IgnoreCase)) { return(lineNo); } } } } if (elementToAdd == ScenarioElementType.WHEN) { return(FindLineToInsertElement(testALMethod, fileContent, scenarioLine, ScenarioElementType.GIVEN)); } if (elementToAdd == ScenarioElementType.THEN) { return(FindLineToInsertElement(testALMethod, fileContent, scenarioLine, ScenarioElementType.WHEN)); } } else { int countElementsOfType = elementsOfType.Count(); int lineNo = scenarioLine; for (; lineNo < fileContent.Count && countElementsOfType > 0; lineNo++) { if (Regex.IsMatch(fileContent[lineNo], @"\s+//\s*\[\s*" + elementToAdd.ToString() + @"\s*\].*", RegexOptions.IgnoreCase)) { countElementsOfType--; } } for (; lineNo < fileContent.Count; lineNo++) { if (fileContent[lineNo].Trim() == "" || Regex.IsMatch(fileContent[lineNo], @"\s+//\s*\[[^]]*\].*") || Regex.IsMatch(fileContent[lineNo], @"\s{4}end;", RegexOptions.IgnoreCase)) { lineToInsert = lineNo; break; } } } return(lineToInsert); }
public static void AddElement(ref List <string> fileContent, string elementValue, ScenarioElementType scenarioElementType, ref int lineToInsert) { ITestScenarioElement testScenarioElement = new TestScenarioElement(); testScenarioElement.Type = scenarioElementType; testScenarioElement.Value = elementValue; fileContent.Insert(lineToInsert++, Library.ALMethodHelper.Write(testScenarioElement)); }