11 static List<StringPair> movedPairs =
new List<StringPair>();
12 public static string BuildTempDir = Path.Combine(Application.temporaryCachePath,
"LLMUnityBuild");
13 static string movedCache = Path.Combine(BuildTempDir,
"moved.json");
15 [InitializeOnLoadMethod]
16 private static void InitializeOnLoad()
21 public static void HandleActionFileRecursive(
string source,
string target, ActionCallback actionCallback)
23 if (File.Exists(source))
25 actionCallback(source, target);
27 else if (Directory.Exists(source))
29 Directory.CreateDirectory(target);
30 List<string> filesAndDirs =
new List<string>();
31 filesAndDirs.AddRange(Directory.GetFiles(source));
32 filesAndDirs.AddRange(Directory.GetDirectories(source));
33 foreach (
string path
in filesAndDirs)
35 HandleActionFileRecursive(path, Path.Combine(target, Path.GetFileName(path)), actionCallback);
40 public static void CopyWithOverwrite(
string source,
string target)
42 File.Copy(source, target,
true);
45 public static void CopyPath(
string source,
string target)
47 HandleActionFileRecursive(source, target, CopyWithOverwrite);
50 public static void MovePath(
string source,
string target)
52 HandleActionFileRecursive(source, target, File.Move);
56 public static bool DeletePath(
string path)
60 LLMUnitySetup.LogError($
"Safeguard: {path} will not be deleted because it may not be safe");
63 if (File.Exists(path)) File.Delete(path);
64 else if (Directory.Exists(path)) Directory.Delete(path,
true);
68 static void AddMovedPair(
string source,
string target)
70 movedPairs.Add(
new StringPair {source = source, target = target});
71 File.WriteAllText(movedCache, JsonUtility.ToJson(
new ListStringPair { pairs = movedPairs },
true));
74 static void AddTargetPair(
string target)
76 AddMovedPair(
"", target);
79 static bool MoveAction(
string source,
string target,
bool addEntry =
true)
81 ActionCallback moveCallback;
82 if (File.Exists(source)) moveCallback = File.Move;
83 else if (Directory.Exists(source)) moveCallback = MovePath;
86 if (addEntry) AddMovedPair(source, target);
87 moveCallback(source, target);
91 static bool CopyAction(
string source,
string target,
bool addEntry =
true)
93 ActionCallback copyCallback;
94 if (File.Exists(source)) copyCallback = File.Copy;
95 else if (Directory.Exists(source)) copyCallback = CopyPath;
98 if (addEntry) AddTargetPair(target);
99 copyCallback(source, target);
103 static void CopyActionAddMeta(
string source,
string target)
105 CopyAction(source, target);
106 AddTargetPair(target +
".meta");
109 static void AddActionAddMeta(
string target)
111 AddTargetPair(target);
112 AddTargetPair(target +
".meta");
115 public static void HideLibraryPlatforms(
string platform)
117 List<string> platforms =
new List<string>(){
"windows",
"macos",
"linux",
"android",
"ios",
"setup" };
118 platforms.Remove(platform);
121 string sourceName = Path.GetFileName(source);
122 foreach (
string platformPrefix
in platforms)
124 bool move = sourceName.StartsWith(platformPrefix);
125 move = move || (sourceName.Contains(
"cuda") && !sourceName.Contains(
"full") &&
LLMUnitySetup.FullLlamaLib);
126 move = move || (sourceName.Contains(
"cuda") && sourceName.Contains(
"full") && !
LLMUnitySetup.FullLlamaLib);
129 string target = Path.Combine(BuildTempDir, sourceName);
130 MoveAction(source, target);
131 MoveAction(source +
".meta", target +
".meta");
137 public static void BuildModels()
143 public static void Build(
string platform)
145 Directory.CreateDirectory(BuildTempDir);
146 HideLibraryPlatforms(platform);
150 public static void Reset()
152 if (!File.Exists(movedCache))
return;
153 List<StringPair> movedPairs = JsonUtility.FromJson<ListStringPair>(File.ReadAllText(movedCache)).pairs;
154 if (movedPairs ==
null)
return;
156 foreach (var pair
in movedPairs)
158 if (pair.source ==
"") DeletePath(pair.target);
159 else MoveAction(pair.target, pair.source,
false);
161 DeletePath(movedCache);