#if ENABLE_MONO && (DEVELOPMENT_BUILD || UNITY_EDITOR)
using System;
using System.Collections.Generic;
using System.IO;
using JetBrains.Annotations;
using SingularityGroup.HotReload.Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Serialization;
namespace SingularityGroup.HotReload {
///
/// Information about the Unity Player build.
///
///
///
/// This info is used by the HotReload Server to compile your project in the same way that the Unity Player build was compiled.
/// For example, when building for Android, Unity sets a bunch of define symbols like UNITY_ANDROID.
///
///
/// Information that changes between builds is generated at build-time and put in StreamingAssets/.
/// This approach means that builds do not need to modify a project file (making file dirty in git). For example,
/// whenever user makes a mono build, the CommitHash changes and we need to regenerate the BuildInfo.
///
///
[Serializable]
class BuildInfo {
///
/// Uniquely identifies the Unity project.
///
///
/// Used on-device to check if Hot Reload server is compatible with the Unity project (same project).
/// When your computer has multiple Unity projects open, each project should provide a different value.
/// This identifier must also be the same between two different computers that are collaborating on the same project.
///
///
/// Edge-case: when a user copy pastes an entire Unity project and has both open at once,
/// then it's fine for this identifier to be the same.
///
///
public string projectIdentifier;
///
/// Git commit hash
///
///
/// Used to detect that your code is different to when the build was made.
///
public string commitHash;
///
/// List of define symbols that were active when this build was made.
///
///
/// Separate the symbols with a semi-colon character ';'
///
public string defineSymbols;
///
/// A regex of C# project names (*.csproj) to be omitted from compilation.
///
///
/// "MyTests|MyEditorAssembly"
///
[FormerlySerializedAs("projectExclusionRegex")]
public string projectOmissionRegex;
///
/// The computer that made the Android (or Standalone etc) build.
/// The hostname (ip address) where Hot Reload server would be listening.
///
public string buildMachineHostName;
///
/// The computer that made the Android (or Standalone etc) build.
/// The port where Hot Reload server would be listening.
///
public int buildMachinePort;
///
/// Selected build target in Unity Editor.
///
public string activeBuildTarget;
///
/// Used to pass in the origin onto the phone which is used to identify the correct server.
///
public string buildMachineRequestOrigin;
[JsonIgnore]
public HashSet DefineSymbolsAsHashSet {
get {
var symbols = defineSymbols.Trim().Split(';');
// split on an empty string produces 1 empty string
if (symbols.Length == 1 && symbols[0] == string.Empty) {
return new HashSet();
}
return new HashSet(symbols);
}
}
[JsonIgnore]
public PatchServerInfo BuildMachineServer {
get {
if (buildMachineHostName == null || buildMachinePort == 0) {
return null;
}
return new PatchServerInfo(buildMachineHostName, buildMachinePort, commitHash, null, customRequestOrigin: buildMachineRequestOrigin);
}
}
public string ToJson() {
return JsonConvert.SerializeObject(this);
}
[CanBeNull]
public static BuildInfo FromJson(string json) {
if (string.IsNullOrEmpty(json)) {
return null;
}
return JsonConvert.DeserializeObject(json);
}
///
/// Path to read/write the json file to.
///
/// A filepath that is inside the player build
public static string GetStoredPath() {
return Path.Combine(Application.streamingAssetsPath, GetStoredName());
}
public static string GetStoredName() {
return "HotReload_BuildInfo.json";
}
/// True if the commit hashes are definately different, otherwise False
public bool IsDifferentCommit(string remoteCommit) {
if (commitHash == PatchServerInfo.UnknownCommitHash) {
return false;
}
return !SameCommit(commitHash, remoteCommit);
}
///
/// Checks whether the commits are equivalent.
///
///
///
/// False if the commit hashes are definately different, otherwise True
public static bool SameCommit(string commitA, string commitB) {
if (commitA == null) {
// unknown commit hash, so approve anything
return true;
}
if (commitA.Length == commitB.Length) {
return commitA == commitB;
} else if (commitA.Length >= 6 && commitB.Length >= 6) {
// depending on OS, the git log pretty output has different length (7 or 8 chars)
// if the longer hash starts with the shorter hash, return true
// Assumption: commits have different length.
var longer = commitA.Length > commitB.Length ? commitA : commitB;
var shorter = commitA.Length > commitB.Length ? commitB : commitA;
return longer.StartsWith(shorter);
}
return false;
}
}
}
#endif