private void CreateProxy(IInvocation invocation, PathAttribute pathAttribute) { var routePath = GetRoutePath(invocation, Path, pathAttribute.Path); var proxy = _proxyGenerator.CreateInterfaceProxyWithoutTarget(invocation.Method.ReturnType, new Interceptor(_httpClient, _proxyGenerator, routePath)); invocation.ReturnValue = proxy; }
public static bool Passes(this PathAttribute condition, SyntaxNode node) { if (condition == null || node == null) { return(false); } return(condition.Pattern.IsMatch(node.SyntaxTree.FilePath) == condition.Expect); }
public void TestConstructor() { const string path = @"PATH"; var x = new PathAttribute(PathIs.Absolute, path); Assert.NotNull(x); Assert.Equal(path, x.Path); Assert.Equal(PathIs.Absolute, x.Usage); Assert.Throws <ArgumentException>(() => { new PathAttribute(PathIs.Absolute, null); }); Assert.Throws <ArgumentException>(() => { new PathAttribute(PathIs.Absolute, string.Empty); }); Assert.Throws <ArgumentOutOfRangeException>(() => { new PathAttribute(PathIs.Unknown, path); }); Assert.Throws <ArgumentOutOfRangeException>(() => { new PathAttribute((PathIs)666, path); }); }
/// <summary> /// Populates the high level metadata properties /// </summary> /// <param name="il">The IL Generator</param> /// <param name="metadataLocal"> The metadata local. </param> /// <param name="interfaceMethod"> The interface method. </param> private static void PopulateMetadata(ILGenerator il, LocalBuilder metadataLocal, MethodInfo interfaceMethod) { HttpVerb verb = ReflectionUtils.DetermineHttpVerb(interfaceMethod); string consumeMediaType = ReflectionUtils.DetermineConsumesMediaType(interfaceMethod, MediaType.TextXml); string producesMediaType = ReflectionUtils.DetermineProducesMediaType(interfaceMethod, MediaType.TextXml); PathAttribute methodPathAttr = ReflectionUtils.GetAttribute <PathAttribute>(interfaceMethod); PathAttribute servicePathAttr = ReflectionUtils.GetAttribute <PathAttribute>(interfaceMethod.DeclaringType); string methodPath = methodPathAttr == null ? "/" : methodPathAttr.Path; string servicePath = servicePathAttr == null ? string.Empty : servicePathAttr.Path; Type methodMetaType = typeof(MethodMetadata); ILWriter.WritePropertyString(il, metadataLocal, methodMetaType, "ServicePath", servicePath); ILWriter.WritePropertyString(il, metadataLocal, methodMetaType, "MethodPath", methodPath); ILWriter.WritePropertyString(il, metadataLocal, methodMetaType, "Consumes", consumeMediaType); ILWriter.WritePropertyString(il, metadataLocal, methodMetaType, "Produces", producesMediaType); ILWriter.WritePropertyInt(il, metadataLocal, methodMetaType, "Verb", (int)verb); }
/// <summary> /// Autoinitialized all fields of the customizer object to the values /// in the codonNode using the XmlMemberAttributeAttribute. /// </summary> void AutoInitializeAttributes(object obj, XmlNode codonNode) { Type currentType = obj.GetType(); while (currentType != typeof(object)) { FieldInfo[] fieldInfoArray = currentType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); foreach (FieldInfo fieldInfo in fieldInfoArray) { // process XmlMemberAttributeAttribute attributes XmlMemberAttributeAttribute codonAttribute = (XmlMemberAttributeAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(XmlMemberAttributeAttribute)); if (codonAttribute != null) { // get value from xml file XmlNode node = codonNode.SelectSingleNode("@" + codonAttribute.Name); // check if its required if (node == null && codonAttribute.IsRequired) { throw new AddInLoadException(String.Format("{0} is a required attribute for node '{1}' ", codonAttribute.Name, codonNode.Name)); } if (node != null) { if (fieldInfo.FieldType.IsSubclassOf(typeof(System.Enum))) { fieldInfo.SetValue(obj, Convert.ChangeType(Enum.Parse(fieldInfo.FieldType, node.Value), fieldInfo.FieldType)); } else { PathAttribute pathAttribute = (PathAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(PathAttribute)); if (pathAttribute != null) { fieldInfo.SetValue(obj, fileUtilityService.GetDirectoryNameWithSeparator(Path.GetDirectoryName(fileName)) + Convert.ChangeType(node.Value, fieldInfo.FieldType).ToString()); } else { fieldInfo.SetValue(obj, Convert.ChangeType(node.Value, fieldInfo.FieldType)); } } } } // process XmlMemberAttributeAttribute attributes XmlMemberArrayAttribute codonArrayAttribute = (XmlMemberArrayAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(XmlMemberArrayAttribute)); if (codonArrayAttribute != null) { // get value from xml file XmlNode node = codonNode.SelectSingleNode("@" + codonArrayAttribute.Name); // check if its required if (node == null && codonArrayAttribute.IsRequired) { throw new ApplicationException(String.Format("{0} is a required attribute.", codonArrayAttribute.Name)); } if (node != null) { string[] attrArray = node.Value.Split(codonArrayAttribute.Separator); // TODO : convert array types (currently only string arrays are supported) fieldInfo.SetValue(obj, attrArray); } } } currentType = currentType.BaseType; } }
public static string GetPath(PathAttribute pathAttribute) { #if UNITY_ANDROID && !UNITY_EDITOR // Automatic => /storage/emulated/0/Android/data/[PACKAGE_NAME]/files: OK // Internal, Persistent, Absolute => /data/user/0/[PACKAGE_NAME]/files: OK // Internal, Persistent, Default => /data/user/0/[PACKAGE_NAME]/files: OK // Internal, Persistent, Canonical => /data/data/[PACKAGE_NAME]/files: OK // Internal, Cached, Absolute => /data/user/0/[PACKAGE_NAME]/cache: OK // Internal, Cached, Default => /data/user/0/[PACKAGE_NAME]/cache: OK // Internal, Cached, Canonical => /data/data/[PACKAGE_NAME]/cache: OK // External, Persistent, Absolute => /storage/emulated/0/Android/data/[PACKAGE_NAME]/files: OK // External, Persistent, Default => /storage/emulated/0/Android/data/[PACKAGE_NAME]/files: OK // External, Persistent, Canonical => /storage/emulated/0/Android/data/[PACKAGE_NAME]/files: OK // External, Cached, Absolute => /storage/emulated/0/Android/data/[PACKAGE_NAME]/cache: OK // External, Cached, Default => /storage/emulated/0/Android/data/[PACKAGE_NAME]/cache: OK // External, Cached, Canonical => /storage/emulated/0/Android/data/[PACKAGE_NAME]/cache: OK if (pathAttribute == PathAttribute.Automatic) { return(Application.persistentDataPath); } var activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); var activity = activityClass.GetStatic <AndroidJavaObject>("currentActivity"); var endMethod = string.Empty; if ((pathAttribute & PathAttribute.Absolute) == PathAttribute.Absolute) { endMethod = "getAbsolutePath"; } else if ((pathAttribute & PathAttribute.Canonical) == PathAttribute.Canonical) { endMethod = "getCanonicalPath"; } else if ((pathAttribute & PathAttribute.Default) == PathAttribute.Default) { endMethod = "getPath"; } if ((pathAttribute & PathAttribute.Internal) == PathAttribute.Internal && (pathAttribute & PathAttribute.Persistent) == PathAttribute.Persistent) { return(activity.Call <AndroidJavaObject>("getFilesDir").Call <string>(endMethod)); } if ((pathAttribute & PathAttribute.Internal) == PathAttribute.Internal && (pathAttribute & PathAttribute.Cached) == PathAttribute.Cached) { return(activity.Call <AndroidJavaObject>("getCacheDir").Call <string>(endMethod)); } if ((pathAttribute & PathAttribute.External) == PathAttribute.External && (pathAttribute & PathAttribute.Persistent) == PathAttribute.Persistent) { return(activity.Call <AndroidJavaObject>("getExternalFilesDir", (object)null).Call <string>(endMethod)); } if ((pathAttribute & PathAttribute.External) == PathAttribute.External && (pathAttribute & PathAttribute.Cached) == PathAttribute.Cached) { return(activity.Call <AndroidJavaObject>("getExternalCacheDir").Call <string>(endMethod)); } throw new System.ArgumentOutOfRangeException(nameof(pathAttribute), pathAttribute, null); #else // PathAttributes are applicable only to Android platform at the moment. return(Application.persistentDataPath); #endif }
public override Panel CreateControl(BaseConfigurationAttribute attr, PropertyInfo prop, object configuration_instance) { var panel = base.CreateControl(attr, prop, configuration_instance); PathAttribute pattr = attr as PathAttribute; var evalue = GetConfigValue(prop, configuration_instance); var path_box = new TextBox() { Text = evalue, Width = 160, Height = 22, VerticalContentAlignment = VerticalAlignment.Center }; var button = new Button() { Width = 75, Margin = new Thickness(5, 0, 5, 0) }; if (!pattr.IsDirectory) { button.Content = DefaultLanguage.BUTTON_OPEN; } else { button.Content = DefaultLanguage.BUTTON_BROWSE; } panel.Children.Add(path_box); panel.Children.Add(button); button.Click += (s, e) => { if (!pattr.IsDirectory) { var fileDialog = new System.Windows.Forms.OpenFileDialog(); try { fileDialog.InitialDirectory = Path.GetFullPath(evalue); fileDialog.FileName = Path.GetFileName(evalue); } catch (ArgumentException) { fileDialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory; } fileDialog.RestoreDirectory = true; if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { path_box.Text = fileDialog.FileName; } } else { var folderDialog = new System.Windows.Forms.FolderBrowserDialog(); if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { path_box.Text = folderDialog.SelectedPath; } } SetConfigValue(prop, configuration_instance, path_box.Text); }; path_box.TextChanged += (s, e) => { if (pattr.Check(path_box.Text)) { SetConfigValue(prop, configuration_instance, path_box.Text); } ConfigWindow.RequireRestart = attr.RequireRestart; }; return(panel); }
private void OnSceneGUI() { if (!enemy) { return; } Event e = Event.current; if (Tools.current == Tool.Custom && EditorTools.activeToolType == typeof(EnemyTool)) { Zone guardZone = enemy.GuardZone; if (guardZone.Width * guardZone.Height != 0) { EditorGUI.BeginChangeCheck(); Vector3 V = Handles.PositionHandle(guardZone.center, Quaternion.identity); Handles.Label(V, "Guard Zone"); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(enemy, "Move Guard Zone"); guardZone.center = V; } else { switch (guardZone.Type) { case ZoneType.Rectangle: { Vector2 size = new Vector2(guardZone.Width, guardZone.Height); Handles.DrawSolidRectangleWithOutline(new Rect(guardZone.center - size / 2f, size), new Color(1f, 0f, 0f, 0.2f), new Color(1f, 0f, 0f, 0.2f)); Handles.CapFunction cap = Handles.CubeHandleCap; Vector3 c = guardZone.center; Vector2 w = new Vector2(guardZone.Width / 2, 0); Vector2 h = new Vector2(0, guardZone.Height / 2); EditorGUI.BeginChangeCheck(); Vector3 rightHandlePosition = guardZone.center + w; Vector3 leftHandlePosition = guardZone.center - w; Vector3 topHandlePosition = guardZone.center + h; Vector3 bottomHandlePosition = guardZone.center - h; Handles.color = Color.red; rightHandlePosition = Handles.Slider(rightHandlePosition, rightHandlePosition - c, 0.2f, cap, 0); leftHandlePosition = Handles.Slider(leftHandlePosition, leftHandlePosition - c, 0.2f, cap, 0); Handles.color = Color.green; topHandlePosition = Handles.Slider(topHandlePosition, topHandlePosition - c, 0.2f, cap, 0); bottomHandlePosition = Handles.Slider(bottomHandlePosition, bottomHandlePosition - c, 0.2f, cap, 0); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(enemy, "Modify Guard Zone"); guardZone.center.x = (rightHandlePosition.x + leftHandlePosition.x) / 2f; guardZone.center.y = (topHandlePosition.y + bottomHandlePosition.y) / 2f; guardZone.Width = rightHandlePosition.x - leftHandlePosition.x; guardZone.Height = topHandlePosition.y - bottomHandlePosition.y; } } break; case ZoneType.Circle: { Handles.color = new Color(1f, 0f, 0f, 0.2f); Handles.DrawSolidDisc(guardZone.center, Vector3.forward, guardZone.Radius); Handles.CapFunction cap = Handles.CubeHandleCap; Vector3 c = guardZone.center; Vector2 w = new Vector2(guardZone.Radius, 0); Vector2 h = new Vector2(0, guardZone.Radius); Vector3 rightHandlePosition = guardZone.center + w; Vector3 leftHandlePosition = guardZone.center - w; Vector3 topHandlePosition = guardZone.center + h; Vector3 bottomHandlePosition = guardZone.center - h; Handles.color = Color.red; EditorGUI.BeginChangeCheck(); rightHandlePosition = Handles.Slider(rightHandlePosition, rightHandlePosition - c, 0.2f, cap, 0); leftHandlePosition = Handles.Slider(leftHandlePosition, leftHandlePosition - c, 0.2f, cap, 0); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(enemy, "Modify Guard Zone"); guardZone.center.x = (rightHandlePosition.x + leftHandlePosition.x) / 2f; guardZone.Radius = (rightHandlePosition.x - leftHandlePosition.x) / 2f; break; } Handles.color = Color.green; EditorGUI.BeginChangeCheck(); topHandlePosition = Handles.Slider(topHandlePosition, topHandlePosition - c, 0.2f, cap, 0); bottomHandlePosition = Handles.Slider(bottomHandlePosition, bottomHandlePosition - c, 0.2f, cap, 0); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(enemy, "Modify Guard Zone"); guardZone.center.y = (topHandlePosition.y + bottomHandlePosition.y) / 2f; guardZone.Radius = (topHandlePosition.y - bottomHandlePosition.y) / 2f; break; } } break; } } } foreach (FieldInfo fieldInfo in enemy.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { try { PathAttribute pathAttribute = fieldInfo.GetCustomAttribute <PathAttribute>(false); if (pathAttribute != null) { Type fieldType = fieldInfo.FieldType; if (fieldType == typeof(Route)) { Route route = fieldInfo.GetValue(enemy) as Route; FieldInfo wayPointsField = route.GetType().GetField("wayPoints", BindingFlags.NonPublic | BindingFlags.Instance); Vector3[] wayPoints = wayPointsField.GetValue(route) as Vector3[]; if (wayPoints.Length > 1) { for (int i = 0; i < wayPoints.Length; ++i) { EditorGUI.BeginChangeCheck(); Handles.color = new Color(0f, 0f, 1f, 0.8f); Handles.DrawSolidDisc(wayPoints[i], Vector3.forward, 0.1f); Vector3 V = Handles.PositionHandle(wayPoints[i], Quaternion.identity); Handles.Label(V, i.ToString()); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(enemy, "Set New Patrol Point " + i); wayPoints[i] = V; break; } } Handles.color = new Color(0f, 1f, 0f, 0.8f); for (int i = 0; i < wayPoints.Length - 1; ++i) { EditorUtility.DrawHandlesArrow(wayPoints[i], wayPoints[i + 1]); } if (pathAttribute.isLooping) { EditorUtility.DrawHandlesArrow(wayPoints[wayPoints.Length - 1], wayPoints[0]); } } } } } catch (Exception) { } } } }
internal void GenerateRoutingRegex() { StringBuilder sb = new StringBuilder(); bool first = true; // Only select methods tagged with a PathAttribute foreach (var method in this.GetType().GetMethods().Where( method => method.GetCustomAttributes(typeof(PathAttribute), false).Length > 0)) { // Ensure the proper return type if (!Webservice.ValidReturnTypes.Contains(method.ReturnType)) { throw new InvalidOperationException(String.Format("Webservice {0} return type is not a supported.", this.GetType().ToString())); } // Populate the map of url parameters Dictionary <string, string> urlParams = new Dictionary <string, string>(); foreach (var param in method.GetParameters()) { if (param.ParameterType == typeof(HttpListenerContext) || param.ParameterType == typeof(HttpListenerRequest) || param.ParameterType == typeof(HttpListenerResponse)) { continue; } try { urlParams[param.Name] = Webservice.ValidParameterTypes[param.ParameterType]; } catch (KeyNotFoundException) { throw new InvalidOperationException(String.Format("Webservice {0} parameter {1} is not a native type.", this.GetType().ToString(), param.Name)); } } // Add a new regex match for each path foreach (string path in PathAttribute.PathsForMethod(method)) { if (first) { first = false; } else { sb.Append("|"); } // Update path regular expression to reflect url parameters string pathRegex = Regex.Escape(path); foreach (var paramKvp in urlParams) { string key = String.Format(":{0}", paramKvp.Key); string regex = String.Format("(?<arg_{0}>{1})", paramKvp.Key, paramKvp.Value); if (!pathRegex.Contains(key)) { throw new InvalidOperationException(String.Format("Webservice {0} parameter {1} not found in path {2}", this.GetType().Name, key, path)); } pathRegex = path.Replace(key, regex); } // "Index" pages if (path == "/") { pathRegex = ""; } sb.AppendFormat("(?<{0}>{1})", WebmethodGroupName(method), pathRegex); RoutingMethods.Add(method); } } // Update instance variable RoutingRegex = new Regex(sb.ToString()); }