Пример #1
0
        public void MergeTest()
        {
            var m1 = new AssetMapping();
            var m2 = new AssetMapping();

            m1.RegisterAssetType(typeof(ZeroBasedByte), AssetType.Portrait);
            m2.RegisterAssetType(typeof(OneBasedByte), AssetType.Portrait);

            Assert.Equal(new AssetId(AssetType.Portrait, 1), m1.EnumToId(ZeroBasedByte.One));
            Assert.Throws <ArgumentOutOfRangeException>(() => m1.EnumToId(OneBasedByte.One));
            Assert.Throws <ArgumentOutOfRangeException>(() => m2.EnumToId(ZeroBasedByte.One));
            Assert.Equal(new AssetId(AssetType.Portrait, 1), m2.EnumToId(OneBasedByte.One));

            Assert.Throws <ArgumentNullException>(() => m2.MergeFrom(null));
            m2.MergeFrom(m1);
            Assert.Equal(new AssetId(AssetType.Portrait, 1), m1.EnumToId(ZeroBasedByte.One));
            Assert.Throws <ArgumentOutOfRangeException>(() => m1.EnumToId(OneBasedByte.One));
            Assert.Equal(new AssetId(AssetType.Portrait, 5), m2.EnumToId(ZeroBasedByte.One));
            Assert.Equal(new AssetId(AssetType.Portrait, 1), m2.EnumToId(OneBasedByte.One));

            m2.RegisterAssetType(typeof(ZeroBasedShort), AssetType.Portrait);
            Assert.Throws <ArgumentOutOfRangeException>(() => m1.EnumToId(ZeroBasedShort.Zero));
            Assert.Equal(new AssetId(AssetType.Portrait, 7), m2.EnumToId(ZeroBasedShort.Zero));

            m1.MergeFrom(m2);
            Assert.Equal(new AssetId(AssetType.Portrait, 1), m1.EnumToId(ZeroBasedByte.One));
            Assert.Equal(new AssetId(AssetType.Portrait, 3), m1.EnumToId(OneBasedByte.One));
            Assert.Equal(new AssetId(AssetType.Portrait, 6), m1.EnumToId(ZeroBasedShort.Zero));

            Assert.Collection(
                new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }.Select(x => m1.IdToEnum(new AssetId(AssetType.Portrait, x))),
                x => { Assert.Equal(typeof(ZeroBasedByte), x.Item1); Assert.Equal(0, x.Item2); },
                x => { Assert.Equal(typeof(ZeroBasedByte), x.Item1); Assert.Equal(1, x.Item2); },
                x => { Assert.Equal(typeof(ZeroBasedByte), x.Item1); Assert.Equal(2, x.Item2); },
                x => { Assert.Equal(typeof(OneBasedByte), x.Item1); Assert.Equal(1, x.Item2); },
                x => { Assert.Equal(typeof(OneBasedByte), x.Item1); Assert.Equal(2, x.Item2); },
                x => { Assert.Equal(typeof(OneBasedByte), x.Item1); Assert.Equal(3, x.Item2); },
                x => { Assert.Equal(typeof(ZeroBasedShort), x.Item1); Assert.Equal(0, x.Item2); },
                x => { Assert.Equal(typeof(ZeroBasedShort), x.Item1); Assert.Equal(1, x.Item2); },
                x => { Assert.Equal(typeof(ZeroBasedShort), x.Item1); Assert.Equal(2, x.Item2); }
                );

            Assert.Collection(
                new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }.Select(x => m2.IdToEnum(new AssetId(AssetType.Portrait, x))),
                x => { Assert.Equal(typeof(OneBasedByte), x.Item1); Assert.Equal(0, x.Item2); },
                x => { Assert.Equal(typeof(OneBasedByte), x.Item1); Assert.Equal(1, x.Item2); },
                x => { Assert.Equal(typeof(OneBasedByte), x.Item1); Assert.Equal(2, x.Item2); },
                x => { Assert.Equal(typeof(OneBasedByte), x.Item1); Assert.Equal(3, x.Item2); },
                x => { Assert.Equal(typeof(ZeroBasedByte), x.Item1); Assert.Equal(0, x.Item2); },
                x => { Assert.Equal(typeof(ZeroBasedByte), x.Item1); Assert.Equal(1, x.Item2); },
                x => { Assert.Equal(typeof(ZeroBasedByte), x.Item1); Assert.Equal(2, x.Item2); },
                x => { Assert.Equal(typeof(ZeroBasedShort), x.Item1); Assert.Equal(0, x.Item2); },
                x => { Assert.Equal(typeof(ZeroBasedShort), x.Item1); Assert.Equal(1, x.Item2); },
                x => { Assert.Equal(typeof(ZeroBasedShort), x.Item1); Assert.Equal(2, x.Item2); }
                );
        }
Пример #2
0
 public int ToDisk(AssetMapping mapping)
 {
     if (mapping == null)
     {
         throw new ArgumentNullException(nameof(mapping));
     }
     var(enumType, enumValue) = AssetMapping.Global.IdToEnum(this);
     return(mapping.EnumToId(enumType, enumValue).Id);
 }
Пример #3
0
    public static AssetId SerdesU16(string name, AssetId id, AssetType type, AssetMapping mapping, ISerializer s)
    {
        if (mapping == null)
        {
            throw new ArgumentNullException(nameof(mapping));
        }
        if (s == null)
        {
            throw new ArgumentNullException(nameof(s));
        }
        var(enumType, enumValue) = AssetMapping.Global.IdToEnum(id);
        ushort diskValue = (ushort)mapping.EnumToId(enumType, enumValue).Id;

        diskValue = s.UInt16(name, diskValue);

        (enumType, enumValue) = mapping.IdToEnum(new AssetId(type, diskValue));
        return(AssetMapping.Global.EnumToId(enumType, enumValue));
    }