forked from UltimaPHP/UltimaPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.class.php
145 lines (117 loc) · 3.78 KB
/
reader.class.php
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Ultima PHP - OpenSource Ultima Online Server written in PHP
* Version: 0.1 - Pre Alpha
*/
class Reader {
const FILE_STRING = 0x00;
const FILE_TILEDATA = 0x01;
const FILE_MAP_FILE = 0x02;
const FILE_MAP_DIF = 0x03;
const FILE_STATIC_INDEX = 0x04;
const FILE_STATIC_FILE = 0x05;
const FILE_TEXTURE_INDEX = 0x06;
const FILE_TEXTURE_FILE = 0x07;
/* Class variables */
public $fileType;
public $fileHolder;
public $fileLength;
public $fileHash;
public function __construct($filePath = null, $type = self::FILE_STRING) {
$this->fileType = $type;
$this->fileHolder = fopen($filePath, "rb");
$this->fileLength = filesize($filePath);
$this->fileHash = Cache::getFileHash($filePath);
return $this;
}
public function getAcutalPosition() {
return ftell($this->fileHolder);
}
public function read($length = 0) {
return fread($this->fileHolder, $length);
}
public function setPosition($offset = 0) {
return fseek($this->fileHolder, $offset);
}
public function readUInt8() {
return self::uInt8(fread($this->fileHolder, 1));
}
public function readInt8() {
return self::int8(fread($this->fileHolder, 1));
}
public function readUInt16() {
return self::uInt16(fread($this->fileHolder, 2));
}
public function readInt16() {
return self::int16(fread($this->fileHolder, 2));
}
public function readUInt32() {
return self::uInt32(fread($this->fileHolder, 4));
}
public function readInt32() {
return self::int32(fread($this->fileHolder, 4));
}
public function readUInt64() {
return self::uInt64(fread($this->fileHolder, 8));
}
public function readInt64() {
return self::int64(fread($this->fileHolder, 8));
}
public static function int8($i) {
return is_int($i) ? pack("c", $i) : unpack("c", $i)[1];
}
public static function uInt8($i) {
return is_int($i) ? pack("C", $i) : unpack("C", $i)[1];
}
public static function int16($i) {
return is_int($i) ? pack("s", $i) : unpack("s", $i)[1];
}
public static function uInt16($i, $endianness = false) {
$f = is_int($i) ? "pack" : "unpack";
if ($endianness === true) {
// big-endian
$i = $f("n", $i);
} else if ($endianness === false) {
// little-endian
$i = $f("v", $i);
} else if ($endianness === null) {
// machine byte order
$i = $f("S", $i);
}
return is_array($i) ? $i[1] : $i;
}
public static function int32($i) {
return is_int($i) ? pack("l", $i) : unpack("l", $i)[1];
}
public static function uInt32($i, $endianness = false) {
$f = is_int($i) ? "pack" : "unpack";
if ($endianness === true) {
// big-endian
$i = $f("N", $i);
} else if ($endianness === false) {
// little-endian
$i = $f("V", $i);
} else if ($endianness === null) {
// machine byte order
$i = $f("L", $i);
}
return is_array($i) ? $i[1] : $i;
}
public static function int64($i) {
return is_int($i) ? pack("q", $i) : unpack("q", $i)[1];
}
public static function uInt64($i, $endianness = false) {
$f = is_int($i) ? "pack" : "unpack";
if ($endianness === true) {
// big-endian
$i = $f("J", $i);
} else if ($endianness === false) {
// little-endian
$i = $f("P", $i);
} else if ($endianness === null) {
// machine byte order
$i = $f("Q", $i);
}
return is_array($i) ? $i[1] : $i;
}
}