annotate dep/animia/src/animia.cc @ 139:478f3b366199

dep/animia: separate lots of things, use base class for OS stuff
author Paper <mrpapersonic@gmail.com>
date Sun, 12 Nov 2023 16:43:07 -0500
parents 28842a8d0c6b
children 1e696863b54c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
1 #include <string>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
2 #include <vector>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
3
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
4 #include <windows.h>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
5
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
6 #include "animia.h"
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
7 #include "animia/strategies.h"
139
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
8 #include "animia/types.h"
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
9
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
10 #ifdef ANIMIA_ON_WIN32
139
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
11 # include "animia/fd/win32.h"
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
12 #elif defined(ANIMIA_ON_LINUX)
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
13 # include "animia/fd/linux.h"
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
14 #elif defined(ANIMIA_ON_UNIX)
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
15 # include "animia/fd/bsd.h"
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
16 #endif
139
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
17
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
18 namespace animia::internal {
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
19
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
20 /* really stupid hack to get fd to point to the right
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
21 thing */
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
22 #ifdef ANIMIA_ON_WIN32
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
23 win32::Win32FdTools os_fd;
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
24 #elif defined(ANIMIA_ON_LINUX)
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
25 linux::LinuxFdTools os_fd;
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
26 #elif defined(ANIMIA_ON_UNIX)
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
27 unix::UnixFdTools os_fd;
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
28 #else
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
29 BaseFdTools os_fd;
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
30 #endif
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
31
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
32 BaseFdTools& fd = os_fd;
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
33
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
34 }
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
35
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
36 #include <iostream>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
37
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
38 namespace animia {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
39
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
40 static bool ProcessInPlayers(const std::vector<Player>& players, const std::string& name, Player& player_) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
41 for (const auto& player : players) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
42 for (const auto& exe : player.executables) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
43 if (exe == name.substr(0, name.rfind(".exe"))) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
44 player_ = player;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
45 return true;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
46 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
47 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
48 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
49 return false;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
50 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
51
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
52 bool GetResults(const std::vector<Player>& players, std::vector<Result>& results) {
139
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
53 std::set<internal::pid_t> pids;
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
54
139
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
55 if (!internal::fd.GetAllPids(pids))
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
56 return false;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
57
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
58 for (const auto& pid : pids) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
59 std::string name;
139
478f3b366199 dep/animia: separate lots of things, use base class for OS stuff
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
60 internal::fd.GetProcessName(pid, name);
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
61
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
62 Player player;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
63 if (!ProcessInPlayers(players, name, player))
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
64 continue;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
65
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
66 Result result;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
67 result.process.pid = pid;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
68 result.process.name = name;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
69 result.player = player;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
70 results.push_back(result);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
71 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
72
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
73 return internal::ApplyStrategies(results);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
74 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
75
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
76 }