aboutsummaryrefslogtreecommitdiff
path: root/project/src/lib-rkg.c
blob: ae8063b122b33c596d3e31c82e1f2bd1a5f8c90a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

/***************************************************************************
 *                         _______ _______ _______                         *
 *                        |  ___  |____   |  ___  |                        *
 *                        | |   |_|    / /| |   |_|                        *
 *                        | |_____    / / | |_____                         *
 *                        |_____  |  / /  |_____  |                        *
 *                         _    | | / /    _    | |                        *
 *                        | |___| |/ /____| |___| |                        *
 *                        |_______|_______|_______|                        *
 *                                                                         *
 *                            Wiimms SZS Tools                             *
 *                          https://szs.wiimm.de/                          *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *   This file is part of the SZS project.                                 *
 *   Visit https://szs.wiimm.de/ for project details and sources.          *
 *                                                                         *
 *   Copyright (c) 2011-2023 by Dirk Clemens <wiimm@wiimm.de>              *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt       *
 *                                                                         *
 ***************************************************************************/

//#include <sys/types.h>
//#include <sys/stat.h>
//#include <unistd.h>
//#include <dirent.h>
//#include <math.h>

#include "lib-rkg.h"

//
///////////////////////////////////////////////////////////////////////////////
///////////////			ScanRawDataGHOST()		///////////////
///////////////////////////////////////////////////////////////////////////////

static u32 ScanTimeRKG ( const u8 * data )
{
    DASSERT(data);
    const u32 min = data[0] >> 1;
    const u32 sec = be16(data) >> 2 & 0x7f;
    return ( be16(data+1) & 0x3ff ) + 1000 * sec + 60000 * min;
}

//-----------------------------------------------------------------------------

static uint CalcFrameTime ( const u8 ** pptr, uint n )
{
    const u8 *ptr = *pptr;
    uint frames = 0;

    while ( n-- > 0 )
    {
	ptr++;			// skip data field
	frames += *ptr++;	// add frame count
    }
    *pptr = ptr;

    // subtract 4 sec (0xfd frames) for countdown, then convert to msec
    frames = frames < 0xf0 ? 0 : frames - 0xf0;
    return frames * 100000 / 5994;
}

//-----------------------------------------------------------------------------

enumError ScanRawDataGHOST
(
    rkg_info_t		* ri,		// RKG info
    bool		init_ri,	// true: initialize 'ri' first
    struct raw_data_t	* raw		// valid raw data
)
{
    DASSERT(ri);
    DASSERT(raw);

    if (init_ri)
	InitializeRKGInfo(ri);
    else
	ResetRKGInfo(ri);

    if ( raw->data_size < sizeof(rkg_head_t) )
    {
	ri->error = "too small";
	return ERR_WARNING;
    }

    rkg_head_t *rh = (rkg_head_t*)raw->data;
    if ( memcmp(&rh->magic,RKG_MAGIC,4) )
    {
	ri->error = "wrong magic";
	return ERR_WARNING;
    }

    ri->score = ScanTimeRKG(rh->score);
    ri->is_compressed = ( ntohs(rh->type) & 0x0800 ) != 0;
    ri->data_size = ntohs(rh->data_size);

    const u8 * data = raw->data + sizeof(*rh);
    uint alloced_size = 0;
    if (ri->is_compressed)
    {
	uint alloced_size = ri->data_size;
	u8 *dest = CALLOC(alloced_size,1);
	size_t written, csize = be32(data);

	enumError err = DecompressYAZ( data+0x14, csize,
		dest, alloced_size, &written, raw->fname, 1, false, 0 );
	if (err)
	{
	    FREE(dest);
	    ri->error = "decompression failed";
	    return ERR_WARNING;
	}

	data = dest;
	ri->data_size = written;
	if ( verbose > 1 )
	    HexDump16(stdout,0,0,data,ri->data_size);
    }

    ri->n_but   = be16(data);
    ri->n_dir   = be16(data+2);
    ri->n_trick = be16(data+4);

    const u8 *ptr = data+8;
    ri->time_but   = CalcFrameTime(&ptr,ri->n_but);
    ri->time_dir   = CalcFrameTime(&ptr,ri->n_dir);
    ri->time_trick = CalcFrameTime(&ptr,ri->n_trick);

    if (alloced_size)
	FREE((char*)data);
    return ERR_OK;
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////			    END				///////////////
///////////////////////////////////////////////////////////////////////////////