remove existing utils and add daybreak utils

This commit is contained in:
Stephen Thiebaud committed 2025-12-10 00:41:39 -06:00
1 parent f2fcecb463
commit 3d9b40f0ff
8 files changed
+339 -107

No files matched your search

+1
View File
@@ -6,6 +6,7 @@ using ReLogic.Content;
using Terraria.ID;
using CalamityMod.Graphics;
using Terraria.Graphics.Shaders;
using Terraria.Graphics.Shaders;
namespace CalamityMod.Backgrounds
{
+5 -5
View File
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\tModLoader.targets"/>
<Import Project="..\tModLoader.targets" />
<PropertyGroup>
<LangVersion>latest</LangVersion>
@@ -8,7 +8,7 @@
<ItemGroup>
<PackageReference Include="Krafs.Publicizer" Version="2.3.0" PrivateAssets="all" ExcludeAssets="runtime"/>
<PackageReference Include="Krafs.Publicizer" Version="2.3.0" PrivateAssets="all" ExcludeAssets="runtime" />
</ItemGroup>
<!-- Taken from: <https://github.com/krafs/Publicizer/pull/128>. This is
@@ -16,15 +16,15 @@
emits a type also included by MonoMod.Utils. -->
<Target Name="RemoveIgnoresAccessChecksToAttributeCs" AfterTargets="PublicizeAssemblies">
<ItemGroup>
<FilesToRemove Include="@(Compile)" Condition="'%(Filename)' == 'IgnoresAccessChecksToAttribute'"/>
<FilesToRemove Include="@(Compile)" Condition="'%(Filename)' == 'IgnoresAccessChecksToAttribute'" />
</ItemGroup>
<ItemGroup>
<Compile Remove="@(FilesToRemove)"/>
<Compile Remove="@(FilesToRemove)" />
</ItemGroup>
</Target>
<ItemGroup>
<Publicize Include="tModLoader" IncludeCompilerGeneratedMembers="false" IncludeVirtualMembers="false"/>
<Publicize Include="tModLoader" IncludeCompilerGeneratedMembers="false" IncludeVirtualMembers="false" />
<DoNotPublicize Include="tModLoader:Terraria.Tile" />
<Publicize Include="FNA" IncludeCompilerGeneratedMembers="false" IncludeVirtualMembers="false" />
@@ -16,7 +16,6 @@ namespace CalamityMod.Systems
Main.QueueMainThreadAction(() =>
{
BakeBlendTexture_Inner(texture);
// TODO: [SAFEACTION] Bandaid fix
});
}
+5
View File
@@ -0,0 +1,5 @@
Everything in this directory is adapted from DAYBREAK and jointly authored by
tomat and syrup. Explicit permission is given to use this code in Calamity
against the AGPL 3.0 license.
<https://github.com/gold-meridian/daybreak-mod>
@@ -0,0 +1,93 @@
#nullable enable
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace CalamityMod.Utilities.Daybreak;
/// <summary>
/// Wraps the parameters of a <see cref="SpriteBatch"/> initialization,
/// permitting <see langword="null"/> values which may be replaced with
/// defaults when being converted to a snapshot.
/// </summary>
public struct SpriteBatchParameters
{
/// <summary>
/// The sort mode.
/// </summary>
public SpriteSortMode? SortMode { get; set; }
/// <summary>
/// The blend state.
/// </summary>
public BlendState? BlendState { get; set; }
/// <summary>
/// The sampler state.
/// </summary>
public SamplerState? SamplerState { get; set; }
/// <summary>
/// The depth stencil state.
/// </summary>
public DepthStencilState? DepthStencilState { get; set; }
/// <summary>
/// The rasterizer state.
/// </summary>
public RasterizerState? RasterizerState { get; set; }
/// <summary>
/// The custom effect, if applicable.
/// </summary>
public Effect? CustomEffect { get; set; }
/// <summary>
/// The transformation matrix.
/// </summary>
public Matrix? TransformMatrix { get; set; }
/// <summary>
/// Initializes a new set of parameters.
/// </summary>
public SpriteBatchParameters(
SpriteSortMode? sortMode = null,
BlendState? blendState = null,
SamplerState? samplerState = null,
DepthStencilState? depthStencilState = null,
RasterizerState? rasterizerState = null,
Effect? customEffect = null,
Matrix? transformMatrix = null
)
{
SortMode = sortMode;
BlendState = blendState;
SamplerState = samplerState;
DepthStencilState = depthStencilState;
RasterizerState = rasterizerState;
CustomEffect = customEffect;
TransformMatrix = transformMatrix;
}
/// <summary>
/// Creates a new <see cref="SpriteBatchSnapshot"/>, with
/// <see langword="null"/> values being replaced with the values
/// provided by <paramref name="defaultValues"/>.
/// </summary>
/// <param name="defaultValues">
/// Default values to populate <see langword="null"/>s with.
/// </param>
/// <returns>The new snapshot.</returns>
public readonly SpriteBatchSnapshot ToSnapshot(SpriteBatchSnapshot defaultValues)
{
return new SpriteBatchSnapshot(
SortMode ?? defaultValues.SortMode,
BlendState ?? defaultValues.BlendState,
SamplerState ?? defaultValues.SamplerState,
DepthStencilState ?? defaultValues.DepthStencilState,
RasterizerState ?? defaultValues.RasterizerState,
CustomEffect ?? defaultValues.CustomEffect,
TransformMatrix ?? defaultValues.TransformMatrix
);
}
}
+67
View File
@@ -0,0 +1,67 @@
#nullable enable
using System;
using Microsoft.Xna.Framework.Graphics;
namespace CalamityMod.Utilities.Daybreak;
/// <summary>
/// Starts a <see cref="SpriteBatch"/> and caches its old start parameters
/// if it was begun to restore it on disposal.
/// </summary>
public readonly struct SpriteBatchScope : IDisposable
{
private readonly SpriteBatch spriteBatch;
private readonly SpriteBatchSnapshot? oldState;
/// <summary>
/// Initializes a new scope. If the <see cref="SpriteBatch"/> has
/// already begun, its current parameters are saved and the
/// <see cref="SpriteBatch"/> is ended; the original parameters will
/// then be reapplied on disposal.
/// </summary>
/// <param name="spriteBatch">The <see cref="SpriteBatch"/>.</param>
public SpriteBatchScope(SpriteBatch spriteBatch)
{
this.spriteBatch = spriteBatch;
if (!spriteBatch.beginCalled)
{
return;
}
spriteBatch.End(out var old);
oldState = old;
}
/// <summary>
/// Ends the <see cref="SpriteBatch"/> and starts it with the old
/// parameters if it has already begun prior.
/// </summary>
public void Dispose()
{
if (spriteBatch.beginCalled)
{
spriteBatch.End();
}
if (oldState.HasValue)
{
spriteBatch.Begin(oldState.Value);
}
}
}
/// <summary>
/// Extensions to types for <see cref="SpriteBatch"/> scopes.
/// </summary>
public static class SpriteBatchScopeExtensions
{
/// <summary>
/// See <see cref="SpriteBatchScope"/>.
/// </summary>
public static SpriteBatchScope Scope(this SpriteBatch @this)
{
return new SpriteBatchScope(@this);
}
}
+168
View File
@@ -0,0 +1,168 @@
#nullable enable
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace CalamityMod.Utilities.Daybreak;
/// <summary>
/// A "snapshot" of the current state of a <see cref="SpriteBatch" />.
/// <br />
/// These values may be manipulated freely.
/// </summary>
/// <remarks>
/// This API exists for making preservation of a <see cref="SpriteBatch" />'s
/// state trivial.
/// <br />
/// The act of taking a snapshot through this object's constructor is pure
/// (that is, it has no side effects). It will not mutate the state of the
/// <see cref="SpriteBatch" /> being analyzed. If you intend to modify the
/// <see cref="SpriteBatch" />, use the APIs provided in
/// <see cref="SpriteBatchSnapshotExtensions" />.
/// </remarks>
public struct SpriteBatchSnapshot
{
/// <summary>
/// The sort mode.
/// </summary>
public SpriteSortMode SortMode { get; set; }
/// <summary>
/// The blend state.
/// </summary>
public BlendState BlendState { get; set; }
/// <summary>
/// The sampler state.
/// </summary>
public SamplerState SamplerState { get; set; }
/// <summary>
/// The depth stencil state.
/// </summary>
public DepthStencilState DepthStencilState { get; set; }
/// <summary>
/// The rasterizer state.
/// </summary>
public RasterizerState RasterizerState { get; set; }
/// <summary>
/// The custom effect, if applicable.
/// </summary>
public Effect? CustomEffect { get; set; }
/// <summary>
/// The transformation matrix.
/// </summary>
public Matrix TransformMatrix { get; set; }
/// <summary>
/// Creates a new <see cref="SpriteBatch"/> snapshot from raw
/// parameters.
/// </summary>
public SpriteBatchSnapshot(
SpriteSortMode sortMode,
BlendState blendState,
SamplerState samplerState,
DepthStencilState depthStencilState,
RasterizerState rasterizerState,
Effect? customEffect,
Matrix transformMatrix
)
{
SortMode = sortMode;
BlendState = blendState;
SamplerState = samplerState;
DepthStencilState = depthStencilState;
RasterizerState = rasterizerState;
CustomEffect = customEffect;
TransformMatrix = transformMatrix;
}
/// <summary>
/// Creates a new <see cref="SpriteBatch"/> snapshot from the current
/// settings of the given <see cref="SpriteBatch"/>.
/// </summary>
/// <param name="spriteBatch">
/// The <see cref="SpriteBatch" /> to take a snapshot of.
/// </param>
public SpriteBatchSnapshot(SpriteBatch spriteBatch)
{
SortMode = spriteBatch.sortMode;
BlendState = spriteBatch.blendState;
SamplerState = spriteBatch.samplerState;
DepthStencilState = spriteBatch.depthStencilState;
RasterizerState = spriteBatch.rasterizerState;
CustomEffect = spriteBatch.customEffect;
TransformMatrix = spriteBatch.transformMatrix;
}
/// <summary>
/// Initializes a set of parameters from this snapshot.
/// </summary>
public readonly SpriteBatchParameters ToParameters()
{
return new SpriteBatchParameters(
SortMode,
BlendState,
SamplerState,
DepthStencilState,
RasterizerState,
CustomEffect,
TransformMatrix
);
}
}
/// <summary>
/// Extensions to <see cref="SpriteBatch" /> using
/// <see cref="SpriteBatchSnapshot" /> instances.
/// </summary>
public static class SpriteBatchSnapshotExtensions
{
/// <param name="sb">The <see cref="SpriteBatch" />.</param>
extension(SpriteBatch sb)
{
/// <summary>
/// Takes a snapshot of the <see cref="SpriteBatch" /> and then ends the
/// <see cref="SpriteBatch" />/
/// </summary>
/// <param name="ss">The produced <see cref="SpriteBatchSnapshot" />.</param>
public void End(out SpriteBatchSnapshot ss)
{
ss = new SpriteBatchSnapshot(sb);
sb.End();
}
/// <summary>
/// Starts a <see cref="SpriteBatch" /> with the parameters from the
/// given <see cref="SpriteBatchSnapshot" />.
/// </summary>
/// <param name="ss">The <see cref="SpriteBatchSnapshot" /> to use.</param>
public void Begin(in SpriteBatchSnapshot ss)
{
sb.Begin(
ss.SortMode,
ss.BlendState,
ss.SamplerState,
ss.DepthStencilState,
ss.RasterizerState,
ss.CustomEffect,
ss.TransformMatrix
);
}
/// <summary>
/// Immediately ends and then starts the given <see cref="SpriteBatch" />
/// with the parameters from the given
/// <see cref="SpriteBatchSnapshot" />.
/// </summary>
/// <param name="ss">The <see cref="SpriteBatchSnapshot" /> to use.</param>
public void Restart(in SpriteBatchSnapshot ss)
{
sb.End();
sb.Begin(ss);
}
}
}
-101
View File
@@ -93,106 +93,5 @@ namespace CalamityMod
return true;
}
}
internal readonly record struct SpritebatchParameters(
BlendState BlendState,
SpriteSortMode SortMode,
DepthStencilState DepthStencilState,
SamplerState SamplerState,
RasterizerState RasterizerState,
Effect Effect,
Matrix TransformMatrix
);
internal static void End(this SpriteBatch spriteBatch, out SpritebatchParameters parameters)
{
spriteBatch.GetParameters(out parameters);
spriteBatch.End();
}
internal static void GetParameters(this SpriteBatch spriteBatch, out SpritebatchParameters parameters)
{
parameters = new SpritebatchParameters(
spriteBatch.blendState,
spriteBatch.sortMode,
spriteBatch.depthStencilState,
spriteBatch.samplerState,
spriteBatch.rasterizerState,
spriteBatch.customEffect,
spriteBatch.transformMatrix
);
}
internal static void Begin(this SpriteBatch spriteBatch, in SpritebatchParameters parameters)
{
spriteBatch.Begin(parameters.SortMode,
parameters.BlendState,
parameters.SamplerState,
parameters.DepthStencilState,
parameters.RasterizerState,
parameters.Effect,
parameters.TransformMatrix);
}
internal static void Restart(this SpriteBatch spriteBatch)
{
spriteBatch.End(out var sp);
spriteBatch.Begin(in sp);
}
internal static void Restart(this SpriteBatch spriteBatch, in SpritebatchParameters parameters)
{
spriteBatch.End();
spriteBatch.Begin(parameters);
}
internal class SpritebatchScope : IDisposable
{
private readonly SpritebatchParameters _parameters;
private readonly SpriteBatch _sb;
/// <summary>
/// Takes in a spritebatch and gets <see cref="SpritebatchParameters"/> from it without ending or otherwise mutating it.
/// </summary>
/// <param name="sb"></param>
public SpritebatchScope(SpriteBatch sb)
{
_sb = sb;
_sb.GetParameters(out _parameters);
}
/// <summary>
/// Takes in a spritebatch and gets <see cref="SpritebatchParameters"/> from it before restarting it with the input <see cref="SpritebatchParameters"/>.
/// </summary>
/// <param name="sb"></param>
/// <param name="parameters"></param>
public SpritebatchScope(SpriteBatch sb, SpritebatchParameters parameters)
{
_sb = sb;
_sb.GetParameters(out _parameters);
_sb.Restart(parameters);
}
/// <summary>
/// SafeBegin equivalent; takes in a <see cref="SpriteBatch"/>, gets <see cref="SpritebatchParameters"/> from it, and then uses the <see cref="SpriteBatch"/> and other parameters to start a new <see cref="SpriteBatch"/>.
/// </summary>
/// <param name="sb"></param>
/// <param name="sortMode"></param>
/// <param name="settings"></param>
/// <param name="effect"></param>
/// <param name="transformMatrix"></param>
public SpritebatchScope(SpriteBatch sb, SpriteSortMode sortMode, BatchSetting settings, Effect effect, Matrix transformMatrix, bool end = false)
{
_sb = sb;
_sb.GetParameters(out _parameters);
if (end) {
_sb.End();
}
_sb.Begin(sortMode, settings.blendState, settings.samplerState, settings.depthStencilState, settings.rasterizerState ?? Main.Rasterizer, effect, transformMatrix);
}
public void Dispose()
{
_sb.Restart(_parameters);
}
}
}
}