diff --git a/core/src/common.h b/core/src/common.h index 919b194..69f17ba 100644 --- a/core/src/common.h +++ b/core/src/common.h @@ -8,9 +8,8 @@ #include class Instance; -// typedef std::function element, std::optional> newParent)> HierarchyUpdateHandler; -typedef std::function object, std::optional> oldParent, std::optional> newParent)> HierarchyPreUpdateHandler; -typedef std::function object, std::optional> oldParent, std::optional> newParent)> HierarchyPostUpdateHandler; +typedef std::function object, nullable std::shared_ptr oldParent, nullable std::shared_ptr newParent)> HierarchyPreUpdateHandler; +typedef std::function object, nullable std::shared_ptr oldParent, nullable std::shared_ptr newParent)> HierarchyPostUpdateHandler; typedef std::function instance, std::string property, Variant newValue)> PropertyUpdateHandler; // TEMPORARY COMMON DATA FOR VARIOUS INTERNAL COMPONENTS diff --git a/core/src/datatypes/ref.cpp b/core/src/datatypes/ref.cpp index 06057bc..388736b 100644 --- a/core/src/datatypes/ref.cpp +++ b/core/src/datatypes/ref.cpp @@ -151,9 +151,9 @@ static int inst_index(lua_State* L) { } // Look for child - std::optional> child = inst->FindFirstChild(key); + nullable std::shared_ptr child = inst->FindFirstChild(key); if (child) { - InstanceRef(child.value()).PushLuaValue(L); + InstanceRef(child).PushLuaValue(L); return 1; } @@ -173,7 +173,7 @@ static int inst_newindex(lua_State* L) { if (meta->flags & PROP_READONLY) return luaL_error(L, "'%s' of %s is read-only", key.c_str(), inst->GetClass()->className.c_str()); if (key == "Parent" && inst->IsParentLocked()) - return luaL_error(L, "Cannot set property Parent (%s) of %s, parent is locked", inst->GetParent() ? inst->GetParent().value()->name.c_str() : "NULL", inst->GetClass()->className.c_str()); + return luaL_error(L, "Cannot set property Parent (%s) of %s, parent is locked", inst->GetParent() ? inst->GetParent()->name.c_str() : "NULL", inst->GetClass()->className.c_str()); // TODO: Make this work for enums, this is not a solution!! result value = meta->type.descriptor->fromLuaValue(L, -1); diff --git a/core/src/objects/base/instance.cpp b/core/src/objects/base/instance.cpp index fc1d2c8..593cf34 100644 --- a/core/src/objects/base/instance.cpp +++ b/core/src/objects/base/instance.cpp @@ -44,21 +44,16 @@ Instance::Instance(const InstanceType* type) { Instance::~Instance () { } -template -std::weak_ptr optional_to_weak(std::optional> a) { - return a ? a.value() : std::weak_ptr(); -} - // TODO: Test this -bool Instance::ancestryContinuityCheck(std::optional> newParent) { - for (std::optional> currentParent = newParent; currentParent.has_value(); currentParent = currentParent.value()->GetParent()) { - if (currentParent.value() == this->shared_from_this()) +bool Instance::ancestryContinuityCheck(nullable std::shared_ptr newParent) { + for (std::shared_ptr currentParent = newParent; currentParent != nullptr; currentParent = currentParent->GetParent()) { + if (currentParent == this->shared_from_this()) return false; } return true; } -bool Instance::SetParent(std::optional> newParent) { +bool Instance::SetParent(nullable std::shared_ptr newParent) { if (this->parentLocked || !ancestryContinuityCheck(newParent)) return false; @@ -70,10 +65,10 @@ bool Instance::SetParent(std::optional> newParent) { oldParent->children.erase(std::find(oldParent->children.begin(), oldParent->children.end(), this->shared_from_this())); } // Add ourselves to the new parent - if (newParent.has_value()) { - newParent.value()->children.push_back(this->shared_from_this()); + if (newParent != nullptr) { + newParent->children.push_back(this->shared_from_this()); } - this->parent = optional_to_weak(newParent); + this->parent = newParent; // TODO: Add code for sending signals for parent updates // TODO: Yeahhh maybe this isn't the best way of doing this? if (hierarchyPostUpdateHandler.has_value()) hierarchyPostUpdateHandler.value()(this->shared_from_this(), lastParent, newParent); @@ -85,21 +80,21 @@ bool Instance::SetParent(std::optional> newParent) { return true; } -void Instance::updateAncestry(std::optional> updatedChild, std::optional> newParent) { +void Instance::updateAncestry(nullable std::shared_ptr updatedChild, nullable std::shared_ptr newParent) { auto oldDataModel = _dataModel; auto oldWorkspace = _workspace; // Update parent data model and workspace, if applicable - if (GetParent()) { - this->_dataModel = GetParent().value()->GetClass() == &DataModel::TYPE ? std::dynamic_pointer_cast(GetParent().value()) : GetParent().value()->_dataModel; - this->_workspace = GetParent().value()->GetClass() == &Workspace::TYPE ? std::dynamic_pointer_cast(GetParent().value()) : GetParent().value()->_workspace; + if (GetParent() != nullptr) { + this->_dataModel = GetParent()->GetClass() == &DataModel::TYPE ? std::dynamic_pointer_cast(GetParent()) : GetParent()->_dataModel; + this->_workspace = GetParent()->GetClass() == &Workspace::TYPE ? std::dynamic_pointer_cast(GetParent()) : GetParent()->_workspace; } else { this->_dataModel = {}; this->_workspace = {}; } OnAncestryChanged(updatedChild, newParent); - AncestryChanged->Fire({updatedChild.has_value() ? InstanceRef(updatedChild.value()) : InstanceRef(), newParent.has_value() ? InstanceRef(newParent.value()) : InstanceRef()}); + AncestryChanged->Fire({updatedChild != nullptr ? InstanceRef(updatedChild) : InstanceRef(), newParent != nullptr ? InstanceRef(newParent) : InstanceRef()}); // Old workspace used to exist, and workspaces differ if (!oldWorkspace.expired() && oldWorkspace != _workspace) { @@ -108,7 +103,7 @@ void Instance::updateAncestry(std::optional> updatedCh // New workspace exists, and workspaces differ if (!_workspace.expired() && (_workspace != oldWorkspace)) { - OnWorkspaceAdded(!oldWorkspace.expired() ? std::make_optional(oldWorkspace.lock()) : std::nullopt, _workspace.lock()); + OnWorkspaceAdded(oldWorkspace.expired() ? nullptr : oldWorkspace.lock(), _workspace.lock()); } // Update ancestry in descendants @@ -117,23 +112,22 @@ void Instance::updateAncestry(std::optional> updatedCh } } -std::optional> Instance::dataModel() { - return (_dataModel.expired()) ? std::nullopt : std::make_optional(_dataModel.lock()); +nullable std::shared_ptr Instance::dataModel() { + return _dataModel.expired() ? nullptr : _dataModel.lock(); } -std::optional> Instance::workspace() { - return (_workspace.expired()) ? std::nullopt : std::make_optional(_workspace.lock()); +nullable std::shared_ptr Instance::workspace() { + return _workspace.expired() ? nullptr : _workspace.lock(); } -std::optional> Instance::GetParent() { - if (parent.expired()) return std::nullopt; - return parent.lock(); +nullable std::shared_ptr Instance::GetParent() { + return parent.expired() ? nullptr : parent.lock(); } void Instance::Destroy() { if (parentLocked) return; // TODO: Implement proper distruction stuff - SetParent(std::nullopt); + SetParent(nullptr); parentLocked = true; } @@ -143,12 +137,12 @@ bool Instance::IsA(std::string className) { return cur != nullptr; } -std::optional> Instance::FindFirstChild(std::string name) { +nullable std::shared_ptr Instance::FindFirstChild(std::string name) { for (auto child : children) { if (child->name == name) return child; } - return std::nullopt; + return nullptr; } static std::shared_ptr DUMMY_INSTANCE; @@ -164,15 +158,15 @@ bool Instance::IsParentLocked() { return this->parentLocked; } -void Instance::OnParentUpdated(std::optional> oldParent, std::optional> newParent) { +void Instance::OnParentUpdated(nullable std::shared_ptr oldParent, nullable std::shared_ptr newParent) { // Empty stub } -void Instance::OnAncestryChanged(std::optional> child, std::optional> newParent) { +void Instance::OnAncestryChanged(nullable std::shared_ptr child, nullable std::shared_ptr newParent) { // Empty stub } -void Instance::OnWorkspaceAdded(std::optional> oldWorkspace, std::shared_ptr newWorkspace) { +void Instance::OnWorkspaceAdded(nullable std::shared_ptr oldWorkspace, std::shared_ptr newWorkspace) { // Empty stub } @@ -230,7 +224,7 @@ fallible Instance::InternalSetPropertyVa this->name = (std::string)value.get(); } else if (name == "Parent") { std::weak_ptr ref = value.get(); - SetParent(ref.expired() ? std::nullopt : std::make_optional(ref.lock())); + SetParent(ref.expired() ? nullptr : ref.lock()); } else if (name == "ClassName") { return AssignToReadOnlyMember(GetClass()->className, name); } else { @@ -430,9 +424,9 @@ DescendantsIterator::self_type DescendantsIterator::operator++(int _) { } // If we've hit the end of this item's children, move one up - while (current->GetParent() && current->GetParent().value()->GetChildren().size() <= size_t(siblingIndex.back() + 1)) { + while (current->GetParent() != nullptr && current->GetParent()->GetChildren().size() <= size_t(siblingIndex.back() + 1)) { siblingIndex.pop_back(); - current = current->GetParent().value(); + current = current->GetParent(); // But not if one up is null or the root element if (!current->GetParent() || current == root) { @@ -443,12 +437,12 @@ DescendantsIterator::self_type DescendantsIterator::operator++(int _) { // Now move to the next sibling siblingIndex.back()++; - current = current->GetParent().value()->GetChildren()[siblingIndex.back()]; + current = current->GetParent()->GetChildren()[siblingIndex.back()]; return *this; } -std::optional> Instance::Clone(RefStateClone state) { +nullable std::shared_ptr Instance::Clone(RefStateClone state) { if (state == nullptr) state = std::make_shared<__RefStateClone>(); std::shared_ptr newInstance = GetClass()->constructor(); @@ -494,9 +488,9 @@ std::optional> Instance::Clone(RefStateClone state) { // Clone children for (std::shared_ptr child : GetChildren()) { - std::optional> clonedChild = child->Clone(state); + nullable std::shared_ptr clonedChild = child->Clone(state); if (clonedChild) - newInstance->AddChild(clonedChild.value()); + newInstance->AddChild(clonedChild); } return newInstance; @@ -521,12 +515,12 @@ std::vector>> Instance::GetRefe std::string Instance::GetFullName() { std::string currentName = name; - std::optional> currentParent = GetParent(); + nullable std::shared_ptr currentParent = GetParent(); - while (currentParent.has_value() && !currentParent.value()->IsA("DataModel")) { - currentName = currentParent.value()->name + "." + currentName; + while (currentParent && !currentParent->IsA("DataModel")) { + currentName = currentParent->name + "." + currentName; - currentParent = currentParent.value()->GetParent(); + currentParent = currentParent->GetParent(); } return currentName; diff --git a/core/src/objects/base/instance.h b/core/src/objects/base/instance.h index e565380..3f57242 100644 --- a/core/src/objects/base/instance.h +++ b/core/src/objects/base/instance.h @@ -16,6 +16,7 @@ #include "error/result.h" #include "member.h" #include "objects/base/refstate.h" +#include "utils.h" class Instance; typedef std::shared_ptr(*InstanceConstructor)(); @@ -59,8 +60,8 @@ private: std::weak_ptr _dataModel; std::weak_ptr _workspace; - bool ancestryContinuityCheck(std::optional> newParent); - void updateAncestry(std::optional> child, std::optional> newParent); + bool ancestryContinuityCheck(nullable std::shared_ptr newParent); + void updateAncestry(nullable std::shared_ptr child, nullable std::shared_ptr newParent); friend JointInstance; // This isn't ideal, but oh well protected: @@ -75,17 +76,17 @@ protected: virtual void InternalUpdateProperty(std::string name); virtual std::vector InternalGetProperties(); - virtual void OnParentUpdated(std::optional> oldParent, std::optional> newParent); - virtual void OnAncestryChanged(std::optional> child, std::optional> newParent); - virtual void OnWorkspaceAdded(std::optional> oldWorkspace, std::shared_ptr newWorkspace); + virtual void OnParentUpdated(nullable std::shared_ptr oldParent, nullable std::shared_ptr newParent); + virtual void OnAncestryChanged(nullable std::shared_ptr child, nullable std::shared_ptr newParent); + virtual void OnWorkspaceAdded(nullable std::shared_ptr oldWorkspace, std::shared_ptr newWorkspace); virtual void OnWorkspaceRemoved(std::shared_ptr oldWorkspace); // The root data model this object is a descendant of - std::optional> dataModel(); + nullable std::shared_ptr dataModel(); // The root workspace this object is a descendant of // NOTE: This value is not necessarily present if dataModel is present // Objects under services other than workspace will NOT have this field set - std::optional> workspace(); + nullable std::shared_ptr workspace(); public: const static InstanceType TYPE; std::string name; @@ -96,8 +97,8 @@ public: // Instance is abstract, so it should not implement GetClass directly virtual const InstanceType* GetClass() = 0; static void PushLuaLibrary(lua_State*); // Defined in lua/instancelib.cpp - bool SetParent(std::optional> newParent); - std::optional> GetParent(); + bool SetParent(nullable std::shared_ptr newParent); + nullable std::shared_ptr GetParent(); bool IsParentLocked(); inline const std::vector> GetChildren() { return children; } void Destroy(); @@ -111,7 +112,7 @@ public: DescendantsIterator GetDescendantsEnd(); // Utility functions inline void AddChild(std::shared_ptr object) { object->SetParent(this->shared_from_this()); } - std::optional> FindFirstChild(std::string); + nullable std::shared_ptr FindFirstChild(std::string); std::string GetFullName(); // Properties @@ -136,7 +137,7 @@ public: // Serialization void Serialize(pugi::xml_node parent, RefStateSerialize state = {}); static result, NoSuchInstance> Deserialize(pugi::xml_node node, RefStateDeserialize state = {}); - std::optional> Clone(RefStateClone state = {}); + nullable std::shared_ptr Clone(RefStateClone state = {}); }; // https://gist.github.com/jeetsukumaran/307264 @@ -158,7 +159,7 @@ public: self_type operator++(int _); private: - std::optional> root; + nullable std::shared_ptr root; std::shared_ptr current; std::vector siblingIndex; }; \ No newline at end of file diff --git a/core/src/objects/base/service.cpp b/core/src/objects/base/service.cpp index 54faaba..e21b574 100644 --- a/core/src/objects/base/service.cpp +++ b/core/src/objects/base/service.cpp @@ -7,9 +7,9 @@ Service::Service(const InstanceType* type) : Instance(type) {} // Fail if parented to non-datamodel, otherwise lock parent -void Service::OnParentUpdated(std::optional> oldParent, std::optional> newParent) { - if (!newParent || newParent.value()->GetClass() != &DataModel::TYPE) { - Logger::fatalErrorf("Service %s was parented to object of type %s", GetClass()->className.c_str(), newParent ? newParent.value()->GetClass()->className.c_str() : "NULL"); +void Service::OnParentUpdated(nullable std::shared_ptr oldParent, nullable std::shared_ptr newParent) { + if (!newParent || newParent->GetClass() != &DataModel::TYPE) { + Logger::fatalErrorf("Service %s was parented to object of type %s", GetClass()->className.c_str(), newParent ? newParent->GetClass()->className.c_str() : "NULL"); panic(); } diff --git a/core/src/objects/base/service.h b/core/src/objects/base/service.h index c519741..3be2082 100644 --- a/core/src/objects/base/service.h +++ b/core/src/objects/base/service.h @@ -13,7 +13,7 @@ protected: virtual void InitService(); virtual void OnRun(); - void OnParentUpdated(std::optional> oldParent, std::optional> newParent) override; + void OnParentUpdated(nullable std::shared_ptr oldParent, nullable std::shared_ptr newParent) override; friend class DataModel; }; \ No newline at end of file diff --git a/core/src/objects/datamodel.cpp b/core/src/objects/datamodel.cpp index 899e071..1a2f277 100644 --- a/core/src/objects/datamodel.cpp +++ b/core/src/objects/datamodel.cpp @@ -107,14 +107,14 @@ result, NoSuchService> DataModel::GetService(std::strin return std::dynamic_pointer_cast(services[className]); } -result>, NoSuchService> DataModel::FindService(std::string className) { +result, NoSuchService> DataModel::FindService(std::string className) { if (!INSTANCE_MAP[className] || (INSTANCE_MAP[className]->flags ^ (INSTANCE_NOTCREATABLE | INSTANCE_SERVICE)) != 0) { return NoSuchService(className); } if (services.count(className) != 0) - return std::make_optional(std::dynamic_pointer_cast(services[className])); - return (std::optional>)std::nullopt; + return std::dynamic_pointer_cast(services[className]); + return nullptr; } std::shared_ptr DataModel::CloneModel() { @@ -166,11 +166,11 @@ std::shared_ptr DataModel::CloneModel() { if (!result) continue; - newModel->AddChild(result.value()); + newModel->AddChild(result); // Special case: Ignore instances parented to DataModel which are not services if (child->GetClass()->flags & INSTANCE_SERVICE) { - newModel->services[child->GetClass()->className] = std::dynamic_pointer_cast(result.value()); + newModel->services[child->GetClass()->className] = std::dynamic_pointer_cast(result); } } diff --git a/core/src/objects/datamodel.h b/core/src/objects/datamodel.h index 236680e..a744772 100644 --- a/core/src/objects/datamodel.h +++ b/core/src/objects/datamodel.h @@ -29,7 +29,7 @@ public: static inline std::shared_ptr New() { return std::make_shared(); }; result, NoSuchService> GetService(std::string className); - result>, NoSuchService> FindService(std::string className); + result, NoSuchService> FindService(std::string className); template std::shared_ptr GetService() { @@ -38,10 +38,10 @@ public: } template - std::optional> FindService() { + nullable std::shared_ptr FindService() { auto result = FindService(T::TYPE.className).expect("FindService() was called with a non-service instance type"); - if (!result) return std::nullopt; - return std::dynamic_pointer_cast(result.value()); + if (!result) return nullptr; + return std::dynamic_pointer_cast(result); } // Saving/loading diff --git a/core/src/objects/joint/jointinstance.cpp b/core/src/objects/joint/jointinstance.cpp index e425a06..06425b6 100644 --- a/core/src/objects/joint/jointinstance.cpp +++ b/core/src/objects/joint/jointinstance.cpp @@ -17,7 +17,7 @@ JointInstance::JointInstance(const InstanceType* type): Instance(type) { JointInstance::~JointInstance() { } -void JointInstance::OnAncestryChanged(std::optional>, std::optional>) { +void JointInstance::OnAncestryChanged(nullable std::shared_ptr, nullable std::shared_ptr) { // Destroy and rebuild the joint, it's the simplest solution that actually works breakJoint(); @@ -54,6 +54,6 @@ void JointInstance::onUpdated(std::string property) { oldPart1 = part1; } -std::optional> JointInstance::workspaceOfPart(std::shared_ptr part) { +nullable std::shared_ptr JointInstance::workspaceOfPart(std::shared_ptr part) { return part->workspace(); } \ No newline at end of file diff --git a/core/src/objects/joint/jointinstance.h b/core/src/objects/joint/jointinstance.h index 3b57ab4..c89ec8e 100644 --- a/core/src/objects/joint/jointinstance.h +++ b/core/src/objects/joint/jointinstance.h @@ -23,9 +23,9 @@ protected: // The workspace the joint was created in, if it exists std::weak_ptr jointWorkspace; - void OnAncestryChanged(std::optional>, std::optional>) override; + void OnAncestryChanged(nullable std::shared_ptr, nullable std::shared_ptr) override; - std::optional> workspaceOfPart(std::shared_ptr); + nullable std::shared_ptr workspaceOfPart(std::shared_ptr); void onUpdated(std::string property); virtual void buildJoint() = 0; virtual void breakJoint() = 0; diff --git a/core/src/objects/joint/rotate.cpp b/core/src/objects/joint/rotate.cpp index a2623f4..61df92a 100644 --- a/core/src/objects/joint/rotate.cpp +++ b/core/src/objects/joint/rotate.cpp @@ -17,9 +17,9 @@ void Rotate::buildJoint() { if (part0.expired() || part1.expired() || part0.lock() == part1.lock() || !workspaceOfPart(part0.lock()) || workspaceOfPart(part0.lock()) != workspaceOfPart(part1.lock())) return; // Don't build the joint if we're not part of either a workspace or JointsService - if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return; + if ((!GetParent() || GetParent()->GetClass() != &JointsService::TYPE) && workspace() != nullptr) return; - std::shared_ptr workspace = workspaceOfPart(part0.lock()).value(); + std::shared_ptr workspace = workspaceOfPart(part0.lock()); // Update Part1's rotation and cframe prior to creating the joint as reactphysics3d locks rotation based on how it // used to be rather than specifying an anchor rotation, so whatever. diff --git a/core/src/objects/joint/rotatev.cpp b/core/src/objects/joint/rotatev.cpp index 11a9faf..6af12a1 100644 --- a/core/src/objects/joint/rotatev.cpp +++ b/core/src/objects/joint/rotatev.cpp @@ -19,9 +19,9 @@ void RotateV::buildJoint() { if (part0.expired() || part1.expired() || part0.lock() == part1.lock() || !workspaceOfPart(part0.lock()) || workspaceOfPart(part0.lock()) != workspaceOfPart(part1.lock())) return; // Don't build the joint if we're not part of either a workspace or JointsService - if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return; + if ((!GetParent() || GetParent()->GetClass() != &JointsService::TYPE) && workspace() != nullptr) return; - std::shared_ptr workspace = workspaceOfPart(part0.lock()).value(); + std::shared_ptr workspace = workspaceOfPart(part0.lock()); // Update Part1's rotation and cframe prior to creating the joint as reactphysics3d locks rotation based on how it diff --git a/core/src/objects/joint/snap.cpp b/core/src/objects/joint/snap.cpp index 90c405a..55b510d 100644 --- a/core/src/objects/joint/snap.cpp +++ b/core/src/objects/joint/snap.cpp @@ -21,9 +21,9 @@ void Snap::buildJoint() { if (part0.expired() || part1.expired() || part0.lock() == part1.lock() || !workspaceOfPart(part0.lock()) || workspaceOfPart(part0.lock()) != workspaceOfPart(part1.lock())) return; // Don't build the joint if we're not part of either a workspace or JointsService - if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return; + if ((!GetParent() || GetParent()->GetClass() != &JointsService::TYPE) && !workspace()) return; - std::shared_ptr workspace = workspaceOfPart(part0.lock()).value(); + std::shared_ptr workspace = workspaceOfPart(part0.lock()); // Update Part1's rotation and cframe prior to creating the joint as reactphysics3d locks rotation based on how it // used to be rather than specifying an anchor rotation, so whatever. diff --git a/core/src/objects/joint/weld.cpp b/core/src/objects/joint/weld.cpp index 79e2e6f..87826e6 100644 --- a/core/src/objects/joint/weld.cpp +++ b/core/src/objects/joint/weld.cpp @@ -21,9 +21,9 @@ void Weld::buildJoint() { if (part0.expired() || part1.expired() || part0.lock() == part1.lock() || !workspaceOfPart(part0.lock()) || workspaceOfPart(part0.lock()) != workspaceOfPart(part1.lock())) return; // Don't build the joint if we're not part of either a workspace or JointsService - if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return; + if ((!GetParent() || GetParent()->GetClass() != &JointsService::TYPE) && workspace() != nullptr) return; - std::shared_ptr workspace = workspaceOfPart(part0.lock()).value(); + std::shared_ptr workspace = workspaceOfPart(part0.lock()); // Update Part1's rotation and cframe prior to creating the joint as reactphysics3d locks rotation based on how it // used to be rather than specifying an anchor rotation, so whatever. diff --git a/core/src/objects/part/basepart.cpp b/core/src/objects/part/basepart.cpp index a21d2a7..856e91d 100644 --- a/core/src/objects/part/basepart.cpp +++ b/core/src/objects/part/basepart.cpp @@ -14,7 +14,6 @@ #include "objects/joint/snap.h" #include "rendering/renderer.h" #include "enum/surface.h" -#include #include #include #include @@ -28,18 +27,18 @@ BasePart::BasePart(const InstanceType* type, PartConstructParams params): PVInst BasePart::~BasePart() { // This relies on physicsCommon still existing. Be very careful. - if (this->rigidBody && workspace()) { - workspace().value()->RemoveBody(shared()); + if (this->rigidBody && workspace() != nullptr) { + workspace()->RemoveBody(shared()); } } -void BasePart::OnAncestryChanged(std::optional> child, std::optional> newParent) { +void BasePart::OnAncestryChanged(nullable std::shared_ptr child, nullable std::shared_ptr newParent) { if (this->rigidBody) - this->rigidBody->setIsActive(workspace().has_value()); + this->rigidBody->setIsActive(workspace() != nullptr); - if (workspace()) - workspace().value()->SyncPartPhysics(std::dynamic_pointer_cast(this->shared_from_this())); + if (workspace() != nullptr) + workspace()->SyncPartPhysics(std::dynamic_pointer_cast(this->shared_from_this())); // Destroy joints if (!workspace()) BreakJoints(); @@ -47,7 +46,7 @@ void BasePart::OnAncestryChanged(std::optional> child, // TODO: Sleeping bodies that touch this one also need to be updated } -void BasePart::OnWorkspaceAdded(std::optional> oldWorkspace, std::shared_ptr newWorkspace) { +void BasePart::OnWorkspaceAdded(nullable std::shared_ptr oldWorkspace, std::shared_ptr newWorkspace) { newWorkspace->AddBody(shared()); } @@ -58,8 +57,8 @@ void BasePart::OnWorkspaceRemoved(std::shared_ptr oldWorkspace) { void BasePart::onUpdated(std::string property) { bool reset = property == "Position" || property == "Rotation" || property == "CFrame" || property == "Size" || property == "Shape"; - if (workspace()) - workspace().value()->SyncPartPhysics(std::dynamic_pointer_cast(this->shared_from_this())); + if (workspace() != nullptr) + workspace()->SyncPartPhysics(std::dynamic_pointer_cast(this->shared_from_this())); // When position/rotation/size is manually edited, break all joints, they don't apply anymore if (reset) @@ -204,7 +203,7 @@ bool BasePart::checkSurfacesTouching(CFrame surfaceFrame, Vector3 size, Vector3 return horizOverlap && vertOverlap; } -std::optional> makeJointFromSurfaces(SurfaceType a, SurfaceType b) { +nullable std::shared_ptr makeJointFromSurfaces(SurfaceType a, SurfaceType b) { if (a == SurfaceType::Weld || b == SurfaceType::Weld || a == SurfaceType::Glue || b == SurfaceType::Glue) return Weld::New(); if ((a == SurfaceType::Studs && (b == SurfaceType::Inlet || b == SurfaceType::Universal)) || (a == SurfaceType::Inlet && (b == SurfaceType::Studs || b == SurfaceType::Universal)) @@ -214,7 +213,7 @@ std::optional> makeJointFromSurfaces(SurfaceType return Rotate::New(); if (a == SurfaceType::Motor) return RotateV::New(); - return std::nullopt; + return nullptr; } void BasePart::MakeJoints() { @@ -229,7 +228,7 @@ void BasePart::MakeJoints() { // TEMPORARY // TODO: Use more efficient algorithm to *actually* find nearby parts) - for (auto it = workspace().value()->GetDescendantsStart(); it != workspace().value()->GetDescendantsEnd(); it++) { + for (auto it = workspace()->GetDescendantsStart(); it != workspace()->GetDescendantsEnd(); it++) { std::shared_ptr obj = *it; if (obj == shared_from_this()) continue; // Skip ourselves if (!obj->IsA()) continue; @@ -276,12 +275,12 @@ void BasePart::MakeJoints() { auto joint_ = makeJointFromSurfaces(mySurface, otherSurface); if (!joint_) continue; - std::shared_ptr joint = joint_.value(); + std::shared_ptr joint = joint_; joint->part0 = shared(); joint->part1 = otherPart->shared(); joint->c0 = contact0; joint->c1 = contact1; - dataModel().value()->GetService()->AddChild(joint); + dataModel()->GetService()->AddChild(joint); joint->UpdateProperty("Part0"); Logger::debugf("Made joint between %s and %s!\n", name.c_str(), otherPart->name.c_str()); diff --git a/core/src/objects/part/basepart.h b/core/src/objects/part/basepart.h index 8564571..ce837bd 100644 --- a/core/src/objects/part/basepart.h +++ b/core/src/objects/part/basepart.h @@ -54,9 +54,9 @@ protected: friend JointInstance; friend PhysWorld; - virtual void OnWorkspaceAdded(std::optional> oldWorkspace, std::shared_ptr newWorkspace) override; + virtual void OnWorkspaceAdded(nullable std::shared_ptr oldWorkspace, std::shared_ptr newWorkspace) override; virtual void OnWorkspaceRemoved(std::shared_ptr oldWorkspace) override; - void OnAncestryChanged(std::optional> child, std::optional> newParent) override; + void OnAncestryChanged(nullable std::shared_ptr child, nullable std::shared_ptr newParent) override; void onUpdated(std::string); virtual void updateCollider(rp::PhysicsCommon* common) = 0; diff --git a/core/src/objects/script.cpp b/core/src/objects/script.cpp index 994c824..b405deb 100644 --- a/core/src/objects/script.cpp +++ b/core/src/objects/script.cpp @@ -23,7 +23,7 @@ Script::~Script() { } void Script::Run() { - std::shared_ptr scriptContext = dataModel().value()->GetService(); + std::shared_ptr scriptContext = dataModel()->GetService(); lua_State* L = scriptContext->state; int top = lua_gettop(L); diff --git a/core/src/objects/service/jointsservice.cpp b/core/src/objects/service/jointsservice.cpp index 4c535e4..820ea8d 100644 --- a/core/src/objects/service/jointsservice.cpp +++ b/core/src/objects/service/jointsservice.cpp @@ -18,8 +18,8 @@ void JointsService::InitService() { } } -std::optional> JointsService::jointWorkspace() { - if (!dataModel()) return std::nullopt; +nullable std::shared_ptr JointsService::jointWorkspace() { + if (!dataModel()) return nullptr; - return dataModel().value()->FindService(); + return dataModel()->FindService(); } \ No newline at end of file diff --git a/core/src/objects/service/jointsservice.h b/core/src/objects/service/jointsservice.h index 0bcc3c2..c606d18 100644 --- a/core/src/objects/service/jointsservice.h +++ b/core/src/objects/service/jointsservice.h @@ -6,7 +6,7 @@ class DEF_INST_SERVICE_(hidden) JointsService : public Service { AUTOGEN_PREAMBLE private: - std::optional> jointWorkspace(); + nullable std::shared_ptr jointWorkspace(); protected: void InitService() override; bool initialized = false; diff --git a/core/src/objects/service/script/scriptcontext.cpp b/core/src/objects/service/script/scriptcontext.cpp index 9515dff..05dc1f9 100644 --- a/core/src/objects/service/script/scriptcontext.cpp +++ b/core/src/objects/service/script/scriptcontext.cpp @@ -59,10 +59,10 @@ void ScriptContext::InitService() { // Add other globals lua_getglobal(state, "_G"); - InstanceRef(dataModel().value()).PushLuaValue(state); + InstanceRef(dataModel()).PushLuaValue(state); lua_setfield(state, -2, "game"); - InstanceRef(dataModel().value()->GetService()).PushLuaValue(state); + InstanceRef(dataModel()->GetService()).PushLuaValue(state); lua_setfield(state, -2, "workspace"); lua_pushlightuserdata(state, this); diff --git a/core/src/objects/service/script/serverscriptservice.cpp b/core/src/objects/service/script/serverscriptservice.cpp index 29e1b40..22c3400 100644 --- a/core/src/objects/service/script/serverscriptservice.cpp +++ b/core/src/objects/service/script/serverscriptservice.cpp @@ -14,7 +14,7 @@ void ServerScriptService::InitService() { } void ServerScriptService::OnRun() { - auto workspace = dataModel().value()->GetService(); + auto workspace = dataModel()->GetService(); for (auto it = workspace->GetDescendantsStart(); it != workspace->GetDescendantsEnd(); it++) { if (!it->IsA