Skip to content

Commit

Permalink
fix(shared): Fix alt.File.read on client + add encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
xLuxy committed Mar 17, 2024
1 parent 20f9a10 commit f236e5d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
6 changes: 3 additions & 3 deletions shared/js/enums/fileEncoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
});
49 changes: 40 additions & 9 deletions shared/src/namespaces/FileNamespace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>(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<uint16_t*>(data.data()));
}
else if(encoding == "binary")
{
const v8::Local<v8::ArrayBuffer> 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
Expand Down
13 changes: 11 additions & 2 deletions types/shared/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion types/shared/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down

0 comments on commit f236e5d

Please sign in to comment.