Subversion Repositories eduke32

Rev

Rev 4948 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4948 Rev 4990
Line 42... Line 42...
42
//  back_p==1: "small int" -> ptr
42
//  back_p==1: "small int" -> ptr
43
//
43
//
44
//  mode: see enum in savegame.h
44
//  mode: see enum in savegame.h
45
void G_Util_PtrToIdx(void *ptr, int32_t count, const void *base, int32_t mode)
45
void G_Util_PtrToIdx(void *ptr, int32_t count, const void *base, int32_t mode)
46
{
46
{
47
    int32_t i;
-
 
48
    intptr_t *iptr = (intptr_t *)ptr;
47
    intptr_t *iptr = (intptr_t *)ptr;
49
    intptr_t ibase = (intptr_t)base;
48
    intptr_t const ibase = (intptr_t)base;
50
    int32_t back_p = mode&P2I_BACK_BIT;
-
 
51
    int32_t onlynon0_p = mode&P2I_ONLYNON0_BIT;
49
    int32_t const onlynon0_p = mode&P2I_ONLYNON0_BIT;
52
50
53
    // TODO: convert to proper offsets/indices for (a step towards) cross-
51
    // TODO: convert to proper offsets/indices for (a step towards) cross-
54
    //       compatibility between 32- and 64-bit systems in the netplay.
52
    //       compatibility between 32- and 64-bit systems in the netplay.
55
    //       REMEMBER to bump BYTEVERSION then.
53
    //       REMEMBER to bump BYTEVERSION then.
56
54
57
    for (i=0; i<count; i++)
-
 
58
        // WARNING: C std doesn't say that bit pattern of NULL is necessarily 0!
55
    // WARNING: C std doesn't say that bit pattern of NULL is necessarily 0!
59
        if (!onlynon0_p || iptr[i])
56
    if ((mode & P2I_BACK_BIT) == 0)
60
        {
57
    {
-
 
58
        for (int i = 0; i < count; i++)
61
            if (!back_p)
59
            if (!onlynon0_p || iptr[i])
62
                iptr[i] -= ibase;
60
                iptr[i] -= ibase;
-
 
61
    }
63
            else
62
    else
-
 
63
    {
-
 
64
        for (int i = 0; i < count; i++)
-
 
65
            if (!onlynon0_p || iptr[i])
64
                iptr[i] += ibase;
66
                iptr[i] += ibase;
65
        }
67
    }
66
}
68
}
67
69
68
void G_Util_PtrToIdx2(void *ptr, int32_t count, size_t stride, const void *base, int32_t mode)
70
void G_Util_PtrToIdx2(void *ptr, int32_t count, size_t stride, const void *base, int32_t mode)
69
{
71
{
70
    int32_t i;
-
 
71
    uint8_t *iptr = (uint8_t *)ptr;
72
    uint8_t *iptr = (uint8_t *)ptr;
72
    intptr_t ibase = (intptr_t)base;
73
    intptr_t const ibase = (intptr_t)base;
73
    int32_t back_p = mode&P2I_BACK_BIT;
-
 
74
    int32_t onlynon0_p = mode&P2I_ONLYNON0_BIT;
74
    int32_t const onlynon0_p = mode&P2I_ONLYNON0_BIT;
75
75
76
    for (i=0; i<count; i++)
76
    if ((mode & P2I_BACK_BIT) == 0)
77
    {
77
    {
78
        if (!onlynon0_p || *(intptr_t *)iptr)
78
        for (int i = 0; i < count; ++i)
79
        {
79
        {
80
            if (!back_p)
80
            if (!onlynon0_p || *(intptr_t *)iptr)
81
                *(intptr_t *)iptr -= ibase;
81
                *(intptr_t *)iptr -= ibase;
-
 
82
-
 
83
            iptr += stride;
-
 
84
        }
-
 
85
    }
82
            else
86
    else
-
 
87
    {
-
 
88
        for (int i = 0; i < count; ++i)
-
 
89
        {
-
 
90
            if (!onlynon0_p || *(intptr_t *)iptr)
83
                *(intptr_t *)iptr += ibase;
91
                *(intptr_t *)iptr += ibase;
84
        }
-
 
85
92
86
        iptr += stride;
93
            iptr += stride;
87
    }
94
        }
88
}
95
    }
-
 
96
}
89
97
90
// TODO: sync with TROR special interpolations? (e.g. upper floor of subway)
98
// TODO: sync with TROR special interpolations? (e.g. upper floor of subway)
91
void G_ResetInterpolations(void)
99
void G_ResetInterpolations(void)
92
{
100
{
93
    int32_t k, i;
101
    int32_t k, i;
Line 354... Line 362...
354
    lockclock = g_timers.lockclock;
362
    lockclock = g_timers.lockclock;
355
}
363
}
356
364
357
//////////
365
//////////
358
366
-
 
367
#ifdef __ANDROID__
-
 
368
static void G_SavePalette(void)
-
 
369
{
-
 
370
    int32_t pfil;
-
 
371
-
 
372
    if ((pfil = kopen4load("palette.dat", 0)) != -1)
-
 
373
    {
-
 
374
        int len = kfilelength(pfil);
-
 
375
-
 
376
        FILE *fil = fopen("palette.dat", "rb");
-
 
377
-
 
378
        if (!fil)
-
 
379
        {
-
 
380
            fil = fopen("palette.dat", "wb");
-
 
381
-
 
382
            if (fil)
-
 
383
            {
-
 
384
                char *buf = (char *) Xaligned_alloc(16, len);
-
 
385
-
 
386
                kread(pfil, buf, len);
-
 
387
                fwrite(buf, len, 1, fil);
-
 
388
                fclose(fil);
-
 
389
            }
-
 
390
        }
-
 
391
        else fclose(fil);
-
 
392
    }
-
 
393
}
-
 
394
#endif
-
 
395
359
int32_t G_SavePlayer(int32_t spot)
396
int32_t G_SavePlayer(int32_t spot)
360
{
397
{
361
    char fn[16];
398
    char fn[16];
362
//    char mpfn[16];
-
 
363
    FILE *fil;
399
    FILE *fil;
364
400
-
 
401
#ifdef __ANDROID__
-
 
402
    G_SavePalette();
-
 
403
#endif
-
 
404
365
    Bassert(spot < MAXSAVEGAMES);
405
    Bassert(spot < MAXSAVEGAMES);
366
406
367
    G_SaveTimers();
407
    G_SaveTimers();
368
408
369
    Bstrcpy(fn, "dukesav0.esv");
409
    Bstrcpy(fn, "dukesav0.esv");
Line 491... Line 531...
491
#define DS_PROTECTFN 512
531
#define DS_PROTECTFN 512
492
#define DS_END (0x70000000)
532
#define DS_END (0x70000000)
493
533
494
static int32_t ds_getcnt(const dataspec_t *sp)
534
static int32_t ds_getcnt(const dataspec_t *sp)
495
{
535
{
-
 
536
    int rv = -1;
-
 
537
496
    switch (sp->flags&DS_CNTMASK)
538
    switch (sp->flags & DS_CNTMASK)
497
    {
539
    {
498
    case 0: return sp->cnt;
540
        case 0: rv = sp->cnt; break;
499
    case DS_CNT16: return *((int16_t *)sp->cnt);
541
        case DS_CNT16: rv = *((int16_t *)sp->cnt); break;
500
    case DS_CNT32: return *((int32_t *)sp->cnt);
542
        case DS_CNT32: rv = *((int32_t *)sp->cnt); break;
501
    default: return -1;
-
 
502
    }
543
    }
-
 
544
-
 
545
    return rv;
503
}
546
}
504
547
505
static void ds_get(const dataspec_t *sp, void **ptr, int32_t *cnt)
548
static inline void ds_get(const dataspec_t *sp, void **ptr, int32_t *cnt)
506
{
549
{
507
    *cnt = ds_getcnt(sp);
550
    *cnt = ds_getcnt(sp);
508
-
 
509
    if (sp->flags&DS_DYNAMIC)
-
 
510
        *ptr = *((void **)sp->ptr);
551
    *ptr = (sp->flags&DS_DYNAMIC) ? *((void **)sp->ptr) : sp->ptr;
511
    else
-
 
512
        *ptr = sp->ptr;
-
 
513
}
552
}
514
553
515
// write state to file and/or to dump
554
// write state to file and/or to dump
516
static uint8_t *writespecdata(const dataspec_t *spec, FILE *fil, uint8_t *dump)
555
static uint8_t *writespecdata(const dataspec_t *spec, FILE *fil, uint8_t *dump)
517
{
556
{
Line 1174... Line 1213...
1174
}
1213
}
1175
#endif
1214
#endif
1176
1215
1177
void sv_freemem()
1216
void sv_freemem()
1178
{
1217
{
1179
    if (svsnapshot)
1218
    DO_FREE_AND_NULL(svsnapshot);
1180
        Bfree(svsnapshot), svsnapshot=NULL;
-
 
1181
    if (svinitsnap)
1219
    DO_FREE_AND_NULL(svinitsnap);
1182
        Bfree(svinitsnap), svinitsnap=NULL;
-
 
1183
    if (svdiff)
-
 
1184
        Bfree(svdiff), svdiff=NULL;
1220
    DO_FREE_AND_NULL(svdiff);
1185
}
1221
}
1186
1222
1187
static void SV_AllocSnap(int32_t allocinit)
1223
static void SV_AllocSnap(int32_t allocinit)
1188
{
1224
{
1189
    sv_freemem();
1225
    sv_freemem();
Line 1193... Line 1229...
1193
        svinitsnap = (uint8_t *)Xmalloc(svsnapsiz);
1229
        svinitsnap = (uint8_t *)Xmalloc(svsnapsiz);
1194
    svdiffsiz = svsnapsiz;  // theoretically it's less than could be needed in the worst case, but practically it's overkill
1230
    svdiffsiz = svsnapsiz;  // theoretically it's less than could be needed in the worst case, but practically it's overkill
1195
    svdiff = (uint8_t *)Xmalloc(svdiffsiz);
1231
    svdiff = (uint8_t *)Xmalloc(svdiffsiz);
1196
}
1232
}
1197
1233
1198
-
 
1199
EDUKE32_STATIC_ASSERT(sizeof(savehead_t) == 310);
1234
EDUKE32_STATIC_ASSERT(sizeof(savehead_t) == SAVEHEAD_SIZE);
1200
1235
1201
// make snapshot only if spot < 0 (demo)
1236
// make snapshot only if spot < 0 (demo)
1202
int32_t sv_saveandmakesnapshot(FILE *fil, int8_t spot, int8_t recdiffsp, int8_t diffcompress, int8_t synccompress)
1237
int32_t sv_saveandmakesnapshot(FILE *fil, int8_t spot, int8_t recdiffsp, int8_t diffcompress, int8_t synccompress)
1203
{
1238
{
1204
    savehead_t h;
1239
    savehead_t h;
Line 1257... Line 1292...
1257
1292
1258
    if ((unsigned)spot < MAXSAVEGAMES)
1293
    if ((unsigned)spot < MAXSAVEGAMES)
1259
    {
1294
    {
1260
        // savegame
1295
        // savegame
1261
        Bstrncpyz(h.savename, ud.savegame[spot], sizeof(h.savename));
1296
        Bstrncpyz(h.savename, ud.savegame[spot], sizeof(h.savename));
-
 
1297
#ifdef __ANDROID__
-
 
1298
        Bstrncpyz(h.volname, EpisodeNames[ud.volume_number], sizeof(h.volname));
-
 
1299
        Bstrncpyz(h.skillname, SkillNames[ud.player_skill], sizeof(h.skillname));
-
 
1300
#endif
1262
    }
1301
    }
1263
    else
1302
    else
1264
    {
1303
    {
1265
        // demo
1304
        // demo
1266
1305
Line 1330... Line 1369...
1330
    g_oldverSavegame[spot] = 0;
1369
    g_oldverSavegame[spot] = 0;
1331
1370
1332
    return 0;
1371
    return 0;
1333
}
1372
}
1334
1373
1335
-
 
1336
EDUKE32_STATIC_ASSERT(sizeof(savehead_t) == 310);
1374
EDUKE32_STATIC_ASSERT(sizeof(savehead_t) == SAVEHEAD_SIZE);
1337
1375
1338
// if file is not an EDuke32 savegame/demo, h->headerstr will be all zeros
1376
// if file is not an EDuke32 savegame/demo, h->headerstr will be all zeros
1339
int32_t sv_loadheader(int32_t fil, int32_t spot, savehead_t *h)
1377
int32_t sv_loadheader(int32_t fil, int32_t spot, savehead_t *h)
1340
{
1378
{
1341
    int32_t havedemo = (spot < 0);
1379
    int32_t havedemo = (spot < 0);
Line 2114... Line 2152...
2114
        actor[i].lightId = -1;
2152
        actor[i].lightId = -1;
2115
    }
2153
    }
2116
}
2154
}
2117
2155
2118
////////// END GENERIC SAVING/LOADING SYSTEM //////////
2156
////////// END GENERIC SAVING/LOADING SYSTEM //////////
-
 
2157
-
 
2158
#ifdef __ANDROID__
-
 
2159
#include <jni.h>
-
 
2160
#include <android/log.h>
-
 
2161
-
 
2162
#ifndef LOGI
-
 
2163
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO,"DUKE", __VA_ARGS__))
-
 
2164
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "DUKE", __VA_ARGS__))
-
 
2165
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR,"DUKE", __VA_ARGS__))
-
 
2166
#endif
-
 
2167
-
 
2168
char const * G_GetStringFromSavegame(const char *filename, int type)
-
 
2169
{
-
 
2170
    LOGI("getSavegameText %s", filename);
-
 
2171
    int32_t fil = kopen4load(filename, 0);
-
 
2172
-
 
2173
    if (fil == -1)
-
 
2174
    {
-
 
2175
        LOGE("couldn't load %s", filename);
-
 
2176
        return "";
-
 
2177
    }
-
 
2178
-
 
2179
    savehead_t saveh;
-
 
2180
-
 
2181
    int32_t i = sv_loadheader(fil, 0, &saveh);
-
 
2182
-
 
2183
    if (i && (i != 2 && i != 3))
-
 
2184
        goto corrupt;
-
 
2185
-
 
2186
    kclose(fil);
-
 
2187
-
 
2188
    static char tempbuf[64];
-
 
2189
-
 
2190
    switch (type)
-
 
2191
    {
-
 
2192
        case 0: Bstrncpyz(tempbuf, saveh.savename, sizeof(saveh.savename) - 1); break;
-
 
2193
        case 1: Bstrncpyz(tempbuf, saveh.volname, sizeof(saveh.volname) - 1); break;
-
 
2194
        case 2: Bstrncpyz(tempbuf, saveh.skillname, sizeof(saveh.skillname) - 1); break;
-
 
2195
    }
-
 
2196
    return tempbuf;
-
 
2197
-
 
2198
corrupt:
-
 
2199
    kclose(fil);
-
 
2200
    LOGE("couldn't load %s", filename);
-
 
2201
    return "";
-
 
2202
}
-
 
2203
-
 
2204
int32_t G_GetScreenshotFromSavegame(const char *filename, char *pal, char *data)
-
 
2205
{
-
 
2206
    LOGI("getSavegameScreenshot %s", filename);
-
 
2207
-
 
2208
    int32_t fil = kopen4load(filename, 0);
-
 
2209
-
 
2210
    if (fil == -1)
-
 
2211
        return -1;
-
 
2212
-
 
2213
    savehead_t saveh;
-
 
2214
-
 
2215
    int32_t i = sv_loadheader(fil, 0, &saveh);
-
 
2216
-
 
2217
    if (i && (i != 2 && i != 3))
-
 
2218
        goto corrupt;
-
 
2219
-
 
2220
    int32_t screenshotofs;
-
 
2221
-
 
2222
    if (kread(fil, &screenshotofs, 4) != 4)
-
 
2223
        goto corrupt;
-
 
2224
-
 
2225
    if (screenshotofs)
-
 
2226
    {
-
 
2227
        if (kdfread(data, 320, 200, fil) != 200)
-
 
2228
        {
-
 
2229
            // OSD_Printf("G_LoadSaveHeaderNew(%d): failed reading screenshot\n", spot);
-
 
2230
            goto corrupt;
-
 
2231
        }
-
 
2232
    }
-
 
2233
    else
-
 
2234
    {
-
 
2235
        kclose(fil);
-
 
2236
        return -1;
-
 
2237
    }
-
 
2238
-
 
2239
    kclose(fil);
-
 
2240
-
 
2241
    char pstr[BMAX_PATH];
-
 
2242
-
 
2243
    Bstrcpy(pstr, filename);
-
 
2244
    Bcorrectfilename(pstr, 1);
-
 
2245
    Bstrcat(pstr, "palette.dat");
-
 
2246
-
 
2247
    int32_t pfil;
-
 
2248
-
 
2249
    if ((pfil = kopen4load(pstr, 0)) == -1)
-
 
2250
    {
-
 
2251
        LOGE("couldn't load %s", pstr);
-
 
2252
        return -1;
-
 
2253
    }
-
 
2254
-
 
2255
    kread(pfil, pal, 768);
-
 
2256
    kclose(pfil);
-
 
2257
-
 
2258
    return 0;
-
 
2259
-
 
2260
corrupt:
-
 
2261
    kclose(fil);
-
 
2262
    return 1;
-
 
2263
}
-
 
2264
#endif