DataLoader.java 4.62 KB
Newer Older
1
2
3
4
5
6
7
8
package emu.grasscutter.data;

import emu.grasscutter.Grasscutter;
import emu.grasscutter.server.http.handlers.GachaHandler;
import emu.grasscutter.tools.Tools;
import emu.grasscutter.utils.FileUtils;
import emu.grasscutter.utils.Utils;

9
10
import static emu.grasscutter.config.Configuration.DATA;

KingRainbow44's avatar
KingRainbow44 committed
11
12
import java.io.FileInputStream;
import java.io.FileNotFoundException;
13
import java.io.IOException;
KingRainbow44's avatar
KingRainbow44 committed
14
import java.io.InputStream;
15
import java.io.InputStreamReader;
16
17
18
19
20
21
22
import java.nio.file.Path;
import java.util.List;

public class DataLoader {

    /**
     * Load a data file by its name. If the file isn't found within the /data directory then it will fallback to the default within the jar resources
KingRainbow44's avatar
KingRainbow44 committed
23
     *
24
25
26
     * @param resourcePath The path to the data file to be loaded.
     * @return InputStream of the data file.
     * @throws FileNotFoundException
KingRainbow44's avatar
KingRainbow44 committed
27
     * @see #load(String, boolean)
28
29
30
31
     */
    public static InputStream load(String resourcePath) throws FileNotFoundException {
        return load(resourcePath, true);
    }
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    
    /**
     * Creates an input stream reader for a data file. If the file isn't found within the /data directory then it will fallback to the default within the jar resources
     *
     * @param resourcePath The path to the data file to be loaded.
     * @return InputStreamReader of the data file.
     * @throws IOException
     * @throws FileNotFoundException
     * @see #load(String, boolean)
     */
    public static InputStreamReader loadReader(String resourcePath) throws IOException, FileNotFoundException {
        try {
            InputStream is = load(resourcePath, true);
            return new InputStreamReader(is);
        } catch (FileNotFoundException exception) {
            throw exception;
        }
    }
50
51
52

    /**
     * Load a data file by its name.
KingRainbow44's avatar
KingRainbow44 committed
53
     *
54
     * @param resourcePath The path to the data file to be loaded.
KingRainbow44's avatar
KingRainbow44 committed
55
     * @param useFallback  If the file does not exist in the /data directory, should it use the default file in the jar?
56
57
58
59
     * @return InputStream of the data file.
     * @throws FileNotFoundException
     */
    public static InputStream load(String resourcePath, boolean useFallback) throws FileNotFoundException {
KingRainbow44's avatar
KingRainbow44 committed
60
        if (Utils.fileExists(DATA(resourcePath))) {
61
62
63
            // Data is in the resource directory
            return new FileInputStream(DATA(resourcePath));
        } else {
KingRainbow44's avatar
KingRainbow44 committed
64
            if (useFallback) {
65
66
67
68
69
70
71
                return FileUtils.readResourceAsStream("/defaults/data/" + resourcePath);
            }
        }

        return null;
    }

72
    public static void checkAllFiles() {
73
74
        try {
            List<Path> filenames = FileUtils.getPathsFromResource("/defaults/data/");
KingRainbow44's avatar
KingRainbow44 committed
75

76
            if (filenames == null) {
KingRainbow44's avatar
KingRainbow44 committed
77
78
                Grasscutter.getLogger().error("We were unable to locate your default data files.");
            } else for (Path file : filenames) {
79
                String relativePath = String.valueOf(file).split("defaults[\\\\\\/]data[\\\\\\/]")[1];
80

81
                checkAndCopyData(relativePath);
82
83
            }
        } catch (Exception e) {
84
            Grasscutter.getLogger().error("An error occurred while trying to check the data folder.", e);
85
86
        }

87
        generateGachaMappings();
88
89
    }

90
    private static void checkAndCopyData(String name) {
91
92
93
94
95
96
97
98
        String filePath = Utils.toFilePath(DATA(name));

        if (!Utils.fileExists(filePath)) {
            // Check if file is in subdirectory
            if (name.indexOf("/") != -1) {
                String[] path = name.split("/");

                String folder = "";
KingRainbow44's avatar
KingRainbow44 committed
99
                for (int i = 0; i < (path.length - 1); i++) {
100
101
102
103
                    folder += path[i] + "/";

                    // Make sure the current folder exists
                    String folderToCreate = Utils.toFilePath(DATA(folder));
KingRainbow44's avatar
KingRainbow44 committed
104
                    if (!Utils.fileExists(folderToCreate)) {
105
106
107
108
109
110
111
112
113
114
115
                        Grasscutter.getLogger().info("Creating data folder '" + folder + "'");
                        Utils.createFolder(folderToCreate);
                    }
                }
            }

            Grasscutter.getLogger().info("Creating default '" + name + "' data");
            FileUtils.copyResource("/defaults/data/" + name, filePath);
        }
    }

116
    private static void generateGachaMappings() {
117
118
119
120
121
122
123
124
125
126
        if (!Utils.fileExists(GachaHandler.gachaMappings)) {
            try {
                Grasscutter.getLogger().info("Creating default '" + GachaHandler.gachaMappings + "' data");
                Tools.createGachaMapping(GachaHandler.gachaMappings);
            } catch (Exception exception) {
                Grasscutter.getLogger().warn("Failed to create gacha mappings. \n" + exception);
            }
        }
    }
}