summaryrefslogtreecommitdiff
path: root/src/code/m_DLF.c
blob: 0bfb4e9e8358d1a715eb23c3a53318cba07f4fdf (plain)
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
#include "m_DLF.h"

// Function names taken from Zelda64 Decompilation

void Overlay_LoadGameState(GameStateOverlay* overlayEntry) {
    void* vramStart;

    if (overlayEntry->loadedRamAddr != NULL) {
        return;
    }

    vramStart = overlayEntry->vramStart;
    if (vramStart == NULL) {
        overlayEntry->unk_28 = 0;
        return;
    }

    overlayEntry->loadedRamAddr = malloc_r((uintptr_t)overlayEntry->vramEnd - (uintptr_t)vramStart);

    if (overlayEntry->loadedRamAddr != NULL) {
        ovlmgr_Load(overlayEntry->vromStart, overlayEntry->vromEnd, overlayEntry->vramStart, overlayEntry->vramEnd,
                    overlayEntry->loadedRamAddr);
        overlayEntry->unk_14 =
            (void*)((overlayEntry->unk_14 != NULL)
                        ? (void*)((uintptr_t)overlayEntry->unk_14 - (intptr_t)((uintptr_t)overlayEntry->vramStart -
                                                                               (uintptr_t)overlayEntry->loadedRamAddr))
                        : NULL);

        overlayEntry->init =
            (void*)((overlayEntry->init != NULL)
                        ? (void*)((uintptr_t)overlayEntry->init - (intptr_t)((uintptr_t)overlayEntry->vramStart -
                                                                             (uintptr_t)overlayEntry->loadedRamAddr))
                        : NULL);

        overlayEntry->destroy =
            (void*)((overlayEntry->destroy != NULL)
                        ? (void*)((uintptr_t)overlayEntry->destroy - (intptr_t)((uintptr_t)overlayEntry->vramStart -
                                                                                (uintptr_t)overlayEntry->loadedRamAddr))
                        : NULL);

        overlayEntry->unk_20 =
            (void*)((overlayEntry->unk_20 != NULL)
                        ? (void*)((uintptr_t)overlayEntry->unk_20 - (intptr_t)((uintptr_t)overlayEntry->vramStart -
                                                                               (uintptr_t)overlayEntry->loadedRamAddr))
                        : NULL);

        overlayEntry->unk_24 =
            (void*)((overlayEntry->unk_24 != NULL)
                        ? (void*)((uintptr_t)overlayEntry->unk_24 - (intptr_t)((uintptr_t)overlayEntry->vramStart -
                                                                               (uintptr_t)overlayEntry->loadedRamAddr))
                        : NULL);

        overlayEntry->unk_28 = 0;
    }
}

void Overlay_FreeGameState(GameStateOverlay* overlayEntry) {
    s32 var_v0;

    if (overlayEntry->loadedRamAddr != NULL) {
        var_v0 = (overlayEntry->unk_28 != 0) ? -1 : 0;
        if (var_v0 == 0) {
            overlayEntry->unk_14 =
                (void*)((overlayEntry->unk_14 != NULL) ? (void*)((uintptr_t)overlayEntry->unk_14 +
                                                                 (intptr_t)((uintptr_t)overlayEntry->vramStart -
                                                                            (uintptr_t)overlayEntry->loadedRamAddr))
                                                       : NULL);

            overlayEntry->init =
                (void*)((overlayEntry->init != NULL) ? (void*)((uintptr_t)overlayEntry->init +
                                                               (intptr_t)((uintptr_t)overlayEntry->vramStart -
                                                                          (uintptr_t)overlayEntry->loadedRamAddr))
                                                     : NULL);

            overlayEntry->destroy =
                (void*)((overlayEntry->destroy != NULL) ? (void*)((uintptr_t)overlayEntry->destroy +
                                                                  (intptr_t)((uintptr_t)overlayEntry->vramStart -
                                                                             (uintptr_t)overlayEntry->loadedRamAddr))
                                                        : NULL);

            overlayEntry->unk_20 =
                (void*)((overlayEntry->unk_20 != NULL) ? (void*)((uintptr_t)overlayEntry->unk_20 +
                                                                 (intptr_t)((uintptr_t)overlayEntry->vramStart -
                                                                            (uintptr_t)overlayEntry->loadedRamAddr))
                                                       : NULL);

            overlayEntry->unk_24 =
                (void*)((overlayEntry->unk_24 != NULL) ? (void*)((uintptr_t)overlayEntry->unk_24 +
                                                                 (intptr_t)((uintptr_t)overlayEntry->vramStart -
                                                                            (uintptr_t)overlayEntry->loadedRamAddr))
                                                       : NULL);

            free(overlayEntry->loadedRamAddr);
            overlayEntry->loadedRamAddr = NULL;
        }
    }
}