Repository navigation
fix: resolve the SDK version on every install layout #411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
717e2c1
fix: resolve the SDK version on every install layout
xianshijing-lk beb8f33
Fix ambigous import
MaxHeimbrock 670540d
fix: read the version file from disk in the Editor on first load
MaxHeimbrock f7f45b3
docs: correct why PackageVersionEmbedder resolves from the assembly
MaxHeimbrock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,108 @@ | ||
| #if UNITY_EDITOR | ||
| using System.IO; | ||
| using UnityEditor; | ||
| using UnityEditor.PackageManager; | ||
| using UnityEngine; | ||
|
|
||
| /// <summary> | ||
| /// Writes the SDK version into a Resources text asset so the runtime can report it | ||
| /// to the server during the join handshake; see <c>LiveKit.Internal.PackageVersion</c>. | ||
| /// | ||
| /// 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. | ||
| /// </summary> | ||
| [InitializeOnLoad] | ||
| public static class PackageVersionEmbedder | ||
| { | ||
| const string PackageName = "io.livekit.livekit-sdk"; | ||
| 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); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Resolves the version however the SDK was installed: through Package Manager, | ||
| /// from a git URL, or vendored straight into Assets/. | ||
| /// </summary> | ||
| 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 <sdkRoot>/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<Manifest>(File.ReadAllText(manifestPath)); | ||
| if (!string.IsNullOrEmpty(manifest?.version)) | ||
| { | ||
| return manifest.version; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
| #endif | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Unreadable version file blocks editor initialization
During an editor reload without a usable version asset,
Getthrows if the on-disk version file exists but is unreadable. InitializeSdk then skips native initialization instead of starting withunknown.Learn more
Unity calls OnAfterAssemblyReload to initialize LiveKit after reloading its managed code. InitializeSdk fetches version metadata before registering the native callback. A file existence check does not guarantee read access. An exception from the fallback therefore aborts initialization instead of reaching the unknown-version fallback.
Example: Open a project with a fresh Unity asset cache and a generated version file copied from another account without read permission. The file can be visible in the directory even though the editor cannot import or read it. LiveKit skips native initialization instead of initializing with
unknown.Recommended fix: Catch I/O and access errors around the editor-only file read, then let the existing warning and
unknownfallback run.Was this helpful? React with 👍 or 👎 to provide feedback.