Merge unstable into development (#2173)

* Remove more scene synchronized

* Fix worktop options not appearing

* Format code [skip actions]

* Fix delay with server tasks

* Format code [skip actions]

* Fully fix fairy clock (#2146)

* Fix scene transition

* fully fix fairy clock

* Re-add call to `Player#updatePlayerGameTime`

* Format code [skip actions]

* Initialize the script loader in `ResourceLoader#loadAll`

* Fix region removal checking

* Format code [skip actions]

* Use Lombok's `EqualsAndHashCode` for comparing scene regions

* Format code [skip actions]

* Move 'invalid gather object' to `trace`

* Add more information to the 'unknown condition handler' message

* Move invalid ability action to trace

* Make `KcpTunnel` public

* Validate the NPC being talked to

* Format code [skip actions]

* NPCs are not spawned server side; change logic to handle it

* Format code [skip actions]

* unload scene when there are no players (#2147)

* unload scene when there are no players

* Update src/main/java/emu/grasscutter/game/world/Scene.java

Co-authored-by: Magix <27646710+KingRainbow44@users.noreply.github.com>

---------

Co-authored-by: Magix <27646710+KingRainbow44@users.noreply.github.com>

* Check if a command should be copied or HTTP should be used

* Lint Code [skip actions]

* Fix character names rendering incorrectly

* Add basic troubleshooting command

* Implement handbook teleporting

also a few formatting changes and sort data by logical sense

* Fix listener `ConcurrentModificationException` issue

* Add color change to `Join the Community!`

* Lint Code [skip actions]

* Make clickable buttons appear clickable

* Remove 'Mechanicus' entities from the list of entities

* Format code [skip actions]

* Fix going back returning a blank screen

* Implement entity spawning

* Add setting level to entity card

* Add support for 'plain text' mode

* Make descriptions of objects scrollable

* Lint Code [skip actions]

* Format code [skip actions]

* Change the way existing hooks work

* Format code [skip actions]

* Upgrade Javalin to 5.5.0 & Fix project warnings

* Upgrade logging libraries

* Fix gacha mappings static file issue

* Add temporary backwards compatability for `ServerHelper`

* Format code [skip actions]

* Remove artifact signatures from VCS

* Fix forge queue data protocol definition

* Run `spotlessApply`

* Format code [skip actions]

* Download data required for building artifacts

* Add call for Facebook logins

* Add the wiki page as a submodule

* Format code [skip actions]

* Update translation (#2150)

* Update translation

* Update translation

* Separate the dispatch and game servers (pt. 1)

gacha is still broken, handbook still needs to be done

* Format code [skip actions]

* Separate the dispatch and game servers (pt. 2)

this commit fixes the gacha page

* Add description for '/troubleshoot'

* Set default avatar talent level to 10

* Separate the dispatch and game servers (pt. 3)

implement handbook across servers!

* Format code [skip actions]

* Update GitHub Actions to use 'download-file' over 'wget'

* Gm handbook lmao (#2149)

* Fix font issue

* Fix avatars

* Fix text overflow in commands

* Fix virtualized lists and items page 😭😭

* magix why 💀

* use hover style in all minicards

* button

* remove console.log

* lint

* Add icons

* magix asked

* Fix overflow padding issue

* Fix achievement text overflow

* remove icons from repo

* Change command icon

* Add the wiki page as a submodule

* total magix moment

* fix text overflow in commands

* Fix discord button

* Make text scale on Minicard

* import icons and font from another source

* Add hover effects to siebar buttons

* move font and readme to submodule repo

* Make data folder a submodule

* import icons and font from data submodule

* Update README.md

* total magix moment

* magix moment v2

* submodule change

* Import `.webp` files

* Resize `HomeButton`

* Fix 'Copy Command' reappearing after changing pages

---------

Co-authored-by: KingRainbow44 <kobedo11@gmail.com>

* Lint Code [skip actions]

* Download data for the build, not for the lint

* format imports

this is really just to see if build handbook works kek

* Implement proper handbook authentication (pt. 1)

* Implement proper handbook authentication (pt. 2)

* Format code [skip actions]

* Add quest data dumping for the handbook

* Change colors to fit _something suitable_

* Format code [skip actions]

* Fix force pushing to branches after linting

* Fix logic of `SetPlayerPropReq`

* Move more group loading to `trace`

* Add handbook IP authentication in hybrid mode

* Fix player level up not displaying on the client properly

* Format code [skip actions]

* Fix game time locking

* Format code [skip actions]

* Update player properties

* Format code [skip actions]

* Move `warn`s for groups to `debug`

* Fix player pausing

* Move more logs to `trace`

* Use `removeItemById` for deleting items via quests

* Clean up logger more

* Pause in-game time when the world is paused

* Format code [skip actions]

* More player property documentation

* Multi-threaded resource loading

* Format code [skip actions]

* Add quest widgets

* Add quests page (basic impl.)

* Add/fix colors

also fix tailwind

* Remove banned packets

client modifications already perform the job of blocking malicious packets from being executed, no point in having this if self-windy is wanted

* Re-add `BeginCameraSceneLookNotify`

* Fix being unable to attack (#2157)

* Add `PlayerOpenChestEvent`

* Add methods to get players from the server

* Add static methods to register an event handler

* Add `PlayerEnterDungeonEvent`

* Remove legacy documentation from `PlayerMoveEvent`

* Add `PlayerChatEvent`

* Add defaults to `Position`

* Clean up `.utils`

* Revert `Multi-threaded resource loading`

* Fix changing target UID when talking to the server

* Lint Code [skip actions]

* Format code [skip actions]

* fix NPC talk triggering main quest in 46101 (#2158)

Make it so that only talks where the param matches the talkId are checked.

* Format code [skip actions]

* Partially fix Chasing Shadows (#2159)

* Partially fix Chasing Shadows

* Go ahead and move it before the return before Magix tells me to.

* Format code [skip actions]

* Bring back period lol (#2160)

* Disable SNI for the HTTPS server

* Add `EntityCreationEvent`

* Add initial startup message

this is so the server appears like its preparing to start

* Format code [skip actions]

* Enable debug mode for plugin loggers if enabled for the primary logger

* Add documentation about `WorldAreaConfigData`

* Make more fields in excels accessible

* Remove deprecated fields from `GetShopRsp`

* Run `spotlessApply` on definitions

* Add `PlayerEnterAreaEvent`

* Optimize event calls

* Fix event invokes

* Format code [skip actions]

* Remove manual autofinish for main quests. (#2162)

* Add world areas to the textmap cache

* Format code [skip actions]

* Don't overdefine variables in extended classes (#2163)

* Add dumper for world areas

* Format code [skip actions]

* instantiate personalLineList (#2165)

* Fix protocol definitions

thank you Nazrin! (+ hiro for raw definitions)

* Fix the background color leaking from the character widget

* Change HTML spacing to 2 spaces

* Implement hiding widgets

* Change scrollbar to a vibrant color

* Add _some_ scaling to the home buttons and its text

* Build the handbook with Gradle

* Fix the 'finer details' with the handbook UI

* Lint Code [skip actions]

* Fix target destination for the Gradle-built handbook

* Implement fetching a player across servers & Add a chainable JsonObject

useful for plugins! might be used in grasscutter eventually

* Fix GitHub actions

* Fix event calling & canceling

* Run `spotlessApply`

* Rename fields (might be wrong)

* Add/update all/more protocol definitions

* Add/update all/more protocol definitions

* Remove outdated packet

* Fix protocol definitions

* Format code [skip actions]

* Implement some lua variables for less console spam (#2172)

* Implement some lua variables for less console spam

* Add GetHostQuestState

This fixes some chapter 3 stuff.

* Format code [skip actions]

* Fix merge import

* Format code [skip actions]

* Fully fix fairy clock for real this time (#2167)

* Fully fix fairy clock For real this time

* Make it so relogging keeps the time lock state.

* Refactor out questLockTime

* Per Hartie, the client packet needs to be changed too

* Update src/main/java/emu/grasscutter/game/world/World.java

Co-authored-by: Magix <27646710+KingRainbow44@users.noreply.github.com>

* Update src/main/java/emu/grasscutter/server/packet/recv/HandlerClientLockGameTimeNotify.java

* Remove all code not needed to get clock working

---------

Co-authored-by: Magix <27646710+KingRainbow44@users.noreply.github.com>

* Implement a proper ability system (#2166)

* Apply fix `21dec2fe`

* Apply fix `89d01d5f`

* Apply fix `d900f154`

this one was already implemented; updated to use call from previous commit

* Ability changing commit

TODO: change info to debug

* Remove use of deprecated methods/fields

* Temp commit v2
(Adding LoseHP and some fixes)

* Oopsie

* Probably fix monster battle

* Fix issue with reflecting into fields

* Fix some things

* Fix ability names for 3.6 resources

* Improve logging

---------

Co-authored-by: StartForKiller <jesussanz2003@gmail.com>

* Format code [skip actions]

* Add system for sending messages between servers

* Format some code

* Remove protocol definitions from Spotless

* Default debug to false; enable with `-debug`

* Implement completely useless global value copying

* HACK: Return the avatar which holds the weapon when the weapon is referred to by ID

* Add properties to `AbilityModifier`

* Change the way HTML is served after authentication

* Use thread executors to speed up the database loading process

* Format code [skip actions]

* Add system for setting handbook address and port

* Lint Code [skip actions]

* Format code [skip actions]

* Fix game-related data not saving

* Format code [skip actions]

* Fix handbook server details

* Lint Code [skip actions]

* Format code [skip actions]

* Use the headers provided by a context to get the IP address

should acknowledge #1975

* Format code [skip actions]

* Move more logs to `trace`

* Format code [skip actions]

* more trace

* Fix something and implement weapon entities

* Format code [skip actions]

* Fix `EntityWeapon`

* Remove deprecated API & Fix resource checking

* Fix unnecessary warning for first-time setup

* Implement handbook request limiting

* Format code [skip actions]

* Fix new avatar weapons being null

* Format code [skip actions]

* Fix issue with 35303 being un-completable & Try to fix fulfilled quest conditions being met

* Load activity config on server startup

* Require plugins to specify an API version and match with the server

* Add default open state ignore list

* Format code [skip actions]

* Quick fix for questing, needs more investigation
This would make the questing work again

* Remove existing hack for 35303

* Fix ignored open states from being set

* Format code [skip actions]

* fix the stupidest bug ive ever seen

* Optimize player kicking on server close

* Format code [skip actions]

* Re-add hack to fix 35303

* Update GitHub actions

* Format code [skip actions]

* Potentially fix issues with regions

* Download additional handbook data

* Revert "Potentially fix issues with regions"

This reverts commit 84e3823695.

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: scooterboo <lewasite@yahoo.com>
Co-authored-by: Tesutarin <105267106+Tesutarin@users.noreply.github.com>
Co-authored-by: Scald <104459145+Arikatsu@users.noreply.github.com>
Co-authored-by: StartForKiller <jesussanz2003@gmail.com>
This commit is contained in:
Magix
2023-05-31 20:48:16 -07:00
committed by GitHub
parent f46fd372d2
commit 9e5b57a043
3839 changed files with 1841548 additions and 37533 deletions

View File

@@ -1,20 +1,490 @@
package emu.grasscutter.tools;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.Command.TargetRequirement;
import emu.grasscutter.command.CommandMap;
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.ResourceLoader;
import emu.grasscutter.game.inventory.ItemType;
import emu.grasscutter.game.props.SceneType;
import emu.grasscutter.utils.JsonUtils;
import emu.grasscutter.utils.lang.Language;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import emu.grasscutter.net.proto.GetGachaInfoRspOuterClass.GetGachaInfoRsp;
import emu.grasscutter.net.proto.GetShopRspOuterClass.GetShopRsp;
public interface Dumpers {
// See `src/handbook/data/README.md` for attributions.
public final class Dumpers {
public static void extractBanner(byte[] data) throws Exception {
GetGachaInfoRsp proto = GetGachaInfoRsp.parseFrom(data);
System.out.println(proto);
/**
* Fetches the description of a command.
*
* @param locale The locale to use.
* @param command The command to get the description of.
* @return The description of the command.
*/
private static String commandDescription(String locale, Command command) {
try {
// Get the language by the locale.
var language = Language.getLanguage(locale);
if (language == null) throw new IllegalArgumentException("Invalid language.");
return language.get("commands." + command.label() + ".description");
} catch (IllegalArgumentException ignored) {
return command.label();
}
}
public static void extractShop(byte[] data) throws Exception {
GetShopRsp proto = GetShopRsp.parseFrom(data);
System.out.println(proto);
/**
* Encodes the dump into comma separated values.
*
* @param dump The dump to encode.
* @return The encoded dump.
*/
private static String miniEncode(Map<Integer, ?> dump) {
return dump.entrySet().stream()
.map(entry -> entry.getKey() + "," + entry.getValue().toString())
.collect(Collectors.joining("\n"));
}
/**
* Encodes the dump into comma separated values.
*
* @param dump The dump to encode.
* @return The encoded dump.
*/
private static String miniEncode(Map<Integer, ?> dump, String... headers) {
return String.join(",", headers) + "\n" + Dumpers.miniEncode(dump);
}
/**
* Dumps all commands to a JSON file.
*
* @param locale The language to dump the commands in.
*/
static void dumpCommands(String locale) {
// Check that commands are registered.
var commandMap = CommandMap.getInstance();
if (commandMap == null) commandMap = new CommandMap(true);
// Convert all registered commands to an info map.
var dump = new HashMap<String, CommandInfo>();
commandMap
.getAnnotationsAsList()
.forEach(
command -> {
var description = Dumpers.commandDescription(locale, command);
var labels =
new ArrayList<String>() {
{
this.add(command.label());
this.addAll(List.of(command.aliases()));
}
};
// Add the command info to the list.
dump.put(
command.label(),
new CommandInfo(
labels,
description,
List.of(command.usage()),
List.of(command.permission(), command.permissionTargeted()),
command.targetRequirement()));
});
try {
// Create a file for the dump.
var file = new File("commands.json");
if (file.exists() && !file.delete()) throw new RuntimeException("Failed to delete file.");
if (!file.exists() && !file.createNewFile())
throw new RuntimeException("Failed to create file.");
// Write the dump to the file.
Files.writeString(file.toPath(), JsonUtils.encode(dump));
} catch (IOException ignored) {
throw new RuntimeException("Failed to write to file.");
}
}
/**
* Dumps all avatars to a CSV file.
*
* @param locale The language to dump the avatars in.
*/
static void dumpAvatars(String locale) {
// Reload resources.
ResourceLoader.loadAll();
Language.loadTextMaps();
// Convert all known avatars to an avatar map.
var dump = new HashMap<Integer, AvatarInfo>();
GameData.getAvatarDataMap()
.forEach(
(id, avatar) -> {
var langHash = avatar.getNameTextMapHash();
dump.put(
id,
new AvatarInfo(
langHash == 0
? avatar.getName()
: Language.getTextMapKey(langHash).get(locale),
avatar.getQualityType().equals("QUALITY_PURPLE")
? Quality.EPIC
: Quality.LEGENDARY));
});
try {
// Create a file for the dump.
var file = new File("avatars.csv");
if (file.exists() && !file.delete()) throw new RuntimeException("Failed to delete file.");
if (!file.exists() && !file.createNewFile())
throw new RuntimeException("Failed to create file.");
// Write the dump to the file.
Files.writeString(file.toPath(), Dumpers.miniEncode(dump));
} catch (IOException ignored) {
throw new RuntimeException("Failed to write to file.");
}
}
/**
* Dumps all items to a CSVv file.
*
* @param locale The language to dump the items in.
*/
static void dumpItems(String locale) {
// Reload resources.
ResourceLoader.loadAll();
Language.loadTextMaps();
// Convert all known items to an item map.
var originalDump = new ArrayList<ItemInfo>();
GameData.getItemDataMap()
.forEach(
(id, item) ->
originalDump.add(
new ItemInfo(
id,
Language.getTextMapKey(item.getNameTextMapHash()).get(locale),
Quality.from(item.getRankLevel()),
item.getItemType(),
item.getIcon().length() > 0 ? item.getIcon().substring(3) : "")));
// Create a new dump with filtered duplicates.
var names = new ArrayList<String>();
var dump = new HashMap<Integer, ItemInfo>();
originalDump.forEach(
item -> {
// Validate the item.
if (item.name.contains("[CHS]")) return;
if (names.contains(item.name)) return;
if (dump.containsKey(item.id)) return;
// Add the item to the dump.
names.add(item.name);
dump.put(item.id, item);
});
try {
// Create a file for the dump.
var file = new File("items.csv");
if (file.exists() && !file.delete()) throw new RuntimeException("Failed to delete file.");
if (!file.exists() && !file.createNewFile())
throw new RuntimeException("Failed to create file.");
// Write the dump to the file.
Files.writeString(file.toPath(), Dumpers.miniEncode(dump));
} catch (IOException ignored) {
throw new RuntimeException("Failed to write to file.");
}
}
/** Dumps all scenes to a CSV file. */
static void dumpScenes() {
// Reload resources.
ResourceLoader.loadAll();
Language.loadTextMaps();
// Convert all known scenes to a scene map.
var dump = new HashMap<Integer, SceneInfo>();
GameData.getSceneDataMap()
.forEach(
(id, scene) ->
dump.put(id, new SceneInfo(scene.getScriptData(), scene.getSceneType())));
try {
// Create a file for the dump.
var file = new File("scenes.csv");
if (file.exists() && !file.delete()) throw new RuntimeException("Failed to delete file.");
if (!file.exists() && !file.createNewFile())
throw new RuntimeException("Failed to create file.");
// Write the dump to the file.
Files.writeString(file.toPath(), Dumpers.miniEncode(dump));
} catch (IOException ignored) {
throw new RuntimeException("Failed to write to file.");
}
}
/**
* Dumps all entities to a CSV file.
*
* @param locale The language to dump the entities in.
*/
static void dumpEntities(String locale) {
// Reload resources.
ResourceLoader.loadAll();
Language.loadTextMaps();
// Convert all known avatars to an avatar map.
var dump = new HashMap<Integer, EntityInfo>();
GameData.getMonsterDataMap()
.forEach(
(id, monster) -> {
var langHash = monster.getNameTextMapHash();
dump.put(
id,
new EntityInfo(
langHash == 0
? monster.getMonsterName()
: Language.getTextMapKey(langHash).get(locale),
monster.getMonsterName()));
});
try {
// Create a file for the dump.
var file = new File("entities.csv");
if (file.exists() && !file.delete()) throw new RuntimeException("Failed to delete file.");
if (!file.exists() && !file.createNewFile())
throw new RuntimeException("Failed to create file.");
// Write the dump to the file.
Files.writeString(file.toPath(), Dumpers.miniEncode(dump));
} catch (IOException ignored) {
throw new RuntimeException("Failed to write to file.");
}
}
/**
* Dumps all quests to a JSON file.
*
* @param locale The language to dump the quests in.
*/
static void dumpQuests(String locale) {
// Reload resources.
ResourceLoader.loadAll();
Language.loadTextMaps();
// Convert all known quests to a quest map.
var dump = new HashMap<Integer, QuestInfo>();
GameData.getQuestDataMap()
.forEach(
(id, quest) -> {
var langHash = quest.getDescTextMapHash();
dump.put(
id,
new QuestInfo(
langHash == 0
? "Unknown"
: Language.getTextMapKey(langHash).get(locale).replaceAll(",", "\\\\"),
quest.getMainId()));
});
// Convert all known main quests into a quest map.
var mainDump = new HashMap<Integer, MainQuestInfo>();
GameData.getMainQuestDataMap()
.forEach(
(id, mainQuest) -> {
var langHash = mainQuest.getTitleTextMapHash();
mainDump.put(
id,
new MainQuestInfo(
langHash == 0
? "Unknown"
: Language.getTextMapKey(langHash).get(locale).replaceAll(",", "\\\\")));
});
try {
// Create a file for the dump.
var file = new File("quests.csv");
if (file.exists() && !file.delete()) throw new RuntimeException("Failed to delete file.");
if (!file.exists() && !file.createNewFile())
throw new RuntimeException("Failed to create file.");
// Write the dump to the file.
Files.writeString(file.toPath(), Dumpers.miniEncode(dump, "id", "description", "mainId"));
} catch (IOException ignored) {
throw new RuntimeException("Failed to write to file.");
}
try {
// Create a file for the dump.
var file = new File("mainquests.csv");
if (file.exists() && !file.delete()) throw new RuntimeException("Failed to delete file.");
if (!file.exists() && !file.createNewFile())
throw new RuntimeException("Failed to create file.");
// Write the dump to the file.
Files.writeString(file.toPath(), Dumpers.miniEncode(mainDump, "id", "title"));
} catch (IOException ignored) {
throw new RuntimeException("Failed to write to file.");
}
}
/**
* Dumps all areas to a CSV file.
*
* @param locale The language to dump the areas in.
*/
static void dumpAreas(String locale) {
// Reload resources.
ResourceLoader.loadAll();
Language.loadTextMaps();
// Convert all known areas to an area map.
var dump = new HashMap<Integer, AreaInfo>();
GameData.getWorldAreaDataMap()
.forEach(
(id, area) -> {
var langHash = area.getTextMapHash();
dump.put(
area.getChildArea() == 0 ? area.getParentArea() : area.getChildArea(),
new AreaInfo(
area.getParentArea(),
langHash == 0 ? "Unknown" : Language.getTextMapKey(langHash).get(locale)));
});
try {
// Create a file for the dump.
var file = new File("areas.csv");
if (file.exists() && !file.delete()) throw new RuntimeException("Failed to delete file.");
if (!file.exists() && !file.createNewFile())
throw new RuntimeException("Failed to create file.");
// Write the dump to the file.
Files.writeString(file.toPath(), Dumpers.miniEncode(dump, "id", "parent", "name"));
} catch (IOException ignored) {
throw new RuntimeException("Failed to write to file.");
}
}
@AllArgsConstructor
class CommandInfo {
public List<String> name;
public String description;
public List<String> usage;
public List<String> permission;
public TargetRequirement target;
}
@AllArgsConstructor
class AvatarInfo {
public String name;
public Quality quality;
@Override
public String toString() {
return this.name + "," + this.quality;
}
}
@AllArgsConstructor
class ItemInfo {
public Integer id;
public String name;
public Quality quality;
public ItemType type;
public String icon;
@Override
public String toString() {
return this.name + "," + this.quality + "," + this.type + "," + this.icon;
}
}
@AllArgsConstructor
class SceneInfo {
public String identifier;
public SceneType type;
@Override
public String toString() {
return this.identifier + "," + this.type;
}
}
@AllArgsConstructor
class EntityInfo {
public String name;
public String internal;
@Override
public String toString() {
return this.name + "," + this.internal;
}
}
@AllArgsConstructor
class MainQuestInfo {
public String title;
@Override
public String toString() {
return this.title;
}
}
@AllArgsConstructor
class QuestInfo {
public String description;
public int mainQuest;
@Override
public String toString() {
return this.description + "," + this.mainQuest;
}
}
@AllArgsConstructor
class AreaInfo {
public int parent;
public String name;
@Override
public String toString() {
return this.parent + "," + this.name;
}
}
enum Quality {
LEGENDARY,
EPIC,
RARE,
UNCOMMON,
COMMON,
UNKNOWN;
/**
* Convert a rank level to a quality.
*
* @param rankLevel The rank level to convert.
* @return The quality.
*/
static Quality from(int rankLevel) {
return switch (rankLevel) {
case 0 -> UNKNOWN;
case 1 -> COMMON;
case 2 -> UNCOMMON;
case 3 -> RARE;
case 4 -> EPIC;
default -> LEGENDARY;
};
}
}
}

View File

@@ -1,10 +1,26 @@
package emu.grasscutter.tools;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import static emu.grasscutter.utils.FileUtils.getResourcePath;
import static emu.grasscutter.utils.lang.Language.getTextMapKey;
import emu.grasscutter.GameConstants;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.command.CommandMap;
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.ResourceLoader;
import emu.grasscutter.data.common.ItemUseData;
import emu.grasscutter.data.excels.HomeWorldBgmData;
import emu.grasscutter.data.excels.ItemData;
import emu.grasscutter.data.excels.achievement.AchievementData;
import emu.grasscutter.data.excels.avatar.AvatarData;
import emu.grasscutter.utils.FileUtils;
import emu.grasscutter.utils.JsonUtils;
import emu.grasscutter.utils.lang.Language;
import emu.grasscutter.utils.lang.Language.TextStrings;
import it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectRBTreeMap;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -15,28 +31,14 @@ import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import emu.grasscutter.GameConstants;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.command.CommandMap;
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.ResourceLoader;
import emu.grasscutter.data.excels.AchievementData;
import emu.grasscutter.data.excels.AvatarData;
import emu.grasscutter.data.excels.ItemData;
import emu.grasscutter.utils.Language;
import emu.grasscutter.utils.Language.TextStrings;
import it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectRBTreeMap;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.val;
import static emu.grasscutter.utils.FileUtils.getResourcePath;
import static emu.grasscutter.utils.Language.getTextMapKey;
public final class Tools {
/**
* This generates the GM handbooks with a message by default.
*
* @throws Exception If an error occurs while generating the handbooks.
*/
public static void createGmHandbooks() throws Exception {
@@ -45,15 +47,26 @@ public final class Tools {
/**
* Generates a GM handbook for each language.
*
* @param message Should a message be printed to the console?
* @throws Exception If an error occurs while generating the handbooks.
*/
public static void createGmHandbooks(boolean message) throws Exception {
// Check if the GM Handbook directory exists.
val handbookDir = new File("GM Handbook");
if (handbookDir.exists()) return;
val languages = Language.TextStrings.getLanguages();
ResourceLoader.loadAll();
val mainQuestTitles = new Int2IntRBTreeMap(GameData.getMainQuestDataMap().int2ObjectEntrySet().stream().collect(Collectors.toMap(e -> (int) e.getIntKey(), e -> (int) e.getValue().getTitleTextMapHash())));
// val questDescs = new Int2IntRBTreeMap(GameData.getQuestDataMap().int2ObjectEntrySet().stream().collect(Collectors.toMap(e -> (int) e.getIntKey(), e -> (int) e.getValue().getDescTextMapHash())));
val mainQuestTitles =
new Int2IntRBTreeMap(
GameData.getMainQuestDataMap().int2ObjectEntrySet().stream()
.collect(
Collectors.toMap(
e -> e.getIntKey(), e -> (int) e.getValue().getTitleTextMapHash())));
// val questDescs = new
// Int2IntRBTreeMap(GameData.getQuestDataMap().int2ObjectEntrySet().stream().collect(Collectors.toMap(e -> (int) e.getIntKey(), e -> (int) e.getValue().getDescTextMapHash())));
val avatarDataMap = new Int2ObjectRBTreeMap<>(GameData.getAvatarDataMap());
val itemDataMap = new Int2ObjectRBTreeMap<>(GameData.getItemDataMap());
@@ -65,77 +78,108 @@ public final class Tools {
Function<SortedMap<?, ?>, String> getPad = m -> "%" + m.lastKey().toString().length() + "s : ";
// Create builders and helper functions
val handbookBuilders = IntStream.range(0, TextStrings.NUM_LANGUAGES).mapToObj(i -> new StringBuilder()).toList();
var h = new Object() {
void newLine(String line) {
handbookBuilders.forEach(b -> b.append(line + "\n"));
}
void newSection(String title) {
newLine("\n\n// " + title);
}
void newTranslatedLine(String template, TextStrings... textstrings) {
for (int i = 0; i < TextStrings.NUM_LANGUAGES; i++) {
String s = template;
for (int j = 0; j < textstrings.length; j++)
s = s.replace("{"+j+"}", textstrings[j].strings[i]);
handbookBuilders.get(i).append(s + "\n");
}
}
void newTranslatedLine(String template, long... hashes) {
newTranslatedLine(template, LongStream.of(hashes).mapToObj(hash -> getTextMapKey(hash)).toArray(TextStrings[]::new));
}
};
val handbookBuilders =
IntStream.range(0, TextStrings.NUM_LANGUAGES).mapToObj(i -> new StringBuilder()).toList();
var h =
new Object() {
void newLine(String line) {
handbookBuilders.forEach(b -> b.append(line + "\n"));
}
void newSection(String title) {
newLine("\n\n// " + title);
}
void newTranslatedLine(String template, TextStrings... textstrings) {
for (int i = 0; i < TextStrings.NUM_LANGUAGES; i++) {
String s = template;
for (int j = 0; j < textstrings.length; j++)
try {
s = s.replace("{" + j + "}", textstrings[j].strings[i]);
} catch (NullPointerException ignored) {
// TextMap cache is outdated.
j--; // Retry the action.
Language.loadTextMaps(true);
}
handbookBuilders.get(i).append(s + "\n");
}
}
void newTranslatedLine(String template, long... hashes) {
newTranslatedLine(
template,
LongStream.of(hashes)
.mapToObj(hash -> getTextMapKey(hash))
.toArray(TextStrings[]::new));
}
};
// Preamble
h.newLine("// Grasscutter " + GameConstants.VERSION + " GM Handbook");
h.newLine("// Created " + DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now()));
h.newLine(
"// Created "
+ DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now()));
// Commands
h.newSection("Commands");
final List<CommandHandler> cmdList = CommandMap.getInstance().getHandlersAsList();
final String padCmdLabel = "%" + cmdList.stream().map(CommandHandler::getLabel).map(String::length).max(Integer::compare).get().toString() + "s : ";
final String padCmdLabel =
"%"
+ cmdList.stream()
.map(CommandHandler::getLabel)
.map(String::length)
.max(Integer::compare)
.get()
+ "s : ";
for (CommandHandler cmd : cmdList) {
final String label = padCmdLabel.formatted(cmd.getLabel());
final String descKey = cmd.getDescriptionKey();
for (int i = 0; i < TextStrings.NUM_LANGUAGES; i++) {
String desc = languages.get(i).get(descKey).replace("\n", "\n\t\t\t\t").replace("\t", " ");
String desc =
languages.get(i).get(descKey).replace("\n", "\n\t\t\t\t").replace("\t", " ");
handbookBuilders.get(i).append(label + desc + "\n");
}
}
// Avatars
h.newSection("Avatars");
val avatarPre = getPad.apply(avatarDataMap);
avatarDataMap.forEach((id, data) -> h.newTranslatedLine(avatarPre.formatted(id) + "{0}", data.getNameTextMapHash()));
avatarDataMap.forEach(
(id, data) ->
h.newTranslatedLine(avatarPre.formatted(id) + "{0}", data.getNameTextMapHash()));
// Items
h.newSection("Items");
val itemPre = getPad.apply(itemDataMap);
itemDataMap.forEach((id, data) -> {
val name = getTextMapKey(data.getNameTextMapHash());
switch (data.getMaterialType()) {
case MATERIAL_BGM:
val bgmName = Optional.ofNullable(data.getItemUse())
.map(u -> u.get(0))
.map(u -> u.getUseParam())
.filter(u -> u.length > 0)
.map(u -> Integer.parseInt(u[0]))
.map(bgmId -> GameData.getHomeWorldBgmDataMap().get(bgmId))
.map(bgm -> bgm.getBgmNameTextMapHash())
.map(hash -> getTextMapKey(hash));
if (bgmName.isPresent()) {
h.newTranslatedLine(itemPre.formatted(id) + "{0} - {1}", name, bgmName.get());
return;
} // Fall-through
default:
h.newTranslatedLine(itemPre.formatted(id) + "{0}", name);
return;
}
});
itemDataMap.forEach(
(id, data) -> {
val name = getTextMapKey(data.getNameTextMapHash());
switch (data.getMaterialType()) {
case MATERIAL_BGM:
val bgmName =
Optional.ofNullable(data.getItemUse())
.map(u -> u.get(0))
.map(ItemUseData::getUseParam)
.filter(u -> u.length > 0)
.map(u -> Integer.parseInt(u[0]))
.map(bgmId -> GameData.getHomeWorldBgmDataMap().get((int) bgmId))
.map(HomeWorldBgmData::getBgmNameTextMapHash)
.map(Language::getTextMapKey);
if (bgmName.isPresent()) {
h.newTranslatedLine(itemPre.formatted(id) + "{0} - {1}", name, bgmName.get());
return;
} // Fall-through
default:
h.newTranslatedLine(itemPre.formatted(id) + "{0}", name);
return;
}
});
// Monsters
h.newSection("Monsters");
val monsterPre = getPad.apply(monsterDataMap);
monsterDataMap.forEach((id, data) -> h.newTranslatedLine(
monsterPre.formatted(id) + data.getMonsterName() + " - {0}",
data.getNameTextMapHash()));
monsterDataMap.forEach(
(id, data) ->
h.newTranslatedLine(
monsterPre.formatted(id) + data.getMonsterName() + " - {0}",
data.getNameTextMapHash()));
// Scenes - no translations
h.newSection("Scenes");
val padSceneId = getPad.apply(sceneDataMap);
@@ -143,25 +187,35 @@ public final class Tools {
// Quests
h.newSection("Quests");
val padQuestId = getPad.apply(questDataMap);
questDataMap.forEach((id, data) -> h.newTranslatedLine(
padQuestId.formatted(id) + "{0} - {1}",
mainQuestTitles.get(data.getMainId()),
data.getDescTextMapHash()));
questDataMap.forEach(
(id, data) ->
h.newTranslatedLine(
padQuestId.formatted(id) + "{0} - {1}",
mainQuestTitles.get(data.getMainId()),
data.getDescTextMapHash()));
// Achievements
h.newSection("Achievements");
val padAchievementId = getPad.apply(achievementDataMap);
achievementDataMap.values().stream()
.filter(AchievementData::isUsed)
.forEach(data -> {
h.newTranslatedLine(padAchievementId.formatted(data.getId()) + "{0} - {1}", data.getTitleTextMapHash(), data.getDescTextMapHash());
});
.filter(AchievementData::isUsed)
.forEach(
data -> {
h.newTranslatedLine(
padAchievementId.formatted(data.getId()) + "{0} - {1}",
data.getTitleTextMapHash(),
data.getDescTextMapHash());
});
// Write txt files
for (int i = 0; i < TextStrings.NUM_LANGUAGES; i++) {
File GMHandbookOutputpath=new File("./GM Handbook");
File GMHandbookOutputpath = new File("./GM Handbook");
GMHandbookOutputpath.mkdir();
final String fileName = "./GM Handbook/GM Handbook - %s.txt".formatted(TextStrings.ARR_LANGUAGES[i]);
try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8), false)) {
final String fileName =
"./GM Handbook/GM Handbook - %s.txt".formatted(TextStrings.ARR_LANGUAGES[i]);
try (PrintWriter writer =
new PrintWriter(
new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8),
false)) {
writer.write(handbookBuilders.get(i).toString());
}
}
@@ -171,83 +225,99 @@ public final class Tools {
public static List<String> createGachaMappingJsons() {
final int NUM_LANGUAGES = Language.TextStrings.NUM_LANGUAGES;
final Language.TextStrings CHARACTER = Language.getTextMapKey(4233146695L); // "Character" in EN
final Language.TextStrings WEAPON = Language.getTextMapKey(4231343903L); // "Weapon" in EN
final Language.TextStrings STANDARD_WISH = Language.getTextMapKey(332935371L); // "Standard Wish" in EN
final Language.TextStrings CHARACTER_EVENT_WISH = Language.getTextMapKey(2272170627L); // "Character Event Wish" in EN
final Language.TextStrings CHARACTER_EVENT_WISH_2 = Language.getTextMapKey(3352513147L); // "Character Event Wish-2" in EN
final Language.TextStrings WEAPON_EVENT_WISH = Language.getTextMapKey(2864268523L); // "Weapon Event Wish" in EN
final Language.TextStrings CHARACTER = Language.getTextMapKey(4233146695L); // "Character" in EN
final Language.TextStrings WEAPON = Language.getTextMapKey(4231343903L); // "Weapon" in EN
final Language.TextStrings STANDARD_WISH =
Language.getTextMapKey(332935371L); // "Standard Wish" in EN
final Language.TextStrings CHARACTER_EVENT_WISH =
Language.getTextMapKey(2272170627L); // "Character Event Wish" in EN
final Language.TextStrings CHARACTER_EVENT_WISH_2 =
Language.getTextMapKey(3352513147L); // "Character Event Wish-2" in EN
final Language.TextStrings WEAPON_EVENT_WISH =
Language.getTextMapKey(2864268523L); // "Weapon Event Wish" in EN
final List<StringBuilder> sbs = new ArrayList<>(NUM_LANGUAGES);
for (int langIdx = 0; langIdx < NUM_LANGUAGES; langIdx++)
sbs.add(new StringBuilder("{\n")); // Web requests should never need Windows line endings
sbs.add(new StringBuilder("{\n")); // Web requests should never need Windows line endings
// Avatars
GameData.getAvatarDataMap().keySet().intStream().sorted().forEach(id -> {
AvatarData data = GameData.getAvatarDataMap().get(id);
int avatarID = data.getId();
if (avatarID >= 11000000) { // skip test avatar
return;
}
String color = switch (data.getQualityType()) {
case "QUALITY_PURPLE" -> "purple";
case "QUALITY_ORANGE" -> "yellow";
case "QUALITY_BLUE" -> "blue";
default -> "";
};
Language.TextStrings avatarName = Language.getTextMapKey(data.getNameTextMapHash());
for (int langIdx = 0; langIdx < NUM_LANGUAGES; langIdx++) {
sbs.get(langIdx)
.append("\t\"")
.append(avatarID % 1000 + 1000)
.append("\": [\"")
.append(avatarName.get(langIdx))
.append(" (")
.append(CHARACTER.get(langIdx))
.append(")\", \"")
.append(color)
.append("\"],\n");
}
});
GameData.getAvatarDataMap()
.keySet()
.intStream()
.sorted()
.forEach(
id -> {
AvatarData data = GameData.getAvatarDataMap().get(id);
int avatarID = data.getId();
if (avatarID >= 11000000) { // skip test avatar
return;
}
String color =
switch (data.getQualityType()) {
case "QUALITY_PURPLE" -> "purple";
case "QUALITY_ORANGE" -> "yellow";
case "QUALITY_BLUE" -> "blue";
default -> "";
};
Language.TextStrings avatarName = Language.getTextMapKey(data.getNameTextMapHash());
for (int langIdx = 0; langIdx < NUM_LANGUAGES; langIdx++) {
sbs.get(langIdx)
.append("\t\"")
.append(avatarID % 1000 + 1000)
.append("\": [\"")
.append(avatarName.get(langIdx))
.append(" (")
.append(CHARACTER.get(langIdx))
.append(")\", \"")
.append(color)
.append("\"],\n");
}
});
// Weapons
GameData.getItemDataMap().keySet().intStream().sorted().forEach(id -> {
ItemData data = GameData.getItemDataMap().get(id);
if (data.getId() <= 11101 || data.getId() >= 20000) {
return; //skip non weapon items
}
String color = switch (data.getRankLevel()) {
case 3 -> "blue";
case 4 -> "purple";
case 5 -> "yellow";
default -> null;
};
if (color == null) return; // skip unnecessary entries
Language.TextStrings weaponName = Language.getTextMapKey(data.getNameTextMapHash());
for (int langIdx = 0; langIdx < NUM_LANGUAGES; langIdx++) {
sbs.get(langIdx)
.append("\t\"")
.append(data.getId())
.append("\": [\"")
.append(weaponName.get(langIdx).replaceAll("\"", "\\\\\""))
.append(" (")
.append(WEAPON.get(langIdx))
.append(")\", \"")
.append(color)
.append("\"],\n");
}
});
GameData.getItemDataMap()
.keySet()
.intStream()
.sorted()
.forEach(
id -> {
ItemData data = GameData.getItemDataMap().get(id);
if (data.getId() <= 11101 || data.getId() >= 20000) {
return; // skip non weapon items
}
String color =
switch (data.getRankLevel()) {
case 3 -> "blue";
case 4 -> "purple";
case 5 -> "yellow";
default -> null;
};
if (color == null) return; // skip unnecessary entries
Language.TextStrings weaponName = Language.getTextMapKey(data.getNameTextMapHash());
for (int langIdx = 0; langIdx < NUM_LANGUAGES; langIdx++) {
sbs.get(langIdx)
.append("\t\"")
.append(data.getId())
.append("\": [\"")
.append(weaponName.get(langIdx).replaceAll("\"", "\\\\\""))
.append(" (")
.append(WEAPON.get(langIdx))
.append(")\", \"")
.append(color)
.append("\"],\n");
}
});
for (int langIdx = 0; langIdx < NUM_LANGUAGES; langIdx++) {
sbs.get(langIdx)
.append("\t\"200\": \"")
.append(STANDARD_WISH.get(langIdx))
.append("\",\n\t\"301\": \"")
.append(CHARACTER_EVENT_WISH.get(langIdx))
.append("\",\n\t\"400\": \"")
.append(CHARACTER_EVENT_WISH_2.get(langIdx))
.append("\",\n\t\"302\": \"")
.append(WEAPON_EVENT_WISH.get(langIdx))
.append("\"\n}");
.append("\t\"200\": \"")
.append(STANDARD_WISH.get(langIdx))
.append("\",\n\t\"301\": \"")
.append(CHARACTER_EVENT_WISH.get(langIdx))
.append("\",\n\t\"400\": \"")
.append(CHARACTER_EVENT_WISH_2.get(langIdx))
.append("\",\n\t\"302\": \"")
.append(WEAPON_EVENT_WISH.get(langIdx))
.append("\"\n}");
}
return sbs.stream().map(StringBuilder::toString).toList();
}
@@ -258,13 +328,17 @@ public final class Tools {
var usedLocales = new HashSet<String>();
StringBuilder sb = new StringBuilder("mappings = {\n");
for (int i = 0; i < Language.TextStrings.NUM_LANGUAGES; i++) {
String locale = Language.TextStrings.ARR_GC_LANGUAGES[i].toLowerCase(); // TODO: change the templates to not use lowercased locale codes
if (usedLocales.add(locale)) { // Some locales fallback to en-us, we don't want to redefine en-us with vietnamese strings
String locale =
Language.TextStrings.ARR_GC_LANGUAGES[i]
.toLowerCase(); // TODO: change the templates to not use lowercased locale codes
if (usedLocales.add(
locale)) { // Some locales fallback to en-us, we don't want to redefine en-us with
// vietnamese strings
sb.append("\t\"%s\": ".formatted(locale));
sb.append(jsons.get(i).replace("\n", "\n\t") + ",\n");
}
}
sb.setLength(sb.length() - 2); // Delete trailing ",\n"
sb.setLength(sb.length() - 2); // Delete trailing ",\n"
sb.append("\n}");
Files.createDirectories(location.getParent());
@@ -275,9 +349,16 @@ public final class Tools {
public static List<String> getAvailableLanguage() {
List<String> availableLangList = new ArrayList<>();
try {
Files.newDirectoryStream(getResourcePath("TextMap"), "TextMap*.json").forEach(path -> {
availableLangList.add(path.getFileName().toString().replace("TextMap", "").replace(".json", "").toLowerCase());
});
Files.newDirectoryStream(getResourcePath("TextMap"), "TextMap*.json")
.forEach(
path -> {
availableLangList.add(
path.getFileName()
.toString()
.replace("TextMap", "")
.replace(".json", "")
.toLowerCase());
});
} catch (IOException e) {
Grasscutter.getLogger().error("Failed to get available languages:", e);
}
@@ -293,12 +374,14 @@ public final class Tools {
return availableLangList.get(0).toUpperCase();
}
StringBuilder stagedMessage = new StringBuilder();
stagedMessage.append("The following languages mappings are available, please select one: [default: EN] \n");
stagedMessage.append(
"The following languages mappings are available, please select one: [default: EN] \n");
StringBuilder groupedLangList = new StringBuilder(">\t"); String input;
StringBuilder groupedLangList = new StringBuilder(">\t");
String input;
int groupedLangCount = 0;
for (String availableLanguage: availableLangList) {
for (String availableLanguage : availableLangList) {
groupedLangCount++;
groupedLangList.append(availableLanguage).append("\t");
@@ -320,6 +403,67 @@ public final class Tools {
return input.toUpperCase();
}
Grasscutter.getLogger().info("Invalid option. Will use EN (English) as fallback."); return "EN";
Grasscutter.getLogger().info("Invalid option. Will use EN (English) as fallback.");
return "EN";
}
/**
* Parses the 'resources.info' file found in resources.
*
* @return A {@link ResourceInfo} object containing the parsed data.
*/
public static ResourceInfo resourcesInfo() {
var file = FileUtils.getResourcePath("resources.info");
try (var reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
var resourceInfo = ResourceInfo.builder();
reader
.lines()
.forEach(
line -> {
var split = line.split(":");
if (split.length != 2) return;
var key = split[0].trim();
var value = split[1].trim();
// Apply the value to the correct field.
switch (key) {
case "repo" -> resourceInfo.repository(value);
case "ver" -> resourceInfo.version(value);
case "patches" -> resourceInfo.patches(value);
case "scripts" -> resourceInfo.scripts(ScriptsType.valueOf(value.toUpperCase()));
case "hasserverres" -> resourceInfo.hasServerResources(
Boolean.parseBoolean(value));
case "hasscenescriptdata" -> resourceInfo.hasSceneScriptData(
Boolean.parseBoolean(value));
}
});
return resourceInfo.build();
} catch (Exception ignored) {
return new ResourceInfo(null, null, null, ScriptsType.UNKNOWN, false, false);
}
}
@AllArgsConstructor
@Builder
public static class ResourceInfo {
private final String repository;
private final String version;
private final String patches;
private final ScriptsType scripts;
private final boolean hasServerResources;
private final boolean hasSceneScriptData;
@Override
public String toString() {
return JsonUtils.encode(this);
}
}
public enum ScriptsType {
OFFICIAL,
DUMPED,
UNKNOWN
}
}