Skip to content

Commit

Permalink
Merge pull request #180 from FlameHorizon/refac-gamelocation
Browse files Browse the repository at this point in the history
Refactored how location of the game is aquired to a simpler version.
  • Loading branch information
Marcin Gomulak committed Mar 18, 2021
2 parents b438981 + e3c2837 commit 22eb63e
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 207 deletions.
10 changes: 4 additions & 6 deletions Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@
<Compile Include="Field\WalkMesh\Vert.cs" />
<Compile Include="Field\WalkMesh\WalkMesh.cs" />
<Compile Include="FPSCounter.cs" />
<Compile Include="GameDirectoryFinder.cs" />
<Compile Include="Image\Bppflag.cs" />
<Compile Include="Image\Cluts.cs" />
<Compile Include="Image\ColorABGR1555.cs" />
Expand Down Expand Up @@ -1117,11 +1118,6 @@
<Compile Include="Saves\Saves.TripleTriad.cs" />
<Compile Include="Saves\Saves.Worldmap.cs" />
<Compile Include="Sources\Environment\Build\BuildEnvironment.cs" />
<Compile Include="Sources\Environment\Game\Location\GameLocation.cs" />
<Compile Include="Sources\Environment\Game\Location\HardcodedGameLocationProvider.cs" />
<Compile Include="Sources\Environment\Game\Location\IGameLocationProvider.cs" />
<Compile Include="Sources\Environment\Game\Location\LinuxGameLocationProvider.cs" />
<Compile Include="Sources\Environment\Game\Location\WindowsGameLocationProvider.cs" />
<Compile Include="Sources\Environment\Runtime\RuntimeEnvironment.cs" />
<Compile Include="Sources\Environment\Runtime\RuntimePlatform.cs" />
<Compile Include="Sources\Exceptions\ExceptionList.cs" />
Expand Down Expand Up @@ -1295,7 +1291,9 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="Sources\Environment\Game\Location\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Encoding\Encoding.csproj">
<Project>{a9b33bf4-9b03-4e3d-bf6a-181521046ee8}</Project>
Expand Down
111 changes: 111 additions & 0 deletions Core/GameDirectoryFinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace OpenVIII.Core {

/// <summary>
/// Class is resposible for finding the root directory where Final Fantasy VIII is stored.
/// </summary>
public static class GameDirectoryFinder
{
/// <summary>
/// Looks for root directory where the game is installed.
/// </summary>
/// <returns>Path to a directory where the game is installed.</returns>
public static string FindRootGameDirectory()
{
return RuntimeEnvironment.Platform switch
{
RuntimePlatform.Windows => WindowsRootGameDirectory(),
RuntimePlatform.Linux => LinuxRootGameDirectory(),
_ => throw new NotSupportedException(RuntimeEnvironment.Platform.ToString()),
};
}

private static string WindowsRootGameDirectory()
{
var commonRoots = new string[]
{
@"C:\Program Files (x86)\Steam\steamapps\common\FINAL FANTASY VIII",
@"D:\SteamLibrary\steamapps\common\FINAL FANTASY VIII",
@"D:\Steam\steamapps\common\FINAL FANTASY VIII",
};

if (commonRoots.Where(path => Directory.Exists(path)).Any())
return commonRoots.Where(path => Directory.Exists(path)).First();

var registryRoots = RootsFromRegistry();

if (registryRoots.Where(path => Directory.Exists(path)).Any())
return registryRoots.Where(path => Directory.Exists(path)).First();

throw new DirectoryNotFoundException($"Cannot find game directory." +
$"Add your own path to the {nameof(WindowsRootGameDirectory)}.");
}

private static List<string> RootsFromRegistry()
{
// Now, we are looking into registers.
#region Registries paths and tags

// Steam 2013
const string SteamRegistryPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 39150";
const string SteamGamePathTag = @"InstallLocation";

// Supplied from LordUrQuan
const string CD2000RegistryPath = @"SOFTWARE\Wow6432Node\Square Soft, Inc\FINAL FANTASY VIII\1.00";
const string CD2000GamePathTag = @"AppPath";

// Steam Remaster
const string SteamRERegistryPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 1026680";
const string SteamREGamePathTag = @"InstallLocation";
#endregion

var regs = new Dictionary<string, string>()
{
{SteamRegistryPath, SteamGamePathTag},
{CD2000RegistryPath, CD2000GamePathTag},
{SteamRERegistryPath, SteamREGamePathTag}
};

var regValues = new List<string>();
foreach (var pair in regs)
{
regValues.Add(ValueFromRegistry(pair.Key, pair.Value, RegistryView.Registry32));
regValues.Add(ValueFromRegistry(pair.Key, pair.Value, RegistryView.Registry64));
}

return regValues;
}

private static string ValueFromRegistry(string subKey, string valueName, RegistryView view)
{
using var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, view);
using var key = baseKey.OpenSubKey(subKey);

// Starting from C# 6 we can use Null-conditional operator (?.)
// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-
return (string)key?.GetValue(valueName);
}

private static string LinuxRootGameDirectory()
{
var commonRoots = new string[]
{
@"/home/robert/Final Fantasy VIII",
@"/media/griever/Data/SteamLibrary/steamapps/common/FINAL FANTASY VIII",
@"/home/griever/.PlayOnLinux/wineprefix/Steam/drive_c/Program Files/Steam/steamapps/common/FINAL FANTASY VIII",
@"/home/parallels/src/ff8/steam"
};

if (commonRoots.Where(path => Directory.Exists(path)).Any())
return commonRoots.Where(path => Directory.Exists(path)).First();

throw new DirectoryNotFoundException($"Cannot find game directory." +
$"Add your own path to the {nameof(LinuxRootGameDirectory)}.");
}
}
}
7 changes: 4 additions & 3 deletions Core/Memory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using OpenVIII;
using OpenVIII.Core;
using OpenVIII.Kernel;
using System;
using System.Collections.Concurrent;
Expand Down Expand Up @@ -833,9 +835,8 @@ public static void Init(GraphicsDeviceManager graphics, SpriteBatch spriteBatch,
Log.WriteLine($"{nameof(Memory)} :: {nameof(MainThreadOnlyActions)}");
MainThreadOnlyActions = new ConcurrentQueue<Action>();

FF8Dir = GameLocation.Current.DataPath;
void SetData() =>
FF8DirData = Extended.GetUnixFullPath(Path.Combine(FF8Dir, "Data"));
FF8Dir = GameDirectoryFinder.FindRootGameDirectory();
void SetData() => FF8DirData = Extended.GetUnixFullPath(Path.Combine(FF8Dir, "Data"));

SetData();
var languageSet = false;
Expand Down
43 changes: 0 additions & 43 deletions Core/Sources/Environment/Game/Location/GameLocation.cs

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 22eb63e

Please sign in to comment.