Created
June 25, 2014 20:26
-
-
Save flibitijibibo/a824db1276664717dbaf to your computer and use it in GitHub Desktop.
FNA Save Migration Code Pile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This is a snippet of code from the FNA version of Rogue Legacy. | |
* | |
* A recent FNA update fixed Storage accuracy, affecting save locations: | |
* https://plus.google.com/102593483001615978126/posts/7qE6nJGcdL2 | |
* As a result, games like Rogue Legacy now need a migration process. | |
* | |
* This is the first one I've written, and I'm posting it here for when | |
* I take care of the rest of my affected ports. | |
* | |
* -flibit | |
*/ | |
using System; | |
using System.IO; | |
using SDL2; | |
namespace RogueCastle | |
{ | |
static class Program | |
{ | |
static string GetOSDir() | |
{ | |
string platform = SDL.SDL_GetPlatform(); | |
if (platform.Equals("Mac OS X")) | |
{ | |
string osDir = Environment.GetEnvironmentVariable("HOME"); | |
if (String.IsNullOrEmpty(osDir)) | |
{ | |
return "."; // Oh well. | |
} | |
return osDir + "/Library/Application Support"; | |
} | |
if (platform.Equals("Linux")) | |
{ | |
string osDir = Environment.GetEnvironmentVariable("XDG_DATA_HOME"); | |
if (String.IsNullOrEmpty(osDir)) | |
{ | |
osDir = Environment.GetEnvironmentVariable("HOME"); | |
if (String.IsNullOrEmpty(osDir)) | |
{ | |
return "."; // Oh well. | |
} | |
return osDir + "/.local/share"; | |
} | |
return osDir; | |
} | |
throw new Exception("SDL2 Platform?"); | |
} | |
static void CopyDirectory(string oldPath, string newPath) | |
{ | |
System.Console.WriteLine("Copying directory " + oldPath + " to " + newPath + "..."); | |
Directory.CreateDirectory(newPath); | |
DirectoryInfo dir = new DirectoryInfo(oldPath); | |
DirectoryInfo[] dirs = dir.GetDirectories(); | |
FileInfo[] files = dir.GetFiles(); | |
foreach (FileInfo file in files) | |
{ | |
System.Console.Write("\tCopying file " + file.Name + "..."); | |
file.CopyTo(Path.Combine(newPath, file.Name), false); | |
System.Console.WriteLine(" Done!"); | |
} | |
foreach (DirectoryInfo subdir in dirs) | |
{ | |
CopyDirectory(subdir.FullName, Path.Combine(newPath, subdir.Name)); | |
} | |
System.Console.WriteLine("Done!"); | |
} | |
static void MigrateSaveData() | |
{ | |
string newSaveDir = Path.Combine(GetOSDir(), "RogueLegacy"); | |
string oldSaveDir = Path.Combine(GetOSDir(), "RogueLegacyStorageContainer"); | |
if (!Directory.Exists(newSaveDir) && Directory.Exists(oldSaveDir)) | |
{ | |
try | |
{ | |
System.Console.WriteLine("Performing save migration..."); | |
System.Console.Write("Creating new base directories..."); | |
Directory.CreateDirectory(newSaveDir); | |
newSaveDir = Path.Combine(newSaveDir, "RogueLegacyStorageContainer"); | |
Directory.CreateDirectory(newSaveDir); | |
newSaveDir = Path.Combine(newSaveDir, "AllPlayers"); | |
Directory.CreateDirectory(newSaveDir); | |
System.Console.WriteLine(" Done!"); | |
CopyDirectory(oldSaveDir, newSaveDir); | |
System.Console.WriteLine("Migration complete!"); | |
} catch (Exception e) | |
{ | |
System.Console.WriteLine(e.ToString()); | |
SDL.SDL_ShowSimpleMessageBox( | |
SDL.SDL_MessageBoxFlags.SDL_MESSAGEBOX_ERROR, | |
"SAVE THIS MESSAGE!", | |
"Save migration failed!\n" + | |
"Your files are safe, they're just not in the right place yet...", | |
IntPtr.Zero | |
); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment