hiepdh-playable-toolkit 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # My Playable Toolkit
2
+ - `Just a Noname Playable Ads Dev!`
3
+ - `Hi, I'm Hiep from HMGames, this is a simple playable toolkit to make an easier build solution!`
@@ -0,0 +1,36 @@
1
+ function injectCustomScript() {
2
+ console.log("Playable Ads: MRAID Smart Start Initialized");
3
+ var isStarted = false;
4
+
5
+ function startAd() {
6
+ if (isStarted) return;
7
+ if (window.boot) {
8
+ console.log("MRAID: Triggering window.boot()");
9
+ isStarted = true;
10
+ window.boot();
11
+ } else {
12
+ setTimeout(startAd, 50);
13
+ }
14
+ }
15
+
16
+ // Kiểm tra MRAID (Chuẩn chung cho Unity, ironSource, AppLovin, TikTok)
17
+ if (typeof mraid !== 'undefined') {
18
+ if (mraid.getState() === 'loading') {
19
+ mraid.addEventListener('ready', checkViewable);
20
+ } else {
21
+ checkViewable();
22
+ }
23
+ } else {
24
+ startAd();
25
+ }
26
+
27
+ function checkViewable() {
28
+ if (mraid.isViewable()) {
29
+ startAd();
30
+ } else {
31
+ mraid.addEventListener('viewableChange', function (viewable) {
32
+ if (viewable) startAd();
33
+ });
34
+ }
35
+ }
36
+ }
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "hiepdh-playable-toolkit",
3
+ "version": "1.0.0",
4
+ "description": "Let's Cook this playable! Nothing can stop us!",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "postinstall": "node setup.js"
9
+ },
10
+ "keywords": [],
11
+ "author": "HiepDH",
12
+ "license": "ISC"
13
+ }
@@ -0,0 +1,78 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ const SOURCE_FILE = './build/web-mobile/application.js';
5
+ const TARGET_DIR = './build-templates/web-mobile';
6
+ const TARGET_FILE = path.join(TARGET_DIR, 'application.js');
7
+
8
+ async function patchApplication() {
9
+ try {
10
+ if (!fs.existsSync(SOURCE_FILE)) {
11
+ console.error(`Không tìm thấy file gốc! Hãy build project trong Cocos trước.`);
12
+ return;
13
+ }
14
+
15
+ let content = fs.readFileSync(SOURCE_FILE, 'utf8');
16
+ console.log("--- Đang tiến hành Patch application.js (Vị trí Resume chuẩn)...");
17
+
18
+ const initGameKey = "function initializeGame(cc, settings, findCanvas) {";
19
+ const videoPatch = `function initializeGame(cc, settings, findCanvas) {
20
+ try {
21
+ if (cc && !cc.internal) cc.internal = {};
22
+ if (cc.internal && !cc.internal.VideoPlayerImplManager) {
23
+ cc.internal.VideoPlayerImplManager = { getImpl: function () { return null; } };
24
+ console.log("Unity Endcard Patch: VideoPlayerImplManager initialized manually.");
25
+ }
26
+ } catch (e) { console.warn("Failed to patch VideoPlayerManager:", e); }`;
27
+
28
+ content = content.replace(initGameKey, videoPatch);
29
+
30
+ const onStartKey = "onGameStarted(cc, settings);";
31
+ const resumeLogic = `onGameStarted(cc, settings);
32
+ if (cc.game.isPaused && cc.game.isPaused()) {
33
+ cc.game.resume();
34
+ }`;
35
+
36
+ content = content.replace(onStartKey, resumeLogic);
37
+
38
+ content = content.replace(/cc\s*=\s*engine;/, 'cc = engine; window.cc = cc;');
39
+
40
+ const startMarker = "var findCanvas = _ref3.findCanvas;";
41
+ const nextFuncMarker = "function topLevelImport";
42
+
43
+ const startIndex = content.indexOf(startMarker);
44
+ const nextFuncIndex = content.indexOf(nextFuncMarker);
45
+
46
+ if (startIndex !== -1 && nextFuncIndex !== -1) {
47
+ const part1 = content.substring(0, startIndex + startMarker.length);
48
+ let middlePart = content.substring(startIndex + startMarker.length, nextFuncIndex).trim();
49
+
50
+ if (middlePart.endsWith('}')) {
51
+ middlePart = middlePart.substring(0, middlePart.lastIndexOf('}')).trim();
52
+ }
53
+
54
+ const part3 = content.substring(nextFuncIndex);
55
+
56
+ const wrappedBoot = `
57
+ window.boot = function () {
58
+ console.log("Cocos 3.x: Engine starting via window.boot()...");
59
+ ${middlePart}
60
+ };
61
+
62
+ console.log("Cocos 3.x: Application ready, waiting for window.boot()...");
63
+ return Promise.resolve();
64
+ }
65
+ `;
66
+ content = part1 + wrappedBoot + "\n " + part3;
67
+ }
68
+
69
+ if (!fs.existsSync(TARGET_DIR)) fs.mkdirSync(TARGET_DIR, { recursive: true });
70
+ fs.writeFileSync(TARGET_FILE, content, 'utf8');
71
+
72
+ console.log(`Thành công! File đã được lưu tại: ${TARGET_FILE}`);
73
+ } catch (err) {
74
+ console.error(" Lỗi:", err);
75
+ }
76
+ }
77
+
78
+ patchApplication();
package/setup.js ADDED
@@ -0,0 +1,66 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ // 1. Xác định đường dẫn
5
+ const projectRoot = process.env.INIT_CWD || path.resolve(__dirname, '../../');
6
+ const pkgPath = path.join(projectRoot, 'package.json');
7
+ const inlineFilePath = path.join(projectRoot, 'build-inline.js');
8
+
9
+ // Đường dẫn file nguồn trong thư viện (cùng cấp với setup.js)
10
+ const sourcePatch = path.join(__dirname, 'patch-template-unityReject.js');
11
+ const sourceInject = path.join(__dirname, 'injectCustomScript.js');
12
+
13
+ // Đường dẫn đích tại thư mục gốc Game
14
+ const targetPatch = path.join(projectRoot, 'patch-template-unityReject.js');
15
+ const templateDir = path.join(projectRoot, 'build-templates', 'web-mobile');
16
+
17
+ console.log("--- HiepDH Playable Toolkit: Khởi tạo cấu hình ---");
18
+
19
+ if (fs.existsSync(pkgPath)) {
20
+ try {
21
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
22
+ pkg.scripts = {
23
+ ...pkg.scripts,
24
+ "clean": "node -e \"fs.rmSync('./build-templates/web-mobile/application.js', {force:true})\"",
25
+ "fixUnity": "node patch-template-unityReject.js",
26
+ "build": "node build-inline.js unity"
27
+ };
28
+ fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2), 'utf8');
29
+ console.log(" [1/3] Đã cập nhật scripts vào package.json.");
30
+ } catch (e) { console.error(" Lỗi package.json: " + e.message); }
31
+ }
32
+
33
+ try {
34
+ if (!fs.existsSync(templateDir)) {
35
+ fs.mkdirSync(templateDir, { recursive: true });
36
+ console.log(" Đã tạo cấu trúc build-templates/web-mobile/");
37
+ }
38
+
39
+ if (fs.existsSync(sourcePatch)) {
40
+ fs.copyFileSync(sourcePatch, targetPatch);
41
+ console.log("[2/3] Đã copy patch-template-unityReject.js ra thư mục gốc.");
42
+ }
43
+ } catch (e) { console.error("Lỗi copy file patch: " + e.message); }
44
+
45
+ // --- BƯỚC 3: ĐỌC FILE INJECT VÀ VÁ VÀO BUILD-INLINE.JS ---
46
+ if (fs.existsSync(inlineFilePath) && fs.existsSync(sourceInject)) {
47
+ try {
48
+ let inlineContent = fs.readFileSync(inlineFilePath, 'utf8');
49
+ const newFunctionContent = fs.readFileSync(sourceInject, 'utf8');
50
+
51
+ // Regex tìm hàm injectCustomScript (bắt từ 'function' đến dấu '}' cuối cùng)
52
+ const regex = /function\s+injectCustomScript\s*\([\s\S]*?\{[\s\S]*?\n\}/;
53
+
54
+ if (regex.test(inlineContent)) {
55
+ inlineContent = inlineContent.replace(regex, newFunctionContent);
56
+ fs.writeFileSync(inlineFilePath, inlineContent, 'utf8');
57
+ console.log(" [3/3] Đã vá injectCustomScript.js vào build-inline.js.");
58
+ } else {
59
+ console.warn(" Không tìm thấy hàm injectCustomScript trong build-inline.js.");
60
+ }
61
+ } catch (e) { console.error(" Lỗi vá build-inline.js: " + e.message); }
62
+ } else {
63
+ console.warn(" Bỏ qua bước 3: Thiếu file build-inline.js hoặc injectCustomScript.js");
64
+ }
65
+
66
+ console.log("\n--- Toolkit Ready! Let's Cook! ---");