6using System.Collections.Generic;
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")};
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 List<string> platforms =
new List<string>(){
"windows",
"macos",
"linux",
"android",
"ios",
"setup" };
168 platforms.Remove(platform);
171 string sourceName = Path.GetFileName(source);
172 foreach (
string platformPrefix
in platforms)
174 bool move = sourceName.StartsWith(platformPrefix);
175 move = move || (sourceName.Contains(
"cuda") && !sourceName.Contains(
"full") &&
LLMUnitySetup.FullLlamaLib);
176 move = move || (sourceName.Contains(
"cuda") && sourceName.Contains(
"full") && !
LLMUnitySetup.FullLlamaLib);
179 string target = Path.Combine(BuildTempDir, sourceName);
180 MoveAction(source, target);
181 MoveAction(source +
".meta", target +
".meta");
186 if (platform ==
"android" || platform ==
"ios")
188 string pluginPlatform = platform ==
"android" ?
"Android" :
"iOS";
190 string target = PluginLibraryDir(pluginPlatform);
191 string pluginDir = PluginDir(pluginPlatform);
192 MoveAction(source, target);
193 MoveAction(source +
".meta", target +
".meta");
194 AddActionAddMeta(pluginDir);
210 public static void Build(
string platform)
213 Directory.CreateDirectory(BuildTempDir);
223 if (!File.Exists(movedCache))
return;
224 List<StringPair> movedPairs = JsonUtility.FromJson<ListStringPair>(File.ReadAllText(movedCache)).pairs;
225 if (movedPairs ==
null)
return;
227 bool refresh =
false;
228 foreach (var pair
in movedPairs)
230 if (pair.source ==
"") refresh |=
DeletePath(pair.target);
231 else refresh |= MoveAction(pair.target, pair.source,
false);
233 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.