Skip to content

Commit b736063

Browse files
authored
Merge pull request #2 from XTsat/AppFolder
Use LocalData
2 parents e8b6e8b + 15c42bb commit b736063

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

CompactGUI/Application.xaml.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Imports Coravel.Scheduling.Schedule
1717

1818
Partial Public Class Application
1919

20-
Public Shared ReadOnly AppVersion As New SemVersion(4, 0, 0, "beta", 7)
20+
Public Shared ReadOnly AppVersion As New SemVersion(4, 0, 0, "beta", 8)
2121

2222
Private Shared _host As IHost
2323

CompactGUI/Models/CompressableFolders/SteamFolder.vb

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

2+
Imports System
23
Imports System.IO
34
Imports System.Net.Http
5+
Imports System.Threading.Tasks
6+
Imports System.Windows.Forms
7+
Imports System.Reflection
8+
Imports System.Runtime.InteropServices
49

510
Imports CommunityToolkit.Mvvm.ComponentModel
611

@@ -67,17 +72,41 @@ Public Class SteamFolder : Inherits CompressableFolder
6772

6873
End Function
6974

75+
Public Shared Function GetAppRootPath() As String
76+
Try
77+
If AppContext.TryGetSwitch("System.AppContext.SingleFile", Nothing) Then
78+
Dim pathBuffer As New String(Chr(0), 260)
79+
Dim exePath As String = pathBuffer.TrimEnd(Chr(0))
80+
Dim appRootPath As String = Path.GetDirectoryName(exePath)
81+
Return If(appRootPath.EndsWith("\"), appRootPath.TrimEnd("\"), appRootPath)
82+
Else
83+
Dim appRootPath As String = AppContext.BaseDirectory
84+
Return If(appRootPath.EndsWith("\"), appRootPath.TrimEnd("\"), appRootPath)
85+
End If
86+
Catch ex As Exception
87+
Return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
88+
End Try
89+
End Function
90+
7091
Public Shared Async Function GetSteamHeaderAsync(folder As SteamFolder) As Task
7192

7293
If folder.SteamAppID = 0 Then Return
7394

7495
Dim tempImg As BitmapImage = Nothing
7596

76-
Dim EnvironmentPath = Environment.GetEnvironmentVariable("IridiumIO", EnvironmentVariableTarget.User)
77-
Dim imageDir = Path.Combine(EnvironmentPath, "CompactGUI", "SteamCache")
97+
Dim appRootPath As String = GetAppRootPath()
98+
Dim EnvironmentPath = Path.Combine(appRootPath, "data")
99+
Dim imageDir = Path.Combine(EnvironmentPath, "SteamCache")
78100
Dim imagePath = Path.Combine(imageDir, $"{folder.SteamAppID}.jpg")
79101

80-
If Not Directory.Exists(imageDir) Then Directory.CreateDirectory(imageDir)
102+
Try
103+
Directory.CreateDirectory(imageDir)
104+
Catch ex As Exception
105+
EnvironmentPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "IridiumIO", "CompactGUI")
106+
imageDir = Path.Combine(EnvironmentPath, "SteamCache")
107+
imagePath = Path.Combine(imageDir, $"{folder.SteamAppID}.jpg")
108+
Directory.CreateDirectory(imageDir)
109+
End Try
81110

82111
If File.Exists(imagePath) Then
83112
tempImg = LoadImageFromDisk(imagePath)

CompactGUI/Services/SettingsService.vb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
Imports System.IO
2+
Imports System.Reflection
23
Imports System.Text.Json
3-
44
Imports CompactGUI.Core.Settings
55
Imports CompactGUI.Logging
6-
76
Imports Microsoft.Extensions.Logging
87

98
Public Class SettingsService : Implements ISettingsService
@@ -15,7 +14,8 @@ Public Class SettingsService : Implements ISettingsService
1514

1615
Public Sub New()
1716

18-
DataFolder = New IO.DirectoryInfo(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "IridiumIO", "CompactGUI"))
17+
Dim appRootPath As String = SteamFolder.GetAppRootPath()
18+
DataFolder = New IO.DirectoryInfo(IO.Path.Combine(appRootPath, "data"))
1919
SettingsJSONFile = New IO.FileInfo(IO.Path.Combine(DataFolder.FullName, "settings.json"))
2020

2121
SettingsVersion = 1.2

CompactGUI/ViewModels/SettingsViewModel.vb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
Imports System.ComponentModel
3+
Imports System.Reflection
34
Imports System.Xml
5+
Imports System.IO
46

57
Imports CommunityToolkit.Mvvm.ComponentModel
68
Imports CommunityToolkit.Mvvm.Input
@@ -40,10 +42,10 @@ Public NotInheritable Class SettingsViewModel : Inherits ObservableObject
4042

4143
Private Shared Async Function SetEnv() As Task
4244
SettingsLog.SettingEnvironmentVariables(Application.GetService(Of ILogger(Of Settings)))
43-
Dim desiredValue = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "IridiumIO")
45+
Dim appRootPath As String = SteamFolder.GetAppRootPath()
46+
Dim desiredValue = IO.Path.Combine(appRootPath, "data")
4447
Dim currentValue = Environment.GetEnvironmentVariable("IridiumIO", EnvironmentVariableTarget.User)
4548
If currentValue <> desiredValue Then Await Task.Run(Sub() Environment.SetEnvironmentVariable("IridiumIO", desiredValue, EnvironmentVariableTarget.User))
46-
4749
End Function
4850

4951
Private Async Sub SettingsPropertyChanged(sender As Object, e As PropertyChangedEventArgs)

0 commit comments

Comments
 (0)