Skip to content

Commit

Permalink
Merge pull request #2556 from RicardoLuis0/portable-installs
Browse files Browse the repository at this point in the history
Allow Portable Installs
  • Loading branch information
KABoissonneault authored May 9, 2024
2 parents f27175b + 98990df commit 6d62f17
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
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

0 comments on commit 6d62f17

Please sign in to comment.