diff --git a/src/ServerInfo.h b/src/ServerInfo.h new file mode 100644 index 0000000..57a6a15 --- /dev/null +++ b/src/ServerInfo.h @@ -0,0 +1,15 @@ +#import + +#include + +@interface ServerInfo: OFObject +@property (readonly, nonatomic) OFString *name; +@property (copy, nonatomic) OFString *full; +@property (copy, nonatomic) OFString *map; +@property (copy, nonatomic) OFString *sdesc; +@property (nonatomic) int mode, numplayers, ping, protocol, minremain; +@property (nonatomic) ENetAddress address; + +- (instancetype)init OF_UNAVAILABLE; +- (instancetype)initWithName:(OFString *)name; +@end diff --git a/src/ServerInfo.mm b/src/ServerInfo.mm new file mode 100644 index 0000000..229d464 --- /dev/null +++ b/src/ServerInfo.mm @@ -0,0 +1,37 @@ +#import "ServerInfo.h" + +#include "cube.h" + +@implementation ServerInfo +- (instancetype)initWithName:(OFString *)name +{ + self = [super init]; + + _name = [name copy]; + _full = @""; + _mode = 0; + _numplayers = 0; + _ping = 9999; + _protocol = 0; + _minremain = 0; + _map = @""; + _sdesc = @""; + _address.host = ENET_HOST_ANY; + _address.port = CUBE_SERVINFO_PORT; + + return self; +} + +- (OFComparisonResult)compare:(id)otherObject +{ + if (![otherObject isKindOfClass:ServerInfo.class]) + @throw [OFInvalidArgumentException exception]; + + if (_ping > [otherObject ping]) + return OFOrderedDescending; + if (_ping < [otherObject ping]) + return OFOrderedAscending; + + return [_name compare:[otherObject name]]; +} +@end diff --git a/src/meson.build b/src/meson.build index aa03627..0363976 100644 --- a/src/meson.build +++ b/src/meson.build @@ -11,6 +11,7 @@ executable('client', 'Menu.m', 'MenuItem.m', 'Projectile.m', + 'ServerInfo.mm', 'Variable.mm', 'client.mm', 'clientextras.mm', diff --git a/src/serverbrowser.mm b/src/serverbrowser.mm index 9fa6162..d9502da 100644 --- a/src/serverbrowser.mm +++ b/src/serverbrowser.mm @@ -4,6 +4,8 @@ #include "SDL_thread.h" #include "cube.h" +#import "ServerInfo.h" + @interface ResolverThread: OFThread { volatile bool _stop; @@ -167,30 +169,6 @@ resolvercheck(OFString **name, ENetAddress *address) return false; } -@interface ServerInfo: OFObject -@property (nonatomic) OFString *name; -@property (nonatomic) OFString *full; -@property (nonatomic) OFString *map; -@property (nonatomic) OFString *sdesc; -@property (nonatomic) int mode, numplayers, ping, protocol, minremain; -@property (nonatomic) ENetAddress address; -@end - -@implementation ServerInfo -- (OFComparisonResult)compare:(id)otherObject -{ - if (![otherObject isKindOfClass:ServerInfo.class]) - @throw [OFInvalidArgumentException exception]; - - if (_ping > [otherObject ping]) - return OFOrderedDescending; - if (_ping < [otherObject ping]) - return OFOrderedAscending; - - return [_name compare:[otherObject name]]; -} -@end - static OFMutableArray *servers; static ENetSocket pingsock = ENET_SOCKET_NULL; static int lastinfo = 0; @@ -209,24 +187,11 @@ addserver(OFString *servername) if ([si.name isEqual:servername]) return; - ServerInfo *si = [[ServerInfo alloc] init]; - si.name = servername; - si.full = @""; - si.mode = 0; - si.numplayers = 0; - si.ping = 9999; - si.protocol = 0; - si.minremain = 0; - si.map = @""; - si.sdesc = @""; - ENetAddress address = { .host = ENET_HOST_ANY, - .port = CUBE_SERVINFO_PORT }; - si.address = address; - if (servers == nil) servers = [[OFMutableArray alloc] init]; - [servers addObject:si]; + [servers + addObject:[[ServerInfo alloc] initWithName:servername]]; } }