diff --git a/Editor/PackageVersionEmbedder.cs b/Editor/PackageVersionEmbedder.cs index a18dd1ad..a6e3e369 100644 --- a/Editor/PackageVersionEmbedder.cs +++ b/Editor/PackageVersionEmbedder.cs @@ -1,7 +1,16 @@ #if UNITY_EDITOR +using System.IO; using UnityEditor; +using UnityEditor.PackageManager; using UnityEngine; +/// +/// Writes the SDK version into a Resources text asset so the runtime can report it +/// to the server during the join handshake; see LiveKit.Internal.PackageVersion. +/// +/// When this does not run, every session reports its SDK version as "unknown" and the +/// install cannot be told apart from any other in per-release telemetry. +/// [InitializeOnLoad] public static class PackageVersionEmbedder { @@ -9,23 +18,91 @@ public static class PackageVersionEmbedder const string ResourcePath = "Assets/Resources"; const string AssetName = "LiveKitSdkVersionInfo"; + [System.Serializable] + class Manifest + { + public string version; + } + [InitializeOnLoadMethod] static void Embed() { - // FindForAssetPath works with any asset inside the package - var info = UnityEditor.PackageManager.PackageInfo.FindForAssetPath($"Packages/{PackageName}"); - if (info == null) + var version = ResolveVersion(); + if (string.IsNullOrEmpty(version)) { - Debug.Log($"Package {PackageName} not found"); + Debug.LogWarning( + $"LiveKit: could not determine the version of {PackageName}. Sessions will " + + "report their SDK version as \"unknown\", and this install will not be " + + "distinguishable from any other in per-release telemetry."); return; } - if (!System.IO.Directory.Exists(ResourcePath)) - System.IO.Directory.CreateDirectory(ResourcePath); - string path = $"{ResourcePath}/{AssetName}.txt"; - System.IO.File.WriteAllText(path, info.version); + if (File.Exists(path) && File.ReadAllText(path).Trim() == version) + { + // Already current. Skipping the write keeps this off the asset-import + // path on every domain reload. + return; + } + + Directory.CreateDirectory(ResourcePath); + File.WriteAllText(path, version); AssetDatabase.ImportAsset(path); } + + /// + /// Resolves the version however the SDK was installed: through Package Manager, + /// from a git URL, or vendored straight into Assets/. + /// + static string ResolveVersion() + { + // Covers every Package Manager source (registry, git URL, local path, embedded). + // Returns null only when the source sits under Assets/. + var info = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(PackageVersionEmbedder).Assembly); + if (info != null && !string.IsNullOrEmpty(info.version)) + { + return info.version; + } + + // Vendored into Assets/: there is no package context at all, so read the + // manifest shipped next to the source. + return VersionFromVendoredManifest(); + } + + static string VersionFromVendoredManifest() + { + foreach (var guid in AssetDatabase.FindAssets($"{nameof(PackageVersionEmbedder)} t:MonoScript")) + { + var scriptPath = AssetDatabase.GUIDToAssetPath(guid); + if (!scriptPath.EndsWith($"/{nameof(PackageVersionEmbedder)}.cs", System.StringComparison.Ordinal)) + { + continue; + } + + // scriptPath is /Editor/PackageVersionEmbedder.cs, so the + // manifest sits two levels up. + var sdkRoot = Path.GetDirectoryName(Path.GetDirectoryName(scriptPath)); + if (string.IsNullOrEmpty(sdkRoot)) + { + continue; + } + + var manifestPath = Path.Combine(sdkRoot, "package.json"); + if (!File.Exists(manifestPath)) + { + continue; + } + + // JsonUtility ignores the manifest fields it cannot map, so a type with + // just the one field is enough. + var manifest = JsonUtility.FromJson(File.ReadAllText(manifestPath)); + if (!string.IsNullOrEmpty(manifest?.version)) + { + return manifest.version; + } + } + + return null; + } } -#endif \ No newline at end of file +#endif diff --git a/Runtime/Scripts/Internal/PackageVersion.cs b/Runtime/Scripts/Internal/PackageVersion.cs index 1ab2a91f..c1707bc8 100644 --- a/Runtime/Scripts/Internal/PackageVersion.cs +++ b/Runtime/Scripts/Internal/PackageVersion.cs @@ -4,10 +4,53 @@ namespace LiveKit.Internal { public static class PackageVersion { + static bool _warned; + + /// + /// The SDK version reported to the server during the join handshake. + /// + /// The value is embedded at edit time by PackageVersionEmbedder. "unknown" means + /// that did not run, and the install cannot be told apart from any other in + /// per-release telemetry. + /// public static string Get() { var asset = Resources.Load("LiveKitSdkVersionInfo"); - return asset != null ? asset.text.Trim() : "unknown"; + var version = asset != null ? asset.text.Trim() : null; +#if UNITY_EDITOR + // PackageVersionEmbedder writes the file during the same domain reload that + // initializes FFI, but its import is deferred until the reload ends, so + // Resources.Load cannot see it yet. The file itself is already on disk. + const string editorPath = "Assets/Resources/LiveKitSdkVersionInfo.txt"; + if (string.IsNullOrEmpty(version) && System.IO.File.Exists(editorPath)) + { + version = System.IO.File.ReadAllText(editorPath).Trim(); + } +#endif + if (!string.IsNullOrEmpty(version)) + { + return version; + } + + // Covers both a missing asset and one that was written empty; each reaches + // the server as an unidentifiable version. + WarnOnce(); + return "unknown"; + } + + static void WarnOnce() + { + if (_warned) + { + return; + } + _warned = true; + + Debug.LogWarning( + "LiveKit: SDK version resource is missing or empty, so sessions will report " + + "their version as \"unknown\". This is embedded at edit time by " + + "PackageVersionEmbedder; check the Editor log for the warning it emits when " + + "it cannot resolve the package."); } } }