Skip to content

Commit

Permalink
improved tempfile handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Orbmu2k committed Mar 23, 2019
1 parent 1af9d1c commit f1ecca7
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 76 deletions.
68 changes: 37 additions & 31 deletions nspector/Common/DrsDecrypterService.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System;
using nspector.Common.Helper;
using nspector.Native.NVAPI2;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using nspector.Native.NVAPI2;

namespace nspector.Common
{
internal class DrsDecrypterService : DrsSettingsServiceBase
{

private static readonly byte[] _InternalSettingsKey = new byte[] {
0x2f, 0x7c, 0x4f, 0x8b, 0x20, 0x24, 0x52, 0x8d, 0x26, 0x3c, 0x94, 0x77, 0xf3, 0x7c, 0x98, 0xa5,
0xfa, 0x71, 0xb6, 0x80, 0xdd, 0x35, 0x84, 0xba, 0xfd, 0xb6, 0xa6, 0x1b, 0x39, 0xc4, 0xcc, 0xb0,
Expand All @@ -33,7 +33,7 @@ public DrsDecrypterService(DrsSettingsMetaService metaService) : base(metaServic
{
CreateInternalSettingMap();
}

private uint GetDwordFromKey(uint offset)
{
var bytes = new byte[4];
Expand Down Expand Up @@ -82,7 +82,7 @@ public void DecryptSettingIfNeeded(string profileName, ref NVDRS_SETTING setting
}
}
}

private string FormatInternalSettingKey(string profileName, uint settingId)
{
return profileName + settingId.ToString("X8").ToLower();
Expand All @@ -97,40 +97,46 @@ public bool IsInternalSetting(string profileName, uint settingId)

private void CreateInternalSettingMap()
{
string tmpfile = Path.GetTempFileName();
DrsSession((hSession) =>
{
SaveSettingsFileEx(hSession, tmpfile);
});
string tmpfile = TempFile.GetTempFileName();

if (File.Exists(tmpfile))
try
{
var lines = File.ReadAllLines(tmpfile);
DrsSession((hSession) =>
{
SaveSettingsFileEx(hSession, tmpfile);
});

if (File.Exists(tmpfile))
{
var lines = File.ReadAllLines(tmpfile);

_InternalSettings = new HashSet<string>();
_InternalSettings = new HashSet<string>();

var paProfile = "Profile\\s\\\"(?<profileName>.*?)\\\"";
var rxProfile = new Regex(paProfile, RegexOptions.Compiled);
var paProfile = "Profile\\s\\\"(?<profileName>.*?)\\\"";
var rxProfile = new Regex(paProfile, RegexOptions.Compiled);

var paSetting = "ID_0x(?<sid>[0-9a-fA-F]+)\\s\\=.*?InternalSettingFlag\\=V0";
var rxSetting = new Regex(paSetting, RegexOptions.Compiled);
var paSetting = "ID_0x(?<sid>[0-9a-fA-F]+)\\s\\=.*?InternalSettingFlag\\=V0";
var rxSetting = new Regex(paSetting, RegexOptions.Compiled);

var currentProfileName = "";
for (int i = 0; i < lines.Length; i++)
{
foreach (Match ms in rxProfile.Matches(lines[i]))
{
currentProfileName = ms.Result("${profileName}");
}
foreach (Match ms in rxSetting.Matches(lines[i]))
var currentProfileName = "";
for (int i = 0; i < lines.Length; i++)
{
_InternalSettings.Add(currentProfileName + ms.Result("${sid}"));
foreach (Match ms in rxProfile.Matches(lines[i]))
{
currentProfileName = ms.Result("${profileName}");
}
foreach (Match ms in rxSetting.Matches(lines[i]))
{
_InternalSettings.Add(currentProfileName + ms.Result("${sid}"));
}
}
}

File.Delete(tmpfile);
}
finally
{
if (File.Exists(tmpfile))
File.Delete(tmpfile);
}
}

}
}
}
47 changes: 27 additions & 20 deletions nspector/Common/DrsScannerService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using nspector.Native.NVAPI2;
using nspector.Common.Helper;
using nspector.Native.NVAPI2;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -250,35 +251,41 @@ private void AddScannedSettingToCache(NVDRS_PROFILE profile, NVDRS_SETTING setti
public string FindProfilesUsingApplication(string applicationName)
{
string lowerApplicationName = applicationName.ToLower();
string tmpfile = Path.GetTempFileName();
var matchingProfiles = new List<string>();
string tmpfile = TempFile.GetTempFileName();

DrsSession((hSession) =>
try
{
SaveSettingsFileEx(hSession, tmpfile);
});
var matchingProfiles = new List<string>();

if (File.Exists(tmpfile))
{
string content = File.ReadAllText(tmpfile);
string pattern = "\\sProfile\\s\\\"(?<profile>.*?)\\\"(?<scope>.*?Executable.*?)EndProfile";
foreach (Match m in Regex.Matches(content, pattern, RegexOptions.Singleline))
DrsSession((hSession) =>
{
string scope = m.Result("${scope}");
foreach (Match ms in Regex.Matches(scope, "Executable\\s\\\"(?<app>.*?)\\\"", RegexOptions.Singleline))
SaveSettingsFileEx(hSession, tmpfile);
});

if (File.Exists(tmpfile))
{
string content = File.ReadAllText(tmpfile);
string pattern = "\\sProfile\\s\\\"(?<profile>.*?)\\\"(?<scope>.*?Executable.*?)EndProfile";
foreach (Match m in Regex.Matches(content, pattern, RegexOptions.Singleline))
{
if (ms.Result("${app}").ToLower() == lowerApplicationName)
string scope = m.Result("${scope}");
foreach (Match ms in Regex.Matches(scope, "Executable\\s\\\"(?<app>.*?)\\\"", RegexOptions.Singleline))
{
matchingProfiles.Add(m.Result("${profile}"));
if (ms.Result("${app}").ToLower() == lowerApplicationName)
{
matchingProfiles.Add(m.Result("${profile}"));
}
}
}
}
}

if (File.Exists(tmpfile))
File.Delete(tmpfile);

return string.Join(";", matchingProfiles);
return string.Join(";", matchingProfiles);
}
finally
{
if (File.Exists(tmpfile))
File.Delete(tmpfile);
}
}

}
Expand Down
61 changes: 36 additions & 25 deletions nspector/Common/DrsSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,45 +59,56 @@ private void RunDrsInitProcess()

public void DeleteAllProfilesHard()
{
var tmpFile = Path.GetTempFileName();
File.WriteAllText(tmpFile, "BaseProfile \"Base Profile\"\r\nProfile \"Base Profile\"\r\nShowOn All\r\nProfileType Global\r\nEndProfile\r\n");

DrsSession((hSession) =>
var tmpFile = TempFile.GetTempFileName();
try
{
LoadSettingsFileEx(hSession, tmpFile);
SaveSettings(hSession);
});
File.WriteAllText(tmpFile, "BaseProfile \"Base Profile\"\r\nProfile \"Base Profile\"\r\nShowOn All\r\nProfileType Global\r\nEndProfile\r\n");

if (File.Exists(tmpFile))
File.Delete(tmpFile);
DrsSession((hSession) =>
{
LoadSettingsFileEx(hSession, tmpFile);
SaveSettings(hSession);
});
}
finally
{
if (File.Exists(tmpFile))
File.Delete(tmpFile);
}

}

public void DeleteProfileHard(string profileName)
{
var tmpFileName = Path.GetTempFileName();
var tmpFileContent = "";
var tmpFileName = TempFile.GetTempFileName();

DrsSession((hSession) =>
try
{
SaveSettingsFileEx(hSession, tmpFileName);
tmpFileContent = File.ReadAllText(tmpFileName);
string pattern = "(?<rpl>\nProfile\\s\"" + Regex.Escape(profileName) + "\".*?EndProfile.*?\n)";
tmpFileContent = Regex.Replace(tmpFileContent, pattern, "", RegexOptions.Singleline);
File.WriteAllText(tmpFileName, tmpFileContent);
});
var tmpFileContent = "";

if (tmpFileContent != "")
{
DrsSession((hSession) =>
{
LoadSettingsFileEx(hSession, tmpFileName);
SaveSettings(hSession);
SaveSettingsFileEx(hSession, tmpFileName);
tmpFileContent = File.ReadAllText(tmpFileName);
string pattern = "(?<rpl>\nProfile\\s\"" + Regex.Escape(profileName) + "\".*?EndProfile.*?\n)";
tmpFileContent = Regex.Replace(tmpFileContent, pattern, "", RegexOptions.Singleline);
File.WriteAllText(tmpFileName, tmpFileContent);
});
}

if (File.Exists(tmpFileName))
File.Delete(tmpFileName);
if (tmpFileContent != "")
{
DrsSession((hSession) =>
{
LoadSettingsFileEx(hSession, tmpFileName);
SaveSettings(hSession);
});
}
}
finally
{
if (File.Exists(tmpFileName))
File.Delete(tmpFileName);
}
}

public void DeleteProfile(string profileName)
Expand Down
28 changes: 28 additions & 0 deletions nspector/Common/Helper/TempFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nspector.Common.Helper
{
internal static class TempFile
{
public static string GetTempFileName()
{
while (true)
{
var tempFile = GenerateTempFileName();
if (!File.Exists(tempFile))
return tempFile;
}
}

private static string GenerateTempFileName()
{
return Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString().Replace("-", ""));
}

}
}
1 change: 1 addition & 0 deletions nspector/nvidiaProfileInspector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<Compile Include="Common\Helper\ListViewGroupSorter.cs" />
<Compile Include="Common\Helper\ShortcutResolver.cs" />
<Compile Include="Common\Helper\SteamAppResolver.cs" />
<Compile Include="Common\Helper\TempFile.cs" />
<Compile Include="Common\Import\ImportExportUitl.cs" />
<Compile Include="Common\Import\SettingValueType.cs" />
<Compile Include="Common\Meta\NvD3dUmxSettingMetaService.cs" />
Expand Down

0 comments on commit f1ecca7

Please sign in to comment.