From f236e5d4026d35b42cfaf99d4c32799f72a72ce7 Mon Sep 17 00:00:00 2001 From: xLuxy <67131061+xLuxy@users.noreply.github.com> Date: Sun, 17 Mar 2024 23:43:11 +0100 Subject: [PATCH] fix(shared): Fix alt.File.read on client + add encoding --- shared/js/enums/fileEncoding.js | 6 +-- shared/src/namespaces/FileNamespace.cpp | 49 ++++++++++++++++++++----- types/shared/index.d.ts | 13 ++++++- types/shared/package.json | 2 +- 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/shared/js/enums/fileEncoding.js b/shared/js/enums/fileEncoding.js index ad9fb00df..0469ef805 100644 --- a/shared/js/enums/fileEncoding.js +++ b/shared/js/enums/fileEncoding.js @@ -2,7 +2,7 @@ const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js"); alt.Enums.FileEncoding = createReverseLookupObject({ - UTF8: "utf-8", - UTF16: "utf-16", - BINARY: "binary" + Utf8: "utf-8", + Utf16: "utf-16", + Binary: "binary" }); diff --git a/shared/src/namespaces/FileNamespace.cpp b/shared/src/namespaces/FileNamespace.cpp index e46452bd3..353c2ca1e 100644 --- a/shared/src/namespaces/FileNamespace.cpp +++ b/shared/src/namespaces/FileNamespace.cpp @@ -5,26 +5,57 @@ static void Exists(js::FunctionContext& ctx) { if(!ctx.CheckArgCount(1)) return; - std::string path; - if(!ctx.GetArg(0, path)) return; + std::string fileName; + if(!ctx.GetArg(0, fileName)) return; js::SourceLocation location = js::SourceLocation::GetCurrent(ctx.GetResource()); - alt::IPackage::PathInfo info = alt::ICore::Instance().Resolve(ctx.GetResource()->GetResource(), path, location.file); + alt::IPackage::PathInfo info = alt::ICore::Instance().Resolve(ctx.GetResource()->GetResource(), fileName, location.file); if(!ctx.Check(info.pkg, "Invalid file")) return; ctx.Return(alt::ICore::Instance().FileExists(info.prefix + info.fileName)); } static void Read(js::FunctionContext& ctx) { - if(!ctx.CheckArgCount(1)) return; + if(!ctx.CheckArgCount(1, 2)) return; + + std::string fileName; + if(!ctx.GetArg(0, fileName)) return; - std::string path; - if(!ctx.GetArg(0, path)) return; + const auto encoding = ctx.GetArg(1, "utf-8"); js::SourceLocation location = js::SourceLocation::GetCurrent(ctx.GetResource()); - alt::IPackage::PathInfo info = alt::ICore::Instance().Resolve(ctx.GetResource()->GetResource(), path, location.file); - if(!ctx.Check(info.pkg, "Invalid file")) return; - ctx.Return(alt::ICore::Instance().FileRead(info.prefix + info.fileName)); + +#ifdef ALT_CLIENT_API + auto path = alt::ICore::Instance().Resolve(ctx.GetResource()->GetResource(), fileName, location.file); + if (!ctx.Check(path.pkg, "invalid asset pack")) return; + + alt::IPackage::File* file = path.pkg->OpenFile(path.fileName); + if (!ctx.Check(file, "file does not exist")) return; + + std::string data(path.pkg->GetFileSize(file), 0); + path.pkg->ReadFile(file, data.data(), data.size()); + path.pkg->CloseFile(file); +#else + std::string data = alt::ICore::Instance().FileRead(fileName); +#endif + + if(encoding == "utf-16") + { + ctx.Return(reinterpret_cast(data.data())); + } + else if(encoding == "binary") + { + const v8::Local buffer = v8::ArrayBuffer::New(ctx.GetIsolate(), data.size()); + const auto contents = buffer->GetBackingStore(); + + std::memcpy(contents->Data(), data.data(), data.size()); + + ctx.Return(buffer); + } + else + { + ctx.Return(data); + } } // clang-format off diff --git a/types/shared/index.d.ts b/types/shared/index.d.ts index 87cc89824..b1f042c7f 100644 --- a/types/shared/index.d.ts +++ b/types/shared/index.d.ts @@ -699,10 +699,13 @@ declare module "@altv/shared" { } export namespace File { - export function exists(path: string): boolean; - export function read(path: string): string; + export function exists(fileName: string): boolean; + export function read(fileName: string, encoding?: FileEncoding): string; } + export enum FileEncoding {} + // + export namespace RPC { interface CustomPlayerToServerRpcEvent {} interface CustomServerToPlayerRpcEvent {} @@ -3401,6 +3404,12 @@ declare module "@altv/shared" { export namespace Symbols { export const serialize: unique symbol; } + + export enum FileEncoding { + Utf8 = "utf-8", + Utf16 = "utf-16", + Binary = "binary" + } } declare abstract class Timer { diff --git a/types/shared/package.json b/types/shared/package.json index d5e2b583d..e4090b8c0 100644 --- a/types/shared/package.json +++ b/types/shared/package.json @@ -1,6 +1,6 @@ { "name": "@altv/shared", - "version": "0.0.14", + "version": "0.0.15", "description": "This package contains the type definitions for the alt:V JS module v2 shared types", "types": "index.d.ts", "files": [