-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathFileUtil.java
More file actions
82 lines (70 loc) · 2.46 KB
/
FileUtil.java
File metadata and controls
82 lines (70 loc) · 2.46 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
71
72
73
74
75
76
77
78
79
80
81
82
package i18nupdatemod.util;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
public class FileUtil {
private static Path resourcePackDirPath;
private static Path temporaryDirPath;
public static void setResourcePackDirPath(Path path) {
safeCreateDir(path);
resourcePackDirPath = path;
}
public static void setTemporaryDirPath(Path temporaryDirPath) {
safeCreateDir(temporaryDirPath);
FileUtil.temporaryDirPath = temporaryDirPath;
}
private static void safeCreateDir(Path path) {
try {
if (!Files.isDirectory(path)) {
Files.createDirectories(path);
}
} catch (Exception e) {
Log.warning("Cannot create dir: " + e);
}
}
public static Path getResourcePackPath(String filename) {
return resourcePackDirPath.resolve(filename);
}
public static Path getTemporaryPath(String filename) {
return temporaryDirPath.resolve(filename);
}
public static void syncTmpFile(Path filePath, Path tmpFilePath) throws IOException {
//Both temp and current file not found
if (!Files.exists(filePath) && !Files.exists(tmpFilePath)) {
Log.debug("Both temp and current file not found");
return;
}
int cmp = compareTmpFile(filePath, tmpFilePath);
Path from, to;
if (cmp == 0) {
Log.debug("Temp and current file has already been synchronized");
return;
} else if (cmp < 0) {
//Current file is newer
from = filePath;
to = tmpFilePath;
} else {
//Temp file is newer
from = tmpFilePath;
to = filePath;
}
if (to == filePath) {
//Don't save to game
return;
}
Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);
//Ensure same last modified time
Files.setLastModifiedTime(to, Files.getLastModifiedTime(from));
Log.info(String.format("Synchronized: %s -> %s", from, to));
}
private static int compareTmpFile(Path filePath, Path tmpFilePath) throws IOException {
if (!Files.exists(filePath)) {
return 1;
}
if (!Files.exists(tmpFilePath)) {
return -1;
}
return Files.getLastModifiedTime(tmpFilePath).compareTo(Files.getLastModifiedTime(filePath));
}
}