Skip to content

Commit

Permalink
Compatibility with VGA/Music and EGA downgrade patches #64
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenyx4 committed Jul 14, 2021
1 parent c05dad2 commit a0ef111
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
44 changes: 43 additions & 1 deletion U4DosRandomizer/Avatar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace U4DosRandomizer
{
public class Avatar
{
private const string upgradeFileHash = "f341263de422dba816a0dbbcde5dfe350e08dcb40cdf8147ed8f65aad5737d48";

private const string filename = "AVATAR.EXE";
private byte[] avatarBytes;

Expand All @@ -19,10 +21,14 @@ public Avatar(SpoilerLog spoilerLog)
SpoilerLog = spoilerLog;
}

public void Load(string path, UltimaData data, IWorldMap worldMap)
public void Load(string path, UltimaData data, IWorldMap worldMap, Flags flags)
{
var file = Path.Combine(path, filename);

if(flags.VGAPatch && HashHelper.BytesToString(HashHelper.GetHashSha256(file)) == upgradeFileHash)
{
DowngradeVGAPatch(file);
}
FileHelper.TryBackupOriginalFile(file);

// Apply delta file to create new file
Expand Down Expand Up @@ -253,6 +259,36 @@ public void Load(string path, UltimaData data, IWorldMap worldMap)
}
}

private void DowngradeVGAPatch(string file)
{
byte[] bytes;
using (var avatarStream = new System.IO.FileStream(file, System.IO.FileMode.Open))
{
bytes = avatarStream.ReadAllBytes();
}

var originalRuneNums = new char[] { '1', '2', '0', '1', '2', '1', '3', '4' };
for(int i = 0; i < 8; i++)
{
bytes[AvatarOffsetsOriginal.RUNE_IMAGE_INDEX+5+i*7] = (byte)originalRuneNums[i];
}
bytes[AvatarOffsetsOriginal.RUNE_IMAGE_INDEX2] = 0x35;

using (var avatarOut = new System.IO.BinaryWriter(new System.IO.FileStream(file, System.IO.FileMode.Truncate)))
{
avatarOut.Write(bytes);
}
}

private void ApplyVGAPatch()
{
for (int i = 0; i < 8; i++)
{
avatarBytes[AvatarOffsetsNew.RUNE_IMAGE_INDEX + 5 + i * 7] = (byte)((i + 1).ToString()[0]);
}
avatarBytes[AvatarOffsetsNew.RUNE_IMAGE_INDEX2] = 0x30;
}

internal static void Restore(string path)
{
var file = Path.Combine(path, filename);
Expand Down Expand Up @@ -588,8 +624,14 @@ public void Update(UltimaData data, Flags flags)
avatarBytes[AvatarOffset.ABYSS_PARTY_COMPARISON] = 0x76;
avatarBytes[AvatarOffset.LB_PARTY_COMPARISON] = 0x00;
}

if (flags.VGAPatch)
{
ApplyVGAPatch();
}
}


public void Save(string path)
{
var exePath = Path.Combine(path, filename);
Expand Down
3 changes: 3 additions & 0 deletions U4DosRandomizer/AvatarOffsetsNew.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ 0x2 1 Y Coordinate of Item

public int SPELL_RECIPE_OFFSET { get; } = 0x11CF2; //11A29

public static int RUNE_IMAGE_INDEX2 { get; } = 0xFE12; // FB85
public static int RUNE_IMAGE_INDEX { get; } = 0x17853; // 17551

public int BLINK_CAST_EXCLUSION_X1_OFFSET { get; } = 0x68BB; // New

public int BLINK_CAST_EXCLUSION_X2_OFFSET { get { return BLINK_CAST_EXCLUSION_X1_OFFSET + 4; } } // New
Expand Down
2 changes: 2 additions & 0 deletions U4DosRandomizer/AvatarOffsetsOriginal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ 0x2 1 Y Coordinate of Item
public int ABYSS_EJECTION_LOCATIONS_Y { get; } = 0xFEBB;

public int SPELL_RECIPE_OFFSET { get; } = 0x11A29;
public static int RUNE_IMAGE_INDEX2 { get; } = 0xFB85;
public static int RUNE_IMAGE_INDEX { get; } = 0x17551;

// Originally blink didn't have an upperbound https://github.com/ergonomy-joe/u4-decompiled/blob/c2c2108fa3bb346bcd1d8c207c526f33a4c8f5ef/SRC/U4_SPELL.C#L179
public int BLINK_CAST_EXCLUSION_X1_OFFSET => throw new NotImplementedException();
Expand Down
1 change: 1 addition & 0 deletions U4DosRandomizer/Flags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public Flags(int seed, int version)
public bool Sextant { get; internal set; }
public bool ClothMap { get; internal set; }
public bool SpoilerLog { get; internal set; }
public bool VGAPatch { get; internal set; }

public List<int> SupportedVersions = new List<int>() { 9 };

Expand Down
7 changes: 6 additions & 1 deletion U4DosRandomizer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ static void Main(string[] args)
"--clothMap",
"Cloth map of the world.",
CommandOptionType.NoValue);
CommandOption vgaPatchArg = commandLineApplication.Option(
"--vgaPatch",
"VGA patch compatibility.",
CommandOptionType.NoValue);

CommandOption spoilerLogArg = commandLineApplication.Option(
"--spoilerLog",
Expand Down Expand Up @@ -285,6 +289,7 @@ static void Main(string[] args)
flags.Sextant = sextantArg.HasValue();
flags.ClothMap = clothMapArg.HasValue();
flags.SpoilerLog = spoilerLogArg.HasValue();
flags.VGAPatch = vgaPatchArg.HasValue();
Randomize(seed, path, flags, encodedArg.Value());
//Console.WriteLine("Seed: " + seed);
//var random = new Random(seed);
Expand Down Expand Up @@ -362,7 +367,7 @@ private static void Randomize(int seed, string path, Flags flags, string encoded
worldMap.Load(path, randomValues[0], randomValues[1], randomValues[2], ultimaData);

var avatar = new Avatar(spoilerLog);
avatar.Load(path, ultimaData, worldMap);
avatar.Load(path, ultimaData, worldMap, flags);

var title = new Title(spoilerLog);
title.Load(path, ultimaData);
Expand Down

0 comments on commit a0ef111

Please sign in to comment.