This repository has been archived by the owner on Apr 29, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
script_syntax_check.cpp
163 lines (149 loc) · 5.65 KB
/
script_syntax_check.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
/**
* EDOProSE Syntax Checker
* Copyright (C) 2019 Kevin Lu
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <vector>
#include <dirent.h>
#include "ocgapi.h"
#include "common.h"
int exitCode = EXIT_SUCCESS;
std::string lastScript;
std::vector<std::string> scriptDirectories;
void GetCard([[maybe_unused]] void *payload, uint32_t code, OCG_CardData *card) {
card->code = code;
}
int LoadScript([[maybe_unused]] void *payload, OCG_Duel duel, const char *path) {
std::ifstream f;
for (const auto& directory : scriptDirectories) {
f.open(directory + '/' + path);
if (f) {
break;
}
}
std::stringstream buf;
buf << f.rdbuf();
lastScript = path;
return buf.str().length() && OCG_LoadScript(duel, buf.str().c_str(), buf.str().length(), path);
}
void Log([[maybe_unused]] void *payload, const char *string, int type) {
std::cerr << type << ": " << string << " from " << lastScript << std::endl;
exitCode = EXIT_FAILURE;
}
void LoadIfExists(OCG_Duel duel, const char* script) {
if (LoadScript(nullptr, duel, script)) {
std::cout << "Loaded " << script << std::endl;
} else {
std::cerr << "Failed to load " << script << std::endl;
}
}
void DirectoryWalk(const std::string& directory, const std::function<void(dirent*)>& callback) {
auto listing = opendir(directory.c_str());
if (!listing) {
throw std::runtime_error("Failed to open " + directory);
}
for (dirent *entry; (entry = readdir(listing)) != nullptr; ) {
callback(entry);
}
}
void DirectoryWalkRecursive(const std::string& directory, const std::function<void(dirent*)>& callback, int levels = 0) {
DirectoryWalk(directory, [&](dirent* entry) {
switch(entry->d_type) {
case DT_REG:
callback(entry);
break;
case DT_DIR:
if (levels && strlen(entry->d_name) && entry->d_name[0] != '.') {
DirectoryWalkRecursive(directory + '/' + entry->d_name, callback, levels - 1);
}
break;
}
});
}
void DirectoryWalkSubfolders(const std::string& directory, const std::function<void(dirent*)>& callback, int levels = 0) {
DirectoryWalk(directory, [&](dirent* entry) {
if (entry->d_type == DT_DIR && strlen(entry->d_name) && entry->d_name[0] != '.') {
callback(entry);
if (levels) {
DirectoryWalkSubfolders(directory + '/' + entry->d_name, callback, levels - 1);
}
}
});
}
int main(int argc, char* argv[]) {
if (argc == 1) {
scriptDirectories.push_back(".");
} else {
for (int i = 1; i < argc; i++) {
const std::string scriptRoot = argv[i];
try {
std::cout << "Passed script folder " << scriptRoot << std::endl;
scriptDirectories.push_back(scriptRoot);
DirectoryWalkSubfolders(scriptRoot, [scriptRoot](dirent* entry) {
std::cout << "Found script folder " << scriptRoot << '/' << entry->d_name << std::endl;
scriptDirectories.push_back(scriptRoot + '/' + entry->d_name);
});
} catch (const std::runtime_error& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
}
}
OCG_DuelOptions config{};
config.cardReader = &GetCard;
config.scriptReader = &LoadScript;
config.logHandler = &Log;
OCG_Duel duel;
if (OCG_CreateDuel(&duel, config) != OCG_DUEL_CREATION_SUCCESS) {
std::cerr << "Failed to create duel instance!" << std::endl;
return EXIT_FAILURE;
}
try {
LoadIfExists(duel, "constant.lua");
LoadIfExists(duel, "utility.lua");
for (const auto& scriptRoot : scriptDirectories) {
DirectoryWalkRecursive(scriptRoot, [duel](dirent* entry) {
std::string name(entry->d_name);
auto length = name.length();
if (length > 0 && name != "constant.lua" && name != "utility.lua" &&
name[0] == 'c' && name.rfind(".lua") == length - 4 && length != 8) {
OCG_NewCardInfo card{};
card.team = card.duelist = card.con = 0;
card.seq = 1;
card.loc = LOCATION_DECK;
card.pos = POS_FACEDOWN_ATTACK;
try {
card.code = std::stoi(name.substr(1, length - 4));
if (card.code == 151000000) return;
} catch (const std::invalid_argument& e) {
return;
}
OCG_DuelNewCard(duel, card);
}
}, 1);
}
} catch (const std::runtime_error& e) {
std::cerr << e.what() << std::endl;
exitCode = EXIT_FAILURE;
}
OCG_DestroyDuel(duel);
return exitCode;
}