Skip to content

Commit

Permalink
move some meshComponent stuff to scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jackhumbert committed Feb 23, 2023
1 parent 845d31e commit ea8b22d
Show file tree
Hide file tree
Showing 18 changed files with 610 additions and 155 deletions.
272 changes: 206 additions & 66 deletions build/r6/scripts/let_there_be_flight/let_there_be_flight.packed.reds

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/r6/tweaks/let_there_be_flight.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Let There Be Flight
# (C) 2022 Jack Humbert
# https://github.com/jackhumbert/let_there_be_flight
# This file was automatically generated on 2023-02-22 04:16:50.7239554
# This file was automatically generated on 2023-02-23 04:49:14.3646377

# quickhacks.yaml

Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions src/red4ext/Addresses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define CreateCRTTIArrayTypeFromClassAddr 0x200660
#define CreateCRTTIHandleTypeFromClassAddr 0x200750
#define CreateCRTTIWeakHandleTypeFromClassAddr 0x200AF0
#define CreateCRTTIRaRefTypeFromClassAddr 0x200920
#define ProcessScriptTypesAddr 0x277560
#define LoadResRefTAddr 0x204A90
#define CGameFramework_Systems_120_128_130Addr 0x13A19A0
Expand Down
11 changes: 6 additions & 5 deletions src/red4ext/EntityAddComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ void __fastcall Entity_InitializeComponents_Hook(RED4ext::ent::Entity *entity, v

auto fc = (FlightComponent *)FlightComponent::GetRTTIType()->CreateInstance(true);
fc->name = "flightComponent";
fc->entity = entity;
auto fch = RED4ext::Handle<FlightComponent>(fc);
vehicle->componentsStorage.components.EmplaceBack(fch);

Expand Down Expand Up @@ -264,12 +265,12 @@ void __fastcall Entity_InitializeComponents_Hook(RED4ext::ent::Entity *entity, v
auto handle = RED4ext::Handle<IFlightConfiguration>(configuration);
configuration->ref = RED4ext::WeakHandle(*reinterpret_cast<RED4ext::Handle<RED4ext::ISerializable> *>(&handle));
configuration->unk30 = configurationCls;

configuration->component = RED4ext::Handle<FlightComponent>(fc);
fc->configuration = handle;

configuration->Setup(fc);
configuration->Setup(vehicle);
configuration->AddSlots(vs);
configuration->AddMeshes(entity, vcc);
//configuration->AddMeshes(entity, vcc);
} else {
spdlog::info("Looked for class '{}'", className);
}
Expand Down Expand Up @@ -516,8 +517,8 @@ struct EntityAddComponentModule : FlightModule {
&IPlacedComponentUpdateHardTransformBinding, {.isNative = true}));


auto mc = rtti->GetClass("entMeshComponent");
mc->props.EmplaceBack(RED4ext::CProperty::Create(rtti->GetType("Vector3"), "visualScale", nullptr, 0x178));
//auto mc = rtti->GetClass("entMeshComponent");
//mc->props.EmplaceBack(RED4ext::CProperty::Create(rtti->GetType("Vector3"), "visualScale", nullptr, 0x178));
}
};

Expand Down
29 changes: 16 additions & 13 deletions src/red4ext/FlightConfiguration.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include "FlightConfiguration.hpp"
#include <RED4ext/Memory/Allocators.hpp>

void IFlightConfiguration::Setup(FlightComponent * component) {
this->component = RED4ext::Handle<FlightComponent>(component);
void IFlightConfiguration::Setup(RED4ext::vehicle::BaseObject * vehicle) {
this->thrusters = RED4ext::DynArray<RED4ext::Handle<IFlightThruster>>(new RED4ext::Memory::DefaultAllocator());
auto onInit = GetType()->GetFunction("OnSetup");
if (onInit) {
auto stack = RED4ext::CStack(this, nullptr, 0, nullptr, 0);
auto rtti = RED4ext::CRTTISystem::Get();
RED4ext::CStackType args[1];
auto handle = RED4ext::Handle<RED4ext::vehicle::BaseObject>(vehicle);
args[0] = RED4ext::CStackType(rtti->GetType("handle:vehicleBaseObject"), &handle);
auto stack = RED4ext::CStack(this, args, 1, nullptr, 0);
onInit->Execute(&stack);
}
}
Expand All @@ -21,16 +24,16 @@ void IFlightConfiguration::AddSlots(RED4ext::ent::SlotComponent *slotComponent)
slotComponent->slots.EmplaceBack(*slot);
slotComponent->slotIndexLookup.Emplace(slot->slotName, slotComponent->slots.size - 1);

for (auto thruster : thrusters) {
auto slot = reinterpret_cast<RED4ext::ent::Slot *>(rtti->GetClass("entSlot")->CreateInstance());
slot->boneName = thruster->boneName;
slot->slotName = thruster->slotName;
slot->relativePosition = thruster->relativePosition;
slot->relativeRotation = thruster->relativeRotation;
//slot->relativeRotation = RED4ext::Quaternion(0.0, 0.0, 0.0, 1.0);
slotComponent->slots.EmplaceBack(*slot);
slotComponent->slotIndexLookup.Emplace(slot->slotName, slotComponent->slots.size - 1);
}
//for (auto thruster : thrusters) {
// auto slot = reinterpret_cast<RED4ext::ent::Slot *>(rtti->GetClass("entSlot")->CreateInstance());
// slot->boneName = thruster->boneName;
// slot->slotName = thruster->slotName;
// slot->relativePosition = thruster->relativePosition;
// slot->relativeRotation = thruster->relativeRotation;
// //slot->relativeRotation = RED4ext::Quaternion(0.0, 0.0, 0.0, 1.0);
// slotComponent->slots.EmplaceBack(*slot);
// slotComponent->slotIndexLookup.Emplace(slot->slotName, slotComponent->slots.size - 1);
//}
}

void IFlightConfiguration::AddMeshes(RED4ext::ent::Entity *entity, RED4ext::ent::VisualControllerComponent *vcc) {
Expand Down
2 changes: 1 addition & 1 deletion src/red4ext/FlightConfiguration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class IFlightConfiguration : public Engine::RTTIClass<IFlightConfiguration, RED4
RED4ext::CName flightCameraBone = "roof_border_front";
RED4ext::Vector3 flightCameraOffset = RED4ext::Vector3(0.0, 0.0, 0.0);

void Setup(FlightComponent* component);
void Setup(RED4ext::vehicle::BaseObject * vehicle);
void AddSlots(RED4ext::ent::SlotComponent* slotComponent);
void AddMeshes(RED4ext::ent::Entity * entity, RED4ext::ent::VisualControllerComponent * vcc);

Expand Down
4 changes: 3 additions & 1 deletion src/red4ext/FlightThruster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class IFlightThruster : public Engine::RTTIClass<IFlightThruster, RED4ext::IScri
RED4ext::CName meshPath = "user\\jackhumbert\\meshes\\engine_corpo.mesh";
RED4ext::CName meshName = "Thruster";
RED4ext::Handle<RED4ext::ent::MeshComponent> meshComponent;
RED4ext::Handle<RED4ext::vehicle::BaseObject> vehicle;

private:
friend Descriptor;
Expand All @@ -35,4 +36,5 @@ RED4EXT_ASSERT_OFFSET(IFlightThruster, relativePosition, 0x60);
RED4EXT_ASSERT_OFFSET(IFlightThruster, relativeRotation, 0x70);
RED4EXT_ASSERT_OFFSET(IFlightThruster, meshPath, 0x80);
RED4EXT_ASSERT_OFFSET(IFlightThruster, meshName, 0x88);
RED4EXT_ASSERT_OFFSET(IFlightThruster, meshComponent, 0x90);
RED4EXT_ASSERT_OFFSET(IFlightThruster, meshComponent, 0x90);
RED4EXT_ASSERT_OFFSET(IFlightThruster, vehicle, 0xA0);
80 changes: 80 additions & 0 deletions src/red4ext/MeshComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "MeshComponent.hpp"
#include <RED4ext/Scripting/Natives/Generated/ent/HardTransformBinding.hpp>
#include <RED4ext/Scripting/Natives/Generated/ent/VisualControllerComponent.hpp>
#include <RED4ext/Scripting/Natives/Generated/ent/SlotComponent.hpp>

void MeshComponent::SetMesh(RED4ext::ResRef mesh) {
this->mesh.path = mesh.resource.path;
}

void IPlacedComponent::SetParentTransform(RED4ext::CName bindName, RED4ext::CName slotName) {
auto rtti = RED4ext::CRTTISystem::Get();
auto htb = (RED4ext::ent::HardTransformBinding *)rtti->GetClass("entHardTransformBinding")->CreateInstance();
htb->bindName = bindName;
htb->slotName = slotName;
this->parentTransform = RED4ext::Handle<RED4ext::ent::ITransformBinding>(htb);
}

void Entity::AddComponent(RED4ext::Handle<RED4ext::ent::IComponent> componentToAdd) {
RED4ext::ent::VisualControllerComponent *vcc = NULL;
auto rtti = RED4ext::CRTTISystem::Get();

for (auto const &handle : this->componentsStorage.components) {
auto component = handle.GetPtr();
if (component->GetNativeType() == rtti->GetClass("entVisualControllerComponent")) {
vcc = reinterpret_cast<RED4ext::ent::VisualControllerComponent *>(component);
break;
}
}

if (vcc != NULL) {
if (componentToAdd->GetNativeType() == rtti->GetClass("entMeshComponent")) {
auto meshComponent = (RED4ext::ent::MeshComponent *)componentToAdd.instance;
this->componentsStorage.components.EmplaceBack(componentToAdd);
auto vcd = reinterpret_cast<RED4ext::ent::VisualControllerDependency *>(
rtti->GetClass("entVisualControllerDependency")->CreateInstance());
vcd->appearanceName = meshComponent->meshAppearance;
vcd->componentName = meshComponent->name;
vcd->mesh.path = meshComponent->mesh.path;
vcc->appearanceDependency.EmplaceBack(*vcd);

if (vcc->resourcePaths.size) {
for (int i = 0; i < vcc->resourcePaths.size; i++) {
if (vcc->resourcePaths[i] == meshComponent->mesh.path) {
break;
} else if (vcc->resourcePaths[i] > meshComponent->mesh.path) {
vcc->resourcePaths.Emplace(&vcc->resourcePaths[i], meshComponent->mesh.path);
break;
}
}
} else {
vcc->resourcePaths.EmplaceBack(meshComponent->mesh.path);
}
}
}
}

void Entity::AddSlot(RED4ext::CName boneName, RED4ext::CName slotName, RED4ext::Vector3 relativePosition, RED4ext::Quaternion relativeRotation) {
RED4ext::ent::SlotComponent *slotComponent = NULL;
auto rtti = RED4ext::CRTTISystem::Get();

for (auto const &handle : this->componentsStorage.components) {
auto component = handle.GetPtr();
if (component->GetNativeType() == rtti->GetClass("entSlotComponent")) {
if (component->name == "vehicle_slots") {
slotComponent = reinterpret_cast<RED4ext::ent::SlotComponent *>(component);
break;
}
}
}

if (slotComponent != NULL) {
auto slot = reinterpret_cast<RED4ext::ent::Slot *>(rtti->GetClass("entSlot")->CreateInstance());
slot->boneName = boneName;
slot->slotName = slotName;
slot->relativePosition = relativePosition;
slot->relativeRotation = relativeRotation;
slotComponent->slots.EmplaceBack(*slot);
slotComponent->slotIndexLookup.Emplace(slot->slotName, slotComponent->slots.size - 1);
}
}
46 changes: 46 additions & 0 deletions src/red4ext/MeshComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include "Engine/RTTIExpansion.hpp"
#include <RED4ext/Scripting/Natives/Generated/ent/MeshComponent.hpp>
#include <RED4ext/Scripting/Natives/Generated/ent/IPlacedComponent.hpp>
#include <RED4ext/Scripting/Natives/Generated/red/ResourceReferenceScriptToken.hpp>

class MeshComponent : public Engine::RTTIExpansion<MeshComponent, RED4ext::ent::MeshComponent> {
public:
void SetMesh(RED4ext::ResRef mesh);

private:
friend Descriptor;

inline static void OnExpand(Descriptor *aType, RED4ext::CRTTISystem *) {
aType->AddFunction<&MeshComponent::SetMesh>("SetMesh");
}
};


class IPlacedComponent : public Engine::RTTIExpansion<IPlacedComponent, RED4ext::ent::IPlacedComponent> {
public:
void SetParentTransform(RED4ext::CName bindName, RED4ext::CName slotName);

private:
friend Descriptor;

inline static void OnExpand(Descriptor *aType, RED4ext::CRTTISystem *) {
aType->AddFunction<&IPlacedComponent::SetParentTransform>("SetParentTransform");
}
};


class Entity : public Engine::RTTIExpansion<Entity, RED4ext::ent::Entity> {
public:
void AddComponent(RED4ext::Handle<RED4ext::ent::IComponent> component);
void AddSlot(RED4ext::CName boneName, RED4ext::CName slotName, RED4ext::Vector3 relativePosition,
RED4ext::Quaternion relativeRotation);
private:
friend Descriptor;

inline static void OnExpand(Descriptor *aType, RED4ext::CRTTISystem *) {
aType->AddFunction<&Entity::AddComponent>("AddComponent");
aType->AddFunction<&Entity::AddSlot>("AddSlot");
}
};
36 changes: 34 additions & 2 deletions src/red4ext/RuntimeProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ RED4ext::CRTTIHandleType **__fastcall CreateCRTTIHandleTypeFromClass(RED4ext::CR
return call(a1, a2);
}

RED4ext::CRTTIResourceAsyncReferenceType **__fastcall CreateCRTTIRaRefTypeFromClass(RED4ext::CRTTIResourceAsyncReferenceType **a1, RED4ext::CBaseRTTIType *a2) {
RED4ext::RelocFunc<decltype(&CreateCRTTIRaRefTypeFromClass)> call(CreateCRTTIRaRefTypeFromClassAddr);
return call(a1, a2);
}

bool __fastcall ProcessScriptTypes(uint32_t* version, ScriptData* scriptData, void* scriptLogger);
decltype(&ProcessScriptTypes) ProcessScriptTypes_Original;

Expand All @@ -54,14 +59,21 @@ bool __fastcall ProcessScriptTypes(uint32_t* version, ScriptData* scriptData, vo
auto cstr = offsetStr->c_str();
char* p;
auto offsetValue = strtoul(cstr, &p, 16);
RED4ext::CName typeName = prop->type->name;
if (*p == 0) {
spdlog::info("{}.{} at 0x{:X} {}", scriptClass->name.ToString(), prop->name.ToString(), offsetValue, prop->type->name.ToString());
if (prop->flags.isNative) {
auto customTypeStr = prop->runtimeProperties.Get("type");
if (customTypeStr) {
spdlog::info("Custom type: {}", customTypeStr->c_str());
typeName = RED4ext::CName(customTypeStr->c_str());
//prop->type->name = typeName;
}
auto rtti = RED4ext::CRTTISystem::Get();
auto rttiClass = rtti->GetClassByScriptName(scriptClass->name);
auto rttiType = rtti->GetType(prop->type->name);
auto rttiType = rtti->GetType(typeName);
if (!rttiType) {
std::string typeStr(prop->type->name.ToString());
std::string typeStr(typeName.ToString());
auto del = typeStr.find(":");
if (del != -1) {
auto outerTypeStr = typeStr.substr(0, del);
Expand All @@ -88,10 +100,22 @@ bool __fastcall ProcessScriptTypes(uint32_t* version, ScriptData* scriptData, vo
CreateCRTTIArrayTypeFromClass(&whType, innerInnerType);
rtti->RegisterType(whType);
innerType = whType;
} else if (innerTypeStr.starts_with("raRef")) {
RED4ext::CRTTIResourceAsyncReferenceType *whType;
CreateCRTTIRaRefTypeFromClass(&whType, innerInnerType);
rtti->RegisterType(whType);
innerType = whType;
}
}
}
if (innerType) {
//if (customTypeStr) {
// //innerScriptType = ScriptType();
// ScriptClass *innerScriptCls = (ScriptClass*)malloc(0x90);
// innerScriptCls->rttiType = (RED4ext::CClass*)innerType;
// innerScriptCls->name = RED4ext::CName(innerTypeStr.c_str());
// prop->type->innerType = innerScriptCls;
//}
if (outerTypeStr.starts_with("wref")) {
RED4ext::CRTTIWeakHandleType *whType;
CreateCRTTIWeakHandleTypeFromClass(&whType, innerType);
Expand All @@ -107,12 +131,20 @@ bool __fastcall ProcessScriptTypes(uint32_t* version, ScriptData* scriptData, vo
CreateCRTTIArrayTypeFromClass(&whType, innerType);
rtti->RegisterType(whType);
rttiType = whType;
} else if (innerTypeStr.starts_with("raRef")) {
RED4ext::CRTTIResourceAsyncReferenceType *whType;
CreateCRTTIRaRefTypeFromClass(&whType, innerType);
rtti->RegisterType(whType);
innerType = whType;
}
} else {
spdlog::warn("could not find inner type '{}' in '{}'", innerTypeStr, typeStr);
}
}
}
//if (customTypeStr) {
// prop->type->rttiType = rttiType;
//}
if (rttiType) {
rttiClass->props.PushBack(
RED4ext::CProperty::Create(rttiType, prop->name.ToString(), nullptr, offsetValue));
Expand Down
1 change: 1 addition & 0 deletions src/red4ext/ScriptDefinitions/ScriptDefinitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct ScriptType : ScriptDefinition {
uint32_t unk28;
EScriptType type;
};
RED4EXT_ASSERT_SIZE(ScriptType, 0x30);

struct ScriptPropertyFlags {
uint16_t isNative : 1;
Expand Down
7 changes: 7 additions & 0 deletions src/red4ext/Signatures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ using CreateCRTTIHandleTypeFromClass = RED4ext::CRTTIHandleType **__fastcall(RED
/// @nth 4/7
using CreateCRTTIWeakHandleTypeFromClass = RED4ext::CRTTIWeakHandleType **__fastcall (RED4ext::CRTTIWeakHandleType **a1,
RED4ext::CBaseRTTIType *a2);

// 1.62 RVA: 0x200920 / 2099488
/// @pattern 48 89 5C 24 08 48 89 74 24 10 57 48 83 EC 30 48 8B FA 48 8B F1 BA 28 00 00 00 48 8D 4C 24 20 E8
/// @nth 2/7
using CreateCRTTIRaRefTypeFromClass = RED4ext::CRTTIResourceAsyncReferenceType **__fastcall (RED4ext::CRTTIResourceAsyncReferenceType **a1,
RED4ext::CBaseRTTIType *a2);

struct ScriptData;

// 1.52 RVA: 0x273160 / 2568544
Expand Down
Binary file modified src/red4ext/build/release/bin/flight_control.pdb
Binary file not shown.
2 changes: 2 additions & 0 deletions src/red4ext/flight_control.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
<ClCompile Include="FmodHelper.hpp" />
<ClCompile Include="FmodHelper.cpp" />
<ClCompile Include="FlightHUDGameController.hpp" />
<ClCompile Include="MeshComponent.cpp" />
<ClCompile Include="RuntimeProperties.cpp" />
<ClCompile Include="ScriptDefinitions\ScriptDefinitions.hpp" />
<ClCompile Include="stdafx.cpp" />
Expand All @@ -150,6 +151,7 @@
<ClInclude Include="FlightThruster.hpp" />
<ClInclude Include="IFlightSystem.hpp" />
<ClInclude Include="INIReader.h" />
<ClInclude Include="MeshComponent.hpp" />
<ClInclude Include="RuntimeClass.hpp" />
<ClInclude Include="ScriptDefinitions\ScriptHost.hpp" />
<ClInclude Include="Signatures.hpp" />
Expand Down
Loading

0 comments on commit ea8b22d

Please sign in to comment.