summaryrefslogtreecommitdiff
path: root/hercules/code/fileutils.py
blob: a0635d4095e81ab0e7c20cf7b4638b6ecf3558a4 (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
# -*- coding: utf8 -*-
#
# Copyright (C) 2014  Evol Online
# Author: Andrei Karas (4144)

import array
import os
import struct
import shutil

def readInt32(f):
    data = f.read(4)
    arr = array.array("I")
    arr.fromstring(data)
    return arr[0]

def readInt16(f):
    data = f.read(2)
    arr = array.array("H")
    arr.fromstring(data)
    return arr[0]

def readInt8(f):
    data = f.read(1)
    arr = array.array("B")
    arr.fromstring(data)
    return arr[0]

def readMapName(f):
    data = f.read(12)
    data = str(data)
    while data[-1] == '\x00':
        data = data[:-1]
    return data

def skipData(f, sz):
    f.read(sz)

def readData(f, sz):
    return f.read(sz)

def readFile(path):
    with open(path, "r") as f:
        return f.read()

def writeInt32(f, i):
    f.write(struct.pack("I", i))

def writeInt16(f, i):
    f.write(struct.pack("H", i))

def writeMapName(f, name):
    if len(name) > 12:
        name = name[:12]
    while len(name) < 12:
        name = name + '\x00'
    f.write(struct.pack("12s", name))

def writeData(f, data):
    f.write(data)

def copyFile(src, dst, name):
    shutil.copyfile(src + name, dst + name)

def saveFile(fileName, data):
    with open(fileName, "w") as w:
        w.write(data)

def makeDir(path):
    if not os.path.exists(path):
        os.makedirs(path)

def removeDir(path):
    if os.path.exists(path):
        shutil.rmtree(path)

def removeAllFiles(path):
    if os.path.exists(path):
        shutil.rmtree(path)

def findFileIn(names, dirs):
    for name in names:
        for path in dirs:
            if os.path.exists(path + name):
                return path
    return None