Spaces:
webzepetoku
/
Sleeping

File size: 11,339 Bytes
b59f1b8
 
19b4775
7566482
b59f1b8
 
989958a
985821c
b59f1b8
6d095c9
b59f1b8
 
 
7566482
989958a
7dc9f14
989958a
34a2a79
19b4775
f2fcb41
457ad9a
f2fcb41
 
 
 
 
 
457ad9a
f2fcb41
a52ba91
f2fcb41
 
985821c
f2fcb41
a52ba91
 
f2fcb41
 
7566482
a52ba91
 
f2fcb41
34a2a79
71c3091
f2fcb41
 
 
 
 
 
 
 
 
1d57ee9
f2fcb41
 
 
 
 
 
 
 
 
 
34a2a79
f2fcb41
985821c
71c3091
7566482
e8e3076
985821c
f2fcb41
7566482
985821c
 
7dc9f14
985821c
 
 
 
 
 
ddc8ca2
f2fcb41
ddc8ca2
f2fcb41
ddc8ca2
7566482
faeb282
 
7566482
34a2a79
faeb282
7566482
faeb282
 
 
7566482
faeb282
 
7566482
 
34a2a79
1d57ee9
ddc8ca2
 
 
 
 
 
 
 
 
 
 
 
7dc9f14
ddc8ca2
 
faeb282
 
 
 
 
f2fcb41
 
faeb282
ddc8ca2
 
f2fcb41
985821c
ddc8ca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2fcb41
7dc9f14
faeb282
 
985821c
 
7dc9f14
 
f2fcb41
7dc9f14
985821c
ddc8ca2
985821c
ddc8ca2
985821c
7566482
f2a9cb6
 
7566482
f2a9cb6
7dc9f14
f2a9cb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34a2a79
7566482
7dc9f14
56741b1
7566482
6d095c9
989958a
f2fcb41
 
 
 
 
989958a
a52ba91
985821c
 
 
188ca2d
 
1d57ee9
f2fcb41
 
 
 
 
 
 
 
 
 
985821c
 
 
f2fcb41
 
 
985821c
7dc9f14
985821c
71c3091
f2fcb41
46f397f
b888a80
7dc9f14
e8e3076
7dc9f14
f2fcb41
 
 
 
 
 
 
 
 
 
 
 
 
985821c
f2fcb41
7566482
989958a
2e240ab
1d57ee9
19b4775
f2fcb41
 
7566482
989958a
b59f1b8
 
67f76e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using ZEPETO.Asset;
using UnityMeshSimplifier; // Pastikan library ada di folder Assets

public class TestBuilder
{
    public static void ManualConvert()
    {
        EditorApplication.LockReloadAssemblies();
        Debug.Log("===============================================================");
        Debug.Log("πŸ› οΈ [ZEPETO BUILDER] Smart Minifier & Texture (Original Material Guard)");
        Debug.Log("===============================================================");
        try
        {
            // --- KUNCI PENYELESAIAN FATAL ERROR (CRASH) ---
            string[] badPackages = Directory.GetFiles("Assets", "package.json", SearchOption.AllDirectories);
            foreach (string badPkg in badPackages)
            {
                File.Delete(badPkg);
                if (File.Exists(badPkg + ".meta")) File.Delete(badPkg + ".meta");
                Debug.Log($"πŸ—‘οΈ [FIX] Menghapus package.json nyasar: {badPkg}");
            }

            // 1. Parsing Argumen Command Line
            string[] args = Environment.GetCommandLineArgs();
            string z_id = "";
            string z_pw = "";
            int targetTris = 9900; 

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i] == "-id" && i + 1 < args.Length) z_id = args[i + 1];
                if (args[i] == "-password" && i + 1 < args.Length) z_pw = args[i + 1];
                if (args[i] == "-maxtris" && i + 1 < args.Length) targetTris = int.Parse(args[i + 1]);
            }

            // 2. Tentukan Path
            string inputPath = "Assets/InputRaw";
            string outputPath = "Assets/OptimizedOutput";

            if (!Directory.Exists(inputPath))
            {
                Debug.LogError($"❌ [BUILDER] Folder tidak ditemukan: {inputPath}");
                EditorApplication.UnlockReloadAssemblies();
                EditorApplication.Exit(1);
                return;
            }

            string[] prefabs = Directory.GetFiles(inputPath, "*.prefab", SearchOption.AllDirectories);
            if (prefabs.Length == 0)
            {
                Debug.LogError("❌ [BUILDER] Tidak ada file .prefab ditemukan.");
                EditorApplication.UnlockReloadAssemblies();
                EditorApplication.Exit(1);
                return;
            }

            string targetPrefabPath = prefabs[0];
            string prefabFileName = Path.GetFileName(targetPrefabPath);

            // 3. Buat Folder Output yang Bersih
            if (Directory.Exists(outputPath)) Directory.Delete(outputPath, true);
            Directory.CreateDirectory(outputPath);
            AssetDatabase.Refresh();

            // 4. Muat Prefab Asli dan Instansiasi ke Memori Scene
            GameObject originalPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(targetPrefabPath);
            GameObject instance = (GameObject)PrefabUtility.InstantiatePrefab(originalPrefab);
            
            // ====================================================================
            // --- TAHAP PERSIAPAN MEGA PREFAB (WADAH) ---
            // ====================================================================
            string finalPrefabPath = $"{outputPath}/{prefabFileName}";
            GameObject tempObj = new GameObject("TempContainer");
            PrefabUtility.SaveAsPrefabAsset(tempObj, finalPrefabPath);
            UnityEngine.Object.DestroyImmediate(tempObj);


            // ====================================================================
            // --- TAHAP SMART TEXTURE SCALER (Target Max 950 KB) ---
            // ====================================================================
            Debug.Log("πŸ–ΌοΈ [TEXTURE] Memulai Kalkulasi Smart Texture Scaler (Target: 950KB)...");
            var allRenderers = instance.GetComponentsInChildren<Renderer>(true);
            HashSet<Texture2D> uniqueTextures = new HashSet<Texture2D>();

            foreach (var rend in allRenderers)
            {
                foreach (var mat in rend.sharedMaterials)
                {
                    if (mat == null) continue;
                    string[] texNames = mat.GetTexturePropertyNames();
                    foreach (var tName in texNames)
                    {
                        Texture tex = mat.GetTexture(tName);
                        if (tex is Texture2D t2d) uniqueTextures.Add(t2d);
                    }
                }
            }

            long totalPixels = 0;
            foreach (var tex in uniqueTextures)
            {
                totalPixels += (tex.width * tex.height);
            }

            long maxSafePixels = 950 * 1024; 
            float textureScaleRatio = 1.0f;

            if (totalPixels > maxSafePixels)
            {
                textureScaleRatio = Mathf.Sqrt((float)maxSafePixels / (float)totalPixels);
                Debug.Log($"πŸ“‰ [TEXTURE MATH] Menerapkan Smart Scale Ratio: {textureScaleRatio:P2}");
            }

            foreach (var tex in uniqueTextures)
            {
                string texPath = AssetDatabase.GetAssetPath(tex);
                if (string.IsNullOrEmpty(texPath)) continue;

                TextureImporter importer = AssetImporter.GetAtPath(texPath) as TextureImporter;
                if (importer != null)
                {
                    int currentMaxDim = Mathf.Max(tex.width, tex.height);
                    
                    importer.mipmapEnabled = false;
                    importer.textureCompression = TextureImporterCompression.CompressedLQ;

                    if (textureScaleRatio < 1.0f)
                    {
                        int targetDim = (int)(currentMaxDim * textureScaleRatio);
                        int powerOf2 = 32;
                        while (powerOf2 * 2 <= targetDim && powerOf2 < 2048)
                        {
                            powerOf2 *= 2;
                        }
                        importer.maxTextureSize = powerOf2;
                    }
                    else
                    {
                        importer.maxTextureSize = Mathf.NextPowerOfTwo(currentMaxDim);
                    }
                    
                    importer.SaveAndReimport();
                    Debug.Log($"βœ… [TEXTURE] {tex.name} divalidasi pada resolusi: {importer.maxTextureSize}px");
                }
            }

            // ====================================================================
            // --- TAHAP PENJAGAAN MATERIAL BAWAAN ---
            // Kita biarkan material terhubung ke file .mat aslinya. Tidak ada kloning!
            // ====================================================================
            Debug.Log("🎨 [MATERIAL] Material bawaan & Custom Shader dipertahankan sepenuhnya.");


            // ====================================================================
            // --- TAHAP SMART MINIFIER POLIGON + EMBEDDING MESH ---
            // ====================================================================
            var skinnedRenderers = instance.GetComponentsInChildren<SkinnedMeshRenderer>(true);
            var meshFilters = instance.GetComponentsInChildren<MeshFilter>(true);
            
            int totalOriginalTris = 0;

            // BYPASS DRM ZEPETO: Menghitung poligon dengan GetIndexCount
            foreach (var smr in skinnedRenderers)
            {
                if (smr.sharedMesh != null)
                {
                    for (int i = 0; i < smr.sharedMesh.subMeshCount; i++)
                    {
                        totalOriginalTris += (int)(smr.sharedMesh.GetIndexCount(i) / 3);
                    }
                }
            }

            foreach (var mf in meshFilters)
            {
                if (mf.sharedMesh != null)
                {
                    for (int i = 0; i < mf.sharedMesh.subMeshCount; i++)
                    {
                        totalOriginalTris += (int)(mf.sharedMesh.GetIndexCount(i) / 3);
                    }
                }
            }

            float quality = (totalOriginalTris > targetTris) ? (float)targetTris / (float)totalOriginalTris : 1.0f;
            Debug.Log($"πŸ“Š [MESH] Poligon Asli: {totalOriginalTris} tris | Target: {targetTris} | Rasio: {quality}");

            foreach (var smr in skinnedRenderers)
            {
                if (smr.sharedMesh == null) continue;
                var meshSimplifier = new UnityMeshSimplifier.MeshSimplifier();
                meshSimplifier.Initialize(smr.sharedMesh);
                meshSimplifier.SimplifyMesh(quality); 
                Mesh optimizedMesh = meshSimplifier.ToMesh();

                if (optimizedMesh != null)
                {
                    optimizedMesh.name = smr.sharedMesh.name + "_embedded";
                    AssetDatabase.AddObjectToAsset(optimizedMesh, finalPrefabPath);
                    smr.sharedMesh = optimizedMesh;
                }
            }

            foreach (var mf in meshFilters)
            {
                if (mf.sharedMesh == null) continue;
                var meshSimplifier = new UnityMeshSimplifier.MeshSimplifier();
                meshSimplifier.Initialize(mf.sharedMesh);
                meshSimplifier.SimplifyMesh(quality); 
                Mesh optimizedMesh = meshSimplifier.ToMesh();

                if (optimizedMesh != null)
                {
                    optimizedMesh.name = mf.sharedMesh.name + "_embedded";
                    AssetDatabase.AddObjectToAsset(optimizedMesh, finalPrefabPath);
                    mf.sharedMesh = optimizedMesh;
                }
            }

            // ====================================================================
            // --- FINALISASI & PACKING ---
            // ====================================================================
            GameObject optimizedPrefab = PrefabUtility.SaveAsPrefabAsset(instance, finalPrefabPath);
            UnityEngine.Object.DestroyImmediate(instance); 
            AssetDatabase.SaveAssets();

            Debug.Log($"βœ… [BUILDER] Prefab siap dengan mesh terpadatkan dan material bawaan utuh!");

            Debug.Log("πŸš€ [PACKING] Menjalankan ZepetoAssetPackage.Pack...");
            var result = new StringBuilder();
            
            var statistics = ZepetoAssetBundleInfo.Create(optimizedPrefab);
            if (statistics != null)
            {
                result.AppendLine("[ZEPETO STUDIO ARCHIVE RESULT]");
                result.AppendLine(JsonUtility.ToJson(statistics, true));
            }

            var packResult = ZepetoAssetPackage.Pack(optimizedPrefab);
            result.Append(packResult);

            Debug.Log(result.ToString());
            Debug.Log("βœ… [BUILDER] Seluruh proses konversi sukses sempurna.");
            
            EditorApplication.UnlockReloadAssemblies();
            EditorApplication.Exit(0);
        }
        catch (Exception e)
        {
            Debug.LogError($"πŸ”₯ [BUILDER] Error: {e.Message}");
            Debug.LogError($"StackTrace: {e.StackTrace}");
            EditorApplication.UnlockReloadAssemblies();
            EditorApplication.Exit(1);
        }
    }
}