Improve identification of .minecraft directory

This will use the following, in this order:

- $XDG_CONFIG_HOME/truecraft
- .config/truecraft (%APPDATA% on Windows)
- ~/.truecraft (%USERPROFILE% on Windows)

Fixes #201
This commit is contained in:
Drew DeVault 2015-10-06 08:09:27 -04:00
parent 2395c535b8
commit c4a65da5a1
5 changed files with 75 additions and 28 deletions

View File

@ -308,8 +308,7 @@ namespace TrueCraft.Client
public void TakeScreenshot() public void TakeScreenshot()
{ {
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), var path = Path.Combine(Paths.Screenshots, DateTime.Now.ToString("yyyy-MM-dd_H.mm.ss") + ".png");
".truecraft", "screenshots", DateTime.Now.ToString("yyyy-MM-dd_H.mm.ss") + ".png");
if (!Directory.Exists(Path.GetDirectoryName(path))) if (!Directory.Exists(Path.GetDirectoryName(path)))
Directory.CreateDirectory(Path.GetDirectoryName(path)); Directory.CreateDirectory(Path.GetDirectoryName(path));
using (var stream = File.OpenWrite(path)) using (var stream = File.OpenWrite(path))

65
TrueCraft.Core/Paths.cs Normal file
View File

@ -0,0 +1,65 @@
using System;
using System.IO;
namespace TrueCraft.Core
{
public static class Paths
{
public static string Base
{
get
{
var xdg_config_home = Environment.GetEnvironmentVariable("XDG_CONFIG_HOME");
string config = null;
if (xdg_config_home != null)
{
config = Path.Combine(xdg_config_home, "truecraft");
if (Directory.Exists(config))
return config;
}
var appdata = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"truecraft");
if (Directory.Exists(appdata))
return appdata;
var userprofile = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".truecraft");
if (Directory.Exists(userprofile))
return userprofile;
// At this point, there's no existing data to choose from, so use the best option
if (config != null)
{
Directory.CreateDirectory(config);
return config;
}
Directory.CreateDirectory(appdata);
return appdata;
}
}
public static string Worlds
{
get
{
return Path.Combine(Base, "worlds");
}
}
public static string Settings
{
get
{
return Path.Combine(Base, "settings.json");
}
}
public static string Screenshots
{
get
{
return Path.Combine(Base, "screenshots");
}
}
}
}

View File

@ -353,6 +353,7 @@
<Compile Include="Physics\PhysicsEngine.cs" /> <Compile Include="Physics\PhysicsEngine.cs" />
<Compile Include="Entities\WolfEntity.cs" /> <Compile Include="Entities\WolfEntity.cs" />
<Compile Include="Windows\FurnaceWindow.cs" /> <Compile Include="Windows\FurnaceWindow.cs" />
<Compile Include="Paths.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup> <ItemGroup>

View File

@ -8,15 +8,6 @@ namespace TrueCraft.Core
{ {
public static UserSettings Local { get; set; } public static UserSettings Local { get; set; }
public static string SettingsPath
{
get
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".truecraft", "settings.json");
}
}
public bool AutoLogin { get; set; } public bool AutoLogin { get; set; }
public string Username { get; set; } public string Username { get; set; }
public string Password { get; set; } public string Password { get; set; }
@ -44,14 +35,14 @@ namespace TrueCraft.Core
public void Load() public void Load()
{ {
if (File.Exists(SettingsPath)) if (File.Exists(Paths.Settings))
JsonConvert.PopulateObject(File.ReadAllText(SettingsPath), this); JsonConvert.PopulateObject(File.ReadAllText(Paths.Settings), this);
} }
public void Save() public void Save()
{ {
Directory.CreateDirectory(Path.GetDirectoryName(SettingsPath)); Directory.CreateDirectory(Path.GetDirectoryName(Paths.Settings));
File.WriteAllText(SettingsPath, JsonConvert.SerializeObject(this, Formatting.Indented)); File.WriteAllText(Paths.Settings, JsonConvert.SerializeObject(this, Formatting.Indented));
} }
} }

View File

@ -11,15 +11,6 @@ namespace TrueCraft.Launcher.Singleplayer
{ {
public class Worlds public class Worlds
{ {
public static string WorldsPath
{
get
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".truecraft", "worlds");
}
}
public static Worlds Local { get; set; } public static Worlds Local { get; set; }
public BlockRepository BlockRepository { get; set; } public BlockRepository BlockRepository { get; set; }
@ -27,11 +18,11 @@ namespace TrueCraft.Launcher.Singleplayer
public void Load() public void Load()
{ {
if (!Directory.Exists(WorldsPath)) if (!Directory.Exists(Paths.Worlds))
Directory.CreateDirectory(WorldsPath); Directory.CreateDirectory(Paths.Worlds);
BlockRepository = new BlockRepository(); BlockRepository = new BlockRepository();
BlockRepository.DiscoverBlockProviders(); BlockRepository.DiscoverBlockProviders();
var directories = Directory.GetDirectories(WorldsPath); var directories = Directory.GetDirectories(Paths.Worlds);
var saves = new List<World>(); var saves = new List<World>();
foreach (var d in directories) foreach (var d in directories)
{ {
@ -63,7 +54,7 @@ namespace TrueCraft.Launcher.Singleplayer
foreach (var c in Path.GetInvalidFileNameChars()) foreach (var c in Path.GetInvalidFileNameChars())
safeName = safeName.Replace(c.ToString(), ""); safeName = safeName.Replace(c.ToString(), "");
world.Name = name; world.Name = name;
world.Save(Path.Combine(WorldsPath, safeName)); world.Save(Path.Combine(Paths.Worlds, safeName));
Saves = Saves.Concat(new[] { world }).ToArray(); Saves = Saves.Concat(new[] { world }).ToArray();
return world; return world;
} }