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()
35 if (File.Exists(source))
37 actionCallback(source, target);
39 else if (Directory.Exists(source))
41 Directory.CreateDirectory(target);
42 List<string> filesAndDirs =
new List<string>();
43 filesAndDirs.AddRange(Directory.GetFiles(source));
44 filesAndDirs.AddRange(Directory.GetDirectories(source));
45 foreach (
string path
in filesAndDirs)
59 File.Copy(source, target,
true);
67 public static void CopyPath(
string source,
string target)
77 public static void MovePath(
string source,
string target)
91 LLMUnitySetup.LogError($
"Safeguard: {path} will not be deleted because it may not be safe");
94 if (File.Exists(path)) File.Delete(path);
95 else if (Directory.Exists(path)) Directory.Delete(path,
true);
99 static void AddMovedPair(
string source,
string target)
101 movedPairs.Add(
new StringPair {source = source, target = target});
102 File.WriteAllText(movedCache, JsonUtility.ToJson(
new ListStringPair { pairs = movedPairs },
true));
105 static void AddTargetPair(
string target)
107 AddMovedPair(
"", target);
110 static bool MoveAction(
string source,
string target,
bool addEntry =
true)
112 ActionCallback moveCallback;
113 if (File.Exists(source)) moveCallback = File.Move;
114 else if (Directory.Exists(source)) moveCallback =
MovePath;
117 if (addEntry) AddMovedPair(source, target);
118 moveCallback(source, target);
122 static bool CopyAction(
string source,
string target,
bool addEntry =
true)
124 ActionCallback copyCallback;
125 if (File.Exists(source)) copyCallback = File.Copy;
126 else if (Directory.Exists(source)) copyCallback =
CopyPath;
129 if (addEntry) AddTargetPair(target);
130 copyCallback(source, target);
134 static void CopyActionAddMeta(
string source,
string target)
136 CopyAction(source, target);
137 AddTargetPair(target +
".meta");
140 static void AddActionAddMeta(
string target)
142 AddTargetPair(target);
143 AddTargetPair(target +
".meta");
152 List<string> platforms =
new List<string>(){
"windows",
"macos",
"linux",
"android",
"ios",
"setup" };
153 platforms.Remove(platform);
156 string sourceName = Path.GetFileName(source);
157 foreach (
string platformPrefix
in platforms)
159 bool move = sourceName.StartsWith(platformPrefix);
160 move = move || (sourceName.Contains(
"cuda") && !sourceName.Contains(
"full") &&
LLMUnitySetup.FullLlamaLib);
161 move = move || (sourceName.Contains(
"cuda") && sourceName.Contains(
"full") && !
LLMUnitySetup.FullLlamaLib);
164 string target = Path.Combine(BuildTempDir, sourceName);
165 MoveAction(source, target);
166 MoveAction(source +
".meta", target +
".meta");
184 public static void Build(
string platform)
186 Directory.CreateDirectory(BuildTempDir);
196 if (!File.Exists(movedCache))
return;
197 List<StringPair> movedPairs = JsonUtility.FromJson<ListStringPair>(File.ReadAllText(movedCache)).pairs;
198 if (movedPairs ==
null)
return;
200 bool refresh =
false;
201 foreach (var pair
in movedPairs)
203 if (pair.source ==
"") refresh |=
DeletePath(pair.target);
204 else refresh |= MoveAction(pair.target, pair.source,
false);
206 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 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.
static void HideLibraryPlatforms(string platform)
Hides all the library platforms apart from the target platform by moving out their library folders ou...
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.