Skip to content

Commit ecd9ef8

Browse files
committed
LoadFile should use standard size - poked ape stuff a little bit.
1 parent f33006a commit ecd9ef8

File tree

5 files changed

+117
-18
lines changed

5 files changed

+117
-18
lines changed

engine/ape/ape.cpp

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,74 @@
1-
// SPDX-License-Identifier: GPL-3.0-or-later
2-
// Copyright © 2020-2022 Mark E Sowden <[email protected]>
1+
/******************************************************************************
2+
Copyright © 1997-2001 Id Software, Inc.
3+
Copyright © 2020-2025 Mark E Sowden <[email protected]>
4+
5+
This program is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU General Public License
7+
as published by the Free Software Foundation; either version 2
8+
of the License, or (at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13+
14+
See the GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software
18+
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19+
******************************************************************************/
320

421
#include "ape.h"
22+
23+
//TODO: much of this is subject to change!!!!
24+
25+
bool chr::ApeInstance::Load( const std::string &filename )
26+
{
27+
void *ptr;
28+
29+
ssize_t size = FS_LoadFile( filename.c_str(), &ptr );
30+
if ( size == -1 || ptr == nullptr )
31+
{
32+
Com_Printf( "Failed to load ape file (%s)!\n", filename.c_str() );
33+
return false;
34+
}
35+
36+
bool success = Parse( ptr, size );
37+
if ( !success )
38+
{
39+
Com_Printf( "Failed to parse ape file (%s)!\n", filename.c_str() );
40+
}
41+
42+
FS_FreeFile( ptr );
43+
44+
return success;
45+
}
46+
47+
bool chr::ApeInstance::Parse( const void *ptr, size_t size )
48+
{
49+
struct ApeFileHeader
50+
{
51+
uint32_t magic;
52+
uint32_t unk0;
53+
};
54+
55+
ApeFileHeader *header = ( ApeFileHeader * ) ptr;
56+
57+
uint32_t magic = LittleLong( header->magic );
58+
if ( magic != 0x3D010000 )
59+
{
60+
Com_Printf( "Invalid magic for ape!\n" );
61+
return false;
62+
}
63+
64+
uint32_t unk0 = LittleLong( header->unk0 );
65+
if ( unk0 != 0xFFFFFFFF )
66+
{
67+
Com_Printf( "Secondary key is invalid for ape!\n" );
68+
return false;
69+
}
70+
71+
//TODO: the rest of it :)
72+
73+
return true;
74+
}

engine/ape/ape.h

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
// SPDX-License-Identifier: GPL-3.0-or-later
2-
// Copyright © 2020-2022 Mark E Sowden <[email protected]>
1+
/******************************************************************************
2+
Copyright © 1997-2001 Id Software, Inc.
3+
Copyright © 2020-2025 Mark E Sowden <[email protected]>
4+
5+
This program is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU General Public License
7+
as published by the Free Software Foundation; either version 2
8+
of the License, or (at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13+
14+
See the GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software
18+
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19+
******************************************************************************/
320

421
#pragma once
522

623
#include "../client/client.h"
724

8-
namespace chr::ape
25+
namespace chr
926
{
10-
enum class OpCode
27+
enum class ApeOpCode
1128
{
1229
OP_STARTSWITCH = 49,
1330
OP_THINKSWITCH = 50,
@@ -23,5 +40,15 @@ namespace chr::ape
2340
OP_STYLE = 87,
2441
};
2542

26-
static constexpr unsigned int MAGIC = 0x3D010000;
27-
}// namespace chr::ape
43+
class ApeInstance
44+
{
45+
public:
46+
ApeInstance();
47+
~ApeInstance();
48+
49+
bool Load( const std::string &filename );
50+
51+
private:
52+
bool Parse( const void *ptr, size_t size );
53+
};
54+
}// namespace chr

game/game.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ typedef struct
175175
void ( *DebugGraph )( float value, int color );
176176

177177
// File System
178-
int ( *LoadFile )( const char *path, void **buffer );
178+
ssize_t ( *LoadFile )( const char *path, void **buffer );
179179
void ( *FreeFile )( void *buffer );
180180
} game_import_t;
181181

qcommon/files.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct Package
8080
/**
8181
* Load a file from the given package, and decompress the data.
8282
*/
83-
void *LoadFile( const char *fileName, uint32_t *fileLength ) const
83+
void *LoadFile( const char *fileName, size_t *fileLength ) const
8484
{
8585
// first, ensure that it's actually in the package file table
8686
const Index *fileIndex = GetFileIndex( fileName );
@@ -343,7 +343,7 @@ Used for streaming data out of either a pak file or
343343
a seperate file.
344344
===========
345345
*/
346-
void *FS_FOpenFile( const char *filename, uint32_t *length )
346+
void *FS_FOpenFile( const char *filename, size_t *length )
347347
{
348348
// search through the path, one element at a time
349349
for ( searchpath_t *search = fs_searchpaths; search; search = search->next )
@@ -464,20 +464,22 @@ Filename are reletive to the quake search path
464464
a null buffer will just return the file length without loading
465465
============
466466
*/
467-
int FS_LoadFile( const char *path, void **buffer )
467+
ssize_t FS_LoadFile( const char *path, void **buffer )
468468
{
469469
char upath[ MAX_QPATH ];
470470
snprintf( upath, sizeof( upath ), "%s", path );
471471

472472
FS_CanonicalisePath( upath );
473473

474474
// look for it in the filesystem or pack files
475-
uint32_t length;
476-
void *buf = FS_FOpenFile( upath, &length );
475+
size_t length;
476+
void *buf = FS_FOpenFile( upath, &length );
477477
if ( buf == nullptr )
478478
{
479479
if ( buffer != nullptr )
480+
{
480481
*buffer = nullptr;
482+
}
481483

482484
return -1;
483485
}
@@ -830,8 +832,8 @@ static void ExtractCommand()
830832
Com_Printf( "Extracting from %s\n", i.second.mappedDir.c_str() );
831833
for ( const auto &j : i.second.indices )
832834
{
833-
unsigned int fileSize;
834-
void *p = i.second.LoadFile( j.name, &fileSize );
835+
size_t fileSize;
836+
void *p = i.second.LoadFile( j.name, &fileSize );
835837
if ( p == nullptr )
836838
{
837839
Com_Printf( "Failed to load %s\n", j.name );

qcommon/qcommon.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,9 +680,9 @@ void FS_InitFilesystem( void );
680680
void FS_SetGamedir( const char *dir );
681681
const char *FS_Gamedir( void );
682682
char *FS_NextPath( char *prevpath );
683-
void FS_ExecAutoexec( void );
683+
void FS_ExecAutoexec( void );
684684

685-
int FS_LoadFile( const char *path, void **buffer );
685+
ssize_t FS_LoadFile( const char *path, void **buffer );
686686
// a null buffer will just return the file length without loading
687687
// a -1 length is not present
688688

0 commit comments

Comments
 (0)