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
16 changes: 14 additions & 2 deletions Assets/Scripts/Game/UserInterface/FolderBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,20 @@ void Setup()

// Setup initial folder conditions
RefreshDrives();
RefreshFolders();

if(DaggerfallUnityApplication.IsPortableInstall)
{
// start browsing from exe path for portable installs
currentPath = AppDomain.CurrentDomain.BaseDirectory;
if(SystemInfo.operatingSystemFamily == OperatingSystemFamily.Windows)
{
var drive = Path.GetPathRoot(currentPath);
driveList.SelectIndexSilent(drives.FindIndex( d => d == drive ));
}
UpdatePathText();
RicardoLuis0 marked this conversation as resolved.
Show resolved Hide resolved
}

RefreshFolders();

// Setup events
confirmButton.OnMouseClick += ConfirmButton_OnMouseClick;
Expand Down Expand Up @@ -358,7 +371,6 @@ private void FolderList_OnUseSelectedItem()
currentPath = newPath;
RefreshFolders();
RaisePathChangedEvent();

UpdatePathText();
}
}
Expand Down
11 changes: 11 additions & 0 deletions Assets/Scripts/Game/UserInterface/ListBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,17 @@ public void SelectIndex(int index)
}
}

public void SelectIndexSilent(int index)
{
if (index < 0 || index >= listItems.Count)
return;

if (listItems[index].Enabled)
{
selectedIndex = index;
}
}

public void SelectNone()
{
selectedIndex = -1;
Expand Down
64 changes: 61 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,35 @@ 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));
}

private static string GetRelativePath(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));

return Uri.UnescapeDataString(relUri.ToString()).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
}

const string defaultsIniName = "defaults.ini";
const string settingsIniName = "settings.ini";
const string settingsBakExt = ".bak";
Expand Down Expand Up @@ -342,6 +372,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 +573,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", GetRelativePath(AppDomain.CurrentDomain.BaseDirectory, MyDaggerfallPath));
SetString(sectionDaggerfall, "MyDaggerfallUnitySavePath", GetRelativePath(AppDomain.CurrentDomain.BaseDirectory, MyDaggerfallUnitySavePath));
SetString(sectionDaggerfall, "MyDaggerfallUnityScreenshotsPath", GetRelativePath(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