Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
Updated use of ProfileMigration class
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryochan7 committed Dec 7, 2023
1 parent e3eb73d commit 016654b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
29 changes: 25 additions & 4 deletions DS4Windows/DS4Control/ProfileMigration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,27 @@ public class ProfileMigration
private bool usedMigration;
public bool UsedMigration { get => usedMigration; }
private string currentMigrationText;
public string CurrentMigrationText { get => currentMigrationText; }

public ProfileMigration(string filePath)
public ProfileMigration(Stream inputStream)
{
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader innerStreamReader = new StreamReader(fileStream);
//FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
SetupFromStream(inputStream);
inputStream.Close();
}

public ProfileMigration(string profileText)
{
using (MemoryStream tempMemStream =
new MemoryStream(Encoding.UTF8.GetBytes(profileText ?? "")))
{
SetupFromStream(tempMemStream);
}
}

private void SetupFromStream(Stream inputStream)
{
StreamReader innerStreamReader = new StreamReader(inputStream);
currentMigrationText = innerStreamReader.ReadToEnd();
innerStreamReader.Dispose();

Expand Down Expand Up @@ -85,17 +101,20 @@ public void Migrate()
{
case 1:
migratedText = Version0002Migration();
currentMigrationText = migratedText;
PrepareReaderMigration(migratedText);
tempVersion = 2;
goto case 2;
case 2:
case 3:
migratedText = Version0004Migration();
currentMigrationText = migratedText;
PrepareReaderMigration(migratedText);
tempVersion = 4;
goto default;
goto case 4;
case 4:
migratedText = Version0005Migration();
currentMigrationText = migratedText;
PrepareReaderMigration(migratedText);
tempVersion = 5;
goto default;
Expand All @@ -118,6 +137,8 @@ private void PrepareReaderMigration(string migratedText)
currentMigrationText = migratedText;
StringReader stringReader = new StringReader(currentMigrationText);
profileReader = XmlReader.Create(stringReader);
// Move stream to root element
//profileReader.MoveToContent();
}

private void DetermineProfileVersion()
Expand Down
6 changes: 4 additions & 2 deletions DS4Windows/DS4Control/ScpUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4907,7 +4907,8 @@ public bool LoadProfileNew(int device, bool launchprogram, ControlService contro
{
XmlDocument migrationDoc = new XmlDocument();

ProfileMigration tmpMigration = new ProfileMigration(profilepath);
using FileStream fileStream = new FileStream(profilepath, FileMode.Open, FileAccess.Read);
ProfileMigration tmpMigration = new ProfileMigration(fileStream);
if (tmpMigration.RequiresMigration())
{
tmpMigration.Migrate();
Expand Down Expand Up @@ -5145,7 +5146,8 @@ public bool LoadProfile(int device, bool launchprogram, ControlService control,
{
XmlNode Item;

ProfileMigration tmpMigration = new ProfileMigration(profilepath);
using FileStream fileStream = new FileStream(profilepath, FileMode.Open, FileAccess.Read);
ProfileMigration tmpMigration = new ProfileMigration(fileStream);
if (tmpMigration.RequiresMigration())
{
tmpMigration.Migrate();
Expand Down

0 comments on commit 016654b

Please sign in to comment.