This repository was archived by the owner on Apr 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAutorun.cpp
More file actions
186 lines (168 loc) · 5.22 KB
/
Copy pathAutorun.cpp
File metadata and controls
186 lines (168 loc) · 5.22 KB
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
#include <bits/stdc++.h>
#include <synchapi.h>
#include <windows.h>
#include "include/configor/json.hpp"
#include "include/inicpp.hpp"
#include "include/path.h"
#include "include/system.h"
using namespace std;
using namespace path;
using namespace configor;
using namespace inicpp;
string self_path, self_name;
class Task {
public:
string path;
size_t trigger_count;
clock_t trigger_interval;
bool auto_trigger;
inline void init();
inline void init(const json::value &task);
inline void init(const string &p, const size_t &tc, const clock_t &ti, const bool &at);
Task() { init(); }
Task(const json::value &task) { init(task); }
Task(const string &p, const size_t &tc, const clock_t &ti, const bool &at) { init(p, tc, ti, at); }
void run();
};
class Config {
public:
clock_t interval_time, interval_eps;
vector<Task> task;
inline void init();
inline void init(const json::value &config);
inline bool init(const string &config_path);
inline void init(const clock_t &it, const clock_t &ie, const vector<Task> &t);
Config() { init(); }
Config(const json::value &config) { init(config); }
Config(const string config_path) { init(config_path); }
Config(const clock_t &it, const clock_t &ie, const vector<Task> &t) { init(it, ie, t); }
};
inline bool loadConfig(string path, json::value &data) {
data.clear();
ifstream file(path.c_str());
if (!file.is_open()) return true;
try {
file >> json::wrap(data);
} catch (exception &_ERROR_) { return true; }
file.close();
return false;
}
inline void Task::init() {
path = "";
trigger_count = 1;
trigger_interval = 0;
auto_trigger = true;
}
inline void Task::init(const json::value &task) {
init();
path = task["path"];
pathDelete(path);
if (task.count("trigger_count") > 0)
trigger_count = stol(task["trigger_count"]);
if (task.count("trigger_interval") > 0)
trigger_interval = stol(task["trigger_interval"]);
if (task.count("auto_trigger") > 0)
auto_trigger = task["auto_trigger"];
}
inline void Task::init(const string &p, const size_t &tc, const clock_t &ti, const bool &at) {
init();
path = p;
trigger_count = tc;
trigger_interval = ti;
auto_trigger = at;
}
inline void Config::init() {
interval_time = 100;
interval_eps = 1000;
task.clear();
}
inline void Config::init(const json::value &config) {
init();
interval_time = stol(config["interval_time"]);
if (config.count("interval_eps"))
interval_eps = stol(config["interval_eps"]);
if (config.count("task") > 0) {
for (json::value subtask : config["task"])
task.push_back(subtask);
}
}
inline bool Config::init(const string &config_path) {
json::value config;
bool is_failed = loadConfig(config_path, config);
if (is_failed) return true;
init(config);
return false;
}
inline void Config::init(const clock_t &it, const clock_t &ie, const vector<Task> &t) {
init();
interval_time = it;
interval_eps = ie;
task = t;
}
inline void debug(const Config &config) {
cout << config.interval_time << "\n";
for (Task subtask : config.task) {
cout << subtask.path << " " << subtask.trigger_count << " " << subtask.trigger_interval << " " << subtask.auto_trigger << "\n";
}
}
void Task::run() {
string target, trash;
pathSplit(path, target, trash);
chdir(target.c_str());
vector< future<void> > pool;
for (size_t i = 1; i <= trigger_count; i++) { // PTSD
pool.push_back(async(launch::async, executefile, path));
if (i < trigger_count)
Sleep(trigger_interval);
}
for (future<void> &fut : pool)
fut.wait();
chdir(self_path.c_str());
}
void process(const Config &config) {
auto time_now = std::chrono::steady_clock::now();
auto time_rec = std::chrono::steady_clock::now();
map<string, bool> exist_rec;
for (Task subtask : config.task)
exist_rec[subtask.path] = false;
while (true) {
time_rec = time_now;
time_now = std::chrono::steady_clock::now();
for (Task subtask : config.task) {
const string &path = subtask.path; // PTSD
bool exist_now = pathExist(path);
if (exist_now && (!exist_rec[path] || (subtask.auto_trigger && (time_now - time_rec).count() * CLOCKS_PER_SEC > config.interval_time + config.interval_eps))) {
future<void> trash = async(launch::async, Task::run, subtask); // 有风险,目前最优
}
exist_rec[path] = exist_now;
}
Sleep(config.interval_time);
}
}
int main(int n_, char** config_path_) {
pathSplit(_pgmptr, self_path, self_name);
HWND console = GetConsoleWindow();
string ini_config_path = self_path + "\\config.ini";
pathDelete(ini_config_path);
IniManager ini_config(ini_config_path.c_str());
if (ini_config["Autorun"]["hide_console_window"] == "1")
ShowWindow(console, SW_HIDE);
vector<string> config_path;
if (n_ == 1)
config_path.push_back(ini_config["Autorun"]["default_profile"]);
else
for (int i = 1; i < n_; i++)
config_path.push_back(config_path_[i]);
for (string path : config_path) {
Config config;
bool is_failed = config.init(path);
if (is_failed) {
//
string alert = "配置文件 \"" + path + "\" 解析失败!";
MessageBox(nullptr, alert.c_str(), "Error - Autorun", MB_OK);
continue;
}
future<void> trash = async(process, config);
}
return 0;
}