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 ( c33da0...2cb87f )
by Telyn
01:52
created

COBDependency::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 {
0 ignored issues
show
Bug introduced by
There is one abstract method Compile in this class; you could implement it, or declare this class as abstract.
Loading history...
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
    /// @brief Gets the agent's name 
63
    /** @return string
64
     */
65
    public function GetAgentName() {
66
        return $this->agentName;
67
    }
68
    /// @brief Gets the agent's description
69
    /** @return string
70
     */
71
    public function GetAgentDescription() {
72
        return $this->agentDescription;
73
    }
74
    /// @brief Gets the agent's install script
75
    /** @return string
76
     */
77
    public function GetInstallScript() {
78
        return $this->installScript;
79
    }
80
    /// @brief Gets the agent's remove script
81
    /** @return string
82
     */
83
    public function GetRemoveScript() {
84
        return $this->removeScript;
85
    }
86
    /// @brief Gets the number of event scripts
87
    /** @return int
88
     */
89
    public function GetEventScriptCount() {
90
        return sizeof($this->eventScripts);
91
    }
92
    /// @brief Gets the agent's event scripts
93
    /** @return array of strings, each string is an event script
94
     */
95
    public function GetEventScripts() {
96
        return $this->eventScripts;
97
    }
98
    /// @brief Gets an event script
99
    /** Event scripts are not necessarily in any order, so you have to work out what each script is for yourself.
100
     * @param $whichScript Which script to get.
101
     * @return A string containing the event script. Each line is seperated by a comma I think.
102
     */
103
    public function GetEventScript($whichScript) {
104
        return $this->eventScripts[$whichScript];
105
    }
106
    /// @brief Gets the thumbnail of this agent as would be shown in the Injector
107
    /** @return SpriteFrame SpriteFrame of the thumbnail
108
     */
109
    public function GetThumbnail() {
110
        return $this->thumbnail;
111
    }
112
    /// @brief Gets dependencies of the given type
113
    /** If type is null, will get all dependencies.
114
     * @param $type One of the COB_DEPENDENCY_* constants, defined
115
     * above.
116
     * @return An array of COBDependency objects
117
     */
118
    public function GetDependencies($type = null) {
119
        $dependenciesToReturn = array();
120
        foreach ($this->dependencies as $dependency) {
121
            if ($type == null || $type == $dependency->GetType()) {
122
                $dependenciesToReturn[] = $dependency;
123
            }
124
        }
125
        return $dependenciesToReturn;
126
    }
127
    /// @brief Gets the value of reserved1
128
    /** Reserved values weren't ever officially used by CL,
129
     * but someone might find them useful for something else.
130
     * @return int
131
     */
132
    public function GetReserved1() {
133
        return $this->reserved1;
134
    }
135
    /// @brief Gets the value of reserved2
136
    /** Reserved values weren't ever officially used by CL,
137
     * but someone might find them useful for something else.
138
     * @return int
139
     */
140
    public function GetReserved2() {
141
        return $this->reserved2;
142
    }
143
    /// @brief Gets the value of reserved3
144
    /** Reserved values weren't ever officially used by CL,
145
     * but someone might find them useful for something else.
146
     * @return int
147
     */
148
    public function GetReserved3() {
149
        return $this->reserved3;
150
    }
151
    /// @brief Adds a dependency to this agent
152
    /** 
153
     * @param $dependency The COBDependency to add.
154
     */
155
    public function AddDependency(COBDependency $dependency) { 
156
        if (!in_array($dependency->GetDependencyName(), $this->dependencies)) {
157
            $this->dependencies[] = $dependency;
158
        }
159
    }
160
    /// @brief Sets the install script
161
    /**
162
     * @param $installScript the text of the script to add
163
     */
164
    public function SetInstallScript($installScript) {
165
        $this->installScript = $installScript;
166
    }
167
    /// @brief Sets the remover script
168
    /**
169
     * @param $removeScript The text of the script to add
170
     */
171
    public function SetRemoveScript($removeScript) {
172
        $this->removeScript = $removeScript;
173
    }
174
    /// @brief Adds an event script
175
    /** 
176
     * @param $eventScript The text of the script to add
177
     */
178
    public function AddEventScript($eventScript) {
179
        $this->eventScripts[] = $eventScript;
180
    }
181
    /// @brief Sets the date this agent was last injected
182
    /** 
183
     * @param $time The date this agent was last injected as a UNIX timestamp
184
     */
185
    public function SetLastUsageDate($time) {
186
        if ($time > time()) {
187
            return false;
188
        } else {
189
            $this->lastUsageDate = $time;
190
        }
191
    }
192
    /// @brief Sets the date this agent will expire
193
    /**
194
     * @param $time The date this agent will expire as a UNIX timestamp
195
     */
196
    public function SetExpiryDate($time) {
197
        $this->expiryDate = $time;
198
    }
199
    /// @brief Sets the quantity of the agent available
200
    /**
201
     * @param $quantity The quantity available, an integer. 0xFF means infinite.
202
     */
203
    public function SetQuantityAvailable($quantity) {
204
        $this->quantityAvailable = $quantity;
205
    }
206
    /// @brief Sets the interval required between re-use.
207
    /** @param $interval The interval in seconds, between re-use of this agent.
208
     */
209
    public function SetReuseInterval($interval) {
210
        $this->reuseInterval = $interval;
211
    }
212
    /// @brief Adds the reserved variables to this agent
213
    /**
214
     * These variables have no meaning to Creatures 2 and don't appear in Creatures 1.
215
     * They're all integers.
216
     * @param $reserved1 The first reserved variable
217
     * @param $reserved2 The second reserved variable
218
     * @param $reserved3 The third reserved variable
219
     */
220
    public function SetReserved($reserved1, $reserved2, $reserved3) {
221
        $this->reserved1 = $reserved1;
222
        $this->reserved2 = $reserved2;
223
        $this->reserved3 = $reserved3;
224
    }
225
    /// @brief Add the thumbnail to this agent.
226
    /** @param $frame The thumbnail as a SpriteFrame 
227
     */
228
    public function SetThumbnail(SpriteFrame $frame) {
229
        if ($this->thumbnail != null) {
230
            throw new Exception('Thumbnail already added');
231
        }
232
        $this->thumbnail = $frame;
233
    }
234
    /// @cond INTERNAL_DOCS
235
    /**
236
     * @brief Adds a remover script by reading from an RCB file.
237
     * @param IReader $reader A StringReader or FileReader for the RCB
238
     */
239
    public function AddC1RemoveScriptFromRCB(IReader $reader) {
240
        if ($this->removeScript != '') {
241
            throw new Exception('Script already added!');
242
        }
243
        $rcb = new COB($reader);
244
        $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...
245
        $this->removeScript = $ablocks[0]->GetInstallScript();
246
    }
247
248
    /// @brief Creates a new COBAgentBlock from an IReader.
249
    /** Reads from the current position of the IReader to fill out the data required by
250
     * the COBAgentBlock, then creates one and adds all the fields to it.
251
     * @param IReader $reader The IReader, seeked to the beginning of the contents of the agent block
252
     * @return COBAgentBlock
253
     */
254
    public static function CreateFromReaderC2(IReader $reader) {
255
        $quantityAvailable = $reader->ReadInt(2);
256
        if ($quantityAvailable == 0xffff) {
257
            $quantityAvailable = -1;
258
        }       
259
        $lastUsageDate = $reader->ReadInt(4);
260
        $reuseInterval = $reader->ReadInt(4);
261
262
        $expiryDay = $reader->ReadInt(1);
263
        $expiryMonth = $reader->ReadInt(1);
264
        $expiryYear = $reader->ReadInt(2);
265
        $expiryDate = mktime(0, 0, 0, $expiryMonth, $expiryDay, $expiryYear);
266
267
        $reserved = array($reader->ReadInt(4), $reader->ReadInt(4), $reader->ReadInt(4));
268
269
        $agentName = $reader->ReadCString();
270
        $agentDescription = $reader->ReadCString();
271
272
        $installScript = str_replace(',', "\n", $reader->ReadCString());
273
        $removeScript = str_replace(',', "\n", $reader->ReadCString());
274
275
        $numEventScripts = $reader->ReadInt(2);
276
277
        $eventScripts = array();
278
279
        for ($i = 0; $i < $numEventScripts; $i++) {
280
            $eventScripts[] = str_replace(',', "\n", $reader->ReadCString());
281
        }
282
        $numDependencies = $reader->ReadInt(2);
283
        $dependencies = array();
284
285
        for ($i = 0; $i < $numDependencies; $i++) {
286
            $type = ($reader->ReadInt(2) == 0) ? DEPENDENCY_SPRITE : DEPENDENCY_SOUND;
287
            $name = $reader->ReadCString();
288
            $dependencies[] = new COBDependency($type, $name);
289
        }
290
        $thumbWidth = $reader->ReadInt(2);
291
        $thumbHeight = $reader->ReadInt(2);
292
293
        $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...
294
        $reader->Skip($thumbHeight*$thumbWidth*2);
295
296
        //parsing finished, onto making an AgentBlock.
297
        $agentBlock = new COBAgentBlock($agentName, $agentDescription);
298
        $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...
299
        $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...
300
        $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...
301
        $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...
302
        $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...
303
        $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...
304
        $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...
305
        foreach ($eventScripts as $eventScript) {
306
            $agentBlock->AddEventScript($eventScript);
307
        }
308
        foreach ($dependencies as $dependency) {
309
            $agentBlock->AddDependency($dependency);
310
        }
311
        $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...
312
        return $agentBlock;
313
314
    }
315
    /// @brief Creates a COBAgentBlock from an IReader
316
    /** Reads from the current position of the IReader to fill out the data required by
317
     * the COBAgentBlock, then creates one and adds all the fields to it.
318
     * @param $reader The IReader, seeked to the beginning of the contents of the agent block
319
     * 
320
     */
321
    public static function CreateFromReaderC1(IReader $reader) {
322
        $quantityAvailable = $reader->ReadInt(2);
323
        $expires_month = $reader->ReadInt(4);
324
        $expires_day = $reader->ReadInt(4);
325
        $expires_year = $reader->ReadInt(4);
326
        $expiryDate = mktime(0, 0, 0, $expires_month, $expires_day, $expires_year);
327
328
        $numObjectScripts = $reader->ReadInt(2);
329
        $numInstallScripts = $reader->ReadInt(2);
330
        $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...
331
        $objectScripts = array();
332 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...
333
            $scriptsize = $reader->ReadInt(1);
334
            if ($scriptsize == 255) {
335
                $scriptsize = $reader->ReadInt(2);
336
            }
337
            $objectScripts[$i] = $reader->Read($scriptsize);
338
        }
339
        $installScripts = array();
340 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...
341
            $scriptsize = $reader->ReadInt(1);
342
            if ($scriptsize == 255) {
343
                $scriptsize = $reader->ReadInt(2);
344
            }
345
            $installScripts[$i] = $reader->Read($scriptsize);
346
        }
347
        $pictureWidth = $reader->ReadInt(4);
348
        $pictureHeight = $reader->ReadInt(4);
349
        $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...
350
        $sprframe = null;
351
        if ($pictureWidth > 0 || $pictureHeight > 0) {
352
            $sprframe = new SPRFrame($reader, $pictureWidth, $pictureHeight);
353
            $sprframe->Flip(); 
354
        }
355
356
        $agentName = $reader->Read($reader->ReadInt(1));
357
358
        $agentBlock = new COBAgentBlock($agentName, '');
359
        $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...
360
        $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...
361
        if ($sprframe != null) {
362
            $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...
363
        }
364
        foreach ($objectScripts as $objectScript) {
365
            $agentBlock->AddEventScript($objectScript);
366
        }
367
        $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...
368
        return $agentBlock;
369
    }
370
    /// @endcond
371
}
372
373
/// @brief defines a dependency which is used in a COB file */
374
class COBDependency {
375
    /// @cond INTERNAL_DOCS
376
377
    private $type;
378
    private $name;
379
380
    /// @endcond
381
382
    /// @brief Creates a new COBDependency
383
    /** @param string $type The type of dependency ('sprite' or 'sound').
384
     * @param $name The name of the dependency (four characters, no file extension)
385
     */
386
    public function COBDependency($type, $name) {
387
        $this->type = $type;
388
        $this->name = $name;
389
    }
390
    /// @brief Gets the dependency type
391
    /** @return string
392
     */
393
    public function GetDependencyType() {
394
        return $this->type;
395
    }
396
    /// @brief Gets the name of the dependency
397
    /** @return string
398
     */
399
    public function GetDependencyName() {
400
        return $this->name;
401
    }
402
403
    /// @brief Supposedly compiles the block into binary. Throws an error to say it's not implemented.
404
    /** @return string
405
     */
406
    public function Compile() {
407
        // TODO: implement
408
        throw new Exception("COBAgentBlock::Compile not implemented");
409
    }
410
}
411
?>
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...
412