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:
parent
2395c535b8
commit
c4a65da5a1
@ -308,8 +308,7 @@ namespace TrueCraft.Client
|
||||
|
||||
public void TakeScreenshot()
|
||||
{
|
||||
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
|
||||
".truecraft", "screenshots", DateTime.Now.ToString("yyyy-MM-dd_H.mm.ss") + ".png");
|
||||
var path = Path.Combine(Paths.Screenshots, DateTime.Now.ToString("yyyy-MM-dd_H.mm.ss") + ".png");
|
||||
if (!Directory.Exists(Path.GetDirectoryName(path)))
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
||||
using (var stream = File.OpenWrite(path))
|
||||
|
65
TrueCraft.Core/Paths.cs
Normal file
65
TrueCraft.Core/Paths.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -353,6 +353,7 @@
|
||||
<Compile Include="Physics\PhysicsEngine.cs" />
|
||||
<Compile Include="Entities\WolfEntity.cs" />
|
||||
<Compile Include="Windows\FurnaceWindow.cs" />
|
||||
<Compile Include="Paths.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<ItemGroup>
|
||||
|
@ -8,15 +8,6 @@ namespace TrueCraft.Core
|
||||
{
|
||||
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 string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
@ -44,14 +35,14 @@ namespace TrueCraft.Core
|
||||
|
||||
public void Load()
|
||||
{
|
||||
if (File.Exists(SettingsPath))
|
||||
JsonConvert.PopulateObject(File.ReadAllText(SettingsPath), this);
|
||||
if (File.Exists(Paths.Settings))
|
||||
JsonConvert.PopulateObject(File.ReadAllText(Paths.Settings), this);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(SettingsPath));
|
||||
File.WriteAllText(SettingsPath, JsonConvert.SerializeObject(this, Formatting.Indented));
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(Paths.Settings));
|
||||
File.WriteAllText(Paths.Settings, JsonConvert.SerializeObject(this, Formatting.Indented));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,15 +11,6 @@ namespace TrueCraft.Launcher.Singleplayer
|
||||
{
|
||||
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 BlockRepository BlockRepository { get; set; }
|
||||
@ -27,11 +18,11 @@ namespace TrueCraft.Launcher.Singleplayer
|
||||
|
||||
public void Load()
|
||||
{
|
||||
if (!Directory.Exists(WorldsPath))
|
||||
Directory.CreateDirectory(WorldsPath);
|
||||
if (!Directory.Exists(Paths.Worlds))
|
||||
Directory.CreateDirectory(Paths.Worlds);
|
||||
BlockRepository = new BlockRepository();
|
||||
BlockRepository.DiscoverBlockProviders();
|
||||
var directories = Directory.GetDirectories(WorldsPath);
|
||||
var directories = Directory.GetDirectories(Paths.Worlds);
|
||||
var saves = new List<World>();
|
||||
foreach (var d in directories)
|
||||
{
|
||||
@ -63,7 +54,7 @@ namespace TrueCraft.Launcher.Singleplayer
|
||||
foreach (var c in Path.GetInvalidFileNameChars())
|
||||
safeName = safeName.Replace(c.ToString(), "");
|
||||
world.Name = name;
|
||||
world.Save(Path.Combine(WorldsPath, safeName));
|
||||
world.Save(Path.Combine(Paths.Worlds, safeName));
|
||||
Saves = Saves.Concat(new[] { world }).ToArray();
|
||||
return world;
|
||||
}
|
||||
|
Reference in New Issue
Block a user