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;
@ -381,9 +383,16 @@ namespace Components
Game::XModel* model = nullptr;
if (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;

View File

@ -30,7 +30,7 @@ namespace Components
buffer(0xC800000),
zoneName(name),
dataMap("zone_source/" + name + ".csv"),
branding{nullptr},
branding{ nullptr },
assetDepth(0),
iw4ofApi(getIW4OfApiParams())
{
@ -354,8 +354,8 @@ namespace Components
Game::XAssetHeader ZoneBuilder::Zone::saveSubAsset(Game::XAssetType type, void* ptr)
{
Game::XAssetHeader header { ptr };
Game::XAsset asset { type, header };
Game::XAssetHeader header{ ptr };
Game::XAsset asset{ type, header };
std::string name = Game::DB_GetXAssetName(&asset);
int assetIndex = this->findAsset(type, name);
@ -428,7 +428,7 @@ namespace Components
zoneBuffer.insert(zoneBuffer.begin(), static_cast<char>(Utils::Cryptography::Rand::GenerateInt()));
char lastByte = 0;
for(unsigned int i = 0; i < zoneBuffer.size(); ++i )
for (unsigned int i = 0; i < zoneBuffer.size(); ++i)
{
char oldLastByte = lastByte;
lastByte = zoneBuffer[i];
@ -540,7 +540,7 @@ namespace Components
auto zoneBranding = std::format("Built using the IW4x ZoneBuilder! {:%d-%m-%Y %H:%M:%OS}", now);
auto brandingLen = zoneBranding.size(); // + 1 is added by the save code
this->branding = {this->zoneName.data(), 0, static_cast<int>(brandingLen), getAllocator()->duplicateString(zoneBranding)};
this->branding = { this->zoneName.data(), 0, static_cast<int>(brandingLen), getAllocator()->duplicateString(zoneBranding) };
if (this->findAsset(Game::ASSET_TYPE_RAWFILE, this->branding.name) != -1)
{
@ -647,7 +647,7 @@ namespace Components
void ZoneBuilder::Zone::addRawAsset(Game::XAssetType type, void* ptr)
{
this->loadedAssets.push_back({type, {ptr}});
this->loadedAssets.push_back({ type, {ptr} });
}
// Remap a scriptString to it's corresponding value in the local scriptString table.
@ -787,6 +787,14 @@ namespace Components
return AssetHandler::FindAssetForZone(static_cast<Game::XAssetType>(type), name, this).data;
};
params.request_mark_asset = [this](int type, void* data) -> void
{
Game::XAsset asset {static_cast<Game::XAssetType>(type), {data}};
AssetHandler::ZoneMark(asset, this);
this->addRawAsset(static_cast<Game::XAssetType>(type), data);
};
params.fs_read_file = [](const std::string& filename) -> std::string
{
auto file = FileSystem::File(filename);
@ -828,7 +836,7 @@ namespace Components
return params;
}
int ZoneBuilder::StoreTexture(Game::GfxImageLoadDef **loadDef, Game::GfxImage *image)
int ZoneBuilder::StoreTexture(Game::GfxImageLoadDef** loadDef, Game::GfxImage* image)
{
size_t size = 16 + (*loadDef)->resourceSize;
void* data = Utils::Memory::GetAllocator()->allocate(size);
@ -1106,13 +1114,8 @@ namespace Components
params.find_other_asset = [](int type, const std::string& name) -> void*
{
if (ZoneBuilder::DumpingZone.empty())
{
return Game::DB_FindXAssetHeader(static_cast<Game::XAssetType>(type), name.data()).data;
}
// Do not deadlock the DB
return nullptr;
return Game::DB_FindXAssetHeader(static_cast<Game::XAssetType>(type), name.data()).data;
};
params.fs_read_file = [](const std::string& filename) -> std::string
@ -1255,7 +1258,7 @@ namespace Components
// don't remap techsets
Utils::Hook::Nop(0x5BC791, 5);
AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader asset, const std::string& name, bool* /*restrict*/)
AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader /* asset*/, const std::string& name, bool* /*restrict*/)
{
if (!ZoneBuilder::TraceZone.empty() && ZoneBuilder::TraceZone == FastFiles::Current())
{
@ -1264,15 +1267,6 @@ namespace Components
OutputDebugStringA(Utils::String::Format("%s\n", name));
#endif
}
if (!ZoneBuilder::DumpingZone.empty())
{
if (ExporterAPI.is_type_supported(type) && name[0] != ',')
{
ExporterAPI.write(type, asset.data);
Components::Logger::Print(".");
}
}
});
Command::Add("dumpzone", [](const Command::Params* params)
@ -1288,11 +1282,53 @@ namespace Components
info.allocFlags = Game::DB_ZONE_MOD;
info.freeFlags = 0;
Logger::Print("Dumping zone '{}'...\n", zone);
Logger::Print("Loading zone '{}'...\n", zone);
{
struct asset_t
{
Game::XAssetType type;
char name[128];
};
std::vector<asset_t> assets{};
const auto handle = AssetHandler::OnLoad([&](Game::XAssetType type, Game::XAssetHeader asset, const std::string& name, bool* /*restrict*/)
{
if (ExporterAPI.is_type_supported(type) && name[0] != ',')
{
Game::XAsset xasset = { type, asset };
asset_t assetIdentifier{};
// Fetch name
const auto assetName = Game::DB_GetXAssetName(&xasset);
std::memcpy(assetIdentifier.name, assetName, strnlen(assetName, ARRAYSIZE(assetIdentifier.name) - 1));
assetIdentifier.name[ARRAYSIZE(assetIdentifier.name) - 1] = '\x00';
assetIdentifier.type = type;
assets.push_back(assetIdentifier);
}
});
Game::DB_LoadXAssets(&info, 1, true);
AssetHandler::FindOriginalAsset(Game::ASSET_TYPE_RAWFILE, zone.data()); // Lock until zone is loaded
Logger::Print("Dumping zone '{}'...\n", zone);
handle(); // Release
for (const auto& asset : assets)
{
const auto assetHeader = Game::DB_FindXAssetHeader(asset.type, asset.name);
if (assetHeader.data)
{
ExporterAPI.write(asset.type, assetHeader.data);
}
else
{
Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!", asset.name);
}
}
}
Logger::Print("Unloading zone '{}'...\n", zone);
info.freeFlags = Game::DB_ZONE_MOD;
info.allocFlags = 0;

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);