Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
@@ -0,0 +1,105 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "LZ4Compressor.h"
#include <lz4.h>
#include <lz4hc.h>
namespace MultiplayerCompression
{
size_t LZ4Compressor::GetMaxChunkSize(size_t maxCompSize) const
{
return maxCompSize;
}
size_t LZ4Compressor::GetMaxCompressedBufferSize(size_t uncompSize) const
{
return LZ4_compressBound(uncompSize);
}
AzNetworking::CompressorError LZ4Compressor::Compress
(
const void* uncompData,
size_t uncompSize,
void* compData,
[[maybe_unused]] size_t compDataSize,
size_t& compSize
)
{
if (uncompData == nullptr)
{
// LZ4 actually never checks for this
AZ_Warning("Multiplayer Compressor", false, "Input buffer is uninitialized");
return AzNetworking::CompressorError::Uninitialized;
}
if (compData == nullptr)
{
// LZ4 actually never checks for this
AZ_Warning("Multiplayer Compressor", false, "Output buffer is uninitialized");
return AzNetworking::CompressorError::Uninitialized;
}
const int compWorstCaseSize = LZ4_compressBound(uncompSize);
if (compWorstCaseSize == 0)
{
AZ_Warning("Multiplayer Compressor", false, "Input size (%lu) passed to Compress() is greater than max allowed (%lu)", uncompSize, LZ4_MAX_INPUT_SIZE);
return AzNetworking::CompressorError::InsufficientBuffer;
}
AZ_Warning("Multiplayer Compressor", compDataSize >= compWorstCaseSize, "Outbuffer size (%lu B) passed to Compress() is less than estimated worst case (%lu B)", compDataSize, compWorstCaseSize);
// Note that this returns a non-negative int so we are narrowing into a size_t here
compSize = LZ4_compressHC(reinterpret_cast<const char*>(uncompData), reinterpret_cast<char*>(compData), uncompSize);
if (compSize == 0)
{
// LZ4_compressHC returns a zero value for corrupt data and insufficient buffer
AZ_Warning("Multiplayer Compressor", false, "Compression failed for uncompSize:(%lu B) compDataSize:(%lu B) compSize:(%lu B)", uncompSize, compDataSize, compSize);
return AzNetworking::CompressorError::CorruptData;
}
return AzNetworking::CompressorError::Ok;
}
AzNetworking::CompressorError LZ4Compressor::Decompress(const void* compData, size_t compDataSize, void* uncompData, size_t uncompDataSize, size_t& consumedSizeOut, size_t& uncompSizeOut)
{
if (uncompData == nullptr)
{
// LZ4 actually never checks for this
AZ_Warning("Multiplayer Compressor", false, "Input buffer is uninitialized");
return AzNetworking::CompressorError::Uninitialized;
}
if (compData == nullptr)
{
// LZ4 actually never checks for this
AZ_Warning("Multiplayer Compressor", false, "Output buffer is uninitialized");
return AzNetworking::CompressorError::Uninitialized;
}
const int uncompSize = LZ4_decompress_safe(reinterpret_cast<const char*>(compData), reinterpret_cast<char*>(uncompData), compDataSize, uncompDataSize);
consumedSizeOut = compDataSize;
if (uncompSize < 0)
{
// LZ4_decompress_safe returns a negative value for corrupt data and insufficient buffer
AZ_Warning("Multiplayer Compressor", false, "Decompression failed for compDataSize:(%lu B) uncompDataSize:(%lu B) uncompSize:(%d B)", compDataSize, uncompDataSize, uncompSize);
return AzNetworking::CompressorError::CorruptData;
}
// Assign into the outbound size_t after validating the negative error case
uncompSizeOut = uncompSize;
return AzNetworking::CompressorError::Ok;
}
}
@@ -0,0 +1,46 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/Memory/SystemAllocator.h>
#include <AzNetworking/Framework/ICompressor.h>
namespace MultiplayerCompression
{
static const char* CompressorName = "LZ4";
static const AzNetworking::CompressorType CompressorType = aznumeric_cast<AzNetworking::CompressorType>(static_cast<AZ::u32>(AZ::Crc32(CompressorName)));
/**
* Implements an LZ4 Compressor against GridMate's Compressor interface for use with the Multiplayer Gem.
* Handles edge and error cases specific to LZ4 that are otherwise not covered in GridMate Carrier
* (where a Compressor is applied).
*/
class LZ4Compressor
: public AzNetworking::ICompressor
{
public:
AZ_CLASS_ALLOCATOR(LZ4Compressor, AZ::SystemAllocator, 0);
LZ4Compressor() = default;
const char* GetName() const { return CompressorName; }
AzNetworking::CompressorType GetType() const { return CompressorType; };
bool Init() { return true; }
size_t GetMaxChunkSize(size_t maxCompSize) const;
size_t GetMaxCompressedBufferSize(size_t uncompSize) const;
AzNetworking::CompressorError Compress(const void* uncompData, size_t uncompSize, void* compData, size_t compDataSize, size_t& compSize);
AzNetworking::CompressorError Decompress(const void* compData, size_t compDataSize, void* uncompData, size_t uncompDataSize, size_t& consumedSize, size_t& uncompSize);
};
}
@@ -0,0 +1,24 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "MultiplayerCompressionFactory.h"
#include "LZ4Compressor.h"
#include <AzCore/std/smart_ptr/unique_ptr.h>
namespace MultiplayerCompression
{
AZStd::unique_ptr<AzNetworking::ICompressor> MultiplayerCompressionFactory::Create()
{
return AZStd::make_unique<LZ4Compressor>();
}
}
@@ -0,0 +1,30 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/Component/Component.h>
#include <AzCore/std/smart_ptr/unique_ptr.h>
#include <AzNetworking/Framework/ICompressor.h>
namespace MultiplayerCompression
{
class MultiplayerCompressionFactory
: public AzNetworking::ICompressorFactory
{
public:
/*
* Instantiate a new compressor
*/
AZStd::unique_ptr<AzNetworking::ICompressor> Create() override;
};
}
@@ -0,0 +1,51 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/Module/Module.h>
#include "MultiplayerCompressionSystemComponent.h"
namespace MultiplayerCompression
{
class MultiplayerCompressionModule
: public AZ::Module
{
public:
AZ_RTTI(MultiplayerCompressionModule, "{939AFA0D-CFBC-4910-88F3-A0CF429307E4}", AZ::Module);
AZ_CLASS_ALLOCATOR(MultiplayerCompressionModule, AZ::SystemAllocator, 0);
MultiplayerCompressionModule()
: AZ::Module()
{
// Push results of MultiplayerCompressionSystemComponent::CreateDescriptor() into m_descriptors here.
m_descriptors.insert(m_descriptors.end(), {
MultiplayerCompressionSystemComponent::CreateDescriptor(),
});
}
/**
* Add required SystemComponents to the SystemEntity.
*/
AZ::ComponentTypeList GetRequiredSystemComponents() const override
{
return AZ::ComponentTypeList{
azrtti_typeid<MultiplayerCompressionSystemComponent>(),
};
}
};
}
// DO NOT MODIFY THIS LINE UNLESS YOU RENAME THE GEM
// The first parameter should be GemName_GemIdLower
// The second should be the fully qualified name of the class above
AZ_DECLARE_MODULE_CLASS(MultiplayerCompression_1d353c8ca3c74ed193fd6c6783ae41cc, MultiplayerCompression::MultiplayerCompressionModule)
@@ -0,0 +1,83 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/std/smart_ptr/make_shared.h>
#include "MultiplayerCompressionSystemComponent.h"
#include "LZ4Compressor.h"
#include "MultiplayerCompressionFactory.h"
namespace MultiplayerCompression
{
void MultiplayerCompressionSystemComponent::Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
{
serialize->Class<MultiplayerCompressionSystemComponent, AZ::Component>()
->Version(0)
;
if (AZ::EditContext* ec = serialize->GetEditContext())
{
ec->Class<MultiplayerCompressionSystemComponent>("MultiplayerCompression", "Provides packet compression via an open source library for the Multiplayer Gem")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System"))
->Attribute(AZ::Edit::Attributes::AutoExpand, true)
;
}
}
}
void MultiplayerCompressionSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
{
provided.push_back(AZ_CRC("MultiplayerCompressionService"));
}
void MultiplayerCompressionSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
{
incompatible.push_back(AZ_CRC("MultiplayerCompressionService"));
}
void MultiplayerCompressionSystemComponent::Init()
{
m_multiplayerCompressionFactory = AZStd::make_shared<MultiplayerCompressionFactory>();
}
void MultiplayerCompressionSystemComponent::Activate()
{
MultiplayerCompressionRequestBus::Handler::BusConnect();
}
void MultiplayerCompressionSystemComponent::Deactivate()
{
MultiplayerCompressionRequestBus::Handler::BusDisconnect();
m_multiplayerCompressionFactory.reset();
}
AZStd::string MultiplayerCompressionSystemComponent::GetCompressorName()
{
return CompressorName;
}
AzNetworking::CompressorType MultiplayerCompressionSystemComponent::GetCompressorType()
{
return CompressorType;
}
AZStd::shared_ptr<AzNetworking::ICompressorFactory> MultiplayerCompressionSystemComponent::GetCompressionFactory()
{
return m_multiplayerCompressionFactory;
}
}
@@ -0,0 +1,57 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/Component/Component.h>
#include <AzCore/std/containers/unordered_set.h>
#include <MultiplayerCompression/MultiplayerCompressionBus.h>
#include <MultiplayerCompressionFactory.h>
namespace MultiplayerCompression
{
/**
* System component whose sole purpose is to own a compression factory and expose it via EBUS
* so GridMate/Multiplayer Gem can easily ingest the compressor.
*/
class MultiplayerCompressionSystemComponent
: public AZ::Component
, protected MultiplayerCompressionRequestBus::Handler
{
public:
AZ_COMPONENT(MultiplayerCompressionSystemComponent, "{C3099AC9-47A6-41D2-8928-F38F904BAC1B}");
static void Reflect(AZ::ReflectContext* context);
static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
protected:
////////////////////////////////////////////////////////////////////////
// MultiplayerCompressionRequestBus interface implementation
AZStd::string GetCompressorName() override;
AzNetworking::CompressorType GetCompressorType() override;
AZStd::shared_ptr<AzNetworking::ICompressorFactory> GetCompressionFactory() override;
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// AZ::Component interface implementation
void Init() override;
void Activate() override;
void Deactivate() override;
////////////////////////////////////////////////////////////////////////
private:
AZStd::shared_ptr<MultiplayerCompressionFactory> m_multiplayerCompressionFactory;
};
}