LLM for Unity  v2.4.1
Create characters in Unity with LLMs!
Loading...
Searching...
No Matches
LLMBuilder.cs
Go to the documentation of this file.
1
3using UnityEditor;
4using UnityEngine;
5using System.IO;
6using System.Collections.Generic;
7
8#if UNITY_EDITOR
9namespace LLMUnity
10{
15 public class LLMBuilder
16 {
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");
22
23 [InitializeOnLoadMethod]
24 private static void InitializeOnLoad()
25 {
26 Reset();
27 }
28
35 public static void HandleActionFileRecursive(string source, string target, ActionCallback actionCallback)
36 {
37 if (File.Exists(source))
38 {
39 actionCallback(source, target);
40 }
41 else if (Directory.Exists(source))
42 {
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)
48 {
49 HandleActionFileRecursive(path, Path.Combine(target, Path.GetFileName(path)), actionCallback);
50 }
51 }
52 }
53
59 public static void CopyWithOverwrite(string source, string target)
60 {
61 File.Copy(source, target, true);
62 }
63
69 public static void CopyPath(string source, string target)
70 {
72 }
73
79 public static void MovePath(string source, string target)
80 {
81 HandleActionFileRecursive(source, target, File.Move);
82 DeletePath(source);
83 }
84
89 public static bool DeletePath(string path)
90 {
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);
94 if (!deleteOK)
95 {
96 LLMUnitySetup.LogError($"Safeguard: {path} will not be deleted because it may not be safe");
97 return false;
98 }
99 if (File.Exists(path)) File.Delete(path);
100 else if (Directory.Exists(path)) Directory.Delete(path, true);
101 return true;
102 }
103
104 static void AddMovedPair(string source, string target)
105 {
106 movedPairs.Add(new StringPair {source = source, target = target});
107 File.WriteAllText(movedCache, JsonUtility.ToJson(new ListStringPair { pairs = movedPairs }, true));
108 }
109
110 static void AddTargetPair(string target)
111 {
112 AddMovedPair("", target);
113 }
114
115 static bool MoveAction(string source, string target, bool addEntry = true)
116 {
117 ActionCallback moveCallback;
118 if (File.Exists(source)) moveCallback = File.Move;
119 else if (Directory.Exists(source)) moveCallback = MovePath;
120 else return false;
121
122 if (addEntry) AddMovedPair(source, target);
123 moveCallback(source, target);
124 return true;
125 }
126
127 static bool CopyAction(string source, string target, bool addEntry = true)
128 {
129 ActionCallback copyCallback;
130 if (File.Exists(source)) copyCallback = File.Copy;
131 else if (Directory.Exists(source)) copyCallback = CopyPath;
132 else return false;
133
134 if (addEntry) AddTargetPair(target);
135 copyCallback(source, target);
136 return true;
137 }
138
139 static void CopyActionAddMeta(string source, string target)
140 {
141 CopyAction(source, target);
142 AddTargetPair(target + ".meta");
143 }
144
145 static void AddActionAddMeta(string target)
146 {
147 AddTargetPair(target);
148 AddTargetPair(target + ".meta");
149 }
150
155 public static void BuildLibraryPlatforms(string platform)
156 {
157 List<string> platforms = new List<string>(){ "windows", "macos", "linux", "android", "ios", "setup" };
158 platforms.Remove(platform);
159 foreach (string source in Directory.GetDirectories(LLMUnitySetup.libraryPath))
160 {
161 string sourceName = Path.GetFileName(source);
162 foreach (string platformPrefix in platforms)
163 {
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);
167 if (move)
168 {
169 string target = Path.Combine(BuildTempDir, sourceName);
170 MoveAction(source, target);
171 MoveAction(source + ".meta", target + ".meta");
172 }
173 }
174 }
175
176 if (platform == "android" || platform == "ios")
177 {
178 string pluginDir = platform == "android"? androidPluginDir: iOSPluginDir;
179 string source = Path.Combine(LLMUnitySetup.libraryPath, platform);
180 string target = Path.Combine(pluginDir, LLMUnitySetup.libraryName);
181 MoveAction(source, target);
182 MoveAction(source + ".meta", target + ".meta");
183 AddActionAddMeta(pluginDir);
184 }
185 }
186
190 public static void BuildModels()
191 {
192 LLMManager.Build(CopyActionAddMeta);
193 if (File.Exists(LLMUnitySetup.LLMManagerPath)) AddActionAddMeta(LLMUnitySetup.LLMManagerPath);
194 }
195
199 public static void Build(string platform)
200 {
201 DeletePath(BuildTempDir);
202 Directory.CreateDirectory(BuildTempDir);
203 BuildLibraryPlatforms(platform);
204 BuildModels();
205 }
206
210 public static void Reset()
211 {
212 if (!File.Exists(movedCache)) return;
213 List<StringPair> movedPairs = JsonUtility.FromJson<ListStringPair>(File.ReadAllText(movedCache)).pairs;
214 if (movedPairs == null) return;
215
216 bool refresh = false;
217 foreach (var pair in movedPairs)
218 {
219 if (pair.source == "") refresh |= DeletePath(pair.target);
220 else refresh |= MoveAction(pair.target, pair.source, false);
221 }
222 if (refresh) AssetDatabase.Refresh();
223 DeletePath(movedCache);
224 }
225 }
226}
227#endif
Class implementing the LLMUnity builder.
Definition LLMBuilder.cs:16
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.
Definition LLMBuilder.cs:79
static bool DeletePath(string path)
Deletes a path after checking if we are allowed to.
Definition LLMBuilder.cs:89
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.
Definition LLMBuilder.cs:69
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.
Definition LLMBuilder.cs:35
static void CopyWithOverwrite(string source, string target)
Overwrites a target file based on the source file.
Definition LLMBuilder.cs:59
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.