BuildInfo.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #if ENABLE_MONO && (DEVELOPMENT_BUILD || UNITY_EDITOR)
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using JetBrains.Annotations;
  6. using SingularityGroup.HotReload.Newtonsoft.Json;
  7. using UnityEngine;
  8. using UnityEngine.Serialization;
  9. namespace SingularityGroup.HotReload {
  10. /// <summary>
  11. /// Information about the Unity Player build.
  12. /// </summary>
  13. /// <remarks>
  14. /// <para>
  15. /// This info is used by the HotReload Server to compile your project in the same way that the Unity Player build was compiled.<br/>
  16. /// For example, when building for Android, Unity sets a bunch of define symbols like UNITY_ANDROID.
  17. /// </para>
  18. /// <para>
  19. /// Information that changes between builds is generated at build-time and put in StreamingAssets/.<br/>
  20. /// This approach means that builds do not need to modify a project file (making file dirty in git). For example,
  21. /// whenever user makes a mono build, the CommitHash changes and we need to regenerate the BuildInfo.
  22. /// </para>
  23. /// </remarks>
  24. [Serializable]
  25. class BuildInfo {
  26. /// <summary>
  27. /// Uniquely identifies the Unity project.
  28. /// </summary>
  29. /// <remarks>
  30. /// Used on-device to check if Hot Reload server is compatible with the Unity project (same project).<br/>
  31. /// When your computer has multiple Unity projects open, each project should provide a different value.<br/>
  32. /// This identifier must also be the same between two different computers that are collaborating on the same project.
  33. ///
  34. /// <para>
  35. /// Edge-case: when a user copy pastes an entire Unity project and has both open at once,
  36. /// then it's fine for this identifier to be the same.
  37. /// </para>
  38. /// </remarks>
  39. public string projectIdentifier;
  40. /// <summary>
  41. /// Git commit hash
  42. /// </summary>
  43. /// <remarks>
  44. /// Used to detect that your code is different to when the build was made.
  45. /// </remarks>
  46. public string commitHash;
  47. /// <summary>
  48. /// List of define symbols that were active when this build was made.
  49. /// </summary>
  50. /// <remarks>
  51. /// Separate the symbols with a semi-colon character ';'
  52. /// </remarks>
  53. public string defineSymbols;
  54. /// <summary>
  55. /// A regex of C# project names (*.csproj) to be omitted from compilation.
  56. /// </summary>
  57. /// <example>
  58. /// "MyTests|MyEditorAssembly"
  59. /// </example>
  60. [FormerlySerializedAs("projectExclusionRegex")]
  61. public string projectOmissionRegex;
  62. /// <summary>
  63. /// The computer that made the Android (or Standalone etc) build.<br/>
  64. /// The hostname (ip address) where Hot Reload server would be listening.
  65. /// </summary>
  66. public string buildMachineHostName;
  67. /// <summary>
  68. /// The computer that made the Android (or Standalone etc) build.<br/>
  69. /// The port where Hot Reload server would be listening.
  70. /// </summary>
  71. public int buildMachinePort;
  72. /// <summary>
  73. /// Selected build target in Unity Editor.
  74. /// </summary>
  75. public string activeBuildTarget;
  76. /// <summary>
  77. /// Used to pass in the origin onto the phone which is used to identify the correct server.
  78. /// </summary>
  79. public string buildMachineRequestOrigin;
  80. [JsonIgnore]
  81. public HashSet<string> DefineSymbolsAsHashSet {
  82. get {
  83. var symbols = defineSymbols.Trim().Split(';');
  84. // split on an empty string produces 1 empty string
  85. if (symbols.Length == 1 && symbols[0] == string.Empty) {
  86. return new HashSet<string>();
  87. }
  88. return new HashSet<string>(symbols);
  89. }
  90. }
  91. [JsonIgnore]
  92. public PatchServerInfo BuildMachineServer {
  93. get {
  94. if (buildMachineHostName == null || buildMachinePort == 0) {
  95. return null;
  96. }
  97. return new PatchServerInfo(buildMachineHostName, buildMachinePort, commitHash, null, customRequestOrigin: buildMachineRequestOrigin);
  98. }
  99. }
  100. public string ToJson() {
  101. return JsonConvert.SerializeObject(this);
  102. }
  103. [CanBeNull]
  104. public static BuildInfo FromJson(string json) {
  105. if (string.IsNullOrEmpty(json)) {
  106. return null;
  107. }
  108. return JsonConvert.DeserializeObject<BuildInfo>(json);
  109. }
  110. /// <summary>
  111. /// Path to read/write the json file to.
  112. /// </summary>
  113. /// <returns>A filepath that is inside the player build</returns>
  114. public static string GetStoredPath() {
  115. return Path.Combine(Application.streamingAssetsPath, GetStoredName());
  116. }
  117. public static string GetStoredName() {
  118. return "HotReload_BuildInfo.json";
  119. }
  120. /// <returns>True if the commit hashes are definately different, otherwise False</returns>
  121. public bool IsDifferentCommit(string remoteCommit) {
  122. if (commitHash == PatchServerInfo.UnknownCommitHash) {
  123. return false;
  124. }
  125. return !SameCommit(commitHash, remoteCommit);
  126. }
  127. /// <summary>
  128. /// Checks whether the commits are equivalent.
  129. /// </summary>
  130. /// <param name="commitA"></param>
  131. /// <param name="commitB"></param>
  132. /// <returns>False if the commit hashes are definately different, otherwise True</returns>
  133. public static bool SameCommit(string commitA, string commitB) {
  134. if (commitA == null) {
  135. // unknown commit hash, so approve anything
  136. return true;
  137. }
  138. if (commitA.Length == commitB.Length) {
  139. return commitA == commitB;
  140. } else if (commitA.Length >= 6 && commitB.Length >= 6) {
  141. // depending on OS, the git log pretty output has different length (7 or 8 chars)
  142. // if the longer hash starts with the shorter hash, return true
  143. // Assumption: commits have different length.
  144. var longer = commitA.Length > commitB.Length ? commitA : commitB;
  145. var shorter = commitA.Length > commitB.Length ? commitB : commitA;
  146. return longer.StartsWith(shorter);
  147. }
  148. return false;
  149. }
  150. }
  151. }
  152. #endif