Skip to content

Add non-Windows backends to a few classes#105

Open
madebr wants to merge 14 commits intow3dhub:mainfrom
madebr:sdl3-things
Open

Add non-Windows backends to a few classes#105
madebr wants to merge 14 commits intow3dhub:mainfrom
madebr:sdl3-things

Conversation

@madebr
Copy link
Copy Markdown

@madebr madebr commented Mar 22, 2026

  • Use SDL3 for getting SDL3 tick count and frequency (this ended up mostly used by the test suite, which are not currently built)
  • Add alternative implementation to ResourceClass: instead of using Windows-only *.res files, embed resources using a code generator (see cmake/embed.{cmake,py})
  • Remove unused LaunchWebBrowser function
  • modeless dialogs were not used, so remove this ability
  • restore accelerators in renegade's resources
  • Remove unused obfuscation: the game is open source
  • Add SDL3 backend to opening, reading and writing files (this commit series first implements it using stdio, before switching it away with SDL3-specific symbols). The SDL3-based implementation is more powerful when other platforms enter the picture.
  • I added wdump and W3DShellExt also built a FileClass related class, I added those to the CMake project and removed some duplicated sources.

Copy link
Copy Markdown
Collaborator

@caseychaos1212 caseychaos1212 left a comment

Choose a reason for hiding this comment

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

CODEX review

Comment thread Code/wwlib/rawfile.cpp Outdated
Comment thread Code/wwlib/rawfile.cpp
Comment thread Code/wwlib/rawfile.cpp Outdated
Comment thread Code/wwlib/rawfile.cpp
Copy link
Copy Markdown
Collaborator

@caseychaos1212 caseychaos1212 left a comment

Choose a reason for hiding this comment

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

CODEX:

Findings

High: In rawfile.cpp (line 442) the SDL3 READ|WRITE path uses SDL_IOFromFile(..., "wb+"), which truncates the file immediately. Existing callers such as tagblock.cpp (line 133) still open persistent files with Open(fname, READ|WRITE) and then read headers back, so this changes an in-place update path into silent data loss.

Medium: verchk.cpp (line 105) no longer reads the file creation time at all; it converts Get_Date_Time() instead, and the FAT day decode at verchk.cpp (line 110) is wrong ((fatDate >> 5) & 0x1f). The DATE field emitted from GameResSend.cpp (line 106) can therefore report an incorrect build timestamp.

Medium: The new SDL3 resource API tells callers to create static StaticResourceFileClass objects in rcfile.h (line 107), but those constructors register into a namespace-scope global map defined in another translation unit at rcfile.cpp (line 50). Cross-TU static initialization order is undefined, so this can fail nondeterministically before main().

Comment thread Code/wwlib/verchk.cpp
Comment on lines +105 to +114
unsigned int dateTime = file->Get_Date_Time();
unsigned int fatDate = dateTime >> 16;
unsigned int fatTime = dateTime & 0xffff;
createTime->year = 1980 + (fatDate >> 9);
createTime->month = (fatDate >> 5) & 0xf;
createTime->day = (fatDate >> 5) & 0x1f;
createTime->hour = fatTime >> 11;
createTime->minute = (fatTime >> 5) & 0x3f;
createTime->second = 2 * (fatTime & 0x1f);
return true;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Medium: verchk.cpp (line 105) no longer reads the file creation time at all; it converts Get_Date_Time() instead, and the FAT day decode at verchk.cpp (line 110) is wrong ((fatDate >> 5) & 0x1f). The DATE field emitted from GameResSend.cpp (line 106) can therefore report an incorrect build timestamp.

Comment thread Code/wwlib/rcfile.h Outdated
Comment on lines +107 to +116
StaticResourceFileClass(const char *filename, const void *data, size_t size)
: m_filename(filename)
, m_data(data)
, m_size(size) {
Register();
}

~StaticResourceFileClass() {
Unregister();
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Medium: The new SDL3 resource API tells callers to create static StaticResourceFileClass objects in rcfile.h (line 107), but those constructors register into a namespace-scope global map defined in another translation unit at rcfile.cpp (line 50). Cross-TU static initialization order is undefined, so this can fail nondeterministically before main().

Comment thread Code/wwlib/rawfile.cpp
Comment thread Code/Tools/W3DShellExt/CMakeLists.txt
Comment thread Code/wwlib/rawfile.cpp Outdated
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
#elif defined(OPENW3D_SDL3)
Handle = fopen(Filename, "a+");
Handle = SDL_IOFromFile(Filename, "a+");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This does not preserve the Win32 OPEN_ALWAYS behavior. Per SDL’s SDL_IOFromFile docs, a+ means all writes are forced to EOF even after seeking, so this is not safe for callers that do random-access updates. For example, TagBlockFile::Save_Header() seeks back to offset 0 and rewrites the header; with append mode, that write will go to the end of the file instead of overwriting the existing header.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Changed it to

Handle = SDL_IOFromFile(Filename, "r+");
if (Handle == nullptr) {
    Handle = SDL_IOFromFile(Filename, "w+");
}

Needs a review from a real human.

Dadud pushed a commit to Dadud/OpenW3D that referenced this pull request Apr 22, 2026
High: rawfile.cpp READ|WRITE path no longer truncates existing files.
  - Changed SDL3 path from 'r+' fallback to 'r+b' then 'w+b', matching
    OPEN_ALWAYS semantics (preserve existing file, create if absent).

Medium: verchk.cpp FAT date month field decode corrected.
  - Mask was 0xf (4 bits), should be 0x1f (5 bits) for FAT month field.

Medium: rcfile.cpp static init order fix (cross-TU).
  - Replaced 'extern std::unordered_map Static_Resources' with an inline
    function GetStaticResources() using a function-local static (Meyers
    singleton), guaranteeing construction on first use.
  - cmake/embed.py generator updated.
Comment thread cmake/embed.py
sources = {}
varnames = set()
for arg_src in args.sources:
match arg_src.rsplit("@", 1):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I get an invalid synatax error here on the version of python I happened to have locally here (Python 3.9)

bool b_placeholder;

Set_Port( packet.Get(i_placeholder));
Set_Port( packet.Get(i_placeholder));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Unintended whitespace change?

Comment thread Code/wwlib/crc.cpp
0xBAD03605L, 0xCDD70693L, 0x54DE5729L, 0x23D967BFL,
0xB3667A2EL, 0xC4614AB8L, 0x5D681B02L, 0x2A6F2B94L,
0xB40BBE37L, 0xC30C8EA1L, 0x5A05DF1BL, 0x2D02EF8DL
0x00000000U, 0x77073096U, 0xEE0E612CU, 0x990951BAU,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Probably don't need the U as hex is treated as unsigned by default, just an observation though.

Comment thread Code/wwlib/rawfile.cpp
Handle = CreateFileA(Filename, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
#elif defined(OPENW3D_SDL3)
Handle = SDL_IOFromFile(Filename, "r+");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You use r+ here and w+ below. However you have rb and wb earlier in the function, don't you need b here as well to open as binary?

Comment thread Code/wwlib/rawfile.cpp
return(false);
}
#elif defined(OPENW3D_SDL3)
deleteok=SDL_RemovePath(Filename)?true:false;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

SDL_RemovePath already returns bool, isn't the ternary a bit overkill?

@Dadud
Copy link
Copy Markdown

Dadud commented Apr 22, 2026

Good catch on the Python 3.9 issue in cmake/embed.py.

The syntax error comes from the match / case block, which requires Python 3.10+.

A Python 3.9-safe equivalent is:

parts = arg_src.rsplit("@", 1)
if len(parts) == 1:
    p = parts[0]
    path = pathlib.Path(p)
    name = path.name
else:
    p, name = parts
    path = pathlib.Path(p)
name = name.lower()

I tested that replacement locally and it works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants