Skip to content

Commit e2c95b4

Browse files
authored
Merge pull request #497 from avaraline/feat-basepath-env-var
look up BasePath from an environment variable AVARA_RSRC_PATH
2 parents af6d853 + ed48fa7 commit e2c95b4

File tree

16 files changed

+115
-44
lines changed

16 files changed

+115
-44
lines changed

.github/workflows/avara-ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ jobs:
4141
- name: Run headless test
4242
run: .\Avara.msvc\x64\Release\Tests.exe
4343
shell: pwsh
44+
env:
45+
AVARA_RSRC_PATH: ${{ github.workspace }}\
4446
- name: WinAvara archive
4547
run: .\bin\winavarazip.ps1
4648
shell: pwsh
@@ -68,7 +70,7 @@ jobs:
6870
brew install googletest
6971
- uses: actions/checkout@v4
7072
- name: Run headless tests
71-
run: make -j4 tests
73+
run: make tests
7274
- name: make macdist
7375
run: make -j4 clean macdist
7476
- name: deploy main nightly

Avara.msvc/AvaraCore.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
<ClInclude Include="..\src\assets\AssetRepository.h" />
160160
<ClInclude Include="..\src\assets\AssetStorage.h" />
161161
<ClInclude Include="..\src\assets\BaseAssetStorage.h" />
162+
<ClInclude Include="..\src\assets\BasePath.h" />
162163
<ClInclude Include="..\src\assets\LocalAssetRepository.h" />
163164
<ClInclude Include="..\src\assets\PackageManifest.h" />
164165
<ClInclude Include="..\src\audio\CBasicSound.h" />
@@ -366,6 +367,7 @@
366367
<ItemGroup>
367368
<ClCompile Include="..\src\assets\AssetManager.cpp" />
368369
<ClCompile Include="..\src\assets\BaseAssetStorage.cpp" />
370+
<ClCompile Include="..\src\assets\BasePath.cpp" />
369371
<ClCompile Include="..\src\assets\LocalAssetRepository.cpp" />
370372
<ClCompile Include="..\src\assets\PackageManifest.cpp" />
371373
<ClCompile Include="..\src\audio\CBasicSound.cpp" />

Avara.msvc/AvaraCore.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
<ClInclude Include="..\src\assets\BaseAssetStorage.h">
5858
<Filter>Header Files</Filter>
5959
</ClInclude>
60+
<ClInclude Include="..\src\assets\BasePath.h">
61+
<Filter>Header Files</Filter>
62+
</ClInclude>
6063
<ClInclude Include="..\src\assets\LocalAssetRepository.h">
6164
<Filter>Header Files</Filter>
6265
</ClInclude>
@@ -980,6 +983,9 @@
980983
<ClCompile Include="..\src\assets\BaseAssetStorage.cpp">
981984
<Filter>Source Files</Filter>
982985
</ClCompile>
986+
<ClCompile Include="..\src\assets\BasePath.cpp">
987+
<Filter>Source Files</Filter>
988+
</ClCompile>
983989
<ClCompile Include="..\vendor\csscolorparser.cpp">
984990
<Filter>Source Files</Filter>
985991
</ClCompile>

Avara.xcodeproj/project.pbxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
/* Begin PBXBuildFile section */
1010
6025AE4E2C255E6A00A57EFB /* LegacyOpenGLRenderer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 60C274E22C23EBE9005B67A9 /* LegacyOpenGLRenderer.cpp */; };
11+
6055BF212DEA8A79005E3CFB /* BasePath.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6055BF202DEA8A79005E3CFB /* BasePath.cpp */; };
12+
6055BF222DEA8A79005E3CFB /* BasePath.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6055BF202DEA8A79005E3CFB /* BasePath.cpp */; };
13+
6055BF232DEA8A79005E3CFB /* BasePath.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6055BF202DEA8A79005E3CFB /* BasePath.cpp */; };
1114
940541ED2B5875B200E32241 /* OpenGLShader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 940541EC2B5875B200E32241 /* OpenGLShader.cpp */; };
1215
940541EE2B5875B200E32241 /* OpenGLShader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 940541EC2B5875B200E32241 /* OpenGLShader.cpp */; };
1316
9419E8472B49ECB1007C50D0 /* CTeamColorAdjuster.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9419E8462B49ECB1007C50D0 /* CTeamColorAdjuster.cpp */; };
@@ -568,6 +571,8 @@
568571
/* End PBXCopyFilesBuildPhase section */
569572

570573
/* Begin PBXFileReference section */
574+
6055BF1F2DEA8A79005E3CFB /* BasePath.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BasePath.h; sourceTree = "<group>"; };
575+
6055BF202DEA8A79005E3CFB /* BasePath.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = BasePath.cpp; sourceTree = "<group>"; };
571576
60C274E22C23EBE9005B67A9 /* LegacyOpenGLRenderer.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = LegacyOpenGLRenderer.cpp; sourceTree = "<group>"; };
572577
60C274E32C23EBE9005B67A9 /* LegacyOpenGLRenderer.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; path = LegacyOpenGLRenderer.h; sourceTree = "<group>"; };
573578
940541EB2B58744500E32241 /* OpenGLShader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OpenGLShader.h; sourceTree = "<group>"; };
@@ -1275,6 +1280,8 @@
12751280
944F2F6E2B323E2100856E53 /* LocalAssetRepository.h */,
12761281
948CBF132B335A1400147E80 /* BaseAssetStorage.cpp */,
12771282
948CBF122B3359D700147E80 /* BaseAssetStorage.h */,
1283+
6055BF1F2DEA8A79005E3CFB /* BasePath.h */,
1284+
6055BF202DEA8A79005E3CFB /* BasePath.cpp */,
12781285
);
12791286
path = assets;
12801287
sourceTree = "<group>";
@@ -2363,6 +2370,7 @@
23632370
files = (
23642371
E517F0EA299714220036B206 /* glad.c in Sources */,
23652372
E517F0EB299714220036B206 /* nanovg.c in Sources */,
2373+
6055BF222DEA8A79005E3CFB /* BasePath.cpp in Sources */,
23662374
E517F0EC299714220036B206 /* stb_vorbis.c in Sources */,
23672375
E517F053299713DB0036B206 /* tests.cpp in Sources */,
23682376
E517F054299713DB0036B206 /* CBasicSound.cpp in Sources */,
@@ -2533,6 +2541,7 @@
25332541
files = (
25342542
E5890F1B29895124007A875D /* pugixml.cpp in Sources */,
25352543
E5890CFE29895118007A875D /* CSmartPart.cpp in Sources */,
2544+
6055BF232DEA8A79005E3CFB /* BasePath.cpp in Sources */,
25362545
E5890F9529895124007A875D /* nanovg.c in Sources */,
25372546
E5890CE729895118007A875D /* CShuriken.cpp in Sources */,
25382547
E5890D0E29895118007A875D /* CRosterWindow.cpp in Sources */,
@@ -2823,6 +2832,7 @@
28232832
E5A21A2F2DC59135007D54FF /* OpenGLShader.cpp in Sources */,
28242833
E5A21A302DC59135007D54FF /* OpenGLVertices.cpp in Sources */,
28252834
E5A21A312DC59135007D54FF /* AbstractRenderer.cpp in Sources */,
2835+
6055BF212DEA8A79005E3CFB /* BasePath.cpp in Sources */,
28262836
E5A21A322DC59135007D54FF /* CommandManager.cpp in Sources */,
28272837
E5A21A332DC59135007D54FF /* TextCommand.cpp in Sources */,
28282838
E5A21A342DC59135007D54FF /* ARGBColor.cpp in Sources */,

Makefile

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,16 @@ DEPS := $(OBJS:.o=.d)
9696
# Alternatively set this to "NONE" for no code signing.
9797
SIGNING_ID := NONE
9898

99-
avara: set-version $(BUILD_DIR)/Avara resources
99+
avara: set-version $(BUILD_DIR)/Avara
100100

101-
tests: set-version $(BUILD_DIR)/tests resources
102-
$(BUILD_DIR)/tests
101+
tests: set-version $(BUILD_DIR)/tests
102+
AVARA_RSRC_PATH=$(shell pwd)/ $(BUILD_DIR)/tests
103103

104-
bspviewer: $(BUILD_DIR)/BSPViewer resources
104+
bspviewer: $(BUILD_DIR)/BSPViewer
105105

106-
levelviewer: $(BUILD_DIR)/AvaraLevelViewer resources
106+
levelviewer: $(BUILD_DIR)/AvaraLevelViewer
107107

108-
hsnd2wav: set-version $(BUILD_DIR)/hsnd2wav resources
108+
hsnd2wav: set-version $(BUILD_DIR)/hsnd2wav
109109

110110
frandom: $(BUILD_DIR)/frandom
111111

@@ -124,7 +124,8 @@ macdist: macapp
124124
winapp: avara
125125
$(RMDIR) $(BUILD_DIR)/WinAvara
126126
$(MKDIR_P) $(BUILD_DIR)/WinAvara
127-
cp -r $(BUILD_DIR)/{Avara.exe,levels,rsrc} $(BUILD_DIR)/WinAvara
127+
cp -r $(BUILD_DIR)/Avara.exe $(BUILD_DIR)/WinAvara
128+
cp -r {levels,rsrc} ${BUILD_DIR}/WinAvara
128129
cp /mingw64/bin/{libstdc++-6,libwinpthread-1,libgcc_s_seh-1,SDL2}.dll $(BUILD_DIR)/WinAvara
129130

130131
windist: winapp
@@ -192,9 +193,4 @@ clean-levels:
192193
$(RM) levels/*/ogg/*.ogg
193194
$(RM) levels/*/wav/*.wav
194195

195-
resources:
196-
# python3 bin/pict2svg.py
197-
# cp -r bsps levels rsrc shaders $(BUILD_DIR)
198-
rsync -av levels rsrc $(BUILD_DIR)
199-
200196
-include $(DEPS)

src/Avara.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "CBSPPart.h"
1515
#include "FastMat.h"
1616
#include "Preferences.h"
17+
#include "BasePath.h"
1718

1819
#ifdef _WIN32
1920
#include <Windows.h>
@@ -82,6 +83,12 @@ std::vector<std::string> combinedArgs(std::string defaultArgs, int argc, char* a
8283
}
8384

8485
int main(int argc, char *argv[]) {
86+
// Check basepath override.
87+
for (int i = 0; i < argc; i++) {
88+
if (strcmp(argv[i], "--basepath") == 0) {
89+
SetBasePath(argv[++i]);
90+
}
91+
}
8592
// Allow Windows to run in HiDPI mode.
8693
SetHiDPI();
8794

@@ -133,6 +140,9 @@ int main(int argc, char *argv[]) {
133140
textCommand.insert(0, "/");
134141
}
135142
textCommands.push_back(textCommand);
143+
} else if (arg == "--basepath") {
144+
// skip, it was handled earlier in main()
145+
i = i + 2;
136146
} else {
137147
SDL_Log("Unknown command-line argument '%s'\n", args[i].c_str());
138148
exit(1);

src/assets/AssetManager.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "AssetManager.h"
22
#include "BaseAssetStorage.h"
33
#include "LocalAssetRepository.h"
4-
4+
#include "BasePath.h"
55
#include "LevelLoader.h"
66

77
#include <algorithm>
@@ -33,18 +33,12 @@ void AssetCache<T>::RemoveAll(std::vector<MaybePackage> &packageList)
3333
}
3434
};
3535

36-
std::shared_ptr<AssetStorage> _baseStorage = BaseAssetStorage::GetInstance();
37-
std::shared_ptr<AssetStorage> _localStorage = LocalAssetRepository::GetInstance();
38-
std::shared_ptr<AssetRepository> _localRepo = LocalAssetRepository::GetInstance();
39-
4036
// Initialize static variables.
4137
BasePackage AssetManager::basePackage = BasePackage::Avara;
4238
std::vector<std::string> AssetManager::externalPackages = {};
43-
std::shared_ptr<AssetStorage> AssetManager::baseStorage = _baseStorage;
44-
std::shared_ptr<AssetStorage> AssetManager::assetStorage = _localStorage;
45-
std::vector<std::shared_ptr<AssetRepository>> AssetManager::repositoryStack = {
46-
_localRepo
47-
};
39+
std::shared_ptr<AssetStorage> AssetManager::baseStorage = {};
40+
std::shared_ptr<AssetStorage> AssetManager::assetStorage = {};
41+
std::vector<std::shared_ptr<AssetRepository>> AssetManager::repositoryStack = {};
4842
std::shared_ptr<nlohmann::json> AssetManager::objectTypes = nullptr;
4943
SimpleAssetCache<PackageManifest> AssetManager::manifestCache = {};
5044
SimpleAssetCache<std::string> AssetManager::avarascriptCache = {};
@@ -206,6 +200,12 @@ std::optional<std::shared_ptr<HullConfigRecord>> AssetManager::GetHull(int16_t i
206200

207201
void AssetManager::Init()
208202
{
203+
baseStorage = BaseAssetStorage::GetInstance();
204+
assetStorage = LocalAssetRepository::GetInstance();
205+
repositoryStack = {
206+
LocalAssetRepository::GetInstance()
207+
};
208+
209209
LoadManifest(NoPackage);
210210
LoadScript(NoPackage);
211211
LoadEnumeratedObjectTypes();

src/assets/AssetManager.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@
2525
# endif
2626
#endif
2727

28-
// Path separator
29-
#if defined(_WIN32)
30-
#define PATHSEP "\\"
31-
#else
32-
#define PATHSEP "/"
33-
#endif
34-
3528
// Package structure
3629
#define MANIFESTFILE "set.json"
3730

@@ -183,7 +176,7 @@ class AssetManager {
183176
static bool PackageInStorage(std::string packageName);
184177
private:
185178
AssetManager() {}
186-
179+
187180
static BasePackage basePackage;
188181
static std::vector<std::string> externalPackages;
189182
static std::shared_ptr<AssetStorage> baseStorage;

src/assets/BaseAssetStorage.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
#include "BaseAssetStorage.h"
22
#include "AssetManager.h"
3+
#include "BasePath.h"
34

4-
#include <SDL2/SDL.h>
55
#include <sstream>
66

77
BaseAssetStorage::BaseAssetStorage()
88
{
9-
char *sdlPathTmp = SDL_GetBasePath();
10-
basePath = std::string(sdlPathTmp);
11-
SDL_free(sdlPathTmp);
9+
basePath = GetBasePath();
1210
}
1311

1412
std::shared_ptr<BaseAssetStorage> BaseAssetStorage::GetInstance() {

src/assets/BasePath.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "BasePath.h"
2+
#include <SDL2/SDL.h>
3+
#include <cstdlib>
4+
5+
6+
#define RSRC_PATH_ENV_VAR "AVARA_RSRC_PATH"
7+
bool basepathLookupDone = false;
8+
std::string basepath = "";
9+
10+
void SetBasePath(char* bp) {
11+
// if set from a command line argument, override env var
12+
basepath.assign(bp);
13+
basepathLookupDone = true;
14+
SDL_Log("Asset base path set by --basepath to %s", basepath.c_str());
15+
}
16+
17+
std::string GetBasePath() {
18+
if (!basepathLookupDone) {
19+
// look at environment variable
20+
if (const char* env_bp = std::getenv(RSRC_PATH_ENV_VAR)) {
21+
size_t length = strlen(env_bp);
22+
if (length > 0) {
23+
basepath.assign(env_bp);
24+
}
25+
}
26+
if (basepath.length() < 1)
27+
basepath.assign(SDL_GetBasePath());
28+
basepathLookupDone = true;
29+
SDL_Log("Asset base path set to %s", basepath.c_str());
30+
}
31+
return basepath;
32+
}
33+

0 commit comments

Comments
 (0)