Compare commits

..

1 Commits

Author SHA1 Message Date
124d51bced fix: potential download fixes 2025-08-05 22:05:29 +10:00
57 changed files with 889 additions and 2533 deletions

View File

@ -51,7 +51,7 @@ jobs:
if: matrix.platform == 'ubuntu-22.04' || matrix.platform == 'ubuntu-22.04-arm' # This must match the platform value defined above.
run: |
sudo apt-get update
sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf xdg-utils
sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf
# webkitgtk 4.0 is for Tauri v1 - webkitgtk 4.1 is for Tauri v2.
@ -69,9 +69,9 @@ jobs:
security set-keychain-settings -t 3600 -u build.keychain
curl https://droposs.org/drop.crt --output drop.pem
sudo security authorizationdb write com.apple.trust-settings.user allow
security add-trusted-cert -r trustRoot -k build.keychain -p codeSign -u -1 drop.pem
sudo security authorizationdb remove com.apple.trust-settings.user
sudo security authorizationdb write com.apple.trust-settings.admin allow
sudo security add-trusted-cert -d -r trustRoot -k build.keychain -p codeSign -u -1 drop.pem
sudo security authorizationdb remove com.apple.trust-settings.admin
security import certificate.p12 -k build.keychain -P "$APPLE_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$KEYCHAIN_PASSWORD" build.keychain

View File

@ -21,13 +21,6 @@ async function spawn(exec, opts) {
});
}
const expectedLibs = ["drop-base/package.json"];
for (const lib of expectedLibs) {
const path = `./libs/${lib}`;
if (!fs.existsSync(path)) throw `Missing "${expectedLibs}". Run "git submodule update --init --recursive"`;
}
const views = fs.readdirSync(".").filter((view) => {
const expectedPath = `./${view}/package.json`;
return fs.existsSync(expectedPath);

View File

@ -1,5 +1,5 @@
<template>
<NuxtLoadingIndicator color="#2563eb" />
<LoadingIndicator />
<NuxtLayout class="select-none w-screen h-screen">
<NuxtPage />
<ModalStack />
@ -44,10 +44,6 @@ router.beforeEach(async () => {
setupHooks();
initialNavigation(state);
// Setup playtime event listeners
const { setupEventListeners } = usePlaytime();
setupEventListeners();
useHead({
title: "Drop",
});

View File

@ -37,7 +37,7 @@
</NuxtLink>
<div class="h-0.5 rounded-full w-full bg-zinc-800" />
<div class="flex flex-col mb-1">
<MenuItem v-if="state.user.admin" v-slot="{ active }">
<MenuItem v-slot="{ active }">
<a
:href="adminUrl"
target="_blank"

View File

@ -1,5 +1,5 @@
<template>
<div class="flex flex-col h-full">
<div>
<div class="mb-3 inline-flex gap-x-2">
<div
class="relative transition-transform duration-300 hover:scale-105 active:scale-95"
@ -20,7 +20,7 @@
/>
</div>
<button
@click="() => calculateGames(true, true)"
@click="() => calculateGames(true)"
class="p-1 flex items-center justify-center transition-transform duration-300 size-10 hover:scale-110 active:scale-90 rounded-lg bg-zinc-800/50 text-zinc-100"
>
<ArrowPathIcon class="size-4" />
@ -28,125 +28,64 @@
</div>
<TransitionGroup name="list" tag="ul" class="flex flex-col gap-y-1.5">
<Disclosure
as="div"
v-for="(nav, navIndex) in filteredNavigation"
:key="nav.id"
class="first:pt-0 last:pb-0"
v-slot="{ open }"
:default-open="nav.deft"
>
<dt>
<DisclosureButton
class="flex w-full items-center justify-between text-left text-gray-900 dark:text-white"
>
<span class="text-sm font-semibold font-display">{{
nav.name
}}</span>
<span class="ml-6 flex h-7 items-center">
<PlusSmallIcon v-if="!open" class="size-6" aria-hidden="true" />
<MinusSmallIcon v-else class="size-6" aria-hidden="true" />
</span>
</DisclosureButton>
</dt>
<DisclosurePanel as="dd" class="mt-2 flex flex-col gap-y-1.5">
<NuxtLink
v-for="item in nav.items"
v-for="nav in filteredNavigation"
:key="nav.id"
:class="[
'transition-all duration-300 rounded-lg flex items-center px-1 py-1.5 hover:scale-105 active:scale-95 hover:shadow-lg hover:shadow-zinc-950/50',
currentNavigation == item.id
'transition-all duration-300 rounded-lg flex items-center py-2 px-3 hover:scale-105 active:scale-95 hover:shadow-lg hover:shadow-zinc-950/50',
nav.index === currentNavigation
? 'bg-zinc-800 text-zinc-100 shadow-md shadow-zinc-950/20'
: item.isInstalled.value
: nav.isInstalled.value
? 'text-zinc-300 hover:bg-zinc-800/90 hover:text-zinc-200'
: 'text-zinc-500 hover:bg-zinc-800/70 hover:text-zinc-300',
]"
:href="item.route"
:href="nav.route"
>
<div class="flex items-center w-full gap-x-2">
<div class="flex items-center w-full gap-x-3">
<div
class="flex-none transition-transform duration-300 hover:-rotate-2"
>
<img
class="size-6 object-cover bg-zinc-900 rounded transition-all duration-300 shadow-sm"
:src="icons[item.id]"
class="size-8 object-cover bg-zinc-900 rounded-lg transition-all duration-300 shadow-sm"
:src="icons[nav.id]"
alt=""
/>
</div>
<div class="inline-flex items-center gap-x-2">
<div class="flex flex-col flex-1">
<p
class="text-sm whitespace-nowrap font-display font-semibold"
class="truncate text-xs font-display leading-5 flex-1 font-semibold"
>
{{ item.label }}
{{ nav.label }}
</p>
<p
class="truncate text-[10px] font-bold uppercase font-display"
:class="[
gameStatusTextStyle[games[item.id].status.value.type],
]"
class="text-xs font-medium"
:class="[gameStatusTextStyle[games[nav.id].status.value.type]]"
>
{{ gameStatusText[games[item.id].status.value.type] }}
{{ gameStatusText[games[nav.id].status.value.type] }}
</p>
</div>
</div>
</NuxtLink>
</DisclosurePanel>
</Disclosure>
</TransitionGroup>
<div
v-if="loading"
class="h-full grow flex p-8 justify-center text-zinc-100"
>
<div role="status">
<svg
aria-hidden="true"
class="w-6 h-6 text-transparent animate-spin fill-zinc-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span class="sr-only">Loading...</span>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { Disclosure, DisclosureButton, DisclosurePanel } from "@headlessui/vue";
import {
ArrowPathIcon,
MagnifyingGlassIcon,
MinusSmallIcon,
PlusSmallIcon,
} from "@heroicons/vue/20/solid";
import { ArrowPathIcon, MagnifyingGlassIcon } from "@heroicons/vue/20/solid";
import { invoke } from "@tauri-apps/api/core";
import {
GameStatusEnum,
type Collection as Collection,
type Game,
type GameStatus,
} from "~/types";
import { GameStatusEnum, type Game, type GameStatus } from "~/types";
import { TransitionGroup } from "vue";
import { listen } from "@tauri-apps/api/event";
// Style information
const gameStatusTextStyle: { [key in GameStatusEnum]: string } = {
[GameStatusEnum.Installed]: "text-green-500",
[GameStatusEnum.Downloading]: "text-zinc-400",
[GameStatusEnum.Downloading]: "text-blue-500",
[GameStatusEnum.Validating]: "text-blue-300",
[GameStatusEnum.Running]: "text-green-500",
[GameStatusEnum.Remote]: "text-zinc-700",
[GameStatusEnum.Queued]: "text-zinc-400",
[GameStatusEnum.Updating]: "text-zinc-400",
[GameStatusEnum.Remote]: "text-zinc-500",
[GameStatusEnum.Queued]: "text-blue-500",
[GameStatusEnum.Updating]: "text-blue-500",
[GameStatusEnum.Uninstalling]: "text-zinc-100",
[GameStatusEnum.SetupRequired]: "text-yellow-500",
[GameStatusEnum.PartiallyInstalled]: "text-gray-400",
@ -168,74 +107,39 @@ const router = useRouter();
const searchQuery = ref("");
const loading = ref(false);
const games: {
[key: string]: { game: Game; status: Ref<GameStatus, GameStatus> };
} = {};
const icons: { [key: string]: string } = {};
const collections: Ref<Collection[]> = ref([]);
const rawGames: Ref<Game[], Game[]> = ref([]);
async function calculateGames(clearAll = false, forceRefresh = false) {
if (clearAll) {
collections.value = [];
loading.value = true;
}
async function calculateGames(clearAll = false) {
if (clearAll) rawGames.value = [];
// If we update immediately, the navigation gets re-rendered before we
// add all the necessary state, and it freaks tf out
const newGames = await invoke<Game[]>("fetch_library", {
hardRefresh: forceRefresh,
});
const otherCollections = await invoke<Collection[]>("fetch_collections", {
hardRefresh: forceRefresh,
});
const allGames = [
...newGames,
...otherCollections
.map((e) => e.entries)
.flat()
.map((e) => e.game),
].filter((v, i, a) => a.indexOf(v) === i);
for (const game of allGames) {
const newGames = await invoke<typeof rawGames.value>("fetch_library");
for (const game of newGames) {
if (games[game.id]) continue;
games[game.id] = await useGame(game.id);
}
for (const game of allGames) {
for (const game of newGames) {
if (icons[game.id]) continue;
icons[game.id] = await useObject(game.mIconObjectId);
}
const libraryCollection = {
id: "library",
name: "Library",
isDefault: true,
entries: newGames.map((e) => ({ gameId: e.id, game: e })),
} satisfies Collection;
loading.value = false;
collections.value = [libraryCollection, ...otherCollections];
rawGames.value = newGames;
}
// Wait up to 300 ms for the library to load, otherwise
// show the loading state while we while
await new Promise<void>((r) => {
let hasResolved = false;
const resolveFunc = () => {
if (!hasResolved) r();
hasResolved = true;
};
calculateGames(true).then(resolveFunc);
setTimeout(resolveFunc, 300);
});
await calculateGames();
const navigation = computed(() =>
collections.value.map((collection) => {
const items = collection.entries.map(({ game }) => {
rawGames.value.map((game) => {
const status = games[game.id].status;
const isInstalled = computed(
() => status.value.type != GameStatusEnum.Remote
() =>
status.value.type == GameStatusEnum.Installed ||
status.value.type == GameStatusEnum.SetupRequired
);
const item = {
@ -246,39 +150,28 @@ const navigation = computed(() =>
id: game.id,
};
return item;
});
return {
id: collection.id,
name: collection.name,
deft: collection.isDefault,
items,
};
})
);
const route = useRoute();
const currentNavigation = computed(() => {
return route.path.slice("/library/".length);
});
const { currentNavigation, recalculateNavigation } = useCurrentNavigationIndex(
navigation.value
);
const filteredNavigation = computed(() => {
if (!searchQuery.value)
return navigation.value.map((e, i) => ({ ...e, index: i }));
const query = searchQuery.value.toLowerCase();
return navigation.value
.map((c) => ({
...c,
items: c.items.filter((nav) => nav.label.toLowerCase().includes(query)),
}))
.filter((e) => e.items.length > 0);
.filter((nav) => nav.label.toLowerCase().includes(query))
.map((e, i) => ({ ...e, index: i }));
});
listen("update_library", async (event) => {
console.log("Updating library");
let oldNavigation = currentNavigation.value;
let oldNavigation = navigation.value[currentNavigation.value];
await calculateGames();
if (oldNavigation !== currentNavigation.value) {
recalculateNavigation();
if (oldNavigation !== navigation.value[currentNavigation.value]) {
console.log("Triggered");
router.push("/library");
}
});

View File

@ -0,0 +1,7 @@
<template></template>
<script setup lang="ts">
const loading = useLoadingIndicator();
watch(loading.isLoading, console.log);
</script>

View File

@ -1,53 +0,0 @@
<template>
<div v-if="stats" class="flex flex-col gap-1">
<!-- Main playtime display -->
<div class="flex items-center gap-2">
<ClockIcon class="w-5 h-5 text-zinc-400" />
<span class="text-base text-zinc-300 font-medium">
{{ formatPlaytime(stats.totalPlaytimeSeconds) }} played
</span>
<span v-if="isActive && showActiveIndicator" class="text-sm text-green-400 font-medium">
Playing
</span>
</div>
<!-- Additional details when expanded -->
<div v-if="showDetails" class="text-xs text-zinc-400 space-y-1 ml-7">
<div>{{ stats.sessionCount }} session{{ stats.sessionCount !== 1 ? 's' : '' }}</div>
<div v-if="stats.sessionCount > 0">
Avg: {{ formatPlaytime(stats.averageSessionLength) }} per session
</div>
<div v-if="stats.currentSessionDuration">
Current session: {{ formatPlaytime(stats.currentSessionDuration) }}
</div>
</div>
</div>
<!-- No playtime data -->
<div v-else-if="showWhenEmpty" class="flex items-center gap-2 text-zinc-500">
<ClockIcon class="w-5 h-5" />
<span class="text-base">Never played</span>
</div>
</template>
<script setup lang="ts">
import { ClockIcon } from "@heroicons/vue/20/solid";
import type { GamePlaytimeStats } from "~/types";
interface Props {
stats: GamePlaytimeStats | null;
isActive?: boolean;
showDetails?: boolean;
showWhenEmpty?: boolean;
showActiveIndicator?: boolean;
}
const props = withDefaults(defineProps<Props>(), {
isActive: false,
showDetails: false,
showWhenEmpty: true,
showActiveIndicator: true,
});
const { formatPlaytime } = usePlaytime();
</script>

View File

@ -1,76 +0,0 @@
<template>
<div class="bg-zinc-800/50 rounded-lg p-4 space-y-4">
<div class="flex items-center gap-2">
<ChartBarIcon class="w-5 h-5 text-zinc-400" />
<h3 class="text-lg font-semibold text-zinc-100">Playtime Statistics</h3>
</div>
<div v-if="stats" class="grid grid-cols-1 md:grid-cols-2 gap-4">
<!-- Total Playtime -->
<div class="bg-zinc-700/50 rounded-lg p-3">
<div class="flex items-center gap-2 mb-2">
<ClockIcon class="w-4 h-4 text-blue-400" />
<span class="text-sm font-medium text-zinc-300">Total Playtime</span>
</div>
<div class="text-2xl font-bold text-zinc-100">
{{ formatDetailedPlaytime(stats.totalPlaytimeSeconds) }}
</div>
<div v-if="stats.currentSessionDuration" class="text-xs text-green-400 mt-1">
+{{ formatPlaytime(stats.currentSessionDuration) }} this session
</div>
</div>
<!-- Sessions -->
<div class="bg-zinc-700/50 rounded-lg p-3">
<div class="flex items-center gap-2 mb-2">
<PlayIcon class="w-4 h-4 text-green-400" />
<span class="text-sm font-medium text-zinc-300">Sessions</span>
</div>
<div class="text-2xl font-bold text-zinc-100">
{{ stats.sessionCount }}
</div>
<div class="text-xs text-zinc-400 mt-1">
Avg: {{ formatPlaytime(stats.averageSessionLength) }}
</div>
</div>
</div>
<!-- No stats available -->
<div v-else class="text-center py-8">
<ClockIcon class="w-12 h-12 text-zinc-600 mx-auto mb-3" />
<p class="text-zinc-400">No playtime data available</p>
<p class="text-sm text-zinc-500 mt-1">Statistics will appear after you start playing</p>
</div>
<!-- Current session indicator -->
<div v-if="isActive && stats" class="border-t border-zinc-700 pt-3">
<div class="flex items-center gap-2 text-green-400">
<div class="w-2 h-2 bg-green-400 rounded-full animate-pulse"></div>
<span class="text-sm font-medium">Currently playing</span>
<span v-if="stats.currentSessionDuration" class="text-xs text-zinc-400">
{{ formatPlaytime(stats.currentSessionDuration) }}
</span>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import {
ChartBarIcon,
ClockIcon,
PlayIcon
} from "@heroicons/vue/20/solid";
import type { GamePlaytimeStats } from "~/types";
interface Props {
stats: GamePlaytimeStats | null;
isActive?: boolean;
}
const props = withDefaults(defineProps<Props>(), {
isActive: false,
});
const { formatPlaytime, formatDetailedPlaytime } = usePlaytime();
</script>

View File

@ -32,5 +32,3 @@ listen("update_stats", (event) => {
const stats = useStatsState();
stats.value = event.payload as StatsState;
});
export const useDownloadHistory = () => useState<Array<number>>('history', () => []);

View File

@ -43,7 +43,6 @@ export const useGame = async (gameId: string) => {
gameStatusRegistry[gameId] = ref(parseStatus(data.status));
listen(`update_game/${gameId}`, (event) => {
console.log(event);
const payload: {
status: SerializedGameStatus;
version?: GameVersion;

View File

@ -1,193 +0,0 @@
import { invoke } from "@tauri-apps/api/core";
import { listen } from "@tauri-apps/api/event";
import type {
GamePlaytimeStats,
PlaytimeUpdateEvent,
PlaytimeSessionStartEvent,
PlaytimeSessionEndEvent
} from "~/types";
export const usePlaytime = () => {
const playtimeStats = useState<Record<string, GamePlaytimeStats>>('playtime-stats', () => ({}));
const activeSessions = useState<Set<string>>('active-sessions', () => new Set());
// Fetch playtime stats for a specific game
const fetchGamePlaytime = async (gameId: string): Promise<GamePlaytimeStats | null> => {
try {
const stats = await invoke<GamePlaytimeStats | null>("fetch_game_playtime", { gameId });
if (stats) {
playtimeStats.value[gameId] = stats;
}
return stats;
} catch (error) {
console.error(`Failed to fetch playtime for game ${gameId}:`, error);
return null;
}
};
// Fetch all playtime stats
const fetchAllPlaytimeStats = async (): Promise<Record<string, GamePlaytimeStats>> => {
try {
const stats = await invoke<Record<string, GamePlaytimeStats>>("fetch_all_playtime_stats");
playtimeStats.value = stats;
return stats;
} catch (error) {
console.error("Failed to fetch all playtime stats:", error);
return {};
}
};
// Check if a session is active
const isSessionActive = async (gameId: string): Promise<boolean> => {
try {
return await invoke<boolean>("is_playtime_session_active", { gameId });
} catch (error) {
console.error(`Failed to check session status for game ${gameId}:`, error);
return false;
}
};
// Get all active sessions
const getActiveSessions = async (): Promise<string[]> => {
try {
const sessions = await invoke<string[]>("get_active_playtime_sessions");
activeSessions.value = new Set(sessions);
return sessions;
} catch (error) {
console.error("Failed to get active sessions:", error);
return [];
}
};
// Format playtime duration
const formatPlaytime = (seconds: number): string => {
if (seconds < 60) {
return `${seconds}s`;
} else if (seconds < 3600) {
const minutes = Math.floor(seconds / 60);
return `${minutes}m`;
} else {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
if (minutes === 0) {
return `${hours}h`;
}
return `${hours}h ${minutes}m`;
}
};
// Format detailed playtime
const formatDetailedPlaytime = (seconds: number): string => {
if (seconds < 60) {
return `${seconds} seconds`;
} else if (seconds < 3600) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
if (remainingSeconds === 0) {
return `${minutes} minutes`;
}
return `${minutes} minutes, ${remainingSeconds} seconds`;
} else {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
if (minutes === 0) {
return `${hours} hours`;
}
return `${hours} hours, ${minutes} minutes`;
}
};
// Format relative time (e.g., "2 hours ago")
const formatRelativeTime = (timestamp: string): string => {
const date = new Date(timestamp);
const now = new Date();
const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);
if (diffInSeconds < 60) {
return "Just now";
} else if (diffInSeconds < 3600) {
const minutes = Math.floor(diffInSeconds / 60);
return `${minutes} minute${minutes !== 1 ? 's' : ''} ago`;
} else if (diffInSeconds < 86400) {
const hours = Math.floor(diffInSeconds / 3600);
return `${hours} hour${hours !== 1 ? 's' : ''} ago`;
} else if (diffInSeconds < 604800) {
const days = Math.floor(diffInSeconds / 86400);
return `${days} day${days !== 1 ? 's' : ''} ago`;
} else {
return date.toLocaleDateString();
}
};
// Get playtime stats for a game (from cache or fetch)
const getGamePlaytime = async (gameId: string): Promise<GamePlaytimeStats | null> => {
if (playtimeStats.value[gameId]) {
return playtimeStats.value[gameId];
}
return await fetchGamePlaytime(gameId);
};
// Setup event listeners
const setupEventListeners = () => {
// Listen for general playtime updates
listen<PlaytimeUpdateEvent>("playtime_update", (event) => {
const { gameId, stats, isActive } = event.payload;
playtimeStats.value[gameId] = stats;
if (isActive) {
activeSessions.value.add(gameId);
} else {
activeSessions.value.delete(gameId);
}
});
// Listen for session start events
listen<PlaytimeSessionStartEvent>("playtime_session_start", (event) => {
const { gameId } = event.payload;
activeSessions.value.add(gameId);
});
// Listen for session end events
listen<PlaytimeSessionEndEvent>("playtime_session_end", (event) => {
const { gameId } = event.payload;
activeSessions.value.delete(gameId);
});
};
// Setup game-specific event listeners
const setupGameEventListeners = (gameId: string) => {
listen<PlaytimeUpdateEvent>(`playtime_update/${gameId}`, (event) => {
const { stats, isActive } = event.payload;
playtimeStats.value[gameId] = stats;
if (isActive) {
activeSessions.value.add(gameId);
} else {
activeSessions.value.delete(gameId);
}
});
listen<PlaytimeSessionStartEvent>(`playtime_session_start/${gameId}`, () => {
activeSessions.value.add(gameId);
});
listen<PlaytimeSessionEndEvent>(`playtime_session_end/${gameId}`, () => {
activeSessions.value.delete(gameId);
});
};
return {
playtimeStats: readonly(playtimeStats),
activeSessions: readonly(activeSessions),
fetchGamePlaytime,
fetchAllPlaytimeStats,
isSessionActive,
getActiveSessions,
formatPlaytime,
formatDetailedPlaytime,
formatRelativeTime,
getGamePlaytime,
setupEventListeners,
setupGameEventListeners,
};
};

View File

@ -1,7 +1,7 @@
{
"name": "view",
"private": true,
"version": "0.3.3",
"version": "0.3.2-dl",
"type": "module",
"scripts": {
"build": "nuxt generate",

View File

@ -1,25 +0,0 @@
<template>
<div class="grow w-full h-full flex items-center justify-center">
<div class="flex flex-col items-center">
<WrenchScrewdriverIcon
class="h-12 w-12 text-blue-600"
aria-hidden="true"
/>
<div class="mt-3 text-center sm:mt-5">
<h1 class="text-3xl font-semibold font-display leading-6 text-zinc-100">
Under construction
</h1>
<div class="mt-4">
<p class="text-sm text-zinc-400 max-w-lg">
This page hasn't been implemented yet.
</p>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import {
WrenchScrewdriverIcon,
} from "@heroicons/vue/20/solid";
</script>

View File

@ -18,21 +18,11 @@
<div class="relative z-10">
<div class="px-8 pb-4">
<h1
class="text-5xl text-zinc-100 font-bold font-display drop-shadow-lg mb-4"
class="text-5xl text-zinc-100 font-bold font-display drop-shadow-lg mb-8"
>
{{ game.mName }}
</h1>
<!-- Playtime Display -->
<div class="mb-8">
<PlaytimeDisplay
:stats="gamePlaytime"
:is-active="isPlaytimeActive"
:show-details="false"
:show-active-indicator="false"
/>
</div>
<div class="flex flex-row gap-x-4 items-stretch mb-8">
<!-- Do not add scale animations to this: https://stackoverflow.com/a/35683068 -->
<GameStatusButton
@ -70,12 +60,6 @@
</div>
<div class="space-y-6">
<!-- Playtime Statistics -->
<PlaytimeStats
:stats="gamePlaytime"
:is-active="isPlaytimeActive"
/>
<div class="bg-zinc-800/50 rounded-xl p-6 backdrop-blur-sm">
<h2 class="text-xl font-display font-semibold text-zinc-100 mb-4">
Game Images
@ -259,10 +243,7 @@
</div>
</Listbox>
</div>
<div
v-else-if="versionOptions === null || versionOptions?.length == 0"
class="mt-1 rounded-md bg-red-600/10 p-4"
>
<div v-else class="mt-1 rounded-md bg-red-600/10 p-4">
<div class="flex">
<div class="flex-shrink-0">
<XCircleIcon class="h-5 w-5 text-red-600" aria-hidden="true" />
@ -275,27 +256,6 @@
</div>
</div>
</div>
<div v-else class="w-full flex items-center justify-center p-4">
<div role="status">
<svg
aria-hidden="true"
class="w-7 h-7 text-transparent animate-spin fill-white"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span class="sr-only">Loading...</span>
</div>
</div>
<div v-if="installDirs">
<Listbox as="div" v-model="installDir">
<ListboxLabel class="block text-sm/6 font-medium text-zinc-100"
@ -544,32 +504,19 @@ const currentImageIndex = ref(0);
const configureModalOpen = ref(false);
// Playtime tracking
const {
getGamePlaytime,
setupGameEventListeners,
activeSessions
} = usePlaytime();
const gamePlaytime = ref(await getGamePlaytime(id));
const isPlaytimeActive = computed(() => activeSessions.value.has(id));
// Setup playtime event listeners for this game
setupGameEventListeners(id);
async function installFlow() {
installFlowOpen.value = true;
versionOptions.value = undefined;
installDirs.value = undefined;
try {
versionOptions.value = await invoke("fetch_game_version_options", {
versionOptions.value = await invoke("fetch_game_verion_options", {
gameId: game.value.id,
});
console.log(versionOptions.value);
installDirs.value = await invoke("fetch_download_dir_stats");
} catch (error) {
installError.value = (error as string).toString();
versionOptions.value = undefined;
}
}

View File

@ -1,25 +0,0 @@
<template>
<div class="grow w-full h-full flex items-center justify-center">
<div class="flex flex-col items-center">
<WrenchScrewdriverIcon
class="h-12 w-12 text-blue-600"
aria-hidden="true"
/>
<div class="mt-3 text-center sm:mt-5">
<h1 class="text-3xl font-semibold font-display leading-6 text-zinc-100">
Under construction
</h1>
<div class="mt-4">
<p class="text-sm text-zinc-400 max-w-lg">
This page hasn't been implemented yet.
</p>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import {
WrenchScrewdriverIcon,
} from "@heroicons/vue/20/solid";
</script>

View File

@ -4,18 +4,18 @@
class="h-16 overflow-hidden relative rounded-xl flex flex-row border border-zinc-900"
>
<div
class="bg-zinc-900 z-10 w-32 flex flex-col gap-x-2 font-display items-left justify-center pl-2"
class="bg-zinc-900 z-10 w-32 flex flex-col gap-x-2 text-blue-400 font-display items-left justify-center pl-2"
>
<span class="font-bold text-zinc-100">{{ formatKilobytes(stats.speed) }}B/s</span>
<span v-if="stats.time > 0" class="text-xs text-zinc-400"
<span class="font-semibold">{{ formatKilobytes(stats.speed) }}/s</span>
<span v-if="stats.time > 0" class="text-sm"
>{{ formatTime(stats.time) }} left</span
>
</div>
<div class="absolute inset-0 h-full flex flex-row items-end justify-end space-x-[1px]">
<div class="absolute inset-0 h-full flex flex-row items-end justify-end">
<div
v-for="bar in speedHistory"
:style="{ height: `${(bar / speedMax) * 100}%` }"
class="w-[3px] bg-blue-600 rounded-t-full"
class="w-[8px] bg-blue-600/40"
/>
</div>
</div>
@ -62,9 +62,9 @@
class="mt-2 inline-flex items-center gap-x-1 text-zinc-400 text-sm font-display"
><span class="text-zinc-300">{{
formatKilobytes(element.current / 1000)
}}B</span>
}}</span>
/
<span class="">{{ formatKilobytes(element.max / 1000) }}B</span
<span class="">{{ formatKilobytes(element.max / 1000) }}</span
><ServerIcon class="size-5"
/></span>
</div>
@ -91,7 +91,7 @@
<script setup lang="ts">
import { ServerIcon, XMarkIcon } from "@heroicons/vue/20/solid";
import { invoke } from "@tauri-apps/api/core";
import { type DownloadableMetadata, type Game, type GameStatus } from "~/types";
import { GameStatusEnum, type DownloadableMetadata, type Game, type GameStatus } from "~/types";
// const actionNames = {
// [GameStatusEnum.Downloading]: "downloading",
@ -105,12 +105,12 @@ window.addEventListener("resize", (event) => {
const queue = useQueueState();
const stats = useStatsState();
const speedHistory = useDownloadHistory();
const speedHistoryMax = computed(() => windowWidth.value / 4);
const speedHistory = useState<Array<number>>(() => []);
const speedHistoryMax = computed(() => windowWidth.value / 8);
const speedMax = computed(
() => speedHistory.value.reduce((a, b) => (a > b ? a : b)) * 1.1
() => speedHistory.value.reduce((a, b) => (a > b ? a : b)) * 1.3
);
const previousGameId = useState<string | undefined>('previous_game');
const previousGameId = ref<string | undefined>();
const games: Ref<{
[key: string]: { game: Game; status: Ref<GameStatus>; cover: string };
@ -122,15 +122,14 @@ function resetHistoryGraph() {
}
function checkReset(v: QueueState) {
const currentGame = v.queue.at(0)?.meta.id;
// If we don't have a game
if (!currentGame) return;
// If we're finished
if (!currentGame && previousGameId.value) {
previousGameId.value = undefined;
resetHistoryGraph();
return;
}
// If we don't have a game
if (!currentGame) return;
// If we started a new download
if (currentGame && !previousGameId.value) {
previousGameId.value = currentGame;
@ -150,10 +149,9 @@ watch(queue, (v) => {
});
watch(stats, (v) => {
if(v.speed == 0) return;
const newLength = speedHistory.value.push(v.speed);
if (newLength > speedHistoryMax.value) {
speedHistory.value.splice(0, newLength - speedHistoryMax.value);
speedHistory.value.splice(0, 1);
}
checkReset(queue.value);
});
@ -185,7 +183,7 @@ async function cancelGame(meta: DownloadableMetadata) {
}
function formatKilobytes(bytes: number): string {
const units = ["K", "M", "G", "T", "P"];
const units = ["KB", "MB", "GB", "TB", "PB"];
let value = bytes;
let unitIndex = 0;
const scalar = 1000;

View File

@ -1,23 +1,7 @@
<template>
<div class="grow w-full h-full flex items-center justify-center">
<div class="flex flex-col items-center">
<WrenchScrewdriverIcon
class="h-12 w-12 text-blue-600"
aria-hidden="true"
/>
<div class="mt-3 text-center sm:mt-5">
<h1 class="text-3xl font-semibold font-display leading-6 text-zinc-100">
Under construction
</h1>
<div class="mt-4">
<p class="text-sm text-zinc-400 max-w-lg">
This page hasn't been implemented yet.
</p>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { WrenchScrewdriverIcon } from "@heroicons/vue/20/solid";
</script>

View File

@ -37,13 +37,6 @@ export type Game = {
mImageCarouselObjectIds: string[];
};
export type Collection = {
id: string;
name: string;
isDefault: boolean;
entries: Array<{ gameId: string; game: Game }>;
};
export type GameVersion = {
launchCommandTemplate: string;
};
@ -94,37 +87,3 @@ export type Settings = {
maxDownloadThreads: number;
forceOffline: boolean;
};
export type GamePlaytimeStats = {
gameId: string;
totalPlaytimeSeconds: number;
sessionCount: number;
firstPlayed: string;
lastPlayed: string;
averageSessionLength: number;
currentSessionDuration?: number;
};
export type PlaytimeSession = {
gameId: string;
startTime: string;
sessionId: string;
};
export type PlaytimeUpdateEvent = {
gameId: string;
stats: GamePlaytimeStats;
isActive: boolean;
};
export type PlaytimeSessionStartEvent = {
gameId: string;
startTime: string;
};
export type PlaytimeSessionEndEvent = {
gameId: string;
sessionDurationSeconds: number;
totalPlaytimeSeconds: number;
sessionCount: number;
};

69
src-tauri/Cargo.lock generated
View File

@ -1284,12 +1284,11 @@ dependencies = [
[[package]]
name = "drop-app"
version = "0.3.3"
version = "0.3.2-dl"
dependencies = [
"atomic-instant-full",
"bitcode",
"boxcar",
"bytes",
"cacache 13.1.0",
"chrono",
"deranged",
@ -1297,7 +1296,6 @@ dependencies = [
"droplet-rs",
"dynfmt",
"filetime",
"futures-core",
"futures-lite",
"gethostname",
"hex 0.4.3",
@ -1314,7 +1312,7 @@ dependencies = [
"rand 0.9.1",
"rayon",
"regex",
"reqwest 0.12.22",
"reqwest 0.12.16",
"reqwest-middleware 0.4.2",
"reqwest-middleware-cache",
"reqwest-websocket",
@ -1341,7 +1339,6 @@ dependencies = [
"tempfile",
"throttle_my_fn",
"tokio",
"tokio-util",
"umu-wrapper-lib",
"url",
"urlencoding",
@ -2384,7 +2381,6 @@ dependencies = [
"hyper 1.6.0",
"hyper-util",
"rustls",
"rustls-native-certs",
"rustls-pki-types",
"tokio",
"tokio-rustls",
@ -3114,20 +3110,20 @@ dependencies = [
"openssl-probe",
"openssl-sys",
"schannel",
"security-framework 2.11.1",
"security-framework",
"security-framework-sys",
"tempfile",
]
[[package]]
name = "native_model"
version = "0.6.4"
source = "git+https://github.com/Drop-OSS/native_model.git#a91b422cbd53116df1f20b2459fb3d8257458bfd"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7050d759e3da6673361dddda4f4a743492279dd2c6484a21fbee0a8278620df0"
dependencies = [
"anyhow",
"bincode",
"doc-comment",
"log",
"native_model_macro",
"rmp-serde",
"serde",
@ -3137,10 +3133,10 @@ dependencies = [
[[package]]
name = "native_model_macro"
version = "0.6.4"
source = "git+https://github.com/Drop-OSS/native_model.git#a91b422cbd53116df1f20b2459fb3d8257458bfd"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1577a0bebf5ed1754e240baf5d9b1845f51e598b20600aa894f55e11cd20cc6c"
dependencies = [
"log",
"proc-macro2",
"quote",
"syn 2.0.101",
@ -4423,9 +4419,9 @@ dependencies = [
[[package]]
name = "reqwest"
version = "0.12.22"
version = "0.12.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbc931937e6ca3a06e3b6c0aa7841849b160a90351d6ab467a8b9b9959767531"
checksum = "2bf597b113be201cb2269b4c39b39a804d01b99ee95a4278f0ed04e45cff1c71"
dependencies = [
"base64 0.22.1",
"bytes",
@ -4440,14 +4436,16 @@ dependencies = [
"hyper-rustls",
"hyper-tls",
"hyper-util",
"ipnet",
"js-sys",
"log",
"mime",
"native-tls",
"once_cell",
"percent-encoding",
"pin-project-lite",
"quinn",
"rustls",
"rustls-native-certs",
"rustls-pki-types",
"serde",
"serde_json",
@ -4493,7 +4491,7 @@ dependencies = [
"anyhow",
"async-trait",
"http 1.3.1",
"reqwest 0.12.22",
"reqwest 0.12.16",
"serde",
"thiserror 1.0.69",
"tower-service",
@ -4528,7 +4526,7 @@ dependencies = [
"async-tungstenite",
"bytes",
"futures-util",
"reqwest 0.12.22",
"reqwest 0.12.16",
"thiserror 2.0.12",
"tokio",
"tokio-util",
@ -4691,18 +4689,6 @@ dependencies = [
"zeroize",
]
[[package]]
name = "rustls-native-certs"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcff2dd52b58a8d98a70243663a0d234c4e2b79235637849d15913394a247d3"
dependencies = [
"openssl-probe",
"rustls-pki-types",
"schannel",
"security-framework 3.2.0",
]
[[package]]
name = "rustls-pki-types"
version = "1.12.0"
@ -4800,19 +4786,6 @@ dependencies = [
"security-framework-sys",
]
[[package]]
name = "security-framework"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "271720403f46ca04f7ba6f55d438f8bd878d6b8ca0a1046e8228c4145bcbb316"
dependencies = [
"bitflags 2.9.1",
"core-foundation 0.10.1",
"core-foundation-sys",
"libc",
"security-framework-sys",
]
[[package]]
name = "security-framework-sys"
version = "2.14.0"
@ -5533,7 +5506,7 @@ dependencies = [
"percent-encoding",
"plist",
"raw-window-handle",
"reqwest 0.12.22",
"reqwest 0.12.16",
"serde",
"serde_json",
"serde_repr",
@ -6086,9 +6059,9 @@ dependencies = [
[[package]]
name = "tokio-util"
version = "0.7.16"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14307c986784f72ef81c89db7d9e28d6ac26d16213b109ea501696195e6e3ce5"
checksum = "66a539a9ad6d5d281510d5bd368c973d636c02dbf8a67300bfb6b950696ad7df"
dependencies = [
"bytes",
"futures-core",
@ -6178,9 +6151,9 @@ dependencies = [
[[package]]
name = "tower-http"
version = "0.6.6"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adc82fd73de2a9722ac5da747f12383d2bfdb93591ee6c58486e0097890f05f2"
checksum = "0fdb0c213ca27a9f57ab69ddb290fd80d970922355b83ae380b395d3986b8a2e"
dependencies = [
"bitflags 2.9.1",
"bytes",

View File

@ -1,6 +1,6 @@
[package]
name = "drop-app"
version = "0.3.3"
version = "0.3.2-dl"
description = "The client application for the open-source, self-hosted game distribution platform Drop"
authors = ["Drop OSS"]
edition = "2024"
@ -65,7 +65,7 @@ whoami = "1.6.0"
filetime = "0.2.25"
walkdir = "2.5.0"
known-folders = "1.2.0"
native_model = { version = "0.6.4", features = ["rmp_serde_1_3"], git = "https://github.com/Drop-OSS/native_model.git"}
native_model = { version = "0.6.1", features = ["rmp_serde_1_3"] }
tauri-plugin-opener = "2.4.0"
bitcode = "0.6.6"
reqwest-websocket = "0.5.0"
@ -73,9 +73,6 @@ futures-lite = "2.6.0"
page_size = "0.6.0"
sysinfo = "0.36.1"
humansize = "2.1.3"
tokio-util = { version = "0.7.16", features = ["io"] }
futures-core = "0.3.31"
bytes = "1.10.1"
# tailscale = { path = "./tailscale" }
[dependencies.dynfmt]
@ -107,17 +104,9 @@ version = "2"
features = ["other_errors"] # You can also use "yaml_enc" or "bin_enc"
[dependencies.reqwest]
version = "0.12.22"
version = "0.12"
default-features = false
features = [
"json",
"http2",
"blocking",
"rustls-tls",
"native-tls-alpn",
"rustls-tls-native-roots",
"stream",
]
features = ["json", "http2", "blocking", "rustls-tls", "native-tls-alpn", "rustls-tls-webpki-roots"]
[dependencies.serde]
version = "1"

View File

@ -8,6 +8,7 @@ use std::{
use chrono::Utc;
use log::{debug, error, info, warn};
use native_model::{Decode, Encode};
use rustbreak::{DeSerError, DeSerializer, PathDatabase, RustbreakError};
use serde::{Serialize, de::DeserializeOwned};
use url::Url;
@ -16,13 +17,8 @@ use crate::DB;
use super::models::data::Database;
#[cfg(not(debug_assertions))]
static DATA_ROOT_PREFIX: &'static str = "drop";
#[cfg(debug_assertions)]
static DATA_ROOT_PREFIX: &str = "drop-debug";
pub static DATA_ROOT_DIR: LazyLock<Arc<PathBuf>> =
LazyLock::new(|| Arc::new(dirs::data_dir().unwrap().join(DATA_ROOT_PREFIX)));
LazyLock::new(|| Arc::new(dirs::data_dir().unwrap().join("drop")));
// Custom JSON serializer to support everything we need
#[derive(Debug, Default, Clone)]
@ -32,7 +28,7 @@ impl<T: native_model::Model + Serialize + DeserializeOwned> DeSerializer<T>
for DropDatabaseSerializer
{
fn serialize(&self, val: &T) -> rustbreak::error::DeSerResult<Vec<u8>> {
native_model::encode(val)
native_model::rmp_serde_1_3::RmpSerde::encode(val)
.map_err(|e| DeSerError::Internal(e.to_string()))
}
@ -40,7 +36,7 @@ impl<T: native_model::Model + Serialize + DeserializeOwned> DeSerializer<T>
let mut buf = Vec::new();
s.read_to_end(&mut buf)
.map_err(|e| rustbreak::error::DeSerError::Other(e.into()))?;
let (val, _version) = native_model::decode(buf)
let val = native_model::rmp_serde_1_3::RmpSerde::decode(buf)
.map_err(|e| DeSerError::Internal(e.to_string()))?;
Ok(val)
}

View File

@ -1,45 +1,32 @@
/**
* NEXT BREAKING CHANGE
*
* UPDATE DATABASE TO USE RPMSERDENAMED
*
* WE CAN'T DELETE ANY FIELDS
*/
pub mod data {
use std::{hash::Hash, path::PathBuf};
use std::path::PathBuf;
use native_model::native_model;
use serde::{Deserialize, Serialize};
// NOTE: Within each version, you should NEVER use these types.
// Declare it using the actual version that it is from, i.e. v1::Settings rather than just Settings from here
pub type GameVersion = v1::GameVersion;
pub type Database = v4::Database;
pub type Database = v3::Database;
pub type Settings = v1::Settings;
pub type DatabaseAuth = v1::DatabaseAuth;
pub type GameDownloadStatus = v2::GameDownloadStatus;
pub type ApplicationTransientStatus = v1::ApplicationTransientStatus;
/**
* Need to be universally accessible by the ID, and the version is just a couple sprinkles on top
*/
pub type DownloadableMetadata = v1::DownloadableMetadata;
pub type DownloadType = v1::DownloadType;
pub type DatabaseApplications = v2::DatabaseApplications;
//pub type DatabaseCompatInfo = v2::DatabaseCompatInfo;
pub type PlaytimeData = v4::PlaytimeData;
pub type GamePlaytimeStats = v4::GamePlaytimeStats;
pub type PlaytimeSession = v4::PlaytimeSession;
pub type DatabaseCompatInfo = v2::DatabaseCompatInfo;
use std::collections::HashMap;
impl PartialEq for DownloadableMetadata {
fn eq(&self, other: &Self) -> bool {
self.id == other.id && self.download_type == other.download_type
}
}
impl Hash for DownloadableMetadata {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state);
self.download_type.hash(state);
}
}
mod v1 {
pub mod v1 {
use crate::process::process_manager::Platform;
use serde_with::serde_as;
use std::{collections::HashMap, path::PathBuf};
@ -129,7 +116,6 @@ pub mod data {
// Stuff that shouldn't be synced to disk
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum ApplicationTransientStatus {
Queued { version_name: String },
Downloading { version_name: String },
Uninstalling {},
Updating { version_name: String },
@ -158,7 +144,7 @@ pub mod data {
}
#[native_model(id = 7, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
#[derive(Debug, Eq, PartialOrd, Ord, Serialize, Deserialize, Clone)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DownloadableMetadata {
pub id: String,
@ -188,21 +174,22 @@ pub mod data {
}
}
mod v2 {
pub mod v2 {
use std::{collections::HashMap, path::PathBuf};
use serde_with::serde_as;
use super::{
Deserialize, Serialize, native_model, v1,
ApplicationTransientStatus, DatabaseAuth, Deserialize, DownloadableMetadata,
GameVersion, Serialize, Settings, native_model, v1,
};
#[native_model(id = 1, version = 2, with = native_model::rmp_serde_1_3::RmpSerde, from = v1::Database)]
#[native_model(id = 1, version = 2, with = native_model::rmp_serde_1_3::RmpSerde)]
#[derive(Serialize, Deserialize, Clone, Default)]
pub struct Database {
#[serde(default)]
pub settings: v1::Settings,
pub auth: Option<v1::DatabaseAuth>,
pub settings: Settings,
pub auth: Option<DatabaseAuth>,
pub base_url: String,
pub applications: v1::DatabaseApplications,
#[serde(skip)]
@ -211,7 +198,7 @@ pub mod data {
pub compat_info: Option<DatabaseCompatInfo>,
}
#[native_model(id = 9, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
#[native_model(id = 8, version = 2, with = native_model::rmp_serde_1_3::RmpSerde)]
#[derive(Serialize, Deserialize, Clone, Default)]
pub struct DatabaseCompatInfo {
@ -234,7 +221,7 @@ pub mod data {
// Strings are version names for a particular game
#[derive(Serialize, Clone, Deserialize, Debug)]
#[serde(tag = "type")]
#[native_model(id = 5, version = 2, with = native_model::rmp_serde_1_3::RmpSerde, from = v1::GameDownloadStatus)]
#[native_model(id = 5, version = 2, with = native_model::rmp_serde_1_3::RmpSerde)]
pub enum GameDownloadStatus {
Remote {},
SetupRequired {
@ -274,17 +261,16 @@ pub mod data {
#[serde_as]
#[derive(Serialize, Clone, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[native_model(id = 3, version = 2, with = native_model::rmp_serde_1_3::RmpSerde, from=v1::DatabaseApplications)]
#[native_model(id = 3, version = 2, with = native_model::rmp_serde_1_3::RmpSerde)]
pub struct DatabaseApplications {
pub install_dirs: Vec<PathBuf>,
// Guaranteed to exist if the game also exists in the app state map
pub game_statuses: HashMap<String, GameDownloadStatus>,
pub game_versions: HashMap<String, HashMap<String, v1::GameVersion>>,
pub installed_game_version: HashMap<String, v1::DownloadableMetadata>,
pub game_versions: HashMap<String, HashMap<String, GameVersion>>,
pub installed_game_version: HashMap<String, DownloadableMetadata>,
#[serde(skip)]
pub transient_statuses: HashMap<v1::DownloadableMetadata, v1::ApplicationTransientStatus>,
pub transient_statuses: HashMap<DownloadableMetadata, ApplicationTransientStatus>,
}
impl From<v1::DatabaseApplications> for DatabaseApplications {
fn from(value: v1::DatabaseApplications) -> Self {
@ -306,21 +292,21 @@ pub mod data {
use std::path::PathBuf;
use super::{
Deserialize, Serialize,
native_model, v2, v1,
DatabaseApplications, DatabaseAuth, DatabaseCompatInfo, Deserialize, Serialize,
Settings, native_model, v2,
};
#[native_model(id = 1, version = 3, with = native_model::rmp_serde_1_3::RmpSerde, from = v2::Database)]
#[native_model(id = 1, version = 3, with = native_model::rmp_serde_1_3::RmpSerde)]
#[derive(Serialize, Deserialize, Clone, Default)]
pub struct Database {
#[serde(default)]
pub settings: v1::Settings,
pub auth: Option<v1::DatabaseAuth>,
pub settings: Settings,
pub auth: Option<DatabaseAuth>,
pub base_url: String,
pub applications: v2::DatabaseApplications,
pub applications: DatabaseApplications,
#[serde(skip)]
pub prev_database: Option<PathBuf>,
pub cache_dir: PathBuf,
pub compat_info: Option<v2::DatabaseCompatInfo>,
pub compat_info: Option<DatabaseCompatInfo>,
}
impl From<v2::Database> for Database {
@ -358,110 +344,7 @@ pub mod data {
settings: Settings::default(),
cache_dir,
compat_info: None,
playtime_data: PlaytimeData::default(),
}
}
}
mod v4 {
use std::{collections::HashMap, path::PathBuf, time::SystemTime};
use super::{
DatabaseApplications, DatabaseAuth, DatabaseCompatInfo, Deserialize, Serialize,
Settings, native_model, v3,
};
#[native_model(id = 1, version = 4, with = native_model::rmp_serde_1_3::RmpSerde)]
#[derive(Serialize, Deserialize, Clone, Default)]
pub struct Database {
#[serde(default)]
pub settings: Settings,
pub auth: Option<DatabaseAuth>,
pub base_url: String,
pub applications: DatabaseApplications,
#[serde(skip)]
pub prev_database: Option<PathBuf>,
pub cache_dir: PathBuf,
pub compat_info: Option<DatabaseCompatInfo>,
#[serde(default)]
pub playtime_data: PlaytimeData,
}
#[derive(Serialize, Deserialize, Clone, Default)]
#[native_model(id = 9, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
pub struct PlaytimeData {
pub game_sessions: HashMap<String, GamePlaytimeStats>,
#[serde(skip)]
pub active_sessions: HashMap<String, PlaytimeSession>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[native_model(id = 10, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
pub struct GamePlaytimeStats {
pub game_id: String,
pub total_playtime_seconds: u64,
pub session_count: u32,
pub first_played: SystemTime,
pub last_played: SystemTime,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[native_model(id = 11, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
pub struct PlaytimeSession {
pub game_id: String,
pub start_time: SystemTime,
pub session_id: String,
}
impl GamePlaytimeStats {
pub fn new(game_id: String) -> Self {
let now = SystemTime::now();
Self {
game_id,
total_playtime_seconds: 0,
session_count: 0,
first_played: now,
last_played: now,
}
}
pub fn average_session_length(&self) -> u64 {
if self.session_count == 0 {
0
} else {
self.total_playtime_seconds / self.session_count as u64
}
}
}
impl PlaytimeSession {
pub fn new(game_id: String) -> Self {
Self {
game_id,
start_time: SystemTime::now(),
session_id: uuid::Uuid::new_v4().to_string(),
}
}
pub fn duration(&self) -> std::time::Duration {
self.start_time.elapsed().unwrap_or_default()
}
}
impl From<v3::Database> for Database {
fn from(value: v3::Database) -> Self {
Self {
settings: value.settings,
auth: value.auth,
base_url: value.base_url,
applications: value.applications,
prev_database: value.prev_database,
cache_dir: value.cache_dir,
compat_info: value.compat_info,
playtime_data: PlaytimeData::default(),
}
}
}
}
}

View File

@ -5,10 +5,10 @@ use log::warn;
use crate::{
database::{
db::borrow_db_mut_checked,
models::data::{DownloadType, DownloadableMetadata},
models::data::v1::{DownloadType, DownloadableMetadata},
},
games::{
downloads::drop_data::{DropData, DROP_DATA_PATH},
downloads::drop_data::{v1::DropData, DROP_DATA_PATH},
library::set_partially_installed_db,
},
};

View File

@ -12,7 +12,6 @@ use tauri::{AppHandle, Emitter};
use crate::{
database::models::data::DownloadableMetadata,
download_manager::download_manager_frontend::DownloadStatus,
error::application_download_error::ApplicationDownloadError,
games::library::{QueueUpdateEvent, QueueUpdateEventQueueData, StatsUpdateEvent},
};
@ -76,6 +75,7 @@ pub struct DownloadManagerBuilder {
status: Arc<Mutex<DownloadManagerStatus>>,
app_handle: AppHandle,
current_download_agent: Option<DownloadAgent>, // Should be the only download agent in the map with the "Go" flag
current_download_thread: Mutex<Option<JoinHandle<()>>>,
active_control_flag: Option<DownloadThreadControl>,
}
@ -95,6 +95,7 @@ impl DownloadManagerBuilder {
progress: active_progress.clone(),
app_handle,
current_download_agent: None,
current_download_thread: Mutex::new(None),
active_control_flag: None,
};
@ -120,18 +121,14 @@ impl DownloadManagerBuilder {
fn cleanup_current_download(&mut self) {
self.active_control_flag = None;
*self.progress.lock().unwrap() = None;
self.current_download_agent = None;
let mut download_thread_lock = self.current_download_thread.lock().unwrap();
if let Some(unfinished_thread) = download_thread_lock.take()
&& !unfinished_thread.is_finished()
{
unfinished_thread.join().unwrap();
}
*download_thread_lock = None;
drop(download_thread_lock);
}
fn stop_and_wait_current_download(&self) -> bool {
fn stop_and_wait_current_download(&self) {
self.set_status(DownloadManagerStatus::Paused);
if let Some(current_flag) = &self.active_control_flag {
current_flag.set(DownloadThreadControlFlag::Stop);
@ -139,10 +136,8 @@ impl DownloadManagerBuilder {
let mut download_thread_lock = self.current_download_thread.lock().unwrap();
if let Some(current_download_thread) = download_thread_lock.take() {
return current_download_thread.join().is_ok();
};
true
current_download_thread.join().unwrap();
}
}
fn manage_queue(mut self) -> Result<(), ()> {
@ -195,7 +190,7 @@ impl DownloadManagerBuilder {
return;
}
download_agent.on_queued(&self.app_handle);
download_agent.on_initialised(&self.app_handle);
self.download_queue.append(meta.clone());
self.download_agent_registry.insert(meta, download_agent);
@ -214,13 +209,23 @@ impl DownloadManagerBuilder {
return;
}
if self.current_download_agent.is_some()
&& self.download_queue.read().front().unwrap()
== &self.current_download_agent.as_ref().unwrap().metadata()
{
debug!(
"Current download agent: {:?}",
self.current_download_agent.as_ref().unwrap().metadata()
);
return;
}
debug!("current download queue: {:?}", self.download_queue.read());
let agent_data = if let Some(agent_data) = self.download_queue.read().front() {
agent_data.clone()
} else {
return;
};
// Should always be Some if the above two statements keep going
let agent_data = self.download_queue.read().front().unwrap().clone();
info!("starting download for {agent_data:?}");
let download_agent = self
.download_agent_registry
@ -228,22 +233,8 @@ impl DownloadManagerBuilder {
.unwrap()
.clone();
let status = download_agent.status();
// This download is already going
if status != DownloadStatus::Queued {
return;
}
// Ensure all others are marked as queued
for agent in self.download_agent_registry.values() {
if agent.metadata() != agent_data && agent.status() != DownloadStatus::Queued {
agent.on_queued(&self.app_handle);
}
}
info!("starting download for {agent_data:?}");
self.active_control_flag = Some(download_agent.control_flag());
self.current_download_agent = Some(download_agent.clone());
let sender = self.sender.clone();
@ -263,16 +254,12 @@ impl DownloadManagerBuilder {
}
};
// If the download gets canceled
// If the download gets cancelled
// immediately return, on_cancelled gets called for us earlier
if !download_result {
return;
}
if download_agent.control_flag().get() == DownloadThreadControlFlag::Stop {
return;
}
let validate_result = match download_agent.validate(&app_handle) {
Ok(v) => v,
Err(e) => {
@ -287,10 +274,6 @@ impl DownloadManagerBuilder {
}
};
if download_agent.control_flag().get() == DownloadThreadControlFlag::Stop {
return;
}
if validate_result {
download_agent.on_complete(&app_handle);
sender
@ -316,8 +299,8 @@ impl DownloadManagerBuilder {
}
fn manage_completed_signal(&mut self, meta: DownloadableMetadata) {
debug!("got signal Completed");
if let Some(interface) = self.download_queue.read().front()
&& interface == &meta
if let Some(interface) = &self.current_download_agent
&& interface.metadata() == meta
{
self.remove_and_cleanup_front_download(&meta);
}
@ -327,25 +310,19 @@ impl DownloadManagerBuilder {
}
fn manage_error_signal(&mut self, error: ApplicationDownloadError) {
debug!("got signal Error");
if let Some(metadata) = self.download_queue.read().front()
&& let Some(current_agent) = self.download_agent_registry.get(metadata)
{
if let Some(current_agent) = self.current_download_agent.clone() {
current_agent.on_error(&self.app_handle, &error);
self.stop_and_wait_current_download();
self.remove_and_cleanup_front_download(metadata);
self.remove_and_cleanup_front_download(&current_agent.metadata());
}
self.push_ui_queue_update();
self.set_status(DownloadManagerStatus::Error);
}
fn manage_cancel_signal(&mut self, meta: &DownloadableMetadata) {
debug!("got signal Cancel");
// If the current download is the one we're tryna cancel
if let Some(current_metadata) = self.download_queue.read().front()
&& current_metadata == meta
&& let Some(current_download) = self.download_agent_registry.get(current_metadata)
{
if let Some(current_download) = &self.current_download_agent {
if &current_download.metadata() == meta {
self.set_status(DownloadManagerStatus::Paused);
current_download.on_cancelled(&self.app_handle);
self.stop_and_wait_current_download();
@ -353,10 +330,9 @@ impl DownloadManagerBuilder {
self.download_queue.pop_front();
self.cleanup_current_download();
self.download_agent_registry.remove(meta);
debug!("current download queue: {:?}", self.download_queue.read());
}
// else just cancel it
// TODO: Collapse these two into a single if statement somehow
else if let Some(download_agent) = self.download_agent_registry.get(meta) {
let index = self.download_queue.get_by_meta(meta);
if let Some(index) = index {
@ -370,7 +346,19 @@ impl DownloadManagerBuilder {
);
}
}
self.sender.send(DownloadManagerSignal::Go).unwrap();
} else if let Some(download_agent) = self.download_agent_registry.get(meta) {
let index = self.download_queue.get_by_meta(meta);
if let Some(index) = index {
download_agent.on_cancelled(&self.app_handle);
let _ = self.download_queue.edit().remove(index).unwrap();
let removed = self.download_agent_registry.remove(meta);
debug!(
"removed {:?} from queue {:?}",
removed.map(|x| x.metadata()),
self.download_queue.read()
);
}
}
self.push_ui_queue_update();
}
fn push_ui_stats_update(&self, kbs: usize, time: usize) {

View File

@ -62,7 +62,7 @@ impl Serialize for DownloadManagerStatus {
}
}
#[derive(Serialize, Clone, Debug, PartialEq)]
#[derive(Serialize, Clone, Debug)]
pub enum DownloadStatus {
Queued,
Downloading,

View File

@ -12,12 +12,6 @@ use super::{
util::{download_thread_control_flag::DownloadThreadControl, progress_object::ProgressObject},
};
/**
* Downloadables are responsible for managing their specific object's download state
* e.g, the GameDownloadAgent is responsible for pushing game updates
*
* But the download manager manages the queue state
*/
pub trait Downloadable: Send + Sync {
fn download(&self, app_handle: &AppHandle) -> Result<bool, ApplicationDownloadError>;
fn validate(&self, app_handle: &AppHandle) -> Result<bool, ApplicationDownloadError>;
@ -26,7 +20,7 @@ pub trait Downloadable: Send + Sync {
fn control_flag(&self) -> DownloadThreadControl;
fn status(&self) -> DownloadStatus;
fn metadata(&self) -> DownloadableMetadata;
fn on_queued(&self, app_handle: &AppHandle);
fn on_initialised(&self, app_handle: &AppHandle);
fn on_error(&self, app_handle: &AppHandle, error: &ApplicationDownloadError);
fn on_complete(&self, app_handle: &AppHandle);
fn on_cancelled(&self, app_handle: &AppHandle);

View File

@ -1,8 +1,8 @@
use std::{
sync::{
Arc, Mutex,
atomic::{AtomicUsize, Ordering},
mpsc::Sender,
Arc, Mutex,
},
time::{Duration, Instant},
};
@ -23,7 +23,7 @@ pub struct ProgressObject {
//last_update: Arc<RwLock<Instant>>,
last_update_time: Arc<AtomicInstant>,
bytes_last_update: Arc<AtomicUsize>,
rolling: RollingProgressWindow<1000>,
rolling: RollingProgressWindow<10>,
}
#[derive(Clone)]
@ -120,7 +120,7 @@ pub fn calculate_update(progress: &ProgressObject) {
let last_update_time = progress
.last_update_time
.swap(Instant::now(), Ordering::SeqCst);
let time_since_last_update = Instant::now().duration_since(last_update_time).as_millis_f64();
let time_since_last_update = Instant::now().duration_since(last_update_time).as_millis();
let current_bytes_downloaded = progress.sum();
let max = progress.get_max();
@ -128,17 +128,17 @@ pub fn calculate_update(progress: &ProgressObject) {
.bytes_last_update
.swap(current_bytes_downloaded, Ordering::Acquire);
let bytes_since_last_update = current_bytes_downloaded.saturating_sub(bytes_at_last_update) as f64;
let bytes_since_last_update = current_bytes_downloaded - bytes_at_last_update;
let kilobytes_per_second = bytes_since_last_update / time_since_last_update;
let kilobytes_per_second = bytes_since_last_update / (time_since_last_update as usize).max(1);
let bytes_remaining = max.saturating_sub(current_bytes_downloaded); // bytes
progress.update_window(kilobytes_per_second as usize);
progress.update_window(kilobytes_per_second);
push_update(progress, bytes_remaining);
}
#[throttle(1, Duration::from_millis(250))]
#[throttle(1, Duration::from_millis(500))]
pub fn push_update(progress: &ProgressObject, bytes_remaining: usize) {
let average_speed = progress.rolling.get_average();
let time_remaining = (bytes_remaining / 1000) / average_speed.max(1);

View File

@ -1,6 +1,6 @@
use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
Arc,
};
#[derive(Clone)]
@ -22,22 +22,17 @@ impl<const S: usize> RollingProgressWindow<S> {
}
pub fn get_average(&self) -> usize {
let current = self.current.load(Ordering::SeqCst);
let valid = self
.window
self.window
.iter()
.enumerate()
.filter(|(i, _)| i < &current)
.map(|(_, x)| x.load(Ordering::Acquire))
.collect::<Vec<usize>>();
let amount = valid.len();
let sum = valid.into_iter().sum::<usize>();
sum / amount
.sum::<usize>()
/ S
}
pub fn reset(&self) {
self.window
.iter()
.for_each(|x| x.store(0, Ordering::Release));
self.current.store(0, Ordering::Release);
}
}

View File

@ -1,6 +1,6 @@
use std::{
fmt::{Display, Formatter},
io, sync::Arc,
io,
};
use serde_with::SerializeDisplay;
@ -11,20 +11,17 @@ use super::remote_access_error::RemoteAccessError;
// TODO: Rename / separate from downloads
#[derive(Debug, SerializeDisplay)]
pub enum ApplicationDownloadError {
NotInitialized,
Communication(RemoteAccessError),
DiskFull(u64, u64),
#[allow(dead_code)]
Checksum,
Lock,
IoError(Arc<io::Error>),
IoError(io::ErrorKind),
DownloadError,
}
impl Display for ApplicationDownloadError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ApplicationDownloadError::NotInitialized => write!(f, "Download not initalized, did something go wrong?"),
ApplicationDownloadError::DiskFull(required, available) => write!(
f,
"Game requires {}, {} remaining left on disk.",
@ -42,7 +39,7 @@ impl Display for ApplicationDownloadError {
ApplicationDownloadError::IoError(error) => write!(f, "io error: {error}"),
ApplicationDownloadError::DownloadError => write!(
f,
"Download failed. See Download Manager status for specific error"
"download failed. See Download Manager status for specific error"
),
}
}

View File

@ -11,8 +11,7 @@ pub enum ProcessError {
IOError(Error),
FormatError(String), // String errors supremacy
InvalidPlatform,
OpenerError(tauri_plugin_opener::Error),
PlaytimeError(String),
OpenerError(tauri_plugin_opener::Error)
}
impl Display for ProcessError {
@ -26,7 +25,6 @@ impl Display for ProcessError {
ProcessError::InvalidPlatform => "This game cannot be played on the current platform",
ProcessError::FormatError(e) => &format!("Failed to format template: {e}"),
ProcessError::OpenerError(error) => &format!("Failed to open directory: {error}"),
ProcessError::PlaytimeError(error) => &format!("Playtime tracking error: {error}"),
};
write!(f, "{s}")
}

View File

@ -23,7 +23,6 @@ pub enum RemoteAccessError {
ManifestDownloadFailed(StatusCode, String),
OutOfSync,
Cache(std::io::Error),
CorruptedState,
}
impl Display for RemoteAccessError {
@ -82,10 +81,6 @@ impl Display for RemoteAccessError {
"server's and client's time are out of sync. Please ensure they are within at least 30 seconds of each other"
),
RemoteAccessError::Cache(error) => write!(f, "Cache Error: {error}"),
RemoteAccessError::CorruptedState => write!(
f,
"Drop encountered a corrupted internal state. Please report this to the developers, with details of reproduction."
),
}
}
}

View File

@ -1,11 +1,10 @@
use bitcode::{Decode, Encode};
use serde::{Deserialize, Serialize};
use crate::games::library::Game;
pub type Collections = Vec<Collection>;
#[derive(Serialize, Deserialize, Debug, Clone, Default, Encode, Decode)]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct Collection {
id: String,
@ -15,7 +14,7 @@ pub struct Collection {
entries: Vec<CollectionObject>,
}
#[derive(Serialize, Deserialize, Debug, Clone, Default, Encode, Decode)]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct CollectionObject {
collection_id: String,

View File

@ -1,110 +1,109 @@
use serde_json::json;
use url::Url;
use crate::{
DB,
database::db::DatabaseImpls,
error::remote_access_error::RemoteAccessError,
remote::{
auth::generate_authorization_header,
cache::{cache_object, get_cached_object},
requests::{generate_url, make_authenticated_get},
utils::DROP_CLIENT_ASYNC,
},
remote::{auth::generate_authorization_header, requests::make_request, utils::DROP_CLIENT_SYNC},
};
use super::collection::{Collection, Collections};
#[tauri::command]
pub async fn fetch_collections(
hard_refresh: Option<bool>,
) -> Result<Collections, RemoteAccessError> {
let do_hard_refresh = hard_refresh.unwrap_or(false);
if !do_hard_refresh && let Ok(cached_response) = get_cached_object::<Collections>("collections")
{
return Ok(cached_response);
}
pub fn fetch_collections() -> Result<Collections, RemoteAccessError> {
let client = DROP_CLIENT_SYNC.clone();
let response = make_request(&client, &["/api/v1/client/collection"], &[], |r| {
r.header("Authorization", generate_authorization_header())
})?
.send()?;
let response =
make_authenticated_get(generate_url(&["/api/v1/client/collection"], &[])?).await?;
let collections: Collections = response.json().await?;
cache_object("collections", &collections)?;
Ok(collections)
Ok(response.json()?)
}
#[tauri::command]
pub async fn fetch_collection(collection_id: String) -> Result<Collection, RemoteAccessError> {
let response = make_authenticated_get(generate_url(
pub fn fetch_collection(collection_id: String) -> Result<Collection, RemoteAccessError> {
let client = DROP_CLIENT_SYNC.clone();
let response = make_request(
&client,
&["/api/v1/client/collection/", &collection_id],
&[],
)?)
.await?;
|r| r.header("Authorization", generate_authorization_header()),
)?
.send()?;
Ok(response.json().await?)
Ok(response.json()?)
}
#[tauri::command]
pub async fn create_collection(name: String) -> Result<Collection, RemoteAccessError> {
let client = DROP_CLIENT_ASYNC.clone();
let url = generate_url(&["/api/v1/client/collection"], &[])?;
pub fn create_collection(name: String) -> Result<Collection, RemoteAccessError> {
let client = DROP_CLIENT_SYNC.clone();
let base_url = DB.fetch_base_url();
let base_url = Url::parse(&format!("{base_url}api/v1/client/collection/"))?;
let response = client
.post(url)
.post(base_url)
.header("Authorization", generate_authorization_header())
.json(&json!({"name": name}))
.send()
.await?;
.send()?;
Ok(response.json().await?)
Ok(response.json()?)
}
#[tauri::command]
pub async fn add_game_to_collection(
pub fn add_game_to_collection(
collection_id: String,
game_id: String,
) -> Result<(), RemoteAccessError> {
let client = DROP_CLIENT_ASYNC.clone();
let url = generate_url(&["/api/v1/client/collection", &collection_id, "entry"], &[])?;
let client = DROP_CLIENT_SYNC.clone();
let url = Url::parse(&format!(
"{}api/v1/client/collection/{}/entry/",
DB.fetch_base_url(),
collection_id
))?;
client
.post(url)
.header("Authorization", generate_authorization_header())
.json(&json!({"id": game_id}))
.send()
.await?;
.send()?;
Ok(())
}
#[tauri::command]
pub async fn delete_collection(collection_id: String) -> Result<bool, RemoteAccessError> {
let client = DROP_CLIENT_ASYNC.clone();
let url = generate_url(&["/api/v1/client/collection", &collection_id], &[])?;
pub fn delete_collection(collection_id: String) -> Result<bool, RemoteAccessError> {
let client = DROP_CLIENT_SYNC.clone();
let base_url = Url::parse(&format!(
"{}api/v1/client/collection/{}",
DB.fetch_base_url(),
collection_id
))?;
let response = client
.delete(url)
.delete(base_url)
.header("Authorization", generate_authorization_header())
.send()
.await?;
.send()?;
Ok(response.json().await?)
Ok(response.json()?)
}
#[tauri::command]
pub async fn delete_game_in_collection(
pub fn delete_game_in_collection(
collection_id: String,
game_id: String,
) -> Result<(), RemoteAccessError> {
let client = DROP_CLIENT_ASYNC.clone();
let url = generate_url(&["/api/v1/client/collection", &collection_id, "entry"], &[])?;
let client = DROP_CLIENT_SYNC.clone();
let base_url = Url::parse(&format!(
"{}api/v1/client/collection/{}/entry",
DB.fetch_base_url(),
collection_id
))?;
client
.delete(url)
.delete(base_url)
.header("Authorization", generate_authorization_header())
.json(&json!({"id": game_id}))
.send()
.await?;
.send()?;
Ok(())
}

View File

@ -18,30 +18,28 @@ use crate::{
use super::{
library::{
FetchGameStruct, Game, fetch_game_logic, fetch_game_version_options_logic,
FetchGameStruct, Game, fetch_game_logic, fetch_game_verion_options_logic,
fetch_library_logic,
},
state::{GameStatusManager, GameStatusWithTransient},
};
#[tauri::command]
pub async fn fetch_library(
state: tauri::State<'_, Mutex<AppState<'_>>>,
hard_refresh: Option<bool>,
pub fn fetch_library(
state: tauri::State<'_, Mutex<AppState>>,
) -> Result<Vec<Game>, RemoteAccessError> {
offline!(
state,
fetch_library_logic,
fetch_library_logic_offline,
state,
hard_refresh
).await
state
)
}
#[tauri::command]
pub async fn fetch_game(
pub fn fetch_game(
game_id: String,
state: tauri::State<'_, Mutex<AppState<'_>>>,
state: tauri::State<'_, Mutex<AppState>>,
) -> Result<FetchGameStruct, RemoteAccessError> {
offline!(
state,
@ -49,7 +47,7 @@ pub async fn fetch_game(
fetch_game_logic_offline,
game_id,
state
).await
)
}
#[tauri::command]
@ -70,9 +68,9 @@ pub fn uninstall_game(game_id: String, app_handle: AppHandle) -> Result<(), Libr
}
#[tauri::command]
pub async fn fetch_game_version_options(
pub fn fetch_game_verion_options(
game_id: String,
state: tauri::State<'_, Mutex<AppState<'_>>>,
state: tauri::State<'_, Mutex<AppState>>,
) -> Result<Vec<GameVersion>, RemoteAccessError> {
fetch_game_version_options_logic(game_id, state).await
fetch_game_verion_options_logic(game_id, state)
}

View File

@ -3,47 +3,44 @@ use std::{
sync::{Arc, Mutex},
};
use crate::{
AppState,
database::{
db::borrow_db_checked,
models::data::GameDownloadStatus,
},
download_manager::downloadable::Downloadable,
database::{db::borrow_db_checked, models::data::GameDownloadStatus},
download_manager::
downloadable::Downloadable
,
error::application_download_error::ApplicationDownloadError,
AppState,
};
use super::download_agent::GameDownloadAgent;
#[tauri::command]
pub async fn download_game(
pub fn download_game(
game_id: String,
game_version: String,
install_dir: usize,
state: tauri::State<'_, Mutex<AppState<'_>>>,
state: tauri::State<'_, Mutex<AppState>>,
) -> Result<(), ApplicationDownloadError> {
let sender = { state.lock().unwrap().download_manager.get_sender().clone() };
let game_download_agent =
GameDownloadAgent::new_from_index(game_id.clone(), game_version.clone(), install_dir, sender).await?;
let game_download_agent =
Arc::new(Box::new(game_download_agent) as Box<dyn Downloadable + Send + Sync>);
let sender = state.lock().unwrap().download_manager.get_sender();
let game_download_agent = GameDownloadAgent::new_from_index(
game_id,
game_version,
install_dir,
sender,
)?;
let game_download_agent = Arc::new(Box::new(game_download_agent) as Box<dyn Downloadable + Send + Sync>);
state
.lock()
.unwrap()
.download_manager
.queue_download(game_download_agent.clone())
.unwrap();
.queue_download(game_download_agent).unwrap();
Ok(())
}
#[tauri::command]
pub async fn resume_download(
pub fn resume_download(
game_id: String,
state: tauri::State<'_, Mutex<AppState<'_>>>,
state: tauri::State<'_, Mutex<AppState>>,
) -> Result<(), ApplicationDownloadError> {
let s = borrow_db_checked()
.applications
@ -65,21 +62,17 @@ pub async fn resume_download(
let sender = state.lock().unwrap().download_manager.get_sender();
let parent_dir: PathBuf = install_dir.into();
let game_download_agent = Arc::new(Box::new(
GameDownloadAgent::new(
let game_download_agent = Arc::new(Box::new(GameDownloadAgent::new(
game_id,
version_name.clone(),
parent_dir.parent().unwrap().to_path_buf(),
sender,
)
.await?,
) as Box<dyn Downloadable + Send + Sync>);
)?) as Box<dyn Downloadable + Send + Sync>);
state
.lock()
.unwrap()
.download_manager
.queue_download(game_download_agent)
.unwrap();
.queue_download(game_download_agent).unwrap();
Ok(())
}

View File

@ -11,18 +11,16 @@ use crate::download_manager::util::download_thread_control_flag::{
use crate::download_manager::util::progress_object::{ProgressHandle, ProgressObject};
use crate::error::application_download_error::ApplicationDownloadError;
use crate::error::remote_access_error::RemoteAccessError;
use crate::games::downloads::manifest::{
DownloadBucket, DownloadContext, DownloadDrop, DropManifest, DropValidateContext, ManifestBody,
};
use crate::games::downloads::manifest::{DropDownloadContext, DropManifest};
use crate::games::downloads::validate::validate_game_chunk;
use crate::games::library::{on_game_complete, push_game_update, set_partially_installed};
use crate::games::state::GameStatusManager;
use crate::process::utils::get_disk_available;
use crate::remote::requests::generate_url;
use crate::remote::utils::{DROP_CLIENT_ASYNC, DROP_CLIENT_SYNC};
use crate::remote::requests::make_request;
use crate::remote::utils::DROP_CLIENT_SYNC;
use log::{debug, error, info, warn};
use rayon::ThreadPoolBuilder;
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use std::fs::{OpenOptions, create_dir_all};
use std::path::{Path, PathBuf};
use std::sync::mpsc::Sender;
@ -33,19 +31,16 @@ use tauri::{AppHandle, Emitter};
#[cfg(target_os = "linux")]
use rustix::fs::{FallocateFlags, fallocate};
use super::download_logic::download_game_bucket;
use super::download_logic::download_game_chunk;
use super::drop_data::DropData;
static RETRY_COUNT: usize = 3;
const TARGET_BUCKET_SIZE: usize = 63 * 1000 * 1000;
const MAX_FILES_PER_BUCKET: usize = (1024 / 4) - 1;
pub struct GameDownloadAgent {
pub id: String,
pub version: String,
pub control_flag: DownloadThreadControl,
buckets: Mutex<Vec<DownloadBucket>>,
contexts: Mutex<Vec<DropDownloadContext>>,
context_map: Mutex<HashMap<String, bool>>,
pub manifest: Mutex<Option<DropManifest>>,
pub progress: Arc<ProgressObject>,
@ -55,21 +50,19 @@ pub struct GameDownloadAgent {
}
impl GameDownloadAgent {
pub async fn new_from_index(
pub fn new_from_index(
id: String,
version: String,
target_download_dir: usize,
sender: Sender<DownloadManagerSignal>,
) -> Result<Self, ApplicationDownloadError> {
let base_dir = {
let db_lock = borrow_db_checked();
let base_dir = db_lock.applications.install_dirs[target_download_dir].clone();
drop(db_lock);
db_lock.applications.install_dirs[target_download_dir].clone()
};
Self::new(id, version, base_dir, sender).await
Self::new(id, version, base_dir, sender)
}
pub async fn new(
pub fn new(
id: String,
version: String,
base_dir: PathBuf,
@ -84,14 +77,12 @@ impl GameDownloadAgent {
let stored_manifest =
DropData::generate(id.clone(), version.clone(), data_base_dir_path.clone());
let context_lock = stored_manifest.contexts.lock().unwrap().clone();
let result = Self {
id,
version,
control_flag,
manifest: Mutex::new(None),
buckets: Mutex::new(Vec::new()),
contexts: Mutex::new(Vec::new()),
context_map: Mutex::new(HashMap::new()),
progress: Arc::new(ProgressObject::new(0, 0, sender.clone())),
sender,
@ -99,7 +90,7 @@ impl GameDownloadAgent {
status: Mutex::new(DownloadStatus::Queued),
};
result.ensure_manifest_exists().await?;
result.ensure_manifest_exists()?;
let required_space = result
.manifest
@ -108,15 +99,9 @@ impl GameDownloadAgent {
.as_ref()
.unwrap()
.values()
.map(|e| {
e.lengths
.iter()
.enumerate()
.filter(|(i, _)| *context_lock.get(&e.checksums[*i]).unwrap_or(&false))
.map(|(_, v)| v)
.map(|e| e.lengths.iter().sum::<usize>())
.sum::<usize>()
})
.sum::<usize>() as u64;
as u64;
let available_space = get_disk_available(data_base_dir_path)? as u64;
@ -132,25 +117,26 @@ impl GameDownloadAgent {
// Blocking
pub fn setup_download(&self, app_handle: &AppHandle) -> Result<(), ApplicationDownloadError> {
let mut db_lock = borrow_db_mut_checked();
let status = ApplicationTransientStatus::Downloading {
version_name: self.version.clone(),
};
db_lock
.applications
.transient_statuses
.insert(self.metadata(), status.clone());
// Don't use GameStatusManager because this game isn't installed
push_game_update(app_handle, &self.metadata().id, None, (None, Some(status)));
self.ensure_manifest_exists()?;
if !self.check_manifest_exists() {
return Err(ApplicationDownloadError::NotInitialized);
}
self.ensure_buckets()?;
self.ensure_contexts()?;
self.control_flag.set(DownloadThreadControlFlag::Go);
let mut db_lock = borrow_db_mut_checked();
db_lock.applications.transient_statuses.insert(
self.metadata(),
ApplicationTransientStatus::Downloading {
version_name: self.version.clone(),
},
);
push_game_update(
app_handle,
&self.metadata().id,
None,
GameStatusManager::fetch_state(&self.metadata().id, &db_lock),
);
Ok(())
}
@ -161,7 +147,9 @@ impl GameDownloadAgent {
info!("beginning download for {}...", self.metadata().id);
let res = self.run().map_err(ApplicationDownloadError::Communication);
let res = self
.run()
.map_err(|()| ApplicationDownloadError::DownloadError);
debug!(
"{} took {}ms to download",
@ -171,43 +159,37 @@ impl GameDownloadAgent {
res
}
pub fn check_manifest_exists(&self) -> bool {
self.manifest.lock().unwrap().is_some()
}
pub async fn ensure_manifest_exists(&self) -> Result<(), ApplicationDownloadError> {
pub fn ensure_manifest_exists(&self) -> Result<(), ApplicationDownloadError> {
if self.manifest.lock().unwrap().is_some() {
return Ok(());
}
self.download_manifest().await
self.download_manifest()
}
async fn download_manifest(&self) -> Result<(), ApplicationDownloadError> {
let client = DROP_CLIENT_ASYNC.clone();
let url = generate_url(
fn download_manifest(&self) -> Result<(), ApplicationDownloadError> {
let header = generate_authorization_header();
let client = DROP_CLIENT_SYNC.clone();
let response = make_request(
&client,
&["/api/v1/client/game/manifest"],
&[("id", &self.id), ("version", &self.version)],
|f| f.header("Authorization", header),
)
.map_err(ApplicationDownloadError::Communication)?;
let response = client
.get(url)
.header("Authorization", generate_authorization_header())
.map_err(ApplicationDownloadError::Communication)?
.send()
.await
.map_err(|e| ApplicationDownloadError::Communication(e.into()))?;
if response.status() != 200 {
return Err(ApplicationDownloadError::Communication(
RemoteAccessError::ManifestDownloadFailed(
response.status(),
response.text().await.unwrap(),
response.text().unwrap(),
),
));
}
let manifest_download: DropManifest = response.json().await.unwrap();
let manifest_download: DropManifest = response.json().unwrap();
if let Ok(mut manifest) = self.manifest.lock() {
*manifest = Some(manifest_download);
@ -219,23 +201,20 @@ impl GameDownloadAgent {
// Sets it up for both download and validate
fn setup_progress(&self) {
let buckets = self.buckets.lock().unwrap();
let contexts = self.contexts.lock().unwrap();
let chunk_count = buckets.iter().map(|e| e.drops.len()).sum();
let length = contexts.len();
let total_length = buckets
.iter()
.map(|bucket| bucket.drops.iter().map(|e| e.length).sum::<usize>())
.sum();
let chunk_count = contexts.iter().map(|chunk| chunk.length).sum();
self.progress.set_max(total_length);
self.progress.set_size(chunk_count);
self.progress.set_max(chunk_count);
self.progress.set_size(length);
self.progress.reset();
}
pub fn ensure_buckets(&self) -> Result<(), ApplicationDownloadError> {
if self.buckets.lock().unwrap().is_empty() {
self.generate_buckets()?;
pub fn ensure_contexts(&self) -> Result<(), ApplicationDownloadError> {
if self.contexts.lock().unwrap().is_empty() {
self.generate_contexts()?;
}
*self.context_map.lock().unwrap() = self.dropdata.get_contexts();
@ -243,18 +222,14 @@ impl GameDownloadAgent {
Ok(())
}
pub fn generate_buckets(&self) -> Result<(), ApplicationDownloadError> {
pub fn generate_contexts(&self) -> Result<(), ApplicationDownloadError> {
let manifest = self.manifest.lock().unwrap().clone().unwrap();
let game_id = self.id.clone();
let mut contexts = Vec::new();
let base_path = Path::new(&self.dropdata.base_path);
create_dir_all(base_path).unwrap();
let mut buckets = Vec::new();
let mut current_buckets = HashMap::<String, DownloadBucket>::new();
let mut current_bucket_sizes = HashMap::<String, usize>::new();
for (raw_path, chunk) in manifest {
let path = base_path.join(Path::new(&raw_path));
@ -269,95 +244,42 @@ impl GameDownloadAgent {
.truncate(false)
.open(path.clone())
.unwrap();
let mut file_running_offset = 0;
let mut running_offset = 0;
for (index, length) in chunk.lengths.iter().enumerate() {
let drop = DownloadDrop {
filename: raw_path.to_string(),
start: file_running_offset,
length: *length,
checksum: chunk.checksums[index].clone(),
permissions: chunk.permissions,
path: path.clone(),
contexts.push(DropDownloadContext {
file_name: raw_path.to_string(),
version: chunk.version_name.to_string(),
offset: running_offset,
index,
};
file_running_offset += *length;
if *length >= TARGET_BUCKET_SIZE {
// They get their own bucket
buckets.push(DownloadBucket {
game_id: game_id.clone(),
version: chunk.version_name.clone(),
drops: vec![drop],
game_id: game_id.to_string(),
path: path.clone(),
checksum: chunk.checksums[index].clone(),
length: *length,
permissions: chunk.permissions,
});
continue;
}
let current_bucket_size = current_bucket_sizes
.entry(chunk.version_name.clone())
.or_insert_with(|| 0);
let c_version_name = chunk.version_name.clone();
let c_game_id = game_id.clone();
let current_bucket = current_buckets
.entry(chunk.version_name.clone())
.or_insert_with(|| DownloadBucket {
game_id: c_game_id,
version: c_version_name,
drops: vec![],
});
if (*current_bucket_size + length >= TARGET_BUCKET_SIZE
|| current_bucket.drops.len() >= MAX_FILES_PER_BUCKET)
&& !current_bucket.drops.is_empty()
{
// Move current bucket into list and make a new one
buckets.push(current_bucket.clone());
*current_bucket = DownloadBucket {
game_id: game_id.clone(),
version: chunk.version_name.clone(),
drops: vec![],
};
*current_bucket_size = 0;
}
current_bucket.drops.push(drop);
*current_bucket_size += *length;
running_offset += *length as u64;
}
#[cfg(target_os = "linux")]
if file_running_offset > 0 && !already_exists {
let _ = fallocate(file, FallocateFlags::empty(), 0, file_running_offset as u64);
if running_offset > 0 && !already_exists {
let _ = fallocate(file, FallocateFlags::empty(), 0, running_offset);
}
}
for (_, bucket) in current_buckets.into_iter() {
if !bucket.drops.is_empty() {
buckets.push(bucket);
}
}
info!("buckets: {}", buckets.len());
let existing_contexts = self.dropdata.get_contexts();
let existing_contexts = self.dropdata.get_completed_contexts();
self.dropdata.set_contexts(
&buckets
&contexts
.iter()
.flat_map(|x| x.drops.iter().map(|v| v.checksum.clone()))
.map(|x| {
let contains = existing_contexts.get(&x).unwrap_or(&false);
(x, *contains)
})
.map(|x| (x.checksum.clone(), existing_contexts.contains(&x.checksum)))
.collect::<Vec<(String, bool)>>(),
);
*self.buckets.lock().unwrap() = buckets;
*self.contexts.lock().unwrap() = contexts;
Ok(())
}
fn run(&self) -> Result<bool, RemoteAccessError> {
fn run(&self) -> Result<bool, ()> {
self.setup_progress();
let max_download_threads = borrow_db_checked().settings.max_download_threads;
@ -370,110 +292,81 @@ impl GameDownloadAgent {
.build()
.unwrap();
let buckets = self.buckets.lock().unwrap();
let mut download_contexts = HashMap::<String, DownloadContext>::new();
let versions = buckets
.iter()
.map(|e| &e.version)
.collect::<HashSet<_>>()
.into_iter()
.cloned()
.collect::<Vec<String>>();
info!("downloading across these versions: {versions:?}");
let completed_contexts = Arc::new(boxcar::Vec::new());
let completed_indexes_loop_arc = completed_contexts.clone();
for version in versions {
let download_context = DROP_CLIENT_SYNC
.post(generate_url(&["/api/v2/client/context"], &[]).unwrap())
.json(&ManifestBody {
game: self.id.clone(),
version: version.clone(),
})
.header("Authorization", generate_authorization_header())
.send()?;
if download_context.status() != 200 {
return Err(RemoteAccessError::InvalidResponse(download_context.json()?));
}
let download_context = download_context.json::<DownloadContext>()?;
info!(
"download context: ({}) {}",
&version, download_context.context
);
download_contexts.insert(version, download_context);
}
let download_contexts = &download_contexts;
let contexts = self.contexts.lock().unwrap();
pool.scope(|scope| {
let client = &DROP_CLIENT_SYNC.clone();
let context_map = self.context_map.lock().unwrap();
for (index, bucket) in buckets.iter().enumerate() {
let mut bucket = (*bucket).clone();
let completed_contexts = completed_indexes_loop_arc.clone();
for (index, context) in contexts.iter().enumerate() {
let client = client.clone();
let completed_indexes = completed_indexes_loop_arc.clone();
let progress = self.progress.get(index);
let progress_handle = ProgressHandle::new(progress, self.progress.clone());
// If we've done this one already, skip it
// Note to future DecDuck, DropData gets loaded into context_map
let todo_drops = bucket
.drops
.into_iter()
.filter(|e| {
let todo = !*context_map.get(&e.checksum).unwrap_or(&false);
if !todo {
progress_handle.skip(e.length);
}
todo
})
.collect::<Vec<DownloadDrop>>();
if todo_drops.is_empty() {
if let Some(v) = context_map.get(&context.checksum)
&& *v
{
progress_handle.skip(context.length);
continue;
};
bucket.drops = todo_drops;
}
let sender = self.sender.clone();
let download_context = download_contexts
.get(&bucket.version)
.ok_or(RemoteAccessError::CorruptedState)
let request = match make_request(
&client,
&["/api/v1/client/chunk"],
&[
("id", &context.game_id),
("version", &context.version),
("name", &context.file_name),
("chunk", &context.index.to_string()),
],
|r| r,
) {
Ok(request) => request,
Err(e) => {
sender
.send(DownloadManagerSignal::Error(
ApplicationDownloadError::Communication(e),
))
.unwrap();
continue;
}
};
scope.spawn(move |_| {
// 3 attempts
for i in 0..RETRY_COUNT {
let loop_progress_handle = progress_handle.clone();
match download_game_bucket(
&bucket,
download_context,
match download_game_chunk(
context,
&self.control_flag,
loop_progress_handle,
request.try_clone().unwrap(),
) {
Ok(true) => {
for drop in bucket.drops {
completed_contexts.push(drop.checksum);
}
completed_indexes.push(context.checksum.clone());
return;
}
Ok(false) => return,
Err(e) => {
warn!("game download agent error: {e}");
let retry = matches!(
&e,
ApplicationDownloadError::Communication(_)
| ApplicationDownloadError::Checksum
| ApplicationDownloadError::Lock
| ApplicationDownloadError::IoError(_)
);
let retry = match &e {
ApplicationDownloadError::Communication(
_remote_access_error,
) => true,
ApplicationDownloadError::Checksum => true,
ApplicationDownloadError::Lock => true,
ApplicationDownloadError::IoError(_error_kind) => false,
ApplicationDownloadError::DownloadError => false,
ApplicationDownloadError::DiskFull(_, _) => false,
};
if i == RETRY_COUNT - 1 || !retry {
warn!("retry logic failed, not re-attempting.");
@ -497,14 +390,14 @@ impl GameDownloadAgent {
context_map_lock.values().filter(|x| **x).count()
};
let context_map_lock = self.context_map.lock().unwrap();
let contexts = buckets
let contexts = contexts
.iter()
.flat_map(|x| x.drops.iter().map(|e| e.checksum.clone()))
.map(|x| {
let completed = context_map_lock.get(&x).unwrap_or(&false);
(x, *completed)
(
x.checksum.clone(),
context_map_lock.get(&x.checksum).copied().unwrap_or(false),
)
})
.collect::<Vec<(String, bool)>>();
drop(context_map_lock);
@ -515,11 +408,10 @@ impl GameDownloadAgent {
// If there are any contexts left which are false
if !contexts.iter().all(|x| x.1) {
info!(
"download agent for {} exited without completing ({}/{}) ({} buckets)",
"download agent for {} exited without completing ({}/{})",
self.id.clone(),
completed_lock_len,
contexts.len(),
buckets.len()
);
return Ok(false);
}
@ -532,30 +424,31 @@ impl GameDownloadAgent {
self.control_flag.set(DownloadThreadControlFlag::Go);
let status = ApplicationTransientStatus::Validating {
version_name: self.version.clone(),
};
let mut db_lock = borrow_db_mut_checked();
db_lock
.applications
.transient_statuses
.insert(self.metadata(), status.clone());
push_game_update(app_handle, &self.metadata().id, None, (None, Some(status)));
db_lock.applications.transient_statuses.insert(
self.metadata(),
ApplicationTransientStatus::Validating {
version_name: self.version.clone(),
},
);
push_game_update(
app_handle,
&self.metadata().id,
None,
GameStatusManager::fetch_state(&self.metadata().id, &db_lock),
);
}
pub fn validate(&self, app_handle: &AppHandle) -> Result<bool, ApplicationDownloadError> {
self.setup_validate(app_handle);
let buckets = self.buckets.lock().unwrap();
let contexts: Vec<DropValidateContext> = buckets
.clone()
.into_iter()
.flat_map(|e| -> Vec<DropValidateContext> { e.into() })
.collect();
let contexts = self.contexts.lock().unwrap();
let max_download_threads = borrow_db_checked().settings.max_download_threads;
info!("{} validation contexts", contexts.len());
debug!(
"validating game: {} with {} threads",
self.dropdata.game_id, max_download_threads
);
let pool = ThreadPoolBuilder::new()
.num_threads(max_download_threads)
.build()
@ -639,17 +532,8 @@ impl Downloadable for GameDownloadAgent {
}
}
fn on_queued(&self, app_handle: &tauri::AppHandle) {
fn on_initialised(&self, _app_handle: &tauri::AppHandle) {
*self.status.lock().unwrap() = DownloadStatus::Queued;
let mut db_lock = borrow_db_mut_checked();
let status = ApplicationTransientStatus::Queued {
version_name: self.version.clone(),
};
db_lock
.applications
.transient_statuses
.insert(self.metadata(), status.clone());
push_game_update(app_handle, &self.id, None, (None, Some(status)));
}
fn on_error(&self, app_handle: &tauri::AppHandle, error: &ApplicationDownloadError) {
@ -658,20 +542,13 @@ impl Downloadable for GameDownloadAgent {
.emit("download_error", error.to_string())
.unwrap();
error!("error while managing download: {error:?}");
error!("error while managing download: {error}");
let mut handle = borrow_db_mut_checked();
handle
.applications
.transient_statuses
.remove(&self.metadata());
push_game_update(
app_handle,
&self.id,
None,
GameStatusManager::fetch_state(&self.id, &handle),
);
}
fn on_complete(&self, app_handle: &tauri::AppHandle) {
@ -684,8 +561,15 @@ impl Downloadable for GameDownloadAgent {
}
fn on_cancelled(&self, app_handle: &tauri::AppHandle) {
info!("cancelled {}", self.id);
self.cancel(app_handle);
/*
on_game_incomplete(
&self.metadata(),
self.dropdata.base_path.to_string_lossy().to_string(),
app_handle,
)
.unwrap();
*/
}
fn status(&self) -> DownloadStatus {

View File

@ -5,19 +5,19 @@ use crate::download_manager::util::progress_object::ProgressHandle;
use crate::error::application_download_error::ApplicationDownloadError;
use crate::error::drop_server_error::DropServerError;
use crate::error::remote_access_error::RemoteAccessError;
use crate::games::downloads::manifest::{ChunkBody, DownloadBucket, DownloadContext, DownloadDrop};
use crate::games::downloads::manifest::DropDownloadContext;
use crate::remote::auth::generate_authorization_header;
use crate::remote::requests::generate_url;
use crate::remote::utils::DROP_CLIENT_SYNC;
use http::response;
use log::{debug, info, warn};
use md5::{Context, Digest};
use reqwest::blocking::Response;
use reqwest::blocking::{RequestBuilder, Response};
use std::fs::{Permissions, set_permissions};
use std::io::Read;
use std::ops::Sub;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::sync::Arc;
use std::thread;
use std::time::Instant;
use std::{
fs::{File, OpenOptions},
@ -25,30 +25,28 @@ use std::{
path::PathBuf,
};
static MAX_PACKET_LENGTH: usize = 4096 * 4;
static BUMP_SIZE: usize = 4096 * 16;
pub struct DropWriter<W: Write> {
hasher: Context,
destination: BufWriter<W>,
progress: ProgressHandle,
}
impl DropWriter<File> {
fn new(path: PathBuf, progress: ProgressHandle) -> Result<Self, io::Error> {
fn new(path: PathBuf, progress: ProgressHandle) -> Self {
let destination = OpenOptions::new()
.write(true)
.create(true)
.truncate(false)
.open(&path)?;
Ok(Self {
destination: BufWriter::with_capacity(1024 * 1024, destination),
.open(&path)
.unwrap();
Self {
destination: BufWriter::with_capacity(1 * 1024 * 1024, destination),
hasher: Context::new(),
progress,
})
}
}
fn finish(mut self) -> io::Result<Digest> {
self.flush()?;
self.flush().unwrap();
Ok(self.hasher.compute())
}
}
@ -78,97 +76,50 @@ impl Seek for DropWriter<File> {
pub struct DropDownloadPipeline<'a, R: Read, W: Write> {
pub source: R,
pub drops: Vec<DownloadDrop>,
pub destination: Vec<DropWriter<W>>,
pub destination: DropWriter<W>,
pub control_flag: &'a DownloadThreadControl,
#[allow(dead_code)]
progress: ProgressHandle,
pub size: usize,
}
impl<'a> Seek for DropDownloadPipeline<'a, Response, File> {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
self.destination.seek(pos)
}
}
impl<'a> DropDownloadPipeline<'a, Response, File> {
fn new(
source: Response,
drops: Vec<DownloadDrop>,
destination: PathBuf,
control_flag: &'a DownloadThreadControl,
progress: ProgressHandle,
) -> Result<Self, io::Error> {
Ok(Self {
size: usize,
) -> Self {
Self {
source,
destination: drops
.iter()
.map(|drop| DropWriter::new(drop.path.clone(), progress.clone()))
.try_collect()?,
drops,
destination: DropWriter::new(destination, progress),
control_flag,
progress,
})
size,
}
}
fn copy(&mut self) -> Result<bool, io::Error> {
let mut copy_buffer = [0u8; MAX_PACKET_LENGTH];
for (index, drop) in self.drops.iter().enumerate() {
let destination = self
.destination
.get_mut(index)
.ok_or(io::Error::other("no destination"))
.unwrap();
let mut remaining = drop.length;
if drop.start != 0 {
destination.seek(SeekFrom::Start(drop.start.try_into().unwrap()))?;
}
let mut last_bump = 0;
loop {
let size = MAX_PACKET_LENGTH.min(remaining);
let size = self.source.read(&mut copy_buffer[0..size]).inspect_err(|_| {
info!("got error from {}", drop.filename);
})?;
remaining -= size;
last_bump += size;
destination.write_all(&copy_buffer[0..size])?;
if last_bump > BUMP_SIZE {
last_bump -= BUMP_SIZE;
if self.control_flag.get() == DownloadThreadControlFlag::Stop {
return Ok(false);
}
}
if remaining == 0 {
break;
};
}
if self.control_flag.get() == DownloadThreadControlFlag::Stop {
return Ok(false);
}
}
io::copy(&mut self.source, &mut self.destination).unwrap();
Ok(true)
}
#[allow(dead_code)]
fn debug_skip_checksum(self) {
self.destination
.into_iter()
.for_each(|mut e| e.flush().unwrap());
}
fn finish(self) -> Result<Vec<Digest>, io::Error> {
let checksums = self
.destination
.into_iter()
.map(|e| e.finish())
.try_collect()?;
Ok(checksums)
fn finish(self) -> Result<Digest, io::Error> {
let checksum = self.destination.finish()?;
Ok(checksum)
}
}
pub fn download_game_bucket(
bucket: &DownloadBucket,
ctx: &DownloadContext,
pub fn download_game_chunk(
ctx: &DropDownloadContext,
control_flag: &DownloadThreadControl,
progress: ProgressHandle,
request: RequestBuilder,
) -> Result<bool, ApplicationDownloadError> {
// If we're paused
if control_flag.get() == DownloadThreadControlFlag::Stop {
@ -178,26 +129,21 @@ pub fn download_game_bucket(
let start = Instant::now();
debug!("started chunk {}", ctx.checksum);
let header = generate_authorization_header();
let header_time = start.elapsed();
let url = generate_url(&["/api/v2/client/chunk"], &[])
.map_err(ApplicationDownloadError::Communication)?;
let body = ChunkBody::create(ctx, &bucket.drops);
let response = DROP_CLIENT_SYNC
.post(url)
.json(&body)
let response = request
.header("Authorization", header)
.send()
.map_err(|e| ApplicationDownloadError::Communication(e.into()))?;
let response_time = start.elapsed();
if response.status() != 200 {
info!("chunk request got status code: {}", response.status());
let raw_res = response.text().map_err(|e| {
ApplicationDownloadError::Communication(RemoteAccessError::FetchError(e.into()))
})?;
info!("{raw_res}");
debug!("chunk request got status code: {}", response.status());
let raw_res = response.text().unwrap();
if let Ok(err) = serde_json::from_str::<DropServerError>(&raw_res) {
return Err(ApplicationDownloadError::Communication(
RemoteAccessError::InvalidResponse(err),
@ -208,67 +154,78 @@ pub fn download_game_bucket(
));
}
let lengths = response
.headers()
.get("Content-Lengths")
.ok_or(ApplicationDownloadError::Communication(
RemoteAccessError::UnparseableResponse("missing Content-Lengths header".to_owned()),
))?
.to_str()
.unwrap();
for (i, raw_length) in lengths.split(",").enumerate() {
let length = raw_length.parse::<usize>().unwrap_or(0);
let Some(drop) = bucket.drops.get(i) else {
warn!("invalid number of Content-Lengths recieved: {i}, {lengths}");
return Err(ApplicationDownloadError::DownloadError);
};
if drop.length != length {
warn!(
"for {}, expected {}, got {} ({})",
drop.filename, drop.length, raw_length, length
);
return Err(ApplicationDownloadError::DownloadError);
}
let content_length = response.content_length();
if content_length.is_none() {
warn!("recieved 0 length content from server");
return Err(ApplicationDownloadError::Communication(
RemoteAccessError::InvalidResponse(response.json().unwrap()),
));
}
let timestep = start.elapsed().as_millis();
let length = content_length.unwrap().try_into().unwrap();
debug!("took {}ms to start downloading", timestep);
if length != ctx.length {
return Err(ApplicationDownloadError::DownloadError);
}
let pipeline_start = start.elapsed();
let mut pipeline =
DropDownloadPipeline::new(response, bucket.drops.clone(), control_flag, progress)
.map_err(|e| ApplicationDownloadError::IoError(Arc::new(e)))?;
DropDownloadPipeline::new(response, ctx.path.clone(), control_flag, progress, length);
if ctx.offset != 0 {
pipeline
.seek(SeekFrom::Start(ctx.offset))
.expect("Failed to seek to file offset");
}
let pipeline_setup = start.elapsed();
let completed = pipeline
.copy()
.map_err(|e| ApplicationDownloadError::IoError(Arc::new(e)))?;
.map_err(|e| ApplicationDownloadError::IoError(e.kind()))?;
if !completed {
return Ok(false);
}
let pipeline_finish = start.elapsed();
// If we complete the file, set the permissions (if on Linux)
#[cfg(unix)]
{
for drop in bucket.drops.iter() {
let permissions = Permissions::from_mode(drop.permissions);
set_permissions(drop.path.clone(), permissions)
.map_err(|e| ApplicationDownloadError::IoError(Arc::new(e)))?;
}
let permissions = Permissions::from_mode(ctx.permissions);
set_permissions(ctx.path.clone(), permissions).unwrap();
}
let checksums = pipeline
let checksum = pipeline
.finish()
.map_err(|e| ApplicationDownloadError::IoError(Arc::new(e)))?;
.map_err(|e| ApplicationDownloadError::IoError(e.kind()))?;
for (index, drop) in bucket.drops.iter().enumerate() {
let res = hex::encode(**checksums.get(index).unwrap());
if res != drop.checksum {
warn!("context didn't match... doing nothing because we will validate later.");
// return Ok(false);
// return Err(ApplicationDownloadError::Checksum);
}
let checksum_finish = start.elapsed();
let res = hex::encode(checksum.0);
if res != ctx.checksum {
return Err(ApplicationDownloadError::Checksum);
}
let header_update = header_time.as_millis();
let response_update = response_time.sub(header_time).as_millis();
let pipeline_start_update = pipeline_start.sub(response_time).as_millis();
let pipeline_setup_update = pipeline_setup.sub(pipeline_start).as_millis();
let pipeline_finish_update = pipeline_finish.sub(pipeline_setup).as_millis();
let checksum_update = checksum_finish.sub(pipeline_finish).as_millis();
debug!(
"\nheader: {}\nresponse: {}\npipeline start: {}\npipeline setup: {}\npipeline finish: {}\nchecksum finish: {}",
header_update,
response_update,
pipeline_start_update,
pipeline_setup_update,
pipeline_finish_update,
checksum_update
);
debug!("finished chunk {}", ctx.checksum);
Ok(true)
}

View File

@ -76,6 +76,14 @@ impl DropData {
pub fn set_context(&self, context: String, state: bool) {
self.contexts.lock().unwrap().entry(context).insert_entry(state);
}
pub fn get_completed_contexts(&self) -> Vec<String> {
self.contexts
.lock()
.unwrap()
.iter()
.filter_map(|x| if *x.1 { Some(x.0.clone()) } else { None })
.collect()
}
pub fn get_contexts(&self) -> HashMap<String, bool> {
self.contexts.lock().unwrap().clone()
}

View File

@ -2,65 +2,6 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
#[derive(Debug, Clone, Serialize)]
// Drops go in buckets
pub struct DownloadDrop {
pub index: usize,
pub filename: String,
pub path: PathBuf,
pub start: usize,
pub length: usize,
pub checksum: String,
pub permissions: u32,
}
#[derive(Debug, Clone, Serialize)]
pub struct DownloadBucket {
pub game_id: String,
pub version: String,
pub drops: Vec<DownloadDrop>,
}
#[derive(Deserialize)]
pub struct DownloadContext {
pub context: String,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ChunkBodyFile {
filename: String,
chunk_index: usize,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ChunkBody {
pub context: String,
pub files: Vec<ChunkBodyFile>,
}
#[derive(Serialize)]
pub struct ManifestBody {
pub game: String,
pub version: String,
}
impl ChunkBody {
pub fn create(context: &DownloadContext, drops: &[DownloadDrop]) -> ChunkBody {
Self {
context: context.context.clone(),
files: drops
.iter()
.map(|e| ChunkBodyFile {
filename: e.filename.clone(),
chunk_index: e.index,
})
.collect(),
}
}
}
pub type DropManifest = HashMap<String, DropChunk>;
#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
@ -73,26 +14,14 @@ pub struct DropChunk {
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DropValidateContext {
pub struct DropDownloadContext {
pub file_name: String,
pub version: String,
pub index: usize,
pub offset: usize,
pub offset: u64,
pub game_id: String,
pub path: PathBuf,
pub checksum: String,
pub length: usize,
}
impl From<DownloadBucket> for Vec<DropValidateContext> {
fn from(value: DownloadBucket) -> Self {
value
.drops
.into_iter()
.map(|e| DropValidateContext {
index: e.index,
offset: e.start,
path: e.path,
checksum: e.checksum,
length: e.length,
})
.collect()
}
pub permissions: u32,
}

View File

@ -7,22 +7,24 @@ use log::debug;
use md5::Context;
use crate::{
download_manager::util::{
download_manager::
util::{
download_thread_control_flag::{DownloadThreadControl, DownloadThreadControlFlag},
progress_object::ProgressHandle,
},
}
,
error::application_download_error::ApplicationDownloadError,
games::downloads::manifest::DropValidateContext,
games::downloads::manifest::DropDownloadContext,
};
pub fn validate_game_chunk(
ctx: &DropValidateContext,
ctx: &DropDownloadContext,
control_flag: &DownloadThreadControl,
progress: ProgressHandle,
) -> Result<bool, ApplicationDownloadError> {
debug!(
"Starting chunk validation {}, {}, {} #{}",
ctx.path.display(), ctx.index, ctx.offset, ctx.checksum
ctx.file_name, ctx.index, ctx.offset, ctx.checksum
);
// If we're paused
if control_flag.get() == DownloadThreadControlFlag::Stop {
@ -36,7 +38,7 @@ pub fn validate_game_chunk(
if ctx.offset != 0 {
source
.seek(SeekFrom::Start(ctx.offset.try_into().unwrap()))
.seek(SeekFrom::Start(ctx.offset))
.expect("Failed to seek to file offset");
}

View File

@ -14,15 +14,13 @@ use crate::database::models::data::{
ApplicationTransientStatus, DownloadableMetadata, GameDownloadStatus, GameVersion,
};
use crate::download_manager::download_manager_frontend::DownloadStatus;
use crate::error::drop_server_error::DropServerError;
use crate::error::library_error::LibraryError;
use crate::error::remote_access_error::RemoteAccessError;
use crate::games::state::{GameStatusManager, GameStatusWithTransient};
use crate::remote::auth::generate_authorization_header;
use crate::remote::cache::cache_object_db;
use crate::remote::cache::{cache_object, get_cached_object, get_cached_object_db};
use crate::remote::requests::generate_url;
use crate::remote::utils::DROP_CLIENT_ASYNC;
use crate::remote::requests::make_request;
use crate::remote::utils::DROP_CLIENT_SYNC;
use bitcode::{Decode, Encode};
@ -78,33 +76,24 @@ pub struct StatsUpdateEvent {
pub time: usize,
}
pub async fn fetch_library_logic(
state: tauri::State<'_, Mutex<AppState<'_>>>,
hard_fresh: Option<bool>,
pub fn fetch_library_logic(
state: tauri::State<'_, Mutex<AppState>>,
) -> Result<Vec<Game>, RemoteAccessError> {
let do_hard_refresh = hard_fresh.unwrap_or(false);
if !do_hard_refresh && let Ok(library) = get_cached_object("library") {
return Ok(library);
}
let header = generate_authorization_header();
let client = DROP_CLIENT_ASYNC.clone();
let response = generate_url(&["/api/v1/client/user/library"], &[])?;
let response = client
.get(response)
.header("Authorization", generate_authorization_header())
.send()
.await?;
let client = DROP_CLIENT_SYNC.clone();
let response = make_request(&client, &["/api/v1/client/user/library"], &[], |f| {
f.header("Authorization", header)
})?
.send()?;
if response.status() != 200 {
let err = response.json().await.unwrap_or(DropServerError {
status_code: 500,
status_message: "Invalid response from server.".to_owned(),
});
let err = response.json().unwrap();
warn!("{err:?}");
return Err(RemoteAccessError::InvalidResponse(err));
}
let mut games: Vec<Game> = response.json().await?;
let mut games: Vec<Game> = response.json()?;
let mut handle = state.lock().unwrap();
@ -146,33 +135,27 @@ pub async fn fetch_library_logic(
Ok(games)
}
pub async fn fetch_library_logic_offline(
_state: tauri::State<'_, Mutex<AppState<'_>>>,
_hard_refresh: Option<bool>,
pub fn fetch_library_logic_offline(
_state: tauri::State<'_, Mutex<AppState>>,
) -> Result<Vec<Game>, RemoteAccessError> {
let mut games: Vec<Game> = get_cached_object("library")?;
let db_handle = borrow_db_checked();
games.retain(|game| {
matches!(
&db_handle
db_handle
.applications
.game_statuses
.get(&game.id)
.unwrap_or(&GameDownloadStatus::Remote {}),
GameDownloadStatus::Installed { .. } | GameDownloadStatus::SetupRequired { .. }
)
.installed_game_version
.contains_key(&game.id)
});
Ok(games)
}
pub async fn fetch_game_logic(
pub fn fetch_game_logic(
id: String,
state: tauri::State<'_, Mutex<AppState<'_>>>,
state: tauri::State<'_, Mutex<AppState>>,
) -> Result<FetchGameStruct, RemoteAccessError> {
let version = {
let state_handle = state.lock().unwrap();
let mut state_handle = state.lock().unwrap();
let db_lock = borrow_db_checked();
@ -201,35 +184,24 @@ pub async fn fetch_game_logic(
return Ok(data);
}
drop(db_lock);
version
};
let client = DROP_CLIENT_ASYNC.clone();
let response = generate_url(&["/api/v1/client/game/", &id], &[])?;
let response = client
.get(response)
.header("Authorization", generate_authorization_header())
.send()
.await?;
let client = DROP_CLIENT_SYNC.clone();
let response = make_request(&client, &["/api/v1/client/game/", &id], &[], |r| {
r.header("Authorization", generate_authorization_header())
})?
.send()?;
if response.status() == 404 {
let offline_fetch = fetch_game_logic_offline(id.clone(), state).await;
if let Ok(fetch_data) = offline_fetch {
return Ok(fetch_data);
}
return Err(RemoteAccessError::GameNotFound(id));
}
if response.status() != 200 {
let err = response.json().await.unwrap();
let err = response.json().unwrap();
warn!("{err:?}");
return Err(RemoteAccessError::InvalidResponse(err));
}
let game: Game = response.json().await?;
let mut state_handle = state.lock().unwrap();
let game: Game = response.json()?;
state_handle.games.insert(id.clone(), game.clone());
let mut db_handle = borrow_db_mut_checked();
@ -255,20 +227,24 @@ pub async fn fetch_game_logic(
Ok(data)
}
pub async fn fetch_game_logic_offline(
pub fn fetch_game_logic_offline(
id: String,
_state: tauri::State<'_, Mutex<AppState<'_>>>,
_state: tauri::State<'_, Mutex<AppState>>,
) -> Result<FetchGameStruct, RemoteAccessError> {
let db_handle = borrow_db_checked();
let metadata_option = db_handle.applications.installed_game_version.get(&id);
let version = match metadata_option {
None => None,
Some(metadata) => db_handle
Some(metadata) => Some(
db_handle
.applications
.game_versions
.get(&metadata.id)
.map(|v| v.get(metadata.version.as_ref().unwrap()).unwrap())
.cloned(),
.unwrap()
.get(metadata.version.as_ref().unwrap())
.unwrap()
.clone(),
),
};
let status = GameStatusManager::fetch_state(&id, &db_handle);
@ -283,26 +259,27 @@ pub async fn fetch_game_logic_offline(
})
}
pub async fn fetch_game_version_options_logic(
pub fn fetch_game_verion_options_logic(
game_id: String,
state: tauri::State<'_, Mutex<AppState<'_>>>,
state: tauri::State<'_, Mutex<AppState>>,
) -> Result<Vec<GameVersion>, RemoteAccessError> {
let client = DROP_CLIENT_ASYNC.clone();
let client = DROP_CLIENT_SYNC.clone();
let response = generate_url(&["/api/v1/client/game/versions"], &[("id", &game_id)])?;
let response = client
.get(response)
.header("Authorization", generate_authorization_header())
.send()
.await?;
let response = make_request(
&client,
&["/api/v1/client/game/versions"],
&[("id", &game_id)],
|r| r.header("Authorization", generate_authorization_header()),
)?
.send()?;
if response.status() != 200 {
let err = response.json().await.unwrap();
let err = response.json().unwrap();
warn!("{err:?}");
return Err(RemoteAccessError::InvalidResponse(err));
}
let data: Vec<GameVersion> = response.json().await?;
let data: Vec<GameVersion> = response.json()?;
let state_lock = state.lock().unwrap();
let process_manager_lock = state_lock.process_manager.lock().unwrap();
@ -369,7 +346,8 @@ pub fn uninstall_game_logic(meta: DownloadableMetadata, app_handle: &AppHandle)
db_handle
.applications
.transient_statuses
.insert(meta.clone(), ApplicationTransientStatus::Uninstalling {});
.entry(meta.clone())
.and_modify(|v| *v = ApplicationTransientStatus::Uninstalling {});
push_game_update(
app_handle,
@ -403,7 +381,8 @@ pub fn uninstall_game_logic(meta: DownloadableMetadata, app_handle: &AppHandle)
db_handle
.applications
.transient_statuses
.insert(meta.clone(), ApplicationTransientStatus::Uninstalling {});
.entry(meta.clone())
.and_modify(|v| *v = ApplicationTransientStatus::Uninstalling {});
drop(db_handle);
@ -421,7 +400,8 @@ pub fn uninstall_game_logic(meta: DownloadableMetadata, app_handle: &AppHandle)
db_handle
.applications
.game_statuses
.insert(meta.id.clone(), GameDownloadStatus::Remote {});
.entry(meta.id.clone())
.and_modify(|e| *e = GameDownloadStatus::Remote {});
let _ = db_handle.applications.transient_statuses.remove(&meta);
push_game_update(
@ -433,6 +413,8 @@ pub fn uninstall_game_logic(meta: DownloadableMetadata, app_handle: &AppHandle)
debug!("uninstalled game id {}", &meta.id);
app_handle.emit("update_library", ()).unwrap();
drop(db_handle);
}
});
} else {
@ -458,17 +440,18 @@ pub fn on_game_complete(
return Err(RemoteAccessError::GameNotFound(meta.id.clone()));
}
let header = generate_authorization_header();
let client = DROP_CLIENT_SYNC.clone();
let response = generate_url(
let response = make_request(
&client,
&["/api/v1/client/game/version"],
&[
("id", &meta.id),
("version", meta.version.as_ref().unwrap()),
],
)?;
let response = client
.get(response)
.header("Authorization", generate_authorization_header())
|f| f.header("Authorization", header),
)?
.send()?;
let game_version: GameVersion = response.json()?;
@ -505,7 +488,6 @@ pub fn on_game_complete(
.game_statuses
.insert(meta.id.clone(), status.clone());
drop(db_handle);
app_handle
.emit(
&format!("update_game/{}", meta.id),
@ -526,13 +508,6 @@ pub fn push_game_update(
version: Option<GameVersion>,
status: GameStatusWithTransient,
) {
if let Some(GameDownloadStatus::Installed { .. } | GameDownloadStatus::SetupRequired { .. }) =
&status.0
&& version.is_none()
{
panic!("pushed game for installed game that doesn't have version information");
}
app_handle
.emit(
&format!("update_game/{game_id}"),

View File

@ -1,6 +1,4 @@
use crate::database::models::data::{
ApplicationTransientStatus, Database, DownloadType, DownloadableMetadata, GameDownloadStatus,
};
use crate::database::models::data::{ApplicationTransientStatus, Database, GameDownloadStatus};
pub type GameStatusWithTransient = (
Option<GameDownloadStatus>,
@ -10,16 +8,10 @@ pub struct GameStatusManager {}
impl GameStatusManager {
pub fn fetch_state(game_id: &String, database: &Database) -> GameStatusWithTransient {
let online_state = database
.applications
.transient_statuses
.get(&DownloadableMetadata {
id: game_id.to_string(),
download_type: DownloadType::Game,
version: None,
})
.cloned();
let online_state = match database.applications.installed_game_version.get(game_id) {
Some(meta) => database.applications.transient_statuses.get(meta).cloned(),
None => None,
};
let offline_state = database.applications.game_statuses.get(game_id).cloned();
if online_state.is_some() {

View File

@ -1,8 +1,6 @@
#![deny(unused_must_use)]
#![feature(fn_traits)]
#![feature(duration_constructors)]
#![feature(duration_millis_float)]
#![feature(iterator_try_collect)]
#![deny(clippy::all)]
mod database;
@ -11,7 +9,6 @@ mod games;
mod client;
mod download_manager;
mod error;
mod playtime;
mod process;
mod remote;
@ -42,17 +39,11 @@ use games::collections::commands::{
fetch_collection, fetch_collections,
};
use games::commands::{
fetch_game, fetch_game_status, fetch_game_version_options, fetch_library, uninstall_game,
fetch_game, fetch_game_status, fetch_game_verion_options, fetch_library, uninstall_game,
};
use games::downloads::commands::download_game;
use games::library::{Game, update_game_configuration};
use log::{LevelFilter, debug, info, warn};
use playtime::manager::PlaytimeManager;
use playtime::commands::{
start_playtime_tracking, end_playtime_tracking, fetch_game_playtime,
fetch_all_playtime_stats, is_playtime_session_active, get_active_playtime_sessions,
cleanup_orphaned_playtime_sessions
};
use log4rs::Config;
use log4rs::append::console::ConsoleAppender;
use log4rs::append::file::FileAppender;
@ -72,6 +63,7 @@ use std::fs::File;
use std::io::Write;
use std::panic::PanicHookInfo;
use std::path::Path;
use std::process::{Command, Stdio};
use std::str::FromStr;
use std::sync::Arc;
use std::time::SystemTime;
@ -116,7 +108,13 @@ fn create_new_compat_info() -> Option<CompatInfo> {
#[cfg(target_os = "windows")]
return None;
let has_umu_installed = UMU_LAUNCHER_EXECUTABLE.is_some();
let has_umu_installed = Command::new(UMU_LAUNCHER_EXECUTABLE)
.stdout(Stdio::null())
.spawn();
if let Err(umu_error) = &has_umu_installed {
warn!("disabling windows support with error: {umu_error}");
}
let has_umu_installed = has_umu_installed.is_ok();
Some(CompatInfo {
umu_installed: has_umu_installed,
})
@ -134,14 +132,10 @@ pub struct AppState<'a> {
#[serde(skip_serializing)]
process_manager: Arc<Mutex<ProcessManager<'a>>>,
#[serde(skip_serializing)]
playtime_manager: Arc<Mutex<PlaytimeManager>>,
#[serde(skip_serializing)]
compat_info: Option<CompatInfo>,
#[serde(skip_serializing)]
app_handle: AppHandle,
}
async fn setup(handle: AppHandle) -> AppState<'static> {
fn setup(handle: AppHandle) -> AppState<'static> {
let logfile = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new(
"{d} | {l} | {f}:{L} - {m}{n}",
@ -175,7 +169,6 @@ async fn setup(handle: AppHandle) -> AppState<'static> {
let games = HashMap::new();
let download_manager = Arc::new(DownloadManagerBuilder::build(handle.clone()));
let process_manager = Arc::new(Mutex::new(ProcessManager::new(handle.clone())));
let playtime_manager = Arc::new(Mutex::new(PlaytimeManager::new(handle.clone())));
let compat_info = create_new_compat_info();
debug!("checking if database is set up");
@ -190,16 +183,14 @@ async fn setup(handle: AppHandle) -> AppState<'static> {
games,
download_manager,
process_manager,
playtime_manager,
compat_info,
app_handle: handle.clone(),
};
}
debug!("database is set up");
// TODO: Account for possible failure
let (app_status, user) = auth::setup().await;
let (app_status, user) = auth::setup();
let db_handle = borrow_db_checked();
let mut missing_games = Vec::new();
@ -251,20 +242,13 @@ async fn setup(handle: AppHandle) -> AppState<'static> {
warn!("failed to sync autostart state: {e}");
}
// Clean up any orphaned playtime sessions
if let Err(e) = playtime_manager.lock().unwrap().cleanup_orphaned_sessions() {
warn!("failed to cleanup orphaned playtime sessions: {e}");
}
AppState {
status: app_status,
user,
games,
download_manager,
process_manager,
playtime_manager,
compat_info,
app_handle: handle.clone(),
}
}
@ -333,7 +317,7 @@ pub fn run() {
delete_download_dir,
fetch_download_dir_stats,
fetch_game_status,
fetch_game_version_options,
fetch_game_verion_options,
update_game_configuration,
// Collections
fetch_collections,
@ -355,15 +339,7 @@ pub fn run() {
kill_game,
toggle_autostart,
get_autostart_enabled,
open_process_logs,
// Playtime tracking
start_playtime_tracking,
end_playtime_tracking,
fetch_game_playtime,
fetch_all_playtime_stats,
is_playtime_session_active,
get_active_playtime_sessions,
cleanup_orphaned_playtime_sessions
open_process_logs
])
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_dialog::init())
@ -373,10 +349,8 @@ pub fn run() {
))
.setup(|app| {
let handle = app.handle().clone();
tauri::async_runtime::block_on(async move {
let state = setup(handle).await;
info!("initialized drop client");
let state = setup(handle);
debug!("initialized drop client");
app.manage(Mutex::new(state));
{
@ -406,27 +380,23 @@ pub fn run() {
let binding = event.urls();
let url = binding.first().unwrap();
if url.host_str().unwrap() == "handshake" {
tauri::async_runtime::spawn(recieve_handshake(
handle.clone(),
url.path().to_string(),
));
recieve_handshake(handle.clone(), url.path().to_string());
}
});
let menu = Menu::with_items(
app,
&[
&MenuItem::with_id(app, "open", "Open", true, None::<&str>).unwrap(),
&PredefinedMenuItem::separator(app).unwrap(),
&MenuItem::with_id(app, "open", "Open", true, None::<&str>)?,
&PredefinedMenuItem::separator(app)?,
/*
&MenuItem::with_id(app, "show_library", "Library", true, None::<&str>)?,
&MenuItem::with_id(app, "show_settings", "Settings", true, None::<&str>)?,
&PredefinedMenuItem::separator(app)?,
*/
&MenuItem::with_id(app, "quit", "Quit", true, None::<&str>).unwrap(),
&MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?,
],
)
.unwrap();
)?;
run_on_tray(|| {
TrayIconBuilder::new()
@ -464,7 +434,6 @@ pub fn run() {
.show(|_| {});
}
}
});
Ok(())
})
@ -473,21 +442,15 @@ pub fn run() {
fetch_object(request, responder).await;
});
})
.register_asynchronous_uri_scheme_protocol("server", |ctx, request, responder| {
tauri::async_runtime::block_on(async move {
let state = ctx
.app_handle()
.state::<tauri::State<'_, Mutex<AppState>>>();
.register_asynchronous_uri_scheme_protocol("server", move |ctx, request, responder| {
let state: tauri::State<'_, Mutex<AppState>> = ctx.app_handle().state();
offline!(
state,
handle_server_proto,
handle_server_proto_offline,
request,
responder
)
.await;
});
);
})
.on_window_event(|window, event| {
if let WindowEvent::CloseRequested { api, .. } = event {

View File

@ -1,95 +0,0 @@
use std::collections::HashMap;
use tauri::State;
use std::sync::Mutex;
use crate::AppState;
use super::manager::PlaytimeStats;
use super::events::{push_playtime_update, push_session_start, push_session_end};
#[tauri::command]
pub fn start_playtime_tracking(
game_id: String,
state: State<'_, Mutex<AppState<'_>>>,
) -> Result<(), String> {
let state_lock = state.lock().unwrap();
let playtime_manager_lock = state_lock.playtime_manager.lock().unwrap();
match playtime_manager_lock.start_session(game_id.clone()) {
Ok(()) => {
push_session_start(&state_lock.app_handle, &game_id);
Ok(())
}
Err(e) => Err(e.to_string())
}
}
#[tauri::command]
pub fn end_playtime_tracking(
game_id: String,
state: State<'_, Mutex<AppState<'_>>>,
) -> Result<PlaytimeStats, String> {
let state_lock = state.lock().unwrap();
let playtime_manager_lock = state_lock.playtime_manager.lock().unwrap();
match playtime_manager_lock.end_session(game_id.clone()) {
Ok(stats) => {
push_session_end(&state_lock.app_handle, &game_id, &stats);
push_playtime_update(&state_lock.app_handle, &game_id, stats.clone(), false);
Ok(stats)
}
Err(e) => Err(e.to_string())
}
}
#[tauri::command]
pub fn fetch_game_playtime(
game_id: String,
state: State<'_, Mutex<AppState<'_>>>,
) -> Result<Option<PlaytimeStats>, String> {
let state_lock = state.lock().unwrap();
let playtime_manager_lock = state_lock.playtime_manager.lock().unwrap();
Ok(playtime_manager_lock.get_game_stats(&game_id))
}
#[tauri::command]
pub fn fetch_all_playtime_stats(
state: State<'_, Mutex<AppState<'_>>>,
) -> Result<HashMap<String, PlaytimeStats>, String> {
let state_lock = state.lock().unwrap();
let playtime_manager_lock = state_lock.playtime_manager.lock().unwrap();
Ok(playtime_manager_lock.get_all_stats())
}
#[tauri::command]
pub fn is_playtime_session_active(
game_id: String,
state: State<'_, Mutex<AppState<'_>>>,
) -> Result<bool, String> {
let state_lock = state.lock().unwrap();
let playtime_manager_lock = state_lock.playtime_manager.lock().unwrap();
Ok(playtime_manager_lock.is_session_active(&game_id))
}
#[tauri::command]
pub fn get_active_playtime_sessions(
state: State<'_, Mutex<AppState<'_>>>,
) -> Result<Vec<String>, String> {
let state_lock = state.lock().unwrap();
let playtime_manager_lock = state_lock.playtime_manager.lock().unwrap();
Ok(playtime_manager_lock.get_active_sessions())
}
#[tauri::command]
pub fn cleanup_orphaned_playtime_sessions(
state: State<'_, Mutex<AppState<'_>>>,
) -> Result<(), String> {
let state_lock = state.lock().unwrap();
let playtime_manager_lock = state_lock.playtime_manager.lock().unwrap();
playtime_manager_lock.cleanup_orphaned_sessions()
.map_err(|e| e.to_string())
}

View File

@ -1,81 +0,0 @@
use serde::Serialize;
use tauri::{AppHandle, Emitter};
use log::warn;
use super::manager::PlaytimeStats;
#[derive(Serialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PlaytimeUpdateEvent {
pub game_id: String,
pub stats: PlaytimeStats,
pub is_active: bool,
}
#[derive(Serialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PlaytimeSessionStartEvent {
pub game_id: String,
pub start_time: std::time::SystemTime,
}
#[derive(Serialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PlaytimeSessionEndEvent {
pub game_id: String,
pub session_duration_seconds: u64,
pub total_playtime_seconds: u64,
pub session_count: u32,
}
/// Push a playtime update event to the frontend
pub fn push_playtime_update(app_handle: &AppHandle, game_id: &str, stats: PlaytimeStats, is_active: bool) {
let event = PlaytimeUpdateEvent {
game_id: game_id.to_string(),
stats,
is_active,
};
if let Err(e) = app_handle.emit(&format!("playtime_update/{}", game_id), &event) {
warn!("Failed to emit playtime update event for {}: {}", game_id, e);
}
// Also emit a general playtime update event for global listeners
if let Err(e) = app_handle.emit("playtime_update", &event) {
warn!("Failed to emit general playtime update event: {}", e);
}
}
/// Push a session start event to the frontend
pub fn push_session_start(app_handle: &AppHandle, game_id: &str) {
let event = PlaytimeSessionStartEvent {
game_id: game_id.to_string(),
start_time: std::time::SystemTime::now(),
};
if let Err(e) = app_handle.emit(&format!("playtime_session_start/{}", game_id), &event) {
warn!("Failed to emit session start event for {}: {}", game_id, e);
}
if let Err(e) = app_handle.emit("playtime_session_start", &event) {
warn!("Failed to emit general session start event: {}", e);
}
}
/// Push a session end event to the frontend
pub fn push_session_end(app_handle: &AppHandle, game_id: &str, stats: &PlaytimeStats) {
let event = PlaytimeSessionEndEvent {
game_id: game_id.to_string(),
session_duration_seconds: stats.current_session_duration.unwrap_or(0),
total_playtime_seconds: stats.total_playtime_seconds,
session_count: stats.session_count,
};
if let Err(e) = app_handle.emit(&format!("playtime_session_end/{}", game_id), &event) {
warn!("Failed to emit session end event for {}: {}", game_id, e);
}
if let Err(e) = app_handle.emit("playtime_session_end", &event) {
warn!("Failed to emit general session end event: {}", e);
}
}

View File

@ -1,255 +0,0 @@
use std::collections::HashMap;
use std::time::SystemTime;
use std::fmt;
use log::{debug, warn};
use serde::{Deserialize, Serialize};
use tauri::AppHandle;
use crate::database::db::{borrow_db_checked, borrow_db_mut_checked};
use crate::database::models::data::{GamePlaytimeStats, PlaytimeSession};
use crate::error::process_error::ProcessError;
#[derive(Debug)]
pub enum PlaytimeError {
DatabaseError(String),
SessionNotFound(String),
SessionAlreadyActive(String),
InvalidGameId(String),
}
impl fmt::Display for PlaytimeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PlaytimeError::DatabaseError(msg) => write!(f, "Database error: {}", msg),
PlaytimeError::SessionNotFound(game_id) => write!(f, "Session not found for game: {}", game_id),
PlaytimeError::SessionAlreadyActive(game_id) => write!(f, "Session already active for game: {}", game_id),
PlaytimeError::InvalidGameId(game_id) => write!(f, "Invalid game ID: {}", game_id),
}
}
}
impl std::error::Error for PlaytimeError {}
impl From<PlaytimeError> for ProcessError {
fn from(error: PlaytimeError) -> Self {
ProcessError::PlaytimeError(error.to_string())
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PlaytimeStats {
pub game_id: String,
pub total_playtime_seconds: u64,
pub session_count: u32,
pub first_played: SystemTime,
pub last_played: SystemTime,
pub average_session_length: u64,
pub current_session_duration: Option<u64>,
}
impl From<GamePlaytimeStats> for PlaytimeStats {
fn from(stats: GamePlaytimeStats) -> Self {
let average_length = stats.average_session_length();
Self {
game_id: stats.game_id,
total_playtime_seconds: stats.total_playtime_seconds,
session_count: stats.session_count,
first_played: stats.first_played,
last_played: stats.last_played,
average_session_length: average_length,
current_session_duration: None,
}
}
}
pub struct PlaytimeManager {
app_handle: AppHandle,
}
impl PlaytimeManager {
pub fn new(app_handle: AppHandle) -> Self {
Self { app_handle }
}
/// Start tracking playtime for a game
pub fn start_session(&self, game_id: String) -> Result<(), PlaytimeError> {
debug!("Starting playtime session for game: {}", game_id);
let mut db_handle = borrow_db_mut_checked();
// Check if session is already active
if db_handle.playtime_data.active_sessions.contains_key(&game_id) {
warn!("Session already active for game: {}", game_id);
return Err(PlaytimeError::SessionAlreadyActive(game_id));
}
// Create new session
let session = PlaytimeSession::new(game_id.clone());
db_handle.playtime_data.active_sessions.insert(game_id.clone(), session);
debug!("Started playtime tracking for game: {}", game_id);
Ok(())
}
/// End tracking playtime for a game and update stats
pub fn end_session(&self, game_id: String) -> Result<PlaytimeStats, PlaytimeError> {
debug!("Ending playtime session for game: {}", game_id);
let mut db_handle = borrow_db_mut_checked();
// Get active session
let session = db_handle.playtime_data.active_sessions.remove(&game_id)
.ok_or_else(|| PlaytimeError::SessionNotFound(game_id.clone()))?;
let session_duration = session.duration().as_secs();
debug!("Session duration for {}: {} seconds", game_id, session_duration);
// Update or create game stats
let stats = db_handle.playtime_data.game_sessions
.entry(game_id.clone())
.or_insert_with(|| GamePlaytimeStats::new(game_id.clone()));
// Update stats
stats.total_playtime_seconds += session_duration;
stats.session_count += 1;
stats.last_played = SystemTime::now();
// If this is the first session, update first_played
if stats.session_count == 1 {
stats.first_played = session.start_time;
}
let result_stats = PlaytimeStats {
game_id: stats.game_id.clone(),
total_playtime_seconds: stats.total_playtime_seconds,
session_count: stats.session_count,
first_played: stats.first_played,
last_played: stats.last_played,
average_session_length: stats.average_session_length(),
current_session_duration: Some(session_duration),
};
debug!("Updated playtime stats for {}: {} total seconds, {} sessions",
game_id, stats.total_playtime_seconds, stats.session_count);
Ok(result_stats)
}
/// Get playtime stats for a specific game
pub fn get_game_stats(&self, game_id: &str) -> Option<PlaytimeStats> {
let db_handle = borrow_db_checked();
if let Some(stats) = db_handle.playtime_data.game_sessions.get(game_id) {
let mut playtime_stats: PlaytimeStats = stats.clone().into();
// If there's an active session, include current session duration
if let Some(session) = db_handle.playtime_data.active_sessions.get(game_id) {
playtime_stats.current_session_duration = Some(session.duration().as_secs());
}
Some(playtime_stats)
} else {
None
}
}
/// Get playtime stats for all games
pub fn get_all_stats(&self) -> HashMap<String, PlaytimeStats> {
let db_handle = borrow_db_checked();
let mut result = HashMap::new();
for (game_id, stats) in &db_handle.playtime_data.game_sessions {
let mut playtime_stats: PlaytimeStats = stats.clone().into();
// If there's an active session, include current session duration
if let Some(session) = db_handle.playtime_data.active_sessions.get(game_id) {
playtime_stats.current_session_duration = Some(session.duration().as_secs());
}
result.insert(game_id.clone(), playtime_stats);
}
result
}
/// Check if a game has an active session
pub fn is_session_active(&self, game_id: &str) -> bool {
let db_handle = borrow_db_checked();
db_handle.playtime_data.active_sessions.contains_key(game_id)
}
/// Get active sessions (for debugging/monitoring)
pub fn get_active_sessions(&self) -> Vec<String> {
let db_handle = borrow_db_checked();
db_handle.playtime_data.active_sessions.keys().cloned().collect()
}
/// Clean up any orphaned sessions (called on startup)
pub fn cleanup_orphaned_sessions(&self) -> Result<(), PlaytimeError> {
debug!("Cleaning up orphaned playtime sessions");
let mut db_handle = borrow_db_mut_checked();
let orphaned_sessions: Vec<String> = db_handle.playtime_data.active_sessions.keys().cloned().collect();
for game_id in orphaned_sessions {
warn!("Found orphaned session for game: {}, ending it", game_id);
if let Some(session) = db_handle.playtime_data.active_sessions.remove(&game_id) {
let session_duration = session.duration().as_secs();
// Only count sessions that lasted more than 5 seconds to avoid counting crashes
if session_duration > 5 {
let stats = db_handle.playtime_data.game_sessions
.entry(game_id.clone())
.or_insert_with(|| GamePlaytimeStats::new(game_id.clone()));
stats.total_playtime_seconds += session_duration;
stats.session_count += 1;
stats.last_played = SystemTime::now();
if stats.session_count == 1 {
stats.first_played = session.start_time;
}
debug!("Recovered orphaned session for {}: {} seconds", game_id, session_duration);
} else {
debug!("Discarded short orphaned session for {}: {} seconds", game_id, session_duration);
}
}
}
Ok(())
}
// Future server-side methods (ready for migration)
/// Start session with server sync (placeholder for future implementation)
#[allow(dead_code)]
pub async fn sync_session_start(&self, game_id: String) -> Result<(), PlaytimeError> {
// For now, just call local method
self.start_session(game_id)?;
// Future: Send to server
// let response = self.api_client.post("/api/v1/playtime/start")
// .json(&StartSessionRequest { game_id })
// .send().await?;
Ok(())
}
/// End session with server sync (placeholder for future implementation)
#[allow(dead_code)]
pub async fn sync_session_end(&self, game_id: String) -> Result<PlaytimeStats, PlaytimeError> {
// For now, just call local method
let stats = self.end_session(game_id)?;
// Future: Send to server
// let response = self.api_client.post("/api/v1/playtime/end")
// .json(&EndSessionRequest { game_id, duration: stats.current_session_duration })
// .send().await?;
Ok(stats)
}
}

View File

@ -1,3 +0,0 @@
pub mod commands;
pub mod events;
pub mod manager;

View File

@ -16,28 +16,14 @@ pub fn launch_game(
// download_type: DownloadType::Game,
//};
match process_manager_lock.launch_process(id.clone(), &state_lock) {
Ok(()) => {
// Start playtime tracking after successful launch
drop(process_manager_lock);
let playtime_manager_lock = state_lock.playtime_manager.lock().unwrap();
if let Err(e) = playtime_manager_lock.start_session(id.clone()) {
log::warn!("Failed to start playtime tracking for {}: {}", id, e);
} else {
log::debug!("Started playtime tracking for game: {}", id);
crate::playtime::events::push_session_start(&state_lock.app_handle, &id);
}
drop(playtime_manager_lock);
}
Err(e) => {
drop(process_manager_lock);
drop(state_lock);
return Err(e);
}
match process_manager_lock.launch_process(id, &state_lock) {
Ok(()) => {}
Err(e) => return Err(e),
}
drop(process_manager_lock);
drop(state_lock);
Ok(())
}
@ -47,18 +33,6 @@ pub fn kill_game(
state: tauri::State<'_, Mutex<AppState>>,
) -> Result<(), ProcessError> {
let state_lock = state.lock().unwrap();
let mut process_manager_lock = state_lock.process_manager.lock().unwrap();
// End playtime tracking before killing the game
drop(process_manager_lock);
let playtime_manager_lock = state_lock.playtime_manager.lock().unwrap();
if let Ok(stats) = playtime_manager_lock.end_session(game_id.clone()) {
log::debug!("Ended playtime tracking for game: {} (manual kill)", game_id);
crate::playtime::events::push_session_end(&state_lock.app_handle, &game_id, &stats);
crate::playtime::events::push_playtime_update(&state_lock.app_handle, &game_id, stats, false);
}
drop(playtime_manager_lock);
let mut process_manager_lock = state_lock.process_manager.lock().unwrap();
process_manager_lock
.kill_game(game_id)

View File

@ -1,11 +1,4 @@
use std::{
ffi::OsStr,
path::PathBuf,
process::{Command, Stdio},
sync::LazyLock,
};
use log::{debug, info};
use log::debug;
use crate::{
AppState,
@ -31,31 +24,7 @@ impl ProcessHandler for NativeGameLauncher {
}
}
pub static UMU_LAUNCHER_EXECUTABLE: LazyLock<Option<PathBuf>> = LazyLock::new(|| {
let x = get_umu_executable();
info!("{:?}", &x);
x
});
const UMU_BASE_LAUNCHER_EXECUTABLE: &str = "umu-run";
const UMU_INSTALL_DIRS: [&str; 4] = ["/app/share", "/use/local/share", "/usr/share", "/opt"];
fn get_umu_executable() -> Option<PathBuf> {
if check_executable_exists(UMU_BASE_LAUNCHER_EXECUTABLE) {
return Some(PathBuf::from(UMU_BASE_LAUNCHER_EXECUTABLE));
}
for dir in UMU_INSTALL_DIRS {
let p = PathBuf::from(dir).join(UMU_BASE_LAUNCHER_EXECUTABLE);
if check_executable_exists(&p) {
return Some(p);
}
}
None
}
fn check_executable_exists<P: AsRef<OsStr>>(exec: P) -> bool {
let has_umu_installed = Command::new(exec).stdout(Stdio::null()).output();
has_umu_installed.is_ok()
}
pub const UMU_LAUNCHER_EXECUTABLE: &str = "umu-run";
pub struct UMULauncher;
impl ProcessHandler for UMULauncher {
fn create_launch_process(
@ -78,8 +47,8 @@ impl ProcessHandler for UMULauncher {
None => game_version.game_id.clone(),
};
format!(
"GAMEID={game_id} {umu:?} \"{launch}\" {args}",
umu = UMU_LAUNCHER_EXECUTABLE.as_ref().unwrap(),
"GAMEID={game_id} {umu} \"{launch}\" {args}",
umu = UMU_LAUNCHER_EXECUTABLE,
launch = launch_command,
args = args.join(" ")
)
@ -111,10 +80,7 @@ impl ProcessHandler for AsahiMuvmLauncher {
game_version,
current_dir,
);
let mut args_cmd = umu_string
.split("umu-run")
.collect::<Vec<&str>>()
.into_iter();
let mut args_cmd = umu_string.split("umu-run").collect::<Vec<&str>>().into_iter();
let args = args_cmd.next().unwrap().trim();
let cmd = format!("umu-run{}", args_cmd.next().unwrap());

View File

@ -29,7 +29,6 @@ use crate::{
},
error::process_error::ProcessError,
games::{library::push_game_update, state::GameStatusManager},
playtime::events::{push_session_end, push_playtime_update},
process::{
format::DropFormatArgs,
process_handlers::{AsahiMuvmLauncher, NativeGameLauncher, UMULauncher},
@ -173,23 +172,10 @@ impl ProcessManager<'_> {
let _ = self.app_handle.emit("launch_external_error", &game_id);
}
// This is too many unwraps for me to be comfortable
let version_data = db_handle
.applications
.game_versions
.get(&game_id)
.unwrap()
.get(&meta.version.unwrap())
.unwrap();
let status = GameStatusManager::fetch_state(&game_id, &db_handle);
drop(db_handle);
push_game_update(
&self.app_handle,
&game_id,
Some(version_data.clone()),
status,
);
push_game_update(&self.app_handle, &game_id, None, status);
}
fn fetch_process_handler(
@ -348,10 +334,11 @@ impl ProcessManager<'_> {
#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;
#[cfg(target_os = "windows")]
let mut command = Command::new("cmd");
let mut command = Command::new("start");
#[cfg(target_os = "windows")]
command.raw_arg(format!("/C \"{}\"", &launch_string));
command.raw_arg(format!("/min cmd /C \"{}\"", &launch_string));
info!("launching (in {install_dir}): {launch_string}",);
@ -395,15 +382,6 @@ impl ProcessManager<'_> {
let app_state = wait_thread_apphandle.state::<Mutex<AppState>>();
let app_state_handle = app_state.lock().unwrap();
// End playtime tracking before processing finish
let playtime_manager_lock = app_state_handle.playtime_manager.lock().unwrap();
if let Ok(stats) = playtime_manager_lock.end_session(wait_thread_game_id.id.clone()) {
debug!("Ended playtime tracking for game: {} (process finished)", wait_thread_game_id.id);
push_session_end(&app_state_handle.app_handle, &wait_thread_game_id.id, &stats);
push_playtime_update(&app_state_handle.app_handle, &wait_thread_game_id.id, stats, false);
}
drop(playtime_manager_lock);
let mut process_manager_handle = app_state_handle.process_manager.lock().unwrap();
process_manager_handle.on_process_finish(wait_thread_game_id.id, result);

View File

@ -1,4 +1,4 @@
use std::{path::PathBuf, sync::Arc};
use std::path::PathBuf;
use futures_lite::io;
use sysinfo::{Disk, DiskRefreshKind, Disks};
@ -21,7 +21,7 @@ pub fn get_disk_available(mount_point: PathBuf) -> Result<u64, ApplicationDownlo
return Ok(disk.available_space());
}
}
Err(ApplicationDownloadError::IoError(Arc::new(io::Error::other(
Err(ApplicationDownloadError::IoError(io::Error::other(
"could not find disk of path",
))))
).kind()))
}

View File

@ -12,12 +12,12 @@ use crate::{
database::{
db::{borrow_db_checked, borrow_db_mut_checked},
models::data::DatabaseAuth,
}, error::{drop_server_error::DropServerError, remote_access_error::RemoteAccessError}, remote::{cache::clear_cached_object, requests::make_authenticated_get, utils::{DROP_CLIENT_ASYNC, DROP_CLIENT_SYNC}}, AppState, AppStatus, User
}, error::{drop_server_error::DropServerError, remote_access_error::RemoteAccessError}, remote::utils::DROP_CLIENT_SYNC, AppState, AppStatus, User
};
use super::{
cache::{cache_object, get_cached_object},
requests::generate_url,
requests::make_request,
};
#[derive(Serialize)]
@ -61,10 +61,16 @@ pub fn generate_authorization_header() -> String {
format!("Nonce {} {} {}", certs.client_id, nonce, signature)
}
pub async fn fetch_user() -> Result<User, RemoteAccessError> {
let response = make_authenticated_get(generate_url(&["/api/v1/client/user"], &[])?).await?;
pub fn fetch_user() -> Result<User, RemoteAccessError> {
let header = generate_authorization_header();
let client = DROP_CLIENT_SYNC.clone();
let response = make_request(&client, &["/api/v1/client/user"], &[], |f| {
f.header("Authorization", header)
})?
.send()?;
if response.status() != 200 {
let err: DropServerError = response.json().await?;
let err: DropServerError = response.json()?;
warn!("{err:?}");
if err.status_message == "Nonce expired" {
@ -74,13 +80,10 @@ pub async fn fetch_user() -> Result<User, RemoteAccessError> {
return Err(RemoteAccessError::InvalidResponse(err));
}
response
.json::<User>()
.await
.map_err(std::convert::Into::into)
response.json::<User>().map_err(std::convert::Into::into)
}
async fn recieve_handshake_logic(app: &AppHandle, path: String) -> Result<(), RemoteAccessError> {
fn recieve_handshake_logic(app: &AppHandle, path: String) -> Result<(), RemoteAccessError> {
let path_chunks: Vec<&str> = path.split('/').collect();
if path_chunks.len() != 3 {
app.emit("auth/failed", ()).unwrap();
@ -102,13 +105,13 @@ async fn recieve_handshake_logic(app: &AppHandle, path: String) -> Result<(), Re
};
let endpoint = base_url.join("/api/v1/client/auth/handshake")?;
let client = DROP_CLIENT_ASYNC.clone();
let response = client.post(endpoint).json(&body).send().await?;
let client = DROP_CLIENT_SYNC.clone();
let response = client.post(endpoint).json(&body).send()?;
debug!("handshake responsded with {}", response.status().as_u16());
if !response.status().is_success() {
return Err(RemoteAccessError::InvalidResponse(response.json().await?));
return Err(RemoteAccessError::InvalidResponse(response.json()?));
}
let response_struct: HandshakeResponse = response.json().await?;
let response_struct: HandshakeResponse = response.json()?;
{
let mut handle = borrow_db_mut_checked();
@ -126,10 +129,9 @@ async fn recieve_handshake_logic(app: &AppHandle, path: String) -> Result<(), Re
.post(base_url.join("/api/v1/client/user/webtoken").unwrap())
.header("Authorization", header)
.send()
.await
.unwrap();
token.text().await.unwrap()
token.text().unwrap()
};
let mut handle = borrow_db_mut_checked();
@ -139,11 +141,11 @@ async fn recieve_handshake_logic(app: &AppHandle, path: String) -> Result<(), Re
Ok(())
}
pub async fn recieve_handshake(app: AppHandle, path: String) {
pub fn recieve_handshake(app: AppHandle, path: String) {
// Tell the app we're processing
app.emit("auth/processing", ()).unwrap();
let handshake_result = recieve_handshake_logic(&app, path).await;
let handshake_result = recieve_handshake_logic(&app, path);
if let Err(e) = handshake_result {
warn!("error with authentication: {e}");
app.emit("auth/failed", e.to_string()).unwrap();
@ -151,17 +153,13 @@ pub async fn recieve_handshake(app: AppHandle, path: String) {
}
let app_state = app.state::<Mutex<AppState>>();
let (app_status, user) = setup().await;
let mut state_lock = app_state.lock().unwrap();
let (app_status, user) = setup();
state_lock.status = app_status;
state_lock.user = user;
let _ = clear_cached_object("collections");
let _ = clear_cached_object("library");
drop(state_lock);
app.emit("auth/finished", ()).unwrap();
@ -201,14 +199,13 @@ pub fn auth_initiate_logic(mode: String) -> Result<String, RemoteAccessError> {
Ok(response)
}
pub async fn setup() -> (AppStatus, Option<User>) {
let auth = {
pub fn setup() -> (AppStatus, Option<User>) {
let data = borrow_db_checked();
data.auth.clone()
};
let auth = data.auth.clone();
drop(data);
if auth.is_some() {
let user_result = match fetch_user().await {
let user_result = match fetch_user() {
Ok(data) => data,
Err(RemoteAccessError::FetchError(_)) => {
let user = get_cached_object::<User>("user").unwrap();

View File

@ -16,11 +16,10 @@ use http::{Response, header::CONTENT_TYPE, response::Builder as ResponseBuilder}
macro_rules! offline {
($var:expr, $func1:expr, $func2:expr, $( $arg:expr ),* ) => {
async move { if $crate::borrow_db_checked().settings.force_offline || $var.lock().unwrap().status == $crate::AppStatus::Offline {
$func2( $( $arg ), *).await
if $crate::borrow_db_checked().settings.force_offline || $var.lock().unwrap().status == $crate::AppStatus::Offline {
$func2( $( $arg ), *)
} else {
$func1( $( $arg ), *).await
}
$func1( $( $arg ), *)
}
}
}
@ -50,12 +49,6 @@ fn read_sync(base: &Path, key: &str) -> io::Result<Vec<u8>> {
Ok(file)
}
fn delete_sync(base: &Path, key: &str) -> io::Result<()> {
let cache_path = get_cache_path(base, key);
std::fs::remove_file(cache_path)?;
Ok(())
}
pub fn cache_object<D: Encode>(key: &str, data: &D) -> Result<(), RemoteAccessError> {
cache_object_db(key, data, &borrow_db_checked())
}
@ -79,17 +72,6 @@ pub fn get_cached_object_db<D: DecodeOwned>(
bitcode::decode::<D>(&bytes).map_err(|e| RemoteAccessError::Cache(io::Error::other(e)))?;
Ok(data)
}
pub fn clear_cached_object(key: &str) -> Result<(), RemoteAccessError> {
clear_cached_object_db(key, &borrow_db_checked())
}
pub fn clear_cached_object_db(
key: &str,
db: &Database,
) -> Result<(), RemoteAccessError> {
delete_sync(&db.cache_dir, key).map_err(RemoteAccessError::Cache)?;
Ok(())
}
#[derive(Encode, Decode)]
pub struct ObjectCache {
content_type: String,

View File

@ -8,14 +8,11 @@ use tauri::{AppHandle, Emitter, Manager};
use url::Url;
use crate::{
AppState, AppStatus,
database::db::{borrow_db_checked, borrow_db_mut_checked},
error::remote_access_error::RemoteAccessError,
remote::{
database::db::{borrow_db_checked, borrow_db_mut_checked}, error::remote_access_error::RemoteAccessError, remote::{
auth::generate_authorization_header,
requests::generate_url,
requests::make_request,
utils::{DROP_CLIENT_SYNC, DROP_CLIENT_WS_CLIENT},
},
}, AppState, AppStatus
};
use super::{
@ -48,10 +45,9 @@ pub fn gen_drop_url(path: String) -> Result<String, RemoteAccessError> {
#[tauri::command]
pub fn fetch_drop_object(path: String) -> Result<Vec<u8>, RemoteAccessError> {
let _drop_url = gen_drop_url(path.clone())?;
let req = generate_url(&[&path], &[])?;
let req = DROP_CLIENT_SYNC
.get(req)
.header("Authorization", generate_authorization_header())
let req = make_request(&DROP_CLIENT_SYNC, &[&path], &[], |r| {
r.header("Authorization", generate_authorization_header())
})?
.send();
match req {
@ -87,15 +83,13 @@ pub fn sign_out(app: AppHandle) {
}
#[tauri::command]
pub async fn retry_connect(state: tauri::State<'_, Mutex<AppState<'_>>>) -> Result<(), ()> {
let (app_status, user) = setup().await;
pub fn retry_connect(state: tauri::State<'_, Mutex<AppState>>) {
let (app_status, user) = setup();
let mut guard = state.lock().unwrap();
guard.status = app_status;
guard.user = user;
drop(guard);
Ok(())
}
#[tauri::command]
@ -130,7 +124,7 @@ pub fn auth_initiate_code(app: AppHandle) -> Result<String, RemoteAccessError> {
let code = auth_initiate_logic("code".to_string())?;
let header_code = code.clone();
println!("using code: {code} to sign in");
println!("using code: {} to sign in", code);
tauri::async_runtime::spawn(async move {
let load = async || -> Result<(), RemoteAccessError> {
@ -151,7 +145,9 @@ pub fn auth_initiate_code(app: AppHandle) -> Result<String, RemoteAccessError> {
match response.response_type.as_str() {
"token" => {
let recieve_app = app.clone();
manual_recieve_handshake(recieve_app, response.value).await.unwrap();
tauri::async_runtime::spawn_blocking(move || {
manual_recieve_handshake(recieve_app, response.value);
});
return Ok(());
}
_ => return Err(RemoteAccessError::HandshakeFailed(response.value)),
@ -175,8 +171,6 @@ pub fn auth_initiate_code(app: AppHandle) -> Result<String, RemoteAccessError> {
}
#[tauri::command]
pub async fn manual_recieve_handshake(app: AppHandle, token: String) -> Result<(), ()> {
recieve_handshake(app, format!("handshake/{token}")).await;
Ok(())
pub fn manual_recieve_handshake(app: AppHandle, token: String) {
recieve_handshake(app, format!("handshake/{token}"));
}

View File

@ -1,16 +1,13 @@
use url::Url;
use reqwest::blocking::{Client, RequestBuilder};
use crate::{
DB,
database::db::DatabaseImpls,
error::remote_access_error::RemoteAccessError,
remote::{auth::generate_authorization_header, utils::DROP_CLIENT_ASYNC},
};
use crate::{database::db::DatabaseImpls, error::remote_access_error::RemoteAccessError, DB};
pub fn generate_url<T: AsRef<str>>(
pub fn make_request<T: AsRef<str>, F: FnOnce(RequestBuilder) -> RequestBuilder>(
client: &Client,
path_components: &[T],
query: &[(T, T)],
) -> Result<Url, RemoteAccessError> {
f: F,
) -> Result<RequestBuilder, RemoteAccessError> {
let mut base_url = DB.fetch_base_url();
for endpoint in path_components {
base_url = base_url.join(endpoint.as_ref())?;
@ -21,13 +18,6 @@ pub fn generate_url<T: AsRef<str>>(
queries.append_pair(param.as_ref(), val.as_ref());
}
}
Ok(base_url)
}
pub async fn make_authenticated_get(url: Url) -> Result<reqwest::Response, reqwest::Error> {
DROP_CLIENT_ASYNC
.get(url)
.header("Authorization", generate_authorization_header())
.send()
.await
let response = client.get(base_url);
Ok(f(response))
}

View File

@ -5,7 +5,7 @@ use tauri::UriSchemeResponder;
use crate::{database::db::borrow_db_checked, remote::utils::DROP_CLIENT_SYNC};
pub async fn handle_server_proto_offline(_request: Request<Vec<u8>>, responder: UriSchemeResponder) {
pub fn handle_server_proto_offline(_request: Request<Vec<u8>>, responder: UriSchemeResponder) {
let four_oh_four = Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Vec::new())
@ -13,7 +13,7 @@ pub async fn handle_server_proto_offline(_request: Request<Vec<u8>>, responder:
responder.respond(four_oh_four);
}
pub async fn handle_server_proto(request: Request<Vec<u8>>, responder: UriSchemeResponder) {
pub fn handle_server_proto(request: Request<Vec<u8>>, responder: UriSchemeResponder) {
let db_handle = borrow_db_checked();
let web_token = match &db_handle.auth.as_ref().unwrap().web_token {
Some(e) => e,

View File

@ -1,7 +1,7 @@
{
"$schema": "https://schema.tauri.app/config/2.0.0",
"productName": "Drop Desktop Client",
"version": "0.3.3",
"version": "0.3.2-dl",
"identifier": "dev.drop.client",
"build": {
"beforeDevCommand": "yarn --cwd main dev --port 1432",