Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom ship sprite options #55

Merged
merged 6 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Randomizer.App/EmbeddedResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Randomizer.App
{
public static class EmbeddedResource
{
public static Stream GetStream<T>(string resourceName)
{
var assembly = typeof(T).Assembly;
return assembly.GetManifestResourceStream(typeof(T), resourceName);
}
}
}
38 changes: 38 additions & 0 deletions src/Randomizer.App/Patches/IpsPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.IO;

namespace Randomizer.App.Patches
{
internal static class IpsPatch
{
public static Stream GetStream(string name)
{
var type = typeof(IpsPatch);
return type.Assembly.GetManifestResourceStream(type, name);
}

/// <summary>
/// Gets a stream for the IPS patch that enables custom ship sprite support.
/// </summary>
/// <returns>A new stream that contains the IPS patch.</returns>
public static Stream CustomShip() => GetStream("custom_ship.ips");

/// <summary>
/// Gets a stream for the IPS patch that enables MSU-1 support.
/// </summary>
/// <returns>A new stream that contains the IPS patch.</returns>
public static Stream MsuSupport() => GetStream("msu1-v6.ips");

/// <summary>
/// Gets a stream for the IPS patch that contains the base SMZ3 ROM patches.
/// </summary>
/// <returns>A new stream that contains the IPS patch.</returns>
public static Stream Smz3() => GetStream("zsm.ips");

/// <summary>
/// Gets a stream for the IPS patch the contains the Respin patch.
/// </summary>
/// <returns>A new stream that contains the IPS patch.</returns>
public static Stream Respin() => GetStream("spinjumprestart.ips");
}
}
Binary file added src/Randomizer.App/Patches/custom_ship.ips
Binary file not shown.
File renamed without changes.
File renamed without changes.
10 changes: 7 additions & 3 deletions src/Randomizer.App/Randomizer.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

<ItemGroup>
<None Remove="chozo20.ico" />
<None Remove="custom_ship.ips" />
<None Remove="kirbyship.ips" />
<None Remove="metroid.ips" />
<None Remove="msu1-v6.ips" />
<None Remove="Resources\Font Awesome 5 Free-Regular-400.otf" />
<None Remove="Resources\Font Awesome 5 Free-Solid-900.otf" />
Expand All @@ -31,9 +34,10 @@

<ItemGroup>
<Resource Include="chozo20.ico" />
<EmbeddedResource Include="msu1-v6.ips" />
<EmbeddedResource Include="spinjumprestart.ips" />
<EmbeddedResource Include="zsm.ips" />
<EmbeddedResource Include="Patches\custom_ship.ips" />
<EmbeddedResource Include="Patches\msu1-v6.ips" />
<EmbeddedResource Include="Patches\spinjumprestart.ips" />
<EmbeddedResource Include="Patches\zsm.ips" />
</ItemGroup>

<ItemGroup>
Expand Down
23 changes: 20 additions & 3 deletions src/Randomizer.App/RomGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text.Json;
using System.Threading;

using Randomizer.App.Patches;
using Randomizer.App.ViewModels;
using Randomizer.Shared;
using Randomizer.Shared.Models;
Expand Down Expand Up @@ -87,10 +88,11 @@ protected byte[] GenerateRomBytes(RandomizerOptions options, out SeedData seed)
{
seed = GenerateSeed(options);

var assembly = GetType().Assembly;
var smIpsFiles = new List<Stream>();
if (options.PatchOptions.CasualSuperMetroidPatches)
{
smIpsFiles.Add(GetType().Assembly.GetManifestResourceStream("Randomizer.App.spinjumprestart.ips"));
smIpsFiles.Add(IpsPatch.Respin());
}

byte[] rom;
Expand All @@ -107,14 +109,29 @@ protected byte[] GenerateRomBytes(RandomizerOptions options, out SeedData seed)
smIpsFiles.ForEach(x => x.Close());
}

using (var ips = GetType().Assembly.GetManifestResourceStream("Randomizer.App.zsm.ips"))
using (var ips = IpsPatch.Smz3())
{
Rom.ApplyIps(rom, ips);
}
Rom.ApplySeed(rom, seed.Worlds[0].Patches);

options.PatchOptions.SamusSprite.ApplyTo(rom);
options.PatchOptions.LinkSprite.ApplyTo(rom);

if (options.PatchOptions.ShipPatch?.FileName != null)
{

var shipPatchFileName = Path.Combine(AppContext.BaseDirectory, "Sprites", "Ships", options.PatchOptions.ShipPatch.FileName);
if (File.Exists(shipPatchFileName))
{
using var customShipBasePatch = IpsPatch.CustomShip();
Rom.ApplySuperMetroidIps(rom, customShipBasePatch);

using var shipPatch = File.OpenRead(shipPatchFileName);
Rom.ApplySuperMetroidIps(rom, shipPatch);
}
}

return rom;
}
/// <summary>
Expand Down Expand Up @@ -250,7 +267,7 @@ private bool EnableMsu1Support(RandomizerOptions options, byte[] rom, string rom
return false;
}

using (var ips = GetType().Assembly.GetManifestResourceStream("Randomizer.App.msu1-v6.ips"))
using (var ips = IpsPatch.MsuSupport())
{
Rom.ApplyIps(rom, ips);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Randomizer.App/ShipSprite.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Randomizer.App
{
public record ShipSprite(string DisplayName, string FileName)
{
public static readonly ShipSprite DefaultShip = new("Default", null);
}
}
Binary file not shown.
Binary file added src/Randomizer.App/Sprites/Samus/cuphead.rdc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions src/Randomizer.App/ViewModels/PatchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class PatchOptions : INotifyPropertyChanged
public Sprite SamusSprite { get; set; }
= Sprite.DefaultSamus;

public ShipSprite ShipPatch { get; set; }
= ShipSprite.DefaultShip;

public string Msu1Path
{
get => _msu1Path;
Expand Down
7 changes: 7 additions & 0 deletions src/Randomizer.App/Windows/GenerateRomWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@
SelectedItem="{Binding LinkSprite}" />
</controls:LabeledControl>

<controls:LabeledControl Text="Ship sprite:">
<ComboBox x:Name="ShipSpriteDropdown"
ItemsSource="{Binding ShipSprites, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:GenerateRomWindow}}}"
DisplayMemberPath="DisplayName"
SelectedItem="{Binding ShipPatch}" />
</controls:LabeledControl>

<controls:LabeledControl Text="Heart color:">
<ComboBox SelectedItem="{Binding HeartColor}"
ItemsSource="{Binding Source={local:EnumBindingSource {x:Type smz3:HeartColor}}}"
Expand Down
12 changes: 12 additions & 0 deletions src/Randomizer.App/Windows/GenerateRomWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public GenerateRomWindow(IServiceProvider serviceProvider)

SamusSprites.Add(Sprite.DefaultSamus);
LinkSprites.Add(Sprite.DefaultLink);
ShipSprites.Add(ShipSprite.DefaultShip);
_loadSpritesTask = Task.Run(() => LoadSprites())
.ContinueWith(_ => Trace.WriteLine("Finished loading sprites."));

Expand All @@ -53,6 +54,8 @@ public GenerateRomWindow(IServiceProvider serviceProvider)

public ObservableCollection<Sprite> LinkSprites { get; } = new();

public ObservableCollection<ShipSprite> ShipSprites { get; } = new();

public RandomizerOptions Options
{
get => _options;
Expand Down Expand Up @@ -105,6 +108,10 @@ public void LoadSprites()
.Select(x => Sprite.LoadSprite(x))
.OrderBy(x => x.Name);

var shipSpritesPath = Path.Combine(AppContext.BaseDirectory, "Sprites", "Ships");
var shipSprites = Directory.EnumerateFiles(shipSpritesPath, "*.ips", SearchOption.AllDirectories)
.Select(x => new ShipSprite(Path.GetFileNameWithoutExtension(x), Path.GetRelativePath(shipSpritesPath, x)));

Dispatcher.Invoke(() =>
{
foreach (var sprite in sprites)
Expand All @@ -120,6 +127,11 @@ public void LoadSprites()
break;
}
}

foreach (var ship in shipSprites)
{
ShipSprites.Add(ship);
}
}, DispatcherPriority.Loaded);
}

Expand Down
31 changes: 26 additions & 5 deletions src/Randomizer.SMZ3/FileData/Rom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace Randomizer.SMZ3.FileData
{
Expand Down Expand Up @@ -63,23 +64,43 @@ public static byte[] ExpandRom(Stream rom, int size)
}

public static void ApplyIps(byte[] rom, Stream ips)
=> ApplyIps(rom, ips, offset => offset);

// Applying a Super Metroid IPS to a combined SMZ3 ROM requires some switching around
public static void ApplySuperMetroidIps(byte[] rom, Stream ips)
=> ApplyIps(rom, ips, TranslateSuperMetroidOffset);

public static int TranslateSuperMetroidOffset(int offset)
{
if (offset >= 0x00300000)
throw new ArgumentOutOfRangeException(nameof(offset), offset, "Super Metroid offset must be below 0x00300000"); ;

var isHiBank = offset < 0x200000;
var baseOffset = isHiBank ? offset : offset - 0x200000;
var i = baseOffset / 0x8000;
if (isHiBank)
i++;
return baseOffset + (i * 0x8000);
}

public static void ApplyIps(byte[] rom, Stream ips, Func<int, int> translateOffset)
{
const int header = 5;
const int footer = 3;
ips.Seek(header, SeekOrigin.Begin);
while (ips.Position + footer < ips.Length)
{
var offset = ips.ReadByte() << 16 | ips.ReadByte() << 8 | ips.ReadByte();
var size = ips.ReadByte() << 8 | ips.ReadByte();
var offset = (ips.ReadByte() << 16) | (ips.ReadByte() << 8) | ips.ReadByte();
var size = (ips.ReadByte() << 8) | ips.ReadByte();
if (size > 0)
{
ips.Read(rom, offset, size);
ips.Read(rom, translateOffset(offset), size);
}
else
{
var rleSize = ips.ReadByte() << 8 | ips.ReadByte();
var rleSize = (ips.ReadByte() << 8) | ips.ReadByte();
var rleByte = (byte)ips.ReadByte();
Array.Fill(rom, rleByte, offset, rleSize);
Array.Fill(rom, rleByte, translateOffset(offset), rleSize);
}
}
}
Expand Down