Merge pull request #65 from iw4x/feature/weapon

Feature/weapon
This commit is contained in:
Louve 2023-11-26 11:04:51 +01:00 committed by GitHub
commit 558ecb6e7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 627 additions and 452 deletions

@ -1 +1 @@
Subproject commit aaf4455bdac1ccd07c6eeeac993b7800124244c0
Subproject commit 0baf29352008266683758170e9687c2e8ab95f13

View File

@ -349,9 +349,13 @@ namespace Components
AssetHandler::TypeCallbacks[type] = callback;
}
void AssetHandler::OnLoad(Utils::Slot<AssetHandler::RestrictCallback> callback)
std::function<void()> AssetHandler::OnLoad(Utils::Slot<AssetHandler::RestrictCallback> callback)
{
AssetHandler::RestrictSignal.connect(callback);
return [callback](){
AssetHandler::RestrictSignal.disconnect(callback);
};
}
void AssetHandler::ClearRelocations()

View File

@ -23,7 +23,7 @@ namespace Components
~AssetHandler();
static void OnFind(Game::XAssetType type, Utils::Slot<Callback> callback);
static void OnLoad(Utils::Slot<RestrictCallback> callback);
static std::function<void()> OnLoad(Utils::Slot<RestrictCallback> callback);
static void ClearRelocations();
static void Relocate(void* start, void* to, DWORD size = 4);

View File

@ -3,9 +3,9 @@
namespace Assets
{
void ITracerDef::load(Game::XAssetHeader* /*header*/, const std::string& /*name*/, Components::ZoneBuilder::Zone* /*builder*/)
void ITracerDef::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
{
// don't load from filesystem right now
header->tracerDef = builder->getIW4OfApi()->read<Game::TracerDef>(Game::XAssetType::ASSET_TYPE_TRACER, name);
}
void ITracerDef::mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder)

View File

@ -3,8 +3,16 @@
namespace Assets
{
void IWeapon::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* /*builder*/)
void IWeapon::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
{
header->weapon = builder->getIW4OfApi()->read<Game::WeaponCompleteDef>(Game::XAssetType::ASSET_TYPE_WEAPON, name);
if (header->weapon)
{
return;
}
// Try loading raw weapon
if (Components::FileSystem::File(std::format("weapons/mp/{}", name)))
{
@ -121,7 +129,10 @@ namespace Assets
if (asset->weapDef->projIgnitionEffect) builder->loadAsset(Game::XAssetType::ASSET_TYPE_FX, asset->weapDef->projIgnitionEffect);
if (asset->weapDef->turretOverheatEffect) builder->loadAsset(Game::XAssetType::ASSET_TYPE_FX, asset->weapDef->turretOverheatEffect);
#define LoadWeapSound(sound) if (asset->weapDef->##sound##) builder->loadAsset(Game::XAssetType::ASSET_TYPE_SOUND, asset->weapDef->##sound##)
// They are not subassets, because they don't get loaded automatically
#define LoadWeapSound(sound) if (asset->weapDef->##sound##) builder->loadAsset(Game::XAssetType::ASSET_TYPE_SOUND, asset->weapDef->##sound##, false)
LoadWeapSound(pickupSound);
LoadWeapSound(pickupSoundPlayer);
@ -193,6 +204,33 @@ namespace Assets
LoadWeapSound(missileConeSoundAlias);
LoadWeapSound(missileConeSoundAliasAtBase);
for (size_t i = 0; i < 37; i++)
{
{
const auto anim = asset->weapDef->szXAnimsLeftHanded[i];
if (anim && strnlen(anim, 1) > 0) {
builder->loadAssetByName(Game::XAssetType::ASSET_TYPE_XANIMPARTS, anim, false);
}
}
{
const auto anim = asset->weapDef->szXAnimsRightHanded[i];
if (anim && strnlen(anim, 1) > 0) {
builder->loadAssetByName(Game::XAssetType::ASSET_TYPE_XANIMPARTS, anim, false);
}
}
{
const auto anim = asset->szXAnims[i];
if (anim && strnlen(anim, 1) > 0) {
builder->loadAssetByName(Game::XAssetType::ASSET_TYPE_XANIMPARTS, anim, false);
}
}
}
if (asset->szAltWeaponName && *asset->szAltWeaponName != 0 && !asset->dpadIcon) // A very bad way to check if this is already an alt
{
builder->loadAssetByName(Game::XAssetType::ASSET_TYPE_WEAPON, asset->szAltWeaponName, false);
}
}
void IWeapon::writeWeaponDef(Game::WeaponDef* def, Components::ZoneBuilder::Zone* builder, Utils::Stream* buffer)
@ -782,4 +820,26 @@ namespace Assets
buffer->popBlock();
}
IWeapon::IWeapon()
{
Components::Command::Add("dumpweapon", [](const Components::Command::Params* params)
{
if (params->size() < 2) return;
std::string weapon = params->get(1);
const auto header = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_WEAPON, weapon.data());
if (header.data)
{
Components::ZoneBuilder::RefreshExporterWorkDirectory();
Components::ZoneBuilder::GetExporter()->write(Game::XAssetType::ASSET_TYPE_WEAPON, header.data);
}
else
{
Components::Logger::Print("Could not find weapon {}!\n", weapon);
}
}
);
}
}

View File

@ -6,6 +6,7 @@ namespace Assets
{
public:
Game::XAssetType getType() override { return Game::XAssetType::ASSET_TYPE_WEAPON; }
IWeapon();
void save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) override;
void mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) override;

View File

@ -1,6 +1,8 @@
#include <STDInclude.hpp>
#include "ClientCommand.hpp"
#include "Weapon.hpp"
#include "GSC/Script.hpp"
using namespace Utils::String;
@ -382,7 +384,14 @@ namespace Components
Game::XModel* model = nullptr;
if (ent->model)
{
model = Game::G_GetModel(ent->model);
if (Components::Weapon::GModelIndexHasBeenReallocated)
{
model = Components::Weapon::G_ModelIndexReallocated[ent->model];
}
else
{
model = Game::G_GetModel(ent->model);
}
}
Game::vec3_t point, angles;

View File

@ -194,7 +194,16 @@ namespace Components::GSC
unsigned int ScriptError::Scr_GetPrevSourcePos(const char* codePos, unsigned int index)
{
return ScrParserGlob.sourcePosLookup[index + Scr_GetPrevSourcePosOpcodeLookup(codePos)->sourcePosIndex].sourcePos;
const auto prevIndex = Scr_GetPrevSourcePosOpcodeLookup(codePos)->sourcePosIndex;
const auto sPos = ScrParserGlob.sourcePosLookup[index + prevIndex].sourcePos;
// I'm not sure why this is necessary - when given a valid codePos, sometimes
// the sourcePos ends up being a negative number (got a case where it was -2)
// which will output a giantic unsigned number and crash
// So we make sure it's not gonna happen here by clamping the number
const auto uPos = static_cast<unsigned int>(std::max(0, static_cast<int>(sPos)));
return uPos;
}
Game::OpcodeLookup* ScriptError::Scr_GetPrevSourcePosOpcodeLookup(const char* codePos)
@ -326,7 +335,9 @@ namespace Components::GSC
if (Game::scrVarPub->programBuffer && Scr_IsInOpcodeMemory(codePos))
{
auto bufferIndex = Scr_GetSourceBuffer(codePos - 1);
Scr_PrintSourcePos(channel, ScrParserPub.sourceBufferLookup[bufferIndex].buf, ScrParserPub.sourceBufferLookup[bufferIndex].sourceBuf, Scr_GetPrevSourcePos(codePos - 1, index));
const auto prevSourcePos = Scr_GetPrevSourcePos(codePos - 1, index);
Scr_PrintSourcePos(channel, ScrParserPub.sourceBufferLookup[bufferIndex].buf, ScrParserPub.sourceBufferLookup[bufferIndex].sourceBuf, prevSourcePos);
return;
}
}

View File

@ -6,6 +6,8 @@
namespace Components
{
const Game::dvar_t* Weapon::BGWeaponOffHandFix;
Game::XModel* Weapon::G_ModelIndexReallocated[G_MODELINDEX_LIMIT];
bool Weapon::GModelIndexHasBeenReallocated;
Game::WeaponCompleteDef* Weapon::LoadWeaponCompleteDef(const char* name)
{
@ -433,6 +435,21 @@ namespace Components
Utils::Hook::Set<DWORD>(0x4F76FB, 0x12EC + (sizeof(bg_sharedAmmoCaps) - (1200 * 4)));
// Move arg4 pointers
Utils::Hook::Set<DWORD>(0x4F7630, 0x12DC + (sizeof(bg_sharedAmmoCaps) - (1200 * 4)));
// Reallocate G_ModelIndex
Utils::Hook::Set(0x420654 + 3, G_ModelIndexReallocated);
Utils::Hook::Set(0x43BCE4 + 3, G_ModelIndexReallocated);
Utils::Hook::Set(0x44F27B + 3, G_ModelIndexReallocated);
Utils::Hook::Set(0x479087 + 1, G_ModelIndexReallocated);
Utils::Hook::Set(0x48069D + 3, G_ModelIndexReallocated);
Utils::Hook::Set(0x48F088 + 3, G_ModelIndexReallocated);
Utils::Hook::Set(0x4F457C + 3, G_ModelIndexReallocated);
Utils::Hook::Set(0x5FC762 + 3, G_ModelIndexReallocated);
Utils::Hook::Set(0x5FC7BE + 3, G_ModelIndexReallocated);
Utils::Hook::Set<DWORD>(0x44F256 + 2, G_MODELINDEX_LIMIT);
GModelIndexHasBeenReallocated = true;
}
void* Weapon::LoadNoneWeaponHook()
@ -636,6 +653,10 @@ namespace Components
Utils::Hook::Nop(0x408230, 5); // is asset default
Utils::Hook::Nop(0x40823A, 2); // jump
// Automatically register weapons, even if the level is not loading
Utils::Hook::Nop(0x49E547, 2);
Utils::Hook::Set<BYTE>(0x44F240, 0xEB);
// Skip double loading for fs_game
Utils::Hook::Set<BYTE>(0x4081FD, 0xEB);

View File

@ -5,12 +5,20 @@
#define WEAPON_LIMIT 2400
#define MAX_CONFIGSTRINGS (4139 - 1200 + WEAPON_LIMIT)
// Double the limit to allow loading of some heavy-duty MW3 maps
#define ADDITIONAL_GMODELS 512
#define G_MODELINDEX_LIMIT (512 + WEAPON_LIMIT - 1200 + ADDITIONAL_GMODELS)
namespace Components
{
class Weapon : public Component
{
public:
Weapon();
static Game::XModel* G_ModelIndexReallocated[G_MODELINDEX_LIMIT];
static bool GModelIndexHasBeenReallocated;
private:
static const Game::dvar_t* BGWeaponOffHandFix;

File diff suppressed because it is too large Load Diff

View File

@ -82,6 +82,31 @@ namespace Utils
Utils::Merge(&this->slots, obj.getSlots());
}
void disconnect(const Slot<T> slot)
{
std::lock_guard<std::recursive_mutex> _(this->mutex);
if (slot)
{
this->slots.erase(
std::remove_if(
this->slots.begin(),
this->slots.end(),
[&](std::function<T>& a)
{
if (a.target<T>() == slot.target<T>())
{
return true;
}
return false;
}
), this->slots.end()
);
}
}
void connect(const Slot<T> slot)
{
std::lock_guard<std::recursive_mutex> _(this->mutex);