misc/libphyslayer/hwpacksmounter.c
author nemo
Sun, 24 Mar 2024 14:34:27 -0400
changeset 15996 19927b70588c
parent 15817 0defc1d3261d
permissions -rw-r--r--
merge the kick notice suppression
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8052
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
     1
#include <string.h>
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
     2
#include <stdio.h>
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
     3
#include <stdlib.h>
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
     4
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
     5
#include "hwpacksmounter.h"
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
     6
8119
257ffa847aa2 physfs: no carbon depency on osx, add cdecl attributes on extra functions
koda
parents: 8074
diff changeset
     7
PHYSFS_DECL void hedgewarsMountPackages()
8052
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
     8
{
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
     9
    char ** filesList = PHYSFS_enumerateFiles("/");
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    10
    char **i;
15817
0defc1d3261d Avoid crashing on physfs failure
alfadur
parents: 13395
diff changeset
    11
    
0defc1d3261d Avoid crashing on physfs failure
alfadur
parents: 13395
diff changeset
    12
    if (!filesList) return;
8052
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    13
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    14
    for (i = filesList; *i != NULL; i++)
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    15
    {
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    16
        char * fileName = *i;
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    17
        int fileNameLength = strlen(fileName);
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    18
        if (fileNameLength > 4)
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    19
            if (strcmp(fileName + fileNameLength - 4, ".hwp") == 0)
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    20
            {
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    21
                const char * dir = PHYSFS_getRealDir(fileName);
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    22
                if(dir)
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    23
                {
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    24
                    char * fullPath = (char *)malloc(strlen(dir) + fileNameLength + 2);
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    25
                    strcpy(fullPath, dir);
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    26
                    strcat(fullPath, "/");
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    27
                    strcat(fullPath, fileName);
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    28
8714
ab201a62d115 Prepend, not append
unc0rr
parents: 8524
diff changeset
    29
                    PHYSFS_mount(fullPath, NULL, 0);
8052
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    30
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    31
                    free(fullPath);
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    32
                }
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    33
            }
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    34
    }
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    35
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    36
    PHYSFS_freeList(filesList);
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents:
diff changeset
    37
}
8978
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    38
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    39
PHYSFS_DECL void hedgewarsMountPackage(char * fileName)
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    40
{
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    41
    int fileNameLength = strlen(fileName);
13393
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    42
    int dirLength = 0;
8978
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    43
    if (fileNameLength > 4)
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    44
        if (strcmp(fileName + fileNameLength - 4, ".hwp") == 0)
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    45
        {
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    46
            const char * dir = PHYSFS_getRealDir(fileName);
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    47
            if(dir)
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    48
            {
13393
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    49
				dirLength = strlen(dir);
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    50
				if (dirLength > 4)
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    51
				{
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    52
					if (strcmp(dir + dirLength - 4, ".hwp") == 0)
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    53
					{
13395
fa81f809fbb9 IFDEF for Phys 2.0 which has no mountHandle - result will just be no hwp in hwp on those systems, which would be rarely used and DLC only.
nemo
parents: 13393
diff changeset
    54
#if PHYSFS_VER_MAJOR > 2 || PHYSFS_VER_MINOR > 0
13393
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    55
						char * uniqName = (char *)malloc(strlen(dir) + fileNameLength + 2);
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    56
						strcpy(uniqName, dir);
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    57
						strcat(uniqName, ",");
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    58
						strcat(uniqName, fileName);
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    59
						PHYSFS_mountHandle(PHYSFS_openRead(fileName), uniqName, NULL, 0);
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    60
						free(uniqName);
13395
fa81f809fbb9 IFDEF for Phys 2.0 which has no mountHandle - result will just be no hwp in hwp on those systems, which would be rarely used and DLC only.
nemo
parents: 13393
diff changeset
    61
#endif
13393
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    62
					}
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    63
					else
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    64
					{
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    65
						char * fullPath = (char *)malloc(strlen(dir) + fileNameLength + 2);
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    66
						strcpy(fullPath, dir);
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    67
						strcat(fullPath, "/");
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    68
						strcat(fullPath, fileName);
8978
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    69
13393
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    70
						PHYSFS_mount(fullPath, NULL, 0);
8978
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    71
13393
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    72
						free(fullPath);
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    73
					}
ae5d6448c5be Make wrapping a lua and its sidecar actually work.
nemo
parents: 8978
diff changeset
    74
				}
8978
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    75
            }
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    76
        }
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8714
diff changeset
    77
}