# How Wand-Enhancer Handles Duplicate Custom Renderer Scripts

> Learn how Wand-Enhancer handles duplicate custom renderer scripts. Discover its efficient method of ignoring duplicates to ensure a clean ASAR build.

- Repository: [k1tbyte/Wand-Enhancer](https://github.com/k1tbyte/Wand-Enhancer)
- Tags: internals
- Published: 2026-09-01

---

**Wand-Enhancer silently ignores duplicate custom renderer scripts during the patching process by checking for existing files before copying, ensuring only the first occurrence of each filename is preserved in the final ASAR build.**

When patching applications with **Wand-Enhancer**, users inject custom JavaScript renderer scripts via the **Patch Config** (`CustomScriptPaths`). According to the k1tbyte/Wand-Enhancer source code, the tool implements a two-layer deduplication strategy to handle duplicate custom renderer scripts gracefully, preventing file conflicts in the generated `remote-panel/renderer-scripts` directory.

## The Core Deduplication Mechanism

The primary defense against duplicates resides in the `CopySelectedJavaScriptFiles` method within [`WandEnhancer/Core/Enhancer.cs`](https://github.com/k1tbyte/Wand-Enhancer/blob/main/WandEnhancer/Core/Enhancer.cs). This method iterates through the `CustomScriptPaths` collection and copies each script to the temporary build folder, but only if a file with the same name does not already exist.

### File Existence Guard

Before performing any copy operation, the code constructs the destination path using `Path.GetFileName(sourcePath)` and explicitly checks for existence:

```csharp
// inside CopySelectedJavaScriptFiles (WandEnhancer/Core/Enhancer.cs)
string destPath = Path.Combine(destinationDir, Path.GetFileName(sourcePath));
if (!File.Exists(destPath)) {
    File.Copy(sourcePath, destPath);
}

```

Because of this `File.Exists` guard, **duplicate scripts are silently skipped**. The first script processed with a specific filename wins, and subsequent duplicates are ignored without throwing exceptions.

## UI-Level Deduplication

Before the copy operation even begins, the user interface performs preliminary deduplication. In [`WandEnhancer/View/Popups/PatchVectorsPopup.xaml.cs`](https://github.com/k1tbyte/Wand-Enhancer/blob/main/WandEnhancer/View/Popups/PatchVectorsPopup.xaml.cs), the selection dialog filters the user’s choices using LINQ’s `Distinct()` method to remove exact path duplicates from the configuration:

```csharp
// 1. UI collects selected script paths
var selectedScripts = openFileDialog.SelectedFiles;
var uniquePaths = selectedScripts.Select(f => f.FullPath).Distinct().ToList();
_config.CustomScriptPaths = uniquePaths;

```

This ensures that identical file paths do not propagate into the patch configuration stored in [`WandEnhancer/Models/PatchConfig.cs`](https://github.com/k1tbyte/Wand-Enhancer/blob/main/WandEnhancer/Models/PatchConfig.cs).

## Handling Filename Collisions from Different Sources

Even if two distinct source files share an identical filename (e.g., [`custom.js`](https://github.com/k1tbyte/Wand-Enhancer/blob/main/custom.js) from different folders), the copy routine’s filename-based check prevents overwrites. The first file copied retains its position, and the second is discarded, maintaining a stable set of renderer scripts in the final build.

## Complete Workflow Example

The following example illustrates the full deduplication pipeline, from UI selection to final copy:

```csharp
// UI Layer: PatchVectorsPopup.xaml.cs
var selectedScripts = openFileDialog.SelectedFiles;
var uniquePaths = selectedScripts.Select(f => f.FullPath).Distinct().ToList();
_config.CustomScriptPaths = uniquePaths;

// Core Layer: Enhancer.cs execution during patch
int copied = Enhancer.CopySelectedJavaScriptFiles(
    _config.CustomScriptPaths, 
    targetScriptsRoot  // typically: remote-panel/renderer-scripts
);

```

In this flow, `Distinct()` removes exact path duplicates at the configuration stage, while `File.Exists` acts as a final safeguard against filename collisions during the ASAR packaging process.

## Summary

- **Dual-layer protection**: Deduplication occurs both in the UI ([`PatchVectorsPopup.xaml.cs`](https://github.com/k1tbyte/Wand-Enhancer/blob/main/PatchVectorsPopup.xaml.cs)) and the core copying method ([`Enhancer.cs`](https://github.com/k1tbyte/Wand-Enhancer/blob/main/Enhancer.cs)).
- **First-write wins**: The `File.Exists` check in `CopySelectedJavaScriptFiles` ensures the first script copied with a given filename is preserved, and duplicates are ignored.
- **Silent operation**: No exceptions are thrown when duplicates are encountered; they are simply skipped.
- **Configuration storage**: The `CustomScriptPaths` property in [`PatchConfig.cs`](https://github.com/k1tbyte/Wand-Enhancer/blob/main/PatchConfig.cs) stores the deduplicated list of absolute paths.

## Frequently Asked Questions

### What happens if two different files have the same filename?

The `CopySelectedJavaScriptFiles` method in [`WandEnhancer/Core/Enhancer.cs`](https://github.com/k1tbyte/Wand-Enhancer/blob/main/WandEnhancer/Core/Enhancer.cs) uses only the filename for collision detection. The first file processed with that name is copied to `remote-panel/renderer-scripts`, and subsequent files with identical names are silently ignored, regardless of their content differences.

### Does Wand-Enhancer merge duplicate script contents?

No. Wand-Enhancer does not merge or concatenate script contents. It performs filename-based duplicate detection using `File.Exists`, meaning the first occurrence of a filename is preserved exactly as-is, and later duplicates are discarded entirely.

### Where are the renderer scripts stored during the patching process?

Custom renderer scripts are copied into a temporary `remote-panel/renderer-scripts` folder inside the build directory. This folder is eventually packaged into the final ASAR archive. The `CopySelectedJavaScriptFiles` method handles this placement according to the paths defined in `PatchConfig.CustomScriptPaths`.

### How can I replace an existing renderer script in the patch?

To replace a script, remove the original from the `CustomScriptPaths` list before adding the new version, or ensure the replacement script appears earlier in the collection. Since the enhancer uses a first-write-wins policy, placing your new script first in the list guarantees it will be copied instead of the old version.