Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed Feb 15, 2024
1 parent 8d19f08 commit 3cb770f
Show file tree
Hide file tree
Showing 34 changed files with 436 additions and 161 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ get_filename_component(PARENT_SOURCE_DIR ${CMAKE_SOURCE_DIR} DIRECTORY)

if(NOT CMAKE_GENERATOR_PLATFORM OR "${CMAKE_GENERATOR_PLATFORM}" STREQUAL "${CMAKE_SYSTEM_PROCESSOR}")
if(EMSCRIPTEN)
message(STATUS "Compiling for architecture: WASM (${CMAKE_SYSTEM_PROCESSOR})")
message(STATUS "Compiling for architecture: WASM (on ${CMAKE_SYSTEM_PROCESSOR} machine)")
elseif(NINTENDO_SWITCH)
message(STATUS "Compiling for architecture: ${CMAKE_SYSTEM_PROCESSOR} (Nintendo Switch)")
elseif(APPLE AND CMAKE_OSX_ARCHITECTURES)
Expand Down
1 change: 1 addition & 0 deletions Sources/Jazz2.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@
<ClInclude Include="$(ExtensionLibraryPath)\IO\DeflateStream.h" />
<ClInclude Include="$(ExtensionLibraryPath)\Containers\SequenceHelpers.h" />
<ClInclude Include="$(ExtensionLibraryPath)\Containers\StringUtils.h" />
<ClInclude Include="$(ExtensionLibraryPath)\Containers\StringConcatenable.h" />
<ClInclude Include="simdjson\simdjson.h" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions Sources/Jazz2.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,9 @@
<ClInclude Include="nCine\Base\pdqsort\pdqsort.h">
<Filter>Header Files\nCine\Base\pdqsort</Filter>
</ClInclude>
<ClInclude Include="$(ExtensionLibraryPath)\Containers\StringBuilder.h">
<Filter>Header Files\Shared\Containers</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Main.cpp">
Expand Down
3 changes: 2 additions & 1 deletion Sources/Jazz2/Actors/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "../../nCine/Graphics/RenderQueue.h"

#include <Containers/GrowableArray.h>
#include <Containers/StringConcatenable.h>

using namespace Jazz2::Tiles;

Expand Down Expand Up @@ -3056,7 +3057,7 @@ namespace Jazz2::Actors
// Preload all weapons
for (std::int32_t i = 0; i < countof(_weaponAmmo); i++) {
if (_weaponAmmo[i] != 0) {
PreloadMetadataAsync("Weapon/"_s + WeaponNames[i]);
PreloadMetadataAsync(String("Weapon/"_s + WeaponNames[i]));
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/Jazz2/Compatibility/JJ2Anims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "JJ2Block.h"
#include "AnimSetMapping.h"

#include <Containers/StringConcatenable.h>
#include <IO/FileSystem.h>

using namespace Death::IO;
Expand Down Expand Up @@ -363,7 +364,7 @@ namespace Jazz2::Compatibility
continue;
} else {
fs::CreateDirectories(fs::CombinePath(targetPath, entry->Category));
filename = fs::CombinePath(entry->Category, entry->Name + ".aura"_s);
filename = fs::CombinePath(entry->Category, String(entry->Name + ".aura"_s));
}

int32_t stride = sizeX * anim.FrameConfigurationX;
Expand Down Expand Up @@ -482,7 +483,7 @@ namespace Jazz2::Compatibility
} else {
fs::CreateDirectories(fs::CombinePath(targetPath, entry->Category));

filename = fs::CombinePath(entry->Category, entry->Name + ".wav"_s);
filename = fs::CombinePath(entry->Category, String(entry->Name + ".wav"_s));
}

auto so = fs::Open(fs::CombinePath(targetPath, filename), FileAccessMode::Write);
Expand Down
3 changes: 2 additions & 1 deletion Sources/Jazz2/Compatibility/JJ2Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "../../nCine/Base/Algorithms.h"

#include <Containers/StringConcatenable.h>
#include <IO/FileSystem.h>
#include <IO/DeflateStream.h>

Expand Down Expand Up @@ -148,7 +149,7 @@ namespace Jazz2::Compatibility
if (sample == nullptr) {
d.WriteValue<std::uint8_t>(0);
} else {
String samplePath = "/"_s.join({ sample->Category, sample->Name + ".wav"_s });
String samplePath = sample->Category + '/' + sample->Name + ".wav"_s;
d.WriteValue<std::uint8_t>((std::uint8_t)samplePath.size());
d.Write(samplePath.data(), (std::uint32_t)samplePath.size());
}
Expand Down
7 changes: 4 additions & 3 deletions Sources/Jazz2/Compatibility/JJ2Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "../../nCine/Base/Algorithms.h"

#include <Containers/StringConcatenable.h>
#include <Containers/StringUtils.h>
#include <IO/DeflateStream.h>
#include <IO/FileSystem.h>
Expand Down Expand Up @@ -428,7 +429,7 @@ namespace Jazz2::Compatibility
i32tos(i / 8, numberBuffer);

StringView foundDot = path.findLastOr('.', path.end());
String extraLayersPath = fs::FindPathCaseInsensitive(path.prefix(foundDot.begin()) + "-MLLE-Data-"_s + numberBuffer + ".j2l"_s);
String extraLayersPath = fs::FindPathCaseInsensitive(String(path.prefix(foundDot.begin()) + "-MLLE-Data-"_s + numberBuffer + ".j2l"_s));

JJ2Level extraLayersFile;
if (extraLayersFile.Open(extraLayersPath, strictParser)) {
Expand Down Expand Up @@ -678,7 +679,7 @@ namespace Jazz2::Compatibility
}

if (isLevelToken) {
String adjustedText = ""_s;
String adjustedText;
auto levelTokens = text.split('|');
for (int j = 0; j < levelTokens.size(); j++) {
if (j != 0) {
Expand Down Expand Up @@ -1049,7 +1050,7 @@ namespace Jazz2::Compatibility
if (levelTokenConversion != nullptr) {
LevelToken token = levelTokenConversion(adjustedValue);
if (!token.Episode.empty()) {
String fullName = token.Episode + "/"_s + token.Level;
String fullName = token.Episode + '/' + token.Level;
so.WriteValue<uint8_t>(fullName.size());
so.Write(fullName.data(), fullName.size());
} else {
Expand Down
1 change: 1 addition & 0 deletions Sources/Jazz2/Compatibility/JJ2Strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "../../nCine/Base/Algorithms.h"

#include <Containers/StringConcatenable.h>
#include <Containers/StringUtils.h>
#include <IO/FileSystem.h>

Expand Down
25 changes: 13 additions & 12 deletions Sources/Jazz2/ContentResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# include <Environment.h>
#endif

#include <Containers/StringConcatenable.h>
#include <Containers/StringStlView.h>
#include <IO/DeflateStream.h>
#include <IO/MemoryStream.h>
Expand Down Expand Up @@ -388,7 +389,7 @@ namespace Jazz2
}

// Try to load it
auto s = fs::Open(fs::CombinePath({ GetContentPath(), "Metadata"_s, pathNormalized + ".res"_s }), FileAccessMode::Read);
auto s = fs::Open(fs::CombinePath({ GetContentPath(), "Metadata"_s, String(pathNormalized + ".res"_s) }), FileAccessMode::Read);
auto fileSize = s->GetSize();
if (fileSize < 4 || fileSize > 64 * 1024 * 1024) {
// 64 MB file size limit
Expand Down Expand Up @@ -583,7 +584,7 @@ namespace Jazz2
return RequestGraphicsAura(pathNormalized, paletteOffset);
}

auto s = fs::Open(fs::CombinePath({ GetContentPath(), "Animations"_s, pathNormalized + ".res"_s }), FileAccessMode::Read);
auto s = fs::Open(fs::CombinePath({ GetContentPath(), "Animations"_s, String(pathNormalized + ".res"_s) }), FileAccessMode::Read);
auto fileSize = s->GetSize();
if (fileSize < 4 || fileSize > 64 * 1024 * 1024) {
// 64 MB file size limit, also if not found try to use cache
Expand Down Expand Up @@ -874,9 +875,9 @@ namespace Jazz2
std::unique_ptr<Tiles::TileSet> ContentResolver::RequestTileSet(const StringView path, uint16_t captionTileId, bool applyPalette, const uint8_t* paletteRemapping)
{
// Try "Content" directory first, then "Cache" directory
String fullPath = fs::CombinePath({ GetContentPath(), "Tilesets"_s, path + ".j2t"_s });
String fullPath = fs::CombinePath({ GetContentPath(), "Tilesets"_s, String(path + ".j2t"_s) });
if (!fs::IsReadableFile(fullPath)) {
fullPath = fs::CombinePath({ GetCachePath(), "Tilesets"_s, path + ".j2t"_s });
fullPath = fs::CombinePath({ GetCachePath(), "Tilesets"_s, String(path + ".j2t"_s) });
}

auto s = fs::Open(fullPath, FileAccessMode::Read);
Expand Down Expand Up @@ -1066,17 +1067,17 @@ namespace Jazz2
bool ContentResolver::LevelExists(const StringView episodeName, const StringView levelName)
{
// Try "Content" directory first, then "Cache" directory
return (fs::IsReadableFile(fs::CombinePath({ GetContentPath(), "Episodes"_s, episodeName, levelName + ".j2l"_s })) ||
fs::IsReadableFile(fs::CombinePath({ GetCachePath(), "Episodes"_s, episodeName, levelName + ".j2l"_s })));
return (fs::IsReadableFile(fs::CombinePath({ GetContentPath(), "Episodes"_s, episodeName, String(levelName + ".j2l"_s) })) ||
fs::IsReadableFile(fs::CombinePath({ GetCachePath(), "Episodes"_s, episodeName, String(levelName + ".j2l"_s) })));
}

bool ContentResolver::TryLoadLevel(const StringView path, GameDifficulty difficulty, LevelDescriptor& descriptor)
{
// Try "Content" directory first, then "Cache" directory
auto pathNormalized = fs::ToNativeSeparators(path);
descriptor.FullPath = fs::CombinePath({ GetContentPath(), "Episodes"_s, pathNormalized + ".j2l"_s });
descriptor.FullPath = fs::CombinePath({ GetContentPath(), "Episodes"_s, String(pathNormalized + ".j2l"_s) });
if (!fs::IsReadableFile(descriptor.FullPath)) {
descriptor.FullPath = fs::CombinePath({ GetCachePath(), "Episodes"_s, pathNormalized + ".j2l"_s });
descriptor.FullPath = fs::CombinePath({ GetCachePath(), "Episodes"_s, String(pathNormalized + ".j2l"_s) });
}

auto s = fs::Open(descriptor.FullPath, FileAccessMode::Read);
Expand Down Expand Up @@ -1236,9 +1237,9 @@ namespace Jazz2

std::optional<Episode> ContentResolver::GetEpisode(const StringView name, bool withImages)
{
String fullPath = fs::CombinePath({ GetContentPath(), "Episodes"_s, name + ".j2e"_s });
String fullPath = fs::CombinePath({ GetContentPath(), "Episodes"_s, String(name + ".j2e"_s) });
if (!fs::IsReadableFile(fullPath)) {
fullPath = fs::CombinePath({ GetCachePath(), "Episodes"_s, name + ".j2e"_s });
fullPath = fs::CombinePath({ GetCachePath(), "Episodes"_s, String(name + ".j2e"_s) });
}
return GetEpisodeByPath(fullPath, withImages);
}
Expand Down Expand Up @@ -1589,12 +1590,12 @@ namespace Jazz2
#if defined(DEATH_DEBUG)
void ContentResolver::MigrateGraphics(const StringView path)
{
String auraPath = fs::CombinePath({ GetContentPath(), "Animations"_s, path.exceptSuffix(4) + ".aura"_s });
String auraPath = fs::CombinePath({ GetContentPath(), "Animations"_s, String(path.exceptSuffix(4) + ".aura"_s) });
if (fs::FileExists(auraPath)) {
return;
}

auto s = fs::Open(fs::CombinePath({ GetContentPath(), "Animations"_s, path + ".res"_s }), FileAccessMode::Read);
auto s = fs::Open(fs::CombinePath({ GetContentPath(), "Animations"_s, String(path + ".res"_s) }), FileAccessMode::Read);
auto fileSize = s->GetSize();
if (fileSize < 4 || fileSize > 64 * 1024 * 1024) {
// 64 MB file size limit, also if not found try to use cache
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/IRootController.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Jazz2
#endif

virtual Flags GetFlags() const = 0;
virtual const char* GetNewestVersion() const = 0;
virtual StringView GetNewestVersion() const = 0;

virtual void RefreshCacheLevels() = 0;

Expand Down
13 changes: 7 additions & 6 deletions Sources/Jazz2/LevelHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@

#include <float.h>

#include <Utf8.h>
#include <Containers/StaticArray.h>
#include <Containers/StringConcatenable.h>
#include <Utf8.h>

using namespace nCine;

Expand Down Expand Up @@ -233,9 +234,9 @@ namespace Jazz2
ZoneScopedC(0x4876AF);

if (!descriptor.DisplayName.empty()) {
theApplication().gfxDevice().setWindowTitle(StringView(NCINE_APP_NAME " - ", countof(NCINE_APP_NAME " - ") - 1) + descriptor.DisplayName);
theApplication().gfxDevice().setWindowTitle(String(NCINE_APP_NAME " - " + descriptor.DisplayName));
} else {
theApplication().gfxDevice().setWindowTitle(StringView(NCINE_APP_NAME, countof(NCINE_APP_NAME) - 1));
theApplication().gfxDevice().setWindowTitle(NCINE_APP_NAME);
}

_defaultNextLevel = std::move(descriptor.NextLevel);
Expand Down Expand Up @@ -311,7 +312,7 @@ namespace Jazz2
std::uint8_t playerParams[2] = { (std::uint8_t)levelInit.PlayerCarryOvers[i].Type, (std::uint8_t)i };
player->OnActivated(Actors::ActorActivationDetails(
this,
Vector3i(spawnPosition.X + (i * 30), spawnPosition.Y - (i * 30), PlayerZ - i),
Vector3i((std::int32_t)spawnPosition.X + (i * 30), (std::int32_t)spawnPosition.Y - (i * 30), PlayerZ - i),
playerParams
));

Expand Down Expand Up @@ -572,7 +573,7 @@ namespace Jazz2
_viewTexture->setMagFiltering(SamplerFilter::Nearest);
_viewTexture->setWrap(SamplerWrapping::ClampToEdge);

_camera->setOrthoProjection(0.0f, w, h, 0.0f);
_camera->setOrthoProjection(0.0f, (float)w, (float)h, 0.0f);

auto& resolver = ContentResolver::Get();

Expand Down Expand Up @@ -1968,7 +1969,7 @@ namespace Jazz2
if (notInitialized) {
_camera = std::make_unique<Camera>();
}
_camera->setOrthoProjection(0.0f, width, height, 0.0f);
_camera->setOrthoProjection(0.0f, (float)width, (float)height, 0.0f);
_camera->setView(0.0f, 0.0f, 0.0f, 1.0f);

if (notInitialized) {
Expand Down
9 changes: 5 additions & 4 deletions Sources/Jazz2/PreferencesCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "LevelHandler.h"
#include "UI/ControlScheme.h"

#include <Containers/StringConcatenable.h>
#include <Environment.h>
#include <IO/DeflateStream.h>
#include <IO/FileSystem.h>
Expand Down Expand Up @@ -495,8 +496,8 @@ namespace Jazz2
if (language == "en"_s) {
break;
}
if (i18n.LoadFromFile(fs::CombinePath({ resolver.GetContentPath(), "Translations"_s, language + ".mo"_s })) ||
i18n.LoadFromFile(fs::CombinePath({ resolver.GetCachePath(), "Translations"_s, language + ".mo"_s }))) {
if (i18n.LoadFromFile(fs::CombinePath({ resolver.GetContentPath(), "Translations"_s, String(language + ".mo"_s) })) ||
i18n.LoadFromFile(fs::CombinePath({ resolver.GetCachePath(), "Translations"_s, String(language + ".mo"_s) }))) {
std::memcpy(PreferencesCache::Language, language.data(), language.size());
std::memset(PreferencesCache::Language + language.size(), 0, sizeof(PreferencesCache::Language) - language.size());
break;
Expand All @@ -508,8 +509,8 @@ namespace Jazz2
if (baseLanguage == "en"_s) {
break;
}
if (i18n.LoadFromFile(fs::CombinePath({ resolver.GetContentPath(), "Translations"_s, baseLanguage + ".mo"_s })) ||
i18n.LoadFromFile(fs::CombinePath({ resolver.GetCachePath(), "Translations"_s, baseLanguage + ".mo"_s }))) {
if (i18n.LoadFromFile(fs::CombinePath({ resolver.GetContentPath(), "Translations"_s, String(baseLanguage + ".mo"_s) })) ||
i18n.LoadFromFile(fs::CombinePath({ resolver.GetCachePath(), "Translations"_s, String(baseLanguage + ".mo"_s) }))) {
std::memcpy(PreferencesCache::Language, baseLanguage.data(), baseLanguage.size());
std::memset(PreferencesCache::Language + baseLanguage.size(), 0, sizeof(PreferencesCache::Language) - baseLanguage.size());
break;
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/Scripting/RegisterArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stdlib.h>
#include <algorithm> // std::sort

#include <Containers/String.h>
#include <Containers/StringConcatenable.h>

using namespace Death::Containers;
using namespace Death::Containers::Literals;
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/Scripting/RegisterString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#define AS_USE_ACCESSORS 1

#include <Containers/String.h>
#include <Containers/StringConcatenable.h>

using namespace Death::Containers;
using namespace Death::Containers::Literals;
Expand Down
17 changes: 9 additions & 8 deletions Sources/Jazz2/Scripting/ScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../ContentResolver.h"

#include <Containers/GrowableArray.h>
#include <Containers/StringConcatenable.h>
#include <IO/FileSystem.h>

#if defined(DEATH_TARGET_WINDOWS) && !defined(CMAKE_BUILD)
Expand Down Expand Up @@ -374,12 +375,12 @@ namespace Jazz2::Scripting
}
case MetadataType::VirtualProperty: {
if (decl.ParentClass.empty()) {
asIScriptFunction* func = _module->GetFunctionByName(("get_"_s + decl.Declaration).data());
asIScriptFunction* func = _module->GetFunctionByName(String("get_"_s + decl.Declaration).data());
if (func != nullptr) {
auto entry = _funcMetadataMap.emplace(func->GetId(), Array<String>(NoInit, decl.Metadata.size())).first;
std::copy(decl.Metadata.begin(), decl.Metadata.end(), entry->second.data());
}
func = _module->GetFunctionByName(("set_"_s + decl.Declaration).data());
func = _module->GetFunctionByName(String("set_"_s + decl.Declaration).data());
if (func != nullptr) {
auto entry = _funcMetadataMap.emplace(func->GetId(), Array<String>(NoInit, decl.Metadata.size())).first;
std::copy(decl.Metadata.begin(), decl.Metadata.end(), entry->second.data());
Expand All @@ -392,12 +393,12 @@ namespace Jazz2::Scripting
}

asITypeInfo* type = _engine->GetTypeInfoById(typeId);
asIScriptFunction* func = type->GetMethodByName(("get_" + decl.Declaration).data());
asIScriptFunction* func = type->GetMethodByName(String("get_" + decl.Declaration).data());
if (func != nullptr) {
auto entry = it->second.FuncMetadataMap.emplace(func->GetId(), Array<String>(NoInit, decl.Metadata.size())).first;
std::copy(decl.Metadata.begin(), decl.Metadata.end(), entry->second.data());
}
func = type->GetMethodByName(("set_" + decl.Declaration).data());
func = type->GetMethodByName(String("set_" + decl.Declaration).data());
if (func != nullptr) {
auto entry = it->second.FuncMetadataMap.emplace(func->GetId(), Array<String>(NoInit, decl.Metadata.size())).first;
std::copy(decl.Metadata.begin(), decl.Metadata.end(), entry->second.data());
Expand Down Expand Up @@ -636,22 +637,22 @@ namespace Jazz2::Scripting
declaration = { };
type = MetadataType::Unknown;

std::string token;
StringView token;
asUINT len = 0;
asETokenClass t = asTC_WHITESPACE;

// Skip white spaces, comments and leading decorators
do {
pos += len;
t = _engine->ParseToken(&scriptContent[pos], scriptSize - pos, &len);
token.assign(&scriptContent[pos], len);
token = scriptContent.sliceSize(pos, len);
} while (t == asTC_WHITESPACE || t == asTC_COMMENT ||
token == "private"_s || token == "protected"_s || token == "shared"_s ||
token == "external"_s || token == "final"_s || token == "abstract"_s);

// We're expecting, either a class, interface, function, or variable declaration
if (t == asTC_KEYWORD || t == asTC_IDENTIFIER) {
token.assign(&scriptContent[pos], len);
token = scriptContent.sliceSize(pos, len);
if (token == "interface"_s || token == "class"_s || token == "enum"_s) {
do {
pos += len;
Expand All @@ -677,7 +678,7 @@ namespace Jazz2::Scripting
pos += len;
for (; pos < scriptSize;) {
t = _engine->ParseToken(&scriptContent[pos], scriptSize - pos, &len);
token.assign(&scriptContent[pos], len);
token = scriptContent.sliceSize(pos, len);
if (t == asTC_KEYWORD) {
if (token == "{"_s && nestedParenthesis == 0) {
if (hasParenthesis) {
Expand Down
Loading

0 comments on commit 3cb770f

Please sign in to comment.