public String resolveFilename(String filename) {
        if (filename.startsWith("/") || filename.startsWith("\\"))
            filename = filename.substring(1);
        
        if (!usingMod) {
            return mainDirName + filename;
        }
        
        String[] splitPath = splitParent(filename);
        
        if (modFile) {
            // EU3-style mod
            if (replaced.contains(splitPath[0])) {
                // Case 1: Directory is replaced.
                // Return the file in the moddir, even if it doesn't exist.
                return modDirName + filename;
            } else if (extended.contains(splitPath[0])) {
                // Case 2: Directory is extended.
                // Check if the file exists in the moddir.
                if (new File(modDirName + filename).exists()) {
                    // It does, so return it.
                    return modDirName + filename;
                } else {
                    // It doesn't, so return the file in the main dir.
                    return mainDirName + filename;
                }
            } else {
                // Case 3: Directory is not modded.
                // Return the file in the main dir.
                return mainDirName + filename;
            }
        } else {
            // EU2-style mod
            if (new File(modDirName + filename).exists())
                return modDirName + filename;
            else
                return mainDirName + filename;
        }
    }