Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复配置文件加载空内容导致的解析错误,增强错误处理和空值检查(#50, #37) #52

Merged
merged 2 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions app/src/main/java/fansirsqi/xposed/sesame/data/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,34 @@ public static synchronized Config load(String userId) {
// 如果配置文件存在,加载内容
if (configV2File.exists()) {
String json = Files.readFromFile(configV2File);
JsonUtil.copyMapper().readerForUpdating(INSTANCE).readValue(json);
String formatted = JsonUtil.formatJson(INSTANCE);
if (formatted != null && !formatted.equals(json)) {
Log.runtime(TAG, "格式化配置: " + userName);
Log.system(TAG, "格式化配置: " + userName);
Files.write2File(formatted, configV2File);
if (StringUtil.isEmpty(json)) {
Log.runtime(TAG, "配置文件内容为空,初始化新配置: " + userName);
unload();
Files.write2File(JsonUtil.formatJson(INSTANCE), configV2File);
} else {
JsonUtil.copyMapper().readerForUpdating(INSTANCE).readValue(json);
String formatted = JsonUtil.formatJson(INSTANCE);
if (formatted != null && !formatted.equals(json)) {
Log.runtime(TAG, "格式化配置: " + userName);
Log.system(TAG, "格式化配置: " + userName);
Files.write2File(formatted, configV2File);
}
}
} else {
// 如果配置文件不存在,复制默认配置或初始化
java.io.File defaultConfigV2File = Files.getDefaultConfigV2File();
if (defaultConfigV2File.exists()) {
String json = Files.readFromFile(defaultConfigV2File);
JsonUtil.copyMapper().readerForUpdating(INSTANCE).readValue(json);
Log.runtime(TAG, "复制新配置: " + userName);
Log.system(TAG, "复制新配置: " + userName);
Files.write2File(json, configV2File);
if (!StringUtil.isEmpty(json)) {
JsonUtil.copyMapper().readerForUpdating(INSTANCE).readValue(json);
Log.runtime(TAG, "复制新配置: " + userName);
Log.system(TAG, "复制新配置: " + userName);
Files.write2File(json, configV2File);
} else {
unload();
Log.runtime(TAG, "默认配置为空,初始化新配置: " + userName);
Files.write2File(JsonUtil.formatJson(INSTANCE), configV2File);
}
} else {
unload();
Log.runtime(TAG, "初始新配置: " + userName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public String getConfigValue() {
public void setConfigValue(String configValue) {
Integer newValue;
// 如果配置值为空,使用默认值
if (configValue == null) {
if (configValue == null || configValue.trim().isEmpty()) {
newValue = defaultValue;
} else {
try {
Expand Down Expand Up @@ -184,7 +184,7 @@ public String getType() {
*/
@Override
public void setConfigValue(String configValue) {
if (configValue == null) {
if (configValue == null || configValue.trim().isEmpty()) {
reset(); // 如果配置值为空,则重置字段
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,10 @@ private void giveProp(String targetUserId) {
do {
// 查询道具列表
JSONObject propListJo = new JSONObject(AntForestRpcCall.queryPropList(true));
if (!propListJo.has("resultCode")) {
Log.record("查询道具列表返回数据格式错误");
return;
}
// 检查查询结果是否成功
if (ResUtil.checkResCode(propListJo)) {
// 获取道具列表
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,23 @@ static void studentSignInRedEnvelope() {
// 定义签到时间范围
final LocalTime START_TIME = LocalTime.of(5, 0); // 5:00 AM
final LocalTime END_TIME = LocalTime.of(10, 0); // 10:00 AM

if (currentTime.isBefore(START_TIME)) {
Log.forest(" 青春特权🧧5点前不执行签到");
return;
}
String tag = currentTime.isBefore(END_TIME) ? "double" : "single";
studentTaskHandle(tag);

if (StatusUtil.canStudentTask()) {
String tag = currentTime.isBefore(END_TIME) ? "double" : "single";
studentTaskHandle(tag);
} else {
Log.record(" 青春特权🧧今日已完成签到");
}

} catch (Exception e) {
Log.runtime(AntForest.TAG, "studentSignInRedEnvelope err:");
Log.printStackTrace(AntForest.TAG, e);
Log.runtime(TAG, "studentSignInRedEnvelope错误:");
Log.printStackTrace(TAG, e);
Log.record(" 青春特权🧧执行异常:" + e.getMessage());
}
}

Expand All @@ -105,17 +113,42 @@ static void studentSignInRedEnvelope() {
static void studentTask(String tag) {
try {
String result = AntForestRpcCall.studentCheckin();
if (result == null || result.isEmpty()) {
Log.record(" 青春特权🧧签到失败:返回数据为空");
return;
}

JSONObject resultJson = new JSONObject(result);
String resultDesc = resultJson.getString("resultDesc");

if (resultDesc.contains("不匹配")) {
Log.forest(" 青春特权🧧 " + tag + ":" + resultDesc + "可能账户不符合条件");
} else {
Log.forest(" 青春特权🧧 " + tag + ":" + resultDesc);
// 检查返回码
String resultCode = resultJson.optString("resultCode", "");
if (!"SUCCESS".equals(resultCode)) {
String resultDesc = resultJson.optString("resultDesc", "未知错误");
if (resultDesc.contains("不匹配")) {
Log.forest(" 青春特权🧧 " + tag + ":" + resultDesc + "可能账户不符合条件");
} else {
Log.forest(" 青春特权🧧 " + tag + ":" + resultDesc);
}
return;
}

// 获取签到结果
String resultDesc = resultJson.optString("resultDesc", "签到成功");
Log.forest(" 青春特权🧧 " + tag + ":" + resultDesc);

// 如果签到成功,设置今日已签到标记
if ("SUCCESS".equals(resultCode)) {
StatusUtil.setStudentTaskToday();
}

} catch (JSONException e) {
Log.runtime(TAG, "studentTask JSON解析错误:");
Log.printStackTrace(TAG, e);
Log.record(" 青春特权🧧签到异常:" + e.getMessage());
} catch (Exception e) {
Log.runtime(AntForest.TAG, "studentTask err:");
Log.printStackTrace(AntForest.TAG, e);
Log.runtime(TAG, "studentTask其他错误:");
Log.printStackTrace(TAG, e);
Log.record(" 青春特权🧧签到异常:" + e.getMessage());
}
}

Expand All @@ -124,23 +157,58 @@ static void studentTask(String tag) {
*/
private static void studentTaskHandle(String tag) {
try {
if (!StatusUtil.canStudentTask()) return;
if (!StatusUtil.canStudentTask()) {
Log.record(" 青春特权🧧今日已达上限");
return;
}

String response = AntForestRpcCall.studentQqueryCheckInModel();
if (response == null || response.isEmpty()) {
Log.record(" 青春特权🧧查询失败:返回数据为空");
return;
}

JSONObject responseJson = new JSONObject(response);

// 检查返回码
if (responseJson.has("resultCode")) {
String resultCode = responseJson.getString("resultCode");
if (!"SUCCESS".equals(resultCode)) {
String resultDesc = responseJson.optString("resultDesc", "未知错误");
Log.record(" 青春特权🧧查询失败:" + resultDesc);
return;
}
}

// 安全获取 studentCheckInInfo
JSONObject studentCheckInInfo = responseJson.optJSONObject("studentCheckInInfo");
if (studentCheckInInfo == null) {
Log.record(" 青春特权🧧查询失败:无签到信息");
return;
}

// 安全获取 action
String action = studentCheckInInfo.optString("action", "");
if (action.isEmpty()) {
Log.record(" 青春特权🧧查询失败:无操作信息");
return;
}

String isTasked = AntForestRpcCall.studentQqueryCheckInModel();
JSONObject isTaskedJson = new JSONObject(isTasked);
String action = isTaskedJson.getJSONObject("studentCheckInInfo").getString("action");
if ("DO_TASK".equals(action)) {
Log.record(" 青春特权🧧今日已签到");
StatusUtil.setStudentTaskToday();
} else {
studentTask(tag);
}

} catch (JSONException e) {
Log.runtime(AntForest.TAG, "studentTaskHandle JSON err:");
Log.printStackTrace(AntForest.TAG, e);
Log.runtime(TAG, "studentTaskHandle JSON解析错误:");
Log.printStackTrace(TAG, e);
Log.record(" 青春特权🧧签到异常:" + e.getMessage());
} catch (Exception e) {
Log.runtime(AntForest.TAG, "studentTaskHandle err:");
Log.printStackTrace(AntForest.TAG, e);
Log.runtime(TAG, "studentTaskHandle其他错误:");
Log.printStackTrace(TAG, e);
Log.record(" 青春特权🧧签到异常:" + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,20 +261,26 @@ public String setModel(String modelCode, String fieldsValue) {
if (modelConfig != null) {
try {
ModelFields modelFields = modelConfig.getFields();
Map<String, ModelFieldShowDto> map = JsonUtil.parseObject(fieldsValue, new TypeReference<Map<String, ModelFieldShowDto>>() {
});
for (Map.Entry<String, ModelFieldShowDto> entry : map.entrySet()) {
ModelFieldShowDto newModelField = entry.getValue();
if (newModelField != null) {
ModelField<?> modelField = modelFields.get(entry.getKey());
if (modelField != null) {
modelField.setConfigValue(newModelField.getConfigValue());
Map<String, ModelFieldShowDto> map = JsonUtil.parseObject(fieldsValue,
new TypeReference<Map<String, ModelFieldShowDto>>() {});
if (map != null) {
for (Map.Entry<String, ModelFieldShowDto> entry : map.entrySet()) {
ModelFieldShowDto newModelField = entry.getValue();
if (newModelField != null) {
ModelField<?> modelField = modelFields.get(entry.getKey());
if (modelField != null) {
String configValue = newModelField.getConfigValue();
if (configValue == null || configValue.trim().isEmpty()) {
continue;
}
modelField.setConfigValue(configValue);
}
}
}
return "SUCCESS";
}
return "SUCCESS";
} catch (Exception e) {
Log.printStackTrace(e);
Log.printStackTrace("NewSettingsActivity", e);
}
}
return "FAILED";
Expand Down
80 changes: 63 additions & 17 deletions app/src/main/java/fansirsqi/xposed/sesame/util/StatisticsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static class TimeStatistics {
int collected, helped, watered;

public TimeStatistics() {
reset(0);
}

TimeStatistics(int i) {
Expand Down Expand Up @@ -116,35 +117,80 @@ public static String getText() {
public static synchronized StatisticsUtil load() {
java.io.File statisticsFile = Files.getStatisticsFile();
try {
if (statisticsFile.exists()) {

if (INSTANCE == null) {
return new StatisticsUtil();
}

if (statisticsFile.exists() && statisticsFile.length() > 0) {
String json = Files.readFromFile(statisticsFile);
JsonUtil.copyMapper().readerForUpdating(INSTANCE).readValue(json);
String formatted = JsonUtil.formatJson(INSTANCE);
if (formatted != null && !formatted.equals(json)) {
Log.runtime(TAG, "重新格式化 statistics.json");
Log.system(TAG, "重新格式化 statistics.json");
Files.write2File(formatted, statisticsFile);
if (!json.trim().isEmpty()) {
try {
JsonUtil.copyMapper().readerForUpdating(INSTANCE).readValue(json);

validateAndInitialize();

String formatted = JsonUtil.formatJson(INSTANCE);
if (formatted != null && !formatted.equals(json)) {
Log.runtime(TAG, "重新格式化 statistics.json");
Log.system(TAG, "重新格式化 statistics.json");
Files.write2File(formatted, statisticsFile);
}
} catch (Exception e) {
Log.printStackTrace(TAG, e);
resetToDefault();
}
} else {
resetToDefault();
}
} else {
JsonUtil.copyMapper().updateValue(INSTANCE, new StatisticsUtil());
Log.runtime(TAG, "初始化 statistics.json");
Log.system(TAG, "初始化 statistics.json");
Files.write2File(JsonUtil.formatJson(INSTANCE), statisticsFile);
resetToDefault();
}
} catch (Throwable t) {
Log.printStackTrace(TAG, t);
Log.runtime(TAG, "统计文件格式有误,已重置统计文件");
Log.system(TAG, "统计文件格式有误,已重置统计文件");
try {
JsonUtil.copyMapper().updateValue(INSTANCE, new StatisticsUtil());
Files.write2File(JsonUtil.formatJson(INSTANCE), Files.getStatisticsFile());
} catch (JsonMappingException e) {
Log.printStackTrace(TAG, e);
}
resetToDefault();
}
return INSTANCE;
}

/**
* 验证并初始化统计数据
* 确保年、月、日的统计数据都存在且有效
*/
private static void validateAndInitialize() {
LocalDate now = LocalDate.now();
if (INSTANCE.year == null) INSTANCE.year = new TimeStatistics(now.getYear());
if (INSTANCE.month == null) INSTANCE.month = new TimeStatistics(now.getMonthValue());
if (INSTANCE.day == null) INSTANCE.day = new TimeStatistics(now.getDayOfMonth());

updateDay(now);
}


/**
* 重置统计数据为默认值
* 使用当前日期初始化新的统计实例
*/
private static void resetToDefault() {
try {
StatisticsUtil newInstance = new StatisticsUtil();
LocalDate now = LocalDate.now();
newInstance.year = new TimeStatistics(now.getYear());
newInstance.month = new TimeStatistics(now.getMonthValue());
newInstance.day = new TimeStatistics(now.getDayOfMonth());

JsonUtil.copyMapper().updateValue(INSTANCE, newInstance);
Files.write2File(JsonUtil.formatJson(INSTANCE), Files.getStatisticsFile());

Log.runtime(TAG, "已重置为默认值");
Log.system(TAG, "已重置为默认值");
} catch (JsonMappingException e) {
Log.printStackTrace(TAG, e);
}
}

/**
* 卸载当前统计数据
*/
Expand Down