Skip to content

Commit

Permalink
Fix material parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
RektInator committed Sep 20, 2019
1 parent 73204b5 commit c93a27e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 139 deletions.
10 changes: 9 additions & 1 deletion premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ workspace "zonetool"
}
configuration {}

startproject "zonetool"
startproject "ZoneTool"

project "ZoneTool"
kind "SharedLib"
Expand Down Expand Up @@ -109,6 +109,14 @@ workspace "zonetool"
"./dep/include",
}

filter "toolset:msc*"
postbuildcommands {
"if \"%COMPUTERNAME%\" == \"DESKTOP-CDFBECH\" ( copy /y \"$(TargetPath)\" \"E:\\Program Files (x86)\\Steam\\steamapps\\common\\Call of Duty 4\\zoneiw3.dll\" )",
"if \"%COMPUTERNAME%\" == \"DESKTOP-CDFBECH\" ( copy /y \"$(TargetPath)\" \"F:\\iw4x_full_game\\zonetool.dll\" )",
"if \"%COMPUTERNAME%\" == \"DESKTOP-CDFBECH\" ( copy /y \"$(TargetPath)\" \"E:\\Program Files (x86)\\Steam\\steamapps\\common\\Call of Duty Modern Warfare 3\\zonetool.dll\" )",
}
filter {}

project "ZoneUtils"
kind "StaticLib"
language "C++"
Expand Down
2 changes: 1 addition & 1 deletion src/IW3/Structs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ namespace ZoneTool

struct MaterialConstantDef
{
int nameHash;
unsigned int nameHash;
char name[12];
vec4_t literal;
};
Expand Down
37 changes: 11 additions & 26 deletions src/IW4/Assets/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,23 +329,23 @@ namespace ZoneTool
mat->techniqueSet = DB_FindXAssetHeader(XAssetType::techset, techset.data()).techset;
}

nlohmann::json maps = matdata["maps"];
if (maps.size())
auto maps = matdata["maps"];
if (!maps.empty())
{
mat->maps = Material_ParseMaps(mat->name, maps, mem);
}

mat->numMaps = maps.size();

nlohmann::json constantTable = matdata["constantTable"];
if (constantTable.size() > 0)
auto constantTable = matdata["constantTable"];
if (!constantTable.empty())
{
auto constant_def = mem->Alloc<MaterialConstantDef>(constantTable.size());
for (int i = 0; i < constantTable.size(); i++)
for (auto i = 0; i < constantTable.size(); i++)
{
strncpy(constant_def[i].name, constantTable[i]["name"].get<std::string>().c_str(), 11);
constant_def[i].name[11] = '\0';
constant_def[i].nameHash = constantTable[i]["nameHash"].get<int>();
constant_def[i].nameHash = constantTable[i]["nameHash"].get<unsigned int>();
constant_def[i].literal[0] = constantTable[i]["literal"][0].get<float>();
constant_def[i].literal[1] = constantTable[i]["literal"][1].get<float>();
constant_def[i].literal[2] = constantTable[i]["literal"][2].get<float>();
Expand All @@ -359,12 +359,11 @@ namespace ZoneTool
}
mat->constantCount = constantTable.size();


nlohmann::json stateMap = matdata["stateMap"];
if (stateMap.size() > 0)
auto stateMap = matdata["stateMap"];
if (!stateMap.empty())
{
auto stateBits = mem->Alloc<GfxStateBits>(stateMap.size());
for (int i = 0; i < stateMap.size(); i++)
for (auto i = 0; i < stateMap.size(); i++)
{
stateBits[i].loadBits[0] = stateMap[i][0].get<unsigned int>();
stateBits[i].loadBits[1] = stateMap[i][1].get<unsigned int>();
Expand Down Expand Up @@ -408,26 +407,12 @@ namespace ZoneTool
{
for (auto& state_bit : iw3TechniqueMap)
{
mat->stateBitsEntry[state_bit.second] = stateBits[state_bit.first];
}
}

// check if there exists a statebit dump for the techset thats being used
/*auto statebitsPath = "techsets\\"s + techset + ".statebits"s;
FileSystem::PreferLocalOverExternal(true);
if (FileSystem::FileExists(statebitsPath))
{
auto file = FileSystem::FileOpen(statebitsPath, "rb");
if (file)
{
if (FileSystem::FileSize(file) == sizeof mat->stateBitsEntry)
if (state_bit.second < sizeof mat->stateBitsEntry)
{
fread(&mat->stateBitsEntry[0], sizeof mat->stateBitsEntry, 1, file);
mat->stateBitsEntry[state_bit.second] = stateBits[state_bit.first];
}
FileSystem::FileClose(file);
}
}
FileSystem::PreferLocalOverExternal(false);*/
}

return mat;
Expand Down
116 changes: 8 additions & 108 deletions src/IW4/Assets/Techset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ namespace ZoneTool
{IW3::CONST_SRC_CODE_NEARPLANE_ORG, CONST_SRC_CODE_NEARPLANE_ORG},
{IW3::CONST_SRC_CODE_NEARPLANE_DX, CONST_SRC_CODE_NEARPLANE_DX},
{IW3::CONST_SRC_CODE_NEARPLANE_DY, CONST_SRC_CODE_NEARPLANE_DY},
{IW3::CONST_SRC_CODE_SHADOW_PARMS, static_cast<std::int32_t>(code_constant::shadow_params)},
{IW3::CONST_SRC_CODE_SHADOW_PARMS, 0},
{IW3::CONST_SRC_CODE_SHADOWMAP_POLYGON_OFFSET, CONST_SRC_CODE_SHADOWMAP_POLYGON_OFFSET},
{IW3::CONST_SRC_CODE_RENDER_TARGET_SIZE, CONST_SRC_CODE_RENDER_TARGET_SIZE},
{IW3::CONST_SRC_CODE_LIGHT_FALLOFF_PLACEMENT, CONST_SRC_CODE_LIGHT_FALLOFF_PLACEMENT},
Expand Down Expand Up @@ -211,13 +211,12 @@ namespace ZoneTool
{IW3::CONST_SRC_CODE_COLOR_MATRIX_G, CONST_SRC_CODE_COLOR_MATRIX_G},
{IW3::CONST_SRC_CODE_COLOR_MATRIX_B, CONST_SRC_CODE_COLOR_MATRIX_B},
{IW3::CONST_SRC_CODE_ALWAYS_DIRTY_PS_END, CONST_SRC_CODE_ALWAYS_DIRTY_PS_END},
{IW3::CONST_SRC_CODE_NEVER_DIRTY_PS_BEGIN, static_cast<std::int32_t>(code_constant::never_dirty_ps_begin)},
{IW3::CONST_SRC_CODE_SHADOWMAP_SWITCH_PARTITION, CONST_SRC_CODE_SHADOWMAP_SWITCH_PARTITION},
{IW3::CONST_SRC_CODE_SHADOWMAP_SCALE, CONST_SRC_CODE_SHADOWMAP_SCALE},
{IW3::CONST_SRC_CODE_ZNEAR, CONST_SRC_CODE_ZNEAR},
{IW3::CONST_SRC_CODE_SUN_POSITION, static_cast<std::int32_t>(code_constant::sun_position)},
{IW3::CONST_SRC_CODE_SUN_DIFFUSE, static_cast<std::int32_t>(code_constant::sun_diffuse)},
{IW3::CONST_SRC_CODE_SUN_SPECULAR, static_cast<std::int32_t>(code_constant::sun_specular)},
{IW3::CONST_SRC_CODE_SUN_POSITION, CONST_SRC_CODE_LIGHT_POSITION},
{IW3::CONST_SRC_CODE_SUN_DIFFUSE, CONST_SRC_CODE_LIGHT_DIFFUSE},
{IW3::CONST_SRC_CODE_SUN_SPECULAR, CONST_SRC_CODE_LIGHT_SPECULAR},
{IW3::CONST_SRC_CODE_LIGHTING_LOOKUP_SCALE, CONST_SRC_CODE_LIGHTING_LOOKUP_SCALE},
{IW3::CONST_SRC_CODE_DEBUG_BUMPMAP, CONST_SRC_CODE_DEBUG_BUMPMAP},
{IW3::CONST_SRC_CODE_MATERIAL_COLOR, CONST_SRC_CODE_MATERIAL_COLOR},
Expand All @@ -239,7 +238,6 @@ namespace ZoneTool
{IW3::CONST_SRC_CODE_CODE_MESH_ARG_1, CONST_SRC_CODE_CODE_MESH_ARG_1},
{IW3::CONST_SRC_CODE_CODE_MESH_ARG_LAST, CONST_SRC_CODE_CODE_MESH_ARG_LAST},
{IW3::CONST_SRC_CODE_BASE_LIGHTING_COORDS, CONST_SRC_CODE_BASE_LIGHTING_COORDS},
{IW3::CONST_SRC_CODE_NEVER_DIRTY_PS_END, static_cast<std::int32_t>(code_constant::never_dirty_ps_end)},
{IW3::CONST_SRC_CODE_COUNT_FLOAT4, CONST_SRC_CODE_COUNT_FLOAT4},
{IW3::CONST_SRC_FIRST_CODE_MATRIX, CONST_SRC_FIRST_CODE_MATRIX},
{IW3::CONST_SRC_CODE_WORLD_MATRIX, CONST_SRC_CODE_WORLD_MATRIX0},
Expand Down Expand Up @@ -316,7 +314,6 @@ namespace ZoneTool
FileSystem::PreferLocalOverExternal(true);

auto path = "techsets\\" + name + ".technique";
//auto statePath = "techsets\\" + name + ".statebits";

if (!FileSystem::FileExists(path) /*|| !FileSystem::FileExists(statePath)*/)
{
Expand Down Expand Up @@ -362,11 +359,6 @@ namespace ZoneTool
asset->hdr.unk = json["hdr"]["flags"].get<short>();
asset->hdr.numPasses = numPasses;

if (m_name == "mc_l_flag_t0c0_sat"s)
{
// DebugBreak();
}

if (asset->hdr.numPasses > 1)
{
// MessageBoxA(NULL, asset->hdr.name, NULL, NULL);
Expand All @@ -376,60 +368,24 @@ namespace ZoneTool
{
auto& curpass = json["pass"][pass];

// allocate empty sub-assets, to tell the tool that we need
// to parse those as well
//if (isExternalTechnique[index])
//{
// if (!curpass["PixelShader"].is_null())
// {
// asset->pass[pass].pixelShader = IPixelShader::parse(&curpass["PixelShader"].get<std::string>()[0], mem, false);// DB_FindXAssetHeader(XAssetType::pixelshader, &curpass["PixelShader"].get<std::string>()[0]).pixelshader;
// }
// if (!curpass["VertexDecl"].is_null())
// {
// asset->pass[pass].vertexDecl = IVertexDecl::parse(&curpass["VertexDecl"].get<std::string>()[0], mem, false); // DB_FindXAssetHeader(XAssetType::vertexdecl, &curpass["VertexDecl"].get<std::string>()[0]).vertexdecl;
// }
// if (!curpass["VertexShader"].is_null())
// {
// asset->pass[pass].vertexShader = IVertexShader::parse(&curpass["VertexShader"].get<std::string>()[0], mem, false); // DB_FindXAssetHeader(XAssetType::vertexshader, &curpass["VertexShader"].get<std::string>()[0]).vertexshader;
// }
//}
//else
//{
if (!curpass["PixelShader"].is_null())
{
auto shaderName = curpass["PixelShader"].get<std::string>();
/*auto shader = mappedShaders.find(shaderName);
if (shader != mappedShaders.end())
{
shaderName = shader->second;
}*/

asset->pass[pass].pixelShader = DB_FindXAssetHeader(pixelshader, &shaderName[0]).pixelshader;
}
if (!curpass["VertexDecl"].is_null())
{
auto shaderName = curpass["VertexDecl"].get<std::string>();
/*auto shader = mappedShaders.find(shaderName);
if (shader != mappedShaders.end())
{
shaderName = shader->second;
}
*/

asset->pass[pass].vertexDecl = DB_FindXAssetHeader(vertexdecl, &shaderName[0]).vertexdecl;
}
if (!curpass["VertexShader"].is_null())
{
auto shaderName = curpass["VertexShader"].get<std::string>();
/*auto shader = mappedShaders.find(shaderName);
if (shader != mappedShaders.end())
{
shaderName = shader->second;
}
*/

asset->pass[pass].vertexShader = DB_FindXAssetHeader(vertexshader, &shaderName[0]).vertexshader;
}
//}


asset->pass[pass].perPrimArgCount = curpass["perPrimArgCount"].get<char>();
asset->pass[pass].perObjArgCount = curpass["perObjArgCount"].get<char>();
Expand Down Expand Up @@ -486,11 +442,6 @@ namespace ZoneTool
{
argdef->u.codeConst.index -= 8;
}
// this is wrong
//else if (argdef->u.codeConst.index == 44)
//{
// argdef->u.codeConst.index -= 3;
//}
else if (argdef->u.codeConst.index >= 37)
{
argdef->u.codeConst.index -= 6;
Expand All @@ -502,28 +453,13 @@ namespace ZoneTool
}
else if (techtype == techniqueSetType::iw3)
{
//// todo, find codeConst conversions
//if (argdef->u.codeConst.index >= 54)
//{
// argdef->u.codeConst.index += 19;
//}
//else if (argdef->u.codeConst.index >= 60)
//{
// argdef->u.codeConst.index += 26;
//}
//else if (argdef->u.codeConst.index >= 76)
//{
// argdef->u.codeConst.index += 22;
//}

if (iw3CodeConstMap.find(argdef->u.codeConst.index) != iw3CodeConstMap.end())
{
argdef->u.codeConst.index = iw3CodeConstMap[argdef->u.codeConst.index];
}
else
{
ZONETOOL_FATAL("codeConst %u is not mapped in technique %s!", argdef->u.codeConst.index,
asset->hdr.name);
ZONETOOL_FATAL("codeConst %u is not mapped in technique %s!", argdef->u.codeConst.index, asset->hdr.name);
}
}
}
Expand All @@ -534,8 +470,6 @@ namespace ZoneTool
}
}

// thats it, I guess
// return parsed technique
return asset;
}

Expand Down Expand Up @@ -707,23 +641,13 @@ namespace ZoneTool

if (!this->m_asset)
{
ZONETOOL_ERROR("Current zone is depending on missing techset \"%s\", zone may not work correctly!", name
.data());

auto log = fopen("techsets.txt", "a");
if (log)
{
fprintf(log, "Missing techset %s", &name[0]);
fclose(log);
}
ZONETOOL_ERROR("Current zone is depending on missing techset \"%s\", zone may not work correctly!", name.data());

this->m_parsed = false;
this->m_asset = DB_FindXAssetHeader(this->type(), &this->name()[0]).techset;
}
else
{
// ENABLE ME WHEN MAPPING TECHSETS!
// this->m_name = techset;
this->m_parsed = true;
}
}
Expand All @@ -744,23 +668,6 @@ namespace ZoneTool
{
auto& techniquePass = data->techniques[technique]->pass[pass];

/*if (isExternalTechnique[technique])
{
if (techniquePass.vertexDecl)
{
zone->AddAssetOfTypePtr(vertexdecl, techniquePass.vertexDecl);
}
if (techniquePass.vertexShader)
{
zone->AddAssetOfTypePtr(vertexshader, techniquePass.vertexShader);
}
if (techniquePass.pixelShader)
{
zone->AddAssetOfTypePtr(pixelshader, techniquePass.pixelShader);
}
}
else
{*/
if (techniquePass.vertexDecl)
{
zone->AddAssetOfType(vertexdecl, techniquePass.vertexDecl->name);
Expand All @@ -773,7 +680,6 @@ namespace ZoneTool
{
zone->AddAssetOfType(pixelshader, techniquePass.pixelShader->name);
}
//}
}
}
}
Expand Down Expand Up @@ -813,12 +719,6 @@ namespace ZoneTool
auto techniqueHeader = buf->write(&data->techniques[technique]->hdr);
auto techniquePasses = buf->write(data->techniques[technique]->pass, techniqueHeader->numPasses);

if (techniqueHeader->numPasses > 1)
{
// DebugBreak();
// ZONETOOL_INFO("Technique %s has %u passes.", data->techniques[technique]->hdr.name, techniqueHeader->numPasses);
}

for (std::int32_t pass = 0; pass < techniqueHeader->numPasses; pass++)
{
if (techniquePasses[pass].vertexDecl)
Expand Down
2 changes: 1 addition & 1 deletion src/IW4/Structs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ namespace ZoneTool

struct MaterialConstantDef
{
int nameHash;
unsigned int nameHash;
char name[12];
float literal[4];
};
Expand Down
2 changes: 1 addition & 1 deletion src/IW5/Structs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ namespace ZoneTool

struct MaterialConstantDef
{
int nameHash;
unsigned int nameHash;
char name[12];
float literal[4];

Expand Down
2 changes: 1 addition & 1 deletion src/ZoneTool/ZoneTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ namespace ZoneTool
// Startup complete, show branding
Branding(currentLinker);

CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)HandleParams, nullptr, 0, nullptr);
CreateThread(nullptr, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(HandleParams), nullptr, 0, nullptr);

#ifdef USE_VMPROTECT
VMProtectEnd();
Expand Down

0 comments on commit c93a27e

Please sign in to comment.