using System.Text; using LANCommander.Packager.Models; using LANCommander.SDK.Enums; namespace LANCommander.Packager.Services; public static class ScriptGeneratorService { public static List Generate(PackageContext context) { var scripts = new List(); bool hasRegistry = context.SelectedRegistryEntries.Count > 0; bool hasInstallActions = hasRegistry || context.PatchGameSpy; if (hasInstallActions) scripts.Add(GenerateInstallScript(context)); if (hasRegistry) scripts.Add(GenerateUninstallScript(context)); return scripts; } private static GeneratedScript GenerateInstallScript(PackageContext context) { var sb = new StringBuilder(); sb.AppendLine("# Install Script - Generated by LANCommander.Packager"); sb.AppendLine(); bool requiresAdmin = false; if (context.SelectedRegistryEntries.Count > 0) { var groupedEntries = context.SelectedRegistryEntries .GroupBy(e => e.KeyPath, StringComparer.OrdinalIgnoreCase); foreach (var group in groupedEntries) { var keyPath = ConvertToPoShRegistryPath(group.Key); if (group.Key.StartsWith("HKLM", StringComparison.OrdinalIgnoreCase) || group.Key.StartsWith("HKEY_LOCAL_MACHINE", StringComparison.OrdinalIgnoreCase)) requiresAdmin = true; sb.AppendLine($"New-Item -Path \"{keyPath}\" -Force | Out-Null"); foreach (var entry in group.Where(e => !string.IsNullOrEmpty(e.ValueName))) { var valueName = entry.ValueName; sb.AppendLine($"Set-ItemProperty -Path \"{keyPath}\" -Name \"{valueName}\" -Value \"\""); } sb.AppendLine(); } } if (context.PatchGameSpy) { sb.AppendLine("# Patch GameSpy references for OpenSpy compatibility"); sb.AppendLine("Edit-PatchGameSpy -Path $InstallDirectory"); sb.AppendLine(); } return new GeneratedScript { Type = ScriptType.Install, Contents = sb.ToString(), RequiresAdmin = requiresAdmin }; } private static GeneratedScript GenerateUninstallScript(PackageContext context) { var sb = new StringBuilder(); sb.AppendLine("# Uninstall Script - Generated by LANCommander.Packager"); sb.AppendLine(); var groupedEntries = context.SelectedRegistryEntries .GroupBy(e => e.KeyPath, StringComparer.OrdinalIgnoreCase); bool requiresAdmin = false; foreach (var group in groupedEntries) { var keyPath = ConvertToPoShRegistryPath(group.Key); if (group.Key.StartsWith("HKLM", StringComparison.OrdinalIgnoreCase) || group.Key.StartsWith("HKEY_LOCAL_MACHINE", StringComparison.OrdinalIgnoreCase)) requiresAdmin = true; foreach (var entry in group.Where(e => !string.IsNullOrEmpty(e.ValueName))) sb.AppendLine($"Remove-ItemProperty -Path \"{keyPath}\" -Name \"{entry.ValueName}\" -ErrorAction SilentlyContinue"); sb.AppendLine($"Remove-Item -Path \"{keyPath}\" -ErrorAction SilentlyContinue"); sb.AppendLine(); } return new GeneratedScript { Type = ScriptType.Uninstall, Contents = sb.ToString(), RequiresAdmin = requiresAdmin }; } private static string ConvertToPoShRegistryPath(string keyPath) { if (keyPath.StartsWith("HKEY_LOCAL_MACHINE\\", StringComparison.OrdinalIgnoreCase)) return "HKLM:\\" + keyPath["HKEY_LOCAL_MACHINE\\".Length..]; if (keyPath.StartsWith("HKEY_CURRENT_USER\\", StringComparison.OrdinalIgnoreCase)) return "HKCU:\\" + keyPath["HKEY_CURRENT_USER\\".Length..]; if (keyPath.StartsWith("HKEY_CLASSES_ROOT\\", StringComparison.OrdinalIgnoreCase)) return "HKCR:\\" + keyPath["HKEY_CLASSES_ROOT\\".Length..]; if (keyPath.StartsWith("HKLM\\", StringComparison.OrdinalIgnoreCase)) return "HKLM:\\" + keyPath["HKLM\\".Length..]; if (keyPath.StartsWith("HKCU\\", StringComparison.OrdinalIgnoreCase)) return "HKCU:\\" + keyPath["HKCU\\".Length..]; return keyPath; } }