From fb02b38279d3aa95bdd243f6fd4277879b410600 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Wed, 7 Apr 2021 21:46:44 +0200 Subject: [PATCH] - better .def loading logic. To allow cumulative loading without interfering with other ports, Raze will now look for files called xxxx-raze.def, where xxxx is the default .def name (e.g. duke3d-raze.def for Duke3D.) and if that is found, cumulatively load all same-named files - it will fall back on the default name if no such thing is found. -def still overrides both and will not cumulatively load. --- source/build/include/build.h | 2 +- source/build/src/defs.cpp | 59 +++++++++++++++++++----------------- source/core/gamecontrol.cpp | 43 ++++++++++++++++++++------ source/core/gamecontrol.h | 1 + 4 files changed, 67 insertions(+), 38 deletions(-) diff --git a/source/build/include/build.h b/source/build/include/build.h index 9ffd26e04..5accd465f 100644 --- a/source/build/include/build.h +++ b/source/build/include/build.h @@ -759,7 +759,7 @@ int32_t md_definehud (int32_t modelid, int32_t tilex, vec3f_t add, int32_t md_undefinetile(int32_t tile); int32_t md_undefinemodel(int32_t modelid); -int32_t loaddefinitionsfile(const char *fn, bool loadadds = false); +int32_t loaddefinitionsfile(const char *fn, bool loadadds = false, bool cumulative = false); // if loadboard() fails with -2 return, try loadoldboard(). if it fails with // -2, board is dodgy diff --git a/source/build/src/defs.cpp b/source/build/src/defs.cpp index c03f6fe29..3c35ff162 100644 --- a/source/build/src/defs.cpp +++ b/source/build/src/defs.cpp @@ -211,8 +211,6 @@ enum scripttoken_t }; static int32_t lastmodelid = -1, lastvoxid = -1, modelskin = -1, lastmodelskin = -1, seenframe = 0; -static char *faketilebuffer = NULL; -static int32_t faketilebuffersiz = 0; static const char *skyfaces[6] = { @@ -3169,37 +3167,44 @@ static int32_t defsparser(scriptfile *script) return 0; } - -int32_t loaddefinitionsfile(const char *fn, bool loadadds) +int32_t loaddefinitionsfile(const char *fn, bool loadadds, bool cumulative) { - scriptfile *script; - - script = scriptfile_fromfile(fn); - - if (script) + bool done = false; + auto parseit = [&](int lump) { - Printf(PRINT_NONOTIFY, "Loading \"%s\"\n",fn); + FScanner sc; + sc.OpenLumpNum(lump); + sc.SetNoOctals(true); + sc.SetNoFatalErrors(true); + defsparser(&sc); + done = true; + Printf(PRINT_NONOTIFY, "\n"); + }; - defsparser(script); + if (!cumulative) + { + int lump = fileSystem.FindFile(fn); + if (lump >= 0) + { + Printf(PRINT_NONOTIFY, "Loading \"%s\"\n", fn); + parseit(lump); + } + } + else + { + int lump, lastlump = 0; + while ((lump = fileSystem.FindLumpFullName(fn, &lastlump)) >= 0) + { + Printf(PRINT_NONOTIFY, "Loading \"%s\"\n", fileSystem.GetFileFullPath(lump)); + parseit(lump); + } } if (userConfig.AddDefs && loadadds) for (auto& m : *userConfig.AddDefs) { Printf("Loading module \"%s\"\n",m.GetChars()); - defsparser_include(m, NULL, NULL); // Q: should we let the external script see our symbol table? - } - - if (script) - scriptfile_close(script); - - DO_FREE_AND_NULL(faketilebuffer); - faketilebuffersiz = 0; - - if (!script) return -1; - - Printf(PRINT_NONOTIFY, "\n"); - - return 0; + defsparser_include(m, nullptr, nullptr); // Q: should we let the external script see our symbol table? + Printf(PRINT_NONOTIFY, "\n"); + } + return done ? 0 : -1; } - -// vim:ts=4: diff --git a/source/core/gamecontrol.cpp b/source/core/gamecontrol.cpp index e3f7d6926..c9d0f4dfb 100644 --- a/source/core/gamecontrol.cpp +++ b/source/core/gamecontrol.cpp @@ -350,7 +350,7 @@ void UserConfig::ProcessOptions() static const char* defs[] = { "-def", "-h", nullptr }; Args->CollectFiles("-def", defs, ".def"); - DefaultDef = Args->CheckValue("-def"); + UserDef = Args->CheckValue("-def"); if (DefaultCon.IsEmpty()) { @@ -1309,27 +1309,50 @@ void DrawCrosshair(int deftile, int health, double xdelta, double ydelta, double void LoadDefinitions() { - loaddefinitionsfile("engine/engine.def"); // Internal stuff that is required. - - const char* defsfile = G_DefFile(); - cycle_t deftimer; deftimer.Reset(); deftimer.Clock(); - if (!loaddefinitionsfile(defsfile, true)) + const char* loaded = nullptr; + + const char* defsfile = G_DefFile(); + FString razedefsfile = defsfile; + razedefsfile.Substitute(".def", "-raze.def"); + + loaddefinitionsfile("engine/engine.def", false); // Internal stuff that is required. + + // check what we have. + // user .defs override the default ones and are not cumulative. + // if we fine even one Raze-specific file, all of those will be loaded cumulatively. + // otherwise the default rules inherited from older ports apply. + if (userConfig.UserDef.IsNotEmpty()) + { + if (!loaddefinitionsfile(userConfig.UserDef, true, false)) loaded = userConfig.UserDef; + } + else + { + if (fileSystem.FileExists(razedefsfile)) + { + if (!loaddefinitionsfile(razedefsfile, true, true)) loaded = razedefsfile; + } + else + { + if (!loaddefinitionsfile(defsfile, true, false)) loaded = defsfile; + } + } + + if (loaded) { deftimer.Unclock(); - Printf(PRINT_NONOTIFY, "Definitions file \"%s\" loaded in %.3f ms.\n", defsfile, deftimer.TimeMS()); + DPrintf(DMSG_SPAMMY, "Definitions file \"%s\" loaded, %f ms.\n", loaded, deftimer.TimeMS()); } userConfig.AddDefs.reset(); - // load the widescreen replacements last so that they do not clobber the CRC for the original items so that mod-side replacement are picked up. + // load the widescreen replacements last. This ensures that mods still get the correct CRCs for their own tile replacements. if (fileSystem.FindFile("engine/widescreen.def") >= 0 && !Args->CheckParm("-nowidescreen")) { loaddefinitionsfile("engine/widescreen.def"); } - fileSystem.InitHashChains(); - + fileSystem.InitHashChains(); // make sure that any resources that got added can be found again. } bool M_Active() diff --git a/source/core/gamecontrol.h b/source/core/gamecontrol.h index 85242b2f7..8a1cd1685 100644 --- a/source/core/gamecontrol.h +++ b/source/core/gamecontrol.h @@ -65,6 +65,7 @@ struct UserConfig { FString gamegrp; FString CommandMap; + FString UserDef; FString DefaultDef; FString DefaultCon; FString CommandDemo;