<commit_before>
#include "stdafx.h"
#include "gameobject.h"
#include "objfields.h"
#include "objsystem.h"
#include "objregistry.h"
#include "arrayidxbitmaps.h"
#include "tio/tio.h"

GameObjectBody::~GameObjectBody()
{
	ForEachField([=](obj_f field, void** storage) {
		auto &fieldDef = objectFields.GetFieldDef(field);
		FreeStorage(fieldDef.type, *storage);
		return true;
	});

	// Delete transient props
	if (!IsProto()) {
		FreeStorage(ObjectFieldType::AbilityArray, reinterpret_cast<void*>(transientProps.renderAlpha));
		FreeStorage(ObjectFieldType::Int32Array, reinterpret_cast<void*>(transientProps.overlayLightHandles));
	}

	// This is the "old" way of doing it
	if (IsProto()) {
		free(difBitmap);
	} else {
		free(propCollBitmap);
	}
	delete[] propCollection;
}

bool GameObjectBody::IsProto() const
{
	return protoId.IsBlocked();
}

int32_t GameObjectBody::GetInt32(obj_f field) const
{
	Expects(objectFields.GetType(field) == ObjectFieldType::Int32);

	if (field == obj_f_type) {
		return type;
	}

	// For 32-bit integers, instead of a pointer to the value, 
	// the pointer itself is the 32-bit value
	auto storageLoc = GetStorageLocation<int32_t>(field);
	if (!storageLoc) {
		return int32_t();
	} else {
		return *storageLoc;
	}

}

float GameObjectBody::GetFloat(obj_f field) const
{
	Expects(objectFields.GetType(field) == ObjectFieldType::Float32);

	// For 32-bit floats, instead of a pointer to the value, 
	// the pointer itself is the 32-bit float
	auto storageLoc = GetStorageLocation<float>(field);
	if (!storageLoc) {
		return float();
	} else {
		return *storageLoc;
	}
}

int64_t GameObjectBody::GetInt64(obj_f field) const
{
	Expects(objectFields.GetType(field) == ObjectFieldType::Int64);
	auto storageLoc = GetStorageLocation<int64_t*>(field);
	if (!storageLoc || !*storageLoc) {
		return int64_t();
	} else {
		return **storageLoc;
	}
}

objHndl GameObjectBody::GetObjHndl(obj_f field) const
{
	Expects(objectFields.GetType(field) == ObjectFieldType::Obj);

	// Special case for prototype handle
	if (field == obj_f_prototype_handle) {
		return GetProtoHandle();
	}

	// Keep in mind that handles are stored in the form of ObjectIds
	auto storageLoc = GetStorageLocation<ObjectId*>(field);
	if (!storageLoc || !(*storageLoc) || !(*storageLoc)->IsHandle()) {
		return objHndl();
	} else {
		return (*storageLoc)->GetHandle();
	}
}

bool GameObjectBody::GetValidObjHndl(obj_f field, objHndl * handleOut)
{
	Expects(objectFields.GetType(field) == ObjectFieldType::Obj);

	// Special case for prototype handle
	if (field == obj_f_prototype_handle) {
		auto handle = GetProtoHandle();
		if (!objSystem->IsValidHandle(handle)) {
			*handleOut = objHndl();
			return false;
		} else {
			*handleOut = handle;
			return true;
		}
	}

	// Keep in mind that handles are stored in the form of ObjectIds
	auto storageLoc = GetStorageLocation<ObjectId*>(field);

	// Null handles are considered valid by this function
	if (!*storageLoc || (*storageLoc)->IsNull()) {
		*handleOut = objHndl();
		return true;
	}

	// If it's not a handle id, that is probably an error
	if ((*storageLoc)->IsHandle()) {
		*handleOut = objHndl();
		return false;
	}
	
	// Use the handle from the id and validate it before returning it
	auto handle = (*storageLoc)->GetHandle();
	if (!objSystem->IsValidHandle(handle)) {
		// Since we know the handle is invalid, we'll fix the issue in the object
		ResetField(field);

		*handleOut = objHndl();
		return false;
	} else {
		*handleOut = handle;
		return true;<commit_msg>Fix for GetValidObjHndl<commit_after>
#include "stdafx.h"
#include "gameobject.h"
#include "objfields.h"
#include "objsystem.h"
#include "objregistry.h"
#include "arrayidxbitmaps.h"
#include "tio/tio.h"

GameObjectBody::~GameObjectBody()
{
	ForEachField([=](obj_f field, void** storage) {
		auto &fieldDef = objectFields.GetFieldDef(field);
		FreeStorage(fieldDef.type, *storage);
		return true;
	});

	// Delete transient props
	if (!IsProto()) {
		FreeStorage(ObjectFieldType::AbilityArray, reinterpret_cast<void*>(transientProps.renderAlpha));
		FreeStorage(ObjectFieldType::Int32Array, reinterpret_cast<void*>(transientProps.overlayLightHandles));
	}

	// This is the "old" way of doing it
	if (IsProto()) {
		free(difBitmap);
	} else {
		free(propCollBitmap);
	}
	delete[] propCollection;
}

bool GameObjectBody::IsProto() const
{
	return protoId.IsBlocked();
}

int32_t GameObjectBody::GetInt32(obj_f field) const
{
	Expects(objectFields.GetType(field) == ObjectFieldType::Int32);

	if (field == obj_f_type) {
		return type;
	}

	// For 32-bit integers, instead of a pointer to the value, 
	// the pointer itself is the 32-bit value
	auto storageLoc = GetStorageLocation<int32_t>(field);
	if (!storageLoc) {
		return int32_t();
	} else {
		return *storageLoc;
	}

}

float GameObjectBody::GetFloat(obj_f field) const
{
	Expects(objectFields.GetType(field) == ObjectFieldType::Float32);

	// For 32-bit floats, instead of a pointer to the value, 
	// the pointer itself is the 32-bit float
	auto storageLoc = GetStorageLocation<float>(field);
	if (!storageLoc) {
		return float();
	} else {
		return *storageLoc;
	}
}

int64_t GameObjectBody::GetInt64(obj_f field) const
{
	Expects(objectFields.GetType(field) == ObjectFieldType::Int64);
	auto storageLoc = GetStorageLocation<int64_t*>(field);
	if (!storageLoc || !*storageLoc) {
		return int64_t();
	} else {
		return **storageLoc;
	}
}

objHndl GameObjectBody::GetObjHndl(obj_f field) const
{
	Expects(objectFields.GetType(field) == ObjectFieldType::Obj);

	// Special case for prototype handle
	if (field == obj_f_prototype_handle) {
		return GetProtoHandle();
	}

	// Keep in mind that handles are stored in the form of ObjectIds
	auto storageLoc = GetStorageLocation<ObjectId*>(field);
	if (!storageLoc || !(*storageLoc) || !(*storageLoc)->IsHandle()) {
		return objHndl();
	} else {
		return (*storageLoc)->GetHandle();
	}
}

bool GameObjectBody::GetValidObjHndl(obj_f field, objHndl * handleOut)
{
	Expects(objectFields.GetType(field) == ObjectFieldType::Obj);

	// Special case for prototype handle
	if (field == obj_f_prototype_handle) {
		auto handle = GetProtoHandle();
		if (!objSystem->IsValidHandle(handle)) {
			*handleOut = objHndl();
			return false;
		} else {
			*handleOut = handle;
			return true;
		}
	}

	// Keep in mind that handles are stored in the form of ObjectIds
	auto storageLoc = GetStorageLocation<ObjectId*>(field);

	// Null handles are considered valid by this function
	if (!*storageLoc || (*storageLoc)->IsNull()) {
		*handleOut = objHndl();
		return true;
	}

	// If it's not a handle id, that is probably an error
	if (!(*storageLoc)->IsHandle()) {
		*handleOut = objHndl();
		return false;
	}
	
	// Use the handle from the id and validate it before returning it
	auto handle = (*storageLoc)->GetHandle();
	if (!objSystem->IsValidHandle(handle)) {
		// Since we know the handle is invalid, we'll fix the issue in the object
		ResetField(field);

		*handleOut = objHndl();
		return false;
	} else {
		*handleOut = handle;
		return true;
{'fraction_non_alphanumeric': 0.13377784082848632, 'fraction_numerical': 0.009079302028656547, 'mean_word_length': 4.792933442892358, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3216372', 'n_tokens_mistral': 2504, 'n_tokens_neox': 2346, 'n_words': 603}