Skip to content

Commit

Permalink
Add custom content folder
Browse files Browse the repository at this point in the history
  • Loading branch information
IntelOrca committed Dec 17, 2023
1 parent b3ca5d0 commit 727b564
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 96 deletions.
10 changes: 5 additions & 5 deletions IntelOrca.Biohazard.BioRand.Tests/TestRandomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,21 @@ protected void Randomize(RandoConfig config)
var dataPath = Path.Combine(Environment.CurrentDirectory, "..", "..", "..", "..", "IntelOrca.Biohazard.BioRand", "data");
Environment.SetEnvironmentVariable("BIORAND_DATA", dataPath);

var reInstall = GetInstallConfig();
var rando = GetRandomizer();
rando.Generate(config, reInstall, new NullProgress());
rando.Generate(config, new NullProgress());
}

private BaseRandomiser GetRandomizer()
{
var installConfig = GetInstallConfig();
switch (Game)
{
case 1:
return new Re1Randomiser(null);
return new Re1Randomiser(installConfig, null);
case 2:
return new Re2Randomiser(null);
return new Re2Randomiser(installConfig, null);
case 3:
return new Re3Randomiser(null);
return new Re3Randomiser(installConfig, null);
default:
throw new Exception("Invalid game");
}
Expand Down
38 changes: 28 additions & 10 deletions IntelOrca.Biohazard.BioRand/BaseRandomiser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace IntelOrca.Biohazard.BioRand
{
public abstract class BaseRandomiser
{
protected ReInstallConfig InstallConfig { get; }
protected IBgCreator? BgCreator { get; }

protected MemoryStream ExePatch { get; } = new MemoryStream();
Expand All @@ -31,8 +32,9 @@ public abstract class BaseRandomiser
protected abstract RdtId[] GetRdtIds(string dataPath);
protected abstract string GetRdtPath(string dataPath, RdtId rdtId, int player, bool mod);

public BaseRandomiser(IBgCreator? bgCreator)
public BaseRandomiser(ReInstallConfig installConfig, IBgCreator? bgCreator)
{
InstallConfig = installConfig;
BgCreator = bgCreator;
}

Expand All @@ -50,10 +52,24 @@ internal DataManager DataManager
#endif
dataPath = Path.Combine(basePath, "data");
}
return new DataManager(dataPath);

var dataPaths = new List<string>();
if (InstallConfig.EnableCustomContent)
{
var userDataPath = Path.Combine(GetSettingsDirectory(), "data");
dataPaths.Add(userDataPath);
}
dataPaths.Add(dataPath);
return new DataManager(dataPaths.ToArray());
}
}

private static string GetSettingsDirectory()
{
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
return Path.Combine(appData, "biorand");
}

private void SetInventory(int player, RandomInventory? inventory)
{
if (inventory == null)
Expand Down Expand Up @@ -163,7 +179,7 @@ private GameData ReadGameData(FileRepository fileRepository, int player, RdtId[]
return new GameData(rdts.ToArray());
}

public void Generate(RandoConfig config, ReInstallConfig reConfig, IRandoProgress progress)
public void Generate(RandoConfig config, IRandoProgress progress)
{
config = config.Clone();
if (config.Version < RandoConfig.LatestVersion)
Expand All @@ -175,14 +191,15 @@ public void Generate(RandoConfig config, ReInstallConfig reConfig, IRandoProgres
throw new BioRandVersionException($"This seed was generated with a newer version of the randomizer and cannot be played.");
}

var reConfig = InstallConfig;
var installPath = reConfig.GetInstallPath(BiohazardVersion);
var originalDataPath = GetDataPath(installPath);
var modPath = Path.Combine(installPath, @"mod_biorand");
var fileRepo = new FileRepository(originalDataPath, modPath);
if (reConfig!.IsEnabled(BioVersion.Biohazard3))
{
var dataPath = GetDataPath(reConfig.GetInstallPath(BioVersion.Biohazard3));
var re3randomizer = new Re3Randomiser(null);
var re3randomizer = new Re3Randomiser(reConfig, null);
re3randomizer.AddArchives(dataPath, fileRepo);
}

Expand All @@ -204,7 +221,7 @@ public void Generate(RandoConfig config, ReInstallConfig reConfig, IRandoProgres
using (progress.BeginTask(config.Player, $"Generating seed"))
{
PreGenerate(config);
Generate(config, reConfig, progress, fileRepo);
Generate(config, progress, fileRepo);
}

using (progress.BeginTask(null, $"Writing manifest"))
Expand Down Expand Up @@ -233,8 +250,9 @@ private void PreGenerate(RandoConfig config)
}
}

public virtual void Generate(RandoConfig config, ReInstallConfig reConfig, IRandoProgress progress, FileRepository fileRepository)
public virtual void Generate(RandoConfig config, IRandoProgress progress, FileRepository fileRepository)
{
var reConfig = InstallConfig;
if (config.RandomItems && config.RandomInventory && !config.ShuffleItems)
{
SerialiseInventory(fileRepository);
Expand Down Expand Up @@ -480,17 +498,17 @@ protected virtual void GenerateBGM(RandoConfig config, ReInstallConfig reConfig,
}
if (enabledBgms.Contains("RE1", StringComparer.OrdinalIgnoreCase))
{
var r = new Re1Randomiser(BgCreator);
var r = new Re1Randomiser(reConfig, BgCreator);
r.AddMusicSelection(bgmRandomizer, reConfig, 1.0);
}
if (enabledBgms.Contains("RE2", StringComparer.OrdinalIgnoreCase))
{
var r = new Re2Randomiser(BgCreator);
var r = new Re2Randomiser(reConfig, BgCreator);
r.AddMusicSelection(bgmRandomizer, reConfig, 1.0);
}
if (enabledBgms.Contains("RE3", StringComparer.OrdinalIgnoreCase))
{
var r = new Re3Randomiser(BgCreator);
var r = new Re3Randomiser(reConfig, BgCreator);
r.AddMusicSelection(bgmRandomizer, reConfig, 0.75);
}

Expand Down Expand Up @@ -693,7 +711,7 @@ public virtual string[] GetMusicAlbums()
"re3"
};
result.AddRange(DataManager
.GetDirectoriesIn("bgm")
.GetDirectories("bgm")
.Select(Path.GetFileName));
return result
.Select(x => x.ToUpper())
Expand Down
2 changes: 1 addition & 1 deletion IntelOrca.Biohazard.BioRand/BgmRandomiser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void AddCutomMusicToSelection(string[] albums)

private void AddCustomMusicToSelection(BgmList bgmList, string directory)
{
var tags = _dataManager.GetDirectoriesIn(directory);
var tags = _dataManager.GetDirectories(directory);
foreach (var tagPath in tags)
{
var tag = Path.GetFileName(tagPath);
Expand Down
90 changes: 56 additions & 34 deletions IntelOrca.Biohazard.BioRand/DataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ namespace IntelOrca.Biohazard.BioRand
{
internal class DataManager
{
public string BasePath { get; }
public string[] BasePaths { get; }

public DataManager(string basePath)
public DataManager(string[] basePaths)
{
BasePath = basePath;
BasePaths = basePaths;
}

public string GetPath(string baseName)
{
return Path.Combine(BasePath, baseName);
string? best = null;
foreach (var basePath in BasePaths)
{
var result = Path.Combine(basePath, baseName);
var exists = Directory.Exists(result) || File.Exists(result);
if (best == null || exists)
{
best = result;
if (exists)
break;
}
}
return best!;
}

public string GetPath(string baseName, string path)
{
return Path.Combine(BasePath, baseName, path);
}
public string GetPath(string baseName, string path) => GetPath(Path.Combine(baseName, path));

public string GetPath(BioVersion version, string path)
{
Expand Down Expand Up @@ -50,31 +59,30 @@ public string GetText(BioVersion version, string path)
return File.ReadAllText(fullPath);
}

public string[] GetDirectories(BioVersion version, string baseName)
public string[] GetDirectories(BioVersion version, string baseName) => GetDirectories(GetSubPath(version, baseName));
public string[] GetDirectories(string baseName)
{
var fullPath = GetPath(version, baseName);
return GetDirectoriesSafe(fullPath);
var result = new List<string>();
foreach (var basePath in BasePaths)
{
var path = Path.Combine(basePath, baseName);
var dirs = GetDirectoriesSafe(path);
result.AddRange(dirs);
}
return result.ToArray();
}

public string[] GetFiles(BioVersion version, string baseName) => GetFiles(GetSubPath(version, baseName));
public string[] GetFiles(string a, string b) => GetFiles(Path.Combine(a, b));
public string[] GetFiles(string baseName)
{
return GetFilesSafe(GetPath(baseName));
}

public string[] GetFiles(BioVersion version, string baseName)
{
return GetFilesSafe(GetPath(version, baseName));
}

public string[] GetFiles(string a, string b)
{
return GetFilesSafe(Path.Combine(BasePath, a, b));
}

public string[] GetDirectoriesIn(string baseName)
{
var basePath = GetPath(baseName);
return GetDirectoriesSafe(basePath);
var result = new List<string>();
foreach (var basePath in BasePaths)
{
var part = GetFilesSafe(Path.Combine(basePath, baseName));
result.AddRange(part);
}
return result.ToArray();
}

public string[] GetBgmFiles(string tag) => GetTaggedFiles("bgm", tag);
Expand All @@ -84,13 +92,16 @@ public string[] GetDirectoriesIn(string baseName)
public string[] GetTaggedFiles(string baseName, string tag)
{
var files = new List<string>();
var top = Path.Combine(BasePath, baseName);
var directories = GetDirectoriesSafe(top);
foreach (var directory in directories)
foreach (var basePath in BasePaths)
{
var dir = Path.Combine(directory, tag);
var subFiles = GetFilesSafe(dir);
files.AddRange(subFiles);
var top = Path.Combine(basePath, baseName);
var directories = GetDirectoriesSafe(top);
foreach (var directory in directories)
{
var dir = Path.Combine(directory, tag);
var subFiles = GetFilesSafe(dir);
files.AddRange(subFiles);
}
}
return files.ToArray();
}
Expand All @@ -112,5 +123,16 @@ private static string[] GetDirectoriesSafe(string path)
}
return new string[0];
}

private string GetSubPath(BioVersion version, string basePath)
{
return version switch
{
BioVersion.Biohazard1 => Path.Combine("re1", basePath),
BioVersion.Biohazard2 => Path.Combine("re2", basePath),
BioVersion.Biohazard3 => Path.Combine("re3", basePath),
_ => throw new NotImplementedException(),
};
}
}
}
23 changes: 11 additions & 12 deletions IntelOrca.Biohazard.BioRand/RE1/Re1Randomiser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace IntelOrca.Biohazard.BioRand.RE1
public class Re1Randomiser : BaseRandomiser
{
private readonly Re1EnemyHelper _enemyHelper = new Re1EnemyHelper();
private ReInstallConfig? _reInstallConfig;
private Re1ItemHelper? _itemHelper;
private object _playerFaceSync = new object();

Expand All @@ -27,14 +26,15 @@ internal override IItemHelper ItemHelper
{
get
{
_itemHelper ??= new Re1ItemHelper(_reInstallConfig!.MaxInventorySize);
_itemHelper ??= new Re1ItemHelper(InstallConfig!.MaxInventorySize);
return _itemHelper;
}
}
internal override IEnemyHelper EnemyHelper => _enemyHelper;
internal override INpcHelper NpcHelper { get; } = new Re1NpcHelper();

public Re1Randomiser(IBgCreator? bgCreator) : base(bgCreator)
public Re1Randomiser(ReInstallConfig installConfig, IBgCreator? bgCreator)
: base(installConfig, bgCreator)
{
}

Expand Down Expand Up @@ -143,10 +143,9 @@ protected override string GetRdtPath(string dataPath, RdtId rdtId, int player, b
return path;
}

public override void Generate(RandoConfig config, ReInstallConfig reConfig, IRandoProgress progress, FileRepository fileRepository)
public override void Generate(RandoConfig config, IRandoProgress progress, FileRepository fileRepository)
{
_reInstallConfig = reConfig;

var reConfig = InstallConfig;
if (!reConfig.IsEnabled(BioVersion.Biohazard1))
{
throw new BioRandUserException("RE1 installation must be enabled to randomize RE1.");
Expand Down Expand Up @@ -187,7 +186,7 @@ public override void Generate(RandoConfig config, ReInstallConfig reConfig, IRan
FixWeaponHitScan(config);
FixYawnPoison(config);

base.Generate(config, reConfig, progress, fileRepository);
base.Generate(config, progress, fileRepository);
}

protected override string[] TitleCardSoundFiles { get; } =
Expand Down Expand Up @@ -432,21 +431,21 @@ private void SwapPlayerCharacter(RandoConfig config, RandoLogger logger, GameDat

internal override void RandomizeNPCs(RandoConfig config, NPCRandomiser npcRandomiser, VoiceRandomiser voiceRandomiser)
{
if (_reInstallConfig!.IsEnabled(BioVersion.Biohazard2))
if (InstallConfig!.IsEnabled(BioVersion.Biohazard2))
{
var dataPath = GetDataPath(_reInstallConfig.GetInstallPath(BioVersion.Biohazard2));
var dataPath = GetDataPath(InstallConfig.GetInstallPath(BioVersion.Biohazard2));
// HACK should be helper function from RE 2 randomizer
if (Directory.Exists(Path.Combine(dataPath, "data", "pl0", "rdt")))
{
dataPath = Path.Combine(dataPath, "data");
}
voiceRandomiser.AddToSelection(BioVersion.Biohazard2, new FileRepository(dataPath));
}
if (_reInstallConfig!.IsEnabled(BioVersion.Biohazard3))
if (InstallConfig!.IsEnabled(BioVersion.Biohazard3))
{
var dataPath = GetDataPath(_reInstallConfig.GetInstallPath(BioVersion.Biohazard3));
var dataPath = GetDataPath(InstallConfig.GetInstallPath(BioVersion.Biohazard3));
var fileRepository = new FileRepository(dataPath);
var re3randomizer = new Re3Randomiser(null);
var re3randomizer = new Re3Randomiser(InstallConfig, null);
re3randomizer.AddArchives(dataPath, fileRepository);
voiceRandomiser.AddToSelection(BioVersion.Biohazard3, fileRepository);
}
Expand Down
Loading

0 comments on commit 727b564

Please sign in to comment.