|
| 1 | +import { Command } from '@cliffy/command' |
| 2 | +import type { GlobalOptions } from '../../lib/types.ts' |
| 3 | +import { generateBlueprintHtml } from '../../lib/utils.ts' |
| 4 | +import { logger } from '../../lib/logger.ts' |
| 5 | +import * as path from 'jsr:@std/path' |
| 6 | +import * as fs from 'jsr:@std/fs' |
| 7 | + |
| 8 | +export type ExtractEventGraphOptions = typeof extractEventGraph extends |
| 9 | + Command<void, void, infer Options extends Record<string, unknown>, [], GlobalOptions> ? Options |
| 10 | + : never |
| 11 | + |
| 12 | +/** |
| 13 | + * Find the EventGraph section in the exported uasset file as is |
| 14 | + * @param fileContent The content of the .copy file as a string |
| 15 | + * @returns The EventGraph section as a string, or null if not found |
| 16 | + */ |
| 17 | +function findEventGraph(fileContent: string): string | null { |
| 18 | + // Find the "Begin Object Name="EventGraph"" section |
| 19 | + const lines = fileContent.split('\n') |
| 20 | + const eventGraphNodes: string[] = [] |
| 21 | + |
| 22 | + let insideEventGraph = false |
| 23 | + let insideNodeSection = false |
| 24 | + let nodeCount = 0 |
| 25 | + let currentObject = '' |
| 26 | + let insideBeginObjectBlock = false |
| 27 | + |
| 28 | + for (let i = 0; i < lines.length; i++) { |
| 29 | + const line = lines[i].trim() |
| 30 | + |
| 31 | + if (!line) continue |
| 32 | + |
| 33 | + // Check if we're starting a Begin Object Name="EventGraph" section |
| 34 | + if (line.startsWith('Begin Object Name="EventGraph"')) { |
| 35 | + insideEventGraph = true |
| 36 | + nodeCount = 0 |
| 37 | + insideNodeSection = false |
| 38 | + continue |
| 39 | + } |
| 40 | + |
| 41 | + // Skip the end of the EventGraph parent object |
| 42 | + if (insideEventGraph && line === 'End Object' && !insideBeginObjectBlock && !insideNodeSection) { |
| 43 | + insideEventGraph = false |
| 44 | + continue |
| 45 | + } |
| 46 | + |
| 47 | + // Check if we're inside an EventGraph and starting a node section with Begin Object Name="..." |
| 48 | + if (insideEventGraph && line.startsWith('Begin Object Name="K2Node_')) { |
| 49 | + nodeCount++ |
| 50 | + currentObject = line |
| 51 | + eventGraphNodes.push(currentObject) |
| 52 | + insideBeginObjectBlock = true |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + // Process EventGraph child blocks that define nodes |
| 57 | + if (insideEventGraph && line.startsWith('Begin Object Class="/Script/BlueprintGraph.K2Node_')) { |
| 58 | + nodeCount++ |
| 59 | + currentObject = line |
| 60 | + eventGraphNodes.push(currentObject) |
| 61 | + insideBeginObjectBlock = true |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + // If we're inside a node definition in the EventGraph, keep collecting its content |
| 66 | + if ( |
| 67 | + insideEventGraph && insideBeginObjectBlock && !line.startsWith('Begin Object') && !line.startsWith('End Object') |
| 68 | + ) { |
| 69 | + eventGraphNodes.push(' ' + line) |
| 70 | + continue |
| 71 | + } |
| 72 | + |
| 73 | + // Handle the end of a node's Begin/End Object block |
| 74 | + if (insideEventGraph && insideBeginObjectBlock && line === 'End Object') { |
| 75 | + eventGraphNodes.push(line) |
| 76 | + insideBeginObjectBlock = false |
| 77 | + currentObject = '' |
| 78 | + continue |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Return the extracted EventGraph nodes, or null if none found |
| 83 | + return nodeCount > 0 ? eventGraphNodes.join('\n') : null |
| 84 | +} |
| 85 | + |
| 86 | +async function extractEventGraphFromFile(filePath: string, render: boolean) { |
| 87 | + const data = await Deno.readTextFile(filePath) |
| 88 | + const eventGraph = findEventGraph(data) |
| 89 | + if (eventGraph) { |
| 90 | + if (render) { |
| 91 | + const html = generateBlueprintHtml(eventGraph) |
| 92 | + const basename = path.basename(filePath, path.extname(filePath)) |
| 93 | + const basepath = path.dirname(filePath) |
| 94 | + await Deno.writeTextFile(`${basepath}/${basename}.html`, html) |
| 95 | + } else { |
| 96 | + logger.info(eventGraph) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export const extractEventGraph = new Command<GlobalOptions>() |
| 102 | + .description('extract event graph from exported uasset') |
| 103 | + .option('-i, --input <file:string>', 'Path to the exported uasset file or directory containing exported uassets', { |
| 104 | + required: true, |
| 105 | + }) |
| 106 | + .option('-r, --render', 'Save the output as rendered html', { default: false }) |
| 107 | + .action(async (options) => { |
| 108 | + try { |
| 109 | + const isDirectory = await fs.exists(options.input, { isDirectory: true }) |
| 110 | + if (isDirectory) { |
| 111 | + const files = await Deno.readDir(options.input) |
| 112 | + for await (const file of files) { |
| 113 | + if (file.isFile) { |
| 114 | + await extractEventGraphFromFile(path.join(options.input, file.name), options.render) |
| 115 | + } |
| 116 | + } |
| 117 | + } else { |
| 118 | + await extractEventGraphFromFile(options.input, options.render) |
| 119 | + } |
| 120 | + } catch (error: unknown) { |
| 121 | + logger.error(`Error parsing blueprint: ${error instanceof Error ? error.message : String(error)}`) |
| 122 | + Deno.exit(1) |
| 123 | + } |
| 124 | + }) |
0 commit comments