Skip to content

Commit 72c6159

Browse files
authored
Add client and server packets. (jo3bingham#4)
* Add Communication class as the base for parsing packets * Move zlib to Network to be consistent * Begin adding packets. Adds Peek methods to NetworkMessage class. * Add client packets. Add Reset method to NetworkMessage. Prepare for allowing packets to be modified before forwarding. Prepare to allow packets to be blocked from being forwarded. Prepare for adding server packets. Prepare for compressing (deflate) packets; work-in-progress. * Allow for blocking/modifying/sending client packets to the game server * Add Position struct. Add Read/Write methods to NetworkMessage class for Position. Uncomment Positions from relevant client packets. * Add server packets. * Add support for object instances to network messages. Add support for loading Tibia 11 dat file. Add missing server packets. * Add OutfitInstance class. Add reading/writing OutfitInstances to NetworkMessage class. * Add Creature class. Add reading/writing of Creatures to NetworkMessage class. * Add reading of daily rewards to NetworkMessage class. Need to add class for daily rewards, and writing them to network messages. * Add Imbuing classes. * Add Market offer classes. Add methods to read/write Market offers to the NetworkMessage class. * Add WorldMap classes. Add reading of map packets to the NetworkMessage class. * Get Fields from parsing map packets. * Allow parsing and modifying of server packets. Some packets are exempt from modification because they don't have finished AppendToNetworkMessage methods. * Finish up packets. Finish forwarding packets properly; server packets are exempt from modification until all AppentToNetworkMessage() methods are complete. Fix issue where Math.Max was called instead of Math.Min.
1 parent 1c0ee7f commit 72c6159

304 files changed

Lines changed: 24049 additions & 206 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using OXGaming.TibiaAPI.Utilities;
2+
3+
namespace OXGaming.TibiaAPI.Appearances
4+
{
5+
public class AppearanceInstance
6+
{
7+
public Appearance Type { get; }
8+
9+
public uint Id { get; }
10+
11+
public byte Phase { get; set; }
12+
13+
public AppearanceInstance(uint id, Appearance type)
14+
{
15+
Id = id;
16+
Type = type;
17+
}
18+
}
19+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Linq;
2+
3+
using OXGaming.TibiaAPI.Constants;
4+
5+
namespace OXGaming.TibiaAPI.Appearances
6+
{
7+
public class AppearanceStorage
8+
{
9+
private Utilities.Appearances appearances;
10+
11+
private uint lastObjectId;
12+
private uint lastOutfitId;
13+
14+
public void LoadAppearances(System.IO.FileStream datFile)
15+
{
16+
appearances = Utilities.Appearances.Parser.ParseFrom(datFile);
17+
lastObjectId = appearances.Object.Aggregate((last, current) => last.Id > current.Id ? last : current).Id;
18+
lastOutfitId = appearances.Outfit.Aggregate((last, current) => last.Id > current.Id ? last : current).Id;
19+
}
20+
21+
public ObjectInstance CreateObjectInstance(uint id, uint data)
22+
{
23+
if (id >= (uint)CreatureInstanceType.Creature && id <= lastObjectId)
24+
{
25+
return new ObjectInstance(id, appearances.Object.FirstOrDefault(i => i.Id == id), data);
26+
}
27+
return null;
28+
}
29+
30+
public OutfitInstance CreateOutfitInstance(uint id, byte colorHead, byte colorTorso, byte colorLegs, byte colorDetail, byte addons)
31+
{
32+
if (id >= 0 && id <= lastOutfitId)
33+
{
34+
return new OutfitInstance(id, appearances.Outfit.FirstOrDefault(i => i.Id == id), colorHead, colorTorso, colorLegs, colorDetail, addons);
35+
}
36+
return null;
37+
}
38+
39+
public Utilities.Appearance GetObjectType(uint id)
40+
{
41+
if (id >= (uint)CreatureInstanceType.Creature && id <= lastObjectId)
42+
{
43+
return appearances.Object.FirstOrDefault(o => o.Id == id);
44+
}
45+
return null;
46+
}
47+
48+
public Utilities.Appearance GetOutfitType(uint id)
49+
{
50+
if (id >= 1 && id <= lastOutfitId)
51+
{
52+
appearances.Outfit.FirstOrDefault(i => i.Id == id);
53+
}
54+
return null;
55+
}
56+
}
57+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using OXGaming.TibiaAPI.Utilities;
2+
3+
namespace OXGaming.TibiaAPI.Appearances
4+
{
5+
public class ObjectInstance : AppearanceInstance
6+
{
7+
public uint Data { get; set; }
8+
public uint LootContainerUnknown { get; set; }
9+
10+
public bool IsLootContainer { get; set; } = false;
11+
12+
public ObjectInstance(uint id, Appearance type, uint data = 0) : base(id, type)
13+
{
14+
Data = data;
15+
}
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using OXGaming.TibiaAPI.Utilities;
2+
3+
namespace OXGaming.TibiaAPI.Appearances
4+
{
5+
public class OutfitInstance : AppearanceInstance
6+
{
7+
public byte Addons { get; set; }
8+
public byte ColorDetail { get; set; }
9+
public byte ColorHead { get; set; }
10+
public byte ColorLegs { get; set; }
11+
public byte ColorTorso { get; set; }
12+
13+
public OutfitInstance(uint id, Appearance type, byte colorHead, byte colorTorso, byte colorLegs, byte colorDetail, byte addons) : base(id, type)
14+
{
15+
ColorHead = colorHead;
16+
ColorTorso = colorTorso;
17+
ColorLegs = colorLegs;
18+
ColorDetail = colorDetail;
19+
Addons = addons;
20+
}
21+
}
22+
}

TibiaAPI/Client.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
11
using System;
2+
using System.IO;
23

34
namespace OXGaming.TibiaAPI
45
{
56
public class Client : IDisposable
67
{
7-
private Network.Connection _connection;
8+
public Appearances.AppearanceStorage AppearanceStorage { get; } = new Appearances.AppearanceStorage();
89

9-
public bool StartProxy()
10+
public WorldMap.WorldMapStorage WorldMapStorage { get; } = new WorldMap.WorldMapStorage();
11+
12+
public Network.Connection Proxy { get; }
13+
14+
public Client(string datFileName)
1015
{
11-
if (_connection == null)
16+
if (string.IsNullOrEmpty(datFileName))
1217
{
13-
_connection = new Network.Connection();
18+
throw new ArgumentNullException(nameof(datFileName));
1419
}
1520

16-
return _connection.Start();
21+
if (!File.Exists(datFileName))
22+
{
23+
throw new FileNotFoundException("Tibia dat file not found.", datFileName);
24+
}
25+
26+
using (var datFile = File.OpenRead(datFileName))
27+
{
28+
AppearanceStorage.LoadAppearances(datFile);
29+
}
30+
31+
Proxy = new Network.Connection(this);
32+
}
33+
34+
public bool StartProxy(bool enablePacketParsing = true)
35+
{
36+
return Proxy.Start(enablePacketParsing);
1737
}
1838

1939
public void StopProxy()
2040
{
21-
if (_connection != null)
22-
{
23-
_connection.Stop();
24-
}
41+
Proxy.Stop();
2542
}
2643

2744
#region IDisposable Support
@@ -33,10 +50,7 @@ protected virtual void Dispose(bool disposing)
3350
{
3451
if (disposing)
3552
{
36-
if (_connection != null)
37-
{
38-
_connection.Dispose();
39-
}
53+
Proxy.Dispose();
4054
}
4155

4256
disposedValue = true;

0 commit comments

Comments
 (0)