17 static List<StringPair> movedPairs =
new List<StringPair>();
18 public static string BuildTempDir = Path.Combine(Application.temporaryCachePath,
"LLMUnityBuild");
19 static string movedCache = Path.Combine(BuildTempDir,
"moved.json");
21 [InitializeOnLoadMethod]
22 private static void InitializeOnLoad()
27 public static string PluginDir(
string platform,
bool relative =
false)
29 string pluginDir = Path.Combine(
"Plugins", platform,
"LLMUnity");
30 if (!relative) pluginDir = Path.Combine(Application.dataPath, pluginDir);
34 public static string PluginLibraryDir(
string platform,
bool relative =
false)
47 if (File.Exists(source))
49 actionCallback(source, target);
51 else if (Directory.Exists(source))
53 Directory.CreateDirectory(target);
54 List<string> filesAndDirs =
new List<string>();
55 filesAndDirs.AddRange(Directory.GetFiles(source));
56 filesAndDirs.AddRange(Directory.GetDirectories(source));
57 foreach (
string path
in filesAndDirs)
71 File.Copy(source, target,
true);
79 public static void CopyPath(
string source,
string target)
89 public static void MovePath(
string source,
string target)
101 string[] allowedDirs =
new string[] {
LLMUnitySetup.GetAssetPath(), BuildTempDir, PluginDir(
"Android"), PluginDir(
"iOS"), PluginDir(
"VisionOS")};
102 bool deleteOK =
false;
103 foreach (
string allowedDir
in allowedDirs) deleteOK = deleteOK ||
LLMUnitySetup.IsSubPath(path, allowedDir);
106 LLMUnitySetup.LogError($
"Safeguard: {path} will not be deleted because it may not be safe");
109 if (File.Exists(path)) File.Delete(path);
110 else if (Directory.Exists(path)) Directory.Delete(path,
true);
114 static void AddMovedPair(
string source,
string target)
116 movedPairs.Add(
new StringPair {source = source, target = target});
117 File.WriteAllText(movedCache, JsonUtility.ToJson(
new ListStringPair { pairs = movedPairs },
true));
120 static void AddTargetPair(
string target)
122 AddMovedPair(
"", target);
125 static bool MoveAction(
string source,
string target,
bool addEntry =
true)
127 ActionCallback moveCallback;
128 if (File.Exists(source)) moveCallback = File.Move;
129 else if (Directory.Exists(source)) moveCallback =
MovePath;
132 if (addEntry) AddMovedPair(source, target);
133 moveCallback(source, target);
137 static bool CopyAction(
string source,
string target,
bool addEntry =
true)
139 ActionCallback copyCallback;
140 if (File.Exists(source)) copyCallback = File.Copy;
141 else if (Directory.Exists(source)) copyCallback =
CopyPath;
144 if (addEntry) AddTargetPair(target);
145 copyCallback(source, target);
149 static void CopyActionAddMeta(
string source,
string target)
151 CopyAction(source, target);
152 AddTargetPair(target +
".meta");
155 static void AddActionAddMeta(
string target)
157 AddTargetPair(target);
158 AddTargetPair(target +
".meta");
167 string platform =
"";
170 case BuildTarget.StandaloneWindows:
171 case BuildTarget.StandaloneWindows64:
172 platform =
"windows";
174 case BuildTarget.StandaloneLinux64:
177 case BuildTarget.StandaloneOSX:
180 case BuildTarget.Android:
181 platform =
"android";
183 case BuildTarget.iOS:
186 case BuildTarget.VisionOS:
187 platform =
"visionos";
193 string sourceName = Path.GetFileName(source);
194 bool move = !sourceName.StartsWith(platform);
195 move = move || (sourceName.Contains(
"cuda") && !sourceName.Contains(
"full") &&
LLMUnitySetup.FullLlamaLib);
196 move = move || (sourceName.Contains(
"cuda") && sourceName.Contains(
"full") && !
LLMUnitySetup.FullLlamaLib);
199 string target = Path.Combine(BuildTempDir, sourceName);
200 MoveAction(source, target);
201 MoveAction(source +
".meta", target +
".meta");
205 if (buildTarget == BuildTarget.Android || buildTarget == BuildTarget.iOS || buildTarget == BuildTarget.VisionOS)
208 string target = PluginLibraryDir(buildTarget.ToString());
209 string pluginDir = PluginDir(buildTarget.ToString());
210 MoveAction(source, target);
211 MoveAction(source +
".meta", target +
".meta");
212 AddActionAddMeta(pluginDir);
216 static void OnPostprocessAllAssets(
string[] importedAssets,
string[] deletedAssets,
string[] movedAssets,
string[] movedFromAssetPaths,
bool didDomainReload)
218 foreach (BuildTarget buildTarget
in new BuildTarget[]{BuildTarget.iOS, BuildTarget.VisionOS})
220 string pathToPlugin = Path.Combine(
"Assets", PluginLibraryDir(buildTarget.ToString(),
true), $
"libundreamai_{buildTarget.ToString().ToLower()}.a");
221 for (
int i = 0; i < movedAssets.Length; i++)
223 if (movedAssets[i] == pathToPlugin)
225 var importer = AssetImporter.GetAtPath(pathToPlugin) as PluginImporter;
226 if (importer !=
null && importer.isNativePlugin)
228 importer.SetCompatibleWithPlatform(buildTarget,
true);
229 importer.SetPlatformData(buildTarget,
"CPU",
"ARM64");
230 AssetDatabase.ImportAsset(pathToPlugin);
249 public static void Build(BuildTarget buildTarget)
252 Directory.CreateDirectory(BuildTempDir);
262 if (!File.Exists(movedCache))
return;
263 List<StringPair> movedPairs = JsonUtility.FromJson<ListStringPair>(File.ReadAllText(movedCache)).pairs;
264 if (movedPairs ==
null)
return;
266 bool refresh =
false;
267 foreach (var pair
in movedPairs)
269 if (pair.source ==
"") refresh |=
DeletePath(pair.target);
270 else refresh |= MoveAction(pair.target, pair.source,
false);
272 if (refresh) AssetDatabase.Refresh();