|
2 | 2 | using UnityEditor.Build;
|
3 | 3 | using UnityEditor.Build.Reporting;
|
4 | 4 | using UnityEngine;
|
5 |
| -using System.IO; |
6 |
| -using System.Collections.Generic; |
7 |
| -using System; |
8 | 5 |
|
9 | 6 | namespace LLMUnity
|
10 | 7 | {
|
11 |
| - public class LLMBuildProcessor : MonoBehaviour, IPreprocessBuildWithReport, IPostprocessBuildWithReport |
| 8 | + public class LLMBuildProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport |
12 | 9 | {
|
13 | 10 | 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"); |
17 | 11 |
|
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 |
26 | 13 | public void OnPreprocessBuild(BuildReport report)
|
27 | 14 | {
|
28 |
| - // Start listening for errors when build starts |
29 | 15 | 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) |
88 | 18 | {
|
89 | 19 | case BuildTarget.StandaloneWindows:
|
90 | 20 | case BuildTarget.StandaloneWindows64:
|
91 |
| - platforms.Remove("windows"); |
| 21 | + platform = "windows"; |
92 | 22 | break;
|
93 | 23 | case BuildTarget.StandaloneLinux64:
|
94 |
| - platforms.Remove("linux"); |
| 24 | + platform = "linux"; |
95 | 25 | break;
|
96 | 26 | case BuildTarget.StandaloneOSX:
|
97 |
| - platforms.Remove("macos"); |
| 27 | + platform = "macos"; |
98 | 28 | break;
|
99 | 29 | case BuildTarget.Android:
|
100 |
| - platforms.Remove("android"); |
| 30 | + platform = "android"; |
| 31 | + break; |
| 32 | + case BuildTarget.iOS: |
| 33 | + platform = "ios"; |
101 | 34 | break;
|
102 | 35 | }
|
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(); |
114 | 39 | }
|
115 | 40 |
|
116 |
| - static void HideModels() |
| 41 | + // called during build to check for errors |
| 42 | + private void OnBuildError(string condition, string stacktrace, LogType type) |
117 | 43 | {
|
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(); |
124 | 45 | }
|
125 | 46 |
|
126 |
| - static void ResetMoves() |
| 47 | + // called after the build |
| 48 | + public void OnPostprocessBuild(BuildReport report) |
127 | 49 | {
|
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(); |
136 | 51 | }
|
137 |
| - } |
138 | 52 |
|
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 | + } |
150 | 58 | }
|
151 | 59 | }
|
0 commit comments