Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Path Class #2619

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion Source/Samples/Sample.inl
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ void Sample::HandleKeyDown(StringHash /*eventType*/, VariantMap& eventData)
Image screenshot(context_);
graphics->TakeScreenShot(screenshot);
// Here we save in the Data folder with date and time appended
screenshot.SavePNG(GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Screenshot_" +
screenshot.SavePNG(GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Screenshot_" +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why padding is so big? Better to remove such trash from PRs

Time::GetTimeStamp().Replaced(':', '_').Replaced('.', '_').Replaced(' ', '_') + ".png");
}
}
Expand Down
48 changes: 26 additions & 22 deletions Source/Tools/PackageTool/PackageTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <Urho3D/Core/Context.h>
#include <Urho3D/Container/ArrayPtr.h>
#include <Urho3D/Core/ProcessUtils.h>
#include <Urho3D/Core/Path.h>
#include <Urho3D/IO/File.h>
#include <Urho3D/IO/FileSystem.h>
#include <Urho3D/IO/PackageFile.h>
Expand All @@ -42,15 +43,15 @@ static const unsigned COMPRESSED_BLOCK_SIZE = 32768;

struct FileEntry
{
String name_;
Path name_;
unsigned offset_{};
unsigned size_{};
unsigned checksum_{};
};

SharedPtr<Context> context_(new Context());
SharedPtr<FileSystem> fileSystem_(new FileSystem(context_));
String basePath_;
Path basePath_;
Vector<FileEntry> entries_;
unsigned checksum_ = 0;
bool compress_ = false;
Expand All @@ -65,8 +66,8 @@ String ignoreExtensions_[] = {

int main(int argc, char** argv);
void Run(const Vector<String>& arguments);
void ProcessFile(const String& fileName, const String& rootDir);
void WritePackageFile(const String& fileName, const String& rootDir);
void ProcessFile(const Path& fileName, const Path& rootDir);
void WritePackageFile(const Path& fileName, const Path& rootDir);
void WriteHeader(File& dest);

int main(int argc, char** argv)
Expand Down Expand Up @@ -109,7 +110,10 @@ void Run(const Vector<String>& arguments)
for (unsigned i = 2; i < arguments.Size(); ++i)
{
if (arguments[i][0] != '-')
basePath_ = AddTrailingSlash(arguments[i]);
{
basePath_ = Path(arguments[i]);
basePath_.AddTrailingSlash();
}
else
{
if (arguments[i].Length() > 1)
Expand All @@ -136,15 +140,15 @@ void Run(const Vector<String>& arguments)
PrintLine("Scanning directory " + dirName + " for files");

// Get the file list recursively
Vector<String> fileNames;
Vector<Path> fileNames;
fileSystem_->ScanDir(fileNames, dirName, "*.*", SCAN_FILES, true);
if (!fileNames.Size())
ErrorExit("No files found");

// Check for extensions to ignore
for (unsigned i = fileNames.Size() - 1; i < fileNames.Size(); --i)
{
String extension = GetExtension(fileNames[i]);
String extension = fileNames[i].GetExtension();
for (unsigned j = 0; j < ignoreExtensions_[j].Length(); ++j)
{
if (extension == ignoreExtensions_[j])
Expand Down Expand Up @@ -180,10 +184,10 @@ void Run(const Vector<String>& arguments)
// Fallthrough
case 'l':
{
const HashMap<String, PackageEntry>& entries = packageFile->GetEntries();
for (HashMap<String, PackageEntry>::ConstIterator i = entries.Begin(); i != entries.End();)
const HashMap<Path, PackageEntry>& entries = packageFile->GetEntries();
for (auto i = entries.Begin(); i != entries.End();)
{
HashMap<String, PackageEntry>::ConstIterator current = i++;
auto current = i++;
String fileEntry(current->first_);
if (outputCompressionRatio)
{
Expand All @@ -203,12 +207,12 @@ void Run(const Vector<String>& arguments)
}
}

void ProcessFile(const String& fileName, const String& rootDir)
void ProcessFile(const Path& fileName, const Path& rootDir)
{
String fullPath = rootDir + "/" + fileName;
Path fullPath = rootDir / fileName;
File file(context_);
if (!file.Open(fullPath))
ErrorExit("Could not open file " + fileName);
ErrorExit("Could not open file " + fileName.ToString());
if (!file.GetSize())
return;

Expand All @@ -220,22 +224,22 @@ void ProcessFile(const String& fileName, const String& rootDir)
entries_.Push(newEntry);
}

void WritePackageFile(const String& fileName, const String& rootDir)
void WritePackageFile(const Path& fileName, const Path& rootDir)
{
if (!quiet_)
PrintLine("Writing package");

File dest(context_);
if (!dest.Open(fileName, FILE_WRITE))
ErrorExit("Could not open output file " + fileName);
ErrorExit("Could not open output file " + fileName.ToString());

// Write ID, number of files & placeholder for checksum
WriteHeader(dest);

for (unsigned i = 0; i < entries_.Size(); ++i)
{
// Write entry (correct offset is still unknown, will be filled in later)
dest.WriteString(basePath_ + entries_[i].name_);
dest.WritePath(basePath_ + entries_[i].name_);
dest.WriteUInt(entries_[i].offset_);
dest.WriteUInt(entries_[i].size_);
dest.WriteUInt(entries_[i].checksum_);
Expand All @@ -248,18 +252,18 @@ void WritePackageFile(const String& fileName, const String& rootDir)
for (unsigned i = 0; i < entries_.Size(); ++i)
{
lastOffset = entries_[i].offset_ = dest.GetSize();
String fileFullPath = rootDir + "/" + entries_[i].name_;
Path fileFullPath = rootDir + "/" + entries_[i].name_;

File srcFile(context_, fileFullPath);
if (!srcFile.IsOpen())
ErrorExit("Could not open file " + fileFullPath);
ErrorExit("Could not open file " + fileFullPath.ToString());

unsigned dataSize = entries_[i].size_;
totalDataSize += dataSize;
SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);

if (srcFile.Read(&buffer[0], dataSize) != dataSize)
ErrorExit("Could not read file " + fileFullPath);
ErrorExit("Could not read file " + fileFullPath.ToString());
srcFile.Close();

for (unsigned j = 0; j < dataSize; ++j)
Expand All @@ -271,7 +275,7 @@ void WritePackageFile(const String& fileName, const String& rootDir)
if (!compress_)
{
if (!quiet_)
PrintLine(entries_[i].name_ + " size " + String(dataSize));
PrintLine(entries_[i].name_.ToString() + " size " + String(dataSize));
dest.Write(&buffer[0], entries_[i].size_);
}
else
Expand All @@ -288,7 +292,7 @@ void WritePackageFile(const String& fileName, const String& rootDir)

auto packedSize = (unsigned)LZ4_compress_HC((const char*)&buffer[pos], (char*)compressBuffer.Get(), unpackedSize, LZ4_compressBound(unpackedSize), 0);
if (!packedSize)
ErrorExit("LZ4 compression failed for file " + entries_[i].name_ + " at offset " + String(pos));
ErrorExit("LZ4 compression failed for file " + entries_[i].name_.ToString() + " at offset " + String(pos));

dest.WriteUShort((unsigned short)unpackedSize);
dest.WriteUShort((unsigned short)packedSize);
Expand Down Expand Up @@ -318,7 +322,7 @@ void WritePackageFile(const String& fileName, const String& rootDir)

for (unsigned i = 0; i < entries_.Size(); ++i)
{
dest.WriteString(basePath_ + entries_[i].name_);
dest.WritePath(basePath_ + entries_[i].name_);
dest.WriteUInt(entries_[i].offset_);
dest.WriteUInt(entries_[i].size_);
dest.WriteUInt(entries_[i].checksum_);
Expand Down
24 changes: 13 additions & 11 deletions Source/Tools/ScriptCompiler/ScriptCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <Urho3D/AngelScript/ScriptFile.h>
#include <Urho3D/Core/Context.h>
#include <Urho3D/Core/ProcessUtils.h>
#include <Urho3D/Core/Path.h>
#include <Urho3D/Engine/Engine.h>
#include <Urho3D/Engine/EngineDefs.h>
#include <Urho3D/IO/File.h>
Expand All @@ -43,7 +44,7 @@

using namespace Urho3D;

void CompileScript(Context* context, const String& fileName);
void CompileScript(Context* context, const Path& fileName);

int main(int argc, char** argv)
{
Expand All @@ -54,8 +55,8 @@ int main(int argc, char** argv)
#endif

bool dumpApiMode = false;
String sourceTree;
String outputFile;
Path sourceTree;
Path outputFile;

if (arguments.Size() < 1)
ErrorExit("Usage: ScriptCompiler <input file> [resource path for includes]\n"
Expand Down Expand Up @@ -110,8 +111,9 @@ int main(int argc, char** argv)

if (!dumpApiMode)
{
String path, file, extension;
SplitPath(outputFile, path, file, extension);
Path path;
String file, extension;
outputFile.Split(path, file, extension);

auto* cache = context->GetSubsystem<ResourceCache>();

Expand All @@ -125,7 +127,7 @@ int main(int argc, char** argv)
CompileScript(context, outputFile);
else
{
Vector<String> scriptFiles;
Vector<Path> scriptFiles;
context->GetSubsystem<FileSystem>()->ScanDir(scriptFiles, path, file + extension, SCAN_FILES, false);
for (unsigned i = 0; i < scriptFiles.Size(); ++i)
CompileScript(context, path + scriptFiles[i]);
Expand Down Expand Up @@ -153,22 +155,22 @@ int main(int argc, char** argv)
return EXIT_SUCCESS;
}

void CompileScript(Context* context, const String& fileName)
void CompileScript(Context* context, const Path& fileName)
{
PrintLine("Compiling script file " + fileName);
PrintLine("Compiling script file " + fileName.ToString());

File inFile(context, fileName, FILE_READ);
if (!inFile.IsOpen())
ErrorExit("Failed to open script file " + fileName);
ErrorExit("Failed to open script file " + fileName.ToString());

ScriptFile script(context);
if (!script.Load(inFile))
ErrorExit();

String outFileName = ReplaceExtension(fileName, ".asc");
Path outFileName = fileName.WithReplacedExtension(".asc");
File outFile(context, outFileName, FILE_WRITE);
if (!outFile.IsOpen())
ErrorExit("Failed to open output file " + fileName);
ErrorExit("Failed to open output file " + outFileName.ToString());

script.SaveByteCode(outFile);
}
11 changes: 6 additions & 5 deletions Source/Tools/Urho3DPlayer/Urho3DPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <Urho3D/AngelScript/Script.h>
#endif
#include <Urho3D/Core/Main.h>
#include <Urho3D/Core/Path.h>
#include <Urho3D/Engine/Engine.h>
#include <Urho3D/Engine/EngineDefs.h>
#include <Urho3D/IO/FileSystem.h>
Expand Down Expand Up @@ -56,7 +57,7 @@ void Urho3DPlayer::Setup()
// Note that the command file name uses a hardcoded path that does not utilize the resource system
// properly (including resource path prefix), as the resource system is not yet initialized at this point
auto* filesystem = GetSubsystem<FileSystem>();
const String commandFileName = filesystem->GetProgramDir() + "Data/CommandLine.txt";
const Path commandFileName = filesystem->GetProgramDir() + "Data/CommandLine.txt";
if (GetArguments().Empty() && filesystem->FileExists(commandFileName))
{
SharedPtr<File> commandFile(new File(context_, commandFileName));
Expand Down Expand Up @@ -131,7 +132,7 @@ void Urho3DPlayer::Setup()
else
{
// Use the script file name as the base name for the log file
engineParameters_[EP_LOG_NAME] = filesystem->GetAppPreferencesDir("urho3d", "logs") + GetFileNameAndExtension(scriptFileName_) + ".log";
engineParameters_[EP_LOG_NAME] = filesystem->GetAppPreferencesDir("urho3d", "logs") + scriptFileName_.GetFileNameAndExtension() + ".log";
}
#else
// On Web platform setup a default windowed resolution similar to the executable samples
Expand Down Expand Up @@ -168,7 +169,7 @@ void Urho3DPlayer::Start()
return;
}

String extension = GetExtension(scriptFileName_);
String extension = scriptFileName_.GetExtension();
if (extension != ".lua" && extension != ".luc")
{
#ifdef URHO3D_ANGELSCRIPT
Expand All @@ -180,7 +181,7 @@ void Urho3DPlayer::Start()

/// \hack If we are running the editor, also instantiate Lua subsystem to enable editing Lua ScriptInstances
#ifdef URHO3D_LUA
if (scriptFileName_.Contains("Editor.as", false))
if (scriptFileName_.ToString().Contains("Editor.as", false))
context_->RegisterSubsystem(new LuaScript(context_));
#endif
// If script loading is successful, proceed to main loop
Expand Down Expand Up @@ -277,5 +278,5 @@ void Urho3DPlayer::GetScriptFileName()
{
const Vector<String>& arguments = GetArguments();
if (arguments.Size() && arguments[0][0] != '-')
scriptFileName_ = GetInternalPath(arguments[0]);
scriptFileName_ = Path(arguments[0]);
}
3 changes: 2 additions & 1 deletion Source/Tools/Urho3DPlayer/Urho3DPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#pragma once

#include <Urho3D/Engine/Application.h>
#include <Urho3D/Core/Path.h>

using namespace Urho3D;

Expand Down Expand Up @@ -53,7 +54,7 @@ class Urho3DPlayer : public Application
void GetScriptFileName();

/// Script file name.
String scriptFileName_;
Path scriptFileName_;
/// Flag whether CommandLine.txt was already successfully read.
bool commandLineRead_;

Expand Down
43 changes: 43 additions & 0 deletions Source/Urho3D/AngelScript/CoreAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,48 @@ static void RegisterStringUtils(asIScriptEngine* engine)
engine->RegisterGlobalFunction("String GetFileSizeString(uint64)", asFUNCTION(GetFileSizeString), asCALL_CDECL);
}


static void ConstructPathDefault(Path* ptr)
{
new(ptr) Path();
}
static void DestructPath(Path* ptr)
{
ptr->~Path();
}
static void ConstructPathCopy(const Path& value, Path* ptr)
{
new(ptr) Path(value);
}
static void ConstructPathString(const String& value, Path* ptr)
{
new(ptr) Path(value);
}
static void RegisterPath(asIScriptEngine* engine)
{
engine->RegisterObjectType("Path", sizeof(Path), asOBJ_VALUE | asOBJ_APP_CLASS_CDK);
engine->RegisterObjectBehaviour("Path", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructPathDefault), asCALL_CDECL_OBJLAST);
engine->RegisterObjectBehaviour("Path", asBEHAVE_CONSTRUCT, "void f(const Path&in)", asFUNCTION(ConstructPathCopy), asCALL_CDECL_OBJLAST);
engine->RegisterObjectBehaviour("Path", asBEHAVE_CONSTRUCT, "void f(const String&in)", asFUNCTION(ConstructPathString), asCALL_CDECL_OBJLAST);
engine->RegisterObjectMethod("Path", "Path& opAssign(const Path&in)", asMETHOD(Path, operator =), asCALL_THISCALL);

engine->RegisterObjectBehaviour("Path", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructPath), asCALL_CDECL_OBJLAST);
engine->RegisterObjectMethod("Path", "const String& ToString() const", asMETHOD(Path, ToString), asCALL_THISCALL);

// engine->RegisterObjectBehaviour("Path", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION([](Path*p){}), asCALL_CDECL_OBJLAST);
// engine->RegisterObjectType("AttributeInfo", sizeof(AttributeInfo), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK);
// engine->RegisterObjectBehaviour("AttributeInfo", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructAttributeInfo), asCALL_CDECL_OBJLAST);
// engine->RegisterObjectBehaviour("AttributeInfo", asBEHAVE_CONSTRUCT, "void f(const AttributeInfo&in)", asFUNCTION(ConstructAttributeInfoCopy), asCALL_CDECL_OBJLAST);
// engine->RegisterObjectBehaviour("AttributeInfo", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructAttributeInfo), asCALL_CDECL_OBJLAST);
// engine->RegisterObjectMethod("AttributeInfo", "AttributeInfo& opAssign(const AttributeInfo&in)", asMETHODPR(AttributeInfo, operator =, (const AttributeInfo&), AttributeInfo&), asCALL_THISCALL);
// engine->RegisterObjectMethod("AttributeInfo", "Array<String>@ get_enumNames() const", asFUNCTION(AttributeInfoGetEnumNames), asCALL_CDECL_OBJLAST);
// engine->RegisterObjectProperty("AttributeInfo", "VariantType type", offsetof(AttributeInfo, type_));
// engine->RegisterObjectProperty("AttributeInfo", "String name", offsetof(AttributeInfo, name_));
// engine->RegisterObjectProperty("AttributeInfo", "Variant defaultValue", offsetof(AttributeInfo, defaultValue_));
// engine->RegisterObjectProperty("AttributeInfo", "uint mode", offsetof(AttributeInfo, mode_));
// engine->RegisterObjectProperty("AttributeInfo", "VariantMap metadata", offsetof(AttributeInfo, metadata_));
}

static void ConstructTimer(Timer* ptr)
{
new(ptr) Timer();
Expand Down Expand Up @@ -1051,6 +1093,7 @@ void RegisterCoreAPI(asIScriptEngine* engine)
RegisterVariant(engine);
RegisterSpline(engine);
RegisterStringUtils(engine);
RegisterPath(engine);
RegisterProcessUtils(engine);
RegisterObject(engine);
RegisterTimer(engine);
Expand Down
Loading