~ruther/NosTale-Gfless

ref: fc51a68e949a505ac95fe9b5e7363245d2b539d8 NosTale-Gfless/NosTaleGfless/GameforgeLauncher.cs -rw-r--r-- 2.1 KiB
fc51a68e — František Boháček Initial commit 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NostaleAuth.Api;
using NostaleAuth.Models;

namespace NosTaleGfless
{
    public class GameforgeLauncher : IDisposable
    {
        private Timer _watchTimer;
        
        public GameforgeLauncher(AuthorizedGameforgeApi api, IEnumerable<GameforgeAccount> accounts, string email)
        {
            Accounts = accounts.ToList().AsReadOnly();
            Email = email;
            Launcher = new NostaleLauncher();
            Api = api;
            
            ActiveProcesses = new ObservableCollection<NostaleProcess>();
        }
        
        public string Email { get; }
        
        public ReadOnlyCollection<GameforgeAccount> Accounts { get; }
        
        public AuthorizedGameforgeApi Api { get; }
        
        public NostaleLauncher Launcher { get; set; }
        
        public ObservableCollection<NostaleProcess> ActiveProcesses { get; }

        public async Task<NostaleProcess> Launch(GameforgeAccount account, string nostalePath)
        {
            NostaleProcess process = await Launcher.Launch(account, await Api.GetSessionToken(account, true), nostalePath);
            if (process != null)
            {
                ActiveProcesses.Add(process);
            }

            return process;
        }

        public void StartProcessWatch()
        {
            StopProcessWatch();
            
            _watchTimer = new Timer((state) =>
            {
                foreach (var process in ActiveProcesses
                    .Where(x => x.HasExited)
                    .ToArray())
                {
                    ActiveProcesses.Remove(process);
                }
            }, null, 1000, 1000);
        }

        public void StopProcessWatch()
        {
            if (_watchTimer != null)
            {
                _watchTimer.Dispose();
                _watchTimer = null;
            }
        }

        public void Dispose()
        {
            StopProcessWatch();
        }
    }
}
Do not follow this link