Split asset reload platform policy
This commit is contained in:
@@ -5,29 +5,75 @@
|
||||
|
||||
namespace pp::platform {
|
||||
|
||||
AssetFileLoadDecision plan_asset_file_load_with_probe(
|
||||
PlatformFamily family,
|
||||
bool already_loaded,
|
||||
std::int64_t previous_last_write_time,
|
||||
AssetFileTimestampProbe probe) noexcept
|
||||
{
|
||||
if (platform_uses_asset_file_mtime_reload(family))
|
||||
{
|
||||
if (!probe.file_exists)
|
||||
return { false, false, previous_last_write_time };
|
||||
|
||||
if (probe.last_write_time <= previous_last_write_time)
|
||||
return { false, false, previous_last_write_time };
|
||||
|
||||
return { true, true, probe.last_write_time };
|
||||
}
|
||||
|
||||
if (already_loaded)
|
||||
return { false, true, previous_last_write_time };
|
||||
|
||||
return { true, true, previous_last_write_time };
|
||||
}
|
||||
|
||||
AssetFileLoadDecision plan_asset_file_load_for_platform(
|
||||
PlatformFamily family,
|
||||
std::string_view absolute_path,
|
||||
bool already_loaded,
|
||||
std::int64_t previous_last_write_time)
|
||||
{
|
||||
if (!platform_uses_asset_file_mtime_reload(family))
|
||||
{
|
||||
return plan_asset_file_load_with_probe(
|
||||
family,
|
||||
already_loaded,
|
||||
previous_last_write_time,
|
||||
{});
|
||||
}
|
||||
|
||||
struct stat file_info {};
|
||||
const std::string path(absolute_path);
|
||||
if (stat(path.c_str(), &file_info) != 0)
|
||||
{
|
||||
return plan_asset_file_load_with_probe(
|
||||
family,
|
||||
already_loaded,
|
||||
previous_last_write_time,
|
||||
{});
|
||||
}
|
||||
|
||||
return plan_asset_file_load_with_probe(
|
||||
family,
|
||||
already_loaded,
|
||||
previous_last_write_time,
|
||||
{
|
||||
true,
|
||||
static_cast<std::int64_t>(file_info.st_mtime),
|
||||
});
|
||||
}
|
||||
|
||||
AssetFileLoadDecision plan_asset_file_load(
|
||||
std::string_view absolute_path,
|
||||
bool already_loaded,
|
||||
std::int64_t previous_last_write_time)
|
||||
{
|
||||
#if defined(_WIN32) || defined(__OSX__)
|
||||
struct stat file_info {};
|
||||
const std::string path(absolute_path);
|
||||
if (stat(path.c_str(), &file_info) != 0)
|
||||
return { false, false, previous_last_write_time };
|
||||
|
||||
const auto current_last_write_time = static_cast<std::int64_t>(file_info.st_mtime);
|
||||
if (current_last_write_time <= previous_last_write_time)
|
||||
return { false, false, previous_last_write_time };
|
||||
|
||||
return { true, true, current_last_write_time };
|
||||
#else
|
||||
if (already_loaded)
|
||||
return { false, true, previous_last_write_time };
|
||||
|
||||
(void)absolute_path;
|
||||
return { true, true, previous_last_write_time };
|
||||
#endif
|
||||
return plan_asset_file_load_for_platform(
|
||||
current_platform_family(),
|
||||
absolute_path,
|
||||
already_loaded,
|
||||
previous_last_write_time);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "platform_api/platform_policy.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <string_view>
|
||||
|
||||
@@ -11,6 +13,23 @@ struct AssetFileLoadDecision {
|
||||
std::int64_t last_write_time = 0;
|
||||
};
|
||||
|
||||
struct AssetFileTimestampProbe {
|
||||
bool file_exists = false;
|
||||
std::int64_t last_write_time = 0;
|
||||
};
|
||||
|
||||
[[nodiscard]] AssetFileLoadDecision plan_asset_file_load_with_probe(
|
||||
PlatformFamily family,
|
||||
bool already_loaded,
|
||||
std::int64_t previous_last_write_time,
|
||||
AssetFileTimestampProbe probe) noexcept;
|
||||
|
||||
[[nodiscard]] AssetFileLoadDecision plan_asset_file_load_for_platform(
|
||||
PlatformFamily family,
|
||||
std::string_view absolute_path,
|
||||
bool already_loaded,
|
||||
std::int64_t previous_last_write_time);
|
||||
|
||||
[[nodiscard]] AssetFileLoadDecision plan_asset_file_load(
|
||||
std::string_view absolute_path,
|
||||
bool already_loaded,
|
||||
|
||||
@@ -43,6 +43,11 @@ bool platform_enables_live_asset_reloading(PlatformFamily family) noexcept
|
||||
return family == PlatformFamily::windows || family == PlatformFamily::macos;
|
||||
}
|
||||
|
||||
bool platform_uses_asset_file_mtime_reload(PlatformFamily family) noexcept
|
||||
{
|
||||
return family == PlatformFamily::windows || family == PlatformFamily::macos;
|
||||
}
|
||||
|
||||
bool platform_saves_native_ui_state(PlatformFamily family) noexcept
|
||||
{
|
||||
return family == PlatformFamily::windows || family == PlatformFamily::macos;
|
||||
|
||||
@@ -23,6 +23,7 @@ enum class PlatformFamily {
|
||||
[[nodiscard]] bool platform_publishes_exported_images(PlatformFamily family) noexcept;
|
||||
[[nodiscard]] bool platform_flushes_persistent_storage(PlatformFamily family) noexcept;
|
||||
[[nodiscard]] bool platform_enables_live_asset_reloading(PlatformFamily family) noexcept;
|
||||
[[nodiscard]] bool platform_uses_asset_file_mtime_reload(PlatformFamily family) noexcept;
|
||||
[[nodiscard]] bool platform_saves_native_ui_state(PlatformFamily family) noexcept;
|
||||
[[nodiscard]] bool platform_supports_working_directory_picker(PlatformFamily family) noexcept;
|
||||
[[nodiscard]] bool platform_uses_prepared_file_writes(PlatformFamily family) noexcept;
|
||||
|
||||
Reference in New Issue
Block a user