|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using UnityEditor; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace GameLovers.Services.Editor |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Set the internal version in any VersionService instances in the project before building |
| 10 | + /// and whenever the project loads. |
| 11 | + /// </summary> |
| 12 | + public static class VersionEditorUtils |
| 13 | + { |
| 14 | + private const int ShortenedCommitLength = 8; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Set the internal version before building the app. |
| 18 | + /// </summary> |
| 19 | + public static void SetAndSaveInternalVersion(bool isStoreBuild) |
| 20 | + { |
| 21 | + var newVersionData = GenerateInternalVersionSuffix(isStoreBuild); |
| 22 | + var newVersionDataSerialized = JsonUtility.ToJson(newVersionData); |
| 23 | + var oldVersionDataSerialized = LoadVersionDataSerializedSync(); |
| 24 | + if (newVersionDataSerialized.Equals(oldVersionDataSerialized, StringComparison.Ordinal)) |
| 25 | + { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + Debug.Log($"Saving new version data: {newVersionDataSerialized}"); |
| 30 | + SaveVersionData(newVersionDataSerialized); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Loads the game version saved in disk into string format |
| 35 | + /// </summary> |
| 36 | + public static string LoadVersionDataSerializedSync() |
| 37 | + { |
| 38 | + var textAsset = Resources.Load<TextAsset>(VersionServices.VersionDataFilename); |
| 39 | + if (!textAsset) |
| 40 | + { |
| 41 | + Debug.LogError("Could not load internal version from Resources."); |
| 42 | + return string.Empty; |
| 43 | + } |
| 44 | + |
| 45 | + var serialized = textAsset.text; |
| 46 | + Resources.UnloadAsset(textAsset); |
| 47 | + return serialized; |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Set the internal version for when the app plays in editor. |
| 52 | + /// </summary> |
| 53 | + [InitializeOnLoadMethod] |
| 54 | + private static void OnEditorLoad() |
| 55 | + { |
| 56 | + SetAndSaveInternalVersion(false); |
| 57 | + } |
| 58 | + |
| 59 | + private static VersionServices.VersionData GenerateInternalVersionSuffix(bool isStoreBuild) |
| 60 | + { |
| 61 | + var data = new VersionServices.VersionData(); |
| 62 | + |
| 63 | + using (var repo = new GitEditorProcess(Application.dataPath)) |
| 64 | + { |
| 65 | + try |
| 66 | + { |
| 67 | + if (!repo.IsValidRepo()) |
| 68 | + { |
| 69 | + Debug.LogWarning("Project is not a git repo. Internal version not set."); |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + var branch = repo.GetBranch(); |
| 74 | + if (string.IsNullOrEmpty(branch)) |
| 75 | + { |
| 76 | + Debug.LogWarning("Could not get git branch for internal version"); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + data.BranchName = branch; |
| 81 | + } |
| 82 | + |
| 83 | + var commitHash = repo.GetCommitHash(); |
| 84 | + if (string.IsNullOrEmpty(commitHash)) |
| 85 | + { |
| 86 | + Debug.LogWarning("Could not get git commit for internal version"); |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + data.Commit = commitHash.Substring(0, ShortenedCommitLength); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + catch (Exception e) |
| 95 | + { |
| 96 | + Debug.LogException(e); |
| 97 | + Debug.LogWarning("Could not execute git commands. Internal version not set."); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + data.BuildNumber = PlayerSettings.iOS.buildNumber; |
| 102 | + data.BuildType = isStoreBuild ? "prod" : "dev"; |
| 103 | + |
| 104 | + return data; |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Set the internal version of this application and save it in resources. This should be |
| 109 | + /// called at edit/build time. |
| 110 | + /// </summary> |
| 111 | + private static void SaveVersionData(string serializedData) |
| 112 | + { |
| 113 | + const string assets = "Assets"; |
| 114 | + const string resources = "Build/Resources"; |
| 115 | + |
| 116 | + var absDirPath = Path.Combine(Application.dataPath, resources); |
| 117 | + if (!Directory.Exists(absDirPath)) |
| 118 | + { |
| 119 | + Directory.CreateDirectory(absDirPath); |
| 120 | + } |
| 121 | + |
| 122 | + // delete old file with incorrect extension |
| 123 | + const string assetExtension = ".asset"; |
| 124 | + var absFilePath = Path.Combine(absDirPath, VersionServices.VersionDataFilename); |
| 125 | + if (File.Exists(Path.ChangeExtension(absFilePath, assetExtension))) |
| 126 | + { |
| 127 | + AssetDatabase.DeleteAsset( |
| 128 | + Path.Combine(assets, resources, |
| 129 | + Path.ChangeExtension(VersionServices.VersionDataFilename, assetExtension))); |
| 130 | + } |
| 131 | + |
| 132 | + // create new text file |
| 133 | + const string textExtension = ".txt"; |
| 134 | + File.WriteAllText(Path.ChangeExtension(absFilePath, textExtension), serializedData); |
| 135 | + |
| 136 | + AssetDatabase.ImportAsset( |
| 137 | + Path.Combine(assets, resources, |
| 138 | + Path.ChangeExtension(VersionServices.VersionDataFilename, textExtension))); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments