forked from marcosvf132/TibiaAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cs
More file actions
166 lines (133 loc) · 6.13 KB
/
Client.cs
File metadata and controls
166 lines (133 loc) · 6.13 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
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace OXGaming.TibiaAPI
{
public class Client : IDisposable
{
private string _appearanceDatFile;
public Appearances.AppearanceStorage AppearanceStorage { get; } = new Appearances.AppearanceStorage();
public Creatures.CreatureStorage CreatureStorage { get; } = new Creatures.CreatureStorage();
public WorldMap.WorldMapStorage WorldMapStorage { get; } = new WorldMap.WorldMapStorage();
public Creatures.Creature Player { get; } = new Creatures.Creature(0, Constants.CreatureType.Player);
public Network.Connection Connection { get; }
public Utilities.Logger Logger { get; } = new Utilities.Logger();
public string Version { get; private set; }
public uint VersionNumber { get; private set; } = 0;
public Client(string tibiaDirectory = "")
{
if (tibiaDirectory == null)
throw new ArgumentNullException(nameof(tibiaDirectory));
if (!Initialize(tibiaDirectory))
throw new Exception("Failed to initialize.");
using (var datFileStream = File.OpenRead(_appearanceDatFile))
{
AppearanceStorage.LoadAppearances(datFileStream);
}
Connection = new Network.Connection(this);
}
public bool StartConnection(int httpPort = 7171, string loginWebService = "")
{
return Connection.Start(httpPort, loginWebService);
}
public void StopConnection()
{
Connection.Stop();
}
private bool Initialize(string tibiaDirectory = "")
{
if (string.IsNullOrEmpty(tibiaDirectory)) {
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
tibiaDirectory = Path.Combine(new string[] {
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"CipSoft GmbH", "Tibia", "packages", "Tibia" });
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
tibiaDirectory = Path.Combine(new string[] {
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"Library", "Application Support", "CipSoft GmbH", "Tibia", "packages", "Tibia.app" });
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
tibiaDirectory = Path.Combine(new string[] {
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Tibia", "packages", "Tibia" });
}
}
if (string.IsNullOrEmpty(tibiaDirectory) || !Directory.Exists(tibiaDirectory)) {
Logger.Error($"Directory does not exist: {tibiaDirectory}");
return false;
}
var packageJsonFile = Path.Combine(tibiaDirectory, "package.json");
if (!File.Exists(packageJsonFile)) {
Logger.Error($"package.json file does not exist: {packageJsonFile}");
return false;
}
var packageJson = string.Empty;
using (var reader = new StreamReader(packageJsonFile))
{
packageJson = reader.ReadToEnd();
if (string.IsNullOrEmpty(packageJson)) {
Logger.Error($"Failed to read package.json file.");
return false;
}
}
dynamic packageData = Newtonsoft.Json.JsonConvert.DeserializeObject(packageJson);
if (packageData == null || packageData.version == null) {
Logger.Error("Failed to deserialize package.json file.");
return false;
}
Version = packageData.version;
if (string.IsNullOrEmpty(Version)) {
Logger.Error($"Failed to get client version.");
return false;
}
if (uint.TryParse(Version.Replace(".", ""), out var versionNumber))
VersionNumber = versionNumber;
else
Logger.Warning($"Failed to convert the client version to a numerical value: {Version}");
var assetsDirectory = string.Empty;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
assetsDirectory = Path.Combine(new string[] { tibiaDirectory, "Contents", "Resources", "assets" });
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
assetsDirectory = Path.Combine(tibiaDirectory, "assets");
if (!Directory.Exists(assetsDirectory)) {
Logger.Error($"Assets directory does not exist: {assetsDirectory}");
return false;
}
var appearanceDatFiles = Directory.GetFiles(assetsDirectory, "*appearances-*.dat");
if (appearanceDatFiles.Length != 1) {
Logger.Error($"Invalid number of appearances dat files: {appearanceDatFiles.Length}");
return false;
}
_appearanceDatFile = appearanceDatFiles[0];
if (string.IsNullOrEmpty(_appearanceDatFile) || !File.Exists(_appearanceDatFile)) {
Logger.Error($"Appearances .dat file does not exist: {_appearanceDatFile}");
return false;
}
return true;
}
#region IDisposable Support
private bool disposedValue = false;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue) {
if (disposing) {
Connection.Dispose();
Logger.Dispose();
}
disposedValue = true;
}
}
~Client()
{
Dispose(false);
}
/// <summary>
/// Releases all the managed resources used by the <see cref="Client"/>.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}