TrySetSidModel.java

package builder.netsiddev.commands;

import libsidplay.common.ChipModel;

import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static server.netsiddev.Command.TRY_SET_SID_MODEL;

public class TrySetSidModel implements NetSIDPkg {
    private final byte sidNum;
    private final byte config;

    private static final Map<SimpleImmutableEntry<ChipModel, String>, Byte> FILTER_TO_SID_MODEL = new HashMap<>();

    /**
     * Select a server-advertised configuration for the target SID slot.
     * <p>
     * "First" within a chip model means case-insensitive alphabetical filter-name order, as returned by
     * {@link #getFilterNames(ChipModel)}. The server must advertise at least one configuration.
     *
     * @param sidNum     SID chip number
     * @param chipModel  SID chip model
     * @param filterName desired filter name; null or empty selects the first configuration for the chip model
     */
    public TrySetSidModel(byte sidNum, ChipModel chipModel, String filterName) {
        this.sidNum = sidNum;
        List<String> filterNames = getFilterNames(chipModel);
        String desiredFilterName = filterName != null && !filterName.isEmpty() ? filterName
                : filterNames.stream().findFirst().orElse(null);
        Optional<SimpleImmutableEntry<ChipModel, String>> configuration = FILTER_TO_SID_MODEL.keySet().stream()
                .filter(p -> p.getValue().equals(desiredFilterName) && p.getKey().equals(chipModel)).findFirst();
        if (configuration.isPresent()) {
            // Case 1: Matching name/model, or default for an empty name.
            this.config = FILTER_TO_SID_MODEL.get(configuration.get());
        } else if (filterNames.size() == 1) {
            // Case 2: One preset for this model, as with Wilfred's device.
            this.config = FILTER_TO_SID_MODEL.get(new SimpleImmutableEntry<>(chipModel, filterNames.getFirst()));
        } else {
            // Missing name: warn and choose a fallback.
            System.err.printf("TrySetSidModel: Filter name %s not found!", filterName);
            Optional<String> filter = filterNames.stream().findFirst();
            if (filter.isPresent()) {
                // Case 3: Missing name; use this model's first preset.
                this.config = FILTER_TO_SID_MODEL.get(new SimpleImmutableEntry<>(chipModel, filter.get()));
                System.err.printf("  TrySetSidModel: Use first configuration of chip model=%s, instead!\n", chipModel);
            } else {
                // Case 4: Missing model; use server preset 0, possibly another model.
                System.err.println("  TrySetSidModel: Use first available configuration, instead!");
                this.config = 0;
            }
        }
    }

    /**
     * Select an explicit server configuration index, bypassing name/model lookup and fallback.
     *
     * @param sidNum SID chip number
     * @param config unsigned 8-bit server configuration index encoded as a byte
     */
    public TrySetSidModel(byte sidNum, byte config) {
        this.sidNum = sidNum;
        this.config = config;
    }

    public static Map<SimpleImmutableEntry<ChipModel, String>, Byte> getFilterToSidModel() {
        return FILTER_TO_SID_MODEL;
    }

    /**
     * @param model chip model
     * @return sorted filter names of the desired chip model (case-insensitive)
     */
    public static List<String> getFilterNames(ChipModel model) {
        return FILTER_TO_SID_MODEL.keySet().stream().filter(p -> p.getKey() == model).map(SimpleImmutableEntry::getValue)
                .sorted(String::compareToIgnoreCase).collect(Collectors.toList());
    }

    @Override
    public byte[] toByteArray() {
        return new byte[]{(byte) TRY_SET_SID_MODEL.ordinal(), sidNum, 0, 0, config};
    }

}