LLM for Unity  v2.3.0
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 static string movedCache = Path.Combine(BuildTempDir, "moved.json");
20
21 [InitializeOnLoadMethod]
22 private static void InitializeOnLoad()
23 {
24 Reset();
25 }
26
33 public static void HandleActionFileRecursive(string source, string target, ActionCallback actionCallback)
34 {
35 if (File.Exists(source))
36 {
37 actionCallback(source, target);
38 }
39 else if (Directory.Exists(source))
40 {
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)
46 {
47 HandleActionFileRecursive(path, Path.Combine(target, Path.GetFileName(path)), actionCallback);
48 }
49 }
50 }
51
57 public static void CopyWithOverwrite(string source, string target)
58 {
59 File.Copy(source, target, true);
60 }
61
67 public static void CopyPath(string source, string target)
68 {
70 }
71
77 public static void MovePath(string source, string target)
78 {
79 HandleActionFileRecursive(source, target, File.Move);
80 DeletePath(source);
81 }
82
87 public static bool DeletePath(string path)
88 {
89 if (!LLMUnitySetup.IsSubPath(path, LLMUnitySetup.GetAssetPath()) && !LLMUnitySetup.IsSubPath(path, BuildTempDir))
90 {
91 LLMUnitySetup.LogError($"Safeguard: {path} will not be deleted because it may not be safe");
92 return false;
93 }
94 if (File.Exists(path)) File.Delete(path);
95 else if (Directory.Exists(path)) Directory.Delete(path, true);
96 return true;
97 }
98
99 static void AddMovedPair(string source, string target)
100 {
101 movedPairs.Add(new StringPair {source = source, target = target});
102 File.WriteAllText(movedCache, JsonUtility.ToJson(new ListStringPair { pairs = movedPairs }, true));
103 }
104
105 static void AddTargetPair(string target)
106 {
107 AddMovedPair("", target);
108 }
109
110 static bool MoveAction(string source, string target, bool addEntry = true)
111 {
112 ActionCallback moveCallback;
113 if (File.Exists(source)) moveCallback = File.Move;
114 else if (Directory.Exists(source)) moveCallback = MovePath;
115 else return false;
116
117 if (addEntry) AddMovedPair(source, target);
118 moveCallback(source, target);
119 return true;
120 }
121
122 static bool CopyAction(string source, string target, bool addEntry = true)
123 {
124 ActionCallback copyCallback;
125 if (File.Exists(source)) copyCallback = File.Copy;
126 else if (Directory.Exists(source)) copyCallback = CopyPath;
127 else return false;
128
129 if (addEntry) AddTargetPair(target);
130 copyCallback(source, target);
131 return true;
132 }
133
134 static void CopyActionAddMeta(string source, string target)
135 {
136 CopyAction(source, target);
137 AddTargetPair(target + ".meta");
138 }
139
140 static void AddActionAddMeta(string target)
141 {
142 AddTargetPair(target);
143 AddTargetPair(target + ".meta");
144 }
145
150 public static void HideLibraryPlatforms(string platform)
151 {
152 List<string> platforms = new List<string>(){ "windows", "macos", "linux", "android", "ios", "setup" };
153 platforms.Remove(platform);
154 foreach (string source in Directory.GetDirectories(LLMUnitySetup.libraryPath))
155 {
156 string sourceName = Path.GetFileName(source);
157 foreach (string platformPrefix in platforms)
158 {
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);
162 if (move)
163 {
164 string target = Path.Combine(BuildTempDir, sourceName);
165 MoveAction(source, target);
166 MoveAction(source + ".meta", target + ".meta");
167 }
168 }
169 }
170 }
171
175 public static void BuildModels()
176 {
177 LLMManager.Build(CopyActionAddMeta);
178 if (File.Exists(LLMUnitySetup.LLMManagerPath)) AddActionAddMeta(LLMUnitySetup.LLMManagerPath);
179 }
180
184 public static void Build(string platform)
185 {
186 Directory.CreateDirectory(BuildTempDir);
187 HideLibraryPlatforms(platform);
188 BuildModels();
189 }
190
194 public static void Reset()
195 {
196 if (!File.Exists(movedCache)) return;
197 List<StringPair> movedPairs = JsonUtility.FromJson<ListStringPair>(File.ReadAllText(movedCache)).pairs;
198 if (movedPairs == null) return;
199
200 bool refresh = false;
201 foreach (var pair in movedPairs)
202 {
203 if (pair.source == "") refresh |= DeletePath(pair.target);
204 else refresh |= MoveAction(pair.target, pair.source, false);
205 }
206 if (refresh) AssetDatabase.Refresh();
207 DeletePath(movedCache);
208 }
209 }
210}
211#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:77
static bool DeletePath(string path)
Deletes a path after checking if we are allowed to.
Definition LLMBuilder.cs:87
static void BuildModels()
Bundles the model information.
static void CopyPath(string source, string target)
Copies a source file to a target file.
Definition LLMBuilder.cs:67
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:33
static void CopyWithOverwrite(string source, string target)
Overwrites a target file based on the source file.
Definition LLMBuilder.cs:57
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.