Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ add_executable(videoframevr
src/foveation.cpp
src/frame_reuse.cpp
src/frame_grabber.cpp
src/funscript.cpp
src/gl_interop.cpp
src/gpu_context.cpp
src/hints.cpp
src/intiface.cpp
src/key_mask.cpp
src/key_picker.cpp
src/key_share.cpp
Expand Down Expand Up @@ -87,6 +89,7 @@ add_executable(videoframevr
src/menu_player.cpp
src/menu_presets.cpp
src/menu_rating.cpp
src/menu_scripts.cpp
src/menu_search.cpp
src/menu_settings.cpp
src/menu_setup.cpp
Expand Down Expand Up @@ -132,6 +135,7 @@ add_executable(videoframevr
src/run_log.cpp
src/scene_renderer.cpp
src/screen_control.cpp
src/script_player.cpp
src/see_through.cpp
src/seek_preview.cpp
src/settings.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ std::string anonymisedSettings(const std::string& text) {
if (eq != std::string::npos && eq + 1 < line.size()) {
const std::string key = line.substr(0, eq), value = line.substr(eq + 1);
bool secret = value.find('/') != std::string::npos;
for (const char* word : {"key", "url", "path", "folder", "password", "user", "host", "share"})
for (const char* word : {"key", "url", "path", "folder", "password", "user", "host", "share", "address"})
secret = secret || key.find(word) != std::string::npos;
if (secret) line = key + "=<removed>";
}
Expand Down
103 changes: 103 additions & 0 deletions src/funscript.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "funscript.h"

#include "remote.h"
#include "stash_stream.h"

#include <nlohmann/json.hpp>

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iterator>

namespace {

// Scripts are a few hundred kilobytes at most; anything larger isn't one.
constexpr uint64_t kMaxBytes = 16 * 1024 * 1024;

} // namespace

int Funscript::actionAt(double ms) const {
const auto next = std::upper_bound(actions.begin(), actions.end(), ms,
[](double t, const Action& a) { return t < a.at; });
return static_cast<int>(next - actions.begin()) - 1;
}

double Funscript::positionAt(double ms) const {
if (actions.empty()) return 0;
const int i = actionAt(ms);
if (i < 0) return actions.front().pos;
if (i + 1 >= static_cast<int>(actions.size())) return actions.back().pos;
const Action& a = actions[i];
const Action& b = actions[i + 1];
return a.pos + (b.pos - a.pos) * (ms - a.at) / (b.at - a.at);
}

bool parseFunscript(const std::string& text, Funscript& script) {
script.actions.clear();
const nlohmann::json j = nlohmann::json::parse(text, nullptr, false);
if (!j.is_object()) return false;
const auto actions = j.find("actions");
if (actions == j.end() || !actions->is_array()) return false;
const auto flag = j.find("inverted");
const bool inverted = flag != j.end() && flag->is_boolean() && flag->get<bool>();
script.actions.reserve(actions->size());
for (const nlohmann::json& a : *actions) {
if (!a.is_object()) continue;
const auto at = a.find("at"), pos = a.find("pos");
if (at == a.end() || pos == a.end() || !at->is_number() || !pos->is_number()) continue;
const double ms = at->get<double>(), place = pos->get<double>();
// A day is longer than any video; the limit keeps the times in an int.
if (!std::isfinite(ms) || !std::isfinite(place) || ms < 0 || ms > 86400000.0) continue;
const int p = static_cast<int>(std::lround(std::clamp(place, 0.0, 100.0)));
script.actions.push_back({static_cast<int>(std::lround(ms)), inverted ? 100 - p : p});
}
// In time order, the last of those written for one time.
std::stable_sort(script.actions.begin(), script.actions.end(),
[](const Funscript::Action& a, const Funscript::Action& b) { return a.at < b.at; });
std::vector<Funscript::Action> unique;
unique.reserve(script.actions.size());
for (const Funscript::Action& a : script.actions) {
if (!unique.empty() && unique.back().at == a.at) unique.back() = a;
else unique.push_back(a);
}
script.actions = std::move(unique);
if (script.actions.size() < 2) script.actions.clear();
return !script.actions.empty();
}

std::string funscriptBeside(const std::string& location) {
if (location.empty() || isStashScene(location)) return {};
// Links (http and the like) have no file beside them.
if (location.find("://") != std::string::npos && !remote::isRemote(location)) return {};
const size_t slash = location.find_last_of('/');
const size_t dot = location.find_last_of('.');
const size_t name = slash == std::string::npos ? 0 : slash + 1;
const bool extension = dot != std::string::npos && dot > name;
return (extension ? location.substr(0, dot) : location) + ".funscript";
}

std::string readFunscript(const std::string& location) {
if (isStashScene(location)) return stashFunscript(location);
const std::string file = funscriptBeside(location);
if (file.empty()) return {};
std::string bytes;
if (!remote::isRemote(file)) {
std::ifstream in(file, std::ios::binary | std::ios::ate);
if (!in || in.tellg() <= 0 || static_cast<uint64_t>(in.tellg()) > kMaxBytes) return {};
bytes.resize(static_cast<size_t>(in.tellg()));
in.seekg(0);
if (!in.read(bytes.data(), static_cast<std::streamsize>(bytes.size()))) return {};
return bytes;
}
remote::Status status;
const auto remoteFile = remote::File::open(file, status);
if (!remoteFile || remoteFile->size() == 0 || remoteFile->size() > kMaxBytes) return {};
bytes.resize(remoteFile->size());
for (uint64_t at = 0; at < bytes.size();) {
const int64_t n = remoteFile->read(at, bytes.data() + at, bytes.size() - at);
if (n <= 0) return {};
at += static_cast<uint64_t>(n);
}
return bytes;
}
35 changes: 35 additions & 0 deletions src/funscript.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <string>
#include <vector>

// A funscript: a device's stroke position over a video's time, as the
// .funscript file beside the video has it (JSON: "actions" of {"at":
// milliseconds, "pos": 0..100}). Only the stroke's script is read, not those
// of other axes (video.roll.funscript and the like).
struct Funscript {
struct Action {
int at = 0; // milliseconds from the video's start
int pos = 0; // 0 (the stroke's bottom) .. 100 (its top)
};
std::vector<Action> actions; // in time order, one per time, at least two

// The action playing at `ms`: the last one at or before it; -1 before the first.
int actionAt(double ms) const;
// Where the stroke is at `ms`, 0..100: on the line between its actions,
// at the first one's place before it and the last one's after it.
double positionAt(double ms) const;
};

// A script's text as a Funscript, "inverted" applied. Its "range" is left
// out: the range set in Settings limits the stroke instead. False if the
// text isn't a script with at least two actions.
bool parseFunscript(const std::string& text, Funscript& script);

// The text of the script of the video at `location`: the file beside a local
// or remote (remote.h) video with its name and .funscript, or what Stash
// serves for a scene played from its stream. Empty if there is none. Blocks:
// worker threads only.
std::string readFunscript(const std::string& location);
// Where that file is, for a local or remote video; empty for anything else.
std::string funscriptBeside(const std::string& location);
Loading