Skip to content

Commit

Permalink
General refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
terminal-cs committed Jan 28, 2023
1 parent 98e0e56 commit 0152d4d
Show file tree
Hide file tree
Showing 22 changed files with 1,025 additions and 634 deletions.
32 changes: 4 additions & 28 deletions PrismBinary/Formats/ELF/ELFFile32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,10 @@ public ELFFile32(byte[] Binary)
fixed (byte* P = Binary)
{
// Load main file header.
Header = new((ELFHeader32*)P);
SectionHeaders = new();
StringTable = new();
ELFHeader32 Header = new((ELFHeader32*)P);

// Assign entry point.
Main = (delegate* unmanaged<void>)Header.EntryPoint;

// Load the first section header.
ELFSectionHeader32 SHHeader = new((ELFSectionHeader32*)(P + Header.SHOffset));

// Load all section headers.
for (int I = 0; I < Header.SHCount; I++)
{
ELFSectionHeader32 X = new(&SHHeader + I);
if (X.Type == ELFSectionType.StringTable)
{
StringTable.Add(X.Offset);
}
SectionHeaders.Add(X);
}
Main = (delegate* unmanaged<void>)(Header.EntryPoint);

// Load the first program header.
ELFProgramHeader32* PHeader = (ELFProgramHeader32*)(P + Header.PHOffset);
Expand All @@ -48,7 +32,7 @@ public ELFFile32(byte[] Binary)
case ELFProgramType.Null:
break;
case ELFProgramType.Load:
Buffer.MemoryCopy((byte*)PHeader->VAddress, P + PHeader->Offset, PHeader->FileSize, PHeader->FileSize);
Buffer.MemoryCopy(PHeader + PHeader->Offset, (byte*)PHeader->VAddress, PHeader->FileSize, PHeader->FileSize);
break;
default:
throw new ArgumentException("Unsupported program type!");
Expand All @@ -65,13 +49,5 @@ public ELFFile32(byte[] Binary)
public delegate* unmanaged<void> Main;

#endregion

#region Fields

public List<ELFSectionHeader32> SectionHeaders;
public List<uint> StringTable;
public ELFHeader32 Header;

#endregion
}
}
}
28 changes: 2 additions & 26 deletions PrismBinary/Formats/ELF/ELFFile64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,10 @@ public ELFFile64(byte[] Binary)
fixed (byte* P = Binary)
{
// Load main file header.
Header = new((ELFHeader64*)P);
SectionHeaders = new();
StringTable = new();
ELFHeader64 Header = new((ELFHeader64*)P);

// Assign entry point.
Main = (delegate* unmanaged<void>)Header.EntryPoint;

// Load the first section header.
ELFSectionHeader64 SHHeader = new((ELFSectionHeader64*)(P + Header.SHOffset));

// Load all section headers.
for (int I = 0; I < Header.SHCount; I++)
{
ELFSectionHeader64 X = new(&SHHeader + I);
if (X.Type == ELFSectionType.StringTable)
{
StringTable.Add(X.Offset);
}
SectionHeaders.Add(X);
}
Main = (delegate* unmanaged<void>)(Header.EntryPoint + 512);

// Load the first program header.
ELFProgramHeader64* PHeader = (ELFProgramHeader64*)(P + Header.PHOffset);
Expand Down Expand Up @@ -65,13 +49,5 @@ public ELFFile64(byte[] Binary)
public delegate* unmanaged<void> Main;

#endregion

#region Fields

public List<ELFSectionHeader64> SectionHeaders;
public List<ulong> StringTable;
public ELFHeader64 Header;

#endregion
}
}
9 changes: 3 additions & 6 deletions PrismBinary/Formats/ELF/Structure/ELFHeader/ELFHeader32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public unsafe struct ELFHeader32
{
public ELFHeader32(ELFHeader32* Original)
{
MagicNumber = Original->MagicNumber;
ClassType = Original->ClassType;
EndianType = Original->EndianType;
Version = Original->Version;
Expand All @@ -32,11 +33,7 @@ public ELFHeader32(ELFHeader32* Original)

public bool IsValid()
{
return
Magic[0] == 0x7f &&
Magic[1] == 0x45 &&
Magic[2] == 0x4c &&
Magic[3] == 0x46;
return MagicNumber == BitConverter.ToUInt32(new byte[] { 0x7F, 0x45, 0x4C, 0x46 });
}

#endregion
Expand All @@ -45,7 +42,7 @@ public bool IsValid()

#region Identifier [14 bytes]

public fixed char Magic[4];
public uint MagicNumber;
public ELFClassType ClassType;
public ELFEndianType EndianType;
private readonly byte Version;
Expand Down
9 changes: 3 additions & 6 deletions PrismBinary/Formats/ELF/Structure/ELFHeader/ELFHeader64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public unsafe struct ELFHeader64
{
public ELFHeader64(ELFHeader64* Original)
{
MagicNumber = Original->MagicNumber;
ClassType = Original->ClassType;
EndianType = Original->EndianType;
Version = Original->Version;
Expand All @@ -32,11 +33,7 @@ public ELFHeader64(ELFHeader64* Original)

public bool IsValid()
{
return
Magic[0] == 0x7f &&
Magic[1] == 0x45 &&
Magic[2] == 0x4c &&
Magic[3] == 0x46;
return MagicNumber == 0x7F454C46;
}

#endregion
Expand All @@ -45,7 +42,7 @@ public bool IsValid()

#region Identifier [14 bytes]

public fixed char Magic[4];
public uint MagicNumber;
public ELFClassType ClassType;
public ELFEndianType EndianType;
private readonly byte Version;
Expand Down
3 changes: 2 additions & 1 deletion PrismBinary/Formats/ELF/Structure/ELFSectionFlagsType.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
namespace PrismBinary.Formats.ELF.Structure
{
[Flags]
public enum ELFSectionFlagsType : uint
{
None,
Write,
Allocate,
ExexInstruction,
Executable,
}
}
175 changes: 175 additions & 0 deletions PrismGraphics/Animators/FadeControler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
namespace PrismGraphics.Animators
{
/// <summary>
/// The class used to create color fading animations.
/// WIP : Not working properly!
/// </summary>
public class FadeControler
{
/// <summary>
/// Creates a new instance of the <see cref="FadeControler"/> class.
/// </summary>
/// <param name="BeginColor">The color to start fading from.</param>
/// <param name="TargetColor">The color to fade to.</param>
/// <param name="Duration">The duration to fade the colors.</param>
/// <param name="Mode">The mode to fade the colors.</param>
public FadeControler(Color BeginColor, Color TargetColor, TimeSpan Duration, FadeMode Mode = FadeMode.Linear)
{
// Assign public API fields.
this.BeginColor = BeginColor;
this.TargetColor = TargetColor;
this.Duration = Duration;
this.Mode = Mode;
IsEnabled = false;

// Assign private API fields.
StepA = (TargetColor.A - BeginColor.A) / (Duration.Milliseconds * 1000000);
StepR = (TargetColor.R - BeginColor.R) / (Duration.Milliseconds * 1000000);
StepG = (TargetColor.G - BeginColor.G) / (Duration.Milliseconds * 1000000);
StepB = (TargetColor.B - BeginColor.B) / (Duration.Milliseconds * 1000000);
A = BeginColor.A;
R = BeginColor.R;
G = BeginColor.G;
B = BeginColor.B;

// Adjust step deltas based on fade mode.
switch (Mode)
{
case FadeMode.FastInSlowOut:
Duration /= 2;
StepA *= 2;
StepR *= 2;
StepG *= 2;
StepB *= 2;
break;
case FadeMode.FastOutSlowIn:
Duration *= 2;
StepA /= 2;
StepR /= 2;
StepG /= 2;
StepB /= 2;
break;
}

// Create an interupt timer.
Cosmos.HAL.Global.PIT.RegisterTimer(new(Next, (ulong)((TargetColor - BeginColor).Brightness / (Duration.TotalMilliseconds * 1000000)), true));
}

#region Properties

/// <summary>
/// The current color of the fade, will always be present.
/// </summary>
public Color ResultColor
{
get
{
return ((byte)A, (byte)R, (byte)G, (byte)B);
}
}

#endregion

#region Methods

/// <summary>
/// Restarts the interpolation, starts it if it was stopped.
/// </summary>
public void Restart()
{
// Reset the result color value.
A = BeginColor.A;
R = BeginColor.R;
G = BeginColor.G;
B = BeginColor.B;

// Enable the animator.
IsEnabled = true;
}

/// <summary>
/// Resets the interpolation, does not change playing state.
/// </summary>
public void Reset()
{
// Reset the result color value.
A = BeginColor.A;
R = BeginColor.R;
G = BeginColor.G;
B = BeginColor.B;
}

/// <summary>
/// Starts the interpolation, doesn't reset the interpolation.
/// </summary>
public void Start()
{
IsEnabled = true;
}

/// <summary>
/// Pauses the interpolation, doesn't reset the interpolation.
/// </summary>
public void Pause()
{
IsEnabled = false;
}

/// <summary>
/// Stops the interpolation, resets the interpolation and pauses it.
/// </summary>
public void Stop()
{
// Reset the result color value.
A = BeginColor.A;
R = BeginColor.R;
G = BeginColor.G;
B = BeginColor.B;

// Disable the animator.
IsEnabled = false;
}

/// <summary>
/// Updates the color to the next of the interpolation.
/// </summary>
private void Next()
{
// Return if no operation needs to be done.
if (!IsEnabled)
{
return;
}
if (ResultColor == TargetColor)
{
IsEnabled = false;
return;
}

A += (byte)StepA;
R += (byte)StepR;
G += (byte)StepG;
B += (byte)StepB;
}

#endregion

#region Fields

// Public API fields.
public Color BeginColor;
public Color TargetColor;
public TimeSpan Duration;
public FadeMode Mode;
public bool IsEnabled;

// Private API fields.
private readonly double StepA;
private readonly double StepR;
private readonly double StepG;
private readonly double StepB;
private double A, R, G, B;

#endregion
}
}
15 changes: 15 additions & 0 deletions PrismGraphics/Animators/FadeMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace PrismGraphics.Animators
{
/// <summary>
/// This is an enum to keep a list of all the possible fade types.
/// </summary>
public enum FadeMode
{
/// <summary>
/// This mode fades a color in fast but slows down the closer it gets to the target color.
/// </summary>
FastInSlowOut,
FastOutSlowIn,
Linear,
}
}
Loading

0 comments on commit 0152d4d

Please sign in to comment.