Reduce potentially unnecessary calls to Sync() by always forcing Sync() for new blocks

This commit is contained in:
NGnius (Graham) 2020-05-21 20:00:31 -04:00
parent e8f59e8641
commit 4e08acf44c
10 changed files with 34 additions and 12 deletions

View file

@ -71,13 +71,35 @@ namespace GamecraftModdingAPI
public Block(EGID id)
{
Id = id;
if (!BlockEngine.BlockExists(Id))
{
Sync();
if (!BlockEngine.BlockExists(Id))
{
throw new BlockDoesNotExistException($"Block {Id.entityID} must be placed using PlaceNew(...) since it does not exist yet");
}
}
}
public Block(uint id)
{
Id = new EGID(id, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
if (!BlockEngine.BlockExists(Id))
{
Sync();
if (!BlockEngine.BlockExists(Id))
{
throw new BlockDoesNotExistException($"Block {Id.entityID} must be placed using PlaceNew(...) since it does not exist yet");
}
}
}
/// <summary>
/// Synchronize newly created entity components with entities DB.
/// This forces a partial game tick, so it may be slow.
/// This also has the potential to make Gamecraft unstable.
/// Use this sparingly.
/// </summary>
protected static void Sync()
{
DeterministicStepCompositionRootPatch.SubmitEntitiesNow();
@ -219,10 +241,8 @@ namespace GamecraftModdingAPI
/// Convert the block to a specialised block class.
/// </summary>
/// <returns>The block.</returns>
/// <param name="sameTick">Force an entity sync when <c>true</c>.
/// Only set this to <c>false</c> when the block was not placed the same tick this was called.</param>
/// <typeparam name="T">The specialised block type.</typeparam>
public T Specialise<T>(bool sameTick = true) where T : Block
public T Specialise<T>() where T : Block
{
// What have I gotten myself into?
// C# can't cast to a child of Block unless the object was originally that child type
@ -233,7 +253,6 @@ namespace GamecraftModdingAPI
{
throw new BlockSpecializationException("Specialized block constructor does not accept an EGID");
}
if (sameTick) Sync();
return (T)ctor.Invoke(new object[] { Id });
}

View file

@ -40,4 +40,15 @@ namespace GamecraftModdingAPI.Blocks
{
}
}
public class BlockDoesNotExistException : BlockException
{
public BlockDoesNotExistException()
{
}
public BlockDoesNotExistException(string message) : base(message)
{
}
}
}

View file

@ -21,7 +21,6 @@ namespace GamecraftModdingAPI.Blocks
{
EGID id = PlacementEngine.PlaceBlock(BlockIDs.ConsoleBlock, color, darkness,
position, uscale, scale, player, rotation);
Sync();
return new ConsoleBlock(id);
}
catch (Exception e)

View file

@ -29,7 +29,6 @@ namespace GamecraftModdingAPI.Blocks
{
EGID id = PlacementEngine.PlaceBlock(block, color, darkness,
position, uscale, scale, player, rotation);
Sync();
return new Motor(id);
}
catch (Exception e)

View file

@ -29,7 +29,6 @@ namespace GamecraftModdingAPI.Blocks
{
EGID id = PlacementEngine.PlaceBlock(block, color, darkness,
position, uscale, scale, player, rotation);
Sync();
return new Piston(id);
}
catch (Exception e)

View file

@ -29,7 +29,6 @@ namespace GamecraftModdingAPI.Blocks
{
EGID id = PlacementEngine.PlaceBlock(block, color, darkness,
position, uscale, scale, player, rotation);
Sync();
return new Servo(id);
}
catch (Exception e)

View file

@ -29,7 +29,6 @@ namespace GamecraftModdingAPI.Blocks
{
EGID id = PlacementEngine.PlaceBlock(block, color, darkness,
position, uscale, scale, player, rotation);
Sync();
return new SignalingBlock(id);
}
catch (Exception e)

View file

@ -31,7 +31,6 @@ namespace GamecraftModdingAPI.Blocks
{
EGID id = PlacementEngine.PlaceBlock(block, color, darkness,
position, uscale, scale, player, rotation);
Sync();
return new SpawnPoint(id);
}
catch (Exception e)

View file

@ -22,7 +22,6 @@ namespace GamecraftModdingAPI.Blocks
{
EGID id = PlacementEngine.PlaceBlock(BlockIDs.TextBlock, color, darkness,
position, uscale, scale, player, rotation);
Sync();
return new TextBlock(id);
}
catch (Exception e)

View file

@ -26,7 +26,6 @@ namespace GamecraftModdingAPI.Blocks
{
EGID id = PlacementEngine.PlaceBlock(BlockIDs.Timer, color, darkness,
position, uscale, scale, player, rotation);
Sync();
return new Timer(id);
}
catch (Exception e)