File size: 1,818 Bytes
660adc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.IO;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;

namespace SentisSkills.Inference
{
    /// <summary>
    /// Player builds read the model, tokenizer and developer block from StreamingAssets/FunctionGemmaHome (see
    /// <see cref="FunctionGemmaFiles"/>): 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.
    /// </summary>
    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();

        /// <summary>Removes a copy left behind by a build that failed before its postprocess step.</summary>
        [InitializeOnLoadMethod]
        static void CleanAfterFailedBuild()
        {
            if (!BuildPipeline.isBuildingPlayer) Clean();
        }

        static void Clean()
        {
            if (Directory.Exists(Staged)) Directory.Delete(Staged, true);
            File.Delete(Staged + ".meta");
        }
    }
}