From aae6b3c89d738683fb28d61936bc86060c6e2b4c Mon Sep 17 00:00:00 2001 From: CT5 <124994670+Cherrytree56567@users.noreply.github.com> Date: Mon, 26 Jan 2026 17:07:12 +1100 Subject: [PATCH] feat: finished adding winrt directory and file --- CMakeLists.txt | 4 +- .../Interfaces/Storage/Directory.h | 2 +- .../Interfaces/Storage/File.h | 2 +- .../CMakeLists.txt | 1 + .../Interfaces/Storage/Directory.h | 10 +- .../Interfaces/Storage/File.h | 12 +- .../src/interfaces/Storage/Directory.cpp | 167 ++++++++++++++++-- .../src/interfaces/Storage/File.cpp | 150 ++++++++++++++-- projects/WinDurango.KernelX/CMakeLists.txt | 1 - projects/WinDurango.Testing/App.cpp | 18 ++ .../WinDurango.Testing.vcxproj | 6 +- 11 files changed, 328 insertions(+), 45 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 21b4003..f398b92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/) add_subdirectory(projects/WinDurango.Common) add_subdirectory(projects/WinDurango.Implementation.WinRT) add_subdirectory(projects/WinDurango.Implementation.Native) -add_subdirectory(projects/WinDurango.WinRT) +#add_subdirectory(projects/WinDurango.WinRT) add_subdirectory(projects/WinDurango.etwplus) add_subdirectory(projects/WinDurango.KernelX) add_subdirectory(projects/WinDurango.D3D11X) @@ -20,7 +20,7 @@ add_custom_target(WinDurango ALL DEPENDS WinDurango.Common WinDurango.Implementation.WinRT WinDurango.Implementation.Native - WinDurango.WinRT + #WinDurango.WinRT WinDurango.etwplus WinDurango.KernelX WinDurango.D3D11X diff --git a/projects/WinDurango.Common/include/WinDurango.Common/Interfaces/Storage/Directory.h b/projects/WinDurango.Common/include/WinDurango.Common/Interfaces/Storage/Directory.h index dd647a0..4e940c9 100644 --- a/projects/WinDurango.Common/include/WinDurango.Common/Interfaces/Storage/Directory.h +++ b/projects/WinDurango.Common/include/WinDurango.Common/Interfaces/Storage/Directory.h @@ -20,7 +20,7 @@ namespace wd::common::interfaces::storage { virtual bool open() = 0; virtual File* CreateFile(std::filesystem::path path) = 0; // todo maybe return stream type, todo can we use this in uwp context??? I forgor - virtual Directory* CreateDirectory(std::filesystem::path path) = 0; + virtual Directory* CreateFolder(std::filesystem::path path) = 0; virtual std::filesystem::path dirpath() = 0; diff --git a/projects/WinDurango.Common/include/WinDurango.Common/Interfaces/Storage/File.h b/projects/WinDurango.Common/include/WinDurango.Common/Interfaces/Storage/File.h index 8b30813..50392c7 100644 --- a/projects/WinDurango.Common/include/WinDurango.Common/Interfaces/Storage/File.h +++ b/projects/WinDurango.Common/include/WinDurango.Common/Interfaces/Storage/File.h @@ -15,7 +15,7 @@ namespace wd::common::interfaces::storage { virtual bool open() = 0; virtual bool create() = 0; - virtual std::istream& read() = 0; + virtual std::string read() = 0; virtual void operator<<(std::string data) = 0; // write virtual bool close() = 0; diff --git a/projects/WinDurango.Implementation.WinRT/CMakeLists.txt b/projects/WinDurango.Implementation.WinRT/CMakeLists.txt index 116d927..9e82a18 100644 --- a/projects/WinDurango.Implementation.WinRT/CMakeLists.txt +++ b/projects/WinDurango.Implementation.WinRT/CMakeLists.txt @@ -17,6 +17,7 @@ add_compile_definitions(WDIMPL_API_EXPORTS) add_library(WinDurango.Implementation.WinRT SHARED ${FILES}) target_include_directories(WinDurango.Implementation.WinRT PUBLIC + $ $ ) diff --git a/projects/WinDurango.Implementation.WinRT/include/WinDurango.Implementation.WinRT/Interfaces/Storage/Directory.h b/projects/WinDurango.Implementation.WinRT/include/WinDurango.Implementation.WinRT/Interfaces/Storage/Directory.h index 7f1778e..0899219 100644 --- a/projects/WinDurango.Implementation.WinRT/include/WinDurango.Implementation.WinRT/Interfaces/Storage/Directory.h +++ b/projects/WinDurango.Implementation.WinRT/include/WinDurango.Implementation.WinRT/Interfaces/Storage/Directory.h @@ -3,10 +3,12 @@ #include #include #include -#include "WinDurango.h" +#include "WinDurango.Implementation.WinRT/WinDurangoWinRT.h" #include "WinDurango.Common/interfaces/Storage/Directory.h" +#include "WinDurango.Implementation.WinRT/Interfaces/Storage/File.h" using namespace winrt::Windows::Storage; +using namespace winrt; /* * I wonder if this is too confusing? @@ -14,11 +16,11 @@ using namespace winrt::Windows::Storage; namespace wd::impl::winrt::interfaces::storage { class WDIMPL_API WinRTDirectory : public wd::common::interfaces::storage::Directory { public: - WinRTDirectory(std::filesystem::path dirpath) : path(dirpath) {} + WinRTDirectory(std::filesystem::path dirpath) : path(dirpath), dir(nullptr) {} virtual bool open() override; virtual wd::common::interfaces::storage::File* CreateFile(std::filesystem::path path) override; - virtual wd::common::interfaces::storage::Directory* CreateDirectory(std::filesystem::path path) override; + virtual wd::common::interfaces::storage::Directory* CreateFolder(std::filesystem::path path) override; virtual std::filesystem::path dirpath() override; @@ -28,6 +30,6 @@ namespace wd::impl::winrt::interfaces::storage { virtual bool copy(std::filesystem::path path) override; private: std::filesystem::path path; - StorageFolder* dir = nullptr; + StorageFolder dir; }; } \ No newline at end of file diff --git a/projects/WinDurango.Implementation.WinRT/include/WinDurango.Implementation.WinRT/Interfaces/Storage/File.h b/projects/WinDurango.Implementation.WinRT/include/WinDurango.Implementation.WinRT/Interfaces/Storage/File.h index 35804a4..d8d18f6 100644 --- a/projects/WinDurango.Implementation.WinRT/include/WinDurango.Implementation.WinRT/Interfaces/Storage/File.h +++ b/projects/WinDurango.Implementation.WinRT/include/WinDurango.Implementation.WinRT/Interfaces/Storage/File.h @@ -1,17 +1,24 @@ #pragma once #include #include +#include +#include +#include +#include #include "WinDurango.Implementation.WinRT/WinDurangoWinRT.h" #include "WinDurango.Common/Interfaces/Storage/File.h" +using namespace winrt::Windows::Storage; +using namespace winrt; + namespace wd::impl::winrt::interfaces::storage { class WDIMPL_API WinRTFile : public wd::common::interfaces::storage::File { public: - WinRTFile(std::filesystem::path filepath) : path(filepath) {} + WinRTFile(std::filesystem::path filepath) : path(filepath), file(nullptr) {} virtual bool open() override; virtual bool create() override; - virtual std::istream& read() override; + virtual std::string read() override; virtual void operator<<(std::string data) override; // write virtual bool close() override; @@ -23,5 +30,6 @@ namespace wd::impl::winrt::interfaces::storage { virtual bool copy(std::filesystem::path path) override; private: std::filesystem::path path; + StorageFile file; }; } \ No newline at end of file diff --git a/projects/WinDurango.Implementation.WinRT/src/interfaces/Storage/Directory.cpp b/projects/WinDurango.Implementation.WinRT/src/interfaces/Storage/Directory.cpp index 8ddd836..1f37120 100644 --- a/projects/WinDurango.Implementation.WinRT/src/interfaces/Storage/Directory.cpp +++ b/projects/WinDurango.Implementation.WinRT/src/interfaces/Storage/Directory.cpp @@ -1,39 +1,180 @@ -#include "WinDurango.Implementation.WinRT/Interfaces/Storage/Directory.h" +#include "Interfaces/Storage/Directory.h" +/* + * This is a Dir Class and all the funcs inside + * are specifically for managing this dir. +*/ bool wd::impl::winrt::interfaces::storage::WinRTDirectory::open() { - StorageFolder sf = ApplicationData::Current().LocalFolder(); - if (sf) { - dir = &sf; - return true; + if (dir != nullptr) { + return false; + } + try { + StorageFolder sf = ApplicationData::Current().LocalFolder(); + + for (const auto& part : path) { + hstring partStr = hstring(part.wstring()); + sf = sf.GetFolderAsync(partStr).get(); + } + + if (sf) { + dir = sf; + return true; + } + } catch (const hresult_error& ex) { + return false; } return false; } -wd::common::interfaces::storage::File* wd::impl::winrt::interfaces::storage::WinRTDirectory::CreateFile(std::filesystem::path path) { - auto file = localFolder.CreateFileAsync(winrt::hstring(path.wstring()), winrt::Windows::Storage::CreationCollisionOption::FailIfExists).get(); - return nullptr; +/* + * Todo: + * Add logging + * + * TargetPath is the filename btw + * any path will be ignored. +*/ +wd::common::interfaces::storage::File* wd::impl::winrt::interfaces::storage::WinRTDirectory::CreateFile(std::filesystem::path targetPath) { + if (dir == nullptr) { + return nullptr; + } + try { + auto file = dir.CreateFileAsync(hstring(targetPath.filename().wstring()), CreationCollisionOption::OpenIfExists).get(); + wd::impl::winrt::interfaces::storage::WinRTFile* fileRT = new wd::impl::winrt::interfaces::storage::WinRTFile(path / targetPath.filename()); + return fileRT; + } + catch (const hresult_error& ex) { + return nullptr; + } } -wd::common::interfaces::storage::Directory* wd::impl::winrt::interfaces::storage::WinRTDirectory::CreateDirectory(std::filesystem::path path) { - return nullptr; +/* + * TODO: Use Unique Pointers +*/ +wd::common::interfaces::storage::Directory* wd::impl::winrt::interfaces::storage::WinRTDirectory::CreateFolder(std::filesystem::path targetPath) { + if (dir == nullptr) { + return nullptr; + } + try { + auto file = dir.CreateFolderAsync(hstring(targetPath.filename().wstring()), CreationCollisionOption::OpenIfExists).get(); + wd::impl::winrt::interfaces::storage::WinRTDirectory* folderRT = new wd::impl::winrt::interfaces::storage::WinRTDirectory(path / targetPath.filename()); + return folderRT; + } + catch (const hresult_error& ex) { + return nullptr; + } } std::filesystem::path wd::impl::winrt::interfaces::storage::WinRTDirectory::dirpath() { return path; } -bool wd::impl::winrt::interfaces::storage::WinRTDirectory::rename(std::string) { +bool wd::impl::winrt::interfaces::storage::WinRTDirectory::rename(std::string newName) { + if (dir == nullptr) { + return false; + } + try { + dir.RenameAsync(to_hstring(newName)).get(); + path.replace_filename(newName); + return true; + } + catch (const hresult_error& ex) { + return false; + } return false; } bool wd::impl::winrt::interfaces::storage::WinRTDirectory::remove() { + if (dir == nullptr) { + return false; + } + try { + dir.DeleteAsync().get(); + return true; + } + catch (const hresult_error& ex) { + return false; + } return false; } -bool wd::impl::winrt::interfaces::storage::WinRTDirectory::move(std::filesystem::path path) { +bool MoveFolder(StorageFolder source, StorageFolder destinationContainer) { + try { + StorageFolder destinationFolder = destinationContainer.CreateFolderAsync(source.Name(), CreationCollisionOption::OpenIfExists).get(); + + for (auto file : source.GetFilesAsync().get()) { + file.MoveAsync(destinationFolder, file.Name(), NameCollisionOption::GenerateUniqueName).get(); + } + + for (auto folder : source.GetFoldersAsync().get()) { + MoveFolder(folder, destinationFolder); + } + return true; + } + catch (const hresult_error& ex) { + return false; + } +} + +bool wd::impl::winrt::interfaces::storage::WinRTDirectory::move(std::filesystem::path targetPath) { + if (dir == nullptr) { + return false; + } + try { + StorageFolder target = ApplicationData::Current().LocalFolder(); + + for (const auto& part : targetPath) { + hstring partStr = hstring(part.wstring()); + target = target.GetFolderAsync(partStr).get(); + } + + if (MoveFolder(dir, target)) { + path = targetPath; + return true; + } + } + catch (const hresult_error& ex) { + return false; + } return false; } -bool wd::impl::winrt::interfaces::storage::WinRTDirectory::copy(std::filesystem::path path) { +bool CopyFolder(StorageFolder source, StorageFolder destinationContainer) { + try { + StorageFolder destinationFolder = destinationContainer.CreateFolderAsync(source.Name(), CreationCollisionOption::OpenIfExists).get(); + + for (auto file : source.GetFilesAsync().get()) { + file.CopyAsync(destinationFolder, file.Name(), NameCollisionOption::GenerateUniqueName).get(); + } + + for (auto folder : source.GetFoldersAsync().get()) { + CopyFolder(folder, destinationFolder); + } + return true; + } + catch (const hresult_error& ex) { + return false; + } +} + +bool wd::impl::winrt::interfaces::storage::WinRTDirectory::copy(std::filesystem::path targetPath) { + if (dir == nullptr) { + return false; + } + try { + StorageFolder target = ApplicationData::Current().LocalFolder(); + + for (const auto& part : targetPath) { + hstring partStr = hstring(part.wstring()); + target = target.GetFolderAsync(partStr).get(); + } + + if (CopyFolder(dir, target)) { + path = targetPath; + return true; + } + } + catch (const hresult_error& ex) { + return false; + } return false; } \ No newline at end of file diff --git a/projects/WinDurango.Implementation.WinRT/src/interfaces/Storage/File.cpp b/projects/WinDurango.Implementation.WinRT/src/interfaces/Storage/File.cpp index 4735ab1..9834bc6 100644 --- a/projects/WinDurango.Implementation.WinRT/src/interfaces/Storage/File.cpp +++ b/projects/WinDurango.Implementation.WinRT/src/interfaces/Storage/File.cpp @@ -1,36 +1,89 @@ -#include "WinDurango.Implementation.WinRT/interfaces/Storage/File.h" +#include "interfaces/Storage/File.h" namespace wd::impl::winrt::interfaces::storage { bool WinRTFile::open() { + if (file != nullptr) { + return false; + } + try { + StorageFolder sf = ApplicationData::Current().LocalFolder(); + + for (const auto& part : path.parent_path()) { + hstring partStr = hstring(part.wstring()); + sf = sf.GetFolderAsync(partStr).get(); + } + if (!sf) { + return false; + } + + StorageFile npFile = sf.GetFileAsync(hstring(path.filename().wstring())).get(); + file = npFile; + if (!npFile) { + return false; + } + } catch (const hresult_error& ex) { + return false; + } return false; } bool WinRTFile::create() { + if (file != nullptr) { + return false; + } + try { + StorageFolder sf = ApplicationData::Current().LocalFolder(); + + for (const auto& part : path.parent_path()) { + hstring partStr = hstring(part.wstring()); + sf = sf.GetFolderAsync(partStr).get(); + } + if (!sf) { + return false; + } + + file = sf.CreateFileAsync(hstring(path.filename().wstring()), CreationCollisionOption::OpenIfExists).get(); + if (!file) { + return false; + } + return true; + } catch (const hresult_error& ex) { + return false; + } return false; } - /* - * From Stack Overflow - * Why did this take me 4 hours to figure out?? + * btw these docs are pretty useful + * https://learn.microsoft.com/en-us/windows/uwp/get-started/fileio-learning-track */ - struct MyIstream : std::streambuf { - public: - int_type underflow() override { - return traits_type::eof(); + std::string WinRTFile::read() { + if (file == nullptr) { + return "Failed"; } - }; - - std::istream& WinRTFile::read() { - MyIstream nullp; - std::istream np(&nullp); - return np; + try { + return to_string(FileIO::ReadTextAsync(file).get()); + } catch (const hresult_error& ex) { + return "Failed"; + } + return "Failed"; } void WinRTFile::operator<<(std::string data) { - + if (file == nullptr) { + return; + } + try { + FileIO::WriteTextAsync(file, to_hstring(data)).get(); + } catch (const hresult_error& ex) { + return; + } } bool WinRTFile::close() { + if (file == nullptr) { + return false; + } + file = nullptr; return false; } @@ -38,19 +91,80 @@ namespace wd::impl::winrt::interfaces::storage { return path; } - bool WinRTFile::rename(std::string) { + bool WinRTFile::rename(std::string name) { + if (file == nullptr) { + return false; + } + try { + file.RenameAsync(to_hstring(name), NameCollisionOption::GenerateUniqueName).get(); + path.replace_filename(name); + return true; + } catch (const hresult_error& ex) { + return false; + } return false; } bool WinRTFile::remove() { + if (file == nullptr) { + return false; + } + try { + file.DeleteAsync().get(); + return true; + } catch (const hresult_error& ex) { + return false; + } return false; } - bool WinRTFile::move(std::filesystem::path path) { + bool WinRTFile::move(std::filesystem::path targetPath) { + if (file == nullptr) { + return false; + } + try { + StorageFolder target = ApplicationData::Current().LocalFolder(); + + for (const auto& part : targetPath.parent_path()) { + hstring partStr = hstring(part.wstring()); + target = target.GetFolderAsync(partStr).get(); + } + if (!target) { + return false; + } + + file.MoveAsync(target).get(); + + std::filesystem::path filename = path.filename(); + path = targetPath; + path.replace_filename(filename); + return true; + } catch (const hresult_error& ex) { + return false; + } return false; } - bool WinRTFile::copy(std::filesystem::path path) { + bool WinRTFile::copy(std::filesystem::path targetPath) { + if (file == nullptr) { + return false; + } + try { + StorageFolder target = ApplicationData::Current().LocalFolder(); + + for (const auto& part : targetPath.parent_path()) { + hstring partStr = hstring(part.wstring()); + target = target.GetFolderAsync(partStr).get(); + } + if (!target) { + return false; + } + + file.CopyAsync(target).get(); + return true; + } catch (const hresult_error& ex) { + return false; + } return false; } } \ No newline at end of file diff --git a/projects/WinDurango.KernelX/CMakeLists.txt b/projects/WinDurango.KernelX/CMakeLists.txt index 7827282..2382897 100644 --- a/projects/WinDurango.KernelX/CMakeLists.txt +++ b/projects/WinDurango.KernelX/CMakeLists.txt @@ -16,7 +16,6 @@ target_link_libraries(WinDurango.KernelX PRIVATE WinDurango.Common) target_include_directories(WinDurango.KernelX PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include/WinDurango.KernelX/ - ../WinDurango.Common/include/ ) set_target_properties(WinDurango.KernelX PROPERTIES diff --git a/projects/WinDurango.Testing/App.cpp b/projects/WinDurango.Testing/App.cpp index 8c93d68..9e8b12e 100644 --- a/projects/WinDurango.Testing/App.cpp +++ b/projects/WinDurango.Testing/App.cpp @@ -1,8 +1,11 @@ #include "pch.h" +#include "WinDurango.Implementation.WinRT/Interfaces/Storage/Directory.h" +#include using namespace winrt; using namespace Windows; +using namespace Windows::ApplicationModel; using namespace Windows::ApplicationModel::Core; using namespace Windows::Foundation::Numerics; using namespace Windows::UI; @@ -15,6 +18,7 @@ struct App : implements VisualCollection m_visuals{ nullptr }; Visual m_selected{ nullptr }; float2 m_offset{}; + bool runFirst = true; IFrameworkView CreateView() { @@ -88,6 +92,20 @@ struct App : implements { AddVisual(point); } + if (runFirst) + { + Package pkg = Package::Current(); + hstring id = pkg.Id().FamilyName(); + std::wcout << L"Name: " << id.c_str() << L"\n"; + wd::impl::winrt::interfaces::storage::WinRTDirectory dir(""); + dir.open(); + auto wddir = dir.CreateFolder("WinDurango"); + wddir->open(); + auto wdlog = wddir->CreateFile("log.txt"); + wdlog->open(); + (*wdlog) << std::string("Testing"); + runFirst = false; + } } void OnPointerMoved(IInspectable const &, PointerEventArgs const & args) diff --git a/projects/WinDurango.Testing/WinDurango.Testing.vcxproj b/projects/WinDurango.Testing/WinDurango.Testing.vcxproj index b131c45..18f4c31 100644 --- a/projects/WinDurango.Testing/WinDurango.Testing.vcxproj +++ b/projects/WinDurango.Testing/WinDurango.Testing.vcxproj @@ -79,7 +79,7 @@ - ..\..\build\projects\$(MSBuildProjectName)\$(Configuration)\ + ..\..\build\bin\$(Configuration)\ ..\..\build\projects\$(MSBuildProjectName)\$(Configuration)\ @@ -98,10 +98,10 @@ _DEBUG;%(PreprocessorDefinitions) - D:\WinDurango\projects\WinDurango.Common\include\WinDurango.Common;%(AdditionalIncludeDirectories) + $(ProjectDir)..\WinDurango.Implementation.WinRT\include;$(ProjectDir)..\WinDurango.Common\include;%(AdditionalIncludeDirectories) - ..\..\build\projects\WinDurango.Common\Debug\WinDurango.Common.lib;%(AdditionalDependencies) + ..\..\build\bin\Debug\WinDurango.Implementation.WinRT.lib;%(AdditionalDependencies) Console