Skip to content

Commit a1a0b71

Browse files
committed
improve build process
1 parent ee7c6ce commit a1a0b71

File tree

5 files changed

+209
-148
lines changed

5 files changed

+209
-148
lines changed

Editor/LLMBuildProcessor.cs

+25-117
Original file line numberDiff line numberDiff line change
@@ -2,150 +2,58 @@
22
using UnityEditor.Build;
33
using UnityEditor.Build.Reporting;
44
using UnityEngine;
5-
using System.IO;
6-
using System.Collections.Generic;
7-
using System;
85

96
namespace LLMUnity
107
{
11-
public class LLMBuildProcessor : MonoBehaviour, IPreprocessBuildWithReport, IPostprocessBuildWithReport
8+
public class LLMBuildProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport
129
{
1310
public int callbackOrder => 0;
14-
static string tempDir = Path.Combine(Application.temporaryCachePath, "LLMBuildProcessor", Path.GetFileName(LLMUnitySetup.libraryPath));
15-
static List<MovedPair> movedPairs = new List<MovedPair>();
16-
static string movedCache = Path.Combine(tempDir, "moved.json");
1711

18-
[InitializeOnLoadMethod]
19-
private static void InitializeOnLoad()
20-
{
21-
if (!Directory.Exists(tempDir)) Directory.CreateDirectory(tempDir);
22-
else ResetMoves();
23-
}
24-
25-
// CALLED BEFORE THE BUILD
12+
// called before the build
2613
public void OnPreprocessBuild(BuildReport report)
2714
{
28-
// Start listening for errors when build starts
2915
Application.logMessageReceived += OnBuildError;
30-
HideLibraryPlatforms(report.summary.platform);
31-
HideModels();
32-
if (movedPairs.Count > 0) AssetDatabase.Refresh();
33-
}
34-
35-
// CALLED DURING BUILD TO CHECK FOR ERRORS
36-
private void OnBuildError(string condition, string stacktrace, LogType type)
37-
{
38-
if (type == LogType.Error)
39-
{
40-
// FAILED TO BUILD, STOP LISTENING FOR ERRORS
41-
BuildCompleted();
42-
}
43-
}
44-
45-
// CALLED AFTER THE BUILD
46-
public void OnPostprocessBuild(BuildReport report)
47-
{
48-
BuildCompleted();
49-
}
50-
51-
public void BuildCompleted()
52-
{
53-
Application.logMessageReceived -= OnBuildError;
54-
ResetMoves();
55-
}
56-
57-
static bool MovePath(string source, string target)
58-
{
59-
bool moved = false;
60-
if (File.Exists(source))
61-
{
62-
File.Move(source, target);
63-
moved = true;
64-
}
65-
else if (Directory.Exists(source))
66-
{
67-
Directory.Move(source, target);
68-
moved = true;
69-
}
70-
if (moved)
71-
{
72-
movedPairs.Add(new MovedPair {source = source, target = target});
73-
File.WriteAllText(movedCache, JsonUtility.ToJson(new FoldersMovedWrapper { movedPairs = movedPairs }));
74-
}
75-
return moved;
76-
}
77-
78-
static void MoveAssetAndMeta(string source, string target)
79-
{
80-
MovePath(source + ".meta", target + ".meta");
81-
MovePath(source, target);
82-
}
83-
84-
static void HideLibraryPlatforms(BuildTarget buildPlatform)
85-
{
86-
List<string> platforms = new List<string>(){ "windows", "macos", "linux", "android" };
87-
switch (buildPlatform)
16+
string platform = null;
17+
switch (report.summary.platform)
8818
{
8919
case BuildTarget.StandaloneWindows:
9020
case BuildTarget.StandaloneWindows64:
91-
platforms.Remove("windows");
21+
platform = "windows";
9222
break;
9323
case BuildTarget.StandaloneLinux64:
94-
platforms.Remove("linux");
24+
platform = "linux";
9525
break;
9626
case BuildTarget.StandaloneOSX:
97-
platforms.Remove("macos");
27+
platform = "macos";
9828
break;
9929
case BuildTarget.Android:
100-
platforms.Remove("android");
30+
platform = "android";
31+
break;
32+
case BuildTarget.iOS:
33+
platform = "ios";
10134
break;
10235
}
103-
104-
foreach (string dirname in Directory.GetDirectories(LLMUnitySetup.libraryPath))
105-
{
106-
foreach (string platform in platforms)
107-
{
108-
if (Path.GetFileName(dirname).StartsWith(platform))
109-
{
110-
MoveAssetAndMeta(dirname, Path.Combine(tempDir, Path.GetFileName(dirname)));
111-
}
112-
}
113-
}
36+
LLMBuilder.HideLibraryPlatforms(platform);
37+
LLMBuilder.CopyModels();
38+
AssetDatabase.Refresh();
11439
}
11540

116-
static void HideModels()
41+
// called during build to check for errors
42+
private void OnBuildError(string condition, string stacktrace, LogType type)
11743
{
118-
foreach (LLM llm in FindObjectsOfType<LLM>())
119-
{
120-
// if (!llm.downloadOnBuild) continue;
121-
// if (llm.modelURL != "") MoveAssetAndMeta(LLMUnitySetup.GetAssetPath(llm.model), Path.Combine(tempDir, Path.GetFileName(llm.model)));
122-
if (llm.loraURL != "") MoveAssetAndMeta(LLMUnitySetup.GetAssetPath(llm.lora), Path.Combine(tempDir, Path.GetFileName(llm.lora)));
123-
}
44+
if (type == LogType.Error) BuildCompleted();
12445
}
12546

126-
static void ResetMoves()
47+
// called after the build
48+
public void OnPostprocessBuild(BuildReport report)
12749
{
128-
if (!File.Exists(movedCache)) return;
129-
List<MovedPair> movedPairs = JsonUtility.FromJson<FoldersMovedWrapper>(File.ReadAllText(movedCache)).movedPairs;
130-
if (movedPairs == null) return;
131-
132-
bool refresh = false;
133-
foreach (var pair in movedPairs) refresh |= MovePath(pair.target, pair.source);
134-
if (refresh) AssetDatabase.Refresh();
135-
File.Delete(movedCache);
50+
BuildCompleted();
13651
}
137-
}
13852

139-
[Serializable]
140-
public struct MovedPair
141-
{
142-
public string source;
143-
public string target;
144-
}
145-
146-
[Serializable]
147-
public class FoldersMovedWrapper
148-
{
149-
public List<MovedPair> movedPairs;
53+
public void BuildCompleted()
54+
{
55+
Application.logMessageReceived -= OnBuildError;
56+
LLMBuilder.Reset();
57+
}
15058
}
15159
}

Editor/LLMEditor.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void AddModelLoaders(SerializedObject llmScriptSO, LLM llmScript)
6363
}
6464
EditorGUILayout.EndHorizontal();
6565
}
66-
AddLoadButtons();
66+
_ = AddLoadButtons();
6767
bool downloadOnStart = EditorGUILayout.Toggle("Download on Start", LLMManager.downloadOnStart);
6868
if (downloadOnStart != LLMManager.downloadOnStart)
6969
{
@@ -92,8 +92,8 @@ static void ResetModelOptions()
9292
{
9393
List<string> existingOptions = new List<string>();
9494
foreach (ModelEntry entry in LLMManager.modelEntries) existingOptions.Add(entry.url);
95-
modelOptions = new List<string>();
96-
modelURLs = new List<string>();
95+
modelOptions = new List<string>(){"Download model", "Custom URL"};
96+
modelURLs = new List<string>(){null, null};
9797
foreach ((string name, string url) in LLMUnitySetup.modelOptions)
9898
{
9999
if (url != null && existingOptions.Contains(url)) continue;

Runtime/LLMBuilder.cs

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
using System.IO;
4+
using System.Collections.Generic;
5+
using System;
6+
7+
#if UNITY_EDITOR
8+
namespace LLMUnity
9+
{
10+
public class LLMBuilder
11+
{
12+
static List<MovedPair> movedPairs = new List<MovedPair>();
13+
static string movedCache = Path.Combine(LLMUnitySetup.buildTempDir, "moved.json");
14+
15+
[InitializeOnLoadMethod]
16+
private static void InitializeOnLoad()
17+
{
18+
Directory.CreateDirectory(LLMUnitySetup.buildTempDir);
19+
Reset();
20+
}
21+
22+
public delegate void ActionCallback(string source, string target);
23+
24+
static void AddMovedPair(string source, string target)
25+
{
26+
movedPairs.Add(new MovedPair {source = source, target = target});
27+
File.WriteAllText(movedCache, JsonUtility.ToJson(new FoldersMovedWrapper { movedPairs = movedPairs }, true));
28+
}
29+
30+
static bool MoveAction(string source, string target, bool addEntry = true)
31+
{
32+
ActionCallback moveCallback;
33+
if (File.Exists(source)) moveCallback = File.Move;
34+
else if (Directory.Exists(source)) moveCallback = LLMUnitySetup.MovePath;
35+
else return false;
36+
37+
if (addEntry) AddMovedPair(source, target);
38+
moveCallback(source, target);
39+
return true;
40+
}
41+
42+
static bool CopyAction(string source, string target, bool addEntry = true)
43+
{
44+
ActionCallback copyCallback;
45+
if (File.Exists(source)) copyCallback = File.Copy;
46+
else if (Directory.Exists(source)) copyCallback = LLMUnitySetup.CopyPath;
47+
else return false;
48+
49+
if (addEntry) AddMovedPair("", target);
50+
copyCallback(source, target);
51+
return true;
52+
}
53+
54+
static bool DeleteAction(string source)
55+
{
56+
return LLMUnitySetup.DeletePath(source);
57+
}
58+
59+
public static void HideLibraryPlatforms(string platform)
60+
{
61+
List<string> platforms = new List<string>(){ "windows", "macos", "linux", "android", "ios" };
62+
platforms.Remove(platform);
63+
foreach (string source in Directory.GetDirectories(LLMUnitySetup.libraryPath))
64+
{
65+
foreach (string platformPrefix in platforms)
66+
{
67+
if (Path.GetFileName(source).StartsWith(platformPrefix))
68+
{
69+
string target = Path.Combine(LLMUnitySetup.buildTempDir, Path.GetFileName(source));
70+
MoveAction(source, target);
71+
MoveAction(source + ".meta", target + ".meta");
72+
}
73+
}
74+
}
75+
}
76+
77+
public static void CopyModels()
78+
{
79+
if (LLMManager.downloadOnStart) return;
80+
foreach (ModelEntry modelEntry in LLMManager.modelEntries)
81+
{
82+
string source = modelEntry.path;
83+
string target = LLMUnitySetup.GetAssetPath(modelEntry.filename);
84+
if (!modelEntry.includeInBuild || File.Exists(target)) continue;
85+
CopyAction(source, target);
86+
AddMovedPair("", target + ".meta");
87+
}
88+
}
89+
90+
public static void Reset()
91+
{
92+
if (!File.Exists(movedCache)) return;
93+
List<MovedPair> movedPairs = JsonUtility.FromJson<FoldersMovedWrapper>(File.ReadAllText(movedCache)).movedPairs;
94+
if (movedPairs == null) return;
95+
96+
bool refresh = false;
97+
foreach (var pair in movedPairs)
98+
{
99+
if (pair.source == "") refresh |= DeleteAction(pair.target);
100+
else refresh |= MoveAction(pair.target, pair.source, false);
101+
}
102+
if (refresh) AssetDatabase.Refresh();
103+
LLMUnitySetup.DeletePath(movedCache);
104+
}
105+
}
106+
107+
[Serializable]
108+
public struct MovedPair
109+
{
110+
public string source;
111+
public string target;
112+
}
113+
114+
[Serializable]
115+
public class FoldersMovedWrapper
116+
{
117+
public List<MovedPair> movedPairs;
118+
}
119+
}
120+
#endif

Runtime/LLMBuilder.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)