2011-05-04 02:00:08 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
namespace BizHawk.MultiClient
|
|
|
|
|
{
|
2011-07-06 01:53:15 +00:00
|
|
|
|
public static class PathManager
|
|
|
|
|
{
|
|
|
|
|
public static string GetExeDirectoryAbsolute()
|
|
|
|
|
{
|
2012-12-23 16:30:00 +00:00
|
|
|
|
var uri = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase);
|
|
|
|
|
string module = uri.LocalPath + System.Web.HttpUtility.UrlDecode(uri.Fragment);
|
|
|
|
|
return Path.GetDirectoryName(module);
|
2011-07-06 01:53:15 +00:00
|
|
|
|
}
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-06-11 22:15:08 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Makes a path relative to the %exe% dir
|
|
|
|
|
/// </summary>
|
2012-12-30 17:52:40 +00:00
|
|
|
|
public static string MakeProgramRelativePath(string path) { return MakeAbsolutePath("%exe%/" + path); }
|
2011-06-11 22:15:08 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The location of the default INI file
|
|
|
|
|
/// </summary>
|
2013-03-08 22:21:04 +00:00
|
|
|
|
public static string DefaultIniPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string blah = MakeProgramRelativePath("config.ini");
|
|
|
|
|
return blah;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-11 22:15:08 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets absolute base as derived from EXE
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2011-07-06 01:53:15 +00:00
|
|
|
|
public static string GetBasePathAbsolute()
|
|
|
|
|
{
|
2013-08-11 21:48:17 +00:00
|
|
|
|
if (Global.Config.PathEntries.GlobalBase.Length < 1) //If empty, then EXE path
|
2011-07-06 01:53:15 +00:00
|
|
|
|
return GetExeDirectoryAbsolute();
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2013-08-11 21:48:17 +00:00
|
|
|
|
if (Global.Config.PathEntries.GlobalBase.Length >= 5 &&
|
|
|
|
|
Global.Config.PathEntries.GlobalBase.Substring(0, 5) == "%exe%")
|
2011-07-06 01:53:15 +00:00
|
|
|
|
return GetExeDirectoryAbsolute();
|
2013-08-11 21:48:17 +00:00
|
|
|
|
if (Global.Config.PathEntries.GlobalBase[0] == '.')
|
2011-07-06 01:53:15 +00:00
|
|
|
|
{
|
2013-08-11 21:48:17 +00:00
|
|
|
|
if (Global.Config.PathEntries.GlobalBase.Length == 1)
|
2011-07-06 01:53:15 +00:00
|
|
|
|
return GetExeDirectoryAbsolute();
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-08-11 21:48:17 +00:00
|
|
|
|
if (Global.Config.PathEntries.GlobalBase.Length == 2 &&
|
|
|
|
|
Global.Config.PathEntries.GlobalBase == ".\\")
|
2011-07-06 01:53:15 +00:00
|
|
|
|
return GetExeDirectoryAbsolute();
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-08-11 21:48:17 +00:00
|
|
|
|
string tmp = Global.Config.PathEntries.GlobalBase;
|
2011-07-06 01:53:15 +00:00
|
|
|
|
tmp = tmp.Remove(0, 1);
|
|
|
|
|
tmp = tmp.Insert(0, GetExeDirectoryAbsolute());
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2013-08-11 21:48:17 +00:00
|
|
|
|
if (Global.Config.PathEntries.GlobalBase.Substring(0, 2) == "..")
|
|
|
|
|
return RemoveParents(Global.Config.PathEntries.GlobalBase, GetExeDirectoryAbsolute());
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
//In case of error, return EXE path
|
|
|
|
|
return GetExeDirectoryAbsolute();
|
|
|
|
|
}
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
public static string GetPlatformBase(string system)
|
|
|
|
|
{
|
2013-08-11 21:48:17 +00:00
|
|
|
|
if (system == "SGX" || system == "PCECD")
|
2011-07-06 01:53:15 +00:00
|
|
|
|
{
|
2013-08-11 21:48:17 +00:00
|
|
|
|
system = "PCE";
|
2011-07-06 01:53:15 +00:00
|
|
|
|
}
|
2013-08-11 21:48:17 +00:00
|
|
|
|
return Global.Config.PathEntries[system, "Base"].Path;
|
2011-07-06 01:53:15 +00:00
|
|
|
|
}
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2012-12-30 17:33:33 +00:00
|
|
|
|
public static string StandardFirmwareName(string name)
|
|
|
|
|
{
|
2013-08-11 21:48:17 +00:00
|
|
|
|
return Path.Combine(MakeAbsolutePath(Global.Config.PathEntries.FirmwaresPath), name);
|
2012-12-30 17:33:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-30 17:52:40 +00:00
|
|
|
|
public static string MakeAbsolutePath(string path, string system = null)
|
2011-07-06 01:53:15 +00:00
|
|
|
|
{
|
|
|
|
|
//This function translates relative path and special identifiers in absolute paths
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
if (path.Length < 1)
|
|
|
|
|
return GetBasePathAbsolute();
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
if (path == "%recent%")
|
|
|
|
|
{
|
|
|
|
|
return Environment.SpecialFolder.Recent.ToString();
|
|
|
|
|
}
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
if (path.Length >= 5 && path.Substring(0, 5) == "%exe%")
|
|
|
|
|
{
|
|
|
|
|
if (path.Length == 5)
|
|
|
|
|
return GetExeDirectoryAbsolute();
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string tmp = path.Remove(0, 5);
|
|
|
|
|
tmp = tmp.Insert(0, GetExeDirectoryAbsolute());
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
if (path[0] == '.')
|
|
|
|
|
{
|
2012-12-30 17:52:40 +00:00
|
|
|
|
if (!String.IsNullOrWhiteSpace(system))
|
2011-07-06 01:53:15 +00:00
|
|
|
|
{
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
path = path.Remove(0, 1);
|
|
|
|
|
path = path.Insert(0, GetPlatformBase(system));
|
|
|
|
|
}
|
|
|
|
|
if (path.Length == 1)
|
|
|
|
|
return GetBasePathAbsolute();
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (path[0] == '.')
|
|
|
|
|
{
|
|
|
|
|
path = path.Remove(0, 1);
|
|
|
|
|
path = path.Insert(0, GetBasePathAbsolute());
|
|
|
|
|
}
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
//If begins wtih .. do alorithm to determine how many ..\.. combos and deal with accordingly, return drive letter only if too many ..
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
if ((path[0] > 'A' && path[0] < 'Z') || (path[0] > 'a' && path[0] < 'z'))
|
|
|
|
|
{
|
|
|
|
|
//C:\
|
|
|
|
|
if (path.Length > 2 && path[1] == ':' && path[2] == '\\')
|
|
|
|
|
return path;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//file:\ is an acceptable path as well, and what FileBrowserDialog returns
|
|
|
|
|
if (path.Length >= 6 && path.Substring(0, 6) == "file:\\")
|
|
|
|
|
return path;
|
|
|
|
|
else
|
|
|
|
|
return GetExeDirectoryAbsolute(); //bad path
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
//all pad paths default to EXE
|
|
|
|
|
return GetExeDirectoryAbsolute();
|
|
|
|
|
}
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
public static string RemoveParents(string path, string workingpath)
|
|
|
|
|
{
|
|
|
|
|
//determines number of parents, then removes directories from working path, return absolute path result
|
|
|
|
|
//Ex: "..\..\Bob\", "C:\Projects\Emulators\Bizhawk" will return "C:\Projects\Bob\"
|
|
|
|
|
int x = NumParentDirectories(path);
|
|
|
|
|
if (x > 0)
|
|
|
|
|
{
|
2012-09-04 20:40:39 +00:00
|
|
|
|
int y = StringHelpers.HowMany(path, "..\\");
|
|
|
|
|
int z = StringHelpers.HowMany(workingpath, "\\");
|
2011-07-06 01:53:15 +00:00
|
|
|
|
if (y >= z)
|
|
|
|
|
{
|
|
|
|
|
//Return drive letter only, working path must be absolute?
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
else return path;
|
|
|
|
|
}
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
public static int NumParentDirectories(string path)
|
|
|
|
|
{
|
|
|
|
|
//determine the number of parent directories in path and return result
|
2012-09-04 20:40:39 +00:00
|
|
|
|
int x = StringHelpers.HowMany(path, '\\');
|
2011-07-06 01:53:15 +00:00
|
|
|
|
if (x > 0)
|
|
|
|
|
{
|
2012-09-04 20:40:39 +00:00
|
|
|
|
return StringHelpers.HowMany(path, "..\\");
|
2011-07-06 01:53:15 +00:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2011-05-04 02:00:08 +00:00
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
public static bool IsRecent(string path)
|
|
|
|
|
{
|
|
|
|
|
if (path == "%recent%")
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-05-21 01:13:48 +00:00
|
|
|
|
|
2012-11-29 18:42:13 +00:00
|
|
|
|
public static string GetLuaPath()
|
|
|
|
|
{
|
2013-08-11 21:48:17 +00:00
|
|
|
|
return MakeAbsolutePath(Global.Config.PathEntries.LuaPath);
|
2012-11-29 18:42:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-06 01:53:15 +00:00
|
|
|
|
public static string GetRomsPath(string sysID)
|
|
|
|
|
{
|
|
|
|
|
if (Global.Config.UseRecentForROMs)
|
2013-08-11 21:48:17 +00:00
|
|
|
|
{
|
2011-07-06 01:53:15 +00:00
|
|
|
|
return Environment.SpecialFolder.Recent.ToString();
|
2013-08-11 21:48:17 +00:00
|
|
|
|
}
|
2011-07-06 01:53:15 +00:00
|
|
|
|
|
2013-08-11 21:48:17 +00:00
|
|
|
|
if (sysID == "SGX" || sysID == "PCECD") //Yucky
|
2011-07-06 01:53:15 +00:00
|
|
|
|
{
|
2013-08-11 21:48:17 +00:00
|
|
|
|
sysID = "PCE";
|
2011-07-06 01:53:15 +00:00
|
|
|
|
}
|
2013-08-11 21:48:17 +00:00
|
|
|
|
else if (sysID == "NULL")
|
|
|
|
|
{
|
|
|
|
|
sysID = "Global";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PathEntry path = Global.Config.PathEntries[sysID, "ROM"];
|
2011-07-06 01:53:15 +00:00
|
|
|
|
|
2013-08-11 21:48:17 +00:00
|
|
|
|
if (path == null)
|
|
|
|
|
{
|
|
|
|
|
path = Global.Config.PathEntries[sysID, "Base"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return path.Path;
|
2011-07-06 01:53:15 +00:00
|
|
|
|
}
|
2011-08-04 03:20:54 +00:00
|
|
|
|
|
2011-09-04 19:24:40 +00:00
|
|
|
|
public static string RemoveInvalidFileSystemChars(string name)
|
|
|
|
|
{
|
|
|
|
|
string newStr = name;
|
|
|
|
|
char[] chars = Path.GetInvalidFileNameChars();
|
2013-05-02 00:56:39 +00:00
|
|
|
|
return chars.Aggregate(newStr, (current, c) => current.Replace(c.ToString(), ""));
|
2011-09-04 19:24:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-08-10 23:50:01 +00:00
|
|
|
|
public static string FilesystemSafeName(GameInfo game)
|
|
|
|
|
{
|
|
|
|
|
string filesystemSafeName = game.Name.Replace("|", "+");
|
2011-09-04 19:24:40 +00:00
|
|
|
|
filesystemSafeName = RemoveInvalidFileSystemChars(filesystemSafeName);
|
2012-07-22 20:26:38 +00:00
|
|
|
|
//zero 22-jul-2012 - i dont think this is used the same way it used to. game.Name shouldnt be a path, so this stuff is illogical.
|
|
|
|
|
//if game.Name is a path, then someone shouldve made it not-a-path already.
|
|
|
|
|
//return Path.Combine(Path.GetDirectoryName(filesystemSafeName), Path.GetFileNameWithoutExtension(filesystemSafeName));
|
|
|
|
|
return filesystemSafeName;
|
2011-08-10 23:50:01 +00:00
|
|
|
|
}
|
2011-08-04 03:20:54 +00:00
|
|
|
|
|
2011-08-10 23:50:01 +00:00
|
|
|
|
public static string SaveRamPath(GameInfo game)
|
|
|
|
|
{
|
|
|
|
|
string name = FilesystemSafeName(game);
|
2012-09-03 19:42:53 +00:00
|
|
|
|
if (Global.MovieSession.Movie.IsActive)
|
2012-03-03 18:11:07 +00:00
|
|
|
|
{
|
2011-09-17 14:23:23 +00:00
|
|
|
|
name += "." + Path.GetFileNameWithoutExtension(Global.MovieSession.Movie.Filename);
|
2012-03-03 18:11:07 +00:00
|
|
|
|
}
|
2011-09-17 14:23:23 +00:00
|
|
|
|
|
2013-08-11 21:48:17 +00:00
|
|
|
|
string sysId = "";
|
2011-08-10 23:50:01 +00:00
|
|
|
|
switch (game.System)
|
|
|
|
|
{
|
2013-08-11 21:48:17 +00:00
|
|
|
|
case "SGX":
|
|
|
|
|
case "PCECD":
|
|
|
|
|
sysId = "PCE";
|
|
|
|
|
break;
|
|
|
|
|
case "NULL":
|
|
|
|
|
sysId = "Global";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
sysId = game.System;
|
|
|
|
|
break;
|
2011-08-10 23:50:01 +00:00
|
|
|
|
}
|
2013-08-11 21:48:17 +00:00
|
|
|
|
|
|
|
|
|
PathEntry pathEntry = Global.Config.PathEntries[sysId, "Save RAM"];
|
|
|
|
|
|
|
|
|
|
if (pathEntry == null)
|
|
|
|
|
{
|
|
|
|
|
pathEntry = Global.Config.PathEntries[game.System, "Base"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Path.Combine(MakeAbsolutePath(pathEntry.Path), name);
|
2011-08-10 23:50:01 +00:00
|
|
|
|
}
|
2011-08-04 03:20:54 +00:00
|
|
|
|
|
2012-04-22 13:56:34 +00:00
|
|
|
|
public static string GetSaveStatePath(GameInfo game)
|
|
|
|
|
{
|
2013-08-11 21:48:17 +00:00
|
|
|
|
string sysId = "";
|
2012-04-22 13:56:34 +00:00
|
|
|
|
switch (game.System)
|
|
|
|
|
{
|
2013-08-11 21:48:17 +00:00
|
|
|
|
case "SGX":
|
|
|
|
|
case "PCECD":
|
|
|
|
|
sysId = "PCE";
|
|
|
|
|
break;
|
|
|
|
|
case "NULL":
|
|
|
|
|
sysId = "Global";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
sysId = game.System;
|
|
|
|
|
break;
|
2012-04-22 13:56:34 +00:00
|
|
|
|
}
|
2013-08-11 21:48:17 +00:00
|
|
|
|
|
|
|
|
|
PathEntry pathEntry = Global.Config.PathEntries[sysId, "Savestates"];
|
|
|
|
|
|
|
|
|
|
if (pathEntry == null)
|
|
|
|
|
{
|
|
|
|
|
pathEntry = Global.Config.PathEntries[game.System, "Base"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return MakeAbsolutePath(pathEntry.Path, sysId == "Global" ? null : sysId);
|
2012-04-22 13:56:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-08-10 23:50:01 +00:00
|
|
|
|
public static string SaveStatePrefix(GameInfo game)
|
|
|
|
|
{
|
|
|
|
|
string name = FilesystemSafeName(game);
|
2012-09-03 19:42:53 +00:00
|
|
|
|
|
|
|
|
|
if (Global.Config.BindSavestatesToMovies && Global.MovieSession.Movie.IsActive)
|
|
|
|
|
{
|
2011-09-05 00:50:50 +00:00
|
|
|
|
name += "." + Path.GetFileNameWithoutExtension(Global.MovieSession.Movie.Filename);
|
2012-09-03 19:42:53 +00:00
|
|
|
|
}
|
2013-08-11 21:48:17 +00:00
|
|
|
|
|
|
|
|
|
string sysId = "";
|
2011-08-10 23:50:01 +00:00
|
|
|
|
switch (game.System)
|
|
|
|
|
{
|
2013-08-11 21:48:17 +00:00
|
|
|
|
case "SGX":
|
|
|
|
|
case "PCECD":
|
|
|
|
|
sysId = "PCE";
|
|
|
|
|
break;
|
|
|
|
|
case "NULL":
|
|
|
|
|
sysId = "Global";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
sysId = game.System;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PathEntry pathEntry = Global.Config.PathEntries[sysId, "Savestates"];
|
|
|
|
|
|
|
|
|
|
if (pathEntry == null)
|
|
|
|
|
{
|
|
|
|
|
pathEntry = Global.Config.PathEntries[sysId, "Base"];
|
2011-08-10 23:50:01 +00:00
|
|
|
|
}
|
2013-08-11 21:48:17 +00:00
|
|
|
|
|
|
|
|
|
return Path.Combine(pathEntry.Path, name);
|
2011-08-10 23:50:01 +00:00
|
|
|
|
}
|
2011-08-04 03:20:54 +00:00
|
|
|
|
|
2011-08-10 23:50:01 +00:00
|
|
|
|
public static string ScreenshotPrefix(GameInfo game)
|
|
|
|
|
{
|
|
|
|
|
string name = FilesystemSafeName(game);
|
2013-08-11 21:48:17 +00:00
|
|
|
|
|
|
|
|
|
string sysId = "";
|
2011-08-10 23:50:01 +00:00
|
|
|
|
switch (game.System)
|
|
|
|
|
{
|
2013-08-11 21:48:17 +00:00
|
|
|
|
case "SGX":
|
|
|
|
|
case "PCECD":
|
|
|
|
|
sysId = "PCE";
|
|
|
|
|
break;
|
|
|
|
|
case "NULL":
|
|
|
|
|
sysId = "Global";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
sysId = game.System;
|
|
|
|
|
break;
|
2011-08-10 23:50:01 +00:00
|
|
|
|
}
|
2013-08-11 21:48:17 +00:00
|
|
|
|
|
|
|
|
|
PathEntry pathEntry = Global.Config.PathEntries[sysId, "Screenshots"];
|
|
|
|
|
|
|
|
|
|
if (pathEntry == null)
|
|
|
|
|
{
|
|
|
|
|
pathEntry = Global.Config.PathEntries[game.System, "Base"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Path.Combine(MakeAbsolutePath(pathEntry.Path), name);
|
2011-08-10 23:50:01 +00:00
|
|
|
|
}
|
2011-07-06 01:53:15 +00:00
|
|
|
|
}
|
2011-05-04 02:00:08 +00:00
|
|
|
|
}
|