Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,7 @@ set_src(GAME_SHARED GLOB src/game
layers.cpp
layers.h
mapitems.h
resource.h
tuning.h
variables.h
version.h
Expand Down Expand Up @@ -1415,6 +1416,8 @@ if(CLIENT)
components/particles.h
components/players.cpp
components/players.h
components/resource.cpp
components/resource.h
components/scoreboard.cpp
components/scoreboard.h
components/skins.cpp
Expand Down Expand Up @@ -1559,6 +1562,8 @@ set_src(GAME_SERVER GLOB_RECURSE src/game/server
gameworld.h
player.cpp
player.h
resource.cpp
resource.h
)
set(GAME_GENERATED_SERVER
src/generated/server_data.cpp
Expand Down
27 changes: 27 additions & 0 deletions datasrc/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,33 @@ def emit_unpack(self):
def emit_unpack_check(self):
return []


class NetRawData(NetVariable):
def emit_declaration(self):
return [f"const void *{self.name};", f"int {self.name}Size;"]
def emit_unpack(self):
return [f"pMsg->{self.name}Size = pUnpacker->GetInt();", f"pMsg->{self.name} = pUnpacker->GetRaw(pMsg->{self.name}Size);"]
def emit_pack(self):
return [f"pPacker->AddInt({self.name}Size);", f"pPacker->AddRaw({self.name}, {self.name}Size);"]

class NetRawDataFixed(NetVariable):
def __init__(self, name, data_size, default=None):
NetVariable.__init__(self,name,default=default)
self.data_size = data_size
def emit_declaration(self):
return [f"const void *{self.name};"]
def emit_unpack(self):
return [f"pMsg->{self.name} = pUnpacker->GetRaw({self.data_size});"]
def emit_pack(self):
return [f"pPacker->AddRaw({self.name}, {self.data_size});"]

class NetRawDataFixedSnapshot(NetVariable):
def __init__(self, name, data_size, default=None):
NetVariable.__init__(self,name,default=default)
self.data_size = data_size
def emit_declaration(self):
return [f"unsigned char {self.name}[{self.data_size}];"]

class NetString(NetVariable):
def emit_declaration(self):
return ["const char *%s;"%self.name]
Expand Down
49 changes: 48 additions & 1 deletion datasrc/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@

GamePredictionFlags = Flags("GAMEPREDICTIONFLAG", ["EVENT", "INPUT"])

Resources = Enum("RESOURCE", ["SOUND", "IMAGE"])

RawHeader = '''

#include <base/hash.h>
#include <base/uuid.h>
#include <engine/message.h>
#include <engine/shared/protocol_ex.h>

Expand Down Expand Up @@ -75,6 +79,7 @@
Votes,
ChatModes,
GameMsgIDs,
Resources,
]

Flags = [
Expand Down Expand Up @@ -274,7 +279,32 @@

NetObjectEx("GameDataPrediction", "game-data-prediction@netobj.teeworlds.wiki", [
NetFlag("m_PredictionFlags", GamePredictionFlags),
])
]),

NetEventEx("CustomSoundWorld:Common", "custom-sound-world@netevent.teeworlds.wiki", [
NetRawDataFixedSnapshot("m_Uuid", "sizeof(Uuid)"),
]),

NetObjectEx("CustomObject", "custom-entity@netobj.teeworlds.wiki", [
NetRawDataFixedSnapshot("m_Uuid", "sizeof(Uuid)"),
NetIntAny("m_X"),
NetIntAny("m_Y"),
]),

NetObjectEx("CustomImageEntity:CustomObject", "custom-image-entity@netobj.teeworlds.wiki", [
NetIntAny("m_Angle"),
NetIntAny("m_Width"),
NetIntAny("m_Height"),
]),

NetObjectEx("CustomSoundEntity:CustomObject", "custom-sound-entity@netobj.teeworlds.wiki", [
NetIntAny("m_Vol"),
NetIntAny("m_Offset"),
]),

NetObjectEx("CharacterGameTexture", "character-game-texture@netobj.teeworlds.wiki", [
NetRawDataFixedSnapshot("m_Uuid", "sizeof(Uuid)"),
]),
]

Messages = [
Expand Down Expand Up @@ -490,4 +520,21 @@
NetStringStrict("m_Arguments")
]),

NetMessageEx("Sv_CustomResource", "custom-resource@netmsg.teeworlds.wiki", [
NetRawDataFixed("m_Uuid", "sizeof(Uuid)"),
NetEnum("m_Type", Resources),
NetStringStrict("m_Name"),
NetIntAny("m_Crc"),
NetIntRange("m_ChunkPerRequest", 0, 'max_int'),
NetIntRange("m_Size", 0, 'max_int'),
NetRawDataFixed("m_Sha256", "sizeof(SHA256_DIGEST)"),
]),
NetMessageEx("Sv_CustomResourceData", "custom-resource-data@netmsg.teeworlds.wiki", [
NetRawDataFixed("m_Uuid", "sizeof(Uuid)"),
NetIntRange("m_ChunkIndex", 0, 'max_int'),
NetRawData("m_Data"),
]),
NetMessageEx("Cl_ReqeustCustomResource", "request-custom-resource@netmsg.teeworlds.wiki", [
NetRawDataFixed("m_Uuid", "sizeof(Uuid)"),
]),
]
10 changes: 10 additions & 0 deletions src/base/uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ inline bool operator!=(const Uuid &that, const Uuid &other)
{
return !(that == other);
}

inline bool operator<(const Uuid &that, const Uuid &other)
{
return uuid_comp(that, other) < 0;
}

inline bool operator<=(const Uuid &that, const Uuid &other)
{
return uuid_comp(that, other) < 1;
}
#endif

#endif // BASE_UUID_H
77 changes: 53 additions & 24 deletions src/engine/client/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,10 @@ void CSound::RateConvert(int SampleID)
pSample->m_NumFrames = NumFrames;
}

ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
ISound::CSampleHandle CSound::LoadOpusMemory(const char *pContext, const unsigned char *pData, int DataSize)
{
CSample *pSample;
int SampleID = -1;

// don't waste memory on sound when we are stress testing
#ifdef CONF_DEBUG
if(m_pConfig->m_DbgStress)
Expand All @@ -349,23 +348,16 @@ ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)

if(!m_pStorage)
return CSampleHandle();

lock_wait(m_SoundLock);
unsigned char *pFileData;
unsigned FileSize;
if(!m_pStorage->ReadFile(pFilename, IStorage::TYPE_ALL, (void **) &pFileData, &FileSize))
{
dbg_msg("sound/opus", "failed to open file. filename='%s'", pFilename);
lock_unlock(m_SoundLock);

if(!pContext || !pData || DataSize <= 0)
return CSampleHandle();
}

lock_wait(m_SoundLock);
int Error = 0;
OggOpusFile *pOpusFile = op_open_memory(pFileData, FileSize, &Error);
OggOpusFile *pOpusFile = op_open_memory(pData, DataSize, &Error);
if(!pOpusFile)
{
dbg_msg("sound/opus", "failed to open opus file '%s': error %d", pFilename, Error);
mem_free(pFileData);
dbg_msg("sound/opus", "failed to open opus '%s': error %d", pContext, Error);
lock_unlock(m_SoundLock);
return CSampleHandle();
}
Expand All @@ -374,19 +366,17 @@ ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
;
if(NumChannels < 0 || NumChannels > 2)
{
dbg_msg("sound/opus", "only mono/stereo supported. channels=%d, file='%s'", NumChannels, pFilename);
dbg_msg("sound/opus", "only mono/stereo supported. channels=%d, file='%s'", NumChannels, pContext);
op_free(pOpusFile);
mem_free(pFileData);
lock_unlock(m_SoundLock);
return CSampleHandle();
}

int TotalSamples = op_pcm_total(pOpusFile, -1);
if(TotalSamples < 0)
{
dbg_msg("sound/opus", "failed to get number of samples, error %d. file='%s'", TotalSamples, pFilename);
dbg_msg("sound/opus", "failed to get number of samples, error %d. file='%s'", TotalSamples, pContext);
op_free(pOpusFile);
mem_free(pFileData);
lock_unlock(m_SoundLock);
return CSampleHandle();
}
Expand All @@ -395,7 +385,6 @@ ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
if(SampleID < 0)
{
op_free(pOpusFile);
mem_free(pFileData);
lock_unlock(m_SoundLock);
return CSampleHandle();
}
Expand All @@ -404,7 +393,6 @@ ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
if(!pSample->m_pData)
{
op_free(pOpusFile);
mem_free(pFileData);
lock_unlock(m_SoundLock);
return CSampleHandle();
}
Expand All @@ -417,8 +405,7 @@ ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
{
mem_free(pSample->m_pData);
op_free(pOpusFile);
mem_free(pFileData);
dbg_msg("sound/opus", "op_read error %d at %d. file='%s'", Read, Pos, pFilename);
dbg_msg("sound/opus", "op_read error %d at %d. file='%s'", Read, Pos, pContext);
return CSampleHandle();
}
else if(Read == 0) // EOF
Expand All @@ -428,7 +415,6 @@ ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
}

op_free(pOpusFile);
mem_free(pFileData);

pSample->m_Channels = NumChannels;
pSample->m_Rate = 48000;
Expand All @@ -438,13 +424,56 @@ ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
pSample->m_PausedAt = 0;

if(m_pConfig->m_Debug)
dbg_msg("sound/opus", "loaded %s (%d samples, %d channels)", pFilename, TotalSamples, NumChannels);
dbg_msg("sound/opus", "loaded %s (%d samples, %d channels)", pContext, TotalSamples, NumChannels);

RateConvert(SampleID);
lock_unlock(m_SoundLock);
return CreateSampleHandle(SampleID);
}

ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
{
// don't waste memory on sound when we are stress testing
#ifdef CONF_DEBUG
if(m_pConfig->m_DbgStress)
return CSampleHandle();
#endif

// no need to load sound when we are running with no sound
if(!m_SoundEnabled)
return CSampleHandle();

if(!m_pStorage)
return CSampleHandle();

unsigned char *pFileData;
unsigned FileSize;
if(!m_pStorage->ReadFile(pFilename, IStorage::TYPE_ALL, (void **) &pFileData, &FileSize))
{
dbg_msg("sound/opus", "failed to open file. filename='%s'", pFilename);
return CSampleHandle();
}

ISound::CSampleHandle Sample = LoadOpusMemory(pFilename, pFileData, FileSize);
mem_free(pFileData);
return Sample;
}

bool CSound::UnloadSample(CSampleHandle *pSampleID)
{
if(!pSampleID)
return false;

if(m_aSamples[pSampleID->Id()].m_pData)
{
mem_free(m_aSamples[pSampleID->Id()].m_pData);
m_aSamples[pSampleID->Id()].m_pData = 0;
pSampleID->Invalidate();
return true;
}
return false;
}

void CSound::SetListenerPos(float x, float y)
{
m_CenterX = (int) x;
Expand Down
2 changes: 2 additions & 0 deletions src/engine/client/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class CSound : public IEngineSound

virtual bool IsSoundEnabled() { return m_SoundEnabled != 0; }

virtual CSampleHandle LoadOpusMemory(const char *pContext, const unsigned char *pData, int DataSize);
virtual CSampleHandle LoadOpus(const char *pFilename);
virtual bool UnloadSample(CSampleHandle *pSampleID);

virtual void SetListenerPos(float x, float y);
virtual void SetChannelVolume(int ChannelID, float Vol);
Expand Down
1 change: 1 addition & 0 deletions src/engine/shared/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class CStorage : public IStorage
fs_makedir(GetPath(TYPE_SAVE, "screenshots/auto", aPath, sizeof(aPath)));
fs_makedir(GetPath(TYPE_SAVE, "maps", aPath, sizeof(aPath)));
fs_makedir(GetPath(TYPE_SAVE, "downloadedmaps", aPath, sizeof(aPath)));
fs_makedir(GetPath(TYPE_SAVE, "downloadedres", aPath, sizeof(aPath)));
fs_makedir(GetPath(TYPE_SAVE, "skins", aPath, sizeof(aPath)));
fs_makedir(GetPath(TYPE_SAVE, "editor", aPath, sizeof(aPath)));
}
Expand Down
3 changes: 3 additions & 0 deletions src/engine/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ class ISound : public IInterface

bool IsValid() const { return Id() >= 0; }
int Id() const { return m_Id; }
void Invalidate() { m_Id = -1; }
};

virtual bool IsSoundEnabled() = 0;

virtual CSampleHandle LoadOpusMemory(const char *pContext, const unsigned char *pData, int DataSize) = 0;
virtual CSampleHandle LoadOpus(const char *pFilename) = 0;
virtual bool UnloadSample(CSampleHandle *pSampleID) = 0;

virtual void SetChannelVolume(int ChannelID, float Volume) = 0;
virtual void SetListenerPos(float x, float y) = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/game/client/components/players.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void CPlayers::RenderHook(
// draw hook
if(Prev.m_HookState > 0 && Player.m_HookState > 0)
{
Graphics()->TextureSet(g_pData->m_aImages[IMAGE_GAME].m_Id);
Graphics()->TextureSet(m_pClient->m_Snap.m_aCharacters[ClientID].m_GameTexture);
Graphics()->QuadsBegin();

vec2 HookPos;
Expand Down Expand Up @@ -195,7 +195,7 @@ void CPlayers::RenderPlayer(
// draw gun
if(Player.m_Weapon >= 0)
{
Graphics()->TextureSet(g_pData->m_aImages[IMAGE_GAME].m_Id);
Graphics()->TextureSet(m_pClient->m_Snap.m_aCharacters[ClientID].m_GameTexture);
Graphics()->QuadsBegin();
Graphics()->QuadsSetRotation(State.GetAttach()->m_Angle * pi * 2 + Angle);

Expand Down
Loading
Loading