6using System.Collections.Generic;
17 static List<StringPair> movedPairs =
new List<StringPair>();
18 public static string BuildTempDir = Path.Combine(Application.temporaryCachePath,
"LLMUnityBuild");
19 public static string androidPluginDir = Path.Combine(Application.dataPath,
"Plugins",
"Android",
"LLMUnity");
20 public static string iOSPluginDir = Path.Combine(Application.dataPath,
"Plugins",
"iOS",
"LLMUnity");
21 static string movedCache = Path.Combine(BuildTempDir,
"moved.json");
23 [InitializeOnLoadMethod]
24 private static void InitializeOnLoad()
37 if (File.Exists(source))
39 actionCallback(source, target);
41 else if (Directory.Exists(source))
43 Directory.CreateDirectory(target);
44 List<string> filesAndDirs =
new List<string>();
45 filesAndDirs.AddRange(Directory.GetFiles(source));
46 filesAndDirs.AddRange(Directory.GetDirectories(source));
47 foreach (
string path
in filesAndDirs)
61 File.Copy(source, target,
true);
69 public static void CopyPath(
string source,
string target)
79 public static void MovePath(
string source,
string target)
91 string[] allowedDirs =
new string[] {
LLMUnitySetup.GetAssetPath(), BuildTempDir, androidPluginDir, iOSPluginDir};
92 bool deleteOK =
false;
93 foreach (
string allowedDir
in allowedDirs) deleteOK = deleteOK ||
LLMUnitySetup.IsSubPath(path, allowedDir);
96 LLMUnitySetup.LogError($
"Safeguard: {path} will not be deleted because it may not be safe");
99 if (File.Exists(path)) File.Delete(path);
100 else if (Directory.Exists(path)) Directory.Delete(path,
true);
104 static void AddMovedPair(
string source,
string target)
106 movedPairs.Add(
new StringPair {source = source, target = target});
107 File.WriteAllText(movedCache, JsonUtility.ToJson(
new ListStringPair { pairs = movedPairs },
true));
110 static void AddTargetPair(
string target)
112 AddMovedPair(
"", target);
115 static bool MoveAction(
string source,
string target,
bool addEntry =
true)
117 ActionCallback moveCallback;
118 if (File.Exists(source)) moveCallback = File.Move;
119 else if (Directory.Exists(source)) moveCallback =
MovePath;
122 if (addEntry) AddMovedPair(source, target);
123 moveCallback(source, target);
127 static bool CopyAction(
string source,
string target,
bool addEntry =
true)
129 ActionCallback copyCallback;
130 if (File.Exists(source)) copyCallback = File.Copy;
131 else if (Directory.Exists(source)) copyCallback =
CopyPath;
134 if (addEntry) AddTargetPair(target);
135 copyCallback(source, target);
139 static void CopyActionAddMeta(
string source,
string target)
141 CopyAction(source, target);
142 AddTargetPair(target +
".meta");
145 static void AddActionAddMeta(
string target)
147 AddTargetPair(target);
148 AddTargetPair(target +
".meta");
157 List<string> platforms =
new List<string>(){
"windows",
"macos",
"linux",
"android",
"ios",
"setup" };
158 platforms.Remove(platform);
161 string sourceName = Path.GetFileName(source);
162 foreach (
string platformPrefix
in platforms)
164 bool move = sourceName.StartsWith(platformPrefix);
165 move = move || (sourceName.Contains(
"cuda") && !sourceName.Contains(
"full") &&
LLMUnitySetup.FullLlamaLib);
166 move = move || (sourceName.Contains(
"cuda") && sourceName.Contains(
"full") && !
LLMUnitySetup.FullLlamaLib);
169 string target = Path.Combine(BuildTempDir, sourceName);
170 MoveAction(source, target);
171 MoveAction(source +
".meta", target +
".meta");
176 if (platform ==
"android" || platform ==
"ios")
178 string pluginDir = platform ==
"android"? androidPluginDir: iOSPluginDir;
181 MoveAction(source, target);
182 MoveAction(source +
".meta", target +
".meta");
183 AddActionAddMeta(pluginDir);
199 public static void Build(
string platform)
202 Directory.CreateDirectory(BuildTempDir);
212 if (!File.Exists(movedCache))
return;
213 List<StringPair> movedPairs = JsonUtility.FromJson<ListStringPair>(File.ReadAllText(movedCache)).pairs;
214 if (movedPairs ==
null)
return;
216 bool refresh =
false;
217 foreach (var pair
in movedPairs)
219 if (pair.source ==
"") refresh |=
DeletePath(pair.target);
220 else refresh |= MoveAction(pair.target, pair.source,
false);
222 if (refresh) AssetDatabase.Refresh();
Class implementing the LLMUnity builder.
static void Reset()
Resets the libraries back to their original state.
static void MovePath(string source, string target)
Moves a source file to a target file.
static bool DeletePath(string path)
Deletes a path after checking if we are allowed to.
static void BuildModels()
Bundles the model information.
static void BuildLibraryPlatforms(string platform)
Moves libraries in the correct place for building.
static void CopyPath(string source, string target)
Copies a source file to a target file.
static void Build(string platform)
Bundles the models and libraries.
static void HandleActionFileRecursive(string source, string target, ActionCallback actionCallback)
Performs an action for a file or a directory recursively.
static void CopyWithOverwrite(string source, string target)
Overwrites a target file based on the source file.
Class implementing the LLM model manager.
static void Build(ActionCallback copyCallback)
Saves the model manager to disk along with models that are not (or can't) be downloaded for the build...
Class implementing helper functions for setup and process management.
static string LLMManagerPath
Path of file with build information for runtime.
static string libraryPath
LlamaLib path.
static string libraryName
LlamaLib name.