22
33import java .io .File ;
44import java .io .IOException ;
5+ import java .io .OutputStream ;
56import java .io .PrintStream ;
67import java .net .InetAddress ;
78import java .net .UnknownHostException ;
1314import be .alexandre01 .dreamnetwork .api .addons .DreamExtension ;
1415import be .alexandre01 .dreamnetwork .api .config .WSSettings ;
1516import be .alexandre01 .dreamnetwork .api .console .Console ;
17+ import be .alexandre01 .dreamnetwork .api .service .IContainer ;
18+ import be .alexandre01 .dreamnetwork .api .utils .messages .Message ;
1619import be .alexandre01 .dreamnetwork .core .commands .CommandReader ;
1720import be .alexandre01 .dreamnetwork .api .service .IExecutor ;
1821import be .alexandre01 .dreamnetwork .api .service .IService ;
@@ -79,6 +82,7 @@ public class Main {
7982 @ Getter private static ProcessHistory processHistory ;
8083
8184 @ Getter private static SecretFile secretFile ;
85+ private static ReaderHistory readerHistory ;
8286
8387 public static void main (String [] args ) throws NoSuchFieldException , IllegalAccessException {
8488
@@ -134,7 +138,7 @@ public static void main(String[] args) throws NoSuchFieldException, IllegalAcces
134138 consoleReader = new ConsoleReader ();
135139 consoleReader .init ();
136140
137- ReaderHistory readerHistory = new ReaderHistory ();
141+ readerHistory = new ReaderHistory ();
138142 readerHistory .init ();
139143
140144 taskOperation = new TaskOperation ();
@@ -217,69 +221,71 @@ public static void main(String[] args) throws NoSuchFieldException, IllegalAcces
217221
218222 Runtime .getRuntime ().addShutdownHook (new Thread () {
219223 public void run () {
220- try {
221- disabling = true ;
222- if (instance != null ){
223- boolean isReady = false ;
224- for (IExecutor jvmExecutor : instance .getJvmContainer ().jvmExecutors ){
225- if (!jvmExecutor .getServices ().isEmpty ()){
226- ArrayList <Long > pIDs = new ArrayList <>();
227- for (IService service : new ArrayList <>(jvmExecutor .getServices ())){
228- if (!service .isConnected ()){
229- service .kill ();
230- }
231- if (service .getProcessID () != -1 ){
232- if (!service .isConnected ())
233- ProcessUtils .killProcess (service .getProcessID ());
234- pIDs .add (service .getProcessID ());
235- }
236- }
237- getProcessHistory ().getProcessHistoryIndex ().put ("TMPProcess" , Base64 .getEncoder ().encodeToString (ProcessHistory .convert (pIDs ).getBytes (StandardCharsets .UTF_8 )));
238- getProcessHistory ().getProcessHistoryIndex ().refreshFile ();
239- }
240-
241- }
242-
243-
244- if (consoleReader .sReader != null ){
245- for (String key : ReaderHistory .getLines ().keySet ()) {
246- /* History h = ConsoleReader.sReader.getHistory();
247- ArrayList<String> l = new ArrayList<>();
248- for (int j = 0; j < h.size()-1; j++) {
249- l.add(h.get(j));
250- }*/
251- ArrayList <String > l = new ArrayList <>(ReaderHistory .getLines ().get (key ));
252-
253- List <String > tail = l .subList (Math .max (l .size () - 15 , 0 ), l .size ());
254- readerHistory .getReaderHistoryIndex ().put (key , Base64 .getEncoder ().encodeToString (ReaderHistory .convert (tail ).getBytes (StandardCharsets .UTF_8 )));
255- readerHistory .getReaderHistoryIndex ().refreshFile ();
256-
257- }
258- }
259- isReady = true ;
260-
261-
262- Core .getInstance ().getAddonsManager ().getAddons ().values ().forEach (DreamExtension ::stop );
263- outputStream .println (Console .getFromLang ("main.shutdown" ));
264- try {
265- Thread .sleep (2000 );
266- } catch (InterruptedException e ) {
267- e .printStackTrace ();
268- }
269- }else {
270- outputStream .println (Console .getFromLang ("main.shutdown" ));
271- try {
272- Thread .sleep (2000 );
273- } catch (InterruptedException e ) {
274- e .printStackTrace ();
275- }
276- }
277-
278-
279- }catch (Exception e ){
280- System .out .println (e .getMessage ());
281- e .printStackTrace ();
282- }
224+ if (!disabling )
225+ Main .stop ();
226+ // try {
227+ // disabling = true;
228+ // if(instance != null){
229+ // boolean isReady = false;
230+ // for(IExecutor jvmExecutor : instance.getJvmContainer().jvmExecutors){
231+ // if(!jvmExecutor.getServices().isEmpty()){
232+ // ArrayList<Long> pIDs = new ArrayList<>();
233+ // for(IService service : new ArrayList<>(jvmExecutor.getServices())){
234+ // if(!service.isConnected()){
235+ // service.kill();
236+ // }
237+ // if(service.getProcessID() != -1){
238+ // if(!service.isConnected())
239+ // ProcessUtils.killProcess(service.getProcessID());
240+ // pIDs.add(service.getProcessID());
241+ // }
242+ // }
243+ // getProcessHistory().getProcessHistoryIndex().put("TMPProcess", Base64.getEncoder().encodeToString(ProcessHistory.convert(pIDs).getBytes(StandardCharsets.UTF_8)));
244+ // getProcessHistory().getProcessHistoryIndex().refreshFile();
245+ // }
246+ //
247+ // }
248+ //
249+ //
250+ // if(consoleReader.sReader != null){
251+ // for (String key : ReaderHistory.getLines().keySet()) {
252+ // /* History h = ConsoleReader.sReader.getHistory();
253+ // ArrayList<String> l = new ArrayList<>();
254+ // for (int j = 0; j < h.size()-1; j++) {
255+ // l.add(h.get(j));
256+ // }*/
257+ // ArrayList<String> l = new ArrayList<>(ReaderHistory.getLines().get(key));
258+ //
259+ // List<String> tail = l.subList(Math.max(l.size() - 15, 0), l.size());
260+ // readerHistory.getReaderHistoryIndex().put(key, Base64.getEncoder().encodeToString(ReaderHistory.convert(tail).getBytes(StandardCharsets.UTF_8)));
261+ // readerHistory.getReaderHistoryIndex().refreshFile();
262+ //
263+ // }
264+ // }
265+ // isReady = true;
266+ //
267+ //
268+ // Core.getInstance().getAddonsManager().getAddons().values().forEach(DreamExtension::stop);
269+ // outputStream.println(Console.getFromLang("main.shutdown"));
270+ // try {
271+ // Thread.sleep(2000);
272+ // } catch (InterruptedException e) {
273+ // e.printStackTrace();
274+ // }
275+ // }else {
276+ // outputStream.println(Console.getFromLang("main.shutdown"));
277+ // try {
278+ // Thread.sleep(2000);
279+ // } catch (InterruptedException e) {
280+ // e.printStackTrace();
281+ // }
282+ // }
283+ //
284+ //
285+ // }catch (Exception e){
286+ // System.out.println(e.getMessage());
287+ // e.printStackTrace();
288+ // }
283289
284290 }
285291 });
@@ -313,7 +319,6 @@ public void run() {
313319 }
314320
315321
316-
317322 loadClient ();
318323 }
319324
@@ -333,4 +338,82 @@ public static void loadClient(){
333338 Core .getInstance ().init ();
334339
335340 }
341+
342+ public static void stop (){
343+ try {
344+ disabling = true ;
345+ if (!Config .isWindows ()){
346+ String [] defSIGKILL = {"/bin/sh" ,"-c" ,"stty intr ^C </dev/tty" };
347+ try {
348+ Runtime .getRuntime ().exec (defSIGKILL );
349+ } catch (IOException e ) {
350+ throw new RuntimeException (e );
351+ }
352+ }
353+ if (instance != null ){
354+ boolean isReady = false ;
355+ for (IExecutor jvmExecutor : instance .getJvmContainer ().jvmExecutors ){
356+ if (!jvmExecutor .getServices ().isEmpty ()){
357+ ArrayList <Long > pIDs = new ArrayList <>();
358+ for (IService service : new ArrayList <>(jvmExecutor .getServices ())){
359+ if (!service .isConnected ()){
360+ service .kill ();
361+ }
362+ if (service .getProcessID () != -1 ){
363+ if (!service .isConnected ())
364+ ProcessUtils .killProcess (service .getProcessID ());
365+ pIDs .add (service .getProcessID ());
366+ }
367+ }
368+ getProcessHistory ().getProcessHistoryIndex ().put ("TMPProcess" , Base64 .getEncoder ().encodeToString (ProcessHistory .convert (pIDs ).getBytes (StandardCharsets .UTF_8 )));
369+ getProcessHistory ().getProcessHistoryIndex ().refreshFile ();
370+ }
371+
372+ }
373+
374+
375+ if (consoleReader .sReader != null ){
376+ for (String key : ReaderHistory .getLines ().keySet ()) {
377+ /* History h = ConsoleReader.sReader.getHistory();
378+ ArrayList<String> l = new ArrayList<>();
379+ for (int j = 0; j < h.size()-1; j++) {
380+ l.add(h.get(j));
381+ }*/
382+ ArrayList <String > l = new ArrayList <>(ReaderHistory .getLines ().get (key ));
383+
384+ List <String > tail = l .subList (Math .max (l .size () - 15 , 0 ), l .size ());
385+ readerHistory .getReaderHistoryIndex ().put (key , Base64 .getEncoder ().encodeToString (ReaderHistory .convert (tail ).getBytes (StandardCharsets .UTF_8 )));
386+ readerHistory .getReaderHistoryIndex ().refreshFile ();
387+
388+ }
389+ }
390+ isReady = true ;
391+
392+
393+ Core .getInstance ().getAddonsManager ().getAddons ().values ().forEach (DreamExtension ::stop );
394+ PrintStream outputStream = Console .getFormatter ().getDefaultStream ();
395+ outputStream .println (Console .getFromLang ("main.shutdown" ));
396+ try {
397+ Thread .sleep (2000 );
398+ } catch (InterruptedException e ) {
399+ e .printStackTrace ();
400+ }
401+ }else {
402+ PrintStream outputStream = Console .getFormatter ().getDefaultStream ();
403+ outputStream .println (Console .getFromLang ("main.shutdown" ));
404+ try {
405+ Thread .sleep (2000 );
406+ } catch (InterruptedException e ) {
407+ e .printStackTrace ();
408+ }
409+ }
410+
411+
412+ }catch (Exception e ){
413+ System .out .println (e .getMessage ());
414+ e .printStackTrace ();
415+ }
416+
417+ System .exit (0 );
418+ }
336419}
0 commit comments