DataLoader.java 3.83 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;

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

import static emu.grasscutter.Configuration.DATA;

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
21
     *
22
23
24
     * @param resourcePath The path to the data file to be loaded.
     * @return InputStream of the data file.
     * @throws FileNotFoundException
KingRainbow44's avatar
KingRainbow44 committed
25
     * @see #load(String, boolean)
26
27
28
29
30
31
32
     */
    public static InputStream load(String resourcePath) throws FileNotFoundException {
        return load(resourcePath, true);
    }

    /**
     * Load a data file by its name.
KingRainbow44's avatar
KingRainbow44 committed
33
     *
34
     * @param resourcePath The path to the data file to be loaded.
KingRainbow44's avatar
KingRainbow44 committed
35
     * @param useFallback  If the file does not exist in the /data directory, should it use the default file in the jar?
36
37
38
39
     * @return InputStream of the data file.
     * @throws FileNotFoundException
     */
    public static InputStream load(String resourcePath, boolean useFallback) throws FileNotFoundException {
KingRainbow44's avatar
KingRainbow44 committed
40
        if (Utils.fileExists(DATA(resourcePath))) {
41
42
43
            // Data is in the resource directory
            return new FileInputStream(DATA(resourcePath));
        } else {
KingRainbow44's avatar
KingRainbow44 committed
44
            if (useFallback) {
45
46
47
48
49
50
51
52
53
54
                return FileUtils.readResourceAsStream("/defaults/data/" + resourcePath);
            }
        }

        return null;
    }

    public static void CheckAllFiles() {
        try {
            List<Path> filenames = FileUtils.getPathsFromResource("/defaults/data/");
KingRainbow44's avatar
KingRainbow44 committed
55

56
            if (filenames == null) {
KingRainbow44's avatar
KingRainbow44 committed
57
58
                Grasscutter.getLogger().error("We were unable to locate your default data files.");
            } else for (Path file : filenames) {
59
                String relativePath = String.valueOf(file).split("defaults[\\\\\\/]data[\\\\\\/]")[1];
60
61
62
63

                CheckAndCopyData(relativePath);
            }
        } catch (Exception e) {
64
            Grasscutter.getLogger().error("An error occurred while trying to check the data folder.", e);
65
66
67
68
69
70
71
72
73
74
75
76
77
78
        }

        GenerateGachaMappings();
    }

    private static void CheckAndCopyData(String name) {
        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
79
                for (int i = 0; i < (path.length - 1); i++) {
80
81
82
83
                    folder += path[i] + "/";

                    // Make sure the current folder exists
                    String folderToCreate = Utils.toFilePath(DATA(folder));
KingRainbow44's avatar
KingRainbow44 committed
84
                    if (!Utils.fileExists(folderToCreate)) {
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
                        Grasscutter.getLogger().info("Creating data folder '" + folder + "'");
                        Utils.createFolder(folderToCreate);
                    }
                }
            }

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

    private static void GenerateGachaMappings() {
        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);
            }
        }
    }
}