Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Ink / InkLibs / InkRuntime / Divert.cs
  1. using System.Text;
  2. namespace Ink.Runtime
  3. {
  4. public class Divert : Runtime.Object
  5. {
  6. public Path targetPath {
  7. get {
  8. // Resolve any relative paths to global ones as we come across them
  9. if (_targetPath != null && _targetPath.isRelative) {
  10. var targetObj = targetPointer.Resolve();
  11. if (targetObj) {
  12. _targetPath = targetObj.path;
  13. }
  14. }
  15. return _targetPath;
  16. }
  17. set {
  18. _targetPath = value;
  19. _targetPointer = Pointer.Null;
  20. }
  21. }
  22. Path _targetPath;
  23. public Pointer targetPointer {
  24. get {
  25. if (_targetPointer.isNull) {
  26. var targetObj = ResolvePath (_targetPath).obj;
  27. if (_targetPath.lastComponent.isIndex) {
  28. _targetPointer.container = targetObj.parent as Container;
  29. _targetPointer.index = _targetPath.lastComponent.index;
  30. } else {
  31. _targetPointer = Pointer.StartOf (targetObj as Container);
  32. }
  33. }
  34. return _targetPointer;
  35. }
  36. }
  37. Pointer _targetPointer;
  38. public string targetPathString {
  39. get {
  40. if (targetPath == null)
  41. return null;
  42. return CompactPathString (targetPath);
  43. }
  44. set {
  45. if (value == null) {
  46. targetPath = null;
  47. } else {
  48. targetPath = new Path (value);
  49. }
  50. }
  51. }
  52. public string variableDivertName { get; set; }
  53. public bool hasVariableTarget { get { return variableDivertName != null; } }
  54. public bool pushesToStack { get; set; }
  55. public PushPopType stackPushType;
  56. public bool isExternal { get; set; }
  57. public int externalArgs { get; set; }
  58. public bool isConditional { get; set; }
  59. public Divert ()
  60. {
  61. pushesToStack = false;
  62. }
  63. public Divert(PushPopType stackPushType)
  64. {
  65. pushesToStack = true;
  66. this.stackPushType = stackPushType;
  67. }
  68. public override bool Equals (object obj)
  69. {
  70. var otherDivert = obj as Divert;
  71. if (otherDivert) {
  72. if (this.hasVariableTarget == otherDivert.hasVariableTarget) {
  73. if (this.hasVariableTarget) {
  74. return this.variableDivertName == otherDivert.variableDivertName;
  75. } else {
  76. return this.targetPath.Equals(otherDivert.targetPath);
  77. }
  78. }
  79. }
  80. return false;
  81. }
  82. public override int GetHashCode ()
  83. {
  84. if (hasVariableTarget) {
  85. const int variableTargetSalt = 12345;
  86. return variableDivertName.GetHashCode() + variableTargetSalt;
  87. } else {
  88. const int pathTargetSalt = 54321;
  89. return targetPath.GetHashCode() + pathTargetSalt;
  90. }
  91. }
  92. public override string ToString ()
  93. {
  94. if (hasVariableTarget) {
  95. return "Divert(variable: " + variableDivertName + ")";
  96. }
  97. else if (targetPath == null) {
  98. return "Divert(null)";
  99. } else {
  100. var sb = new StringBuilder ();
  101. string targetStr = targetPath.ToString ();
  102. int? targetLineNum = DebugLineNumberOfPath (targetPath);
  103. if (targetLineNum != null) {
  104. targetStr = "line " + targetLineNum;
  105. }
  106. sb.Append ("Divert");
  107. if (isConditional)
  108. sb.Append ("?");
  109. if (pushesToStack) {
  110. if (stackPushType == PushPopType.Function) {
  111. sb.Append (" function");
  112. } else {
  113. sb.Append (" tunnel");
  114. }
  115. }
  116. sb.Append (" -> ");
  117. sb.Append (targetPathString);
  118. sb.Append (" (");
  119. sb.Append (targetStr);
  120. sb.Append (")");
  121. return sb.ToString ();
  122. }
  123. }
  124. }
  125. }