Skip to content

Commit

Permalink
🐛 增加配置文件为空处理,防止因为配置文件为空而导致加载失败
Browse files Browse the repository at this point in the history
  • Loading branch information
LIlGG committed Dec 12, 2022
1 parent 5c161ca commit 2c5be6a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
9 changes: 6 additions & 3 deletions src/main/java/run/halo/live2d/Live2dInitProcessor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package run.halo.live2d;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -55,7 +54,7 @@ public Mono<Void> process(ITemplateContext context, IModel model,
IElementModelStructureHandler structureHandler) {
return live2dSetting.getConfig().map(config -> {
if (log.isDebugEnabled()) {
log.info("live2d config {}", config.toPrettyString());
log.debug("live2d config {}", config.toPrettyString());
}
final IModelFactory modelFactory = context.getModelFactory();
model.add(modelFactory.createText(live2dAutoloadScript(config)));
Expand All @@ -64,7 +63,11 @@ public Mono<Void> process(ITemplateContext context, IModel model,
}

private CharSequence live2dAutoloadScript(JsonNode config) {
String loadTime = this.live2dSetting.getValue("advanced", "loadTime").asText(LIVE2D_LOAD_TIME);
String loadTime = LIVE2D_LOAD_TIME;
JsonNode node = this.live2dSetting.getValue("advanced", "loadTime");
if (Objects.nonNull(node)) {
loadTime = node.asText(LIVE2D_LOAD_TIME);
}
String template = """
live2d.init("%1$s", %2$s)
""".formatted(LIVE2D_SOURCE_PATH, config.toPrettyString());
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/run/halo/live2d/Live2dSettingProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ public Live2dSettingProcess(SettingFetcher settingFetcher,

public void initConfigNode() {
this.configNode = new ObjectNode(this);
settingMap.forEach((group, jsonNode) -> {
JsonNode node = settingMap.get(group);
log.debug("live2d config -> {} group save settingMap json {}", group, node.toPrettyString());
this.settingMap.forEach((group, jsonNode) -> {
JsonNode node = this.settingMap.get(group);
if(log.isDebugEnabled()) {
log.debug("live2d config -> {} group save settingMap json {}", group, node.toPrettyString());
}
if (jsonNode instanceof ObjectNode) {
configNode.setAll((ObjectNode) node);
this.configNode.setAll((ObjectNode) node);
}
});
// 移除不必要的参数
Expand All @@ -51,18 +53,21 @@ public void initConfigNode() {
}

private void setThemeLive2dTipsPath() {
themeFetcher.getActiveThemeName().ifPresent(activeThemeName -> {
this.themeFetcher.getActiveThemeName().ifPresent(activeThemeName -> {
this.configNode.put("themeTipsPath", THEME_TIPS_PATH_TEMPLATE.formatted(activeThemeName));
});
}

@Override
public JsonNode getValue(String groupName, String key) {
return settingMap.get(groupName).get(key);
return this.settingMap.getOrDefault(groupName, new ObjectNode(this)).get(key);
}

@Override
public Optional<JsonNode> getConfig() {
if(log.isDebugEnabled()) {
log.debug("live2d config -> {}", configNode.toPrettyString());
}
return Optional.of(configNode);
}
}
26 changes: 14 additions & 12 deletions src/main/resources/static/js/live2d-autoload.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,18 +359,20 @@ function Live2d() {
message: {}
}
// selector
config["selectorTips"].forEach(item => {
let texts = item['messageTexts'].map(text => text.message);
let obj = {
selector: item['selector'],
text: texts
}
if (item['mouseAction'] === 'click') {
tips.click.push(obj);
} else {
tips.mouseover.push(obj);
}
})
if (!!config["selectorTips"]) {
config["selectorTips"].forEach(item => {
let texts = item['messageTexts'].map(text => text.message);
let obj = {
selector: item['selector'],
text: texts
}
if (item['mouseAction'] === 'click') {
tips.click.push(obj);
} else {
tips.mouseover.push(obj);
}
})
}
// message
tips.message.visibilitychange = config["backSiteTip"];
tips.message.copy = config["copyContentTip"];
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/static/js/live2d-autoload.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2c5be6a

Please sign in to comment.