-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinecraftServer.Files.cs
More file actions
70 lines (57 loc) · 1.85 KB
/
MinecraftServer.Files.cs
File metadata and controls
70 lines (57 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.IO;
using System.Linq;
namespace Minecraft;
public partial class MinecraftServer
{
public sealed class FilePaths
{
public string Base { get; } = Directory.GetCurrentDirectory();
public string Logs { get; }
public string Plugins { get; }
public string Configs { get; }
public string LatestLog { get; }
public const string LogsDirectory = "logs";
public const string PluginsDirectory = "plugins";
public const string ConfigsDirectory = "configs";
public const string LatestLogFile = "latest.log";
internal FilePaths()
{
Logs = Path.Combine(Base, LogsDirectory);
Plugins = Path.Combine(Base, PluginsDirectory);
Configs = Path.Combine(Base, ConfigsDirectory);
LatestLog = Path.Combine(Logs, LatestLogFile);
}
public bool ArchiveLatestLog()
{
if (!Directory.Exists(Logs))
{
Directory.CreateDirectory(Logs);
return true;
}
if (!File.Exists(LatestLog))
{
return true;
}
DateTime date = File.GetLastWriteTime(LatestLog);
string[] filesWithSameDateAsLatest = Directory.GetFiles(LogsDirectory, $"log-{date:MM-dd-yyyy}-*.log");
for (int i = 1; ; i++)
{
string path = Path.Combine(Logs, $"log-{date:MM-dd-yyyy}-{i}.log");
if (filesWithSameDateAsLatest.Contains(path))
{
continue;
}
try
{
File.Move(LatestLog, path, true);
}
catch
{
return false;
}
return true;
}
}
}
}