Changeset View
Changeset View
Standalone View
Standalone View
source/ps/VisualReplay.cpp
/* Copyright (C) 2020 Wildfire Games. | /* Copyright (C) 2021 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 333 Lines • ▼ Show 20 Lines | JS::Value VisualReplay::LoadReplayData(const ScriptInterface& scriptInterface, const OsPath& directory) | ||||
// Get file size and modification date | // Get file size and modification date | ||||
CFileInfo fileInfo; | CFileInfo fileInfo; | ||||
GetFileInfo(replayFile, &fileInfo); | GetFileInfo(replayFile, &fileInfo); | ||||
const off_t fileSize = fileInfo.Size(); | const off_t fileSize = fileInfo.Size(); | ||||
if (fileSize == 0) | if (fileSize == 0) | ||||
return JS::NullValue(); | return JS::NullValue(); | ||||
std::ifstream* replayStream = new std::ifstream(OsString(replayFile).c_str()); | std::ifstream* replayStream = new std::ifstream(OsString(replayFile)); | ||||
CStr type; | CStr type; | ||||
if (!(*replayStream >> type).good()) | if (!(*replayStream >> type).good()) | ||||
{ | { | ||||
LOGERROR("Couldn't open %s.", replayFile.string8().c_str()); | LOGERROR("Couldn't open %s.", replayFile.string8().c_str()); | ||||
SAFE_DELETE(replayStream); | SAFE_DELETE(replayStream); | ||||
return JS::NullValue(); | return JS::NullValue(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | JS::Value VisualReplay::GetReplayAttributes(ScriptInterface::CmptPrivate* pCmptPrivate, const OsPath& directoryName) | ||||
ScriptInterface::CreateObject(rq, &attribs); | ScriptInterface::CreateObject(rq, &attribs); | ||||
// Return empty object if file doesn't exist | // Return empty object if file doesn't exist | ||||
const OsPath replayFile = GetDirectoryPath() / directoryName / L"commands.txt"; | const OsPath replayFile = GetDirectoryPath() / directoryName / L"commands.txt"; | ||||
if (!FileExists(replayFile)) | if (!FileExists(replayFile)) | ||||
return attribs; | return attribs; | ||||
// Open file | // Open file | ||||
std::istream* replayStream = new std::ifstream(OsString(replayFile).c_str()); | std::istream* replayStream = new std::ifstream(OsString(replayFile)); | ||||
CStr type, line; | CStr type, line; | ||||
ENSURE((*replayStream >> type).good() && type == "start"); | ENSURE((*replayStream >> type).good() && type == "start"); | ||||
// Read and return first line | // Read and return first line | ||||
std::getline(*replayStream, line); | std::getline(*replayStream, line); | ||||
pCmptPrivate->pScriptInterface->ParseJSON(line, &attribs); | pCmptPrivate->pScriptInterface->ParseJSON(line, &attribs); | ||||
SAFE_DELETE(replayStream);; | SAFE_DELETE(replayStream);; | ||||
return attribs; | return attribs; | ||||
Show All 35 Lines | |||||
JS::Value VisualReplay::GetReplayMetadata(ScriptInterface::CmptPrivate* pCmptPrivate, const OsPath& directoryName) | JS::Value VisualReplay::GetReplayMetadata(ScriptInterface::CmptPrivate* pCmptPrivate, const OsPath& directoryName) | ||||
{ | { | ||||
if (!HasReplayMetadata(directoryName)) | if (!HasReplayMetadata(directoryName)) | ||||
return JS::NullValue(); | return JS::NullValue(); | ||||
ScriptRequest rq(pCmptPrivate->pScriptInterface); | ScriptRequest rq(pCmptPrivate->pScriptInterface); | ||||
JS::RootedValue metadata(rq.cx); | JS::RootedValue metadata(rq.cx); | ||||
std::ifstream* stream = new std::ifstream(OsString(GetDirectoryPath() / directoryName / L"metadata.json").c_str()); | std::ifstream* stream = new std::ifstream(OsString(GetDirectoryPath() / directoryName / L"metadata.json")); | ||||
ENSURE(stream->good()); | ENSURE(stream->good()); | ||||
CStr line; | CStr line; | ||||
std::getline(*stream, line); | std::getline(*stream, line); | ||||
stream->close(); | stream->close(); | ||||
SAFE_DELETE(stream); | SAFE_DELETE(stream); | ||||
pCmptPrivate->pScriptInterface->ParseJSON(line, &metadata); | pCmptPrivate->pScriptInterface->ParseJSON(line, &metadata); | ||||
return metadata; | return metadata; | ||||
} | } |
Wildfire Games · Phabricator