using System.IO; using UnityEditor; using UnityEditor.Build; using UnityEditor.Build.Reporting; using UnityEngine; namespace SentisSkills.Inference { /// /// Player builds read the model, tokenizer and developer block from StreamingAssets/FunctionGemmaHome (see /// ): before a build this copies them there from the package's Models~ folder, /// and after the build it removes the copy, so the Editor keeps reading the package and the project holds no /// second copy of the model. /// sealed class FunctionGemmaBuildStaging : IPreprocessBuildWithReport, IPostprocessBuildWithReport { public int callbackOrder => 0; static string Staged => Path.Combine(Application.streamingAssetsPath, FunctionGemmaFiles.StreamingFolder); public void OnPreprocessBuild(BuildReport report) { Clean(); if (FunctionGemmaFiles.Root == null) throw new BuildFailedException($"[FunctionGemma] {FunctionGemmaFiles.PackageName} not found in the package cache."); Directory.CreateDirectory(Staged); foreach (var file in FunctionGemmaFiles.All) File.Copy(FunctionGemmaFiles.PathOf(file), Path.Combine(Staged, file), true); } public void OnPostprocessBuild(BuildReport report) => Clean(); /// Removes a copy left behind by a build that failed before its postprocess step. [InitializeOnLoadMethod] static void CleanAfterFailedBuild() { if (!BuildPipeline.isBuildingPlayer) Clean(); } static void Clean() { if (Directory.Exists(Staged)) Directory.Delete(Staged, true); File.Delete(Staged + ".meta"); } } }