PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/Assets/Plugins/MoonSharp/Debugger/SDK/Utilities.cs

https://bitbucket.org/nbrady/caminho-unity
C# | 95 lines | 50 code | 8 blank | 37 comment | 10 complexity | 2fa1c2eefd5c1d30df747b1367da96d0 MD5 | raw file
Possible License(s): MIT
  1. #if (!PCL) && ((!UNITY_5) || UNITY_STANDALONE)
  2. /*---------------------------------------------------------------------------------------------
  3. Copyright (c) Microsoft Corporation
  4. All rights reserved.
  5. MIT License
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in all
  13. copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. *--------------------------------------------------------------------------------------------*/
  22. using System;
  23. using System.Net;
  24. using System.IO;
  25. using System.Linq;
  26. using System.Text.RegularExpressions;
  27. using System.Reflection;
  28. using MoonSharp.Interpreter.Compatibility;
  29. namespace MoonSharp.VsCodeDebugger.SDK
  30. {
  31. internal class Utilities
  32. {
  33. private static readonly Regex VARIABLE = new Regex(@"\{(\w+)\}");
  34. /*
  35. * Resolve hostname, dotted-quad notation for IPv4, or colon-hexadecimal notation for IPv6 to IPAddress.
  36. * Returns null on failure.
  37. */
  38. public static string ExpandVariables(string format, object variables, bool underscoredOnly = true)
  39. {
  40. if (variables == null)
  41. {
  42. variables = new { };
  43. }
  44. Type type = variables.GetType();
  45. return VARIABLE.Replace(format, match => {
  46. string name = match.Groups[1].Value;
  47. if (!underscoredOnly || name.StartsWith("_"))
  48. {
  49. PropertyInfo property = Framework.Do.GetProperty(type, name);
  50. if (property != null)
  51. {
  52. object value = property.GetValue(variables, null);
  53. return value.ToString();
  54. }
  55. return '{' + name + ": not found}";
  56. }
  57. return match.Groups[0].Value;
  58. });
  59. }
  60. /**
  61. * converts the given absPath into a path that is relative to the given dirPath.
  62. */
  63. public static string MakeRelativePath(string dirPath, string absPath)
  64. {
  65. if (!dirPath.EndsWith("/"))
  66. {
  67. dirPath += "/";
  68. }
  69. if (absPath.StartsWith(dirPath))
  70. {
  71. return absPath.Replace(dirPath, "");
  72. }
  73. return absPath;
  74. /*
  75. Uri uri1 = new Uri(path);
  76. Uri uri2 = new Uri(dir_path);
  77. return uri2.MakeRelativeUri(uri1).ToString();
  78. */
  79. }
  80. }
  81. }
  82. #endif