Tools.java 8.5 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.tools;

3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.InputStreamReader;
7
import java.io.OutputStreamWriter;
Melledy's avatar
Melledy committed
8
import java.io.PrintWriter;
9
import java.nio.charset.StandardCharsets;
Melledy's avatar
Melledy committed
10
11
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
12
import java.util.*;
Melledy's avatar
Melledy committed
13
14
15

import com.google.gson.reflect.TypeToken;

16
import emu.grasscutter.GameConstants;
Melledy's avatar
Melledy committed
17
import emu.grasscutter.Grasscutter;
18
19
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandMap;
20
import emu.grasscutter.data.GameData;
Melledy's avatar
Melledy committed
21
22
23
24
import emu.grasscutter.data.ResourceLoader;
import emu.grasscutter.data.def.AvatarData;
import emu.grasscutter.data.def.ItemData;
import emu.grasscutter.data.def.MonsterData;
25
import emu.grasscutter.data.def.SceneData;
26
import emu.grasscutter.utils.Utils;
Melledy's avatar
Melledy committed
27

方块君's avatar
方块君 committed
28
import static emu.grasscutter.utils.Language.translate;
29
import static emu.grasscutter.Configuration.*;
方块君's avatar
方块君 committed
30

KingRainbow44's avatar
KingRainbow44 committed
31
public final class Tools {
32
33
34
35
36
37
38
39
	public static void createGmHandbook() throws Exception {
		ToolsWithLanguageOption.createGmHandbook(getLanguageOption());
	}

	public static void createGachaMapping(String location) throws Exception {
		ToolsWithLanguageOption.createGachaMapping(location, getLanguageOption());
	}

40
41
42
43
44
45
	public static List<String> getAvailableLanguage() {
		File textMapFolder = new File(RESOURCE("TextMap"));
		List<String> availableLangList = new ArrayList<>();
		for (String textMapFileName : Objects.requireNonNull(textMapFolder.list((dir, name) -> name.startsWith("TextMap") && name.endsWith(".json")))) {
			availableLangList.add(textMapFileName.replace("TextMap", "").replace(".json", "").toLowerCase());
		} return availableLangList;
46
47
	}

48
	public static String getLanguageOption() {
49
		List<String> availableLangList = getAvailableLanguage();
Melledy's avatar
Melledy committed
50
	
51
52
53
54
		// Use system out for better format
		if (availableLangList.size() == 1) {
			return availableLangList.get(0).toUpperCase();
		}
55
56
57
58
		StringBuilder stagedMessage = new StringBuilder();
		stagedMessage.append("The following languages mappings are available, please select one: [default: EN] \n");
		
		StringBuilder groupedLangList = new StringBuilder(">\t"); String input;
59
		int groupedLangCount = 0;
60
		
61
62
		for (String availableLanguage: availableLangList){
			groupedLangCount++;
63
64
			groupedLangList.append(availableLanguage).append("\t");
			
65
			if (groupedLangCount == 6) {
66
				stagedMessage.append(groupedLangList).append("\n");
67
				groupedLangCount = 0;
68
				groupedLangList = new StringBuilder(">\t");
69
70
			}
		}
71
		
72
		if (groupedLangCount > 0) {
73
			stagedMessage.append(groupedLangList).append("\n");
74
		}
mingjun97's avatar
mingjun97 committed
75
		
76
77
78
		stagedMessage.append("\nYour choice:[EN] ");
		
		input = Grasscutter.getConsole().readLine(stagedMessage.toString());
79
80
81
82
83
84
85
86
87
88
		if (availableLangList.contains(input.toLowerCase())) {
			return input.toUpperCase();
		}
		Grasscutter.getLogger().info("Invalid option. Will use EN(English) as fallback");

		return "EN";
	}
}

final class ToolsWithLanguageOption {
Melledy's avatar
Melledy committed
89
	@SuppressWarnings("deprecation")
90
	public static void createGmHandbook(String language) throws Exception {
Melledy's avatar
Melledy committed
91
92
93
		ResourceLoader.loadResources();
		
		Map<Long, String> map;
94
		try (InputStreamReader fileReader = new InputStreamReader(new FileInputStream(Utils.toFilePath(RESOURCE("TextMap/TextMap"+language+".json"))), StandardCharsets.UTF_8)) {
Melledy's avatar
Melledy committed
95
96
97
98
99
			map = Grasscutter.getGsonFactory().fromJson(fileReader, new TypeToken<Map<Long, String>>() {}.getType());
		}
		
		List<Integer> list;
		String fileName = "./GM Handbook.txt";
100
		try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8), false)) {
Melledy's avatar
Melledy committed
101
102
103
			DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");  
			LocalDateTime now = LocalDateTime.now();
			   
104
			writer.println("// Grasscutter " + GameConstants.VERSION + " GM Handbook");
Melledy's avatar
Melledy committed
105
			writer.println("// Created " + dtf.format(now) + System.lineSeparator() + System.lineSeparator());
106
107

			CommandMap cmdMap = new CommandMap(true);
方块君's avatar
方块君 committed
108
			List<Command> cmdList = new ArrayList<>(cmdMap.getAnnotationsAsList());
109
110

			writer.println("// Commands");
方块君's avatar
方块君 committed
111
			for (Command cmd : cmdList) {
112
				StringBuilder cmdName = new StringBuilder(cmd.label());
113
				while (cmdName.length() <= 15) {
114
					cmdName.insert(0, " ");
115
				}
方块君's avatar
方块君 committed
116
117
				writer.println(cmdName + " : " + translate(cmd.description()));
			}
118
119
			writer.println();

120
			list = new ArrayList<>(GameData.getAvatarDataMap().keySet());
Melledy's avatar
Melledy committed
121
122
123
124
			Collections.sort(list); 
			 
			writer.println("// Avatars");
			for (Integer id : list) {
125
				AvatarData data = GameData.getAvatarDataMap().get(id);
Melledy's avatar
Melledy committed
126
127
128
129
130
				writer.println(data.getId() + " : " + map.get(data.getNameTextMapHash()));
			}
			
			writer.println();
			
131
			list = new ArrayList<>(GameData.getItemDataMap().keySet());
Melledy's avatar
Melledy committed
132
133
134
135
			Collections.sort(list); 
			
			writer.println("// Items");
			for (Integer id : list) {
136
				ItemData data = GameData.getItemDataMap().get(id);
Melledy's avatar
Melledy committed
137
138
139
140
141
				writer.println(data.getId() + " : " + map.get(data.getNameTextMapHash()));
			}
			
			writer.println();
			
142
			writer.println("// Scenes");
143
			list = new ArrayList<>(GameData.getSceneDataMap().keySet());
144
145
146
			Collections.sort(list); 
			
			for (Integer id : list) {
147
				SceneData data = GameData.getSceneDataMap().get(id);
148
149
150
151
152
				writer.println(data.getId() + " : " + data.getScriptData());
			}
			
			writer.println();
			
Melledy's avatar
Melledy committed
153
			writer.println("// Monsters");
154
			list = new ArrayList<>(GameData.getMonsterDataMap().keySet());
Melledy's avatar
Melledy committed
155
156
157
			Collections.sort(list); 
			
			for (Integer id : list) {
158
				MonsterData data = GameData.getMonsterDataMap().get(id);
Melledy's avatar
Melledy committed
159
160
161
162
163
164
				writer.println(data.getId() + " : " + map.get(data.getNameTextMapHash()));
			}
		}
		
		Grasscutter.getLogger().info("GM Handbook generated!");
	}
165
166

	@SuppressWarnings("deprecation")
167
	public static void createGachaMapping(String location, String language) throws Exception {
168
169
170
		ResourceLoader.loadResources();
		
		Map<Long, String> map;
171
		try (InputStreamReader fileReader = new InputStreamReader(new FileInputStream(Utils.toFilePath(RESOURCE("TextMap/TextMap" + language + ".json"))), StandardCharsets.UTF_8)) {
172
173
174
175
176
			map = Grasscutter.getGsonFactory().fromJson(fileReader, new TypeToken<Map<Long, String>>() {}.getType());
		}
		
		List<Integer> list;

177
		try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(location), StandardCharsets.UTF_8), false)) {
178
179
180
			list = new ArrayList<>(GameData.getAvatarDataMap().keySet());
			Collections.sort(list); 
			 
181
182
183
			// if the user made choices for language, I assume it's okay to assign his/her selected language to "en-us"
			// since it's the fallback language and there will be no difference in the gacha record page. 
			// The enduser can still modify the `gacha_mappings.js` directly to enable multilingual for the gacha record system.
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
			writer.println("mappings = {\"en-us\": {"); 

			// Avatars
			boolean first = true;
			for (Integer id : list) {
				AvatarData data = GameData.getAvatarDataMap().get(id);
				int avatarID = data.getId();
				if (avatarID >= 11000000) { // skip test avatar
					continue;
				}
				if (first) { // skip adding comma for the first element
					first = false;
				} else {
					writer.print(",");
				}
199
200
201
202
203
				String color = switch (data.getQualityType()) {
					case "QUALITY_PURPLE" -> "purple";
					case "QUALITY_ORANGE" -> "yellow";
					default -> "blue";
				};
204
				// Got the magic number 4233146695 from manually search in the json file
205
206
				writer.println(
					"\"" + (avatarID % 1000 + 1000) + "\" : [\"" 
207
					+ map.get(data.getNameTextMapHash()) + "(" +  map.get(4233146695L)+ ")\", \"" 
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
					+ color + "\"]");
			}
			
			writer.println();
			
			list = new ArrayList<>(GameData.getItemDataMap().keySet());
			Collections.sort(list); 
			
			// Weapons
			for (Integer id : list) {
				ItemData data = GameData.getItemDataMap().get(id);
				if (data.getId() <= 11101 || data.getId() >= 20000) {
					continue; //skip non weapon items
				}
				String color;

				switch (data.getRankLevel()){
					case 3: 
						color = "blue";
						break;
					case 4:
						color = "purple";
						break;
					case 5:
						color = "yellow";
						break;
					default:
						continue; // skip unnecessary entries
				}
237
238
239
				
				// Got the magic number 4231343903 from manually search in the json file

240
241
				writer.println(",\"" + data.getId() +
						 "\" : [\"" + map.get(data.getNameTextMapHash()).replaceAll("\"", "")
242
						 + "("+ map.get(4231343903L)+")\",\""+ color + "\"]");
243
			}
244
			writer.println(",\"200\": \""+map.get(332935371L)+"\", \"301\": \""+ map.get(2272170627L) + "\", \"302\": \""+map.get(2864268523L)+"\"");
245
246
			writer.println("}\n}");
		}
247
		
mingjun97's avatar
mingjun97 committed
248
		Grasscutter.getLogger().info("Mappings generated to " + location + " !");
249
	}
Melledy's avatar
Melledy committed
250
}