-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRenderService.cpp
188 lines (169 loc) · 6.63 KB
/
RenderService.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// Created by epiphyllum on 22/04/22.
//
#include <iostream>
#include <algorithm>
#include <utility>
#include "RenderService.h"
#include "Utility/Util.h"
#include "Utility/UiUtil.h"
#include "HacknetApplication.h"
#include "AsciiArt.h"
const AsciiArt serverLogos[] = {
AsciiArt("ASCII/laptop.ascii"),
AsciiArt("ASCII/server.ascii"),
};
void RenderService::RenderTick()
{
if (requireUpdate)
{
Util::clearScreen();
UIUtil::drawFramework();
}
RenderMemory();
RenderStatusBar();
RenderTerminal();
requireUpdate = false;
}
void RenderService::RenderTerminal()
{
// Area buffer
if (requireUpdate)
{
int buffSize = std::min(UIUtil::SIZE_TERMINAL.height - 2, static_cast<int>(ref->commandBuffer.size()));
int h = 0;
for (auto it = ref->commandBuffer.end() - buffSize; it != ref->commandBuffer.end(); ++it)
{
Util::setCursorPos(UIUtil::START_TERMINAL + Coord(0, h++));
std::cout << *it;
}
}
else
{
// only update prompt
Util::clearLine(UIUtil::START_TERMINAL + Coord(0, UIUtil::SIZE_TERMINAL.height - 1),
UIUtil::SIZE_TERMINAL.width);
}
// Area prompt
ref->inputService.renderCMD(ref->getPrompt(), UIUtil::START_TERMINAL + Coord(0, UIUtil::SIZE_TERMINAL.height - 1));
}
void RenderService::RenderStatusBar()
{
// write time
if (ref->CurrentConnected == nullptr)
{
// disconnected
Util::setCursorPos(UIUtil::START_STATUSPANEL + Coord(84, 2));
std::cout << "已断开连接";
}
else
{
// connected
serverLogos[ref->CurrentConnected->getName().size() % 2].draw(UIUtil::START_STATUSPANEL);
Util::setCursorPos(UIUtil::START_STATUSPANEL + Coord(11, 0));
std::cout << "连接到";
Util::setCursorPos(UIUtil::START_STATUSPANEL + Coord(11, 1));
std::cout << ref->CurrentConnected->getName();
Util::setCursorPos(UIUtil::START_STATUSPANEL + Coord(11, 2));
std::cout << "@" << ref->CurrentConnected->getIp();
if (ref->CurrentConnected->isAccessible())
{
// :: 您是本系统的管理员 ::
Util::setCursorPos(UIUtil::START_STATUSPANEL + Coord(11, 4));
Util::setColorAttr(Util::BG_LIGHT_BLUE);
Util::setColorAttr(Util::FG_BLACK);
// 72
for (int i = 0; i < 72; ++i)
{
std::cout << ' ';
}
std::cout << ":: 您是本系统的管理员 ::";
for (int i = 0; i < 72; ++i)
{
std::cout << ' ';
}
Util::setColorAttr(Util::ATTR_NORMAL);
}
else if (ref->nmapDetail)
{
// show nmap detail
Util::setCursorPos(UIUtil::START_STATUSPANEL + Coord(100, 0));
Util::setColorAttr(ref->CurrentConnected->isHttpLocked() ? Util::FG_RED : Util::FG_GREEN);
std::cout << "#80: HTTP";
Util::setCursorPos(UIUtil::START_STATUSPANEL + Coord(100, 1));
Util::setColorAttr(ref->CurrentConnected->isSmtpLocked() ? Util::FG_RED : Util::FG_GREEN);
std::cout << "#25: SMTP";
Util::setCursorPos(UIUtil::START_STATUSPANEL + Coord(100, 2));
Util::setColorAttr(ref->CurrentConnected->isFtpLocked() ? Util::FG_RED : Util::FG_GREEN);
std::cout << "#21: FTP";
Util::setCursorPos(UIUtil::START_STATUSPANEL + Coord(100, 3));
Util::setColorAttr(ref->CurrentConnected->isSshLocked() ? Util::FG_RED : Util::FG_GREEN);
std::cout << "#22: SSH";
Util::setColorAttr(Util::ATTR_NORMAL);
if (ref->CurrentConnected->getShellLife() != 0)
{
Util::setCursorPos(UIUtil::START_STATUSPANEL + Coord(110, 1));
// detected shell
int prog = std::min(std::max(ref->shellProgress, 0), ref->getCurrentConnected()->getShellLife()) * 30 /
ref->CurrentConnected->getShellLife();
std::string text = ref->shellProgress == -1 ? "Proxy detected" : (ref->shellProgress >
ref->CurrentConnected->getShellLife()
? "Proxy failed"
: "Proxy overloading...");
Util::setColorAttr(Util::FG_WHITE);
Util::setColorAttr(Util::BG_LIGHT_GREEN);
for (int i = 0; i < 30; ++i)
{
if (i == prog)
{
Util::setColorAttr(Util::BG_LIGHT_RED);
}
std::cout << (i >= text.size() ? ' ' : text[i]);
}
}
Util::setCursorPos(UIUtil::START_STATUSPANEL + Coord(110, 2));
Util::setColorAttr(
ref->CurrentConnected->checkIfSecureBroken(ref->shellProgress) ? Util::BG_GREEN : Util::BG_RED);
Util::setColorAttr(Util::FG_WHITE);
std::cout << "Open ports required: " << ref->CurrentConnected->getMinRequired();
Util::clearLine(Util::getCursorPos(),
(UIUtil::START_STATUSPANEL + Coord(140, 2)).x - Util::getCursorPos().x);
Util::setColorAttr(Util::ATTR_NORMAL);
}
}
}
RenderService::RenderService(HacknetApplication *ref) : ref(ref)
{
}
void RenderService::RenderMemory()
{
int totalHeight = 0;
for (auto &item: ref->backgroundTasks)
{
if (item->isStopped())
{
continue;
}
// Draw Title
Util::setCursorPos(UIUtil::START_MEMORYPANEL + Coord(0, totalHeight));
Util::setColorAttr(Util::BG_LIGHT_BLUE);
Util::setColorAttr(Util::FG_BLACK);
std::cout << "App: " << item->getThreadName();
Util::clearLine(Util::getCursorPos(),
UIUtil::SIZE_MEMORYPANEL.width - Util::getCursorPos().x + UIUtil::START_MEMORYPANEL.x);
Util::setColorAttr(Util::ATTR_NORMAL);
item->renderMemory(UIUtil::START_MEMORYPANEL + Coord(0, totalHeight + 1));
totalHeight += item->getMemorySize() + 1;
}
// clear empty area
Util::clearArea(UIUtil::START_MEMORYPANEL + Coord(0, totalHeight),
Size2D(UIUtil::SIZE_MEMORYPANEL.width, UIUtil::SIZE_MEMORYPANEL.height - totalHeight));
}
bool RenderService::isRequireUpdate() const
{
return requireUpdate;
}
void RenderService::setRequireUpdate(bool requireUpdate)
{
RenderService::requireUpdate = requireUpdate;
}