UNPKG

il2cpp-dump-analyzer-mcp

Version:

Agentic RAG system for analyzing IL2CPP dump.cs files from Unity games

286 lines 10.1 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.SupabaseHashManager = void 0; const fs = __importStar(require("fs")); const crypto = __importStar(require("crypto")); const supabase_js_1 = require("@supabase/supabase-js"); /** * Manages file hashes in Supabase to prevent duplicate processing of dump.cs files */ class SupabaseHashManager { constructor(supabaseUrl, supabaseKey, tableName = 'file_hashes') { this.processedHashes = new Set(); // Cache for performance this.isInitialized = false; this.supabaseClient = (0, supabase_js_1.createClient)(supabaseUrl, supabaseKey); this.tableName = tableName; } /** * Initialize the hash manager by loading existing hashes from Supabase */ async initialize() { if (this.isInitialized) return; try { console.log('Loading file hashes from Supabase...'); const { data, error } = await this.supabaseClient .from(this.tableName) .select('hash_value'); if (error) { console.warn(`Warning: Could not load hashes from Supabase table ${this.tableName}:`, error); // Continue with empty cache - table might not exist yet } else if (data) { data.forEach(row => this.processedHashes.add(row.hash_value)); console.log(`Loaded ${data.length} file hashes from Supabase`); } this.isInitialized = true; } catch (error) { console.warn('Warning: Could not initialize Supabase hash manager:', error); this.isInitialized = true; // Continue with empty cache } } /** * Calculate SHA-256 hash of a file * @param filePath Path to the file * @returns SHA-256 hash as hex string */ calculateFileHash(filePath) { const fileBuffer = fs.readFileSync(filePath); const hashSum = crypto.createHash('sha256'); hashSum.update(fileBuffer); return hashSum.digest('hex'); } /** * Check if a file has already been processed * @param filePath Path to the dump.cs file * @returns true if file was already processed, false otherwise * @deprecated Use isFileProcessedAsync for proper async handling */ isFileProcessed(filePath) { // For synchronous compatibility, we can only check the cache // If not initialized, we assume the file hasn't been processed if (!this.isInitialized) { console.warn('SupabaseHashManager not initialized. Use isFileProcessedAsync() or call initialize() first.'); return false; } const hash = this.calculateFileHash(filePath); return this.processedHashes.has(hash); } /** * Async version that checks both cache and database */ async isFileProcessedAsync(filePath) { await this.initialize(); const hash = this.calculateFileHash(filePath); // Check cache first if (this.processedHashes.has(hash)) { return true; } // Check database as fallback try { const { data, error } = await this.supabaseClient .from(this.tableName) .select('id') .eq('file_path', filePath) .eq('hash_value', hash) .limit(1); if (error) { console.warn('Error checking if file was processed:', error); return false; } const isProcessed = data && data.length > 0; if (isProcessed) { // Update cache this.processedHashes.add(hash); } return isProcessed; } catch (error) { console.warn('Error checking if file was processed:', error); return false; } } /** * Mark a file as processed by storing its hash * @param filePath Path to the dump.cs file * @returns The hash that was stored */ markFileAsProcessed(filePath) { const hash = this.calculateFileHash(filePath); // Fire and forget async operation this.markFileAsProcessedAsync(filePath).catch(error => { console.error('Error in async markFileAsProcessed:', error); }); // Update cache immediately for synchronous behavior this.processedHashes.add(hash); return hash; } /** * Async version of markFileAsProcessed */ async markFileAsProcessedAsync(filePath) { await this.initialize(); const hash = this.calculateFileHash(filePath); try { // Use upsert to handle both insert and update cases const { error } = await this.supabaseClient .from(this.tableName) .upsert({ file_path: filePath, hash_value: hash }, { onConflict: 'file_path' }); if (error) { console.error('Error marking file as processed:', error); throw error; } // Update cache this.processedHashes.add(hash); return hash; } catch (error) { console.error('Error marking file as processed:', error); throw error; } } /** * Get the hash of a file without marking it as processed * @param filePath Path to the dump.cs file * @returns The SHA-256 hash of the file */ getFileHash(filePath) { return this.calculateFileHash(filePath); } /** * Remove a hash from the processed list (useful for reprocessing) * @param filePath Path to the dump.cs file * @returns true if hash was removed, false if it wasn't found */ removeFileHash(filePath) { const hash = this.calculateFileHash(filePath); // Fire and forget async operation this.removeFileHashAsync(filePath).catch(error => { console.error('Error in async removeFileHash:', error); }); // Update cache immediately for synchronous behavior return this.processedHashes.delete(hash); } /** * Async version of removeFileHash */ async removeFileHashAsync(filePath) { await this.initialize(); const hash = this.calculateFileHash(filePath); try { const { error } = await this.supabaseClient .from(this.tableName) .delete() .eq('file_path', filePath); if (error) { console.error('Error removing file hash:', error); return false; } // Update cache this.processedHashes.delete(hash); return true; } catch (error) { console.error('Error removing file hash:', error); return false; } } /** * Clear all processed hashes */ clearAllHashes() { // Fire and forget async operation this.clearAllHashesAsync().catch(error => { console.error('Error in async clearAllHashes:', error); }); // Clear cache immediately for synchronous behavior this.processedHashes.clear(); } /** * Async version of clearAllHashes */ async clearAllHashesAsync() { await this.initialize(); try { const { error } = await this.supabaseClient .from(this.tableName) .delete() .neq('id', 0); // Delete all rows if (error) { console.error('Error clearing all hashes:', error); throw error; } // Clear cache this.processedHashes.clear(); } catch (error) { console.error('Error clearing all hashes:', error); throw error; } } /** * Get all processed hashes * @returns Array of all stored hashes */ getAllHashes() { return Array.from(this.processedHashes); } /** * Get the number of processed files * @returns Count of processed files */ getProcessedCount() { return this.processedHashes.size; } /** * Get information about the hash storage * @returns Object with table name and count */ getInfo() { return { hashFilePath: `Supabase table: ${this.tableName}`, processedCount: this.getProcessedCount() }; } } exports.SupabaseHashManager = SupabaseHashManager; //# sourceMappingURL=supabase-hash-manager.js.map