LLM for Unity  v2.2.5
Create characters in Unity with LLMs!
Loading...
Searching...
No Matches
LLMBuilder.cs
1using UnityEditor;
2using UnityEngine;
3using System.IO;
4using System.Collections.Generic;
5
6#if UNITY_EDITOR
7namespace LLMUnity
8{
9 public class LLMBuilder
10 {
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");
14
15 [InitializeOnLoadMethod]
16 private static void InitializeOnLoad()
17 {
18 Reset();
19 }
20
21 public static void HandleActionFileRecursive(string source, string target, ActionCallback actionCallback)
22 {
23 if (File.Exists(source))
24 {
25 actionCallback(source, target);
26 }
27 else if (Directory.Exists(source))
28 {
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)
34 {
35 HandleActionFileRecursive(path, Path.Combine(target, Path.GetFileName(path)), actionCallback);
36 }
37 }
38 }
39
40 public static void CopyWithOverwrite(string source, string target)
41 {
42 File.Copy(source, target, true);
43 }
44
45 public static void CopyPath(string source, string target)
46 {
47 HandleActionFileRecursive(source, target, CopyWithOverwrite);
48 }
49
50 public static void MovePath(string source, string target)
51 {
52 HandleActionFileRecursive(source, target, File.Move);
53 DeletePath(source);
54 }
55
56 public static bool DeletePath(string path)
57 {
58 if (!LLMUnitySetup.IsSubPath(path, LLMUnitySetup.GetAssetPath()) && !LLMUnitySetup.IsSubPath(path, BuildTempDir))
59 {
60 LLMUnitySetup.LogError($"Safeguard: {path} will not be deleted because it may not be safe");
61 return false;
62 }
63 if (File.Exists(path)) File.Delete(path);
64 else if (Directory.Exists(path)) Directory.Delete(path, true);
65 return true;
66 }
67
68 static void AddMovedPair(string source, string target)
69 {
70 movedPairs.Add(new StringPair {source = source, target = target});
71 File.WriteAllText(movedCache, JsonUtility.ToJson(new ListStringPair { pairs = movedPairs }, true));
72 }
73
74 static void AddTargetPair(string target)
75 {
76 AddMovedPair("", target);
77 }
78
79 static bool MoveAction(string source, string target, bool addEntry = true)
80 {
81 ActionCallback moveCallback;
82 if (File.Exists(source)) moveCallback = File.Move;
83 else if (Directory.Exists(source)) moveCallback = MovePath;
84 else return false;
85
86 if (addEntry) AddMovedPair(source, target);
87 moveCallback(source, target);
88 return true;
89 }
90
91 static bool CopyAction(string source, string target, bool addEntry = true)
92 {
93 ActionCallback copyCallback;
94 if (File.Exists(source)) copyCallback = File.Copy;
95 else if (Directory.Exists(source)) copyCallback = CopyPath;
96 else return false;
97
98 if (addEntry) AddTargetPair(target);
99 copyCallback(source, target);
100 return true;
101 }
102
103 static void CopyActionAddMeta(string source, string target)
104 {
105 CopyAction(source, target);
106 AddTargetPair(target + ".meta");
107 }
108
109 static void AddActionAddMeta(string target)
110 {
111 AddTargetPair(target);
112 AddTargetPair(target + ".meta");
113 }
114
115 public static void HideLibraryPlatforms(string platform)
116 {
117 List<string> platforms = new List<string>(){ "windows", "macos", "linux", "android", "ios", "setup" };
118 platforms.Remove(platform);
119 foreach (string source in Directory.GetDirectories(LLMUnitySetup.libraryPath))
120 {
121 string sourceName = Path.GetFileName(source);
122 foreach (string platformPrefix in platforms)
123 {
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);
127 if (move)
128 {
129 string target = Path.Combine(BuildTempDir, sourceName);
130 MoveAction(source, target);
131 MoveAction(source + ".meta", target + ".meta");
132 }
133 }
134 }
135 }
136
137 public static void BuildModels()
138 {
139 LLMManager.Build(CopyActionAddMeta);
140 if (File.Exists(LLMUnitySetup.LLMManagerPath)) AddActionAddMeta(LLMUnitySetup.LLMManagerPath);
141 }
142
143 public static void Build(string platform)
144 {
145 Directory.CreateDirectory(BuildTempDir);
146 HideLibraryPlatforms(platform);
147 BuildModels();
148 }
149
150 public static void Reset()
151 {
152 if (!File.Exists(movedCache)) return;
153 List<StringPair> movedPairs = JsonUtility.FromJson<ListStringPair>(File.ReadAllText(movedCache)).pairs;
154 if (movedPairs == null) return;
155
156 foreach (var pair in movedPairs)
157 {
158 if (pair.source == "") DeletePath(pair.target);
159 else MoveAction(pair.target, pair.source, false);
160 }
161 DeletePath(movedCache);
162 }
163 }
164}
165#endif
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.