Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 86 additions & 9 deletions Editor/PackageVersionEmbedder.cs
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
45 changes: 44 additions & 1 deletion Runtime/Scripts/Internal/PackageVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,53 @@ namespace LiveKit.Internal
{
public static class PackageVersion
{
static bool _warned;

/// <summary>
/// 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.
/// </summary>
public static string Get()
{
var asset = Resources.Load<TextAsset>("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();
}
Comment on lines +25 to +28

Copy link
Copy Markdown

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, Get throws if the on-disk version file exists but is unreadable. InitializeSdk then skips native initialization instead of starting with unknown.

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 unknown fallback run.

Suggested change
if (string.IsNullOrEmpty(version) && System.IO.File.Exists(editorPath))
{
version = System.IO.File.ReadAllText(editorPath).Trim();
}
if (string.IsNullOrEmpty(version) && System.IO.File.Exists(editorPath))
{
try
{
version = System.IO.File.ReadAllText(editorPath).Trim();
}
catch (System.Exception ex) when (ex is System.IO.IOException || ex is System.UnauthorizedAccessException)
{
// Version metadata is optional. Use the normal unknown-version fallback.
}
}

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

#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.");
}
}
}
Loading