X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;ds=sidebyside;f=qcsrc%2Flib%2Fnet.qh;h=106f00998c3daac770fbbc562d583ba62441c99a;hb=641c47df604de42c11c68a7d80813b29affcefb2;hp=f73fcf97aa037fb9ce5dc0b099ad710e57b827e2;hpb=f7451c9274afcfbd95930ac123f622ce1615b6b2;p=xonotic%2Fxonotic-data.pk3dir.git diff --git a/qcsrc/lib/net.qh b/qcsrc/lib/net.qh index f73fcf97a..106f00998 100644 --- a/qcsrc/lib/net.qh +++ b/qcsrc/lib/net.qh @@ -1,5 +1,4 @@ -#ifndef NET_H -#define NET_H +#pragma once #include "registry.qh" #include "sort.qh" @@ -7,8 +6,8 @@ .string netname; .int m_id; -.bool(entity this, bool isNew) m_read; -#define NET_HANDLE(id, param) bool Net_Handle_##id(entity this, param) +.bool(entity this, entity sender, bool isNew) m_read; +#define NET_HANDLE(id, param) bool Net_Handle_##id(entity this, entity sender, param) #ifdef CSQC @@ -42,9 +41,8 @@ STATIC_INIT(RegisterTempEntities_renumber) { FOREACH(TempEntities, true, it.m_id #define REGISTER_NET_LINKED(id) \ [[accumulate]] NET_HANDLE(id, bool isnew) \ { \ - this = self; \ - this.sourceLocFile = __FILE__; \ - this.sourceLocLine = __LINE__; \ + this = __self; \ + this.sourceLoc = __FILE__ ":" STR(__LINE__); \ if (!this) isnew = true; \ } \ REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \ @@ -99,17 +97,12 @@ STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); } .int Version; // deprecated, use SendFlags .int SendFlags; - .bool(entity to, int sendflags) SendEntity; - /** return false to remove from the client */ - .bool(entity this, entity to, int sendflags) SendEntity3; - - bool SendEntity_self(entity to, int sendflags) { return self.SendEntity3(self, to, sendflags); } void Net_LinkEntity(entity e, bool docull, float dt, bool(entity this, entity to, int sendflags) sendfunc) { if (e.classname == "") e.classname = "net_linked"; - if (e.model == "" || self.modelindex == 0) + if (e.model == "" || e.modelindex == 0) { vector mi = e.mins; vector ma = e.maxs; @@ -117,8 +110,7 @@ STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); } setsize(e, mi, ma); } - e.SendEntity = SendEntity_self; - e.SendEntity3 = sendfunc; + setSendEntity(e, sendfunc); e.SendFlags = 0xFFFFFF; if (!docull) e.effects |= EF_NODEPTHTEST; @@ -126,36 +118,35 @@ STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); } if (dt) { e.nextthink = time + dt; - e.think = SUB_Remove_self; + setthink(e, SUB_Remove); } } void Net_UnlinkEntity(entity e) { - e.SendEntity = func_null; + setSendEntity(e, func_null); } - .void() uncustomizeentityforclient; + .void(entity this) uncustomizeentityforclient; .float uncustomizeentityforclient_set; - void SetCustomizer(entity e, float() customizer, void() uncustomizer) + void SetCustomizer(entity e, bool(entity this, entity client) customizer, void(entity this) uncustomizer) { - e.customizeentityforclient = customizer; + setcefc(e, customizer); e.uncustomizeentityforclient = uncustomizer; e.uncustomizeentityforclient_set = !!uncustomizer; } void UncustomizeEntitiesRun() { - for (entity e = NULL; (e = findfloat(e, uncustomizeentityforclient_set, 1)); ) - WITH(entity, self, e, e.uncustomizeentityforclient()); + FOREACH_ENTITY_FLOAT(uncustomizeentityforclient_set, true, it.uncustomizeentityforclient(it)); } STRING_ITERATOR(g_buf, string_null, 0); int ReadByte(); - void Net_ClientCommand(string command) + void Net_ClientCommand(entity sender, string command) { // command matches `c2s "(.+)"` string buf = substring(command, argv_start_index(1) + 1, -2); @@ -164,14 +155,14 @@ STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); } for (int C2S; (C2S = ReadByte()) >= 0; ) { entity reader = C2S_Protocol_from(C2S); - if (reader && reader.m_read && reader.m_read(NULL, true)) continue; - LOG_SEVEREF("Net_ClientCommand() with malformed C2S=%d\n", C2S); + if (reader && reader.m_read && reader.m_read(NULL, sender, true)) continue; + LOG_SEVEREF("Net_ClientCommand() with malformed C2S=%d", C2S); return; } g_buf_i--; int expected = strlen(buf); - if (g_buf_i > expected) LOG_WARNINGF("Underflow: %d", g_buf_i - expected); - if (g_buf_i < expected) LOG_WARNINGF("Overrflow: %d", expected - g_buf_i); + if (g_buf_i > expected) LOG_WARNF("Underflow: %d", g_buf_i - expected); + if (g_buf_i < expected) LOG_WARNF("Overrflow: %d", expected - g_buf_i); } #endif @@ -185,7 +176,7 @@ STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); } } MACRO_END #define Net_Reject() \ MACRO_BEGIN { \ - if (this) remove(this); \ + if (this) delete(this); \ } MACRO_END string g_buf; @@ -212,8 +203,56 @@ STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); } } MACRO_END #endif -#define ReadRegistry(r) r##_from(Read_byte()) -#define WriteRegistry(r, to, it) Write_byte(to, it.m_id) +// serialization: new style + +USING(Stream, int); +#if defined(SVQC) + #define stream_reading(stream) false + #define stream_writing(stream) true +#elif defined(CSQC) + #define stream_reading(stream) true + #define stream_writing(stream) false +#endif + +#define serialize(T, stream, ...) serialize_##T(stream, __VA_ARGS__) + +#if defined(SVQC) + #define serialize_byte(stream, this) \ + MACRO_BEGIN \ + WriteByte(stream, this); \ + MACRO_END +#elif defined(CSQC) + #define serialize_byte(stream, this) \ + MACRO_BEGIN \ + this = ReadByte(); \ + MACRO_END +#endif + +#if defined(SVQC) + #define serialize_float(stream, this) \ + MACRO_BEGIN \ + WriteCoord(stream, this); \ + MACRO_END +#elif defined(CSQC) + #define serialize_float(stream, this) \ + MACRO_BEGIN \ + this = ReadCoord(); \ + MACRO_END +#endif + +#define serialize_vector(stream, this) \ + MACRO_BEGIN \ + vector _v = this; \ + serialize_float(stream, _v.x); \ + serialize_float(stream, _v.y); \ + serialize_float(stream, _v.z); \ + this = _v; \ + MACRO_END + +// serialization: old + +#define ReadRegistered(r) r##_from(Read_byte()) +#define WriteRegistered(r, to, it) Write_byte(to, it.m_id) #define Read_byte() ReadByte() #define Write_byte(to, f) WriteByte(to, f) @@ -223,7 +262,7 @@ STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); } void WriteByte(int to, int b) { assert(to == MSG_C2S); - string s; + string s = string_null; yenc_single(b, s); string tmp = strcat(g_buf, s); if (g_buf) strunzone(g_buf); @@ -232,7 +271,9 @@ STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); } #elif defined(SVQC) int ReadByte() { - ydec_single(g_buf, return); + int ret = -1; + ydec_single(g_buf, ret); + return ret; } void WriteByte(int to, int b); #endif @@ -264,28 +305,14 @@ STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); } v += ReadByte(); // note: this is unsigned return v; } - vector ReadInt48_t() - { - vector v; - v.x = ReadInt24_t(); - v.y = ReadInt24_t(); - v.z = 0; - return v; - } - vector ReadInt72_t() - { - vector v; - v.x = ReadInt24_t(); - v.y = ReadInt24_t(); - v.z = ReadInt24_t(); - return v; - } + #define ReadInt48_t() vec3(ReadInt24_t(), ReadInt24_t(), 0) + #define ReadInt72_t() vec3(ReadInt24_t(), ReadInt24_t(), ReadInt24_t()) int _ReadSByte; #define ReadSByte() (_ReadSByte = ReadByte(), (_ReadSByte & BIT(7) ? -128 : 0) + (_ReadSByte & BITS(7))) #define ReadFloat() ReadCoord() - vector ReadVector() { vector v; v.x = ReadFloat(); v.y = ReadFloat(); v.z = ReadFloat(); return v; } - vector ReadVector2D() { vector v; v.x = ReadFloat(); v.y = ReadFloat(); v.z = 0; return v; } + #define ReadVector() vec3(ReadFloat(), ReadFloat(), ReadFloat()) + #define ReadVector2D() vec3(ReadFloat(), ReadFloat(), 0) float ReadApproxPastTime() { @@ -343,21 +370,17 @@ STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); } } // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example) - #define WRITESPECTATABLE_MSG_ONE_VARNAME(varname, statement) \ - entity varname; varname = msg_entity; \ - FOR_EACH_REALCLIENT(msg_entity) \ - if (msg_entity == varname || (msg_entity.classname == STR_SPECTATOR && msg_entity.enemy == varname)) \ - statement msg_entity = varname - #define WRITESPECTATABLE_MSG_ONE(statement) \ - MACRO_BEGIN \ - { \ - WRITESPECTATABLE_MSG_ONE_VARNAME(oldmsg_entity, statement); \ - } MACRO_END - #define WRITESPECTATABLE(msg, statement) \ - if (msg == MSG_ONE) WRITESPECTATABLE_MSG_ONE(statement); \ - else \ - statement float WRITESPECTATABLE_workaround = 0 + #define WRITESPECTATABLE_MSG_ONE(to, statement) MACRO_BEGIN { \ + entity prev = msg_entity; \ + entity dst = to; \ + FOREACH_CLIENT(IS_REAL_CLIENT(it), { \ + if (it == dst || (it.classname == STR_SPECTATOR && it.enemy == dst)) \ + { \ + msg_entity = it; \ + LAMBDA(statement); \ + } \ + }); \ + msg_entity = prev; \ + } MACRO_END #endif #endif - -#endif