44import org .apache .logging .log4j .LogManager ;
55import org .apache .logging .log4j .Logger ;
66
7+ import java .awt .*;
8+ import java .io .*;
9+ import java .lang .reflect .Method ;
10+ import java .net .URL ;
11+ import java .net .URLClassLoader ;
12+ import java .nio .charset .StandardCharsets ;
13+ import java .nio .file .Files ;
714import java .util .Map ;
815
916@ IFMLLoadingPlugin .MCVersion ("1.12.2" )
1017@ IFMLLoadingPlugin .Name ("BalloonUpdatePlugin" )
1118@ IFMLLoadingPlugin .TransformerExclusions ("github.kasuminova" )
1219public class BalloonUpdate implements IFMLLoadingPlugin {
13- public static String mcLocation = null ;
14- static Logger logger = LogManager .getLogger ("BalloonUpdateMod" );
20+ // static 代码块, 超前调用外部 JAR 包
21+ static {
22+ Logger logger = LogManager .getLogger ("BalloonUpdateModLoader" );
23+ File directory = new File ("." );
24+ File [] files = directory .listFiles ();
25+ assert files != null ;
26+
27+ String path = null ;
28+
29+ //根据自定义文件名 BalloonUpdateFileName 内的文件名搜索文件, 如果文件不存在则不做任何修改
30+ File fileNameTXT = new File ("./BalloonUpdateFileName.txt" );
31+ if (fileNameTXT .exists () && fileNameTXT .isFile ()) {
32+ try {
33+ Reader reader = new InputStreamReader (Files .newInputStream (fileNameTXT .toPath ()), StandardCharsets .UTF_8 );
34+ StringBuilder sb = new StringBuilder ();
35+ int ch ;
36+ while ((ch = reader .read ()) != -1 ) {
37+ sb .append ((char ) ch );
38+ }
39+ reader .close ();
40+
41+ File target = new File ("./" + sb );
42+ if (target .exists () && target .isFile ()) {
43+ path = target .getAbsoluteFile ().toURI ().toString ();
44+ }
45+ } catch (Exception e ) {
46+ e .printStackTrace ();
47+ }
48+ }
49+
50+ //根据自定义文件名 OldBalloonUpdateFileName 内的文件名搜索文件, 如果存在则删除指定文件
51+ File oldFileNamesTXT = new File ("./OldBalloonUpdateFileName.txt" );
52+ if (oldFileNamesTXT .exists () && oldFileNamesTXT .isFile ()) {
53+ try {
54+ Reader reader = new InputStreamReader (Files .newInputStream (oldFileNamesTXT .toPath ()), StandardCharsets .UTF_8 );
55+ StringBuilder sb = new StringBuilder ();
56+ int ch ;
57+ while ((ch = reader .read ()) != -1 ) {
58+ sb .append ((char ) ch );
59+ }
60+ reader .close ();
61+
62+ //读取旧文件名 txt 文件后, 将每行分割成多个文件名然后循环搜索
63+ String [] oldFileNames = sb .toString ().split ("\n " );
64+ for (String oldFileName : oldFileNames ) {
65+ File target = new File ("./" + oldFileName );
66+ if (target .exists () && target .isFile ()) {
67+ if (target .delete ()) {
68+ logger .info ("Successfully Delete Old JarClient File: " + target .getName ());
69+ } else {
70+ logger .warn ("Failed Delete Old JarClient File: " + target .getName ());
71+ }
72+ }
73+ }
74+ } catch (Exception e ) {
75+ e .printStackTrace ();
76+ }
77+ }
78+
79+ //如果自定义 JarClient 文件未找到, 则搜索当前文件夹所有文件, 寻找对应的 JarClient
80+ if (path == null ) {
81+ for (File target : files ) {
82+ if (target .isFile () && target .getName ().contains ("JarClient" ) && target .getName ().endsWith (".jar" )) {
83+ path = target .getAbsoluteFile ().toURI ().toString ();
84+ break ;
85+ }
86+ }
87+ }
88+
89+ if (path == null ) {
90+ logger .error ("Cannot Find JarClient JAR File!" );
91+ } else {
92+ //开始载入外部 Jar
93+ URLClassLoader urlClassLoader = null ;
94+ Class <?> BalloonUpdateMain ;
95+ Class <?> SetupSwing ;
96+ try {
97+ logger .info ("Loading JAR: " + path );
98+ //通过URLClassLoader加载外部jar
99+ urlClassLoader = new URLClassLoader (new URL []{new URL (path )});
100+
101+ //获取外部jar里面的具体类对象
102+ logger .info ("Loading Class: " + "com.github.balloonupdate.BalloonUpdateMain" );
103+
104+ BalloonUpdateMain = urlClassLoader .loadClass ("com.github.balloonupdate.BalloonUpdateMain" );
105+
106+ if (Desktop .isDesktopSupported ()) {
107+ SetupSwing = urlClassLoader .loadClass ("com.github.kasuminova.GUI.SetupSwing" );
108+ Method init = SetupSwing .getMethod ("init" );
109+ init .invoke (null );
110+ }
111+
112+ Method main = BalloonUpdateMain .getMethod ("main" , String [].class );
113+
114+ logger .info ("Method Load Successfully, invoke Method main()..." );
115+
116+ main .invoke (null , (Object ) new String [0 ]);
117+ } catch (Exception e ) {
118+ e .printStackTrace ();
119+ } finally {
120+ //卸载关闭外部 JAR
121+ try {
122+ if (urlClassLoader != null ) urlClassLoader .close ();
123+ System .out .println ("Finished!" );
124+ } catch (IOException e ) {
125+ System .out .println ("Close JAR Failed:" + e .getMessage ());
126+ }
127+ }
128+ }
129+ }
130+
15131 @ Override
16132 public String [] getASMTransformerClass () {
17133 return new String [0 ];
@@ -24,12 +140,12 @@ public String getModContainerClass() {
24140
25141 @ Override
26142 public String getSetupClass () {
27- return "com.github.kasuminova.StartUpdate" ;
143+ return null ;
28144 }
29145
30146 @ Override
31147 public void injectData (Map <String , Object > data ) {
32- mcLocation = data . get ( "mcLocation" ). toString ();
148+
33149 }
34150
35151 @ Override
0 commit comments