forked from ProjectIgnis/windbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
279 lines (259 loc) · 10.6 KB
/
Program.cs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.IO;
using System.Threading;
using System.Net;
using System.Web;
using WindBot.Game;
using WindBot.Game.AI;
using YGOSharp.OCGWrapper;
using System.Runtime.Serialization.Json;
namespace WindBot
{
public class Program
{
public static string AssetPath;
internal static Random Rand;
internal static void Main(string[] args)
{
Logger.WriteLine("WindBot starting...");
Config.Load(args);
Logger.WriteLine(Config.GetString("Deck"));
AssetPath = Config.GetString("AssetPath", "");
string databasePath = Config.GetString("DbPath");
string databasePaths = Config.GetString("DbPaths");
InitDatas(databasePath, databasePaths);
bool serverMode = Config.GetBool("ServerMode", false);
if (serverMode)
{
// Run in server mode, provide a http interface to create bot.
int serverPort = Config.GetInt("ServerPort", 2399);
RunAsServer(serverPort);
}
else
{
// Join the host specified on the command line.
if (args.Length == 0)
{
Logger.WriteErrorLine("=== WARN ===");
Logger.WriteLine("No input found, tring to connect to localhost YGOPro host.");
Logger.WriteLine("If it fail, the program will quit sliently.");
}
RunFromArgs();
}
}
public static void InitDatas(string databasePath, string databasePaths)
{
Rand = new Random();
DecksManager.Init();
string[] dbPaths = null;
try
{
if (databasePath == null && databasePaths != null)
{
MemoryStream json = new MemoryStream(Convert.FromBase64String(databasePaths));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(string[]));
dbPaths = serializer.ReadObject(json) as string[];
}
}
catch (Exception)
{
}
if (dbPaths == null)
{
if (databasePath == null)
databasePath = "cards.cdb";
//If databasePath is an absolute path like "C:/ProjectIgnis/expansions/cards.cdb",
//then Path.GetFullPath("../C:/ProjectIgnis/expansions/cards.cdb" would give an error,
//due to containing a colon that's not part of a volume identifier.
if (Path.IsPathRooted(databasePath)) dbPaths = new string[] { databasePath };
else dbPaths = new string[]{
Path.GetFullPath(databasePath),
Path.GetFullPath("../" + databasePath),
Path.GetFullPath("../expansions/" + databasePath)
};
}
bool loadedone = false;
foreach (var absPath in dbPaths)
{
try
{
if (File.Exists(absPath))
{
NamedCardsManager.LoadDatabase(absPath);
Logger.DebugWriteLine("Loaded database: " + absPath + ".");
loadedone = true;
}
} catch (Exception ex)
{
Logger.WriteErrorLine("Failed loading database: " + absPath + " error: " + ex);
}
}
if (!loadedone)
{
Logger.WriteErrorLine("Can't find cards database file.");
Logger.WriteErrorLine("Please place cards.cdb next to WindBot.exe or Bot.exe .");
}
}
private static void RunFromArgs()
{
WindBotInfo Info = new WindBotInfo();
Info.Name = Config.GetString("Name", Info.Name);
Info.Deck = Config.GetString("Deck", Info.Deck);
Info.DeckFile = Config.GetString("DeckFile", Info.DeckFile);
Info.Dialog = Config.GetString("Dialog", Info.Dialog);
Info.Host = Config.GetString("Host", Info.Host);
Info.Port = Config.GetInt("Port", Info.Port);
Info.HostInfo = Config.GetString("HostInfo", Info.HostInfo);
Info.Version = Config.GetInt("Version", Info.Version);
Info.Hand = Config.GetInt("Hand", Info.Hand);
Info.Debug = Config.GetBool("Debug", Info.Debug);
Info.Chat = Config.GetBool("Chat", Info.Chat);
////////kdiy///////
Info.Seed = Config.GetInt("Seed", Info.Seed);
////////kdiy///////
Info.RoomId = Config.GetInt("RoomId", Info.RoomId);
string b64CreateGame = Config.GetString("CreateGame");
if (b64CreateGame != null)
{
try
{
var ms = new MemoryStream(Convert.FromBase64String(b64CreateGame));
var ser = new DataContractJsonSerializer(typeof(CreateGameInfo));
Info.CreateGame = ser.ReadObject(ms) as CreateGameInfo;
// "Best of 0" is not allowed by the server, use that to check for validity.
if (Info.CreateGame.bestOf == 0) Info.CreateGame = null;
}
catch (Exception ex)
{
Info.CreateGame = null;
Logger.DebugWriteLine("Error while parsing CreateGame json: " + ex);
}
}
Run(Info);
}
private static void RunAsServer(int ServerPort)
{
using (HttpListener MainServer = new HttpListener())
{
MainServer.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
MainServer.Prefixes.Add("http://+:" + ServerPort + "/");
MainServer.Start();
Logger.WriteLine("WindBot server start successed.");
Logger.WriteLine("HTTP GET http://127.0.0.1:" + ServerPort + "/?name=WindBot&host=127.0.0.1&port=7911 to call the bot.");
while (true)
{
#if !DEBUG
try
{
#endif
HttpListenerContext ctx = MainServer.GetContext();
WindBotInfo Info = new WindBotInfo();
string RawUrl = Path.GetFileName(ctx.Request.RawUrl);
Info.Name = HttpUtility.ParseQueryString(RawUrl).Get("name");
Info.Deck = HttpUtility.ParseQueryString(RawUrl).Get("deck");
Info.Host = HttpUtility.ParseQueryString(RawUrl).Get("host");
string port = HttpUtility.ParseQueryString(RawUrl).Get("port");
if (port != null)
Info.Port = Int32.Parse(port);
string deckfile = HttpUtility.ParseQueryString(RawUrl).Get("deckfile");
if (deckfile != null)
Info.DeckFile = deckfile;
string dialog = HttpUtility.ParseQueryString(RawUrl).Get("dialog");
if (dialog != null)
Info.Dialog = dialog;
string version = HttpUtility.ParseQueryString(RawUrl).Get("version");
if (version != null)
Info.Version = Int16.Parse(version);
string password = HttpUtility.ParseQueryString(RawUrl).Get("password");
if (password != null)
Info.HostInfo = password;
string hand = HttpUtility.ParseQueryString(RawUrl).Get("hand");
if (hand != null)
Info.Hand = Int32.Parse(hand);
string debug = HttpUtility.ParseQueryString(RawUrl).Get("debug");
if (debug != null)
Info.Debug= bool.Parse(debug);
string chat = HttpUtility.ParseQueryString(RawUrl).Get("chat");
if (chat != null)
Info.Chat = bool.Parse(chat);
//////kdiy////////
string seed = HttpUtility.ParseQueryString(RawUrl).Get("seed");
if (seed != null)
Info.Seed = Int32.Parse(seed);
//////kdiy////////
if (Info.Name == null || Info.Host == null || port == null)
{
ctx.Response.StatusCode = 400;
ctx.Response.Close();
}
else
{
#if !DEBUG
try
{
#endif
Thread workThread = new Thread(new ParameterizedThreadStart(Run));
workThread.Start(Info);
#if !DEBUG
}
catch (Exception ex)
{
Logger.WriteErrorLine("Start Thread Error: " + ex);
}
#endif
ctx.Response.StatusCode = 200;
ctx.Response.Close();
}
#if !DEBUG
}
catch (Exception ex)
{
Logger.WriteErrorLine("Parse Http Request Error: " + ex);
}
#endif
}
}
}
private static void Run(object o)
{
#if !DEBUG
try
{
//all errors will be catched instead of causing the program to crash.
#endif
WindBotInfo Info = (WindBotInfo)o;
GameClient client = new GameClient(Info);
client.Start();
Logger.DebugWriteLine(client.Username + " started.");
while (client.Connection.IsConnected)
{
#if !DEBUG
try
{
#endif
client.Tick();
Thread.Sleep(30);
#if !DEBUG
}
catch (Exception ex)
{
Logger.WriteErrorLine("Tick Error: " + ex);
client.Chat("I crashed, check the crash.log file in the WindBot folder", true);
using (StreamWriter sw = File.AppendText(Path.Combine(AssetPath, "crash.log"))) {
sw.WriteLine("[" + DateTime.Now.ToString("yy-MM-dd HH:mm:ss") + "] Tick Error: " + ex);
}
return;
}
#endif
}
Logger.DebugWriteLine(client.Username + " end.");
#if !DEBUG
}
catch (Exception ex)
{
Logger.WriteErrorLine("Run Error: " + ex);
}
#endif
}
}
}