private void okBtn_Click(object sender, EventArgs e) { bool isValid = ValidateChildren(); if (!isValid) { // Prevent closing the dialog if we've not got valid fields. // necessary because the name attribute name field starts empty - and hence doesn't get validated by default this.DialogResult = DialogResult.None; return; } else { this.DialogResult = DialogResult.OK; } BasePatch patch; if ((AttributePatchTypes)patchTypeCombo.SelectedItem == AttributePatchTypes.Patch) { patch = new PatchAttribute(elementXPathTextBox.Text, nameTextBox.Text, valueTextBox.Text); } else { patch = new SetAttribute(elementXPathTextBox.Text, nameTextBox.Text, valueTextBox.Text); } Patch = new PatchItem(patch, _treeNode); }
static void ProcessType(Type type, ICollection <PatchDescriptor> descriptors) { var allMethods = type.GetMethods(ALL_INCLUSIVE_BINDING_FLAGS); foreach (var method in allMethods) { PatchAttribute attr = Attribute.GetCustomAttribute(method, typeof(PatchAttribute)) as PatchAttribute; if (attr != null) { descriptors.Add(new PatchDescriptor(method, attr)); } } }
static MethodBase[] _getPatchTargets(MethodInfo method) { string _error(string targetMethod) => $"Patch target method is null! Patch method: {method.fullName()}, target method: {targetMethod}"; var harmonyPatches = method.getAttrs <HarmonyPatch>(); if (!harmonyPatches.isNullOrEmpty()) { MethodBase _getTargetMethod(HarmonyPatch patch) { var targetMethod = patch.info.getTargetMethod(); Debug.assert(targetMethod != null, _error(PatchesValidator.getFullName(patch.info))); return(targetMethod); } return(harmonyPatches.Select(_getTargetMethod).ToArray()); } if (PatchAttribute.merge(method.getAttrs <PatchAttribute>()) is PatchAttribute patchAttr) { var targetMethod = patchAttr.targetMethod; Debug.assert(patchAttr.options.HasFlag(PatchOptions.CanBeAbsent) || targetMethod != null, _error(PatchesValidator.getFullName(patchAttr))); if (targetMethod == null) { return(null); } if (patchAttr.options.HasFlag(PatchOptions.PatchOnce) && isPatchedBy(targetMethod, method, true)) { return(null); } return(new[] { targetMethod }); } return(null); }
public PatchDescriptor(MethodInfo method, PatchAttribute attr) { patchMethod = method; patchAttribute = attr; if (patchAttribute is ReplaceAttribute) { patchType = PatchType.Replace; } else if (patchAttribute is PreHookAttribute) { patchType = PatchType.PreHook; } else if (patchAttribute is PostHookAttribute) { patchType = PatchType.PostHook; } else { throw new ArgumentException("Unknown PatchAttribute! " + attr, "attr"); } }
public void PatchAttribute_PatchGenerator_Accepts_PatchAttribute() { var pa = new PatchAttribute("/sitecore/sites/site[@name='a']", "cheese", "1"); var xml = XDocument.Parse("<sitecore><sites><site name=\"a\"/></sites></sitecore>"); var sut = new PatchGenerator(xml); var result = sut.GeneratePatchFile(new BasePatch[] { pa }); var el = result .Element("configuration") .Element("sitecore") .Element("sites") .Element("site"); Assert.IsNotNull(el); var patch = el.Element(Namespaces.Patch + "attribute"); Assert.IsNotNull(patch); Assert.AreEqual("cheese", patch.Attribute("name").Value); Assert.AreEqual("1", patch.Attribute("value").Value); }
private static void TryInject(this MethodDefinition def, MethodDefinition toInject, PatchAttribute attrib) { try { def.InjectWith( toInject, attrib.Offset, attrib.Tag, (InjectFlags)attrib.Flags, attrib.After ? InjectDirection.After : InjectDirection.Before, attrib.LocalIds); } catch (Exception ex) { throw new Exception($"Error applying patch for '{attrib.MethodSig}'", ex); } }
public MethodStore(string name, PatchAttribute attrib, MethodDefinition target) { Name = name; Attribute = attrib; Target = target; }
private void ProcessType(Type type, PatchAttribute pa) { PatchMetaClass meta = new PatchMetaClass(type); patches.Add(pa.Name, meta); }
public void PatchAttribute_Constructor_ValidAttrValue_Works() { var sut = new PatchAttribute("/sites/site", "a", "test"); Assert.AreEqual("test", sut.AttributeValue); }
public void PatchAttribute_Constructor_NullAttrValue_Throws() { var sut = new PatchAttribute("/site", "a", null); }
public void PatchAttribute_Constructor_EmptyAttrName_Throws() { var sut = new PatchAttribute("/site", string.Empty, "a"); }
public void PatchAttribute_Constructor_ValidPath_Works() { var sut = new PatchAttribute("/sites/site", "test", "a"); Assert.AreEqual("/sites/site", sut.XPathForElement); }
public void PatchAttribute_Constructor_EmptyPath_Throws() { var sut = new PatchAttribute(string.Empty, "test", "a"); }
public void PatchAttribute_Constructor_NullPath_Throws() { var sut = new PatchAttribute(null, "test", "a"); }