summaryrefslogtreecommitdiff
path: root/src/puzzles/collection.c
blob: bb95ebad66fc9dbc59758ebaea319b438772fa4b (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
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
#include <dirent.h>
#include "puzzle.h"
#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "../utils/arena.h"
#include <string.h>

static const char *IMAGE_NAME = "icon.png";

CollectionElementType getTypeFromPath(char *fullPath) {
    struct stat st;
    if (stat(fullPath, &st) != 0) return COLLECTION_ELEMENT_UNKNOWN;

    if (S_ISDIR(st.st_mode)) {
        return COLLECTION_ELEMENT_COLLECTION;
    } else if (S_ISREG(st.st_mode)) {
        return COLLECTION_ELEMENT_PUZZLE;
    }
    return COLLECTION_ELEMENT_UNKNOWN;
}

bool isIcon(struct dirent *entry) {
    return strncmp(entry->d_name, IMAGE_NAME, strlen(IMAGE_NAME)) == 0;
}

void addIcon(CollectionElement *el, struct dirent *entry) {
    if (el == NULL || entry == NULL) return;
    char fullPath[1024];
}

bool isElement(struct dirent *entry, char *basePath) {
    if (entry == NULL) return false;
    if (strncmp(".", entry->d_name, 1) == 0) return false;
    if (isIcon(entry)) return false;

    char full_path_buf[1024];
    snprintf(full_path_buf, sizeof(full_path_buf), "%s/%s", basePath, entry->d_name);
    struct stat st;
    if (stat(full_path_buf, &st) != 0) return false;
    if (S_ISDIR(st.st_mode)) return true;

    size_t entry_name_len = strlen(entry->d_name);
    if (entry_name_len < 4) return false;
    char *last_bytes = entry->d_name + entry_name_len - 3;
    if (strncmp(last_bytes, ".pz", 3) != 0) return false;

    return true;
}

size_t getElementCount(DIR *dir, CollectionElement *el) {
    struct dirent *entry;
    size_t outp = 0;
    while ((entry = readdir(dir)) != NULL) {
        if (isElement(entry, el->path)) outp += 1;
    }
    rewinddir(dir);
    return outp;
}

void initCollectionFromDir(CollectionElement *el) {
    if (el == NULL) return;

    TraceLog(LOG_INFO, "Initializing a collection element from %s/", el->path);
    DIR *dir = opendir(el->path);
    if (dir == NULL) {
        TraceLog(LOG_ERROR, "Failed to open the directory");
        return;
    }

    el->childCount = getElementCount(dir, el);
    el->children = malloc(sizeof(CollectionElement) * el->childCount);
    // printf("element count: %zu\n", el->childCount);
    if (el->children == NULL) {
        TraceLog(LOG_ERROR, "Failed to allocate the collection element children array");
        closedir(dir);
        return;
    }

    struct dirent *entry;
    char full_path_buf[1024];
    size_t idx = 0;

    while ((entry = readdir(dir)) != NULL) {
        if (isIcon(entry)) {
            addIcon(el, entry);
            continue;
        }
        if (!isElement(entry, el->path)) continue;
        snprintf(full_path_buf, sizeof(full_path_buf), "%s/%s", el->path, entry->d_name);

        struct stat st;
        if (stat(full_path_buf, &st) != 0) continue;

        CollectionElement *read_el = &el->children[idx];
        // printf("idx = %zu\n", idx);
        if (S_ISDIR(st.st_mode)) {
            read_el->type = COLLECTION_ELEMENT_COLLECTION;
        } else if (S_ISREG(st.st_mode)) {
            read_el->type = COLLECTION_ELEMENT_PUZZLE;
        }
        read_el->IsInitialized = false;
        read_el->path = strdup(full_path_buf);

        idx += 1;
    }

    closedir(dir);
}

void InitializeCollection(CollectionElement *el) {
    if (el == NULL) {
        TraceLog(LOG_ERROR, "Failed to initialize collection element: the element is NULL");
        return;
    }
    switch (el->type) {
        case COLLECTION_ELEMENT_PUZZLE: break;
        case COLLECTION_ELEMENT_COLLECTION: 
                     initCollectionFromDir(el);
                     break;
        default:
                     TraceLog(LOG_WARNING, "Invalid CollectionElement type");
                     break;
    }
}