三国卡牌客户端基础资源仓库
yyl
10 小时以前 87aca7f87b4dc67999ba81ed61258f4ce7d6ce35
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
 
namespace ProjSG.Editor
{
    /// <summary>
    /// 抖音小游戏(TTSDK 6.8.0)构建后自动修复。
    ///
    /// 背景:
    /// 1. 本项目固定 CDN 模式(dataLoadType: 0),首包数据部署在 DATA_CDN 上。
    ///    但 TTSDK 构建工具在生成 tt-minigame/tt-minigame/game.js 时,
    ///    会把 loadDataPackageFromSubpackage 写成 true(无论模板/dataLoadType 如何),
    ///    导致引擎去空的 data-package 子包找首包数据,报 "processDataJob: unknown data format"。
    /// 2. TTSDK 生成的子包 wasmcode/game.js 与 data-package/game.js 是 0 字节空文件,
    ///    真机会报 "not loaded pkg:game.js",必须补成 module.exports = {};。
    ///
    /// 用法:
    /// - 构建 WebGL(抖音小游戏)完成后自动执行(IPostprocessBuildWithReport + 延迟重试)。
    /// - 也可手动执行菜单 Tools/抖音小游戏/修复 game.js。
    /// </summary>
    public class DouyinBuildPostFixer : IPostprocessBuildWithReport
    {
        public int callbackOrder => 9999; // 尽量晚执行
 
        private static readonly UTF8Encoding Utf8NoBom = new UTF8Encoding(false);
        private const int MaxRetry = 20; // 最多重试 20 次(每次间隔 1 帧),应对转换异步的情况
 
        // ---------------------------------------------------------------
        // 自动 Hook
        // ---------------------------------------------------------------
        public void OnPostprocessBuild(BuildReport report)
        {
            bool isMiniGame = report.summary.platform == BuildTarget.WebGL;
            if (!isMiniGame)
            {
                var outputPath = report.summary.outputPath ?? string.Empty;
                isMiniGame = outputPath.IndexOf("tt-minigame", StringComparison.OrdinalIgnoreCase) >= 0;
            }
 
            if (!isMiniGame)
                return;
 
            // TTSDK 转小游戏(生成 game.js)发生在 Unity WebGL 构建之后,
            // 且是同步的,所以把修复延后到下一帧,确保产物已落盘。
            EditorApplication.delayCall += () => PatchWithRetry(0);
        }
 
        [MenuItem("Tools/抖音小游戏/修复 game.js(构建后自动/手动)")]
        public static void PatchMenu()
        {
            PatchWithRetry(0);
        }
 
        // ---------------------------------------------------------------
        // 修复入口
        // ---------------------------------------------------------------
        private static void PatchWithRetry(int attempt)
        {
            var miniRoot = GetMiniGameRoot();
 
            // 主 game.js 还没生成(构建尚未完成/转换异步),稍后重试
            if (!File.Exists(Path.Combine(miniRoot, "game.js")))
            {
                if (attempt < MaxRetry)
                {
                    var next = attempt + 1;
                    EditorApplication.delayCall += () => PatchWithRetry(next);
                }
                else
                {
                    Debug.LogWarning("[DouyinBuildPostFixer] 未找到构建产物 " + miniRoot +
                                     ",请确认已用 TTSDK 打包,或手动执行菜单 Tools/抖音小游戏/修复 game.js。");
                }
                return;
            }
 
            try
            {
                PatchAll(miniRoot);
            }
            catch (Exception e)
            {
                Debug.LogError("[DouyinBuildPostFixer] 修复失败: " + e);
            }
        }
 
        // ---------------------------------------------------------------
        // 实际修复逻辑
        // ---------------------------------------------------------------
        public static void PatchAll(string miniRoot)
        {
            PatchMainGameJs(Path.Combine(miniRoot, "game.js"));
            PatchSubpackageGameJs(Path.Combine(miniRoot, "wasmcode", "game.js"), "wasmcode");
            PatchSubpackageGameJs(Path.Combine(miniRoot, "data-package", "game.js"), "data-package");
            // wasm 分包(开发者工具「启用代码分包」)会生成 .dy-game-cache 缓存,里面的 wasmcode 子包
            // 可能缺 game.js,导致真机预览报 "not loaded pkg:game.js",同样补上。
            PatchSubpackageGameJs(Path.Combine(miniRoot, ".dy-game-cache", "wasmcode", "game.js"), ".dy-game-cache/wasmcode");
            PatchGameJson(Path.Combine(miniRoot, "game.json"));
            PatchLoadingImages(miniRoot);
            EnsureXingwanSdkFile(miniRoot);
            EnsureXingwanImport(Path.Combine(miniRoot, "game.js"));
            Debug.Log("[DouyinBuildPostFixer] 构建产物 game.js/game.json 修复完成。");
        }
 
        /// <summary>
        /// 主 game.js:loadDataPackageFromSubpackage 必须为 false(CDN 模式)。
        /// </summary>
        private static void PatchMainGameJs(string path)
        {
            if (!File.Exists(path))
            {
                Debug.LogWarning("[DouyinBuildPostFixer] 主 game.js 不存在: " + path);
                return;
            }
 
            string text = File.ReadAllText(path); // 自动识别 BOM
            string newText = Regex.Replace(
                text,
                @"loadDataPackageFromSubpackage\s*:\s*true",
                "loadDataPackageFromSubpackage: false");
 
            if (newText == text)
            {
                Debug.Log("[DouyinBuildPostFixer] 主 game.js 的 loadDataPackageFromSubpackage 已是 false,无需修改。");
                return;
            }
 
            File.WriteAllText(path, newText, Utf8NoBom);
            Debug.Log("[DouyinBuildPostFixer] 已修复主 game.js:loadDataPackageFromSubpackage true → false。");
        }
 
        /// <summary>
        /// 子包 game.js:0 字节 → module.exports = {};(已有内容则跳过,幂等)。
        /// </summary>
        private static void PatchSubpackageGameJs(string path, string subName)
        {
            Directory.CreateDirectory(Path.GetDirectoryName(path) ?? string.Empty);
 
            if (File.Exists(path) && new FileInfo(path).Length > 0)
            {
                Debug.Log("[DouyinBuildPostFixer] 子包 " + subName + "/game.js 已有内容,跳过。");
                return;
            }
 
            File.WriteAllText(path, "module.exports = {};\n", Utf8NoBom);
            Debug.Log("[DouyinBuildPostFixer] 已补全子包 " + subName + "/game.js:module.exports = {};");
        }
 
        /// <summary>
        /// game.json:补 networkTimeout,提高 tt.request 超时(抖音默认 60s,慢 CDN 下会超时)。
        /// </summary>
        private static void PatchGameJson(string path)
        {
            if (!File.Exists(path))
            {
                Debug.LogWarning("[DouyinBuildPostFixer] game.json 不存在: " + path);
                return;
            }
 
            string text = File.ReadAllText(path);
            if (text.Contains("\"networkTimeout\""))
            {
                Debug.Log("[DouyinBuildPostFixer] game.json 已有 networkTimeout,跳过。");
                return;
            }
 
            const string block =
                "  \"networkTimeout\": {\n" +
                "    \"request\": 300000,\n" +
                "    \"connectSocket\": 300000,\n" +
                "    \"uploadFile\": 300000,\n" +
                "    \"downloadFile\": 300000\n" +
                "  },\n";
 
            // 在 "subpackages" 前插入,若找不到则插到 "plugins" 前,再不行插到第一个 { 后。
            int idx = text.IndexOf("\"subpackages\"", StringComparison.Ordinal);
            if (idx < 0) idx = text.IndexOf("\"plugins\"", StringComparison.Ordinal);
            if (idx < 0)
            {
                idx = text.IndexOf('{');
                if (idx >= 0) idx += 1;
            }
            if (idx < 0)
            {
                Debug.LogWarning("[DouyinBuildPostFixer] game.json 结构异常,跳过 networkTimeout 补丁。");
                return;
            }
 
            text = text.Insert(idx, block);
            File.WriteAllText(path, text, Utf8NoBom);
            Debug.Log("[DouyinBuildPostFixer] 已为 game.json 补 networkTimeout(request/downloadFile = 300000ms)。");
        }
 
        /// <summary>
        /// 加载页图片:用工程内 Assets/Editor/DouyinLoading 下的自定义图覆盖产物 images 目录,
        /// 不修改 DefaultTemplate。background.png 为背景图、logo.png 为 LOGO 图标
        /// (产物里叫 unity_logo.png,保持文件名不变,game.js 无需改动)。源图不存在则跳过。
        /// </summary>
        private static void PatchLoadingImages(string miniRoot)
        {
            var sourceDir = Path.Combine(Application.dataPath, "Editor", "DouyinLoading");
            var targetDir = Path.Combine(miniRoot, "images");
 
            CopyIfExists(Path.Combine(sourceDir, "background.png"), Path.Combine(targetDir, "background.png"));
            CopyIfExists(Path.Combine(sourceDir, "logo.png"), Path.Combine(targetDir, "unity_logo.png"));
        }
 
        /// <summary>
        /// 星玩渠道 SDK:确保小游戏根目录存在 xingwanTTGameSdk.js。
        /// 构建工具可能未把 DefaultTemplate 下的自定义 js 拷进产物,这里兜底复制。
        /// </summary>
        private static void EnsureXingwanSdkFile(string miniRoot)
        {
            const string fileName = "xingwanTTGameSdk.js";
            var target = Path.Combine(miniRoot, fileName);
            if (File.Exists(target) && new FileInfo(target).Length > 0)
                return;
 
            var source = Path.Combine(Application.dataPath, "Plugins", "ByteGame",
                "com.bytedance.starksdk", "DefaultTemplate", fileName);
            if (!File.Exists(source))
            {
                Debug.LogWarning("[DouyinBuildPostFixer] 未找到星玩 SDK 源文件: " + source);
                return;
            }
 
            Directory.CreateDirectory(miniRoot);
            File.Copy(source, target, true);
            Debug.Log("[DouyinBuildPostFixer] 已复制星玩 SDK 到产物目录: " + fileName);
        }
 
        /// <summary>
        /// 星玩渠道 SDK:确保产物 game.js 已 import 并把 SDK 挂到全局作用域。
        /// 模板被还原/覆盖时自动补回,避免 jslib 找不到 SDK。
        /// </summary>
        private static void EnsureXingwanImport(string gameJsPath)
        {
            if (!File.Exists(gameJsPath))
                return;
 
            string text = File.ReadAllText(gameJsPath);
            if (text.Contains("xingwanTTGameSdk"))
                return;
 
            const string block =
                "// 星玩渠道 SDK 注入(构建后自动补回)\n" +
                "try {\n" +
                "  const xingwanModule = require('./xingwanTTGameSdk.js');\n" +
                "  const xingwanTTGameSdk = (xingwanModule && xingwanModule.default) || xingwanModule;\n" +
                "  GameGlobal.xingwanTTGameSdk = xingwanTTGameSdk;\n" +
                "  if (typeof globalThis !== 'undefined') globalThis.xingwanTTGameSdk = xingwanTTGameSdk;\n" +
                "  if (typeof window !== 'undefined') window.xingwanTTGameSdk = xingwanTTGameSdk;\n" +
                "} catch (e) {\n" +
                "  console.error('[Xingwan] 星玩 SDK 注入失败: ' + (e && e.message ? e.message : e));\n" +
                "}\n";
 
            File.WriteAllText(gameJsPath, block + text, Utf8NoBom);
            Debug.Log("[DouyinBuildPostFixer] 已为 game.js 补回星玩 SDK 注入代码。");
        }
 
        private static void CopyIfExists(string source, string target)
        {
            if (!File.Exists(source))
                return;
 
            Directory.CreateDirectory(Path.GetDirectoryName(target) ?? string.Empty);
            File.Copy(source, target, true);
            Debug.Log("[DouyinBuildPostFixer] 已覆盖加载页图片: " + Path.GetFileName(target) + " <- " + source);
        }
 
        // ---------------------------------------------------------------
        // 路径解析
        // ---------------------------------------------------------------
        private static string GetMiniGameRoot()
        {
            string outputRoot = ReadStarkOutputDir();
            if (string.IsNullOrEmpty(outputRoot))
            {
                outputRoot = Path.GetFullPath(Path.Combine(Application.dataPath, "..", "tt-minigame"));
            }
 
            return Path.Combine(outputRoot, "tt-minigame");
        }
 
        /// <summary>
        /// 从 Assets/Editor/StarkBuilderSetting.asset 读取 OutputDir,
        /// 避免写死路径;读取失败时回退到 工程根/tt-minigame。
        /// </summary>
        private static string ReadStarkOutputDir()
        {
            string settingPath = Path.Combine(Application.dataPath, "Editor", "StarkBuilderSetting.asset");
            if (!File.Exists(settingPath))
                return null;
 
            string txt = File.ReadAllText(settingPath);
            var m = Regex.Match(txt, @"^OutputDir:\s*(.+)$", RegexOptions.Multiline);
            if (!m.Success)
                return null;
 
            string dir = m.Groups[1].Value.Trim();
            return string.IsNullOrEmpty(dir) ? null : dir;
        }
    }
}