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

Allow Portable Installs #2556

Merged
merged 9 commits into from
May 9, 2024
24 changes: 23 additions & 1 deletion Assets/Scripts/DaggerfallUnityApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@
public static class DaggerfallUnityApplication
{
static string persistentDataPath;
private static bool? isPortableInstall;

public static bool IsPortableInstall
{
get
{
if (isPortableInstall == null)
{
isPortableInstall = !Application.isEditor && File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Portable.txt"));
}

return isPortableInstall.Value;
}
}

public static string PersistentDataPath
{
Expand All @@ -38,7 +52,15 @@ private static void InitializePersistentPath()
persistentDataPath = String.Concat(Application.persistentDataPath, ".devenv");
Directory.CreateDirectory(persistentDataPath);
#else
persistentDataPath = Application.persistentDataPath;
if (IsPortableInstall)
{
persistentDataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PortableAppdata");
Directory.CreateDirectory(persistentDataPath);
}
else
{
persistentDataPath = Application.persistentDataPath;
}
#endif
}

Expand Down
72 changes: 69 additions & 3 deletions Assets/Scripts/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Globalization;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using DaggerfallWorkshop.Game;
using IniParser;
using IniParser.Model;
Expand All @@ -29,6 +30,43 @@ namespace DaggerfallWorkshop
/// </summary>
public class SettingsManager
{
private static string GetFullPath(string basePath, string path)
{
if(string.IsNullOrEmpty(path) || Path.IsPathRooted(path))
{
return path;
}

return Path.GetFullPath(Path.Combine(basePath, path));
}

// Returns a relative path if embedded within the base path,
// returns the full path as is if outside the base path
private static string GetPortablePath(string basePath, string path)
{
if(string.IsNullOrEmpty(path))
{
return path;
}

char lastChar = basePath.Last();
if (lastChar != Path.DirectorySeparatorChar && lastChar != Path.AltDirectorySeparatorChar)
{
basePath += Path.DirectorySeparatorChar;
}

Uri baseUri = new Uri(basePath);
Uri relUri = baseUri.MakeRelativeUri(new Uri(path));

string relativePath = Uri.UnescapeDataString(relUri.ToString()).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (relativePath.StartsWith(".." + Path.DirectorySeparatorChar))
{
return path;
}

return relativePath;
}

const string defaultsIniName = "defaults.ini";
const string settingsIniName = "settings.ini";
const string settingsBakExt = ".bak";
Expand Down Expand Up @@ -342,6 +380,24 @@ public void LoadSettings()
MyDaggerfallUnitySavePath = GetString(sectionDaggerfall, "MyDaggerfallUnitySavePath");
MyDaggerfallUnityScreenshotsPath = GetString(sectionDaggerfall, "MyDaggerfallUnityScreenshotsPath");

// In Portable Install mode, we save those paths as relative to the current directory
// This allows users to move the directory around without breaking the settings
if(DaggerfallUnityApplication.IsPortableInstall)
{
if(!string.IsNullOrEmpty(MyDaggerfallPath))
{
MyDaggerfallPath = GetFullPath(AppDomain.CurrentDomain.BaseDirectory, MyDaggerfallPath);
}
if(!string.IsNullOrEmpty(MyDaggerfallUnitySavePath))
{
MyDaggerfallUnitySavePath = GetFullPath(AppDomain.CurrentDomain.BaseDirectory, MyDaggerfallUnitySavePath);
}
if(!string.IsNullOrEmpty(MyDaggerfallUnityScreenshotsPath))
{
MyDaggerfallUnityScreenshotsPath = GetFullPath(AppDomain.CurrentDomain.BaseDirectory, MyDaggerfallUnityScreenshotsPath);
}
}

ResolutionWidth = GetInt(sectionVideo, "ResolutionWidth");
ResolutionHeight = GetInt(sectionVideo, "ResolutionHeight");
RetroRenderingMode = GetInt(sectionVideo, "RetroRenderingMode", 0, 2);
Expand Down Expand Up @@ -525,9 +581,19 @@ public void LoadSettings()
public void SaveSettings()
{
// Write property cache to ini data
SetString(sectionDaggerfall, "MyDaggerfallPath", MyDaggerfallPath);
SetString(sectionDaggerfall, "MyDaggerfallUnitySavePath", MyDaggerfallUnitySavePath);
SetString(sectionDaggerfall, "MyDaggerfallUnityScreenshotsPath", MyDaggerfallUnityScreenshotsPath);
if (DaggerfallUnityApplication.IsPortableInstall)
{
// Save relative paths
SetString(sectionDaggerfall, "MyDaggerfallPath", GetPortablePath(AppDomain.CurrentDomain.BaseDirectory, MyDaggerfallPath));
SetString(sectionDaggerfall, "MyDaggerfallUnitySavePath", GetPortablePath(AppDomain.CurrentDomain.BaseDirectory, MyDaggerfallUnitySavePath));
SetString(sectionDaggerfall, "MyDaggerfallUnityScreenshotsPath", GetPortablePath(AppDomain.CurrentDomain.BaseDirectory,MyDaggerfallUnityScreenshotsPath));
}
else
{
SetString(sectionDaggerfall, "MyDaggerfallPath", MyDaggerfallPath);
SetString(sectionDaggerfall, "MyDaggerfallUnitySavePath", MyDaggerfallUnitySavePath);
SetString(sectionDaggerfall, "MyDaggerfallUnityScreenshotsPath", MyDaggerfallUnityScreenshotsPath);
}

SetInt(sectionVideo, "ResolutionWidth", ResolutionWidth);
SetInt(sectionVideo, "ResolutionHeight", ResolutionHeight);
Expand Down