Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
levinli303 committed Apr 27, 2021
2 parents 917586d + cb8c1e8 commit 4b4f4c0
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 111 deletions.
38 changes: 36 additions & 2 deletions src/celcompat/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
#include <cstdlib>
#endif

#ifdef _WIN32
#define W(s) L##s
#else
#define W(s) s
#endif

namespace celestia
{
namespace filesystem
Expand Down Expand Up @@ -83,7 +89,7 @@ path path::filename() const
path path::stem() const
{
auto fn = filename().native();
auto pos = fn.rfind('.');
auto pos = fn.rfind(W('.'));

if (pos == 0 || pos == string_type::npos)
return fn;
Expand All @@ -94,7 +100,7 @@ path path::stem() const
path path::extension() const
{
auto fn = filename().native();
auto pos = fn.rfind('.');
auto pos = fn.rfind(W('.'));

if (pos == 0 || pos == string_type::npos)
return path();
Expand All @@ -114,6 +120,34 @@ path path::parent_path() const
return path(m_path.substr(0, pos));
}

path& path::replace_extension(const path& replacement)
{
auto pos = m_path.rfind(W('.'));

if (pos != (m_path.length() - 1))
{
if (pos != 0 && pos != string_type::npos)
m_path.resize(pos);
}
else
{
// special case for "/dir/" or "/dir/."
path basename = filename();
if (!(basename.empty() || basename == W(".")))
m_path.resize(pos);
}

if (replacement.empty())
return *this;

if (replacement.m_path[0] != W('.'))
m_path += W('.');

m_path += replacement.m_path;

return *this;
}


bool path::is_absolute() const
{
Expand Down
2 changes: 2 additions & 0 deletions src/celcompat/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ class path
path extension() const;
path parent_path() const;

path& replace_extension(const path& replacement = path());

bool is_relative() const
{
return !is_absolute();
Expand Down
28 changes: 9 additions & 19 deletions src/celengine/mapmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
// of the License, or (at your option) any later version.

#include <celutil/debug.h>
#include <celutil/fsutils.h>
#include <array>
#include <fstream>
#include "mapmanager.h"

using namespace std;
using namespace celestia;

static std::array<const char*, 1> extensions = {"map"};

WarpMesh::WarpMesh(int nx, int ny, float *data) :
nx(nx),
Expand Down Expand Up @@ -113,31 +118,16 @@ WarpMeshManager* GetWarpMeshManager()
return warpMeshManager;
}

static string resolveWildcard(const string& filename)
{
string base(filename, 0, filename.length() - 1);

string mapfile = base + "map";
ifstream in(mapfile);
if (in.good())
return mapfile;

return {};
}

fs::path WarpMeshInfo::resolve(const fs::path& baseDir)
{
bool wildcard = false;
if (!source.empty() && source.at(source.length() - 1) == '*')
wildcard = true;
bool wildcard = source.extension() == ".*";

fs::path filename = baseDir / source;

if (wildcard)
{
string matched = resolveWildcard(filename.string());
if (matched.empty())
return filename; // . . . for lack of any better way to handle it.
else
fs::path matched = util::ResolveWildcard(filename, extensions);
if (!matched.empty())
return matched;
}

Expand Down
6 changes: 2 additions & 4 deletions src/celengine/mapmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

#pragma once

#include <string>
#include <map>
#include <functional>
#include <celutil/resmanager.h>

Expand Down Expand Up @@ -39,9 +37,9 @@ class WarpMesh
class WarpMeshInfo : public ResourceInfo<WarpMesh>
{
public:
std::string source;
fs::path source;

WarpMeshInfo(const std::string& source) : source(source) {};
WarpMeshInfo(const fs::path& source) : source(source) {};

fs::path resolve(const fs::path&) override;
WarpMesh* load(const fs::path&) override;
Expand Down
6 changes: 3 additions & 3 deletions src/celengine/modelgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,12 @@ ModelGeometry::loadTextures()
}


string
fs::path
CelestiaTextureResource::source() const
{
if (m_textureHandle == InvalidResource)
return "";
return fs::path();

const TextureInfo* t = GetTextureManager()->getResourceInfo(textureHandle());
return t ? t->source : "";
return t ? t->source : fs::path();
}
2 changes: 1 addition & 1 deletion src/celengine/modelgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CelestiaTextureResource : public cmod::Material::TextureResource
return m_textureHandle;
}

std::string source() const;
fs::path source() const;

private:
ResourceHandle m_textureHandle;
Expand Down
69 changes: 17 additions & 52 deletions src/celengine/texmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,34 @@
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

#include <config.h>
#include <celutil/debug.h>
#include <iostream>
#include <celutil/fsutils.h>
#include <fstream>
#include <array>
#include "multitexture.h"
#include "texmanager.h"

using namespace std;

using namespace celestia;

static TextureManager* textureManager = nullptr;

static const char *directories[]=
static std::array<const char*, 3> directories =
{
"lores",
"medres",
"hires"
};

static std::array<const char*, 6> extensions =
{
"png",
"jpg",
"jpeg",
"dds",
"dxt5nm",
"ctx"
};

TextureManager* GetTextureManager()
{
Expand All @@ -34,59 +43,17 @@ TextureManager* GetTextureManager()
return textureManager;
}


static string resolveWildcard(const string& filename)
{
string base(filename, 0, filename.length() - 1);

string pngfile = base + "png";
{
ifstream in(pngfile);
if (in.good())
return pngfile;
}
string jpgfile = base + "jpg";
{
ifstream in(jpgfile);
if (in.good())
return jpgfile;
}
string ddsfile = base + "dds";
{
ifstream in(ddsfile);
if (in.good())
return ddsfile;
}
string dxt5file = base + "dxt5nm";
{
ifstream in(dxt5file);
if (in.good())
return dxt5file;
}
string ctxfile = base + "ctx";
{
ifstream in(ctxfile);
if (in.good())
return ctxfile;
}

return "";
}


fs::path TextureInfo::resolve(const fs::path& baseDir)
{
bool wildcard = false;
if (!source.empty() && source.at(source.length() - 1) == '*')
wildcard = true;
bool wildcard = source.extension() == ".*";

if (!path.empty())
{
fs::path filename = path / "textures" / directories[resolution] / source;
// cout << "Resolve: testing [" << filename << "]\n";
if (wildcard)
{
filename = resolveWildcard(filename.string());
filename = util::ResolveWildcard(filename, extensions);
if (!filename.empty())
return filename;
}
Expand All @@ -101,10 +68,8 @@ fs::path TextureInfo::resolve(const fs::path& baseDir)
fs::path filename = baseDir / directories[resolution] / source;
if (wildcard)
{
string matched = resolveWildcard(filename.string());
if (matched.empty())
return filename; // . . . for lack of any better way to handle it.
else
fs::path matched = util::ResolveWildcard(filename, extensions);
if (!matched.empty())
return matched;
}

Expand Down
10 changes: 4 additions & 6 deletions src/celengine/texmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#ifndef _TEXMANAGER_H_
#define _TEXMANAGER_H_

#include <string>
#include <map>
#include <celutil/resmanager.h>
#include <celengine/texture.h>
#include "multitexture.h"
Expand All @@ -20,7 +18,7 @@
class TextureInfo : public ResourceInfo<Texture>
{
public:
std::string source;
fs::path source;
fs::path path;
unsigned int flags;
float bumpHeight;
Expand All @@ -35,7 +33,7 @@ class TextureInfo : public ResourceInfo<Texture>
BorderClamp = 0x20,
};

TextureInfo(const std::string& _source,
TextureInfo(const fs::path& _source,
const fs::path& _path,
unsigned int _flags,
unsigned int _resolution = medres) :
Expand All @@ -45,7 +43,7 @@ class TextureInfo : public ResourceInfo<Texture>
bumpHeight(0.0f),
resolution(_resolution) {};

TextureInfo(const std::string& _source,
TextureInfo(const fs::path& _source,
const fs::path& _path,
float _bumpHeight,
unsigned int _flags,
Expand All @@ -56,7 +54,7 @@ class TextureInfo : public ResourceInfo<Texture>
bumpHeight(_bumpHeight),
resolution(_resolution) {};

TextureInfo(const std::string& _source,
TextureInfo(const fs::path& _source,
unsigned int _flags,
unsigned int _resolution = medres) :
source(_source),
Expand Down
11 changes: 6 additions & 5 deletions src/celmodel/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <string>
#include <array>
#include <celutil/color.h>
#include <celcompat/filesystem.h>


namespace cmod
Expand Down Expand Up @@ -89,18 +90,18 @@ class Material
class TextureResource
{
public:
virtual ~TextureResource() {};
virtual std::string source() const = 0;
virtual ~TextureResource() = default;
virtual fs::path source() const = 0;
};

class DefaultTextureResource : public TextureResource
{
public:
DefaultTextureResource(const std::string& source) : m_source(source) {};
std::string source() const { return m_source; }
DefaultTextureResource(const fs::path& source) : m_source(source) {};
fs::path source() const override { return m_source; }

private:
std::string m_source;
fs::path m_source;
};

enum BlendMode
Expand Down
8 changes: 4 additions & 4 deletions src/celmodel/modelfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ AsciiModelWriter::writeMaterial(const Material& material)

for (int i = 0; i < Material::TextureSemanticMax; i++)
{
string texSource;
fs::path texSource;
if (material.maps[i] != nullptr)
{
texSource = material.maps[i]->source();
Expand All @@ -1588,7 +1588,7 @@ AsciiModelWriter::writeMaterial(const Material& material)
assert(0);
}

out << " \"" << texSource << "\"\n";
out << " \"" << texSource.string() << "\"\n";
}
}

Expand Down Expand Up @@ -2345,12 +2345,12 @@ BinaryModelWriter::writeMaterial(const Material& material)
{
if (material.maps[i])
{
string texSource = material.maps[i]->source();
fs::path texSource = material.maps[i]->source();
if (!texSource.empty())
{
writeToken(out, CMOD_Texture);
writeInt16(out, (int16_t) i);
writeTypeString(out, texSource);
writeTypeString(out, texSource.string());
}
}
}
Expand Down
Loading

0 comments on commit 4b4f4c0

Please sign in to comment.