/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #include "StandaloneTools_precompiled.h" #include #include #include #include #include #include #include #include "Workspace.h" namespace Driller { // this is called when the settings are loaded from file. Since the root element is just a workspace settings provider object, // we verify the cast and return it: static void OnObjectLoaded(void* classPtr, const AZ::Uuid& classId, const AZ::SerializeContext* sc, WorkspaceSettingsProvider** target) { AZ_Assert(classPtr, "classPtr is NULL!"); AZ_Assert(target, "You must pass the target to OnObjectLoaded"); WorkspaceSettingsProvider* container = sc->Cast(classPtr, classId); AZ_Assert(container, "Failed to cast classPtr to WorkspaceSettingsProvider!"); *target = container; } // remember to delete your setting objects on shutdown, since you own them! WorkspaceSettingsProvider::~WorkspaceSettingsProvider() { for (auto it = m_WorkspaceSaveData.begin(); it != m_WorkspaceSaveData.end(); ++it) { delete it->second; } } /// Given a filename, attempt to deserialize a WorkspaceSettingsProvider object from it, using the DH objectstream: WorkspaceSettingsProvider* WorkspaceSettingsProvider::CreateFromFile(const AZStd::string& filename) { using namespace AZ; SerializeContext* sc = NULL; EBUS_EVENT_RESULT(sc, ComponentApplicationBus, GetSerializeContext); AZ_Assert(sc, "Can't retrieve application's serialization context!"); WorkspaceSettingsProvider* resultItem = NULL; IO::FileIOStream readStream(filename.c_str(), AZ::IO::OpenMode::ModeRead); if (readStream.IsOpen()) { ObjectStream::ClassReadyCB readyCB(AZStd::bind(&OnObjectLoaded, AZStd::placeholders::_1, AZStd::placeholders::_2, AZStd::placeholders::_3, &resultItem)); if (!ObjectStream::LoadBlocking(&readStream, *sc, readyCB)) { AZ_TracePrintf("Driller", "
Failed to deserialize the workspace file: '%s'
", filename.c_str()); } } else { AZ_TracePrintf("Driller", "
Failed to open the given filename in order to read it in as a workspace: '%s'
", filename.c_str()); } // use the serializer to consume filename. return resultItem; } /// serializes this object into the given filename for later retrieval. bool WorkspaceSettingsProvider::WriteToFile(const AZStd::string& filename) { using namespace AZ; SerializeContext* sc = NULL; EBUS_EVENT_RESULT(sc, ComponentApplicationBus, GetSerializeContext); AZ_Assert(sc, "Can't retrieve application's serialization context!"); /// note: Will probably throw an error if it fails. Should check all these things before we call in here. IO::FileIOStream writeStream(filename.c_str(), AZ::IO::OpenMode::ModeWrite); if (!writeStream.IsOpen()) { // (will have already called AZ_Error) return false; } ObjectStream* objStream = ObjectStream::Create(&writeStream, *sc, ObjectStream::ST_XML); bool writtenOk = objStream->WriteClass(this); if (!writtenOk) { AZ_TracePrintf("Driller", "
Failed to write the workspace object to the workspace file: '%s'
", filename.c_str()); return false; } bool streamOk = objStream->Finalize(); if (!streamOk) { AZ_TracePrintf("Driller", "
Failed to finalize the workspace file: '%s'
", filename.c_str()); return false; } return true; } void WorkspaceSettingsProvider::Reflect(AZ::ReflectContext* context) { AZ::SerializeContext* serialize = azrtti_cast(context); if (serialize) { serialize->Class() ->Version(2) ->Field("m_WorkspaceSaveData", &WorkspaceSettingsProvider::m_WorkspaceSaveData); } } }