Check if block type is correct

This commit is contained in:
Norbi Peti 2021-05-30 02:12:38 +02:00
parent 94c0c1370b
commit b31eaa20c0
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56

View file

@ -87,23 +87,27 @@ namespace TechbloxModdingAPI
remove => BlockEventsEngine.Removed -= value;
}
internal static readonly Dictionary<ExclusiveBuildGroup, Func<EGID, Block>> GroupToConstructor =
new Dictionary<ExclusiveBuildGroup, Func<EGID, Block>>
private static readonly Dictionary<ExclusiveBuildGroup, (Func<EGID, Block> Constructor, Type Type)> GroupToConstructor =
new Dictionary<ExclusiveBuildGroup, (Func<EGID, Block>, Type)>
{
{CommonExclusiveGroups.DAMPEDSPRING_BLOCK_GROUP, id => new DampedSpring(id)},
{CommonExclusiveGroups.ENGINE_BLOCK_BUILD_GROUP, id => new Engine(id)}
{CommonExclusiveGroups.DAMPEDSPRING_BLOCK_GROUP, (id => new DampedSpring(id), typeof(DampedSpring))},
{CommonExclusiveGroups.ENGINE_BLOCK_BUILD_GROUP, (id => new Engine(id), typeof(Engine))}
};
internal static Block New(EGID egid)
{
return GroupToConstructor.ContainsKey(egid.groupID)
? GroupToConstructor[egid.groupID](egid)
? GroupToConstructor[egid.groupID].Constructor(egid)
: new Block(egid);
}
public Block(EGID id)
{
Id = id; //TODO: Check if block type is correct
Id = id;
Type expectedType;
if (GroupToConstructor.ContainsKey(id.groupID) &&
!GetType().IsAssignableFrom(expectedType = GroupToConstructor[id.groupID].Type))
throw new BlockSpecializationException($"Incorrect block type! Expected: {expectedType} Actual: {GetType()}");
}
/// <summary>
@ -111,11 +115,11 @@ namespace TechbloxModdingAPI
/// It will throw an exception if the block doesn't exist.
/// Use the EGID constructor where possible or subclasses of Block as those specify the group.
/// </summary>
public Block(uint id)
public Block(uint id) : this(BlockEngine.FindBlockEGID(id)
?? throw new BlockTypeException(
"Could not find the appropriate group for the block." +
" The block probably doesn't exist or hasn't been submitted."))
{
Id = BlockEngine.FindBlockEGID(id)
?? throw new BlockTypeException("Could not find the appropriate group for the block." +
" The block probably doesn't exist or hasn't been submitted.");
}
/// <summary>