GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 2cb87f...108f3b )
by Telyn
01:55
created

COBAgentBlock::Compile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
require_once(dirname(__FILE__).'/COBBlock.php');
4
require_once(dirname(__FILE__).'/../../sprites/SpriteFrame.php');
5
require_once(dirname(__FILE__).'/../../sprites/S16Frame.php');
6
require_once(dirname(__FILE__).'/../../sprites/SPRFrame.php');
7
8
/**
9
 * @relates COBAgentBlock
10
 * @name Dependency Types
11
 * The two types of dependency available to C1/C2 COBs
12
 */
13
//@{
14
/** Sprite dependency - 'sprite' */
15
define('DEPENDENCY_SPRITE', 'sprite');
16
/** Sound dependency - 'sound' */
17
define('DEPENDENCY_SOUND', 'sound');
18
//@}
19
20
21
/// @brief COB Agent Block for C1 and C2
22
/**
23
 * For Creatures 1, this block contains all the useful data in a typical COB and will be the only block.\n
24
 * For Creatures 2, this block contains the scripts and metadata about the actual object.
25
 */
26
class COBAgentBlock extends COBBlock {
27
    /// @cond INTERNAL_DOCS
28
29
    private $agentName;
30
    private $agentDescription;
31
32
    private $lastUsageDate; //unix timestamp
33
    private $reuseInterval; // seconds
34
    private $quantityAvailable;
35
    private $expiryDate; //unix timestamp
36
37
    //'reserved' - never officially used by CL
38
    private $reserved1;
39
    private $reserved2;
40
    private $reserved3;
41
42
    private $dependencies = array();
43
    private $thumbnail; // SpriteFrame
44
45
    private $installScript;
46
    private $removeScript;
47
    private $eventScripts;
48
    /// @endcond
49
50
    /// @brief Initialises a new COBAgentBlock with the given name and description. 
51
    /** As defaults can be made for everything else these are the only non-optional
52
     * parts of a COB file in my opinion. Even then they could just be '' if you
53
     * really felt like it. 
54
     * @param $agentName The name of the agent (as displayed in the C2 injector)
55
     * @param $agentDescription The description of the agent (as displayed in the C2 injector)
56
     */
57
    public function COBAgentBlock($agentName, $agentDescription) {
58
        parent::COBBlock(COB_BLOCK_AGENT);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (COBBlock() instead of COBAgentBlock()). Are you sure this is correct? If so, you might want to change this to $this->COBBlock().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
59
        $this->agentName = $agentName;
60
        $this->agentDescription = $agentDescription;
61
    }
62
63
    /// @brief Supposedly compiles the block into binary. Throws an error to say it's not implemented.
64
    /** @return string
65
     */
66
    public function Compile() {
67
        // TODO: implement
68
        throw new Exception("COBAgentBlock::Compile not implemented");
69
    }
70
71
    /// @brief Gets the agent's name 
72
    /** @return string
73
     */
74
    public function GetAgentName() {
75
        return $this->agentName;
76
    }
77
    /// @brief Gets the agent's description
78
    /** @return string
79
     */
80
    public function GetAgentDescription() {
81
        return $this->agentDescription;
82
    }
83
    /// @brief Gets the agent's install script
84
    /** @return string
85
     */
86
    public function GetInstallScript() {
87
        return $this->installScript;
88
    }
89
    /// @brief Gets the agent's remove script
90
    /** @return string
91
     */
92
    public function GetRemoveScript() {
93
        return $this->removeScript;
94
    }
95
    /// @brief Gets the number of event scripts
96
    /** @return int
97
     */
98
    public function GetEventScriptCount() {
99
        return sizeof($this->eventScripts);
100
    }
101
    /// @brief Gets the agent's event scripts
102
    /** @return array of strings, each string is an event script
103
     */
104
    public function GetEventScripts() {
105
        return $this->eventScripts;
106
    }
107
    /// @brief Gets an event script
108
    /** Event scripts are not necessarily in any order, so you have to work out what each script is for yourself.
109
     * @param $whichScript Which script to get.
110
     * @return A string containing the event script. Each line is seperated by a comma I think.
111
     */
112
    public function GetEventScript($whichScript) {
113
        return $this->eventScripts[$whichScript];
114
    }
115
    /// @brief Gets the thumbnail of this agent as would be shown in the Injector
116
    /** @return SpriteFrame SpriteFrame of the thumbnail
117
     */
118
    public function GetThumbnail() {
119
        return $this->thumbnail;
120
    }
121
    /// @brief Gets dependencies of the given type
122
    /** If type is null, will get all dependencies.
123
     * @param $type One of the COB_DEPENDENCY_* constants, defined
124
     * above.
125
     * @return An array of COBDependency objects
126
     */
127
    public function GetDependencies($type = null) {
128
        $dependenciesToReturn = array();
129
        foreach ($this->dependencies as $dependency) {
130
            if ($type == null || $type == $dependency->GetType()) {
131
                $dependenciesToReturn[] = $dependency;
132
            }
133
        }
134
        return $dependenciesToReturn;
135
    }
136
    /// @brief Gets the value of reserved1
137
    /** Reserved values weren't ever officially used by CL,
138
     * but someone might find them useful for something else.
139
     * @return int
140
     */
141
    public function GetReserved1() {
142
        return $this->reserved1;
143
    }
144
    /// @brief Gets the value of reserved2
145
    /** Reserved values weren't ever officially used by CL,
146
     * but someone might find them useful for something else.
147
     * @return int
148
     */
149
    public function GetReserved2() {
150
        return $this->reserved2;
151
    }
152
    /// @brief Gets the value of reserved3
153
    /** Reserved values weren't ever officially used by CL,
154
     * but someone might find them useful for something else.
155
     * @return int
156
     */
157
    public function GetReserved3() {
158
        return $this->reserved3;
159
    }
160
    /// @brief Adds a dependency to this agent
161
    /** 
162
     * @param $dependency The COBDependency to add.
163
     */
164
    public function AddDependency(COBDependency $dependency) { 
165
        if (!in_array($dependency->GetDependencyName(), $this->dependencies)) {
166
            $this->dependencies[] = $dependency;
167
        }
168
    }
169
    /// @brief Sets the install script
170
    /**
171
     * @param $installScript the text of the script to add
172
     */
173
    public function SetInstallScript($installScript) {
174
        $this->installScript = $installScript;
175
    }
176
    /// @brief Sets the remover script
177
    /**
178
     * @param $removeScript The text of the script to add
179
     */
180
    public function SetRemoveScript($removeScript) {
181
        $this->removeScript = $removeScript;
182
    }
183
    /// @brief Adds an event script
184
    /** 
185
     * @param $eventScript The text of the script to add
186
     */
187
    public function AddEventScript($eventScript) {
188
        $this->eventScripts[] = $eventScript;
189
    }
190
    /// @brief Sets the date this agent was last injected
191
    /** 
192
     * @param $time The date this agent was last injected as a UNIX timestamp
193
     */
194
    public function SetLastUsageDate($time) {
195
        if ($time > time()) {
196
            return false;
197
        } else {
198
            $this->lastUsageDate = $time;
199
        }
200
    }
201
    /// @brief Sets the date this agent will expire
202
    /**
203
     * @param $time The date this agent will expire as a UNIX timestamp
204
     */
205
    public function SetExpiryDate($time) {
206
        $this->expiryDate = $time;
207
    }
208
    /// @brief Sets the quantity of the agent available
209
    /**
210
     * @param $quantity The quantity available, an integer. 0xFF means infinite.
211
     */
212
    public function SetQuantityAvailable($quantity) {
213
        $this->quantityAvailable = $quantity;
214
    }
215
    /// @brief Sets the interval required between re-use.
216
    /** @param $interval The interval in seconds, between re-use of this agent.
217
     */
218
    public function SetReuseInterval($interval) {
219
        $this->reuseInterval = $interval;
220
    }
221
    /// @brief Adds the reserved variables to this agent
222
    /**
223
     * These variables have no meaning to Creatures 2 and don't appear in Creatures 1.
224
     * They're all integers.
225
     * @param $reserved1 The first reserved variable
226
     * @param $reserved2 The second reserved variable
227
     * @param $reserved3 The third reserved variable
228
     */
229
    public function SetReserved($reserved1, $reserved2, $reserved3) {
230
        $this->reserved1 = $reserved1;
231
        $this->reserved2 = $reserved2;
232
        $this->reserved3 = $reserved3;
233
    }
234
    /// @brief Add the thumbnail to this agent.
235
    /** @param $frame The thumbnail as a SpriteFrame 
236
     */
237
    public function SetThumbnail(SpriteFrame $frame) {
238
        if ($this->thumbnail != null) {
239
            throw new Exception('Thumbnail already added');
240
        }
241
        $this->thumbnail = $frame;
242
    }
243
    /// @cond INTERNAL_DOCS
244
    /**
245
     * @brief Adds a remover script by reading from an RCB file.
246
     * @param IReader $reader A StringReader or FileReader for the RCB
247
     */
248
    public function AddC1RemoveScriptFromRCB(IReader $reader) {
249
        if ($this->removeScript != '') {
250
            throw new Exception('Script already added!');
251
        }
252
        $rcb = new COB($reader);
253
        $ablocks = $rcb->GetBlocks(COB_BLOCK_AGENT);
0 ignored issues
show
Documentation introduced by
COB_BLOCK_AGENT is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
254
        $this->removeScript = $ablocks[0]->GetInstallScript();
255
    }
256
257
    /// @brief Creates a new COBAgentBlock from an IReader.
258
    /** Reads from the current position of the IReader to fill out the data required by
259
     * the COBAgentBlock, then creates one and adds all the fields to it.
260
     * @param IReader $reader The IReader, seeked to the beginning of the contents of the agent block
261
     * @return COBAgentBlock
262
     */
263
    public static function CreateFromReaderC2(IReader $reader) {
264
        $quantityAvailable = $reader->ReadInt(2);
265
        if ($quantityAvailable == 0xffff) {
266
            $quantityAvailable = -1;
267
        }       
268
        $lastUsageDate = $reader->ReadInt(4);
269
        $reuseInterval = $reader->ReadInt(4);
270
271
        $expiryDay = $reader->ReadInt(1);
272
        $expiryMonth = $reader->ReadInt(1);
273
        $expiryYear = $reader->ReadInt(2);
274
        $expiryDate = mktime(0, 0, 0, $expiryMonth, $expiryDay, $expiryYear);
275
276
        $reserved = array($reader->ReadInt(4), $reader->ReadInt(4), $reader->ReadInt(4));
277
278
        $agentName = $reader->ReadCString();
279
        $agentDescription = $reader->ReadCString();
280
281
        $installScript = str_replace(',', "\n", $reader->ReadCString());
282
        $removeScript = str_replace(',', "\n", $reader->ReadCString());
283
284
        $numEventScripts = $reader->ReadInt(2);
285
286
        $eventScripts = array();
287
288
        for ($i = 0; $i < $numEventScripts; $i++) {
289
            $eventScripts[] = str_replace(',', "\n", $reader->ReadCString());
290
        }
291
        $numDependencies = $reader->ReadInt(2);
292
        $dependencies = array();
293
294
        for ($i = 0; $i < $numDependencies; $i++) {
295
            $type = ($reader->ReadInt(2) == 0) ? DEPENDENCY_SPRITE : DEPENDENCY_SOUND;
296
            $name = $reader->ReadCString();
297
            $dependencies[] = new COBDependency($type, $name);
298
        }
299
        $thumbWidth = $reader->ReadInt(2);
300
        $thumbHeight = $reader->ReadInt(2);
301
302
        $thumbnail = new S16Frame($reader, '565', $thumbWidth, $thumbHeight, $reader->GetPosition());
0 ignored issues
show
Documentation introduced by
$reader->GetPosition() is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
303
        $reader->Skip($thumbHeight*$thumbWidth*2);
304
305
        //parsing finished, onto making an AgentBlock.
306
        $agentBlock = new COBAgentBlock($agentName, $agentDescription);
307
        $agentBlock->AddQuantityAvailable($quantityAvailable);
0 ignored issues
show
Bug introduced by
The method AddQuantityAvailable() does not seem to exist on object<COBAgentBlock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
308
        $agentBlock->AddReuseInterval($reuseInterval);
0 ignored issues
show
Bug introduced by
The method AddReuseInterval() does not seem to exist on object<COBAgentBlock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
309
        $agentBlock->AddExpiryDate($expiryDate);
0 ignored issues
show
Bug introduced by
The method AddExpiryDate() does not seem to exist on object<COBAgentBlock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
310
        $agentBlock->AddLastUsageDate($lastUsageDate);
0 ignored issues
show
Bug introduced by
The method AddLastUsageDate() does not seem to exist on object<COBAgentBlock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
311
        $agentBlock->AddReserved($reserved[0], $reserved[1], $reserved[2]);
0 ignored issues
show
Bug introduced by
The method AddReserved() does not seem to exist on object<COBAgentBlock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
312
        $agentBlock->AddInstallScript($installScript);
0 ignored issues
show
Bug introduced by
The method AddInstallScript() does not seem to exist on object<COBAgentBlock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
313
        $agentBlock->AddRemoveScript($removeScript);
0 ignored issues
show
Bug introduced by
The method AddRemoveScript() does not seem to exist on object<COBAgentBlock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
314
        foreach ($eventScripts as $eventScript) {
315
            $agentBlock->AddEventScript($eventScript);
316
        }
317
        foreach ($dependencies as $dependency) {
318
            $agentBlock->AddDependency($dependency);
319
        }
320
        $agentBlock->AddThumbnail($thumbnail);
0 ignored issues
show
Bug introduced by
The method AddThumbnail() does not seem to exist on object<COBAgentBlock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
321
        return $agentBlock;
322
323
    }
324
    /// @brief Creates a COBAgentBlock from an IReader
325
    /** Reads from the current position of the IReader to fill out the data required by
326
     * the COBAgentBlock, then creates one and adds all the fields to it.
327
     * @param $reader The IReader, seeked to the beginning of the contents of the agent block
328
     * 
329
     */
330
    public static function CreateFromReaderC1(IReader $reader) {
331
        $quantityAvailable = $reader->ReadInt(2);
332
        $expires_month = $reader->ReadInt(4);
333
        $expires_day = $reader->ReadInt(4);
334
        $expires_year = $reader->ReadInt(4);
335
        $expiryDate = mktime(0, 0, 0, $expires_month, $expires_day, $expires_year);
336
337
        $numObjectScripts = $reader->ReadInt(2);
338
        $numInstallScripts = $reader->ReadInt(2);
339
        $quantityUsed = $reader->ReadInt(4);
0 ignored issues
show
Unused Code introduced by
$quantityUsed is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
340
        $objectScripts = array();
341 View Code Duplication
        for ($i = 0; $i < $numObjectScripts; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
342
            $scriptsize = $reader->ReadInt(1);
343
            if ($scriptsize == 255) {
344
                $scriptsize = $reader->ReadInt(2);
345
            }
346
            $objectScripts[$i] = $reader->Read($scriptsize);
347
        }
348
        $installScripts = array();
349 View Code Duplication
        for ($i = 0; $i < $numInstallScripts; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
350
            $scriptsize = $reader->ReadInt(1);
351
            if ($scriptsize == 255) {
352
                $scriptsize = $reader->ReadInt(2);
353
            }
354
            $installScripts[$i] = $reader->Read($scriptsize);
355
        }
356
        $pictureWidth = $reader->ReadInt(4);
357
        $pictureHeight = $reader->ReadInt(4);
358
        $unknown = $reader->ReadInt(2);
0 ignored issues
show
Unused Code introduced by
$unknown is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
359
        $sprframe = null;
360
        if ($pictureWidth > 0 || $pictureHeight > 0) {
361
            $sprframe = new SPRFrame($reader, $pictureWidth, $pictureHeight);
362
            $sprframe->Flip(); 
363
        }
364
365
        $agentName = $reader->Read($reader->ReadInt(1));
366
367
        $agentBlock = new COBAgentBlock($agentName, '');
368
        $agentBlock->AddQuantityAvailable($quantityAvailable);
0 ignored issues
show
Bug introduced by
The method AddQuantityAvailable() does not seem to exist on object<COBAgentBlock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
369
        $agentBlock->AddExpiryDate($expiryDate);
0 ignored issues
show
Bug introduced by
The method AddExpiryDate() does not seem to exist on object<COBAgentBlock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
370
        if ($sprframe != null) {
371
            $agentBlock->AddThumbnail($sprframe);
0 ignored issues
show
Bug introduced by
The method AddThumbnail() does not seem to exist on object<COBAgentBlock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
372
        }
373
        foreach ($objectScripts as $objectScript) {
374
            $agentBlock->AddEventScript($objectScript);
375
        }
376
        $agentBlock->AddInstallScript(implode("\n*c2ephp Install script seperator\n", $installScripts));
0 ignored issues
show
Bug introduced by
The method AddInstallScript() does not seem to exist on object<COBAgentBlock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
377
        return $agentBlock;
378
    }
379
    /// @endcond
380
}
381
382
/// @brief defines a dependency which is used in a COB file */
383
class COBDependency {
384
    /// @cond INTERNAL_DOCS
385
386
    private $type;
387
    private $name;
388
389
    /// @endcond
390
391
    /// @brief Creates a new COBDependency
392
    /** @param string $type The type of dependency ('sprite' or 'sound').
393
     * @param $name The name of the dependency (four characters, no file extension)
394
     */
395
    public function COBDependency($type, $name) {
396
        $this->type = $type;
397
        $this->name = $name;
398
    }
399
    /// @brief Gets the dependency type
400
    /** @return string
401
     */
402
    public function GetDependencyType() {
403
        return $this->type;
404
    }
405
    /// @brief Gets the name of the dependency
406
    /** @return string
407
     */
408
    public function GetDependencyName() {
409
        return $this->name;
410
    }
411
412
}
413
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
414