public void TestDoubleRegistration() { var m = new AssetMapping().RegisterAssetType(typeof(ZeroBasedByte), AssetType.Portrait); // Used to throw, but now we forgive it to prevent issues when a mod overrides some assets using a dependency's ids. m.RegisterAssetType(typeof(ZeroBasedByte), AssetType.Portrait); m.RegisterAssetType(typeof(ZeroBasedByte), AssetType.Automap); }
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); } ); }
static void MergeTypesToMapping(AssetMapping mapping, AssetConfig config, string assetConfigPath) { foreach (var assetType in config.Types) { var enumType = Type.GetType(assetType.Key); if (enumType == null) { throw new InvalidOperationException($"Could not load enum type \"{assetType.Key}\" defined in \"{assetConfigPath}\""); } mapping.RegisterAssetType(assetType.Key, assetType.Value.AssetType); } }
static (MockFileSystem disk, string baseDir, GeneralConfig generalConfig, AssetMapping mapping) SetupAssets(IJsonUtil jsonUtil) { Event.AddEventsFromAssembly(typeof(ActionEvent).Assembly); var mapping = new AssetMapping(); var disk = new MockFileSystem(true); var baseDir = ConfigUtil.FindBasePath(disk); var assetConfigPath = Path.Combine(baseDir, "mods", "Base", "assets.json"); var assetConfig = AssetConfig.Load(assetConfigPath, mapping, disk, jsonUtil); var generalConfig = AssetSystem.LoadGeneralConfig(baseDir, disk, jsonUtil); foreach (var assetType in assetConfig.IdTypes.Values) { var enumType = Type.GetType(assetType.EnumType); if (enumType == null) { throw new InvalidOperationException( $"Could not load enum type \"{assetType.EnumType}\" defined in \"{assetConfigPath}\""); } mapping.RegisterAssetType(assetType.EnumType, assetType.AssetType); } return(disk, baseDir, generalConfig, mapping); }