001/*
002 * Copyright (c) 2024 See AUTHORS file.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 */
017
018package com.github.tommyettinger.kryo.cringe;
019
020import com.badlogic.gdx.utils.Array;
021import com.esotericsoftware.kryo.Kryo;
022import com.esotericsoftware.kryo.Serializer;
023import com.esotericsoftware.kryo.io.Input;
024import com.esotericsoftware.kryo.io.Output;
025import com.github.tommyettinger.cringe.GapShuffler;
026import com.github.tommyettinger.cringe.GdxRandom;
027import com.github.tommyettinger.kryo.gdx.ArraySerializer;
028
029/**
030 * Needs {@link ArraySerializer} to be registered for {@link Array},
031 * the concrete subclass of {@link GdxRandom} registered, and
032 * the type of whatever items are in the GapShuffler registered.
033 */
034@SuppressWarnings({"rawtypes", "unchecked"})
035public class GapShufflerSerializer extends Serializer<GapShuffler> {
036    public GapShufflerSerializer() {
037        setImmutable(false);
038        setAcceptsNull(false);
039    }
040
041    @Override
042    public void write(final Kryo kryo, final Output output, final GapShuffler data) {
043        Array items = new Array();
044        data.fillInto(items);
045        kryo.writeObject(output, items);
046        kryo.writeClassAndObject(output, data.random);
047        output.writeInt(data.getIndex(), true);
048    }
049
050    @Override
051    public GapShuffler read(final Kryo kryo, final Input input, final Class<? extends GapShuffler> dataClass) {
052        return new GapShuffler<>(kryo.readObject(input, Array.class),
053                (GdxRandom) kryo.readClassAndObject(input),
054                input.readInt(true),
055                true, false);
056    }
057
058
059    @Override
060    public GapShuffler copy(Kryo kryo, GapShuffler original) {
061        return new GapShuffler(original);
062    }
063}