Skip to content

Commit db73336

Browse files
committed
ver 0.0.4.2
1 parent 7b96cf0 commit db73336

37 files changed

Lines changed: 1502 additions & 726 deletions

src/main/java/priv/samera2022/module/DropTarget.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void drop(DropTargetDropEvent dtde) {
5353
for (File file : list) {
5454
switch (code) {
5555
case INPUT_ANALYZE:
56-
dsd.insertString(dsd.getLength(), file.getAbsolutePath(), FontStyle.plainStyle);
56+
dsd.insertString(dsd.getLength(), file.getAbsolutePath().replace("\"","/"), FontStyle.plainStyle);
5757
break;
5858
case FILE_CONTENT_ANALYZE:
5959
// sb.append(file.getAbsolutePath() + "\r\n");

src/main/java/priv/samera2022/module/UpdateInfo.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ public class UpdateInfo {
107107
"## [Usage]\n" +
108108
"# config指令\n" +
109109
" - \"config reload\" 执行该指令后将会重载config.cfg。";
110+
private static final String version_0_0_4_2 =
111+
" - [Released] - [0.0.4.2] - 2024-02-20 13:22\n" +
112+
"## [Changed]\n" +
113+
" - 完善了mainFrame.ExceptionHandler()方法,但并未进行相关测试。\n" +
114+
" - web包更名为gpt包,原web包下HttpURLConnection.java更名为Connection.java\n" +
115+
"## [Added]\n" +
116+
// " - 增加config/ConfigHandler.java对于过时配置文件的逻辑,选择动态添加新的键值对并将值设为Updated。\n" + //没啥用,因为在read的时候还是要设置属性的
117+
" - 增加analyze指令。\n" +
118+
" - 增加downloadModpack指令。\n" +
119+
"## [Usage]\n" +
120+
"# downloadModpack指令\n" +
121+
" - \"downloadModpack key <zipPath> <targetPath>\" 执行该指令后将会从zipPath所指向的文件进行模组补全下载,保存至targetPath。同时将配置等文件夹解压至targetPath中。" +
122+
"请注意:key下载需要提供curseforge_api_key,即cf_api_key。配置在config.cfg中填入。\n"+
123+
" - \"downloadModpack browser <zipPath>\" 执行该指令后将会从zipPath所指向的文件进行浏览器模组补全下载,该操作不需要提供curseforge_api_key。目前没有设置配置等文件的解压," +
124+
"因为涉及不同浏览器的保存地址,所以暂时不做相关处理。\n" +
125+
"# analyze指令\n" +
126+
" - \"analyze <filePath>\" 执行该指令后将会读取报错日志,并从已设置的报错内容特征进行检索。目前仅支持一小部分报错分析,更多分析请等待后续版本。\n" +
127+
"## [To-do]\n" +
128+
" - 考虑添加依赖缺失报错的模组下载处理逻辑。\n" +
129+
" - 或许应当考虑将关于mc的指令全部放在一级指令mc下面?\n" +
130+
"## [Warns]\n" +
131+
" - Logger的线程提示有问题,需要解决!";
110132
private static final String warns =
111133
"警示: " +
112134
"-后续指令注册时,应在Info中向COMMANDS加入该指令以进行模糊匹配!" +
@@ -116,6 +138,6 @@ public class UpdateInfo {
116138
"-在各个Listener中,你其实有很多变量都是可以调用的。很多时候如果这个方法用这个变量不行,可以试试用另外一个方法调用另外一个变量来解决。" +
117139
"比如说在模糊判断输出Couldn't find that command! Guess...的时候,用message会导致时间和内容吞并,但是用dsdFileContent附加时间就可以解决。";
118140
public static void main(String[] args) {
119-
System.out.println(version_0_0_4);
141+
System.out.println(version_0_0_4_2);
120142
}
121143
}

src/main/java/priv/samera2022/module/commands/registry/CommandHeads.java

Lines changed: 101 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
import priv.samera2022.module.*;
44
import priv.samera2022.module.annotation.Command;
5+
import priv.samera2022.module.config.ConfigHandler;
6+
import priv.samera2022.module.file.FileHandler;
7+
import priv.samera2022.module.gadgets.gpt.Connection;
8+
import priv.samera2022.module.gadgets.gpt.response.Response;
9+
import priv.samera2022.module.gadgets.mc.modpack.ModpackHandler;
10+
import priv.samera2022.module.gadgets.mc.modpack.download.Concept;
11+
import priv.samera2022.module.gadgets.mc.modpack.download.key.Download;
512
import priv.samera2022.module.gadgets.quiz.Quiz;
6-
import priv.samera2022.module.gadgets.web.HttpURLConnection;
7-
import priv.samera2022.module.gadgets.web.response.Response;
813

914
import javax.swing.*;
1015
import javax.swing.text.BadLocationException;
@@ -22,6 +27,7 @@
2227
import java.text.SimpleDateFormat;
2328
import java.util.ArrayList;
2429
import java.util.Arrays;
30+
import java.util.HashMap;
2531
import java.util.Scanner;
2632

2733
import static priv.samera2022.module.mainFrame.dsdFileContent;
@@ -222,7 +228,7 @@ public static void openai(ArrayList<String> commands) {
222228
formatter(true, new Mixture<>("请等待,正在请求中......", FontStyle.blackStyle));
223229
String content = commands.get(1);
224230
clearInput();
225-
Response response = HttpURLConnection.question(content);
231+
Response response = Connection.question(content);
226232
formatter(true, new Mixture<>("CompletionTokens: " + response.getUsage().getCompletionTokens()
227233
+ " " + "Model: " + response.getModel() + " " + "Content: \n" +
228234
response.getChoices().getMessage().getContent(), FontStyle.blackStyle));
@@ -243,51 +249,101 @@ public static void config(ArrayList<String> commands) {
243249
}
244250

245251
@Command
246-
public static void analyze(ArrayList<String> commands) throws IOException {
247-
String path = commands.get(1);
248-
String content = FileHandler.read(path);
249-
File file = new File(path);
250-
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
251-
byte[] buf = new byte[1024];
252-
int len;
253-
ByteBuffer bb = ByteBuffer.allocate((int) file.length());
254-
// 向内存中写数据
255-
while ((len = fis.read(buf)) > -1) {
256-
bb.put(buf, 0, len);
257-
}
258-
fis.close();
259-
// 在控件中显示文件内容,注意文件编码问题
260-
InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(bb.array()), StandardCharsets.UTF_8);
261-
Scanner sc = new Scanner(isr);
262-
int line = 0;
263-
while (sc.hasNext()) {
264-
line++;
265-
String t = sc.nextLine();
266-
if (t.indexOf("Thread: ") == 0) {
267-
switch (t) {
268-
case "Thread: Render Thread":
269-
formatter(true, new Mixture<>("渲染进程(Render Thread)报错!\n", FontStyle.darkRedStyle));
270-
if (content.contains("Optifine"))
271-
formatter(true, new Mixture<>("Optifine已安装!\n", FontStyle.darkRedStyle));
272-
else
273-
formatter(true, new Mixture<>("Optifine未安装。\n", FontStyle.blackStyle));
274-
break;
275-
case "Thread: Client thread":
276-
formatter(true, new Mixture<>("客户端进程(Client Thread)报错!\n", FontStyle.blackStyle));
277-
break;
278-
}
252+
public static void analyze(ArrayList<String> commands) {
253+
try {
254+
String path = commands.get(1);
255+
String content = FileHandler.read(path);
256+
File file = new File(path);
257+
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
258+
byte[] buf = new byte[1024];
259+
int len;
260+
ByteBuffer bb = ByteBuffer.allocate((int) file.length());
261+
// 向内存中写数据
262+
while ((len = fis.read(buf)) > -1) {
263+
bb.put(buf, 0, len);
279264
}
280-
if (t.contains("LE") || t.contains("LCHIJE")) {
281-
String[] arr = t.split("\\|");
282-
formatter(true, new Mixture<>("\n异常可能点:" +
283-
"\n模组注册名: " + arr[2] + "\n模组名称: " + arr[4], FontStyle.darkRedStyle),
284-
new Mixture<>("\n分析指示:可能该模组出现异常或存在模组互相冲突\n" +
285-
"报错切片如下:\n" + t + "\n" +
286-
"该异常位于第" + line + "行", FontStyle.blackStyle));
265+
fis.close();
266+
// 在控件中显示文件内容,注意文件编码问题
267+
InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(bb.array()), StandardCharsets.UTF_8);
268+
Scanner sc = new Scanner(isr);
269+
int line = 0;
270+
while (sc.hasNext()) {
271+
line++;
272+
String t = sc.nextLine();
273+
if (t.indexOf("Thread: ") == 0) {
274+
switch (t) {
275+
case "Thread: Render Thread":
276+
formatter(true, new Mixture<>("渲染进程(Render Thread)报错!\n", FontStyle.darkRedStyle));
277+
if (content.contains("Optifine"))
278+
formatter(true, new Mixture<>("Optifine已安装!\n", FontStyle.darkRedStyle));
279+
else
280+
formatter(true, new Mixture<>("Optifine未安装。\n", FontStyle.blackStyle));
281+
break;
282+
case "Thread: Client thread":
283+
formatter(true, new Mixture<>("客户端进程(Client Thread)报错!\n", FontStyle.blackStyle));
284+
break;
285+
}
286+
}
287+
if (t.contains("LE") || t.contains("LCHIJE")) {
288+
String[] arr = t.split("\\|");
289+
formatter(true, new Mixture<>("\n异常可能点:" +
290+
"\n模组注册名: " + arr[2] + "\n模组名称: " + arr[4], FontStyle.darkRedStyle),
291+
new Mixture<>("\n分析指示:可能该模组出现异常或存在模组互相冲突\n" +
292+
"报错切片如下:\n" + t + "\n" +
293+
"该异常位于第" + line + "行", FontStyle.blackStyle));
294+
} else if (t.contains("unable to find valid certification path to requested target")||t.contains("PKIX path building failed")) {
295+
formatter(true, new Mixture<>("\n异常可能点:Java出现异常。",FontStyle.darkRedStyle), new Mixture<>(
296+
"可能原因(仅供参考):这个问题的根本原因是你安装JDK时,Java\\jar 1.8.0_141\\lib\\ext\\里面缺少了一个安全凭证jssecacerts证书文件,通过运行下面类可以生成证书,将生成的证书放在Java\\jar 1.8.0_141\\lib\\ext\\这个目录下,重启编译器就可以解决。\n" +
297+
"可能的解决办法:" +
298+
"1. (实践可行)重新安装Java" +
299+
"2. 生成安全证书并放入jre相应路径下,参照网页https://blog.csdn.net/weixin_44519124/article/details/119909354",FontStyle.blackStyle));
300+
}
287301
}
302+
} catch (IOException e) {
303+
e.printStackTrace();
288304
}
289305
}
290306

307+
@Command
308+
public static void downloadModpacks(ArrayList<String> commands) {
309+
String rawSubCommand = commands.get(1);
310+
String subCommand = FuzzyMatcher.fuzzyMatch(rawSubCommand, Arrays.asList("key","browser"));
311+
if (ConfigHandler.CONFIG.getCf_api_key()!=null) {
312+
ModpackHandler.handle(commands.get(2),commands.get(3),subCommand);
313+
switch (subCommand) {
314+
case "key":
315+
//zip在前,目录在后
316+
File[] filesN = new File(commands.get(3)+"\\necessary\\").listFiles();
317+
File[] filesO = new File(commands.get(3)+"\\optional\\").listFiles();
318+
int optionalMods = filesO==null ? 0 : filesO.length;
319+
if (filesN != null && filesN.length > 0) {
320+
formatter(true,new Mixture<>("检验中,存在下载内容。",FontStyle.blackStyle));
321+
int mods = priv.samera2022.module.gadgets.mc.modpack.download.browser.Download.getNum(commands.get(2));
322+
if (filesN.length+optionalMods==mods) {
323+
formatter(true,new Mixture<>("模组全部下载完成,共"+mods+"个!",FontStyle.greenStyle));
324+
} else {
325+
formatter(true,new Mixture<>("模组下载未完成,应有"+mods+"个,实有"+(filesN.length+optionalMods)+"个!",FontStyle.greenStyle));
326+
//TODO 缺失文件输出本地检查以后再写
327+
}
328+
} else {
329+
formatter(true,new Mixture<>("不存在下载内容,下载失败!",FontStyle.darkRedStyle));
330+
}
331+
if (Download.ERROR_LIST.size()!=0) {
332+
formatter(true,new Mixture<>("下载未完成,失败文件列表如下所示:",FontStyle.darkRedStyle));
333+
for (HashMap<String,Object> map : Download.ERROR_LIST){
334+
formatter(true, new Mixture<>("\nmodId: "+map.get(Concept.PROJECT_ID)+"\nfileId: "+map.get(Concept.FILE_ID)+"\nrequired: "+map.get(Concept.REQUIRED)+"\n--------",FontStyle.blackStyle));
335+
}
336+
}
337+
break;
338+
case "browser":
339+
// priv.samera2022.module.gadgets.mc.modpack.download.browser.Download.downloadModpacks(commands.get(2));
340+
break;
341+
default:
342+
break;
343+
}
344+
} else priv.samera2022.module.gadgets.mc.modpack.download.browser.Download.downloadModpacks(commands.get(2));
345+
}
346+
291347
private static void clearInput() {
292348
try {
293349
dsdInput.remove(0, dsdInput.getLength());
@@ -311,8 +367,9 @@ private static void clearInput() {
311367
* 而这个数组在可变参数的函数中被套上了Mixture<String,Style>的泛型,就可以使用@SafeVarargs提示已经除去了堆污染和泛型错误。
312368
* 简单地来解释,就是 多个抽象元素(具体元素也可以,只不过cast了也一样)->调用可变参数函数->(在可变参数函数中)形成数组,并在这个时候对数组加上泛型cast->警告用@SafeVarargs消除
313369
*/
370+
314371
@SafeVarargs
315-
private static void formatter(boolean delete, Mixture<String, Style>... objects) {
372+
public static void formatter(boolean delete, Mixture<String, Style>... objects) {
316373
output(objects, delete);
317374
}
318375

src/main/java/priv/samera2022/module/config/Concept.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
public class Concept {
44
public static final String COMMAND_OUTPUT = "command_output";
5+
public static final String CF_API_KEY = "cf_api_key";
6+
public static final String CF_DOA = "cf_api";
7+
//curseforge download overwrite ask
58
}

src/main/java/priv/samera2022/module/config/Config.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
public class Config {
44
private boolean commandOutput;
5+
private String cf_api_key;
6+
private boolean cf_doa;
57
public Config(){ }
68

79
public void setCommandOutput(boolean commandOutput) { this.commandOutput = commandOutput; }
810
public boolean isCommandOutput() { return commandOutput; }
911

12+
public void setCf_api_key(String cf_api_key) { this.cf_api_key = cf_api_key; }
13+
public String getCf_api_key() { return cf_api_key; }
14+
15+
public void setCf_doa(boolean cf_doa) { this.cf_doa = cf_doa; }
16+
public boolean isCf_doa() { return cf_doa; }
17+
1018
}

src/main/java/priv/samera2022/module/config/ConfigHandler.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package priv.samera2022.module.config;
22

3-
import priv.samera2022.module.FileHandler;
3+
import priv.samera2022.module.file.FileHandler;
44
import priv.samera2022.module.mainFrame;
55

66
import java.io.FileReader;
@@ -10,15 +10,26 @@
1010

1111
public class ConfigHandler {
1212
private static final String CONFIG_PATH = FileHandler.FOLDER_PATH+FileHandler.CONFIG_PATH+FileHandler.CONFIG_NAME;
13+
private static Properties DEFAULT = new Properties();
14+
private static Properties READ = new Properties();
1315
public static Config CONFIG = read();
1416

17+
18+
//当config发生更新时,需要更改以下语句:
1519
public static void write(){
1620
Config config = new Config();
21+
//1. 默认config注册
1722
config.setCommandOutput(true);
23+
config.setCf_api_key("null");
24+
config.setCf_doa(true);
1825

26+
//2. 默认pro设值
1927
Properties pro = new Properties();
2028
pro.setProperty(Concept.COMMAND_OUTPUT,String.valueOf(config.isCommandOutput()));
29+
pro.setProperty(Concept.CF_API_KEY,config.getCf_api_key());
30+
pro.setProperty(Concept.CF_DOA, config.getCf_api_key());
2131

32+
DEFAULT = pro;
2233
try {
2334
FileWriter f = new FileWriter(CONFIG_PATH);
2435
pro.store(f,null);
@@ -27,7 +38,6 @@ public static void write(){
2738
e.printStackTrace();
2839
mainFrame.ExceptionHandler(e);
2940
}
30-
3141
}
3242

3343
private static Config read(){
@@ -40,9 +50,42 @@ private static Config read(){
4050
e.printStackTrace();
4151
mainFrame.ExceptionHandler(e);
4252
}
53+
READ = pro;
54+
//3. 读取config
4355
config.setCommandOutput(Boolean.parseBoolean(pro.getProperty(Concept.COMMAND_OUTPUT)));
56+
57+
String in1 = pro.getProperty(Concept.CF_API_KEY);
58+
if (in1.equals("null")) in1 = null;
59+
config.setCf_api_key(in1);
60+
61+
config.setCf_doa(Boolean.parseBoolean(pro.getProperty(Concept.CF_DOA)));
4462
return config;
4563
}
4664

4765
public static void reload(){ CONFIG = read(); }
66+
67+
//这个方法其实没多大用,因为在read的时候就已经设置属性了
68+
@Deprecated
69+
public static void UpdateIfPossible(){
70+
if (READ.size() != DEFAULT.size()) {
71+
mainFrame.logger.warn("Config已过时!正在更新Config文件!");
72+
for (Object object : DEFAULT.keySet()){
73+
if (!READ.containsKey(object)){
74+
READ.setProperty((String) object,"Updated");
75+
}
76+
}
77+
writeProperties(READ);
78+
}
79+
}
80+
81+
private static void writeProperties(Properties pro){
82+
try {
83+
FileWriter f = new FileWriter(CONFIG_PATH);
84+
pro.store(f,null);
85+
FileHandler.deleteLine(CONFIG_PATH,1);
86+
} catch (IOException e) {
87+
e.printStackTrace();
88+
mainFrame.ExceptionHandler(e);
89+
}
90+
}
4891
}

0 commit comments

Comments
 (0)