显示Engine版本号

This commit is contained in:
xu_yanfeng 2025-01-12 17:57:02 +08:00
parent aba21c6f89
commit 8bff11cc10
10 changed files with 91 additions and 27 deletions

View File

@ -1,5 +1,5 @@
import { Chunk } from "../scripts/terminal";
import { FrameDetails, Info, NodeInfoData, ObjectData, ObjectItemRequestData, TreeData } from "../views/devtools/data";
import { FrameDetails, Info, NodeInfoData, TreeData } from "../views/devtools/data";
export enum Page {
None = "None",
@ -38,6 +38,10 @@ export interface ResponseSupportData {
*
*/
msg: string;
/**
* engine版本
*/
version: string;
}
export type ResponseUpdateFramesData = FrameDetails[];

View File

@ -14,7 +14,13 @@ export class GoogleAnalytics {
}
return clientId;
}
private isChromeEnv() {
return !!chrome?.storage?.local?.get;
}
public async fireEventWithParam(name: GA_EventName, param: string) {
if (!this.isChromeEnv()) {
return;
}
const time = Date.now();
const id = await this.getOrCreateSessionId();
fetch(`${GA_ENDPOINT}?measurement_id=${MEASUREMENT_ID}&api_secret=${API_SECRET}`, {
@ -35,6 +41,9 @@ export class GoogleAnalytics {
});
}
public async fireEvent(name: string) {
if (!this.isChromeEnv()) {
return;
}
const time = Date.now();
const id = await this.getOrCreateSessionId();
fetch(`${GA_ENDPOINT}?measurement_id=${MEASUREMENT_ID}&api_secret=${API_SECRET}`, {

View File

@ -77,6 +77,7 @@ export enum GA_EventName {
MouseMenu = "mouse_menu",
Hierarchy = "hierarchy",
Inspector = "Inspector",
EngineVersion = "engine_version",
}
export enum GA_Button {
Github = "github",

View File

@ -19,7 +19,7 @@ export class PortDevtools extends PortMan {
if (port) {
port.send(data);
} else {
const e = new PluginEvent(Page.Background, Page.Devtools, Msg.ResponseSupport, { support: false, msg: "disconnect with game, please refresh page" } as ResponseSupportData);
const e = new PluginEvent(Page.Background, Page.Devtools, Msg.ResponseSupport, { support: false, msg: "disconnect with game, please refresh page", version: "" } as ResponseSupportData);
this.send(e);
}
}

View File

@ -7,4 +7,5 @@ export enum DocumentEvent {
* content到inject的事件
*/
Content2Inject = "content2inject",
EngineVersion = "engineVersion",
}

View File

@ -1,7 +1,9 @@
// content.js 和原始界面共享DOM具有操作dom的能力
// 但是不共享js,要想访问页面js,只能通过注入的方式
import { ChromeConst } from "cc-plugin/src/chrome/const";
import { debugLog, Msg, Page, PluginEvent, ResponseSupportData } from "../../core/types";
import { debugLog, Page, PluginEvent } from "../../core/types";
import { ga } from "../../ga";
import { GA_EventName } from "../../ga/type";
import { DocumentEvent } from "../const";
import { Terminal } from "../terminal";
@ -25,6 +27,12 @@ export function injectScript(url: string) {
}
}
document.addEventListener(DocumentEvent.EngineVersion, async (event: CustomEvent) => {
const version: string = event.detail;
if (version) {
ga.fireEventWithParam(GA_EventName.EngineVersion, version);
}
});
// #region 和Inject通讯
document.addEventListener(DocumentEvent.Inject2Content, (event: CustomEvent) => {
let data: PluginEvent = PluginEvent.create(event.detail);
@ -60,20 +68,4 @@ connect.onMessage.addListener((data: PluginEvent, sender: chrome.runtime.Port) =
throw new Error(`invalid data: ${data}`);
}
});
function checkGame() {
let gameCanvas = document.querySelector("#GameCanvas");
const sendData = new PluginEvent(Page.Content, Page.Devtools, Msg.ResponseSupport, {
support: !!gameCanvas,
msg: "",
} as ResponseSupportData);
if (connect) {
connect.postMessage(sendData);
} else {
debugLog && console.log(...terminal.log(`connect is null`));
throw new Error("connect is null");
}
}
injectScript(ChromeConst.script.inject);
// checkGame();

View File

@ -18,4 +18,9 @@ export class InjectEvent {
const event = new CustomEvent(DocumentEvent.Inject2Content, { detail });
document.dispatchEvent(event);
}
sendEngineVersion(version: string) {
const detail = version;
const event = new CustomEvent(DocumentEvent.EngineVersion, { detail });
document.dispatchEvent(event);
}
}

View File

@ -70,16 +70,47 @@ export class Inspector extends InjectEvent {
}
init() {
console.log(...this.terminal.init());
this.watchIsCocosGame();
}
private watchIsCocosGame() {
const timer = setInterval(() => {
const b = this._isCocosGame();
if (b) {
clearInterval(timer);
const version = this.getEngineVersion();
if (b && version) {
this.uploadEngineVersion(version);
}
}
}, 300);
}
private getEngineVersion() {
if (this._isCocosGame()) {
return cc.ENGINE_VERSION || "";
} else {
return "";
}
}
notifySupportGame(b: boolean) {
this.sendMsgToContent(Msg.ResponseSupport, { support: b, msg: "" } as ResponseSupportData);
const version = this.getEngineVersion();
this.sendMsgToContent(Msg.ResponseSupport, { support: b, msg: "", version } as ResponseSupportData);
}
/**
* Engine版本数据
*/
private hasUploadEngineVersion = false;
private uploadEngineVersion(version: string) {
if (this.hasUploadEngineVersion) {
return;
}
if (version) {
this.hasUploadEngineVersion = true;
this.sendEngineVersion(version);
}
}
updateTreeInfo() {
let isCocosCreatorGame = this._isCocosGame();
if (isCocosCreatorGame) {
//@ts-ignore
let scene = cc.director.getScene();
if (scene) {
let treeData = new TreeData();
@ -617,8 +648,8 @@ export class Inspector extends InjectEvent {
const data: NodeInfoData = new NodeInfoData(uuid, groupData);
this.sendMsgToContent(Msg.ResponseNodeInfo, data as ResponseNodeInfoData);
} else {
// 未获取到节点数据
console.log("未获取到节点数据");
const data: NodeInfoData = new NodeInfoData(uuid, []);
this.sendMsgToContent(Msg.ResponseNodeInfo, data as ResponseNodeInfoData);
}
}

View File

@ -1,6 +1,9 @@
<template>
<div class="left">
<CCDock name="Hierarchy">
<template v-slot:title>
<div class="engine-version" v-if="engineVersion">Cocos Creator V{{ engineVersion }}</div>
</template>
<CCInput style="flex: none" placeholder="enter keywords to filter" :data="filterText" v-if="false">
<slot>
<i class="matchCase iconfont icon_font_size" @click.stop="onChangeCase" title="match case" :style="{ color: matchCase ? 'red' : '' }"></i>
@ -16,7 +19,7 @@ import { IUiMenuItem } from "@xuyanfeng/cc-ui/types/cc-menu/const";
import Mousetrap, { MousetrapInstance } from "mousetrap";
import { storeToRefs } from "pinia";
import { defineComponent, nextTick, onMounted, onUnmounted, ref, toRaw, watch } from "vue";
import { Msg, PluginEvent, RequestTreeInfoData, RequestUseFrameData, ResponseSetPropertyData } from "../../core/types";
import { Msg, PluginEvent, RequestTreeInfoData, RequestUseFrameData, ResponseSetPropertyData, ResponseSupportData } from "../../core/types";
import { ga } from "../../ga";
import { GA_EventName } from "../../ga/type";
import { bridge } from "./bridge";
@ -99,6 +102,16 @@ export default defineComponent({
const elTree = ref<typeof CCTree>(null);
const treeData = ref<TreeData[]>([]);
let selectedUUID: string | null = null;
const engineVersion = ref("");
bridge.on(Msg.ResponseSupport, (event: PluginEvent) => {
let data: ResponseSupportData = event.data;
const isCocosGame: boolean = data.support;
if (isCocosGame) {
engineVersion.value = data.version;
} else {
engineVersion.value = "";
}
});
bridge.on(Msg.ResponseTreeInfo, (event: PluginEvent) => {
let data: Array<TreeData> = event.data;
if (!Array.isArray(data)) {
@ -144,6 +157,7 @@ export default defineComponent({
Bus.emit(BusMsg.SelectNode, uuid);
}
return {
engineVersion,
expandedKeys,
elTree,
filterText,
@ -234,7 +248,13 @@ export default defineComponent({
flex-direction: column;
min-width: 200px;
width: 300px;
.engine-version {
flex: 1;
text-align: right;
padding-right: 5px;
font-size: 10px;
user-select: none;
}
.matchCase {
width: 30px;
height: 26px;

View File

@ -163,6 +163,7 @@ export class TestServer {
const e = new PluginEvent(Page.Background, Page.Devtools, Msg.ResponseSupport, {
support: this.support,
msg: "",
version: "0.0.0",
} as ResponseSupportData);
this.send(e);
break;