Skip to content

Instantly share code, notes, and snippets.

@llint
Created July 10, 2024 08:16
Show Gist options
  • Save llint/fe0295af14c471e48620b3bf8d08bb22 to your computer and use it in GitHub Desktop.
Save llint/fe0295af14c471e48620b3bf8d08bb22 to your computer and use it in GitHub Desktop.
import * as UE from 'ue';
import { blueprint } from 'puerts';
import * as fs from 'fs';
import * as path from 'path';
export function setupMixin<A extends typeof UE.Object, R extends InstanceType<A>>(assetPath: string, mixinCls: new (...args: any) => R) {
console.log(`EntryPoint.setupMixin('${assetPath}', '${mixinCls.name}') ******************`);
let ueAssetCls = UE.Class.Load(assetPath);
let jsAssetCls = blueprint.tojs<A>(ueAssetCls);
return blueprint.mixin(jsAssetCls, mixinCls);
}
// Helper function to capitalize the first letter of a string
function capitalizeFirstLetter(string: string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
// Recursive function to traverse directories and process files
async function processDirectory(directory: string) {
console.log(`EntryPoint.processDirectory('${directory}') ******************`);
const entries = fs.readdirSync(directory, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
await processDirectory(fullPath);
} else if (entry.isFile() && entry.name.endsWith('.js')) {
await processFile(fullPath);
} else {
console.log(`EntryPoint.processDirectory(${directory}) - skipping ${entry.name} ==========`);
}
}
}
// Function to process each TypeScript file
async function processFile(filePath: string) {
console.log(`EntryPoint.processFile('${filePath}') ******************`);
const relativePath = path.relative(__dirname, filePath);
const modulePath = './' + relativePath.replace(/\\/g, '/').replace(/\.js$/, '');
const filename = path.basename(filePath, '.js');
// Ensure the filename ends with '_C'
if (!filename.endsWith('_C')) {
console.warn(`Filename ${filename} does not end with '_C'`);
return;
}
const className = capitalizeFirstLetter(filename);
try {
const module = await import(modulePath);
// Ensure the exported class name is the same as filename
if (module[className]) {
const assetPath = generateAssetPath(filePath);
setupMixin(assetPath, module[className]);
} else {
console.warn(`Class ${className} not found in ${filePath}`);
}
} catch (error) {
console.error(`Failed to import module ${filePath}:`, error);
}
}
const SCRIPT_BINDINGS = 'ScriptBindings';
// Function to generate the asset path based on the file path
function generateAssetPath(filePath: string): string {
const relativePath = path.relative(path.join(__dirname, SCRIPT_BINDINGS), filePath);
const assetPath = relativePath
.replace(/\\/g, '/')
.replace(/\.js$/, '');
const directoryPath = path.dirname(assetPath);
const baseName = path.basename(assetPath, '_C');
return `/${directoryPath}/${baseName}.${baseName}_C`;
}
// Entry point to start processing the directory
async function main() {
const rootDir = path.join(__dirname, SCRIPT_BINDINGS);
await processDirectory(rootDir);
}
main().catch(error => {
console.error('Error during processing:', error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment