2025-10-15 00:23:19 +08:00
|
|
|
import type { IService } from '@esengine/ecs-framework';
|
|
|
|
|
import { Injectable } from '@esengine/ecs-framework';
|
|
|
|
|
import { createLogger } from '@esengine/ecs-framework';
|
|
|
|
|
import { MessageHub } from './MessageHub';
|
|
|
|
|
|
|
|
|
|
const logger = createLogger('ProjectService');
|
|
|
|
|
|
|
|
|
|
export type ProjectType = 'cocos' | 'laya' | 'unknown';
|
|
|
|
|
|
|
|
|
|
export interface ProjectInfo {
|
|
|
|
|
path: string;
|
|
|
|
|
type: ProjectType;
|
|
|
|
|
name: string;
|
|
|
|
|
configPath?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ProjectConfig {
|
|
|
|
|
projectType?: ProjectType;
|
|
|
|
|
componentsPath?: string;
|
|
|
|
|
componentPattern?: string;
|
|
|
|
|
buildOutput?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class ProjectService implements IService {
|
|
|
|
|
private currentProject: ProjectInfo | null = null;
|
|
|
|
|
private projectConfig: ProjectConfig | null = null;
|
|
|
|
|
private messageHub: MessageHub;
|
|
|
|
|
|
|
|
|
|
constructor(messageHub: MessageHub) {
|
|
|
|
|
this.messageHub = messageHub;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async openProject(projectPath: string): Promise<void> {
|
|
|
|
|
try {
|
|
|
|
|
const projectInfo = await this.validateProject(projectPath);
|
|
|
|
|
|
|
|
|
|
this.currentProject = projectInfo;
|
|
|
|
|
|
|
|
|
|
if (projectInfo.configPath) {
|
|
|
|
|
this.projectConfig = await this.loadConfig(projectInfo.configPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await this.messageHub.publish('project:opened', {
|
|
|
|
|
path: projectPath,
|
|
|
|
|
type: projectInfo.type,
|
|
|
|
|
name: projectInfo.name
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
logger.info('Project opened', { path: projectPath, type: projectInfo.type });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error('Failed to open project', error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async closeProject(): Promise<void> {
|
|
|
|
|
if (!this.currentProject) {
|
|
|
|
|
logger.warn('No project is currently open');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const projectPath = this.currentProject.path;
|
|
|
|
|
this.currentProject = null;
|
|
|
|
|
this.projectConfig = null;
|
|
|
|
|
|
|
|
|
|
await this.messageHub.publish('project:closed', { path: projectPath });
|
|
|
|
|
logger.info('Project closed', { path: projectPath });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getCurrentProject(): ProjectInfo | null {
|
|
|
|
|
return this.currentProject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getProjectConfig(): ProjectConfig | null {
|
|
|
|
|
return this.projectConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public isProjectOpen(): boolean {
|
|
|
|
|
return this.currentProject !== null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getComponentsPath(): string | null {
|
2025-10-15 09:19:30 +08:00
|
|
|
if (!this.currentProject) {
|
2025-10-15 00:23:19 +08:00
|
|
|
return null;
|
|
|
|
|
}
|
2025-10-15 09:19:30 +08:00
|
|
|
if (!this.projectConfig?.componentsPath) {
|
|
|
|
|
return this.currentProject.path;
|
|
|
|
|
}
|
|
|
|
|
const sep = this.currentProject.path.includes('\\') ? '\\' : '/';
|
|
|
|
|
return `${this.currentProject.path}${sep}${this.projectConfig.componentsPath}`;
|
2025-10-15 00:23:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async validateProject(projectPath: string): Promise<ProjectInfo> {
|
|
|
|
|
const projectName = projectPath.split(/[\\/]/).pop() || 'Unknown Project';
|
|
|
|
|
|
|
|
|
|
const projectInfo: ProjectInfo = {
|
|
|
|
|
path: projectPath,
|
|
|
|
|
type: 'unknown',
|
|
|
|
|
name: projectName
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-15 09:19:30 +08:00
|
|
|
const sep = projectPath.includes('\\') ? '\\' : '/';
|
|
|
|
|
const configPath = `${projectPath}${sep}ecs-editor.config.json`;
|
2025-10-15 00:23:19 +08:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
projectInfo.configPath = configPath;
|
|
|
|
|
projectInfo.type = 'cocos';
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.warn('No ecs-editor.config.json found, using defaults');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return projectInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async loadConfig(configPath: string): Promise<ProjectConfig> {
|
|
|
|
|
return {
|
|
|
|
|
projectType: 'cocos',
|
2025-10-15 09:19:30 +08:00
|
|
|
componentsPath: '',
|
|
|
|
|
componentPattern: '**/*.ts',
|
2025-10-15 00:23:19 +08:00
|
|
|
buildOutput: 'temp/editor-components'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public dispose(): void {
|
|
|
|
|
this.currentProject = null;
|
|
|
|
|
this.projectConfig = null;
|
|
|
|
|
logger.info('ProjectService disposed');
|
|
|
|
|
}
|
|
|
|
|
}
|