Skip to content

Commit

Permalink
AddonMapEnts is now supported on iw4
Browse files Browse the repository at this point in the history
  • Loading branch information
RektInator committed Dec 1, 2019
1 parent 204c746 commit 1e9f9d2
Show file tree
Hide file tree
Showing 19 changed files with 1,665 additions and 2,584 deletions.
147 changes: 147 additions & 0 deletions src/IW4/Assets/AddonMapEnts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// ======================= ZoneTool =======================
// zonetool, a fastfile linker for various
// Call of Duty titles.
//
// Project: https://github.com/ZoneTool/zonetool
// Author: RektInator (https://github.com/RektInator)
// License: GNU GPL v3.0
// ========================================================
#include "stdafx.hpp"
#include "ZoneUtils/Utils/BinaryDumper.hpp"

namespace ZoneTool
{
namespace IW4
{
AddonMapEnts* IAddonMapEnts::parse(std::string name, ZoneMemory* mem)
{
// check if we can open a filepointer
if (!FileSystem::FileExists(name + ".addon.ents"))
{
return nullptr;
}

auto file = FileSystem::FileOpen(name + ".addon.ents", "rb");

// let them know that we're parsing a custom mapents file
ZONETOOL_INFO("Parsing addon mapents \"%s\"...", name.c_str());

// alloc mapents
auto ents = mem->Alloc<AddonMapEnts>();

ents->name = mem->StrDup(name);
ents->numEntityChars = FileSystem::FileSize(file);

ents->entityString = mem->Alloc<char>(ents->numEntityChars + 1);
memset((char*)ents->entityString, 0, ents->numEntityChars);

fread((char*)ents->entityString, ents->numEntityChars, 1, file);
((char*)ents->entityString)[ents->numEntityChars] = '\0';

// convert the mapents!
IMapEnts::ConvertEnts(reinterpret_cast<MapEnts*>(ents), mem);

// close filepointer
FileSystem::FileClose(file);

AssetReader triggerReader(mem);
AssetReader stageReader(mem);

if (triggerReader.open(name + ".addon.ents.triggers"))
{
ents->trigger.modelCount = triggerReader.read_int();
ents->trigger.models = triggerReader.read_array<TriggerModel>();

ents->trigger.hullCount = triggerReader.read_int();
ents->trigger.hulls = triggerReader.read_array<TriggerHull>();

ents->trigger.slabCount = triggerReader.read_int();
ents->trigger.slabs = triggerReader.read_array<TriggerSlab>();
}

triggerReader.close();

// return mapents
return ents;
}

void IAddonMapEnts::init(const std::string& name, ZoneMemory* mem)
{
this->name_ = "maps/mp/" + currentzone + ".d3dbsp"; // name;
this->asset_ = this->parse(name, mem);

if (!this->asset_)
{
this->asset_ = DB_FindXAssetHeader(this->type(), name.data()).addon_map_ents;
}
}

void IAddonMapEnts::prepare(ZoneBuffer* buf, ZoneMemory* mem)
{
}

void IAddonMapEnts::load_depending(IZone* zone)
{

}

std::string IAddonMapEnts::name()
{
return this->name_;
}

std::int32_t IAddonMapEnts::type()
{
return addon_map_ents;
}

void IAddonMapEnts::write(IZone* zone, ZoneBuffer* buf)
{
auto data = this->asset_;
auto dest = buf->write(data);

buf->push_stream(3);
START_LOG_STREAM;

dest->name = buf->write_str(this->name());

if (data->entityString)
{
buf->align(0);
buf->write(data->entityString, data->numEntityChars);
ZoneBuffer::ClearPointer(&dest->entityString);
}

IMapEnts::write_triggers(zone, buf, &dest->trigger);

END_LOG_STREAM;
buf->pop_stream();
}

void IAddonMapEnts::dump(AddonMapEnts* asset)
{
auto file = FileSystem::FileOpen(asset->name + ".addon.ents"s, "wb");

if (file)
{
fwrite(asset->entityString, asset->numEntityChars, 1, file);
FileSystem::FileClose(file);
}

AssetDumper triggerDumper;
if (triggerDumper.open(asset->name + ".addon.ents.triggers"s))
{
triggerDumper.dump_int(asset->trigger.modelCount);
triggerDumper.dump_array<TriggerModel>(asset->trigger.models, asset->trigger.modelCount);

triggerDumper.dump_int(asset->trigger.hullCount);
triggerDumper.dump_array<TriggerHull>(asset->trigger.hulls, asset->trigger.hullCount);

triggerDumper.dump_int(asset->trigger.slabCount);
triggerDumper.dump_array<TriggerSlab>(asset->trigger.slabs, asset->trigger.slabCount);

triggerDumper.close();
}
}
}
}
35 changes: 35 additions & 0 deletions src/IW4/Assets/AddonMapEnts.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// ======================= ZoneTool =======================
// zonetool, a fastfile linker for various
// Call of Duty titles.
//
// Project: https://github.com/ZoneTool/zonetool
// Author: RektInator (https://github.com/RektInator)
// License: GNU GPL v3.0
// ========================================================
#pragma once

namespace ZoneTool
{
namespace IW4
{
class IAddonMapEnts : public IAsset
{
private:
std::string name_;
AddonMapEnts* asset_;

public:
AddonMapEnts* parse(std::string name, ZoneMemory* mem);

void init(const std::string& name, ZoneMemory* mem) override;
void prepare(ZoneBuffer* buf, ZoneMemory* mem) override;
void load_depending(IZone* zone) override;

std::string name() override;
std::int32_t type() override;
void write(IZone* zone, ZoneBuffer* buffer) override;

static void dump(AddonMapEnts* asset);
};
}
}
2 changes: 1 addition & 1 deletion src/IW4/Assets/MapEnts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace ZoneTool
MapEnts* asset_;

public:
void ConvertEnts(MapEnts* ents, ZoneMemory* mem);
static void ConvertEnts(MapEnts* ents, ZoneMemory* mem);

MapEnts* parse(std::string name, ZoneMemory* mem);
IMapEnts();
Expand Down
Loading

0 comments on commit 1e9f9d2

Please sign in to comment.