forked from IceYGO/windbot
-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
WindBot.cs
147 lines (141 loc) · 5.06 KB
/
WindBot.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using WindBot.Game;
using WindBot.Game.AI;
using YGOSharp.OCGWrapper;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
namespace WindBot
{
public class WindBot
{
public static void InitAndroid(string assetPath)
{
NamedCardsManager.SetThreadSafe();
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Program.Rand = new Random();
Program.AssetPath = assetPath;
DecksManager.Init();
}
public static void AddDatabase(string databasePath)
{
try
{
NamedCardsManager.LoadDatabase(databasePath);
}
catch (Exception ex)
{
Logger.WriteErrorLine("Failed loading database: " + databasePath + " error: " + ex);
}
}
[DataContract]
public class LaunchData
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Deck { get; set; }
[DataMember]
public string DeckFile { get; set; }
[DataMember]
public string Dialog { get; set; }
[DataMember]
public string Host { get; set; }
[DataMember]
public string Port { get; set; }
[DataMember]
public string HostInfo { get; set; }
[DataMember]
public string Version { get; set; }
[DataMember]
public string Hand { get; set; }
[DataMember]
public string Debug { get; set; }
[DataMember]
public string Chat { get; set; }
}
public static void RunAndroid(string arg)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
WindBotInfo Info = new WindBotInfo();
try
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(LaunchData));
MemoryStream json = new MemoryStream(Encoding.Unicode.GetBytes(arg));
LaunchData data = (LaunchData)serializer.ReadObject(json);
if (data.Name != null) Info.Name = data.Name;
if (data.Deck != null) Info.Deck = data.Deck;
if (data.DeckFile != null) Info.DeckFile = data.DeckFile;
if (data.Dialog != null) Info.Dialog = data.Dialog;
if (data.Port != null) Info.Port = int.Parse(data.Port);
if (data.Hand != null) Info.Hand = int.Parse(data.Hand);
if (data.Host != null) Info.Host = data.Host;
if (data.HostInfo != null) Info.HostInfo = data.HostInfo;
if (data.Version != null) Info.Version = int.Parse(data.Version);
if (data.Chat != null) Info.Chat = int.Parse(data.Chat) != 0;
if (data.Debug != null) Info.Debug = int.Parse(data.Debug) != 0;
}
catch (Exception ex)
{
Logger.WriteErrorLine("Argument parsing error: " + ex);
}
Thread workThread = new Thread(new ParameterizedThreadStart(Run));
workThread.Start(Info);
}
private static void Run(object o)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
#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(Program.AssetPath, "crash.log")))
{
sw.WriteLine("[" + DateTime.Now.ToString("yy-MM-dd HH:mm:ss") + "] Tick Error: " + ex);
}
client.Connection.Close();
return;
}
#endif
}
Logger.DebugWriteLine(client.Username + " end.");
#if !DEBUG
}
catch (Exception ex)
{
Logger.WriteErrorLine("Run Error: " + ex);
}
#endif
}
}
public class Program
{
public static string AssetPath;
internal static Random Rand;
}
}