Skip to content

Commit

Permalink
Merge pull request #79 from Moros1138/78-define-olc_pge_application-l…
Browse files Browse the repository at this point in the history
…ine-can-have-miscellaneous-text-surrounding-it-no-side-effects-bug-in-version-aee3eb2

78 define olc pge application line can have miscellaneous text surrounding it no side effects bug in version aee3eb2
  • Loading branch information
Moros1138 authored May 28, 2024
2 parents aee3eb2 + 07c22dc commit 9ebe4ac
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. Each batch

It is a summary of changes that would be pertinent to the end user of the PGEtinker website. For a comprehensive history of changes made to the project, please refer to the repository's commit history.

## 2024-05-28

- Fixed [Issue #78](https://github.com/Moros1138/PGEtinker/issues/78)

## 2024-05-27

- Changed complete revamp of the frontend code, much more organized
Expand Down
68 changes: 43 additions & 25 deletions pgetinker/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public function deserialize(string $json)
$this->workingDirectory = "";
}

public function getCode()
{
return implode("\n", $this->code);
}

public function setCode(string $code)
{
$this->code = explode("\n", $code);
Expand Down Expand Up @@ -162,6 +167,10 @@ private function processCodeDetectGeometryUtility($index)

private function processCodeDetectImplementationMacros($index)
{
// broad phase, don't process if this line doesn't at least contain the word "define"
if(!str_contains($this->code[$index], "define"))
return false;

$libraryMap = [
'OLC_PGE_APPLICATION' => 'olcPixelGameEngine.o',
'OLC_SOUNDWAVE_ENGINE' => 'olcSoundWaveEngine.o',
Expand All @@ -176,35 +185,44 @@ private function processCodeDetectImplementationMacros($index)
'OLC_PGEX_WIREFRAME' => 'olcPGEX_Wireframe.o',
];

// filter macros to detect implementation #define
if(str_contains($this->code[$index], "#define"))
$foundImplementationMacro = false;

foreach($libraryMap as $macro => $objectFileName)
{
$foundImplementationMacro = false;
foreach($libraryMap as $macro => $objectFileName)
preg_match(
'/(.*)\s*#\s*define?\s+' . $macro . '(.*)/',
$this->code[$index],
$match,
PREG_OFFSET_CAPTURE,
0
);

// no match, this time
if(count($match) == 0)
continue;

if(!empty(trim($match[1][0])) || !empty(trim($match[2][0])))
continue;

// blank the line
$this->code[$index] = "";

if($macro == "OLC_PGE_APPLICATION" && $this->foundGeometryHeader)
{
if(str_contains($this->code[$index], $macro))
{
// blank the line
$this->code[$index] = "";

if($macro == "OLC_PGE_APPLICATION" && $this->foundGeometryHeader)
{
$objectFileName = "olcPixelGameEngine_withGeometry.o";
$this->logger->info("Found the need for geometry utility support");
}
$objectFileName = "olcPixelGameEngine_withGeometry.o";
$this->logger->info("Found the need for geometry utility support");
}

// indicate that we use this library
$this->linkerInputFiles[] = "./lib/{$objectFileName}";
// indicate that we use this library
$this->linkerInputFiles[] = "./lib/{$objectFileName}";

$this->logger->info("Found implementation macro: {$macro}");
$foundImplementationMacro = true;
break;
}
}
$this->logger->info("Found implementation macro: {$macro}");
$foundImplementationMacro = true;
break;
}

if($foundImplementationMacro)
return true;
}
if($foundImplementationMacro)
return true;

return false;
}
Expand Down Expand Up @@ -341,7 +359,7 @@ private function processCodeRemoteInclude($index)
}
}

private function processCode()
public function processCode()
{
$this->logger->info("begin processing code");
$startTime = microtime(true);
Expand Down
69 changes: 69 additions & 0 deletions tests/Feature/ProcessCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Tests\Feature;

use PGEtinker\Compiler;
use Tests\TestCase;

class ProcessCodeTest extends TestCase
{

public function test_passthru_non_macros(): void
{
$compiler = new Compiler();

$code = "I can write anything wheeeee!#define OLC_PGE_APPLICATION\n";
$compiler->setCode($code);
$compiler->processCode();
$this->assertTrue($code == $compiler->getCode());

$code = "#define OLC_PGE_APPLICATIONYEAAAAAAA\n";
$compiler->setCode($code);
$compiler->processCode();
$this->assertTrue($code == $compiler->getCode());

$code = "I can write anything wheeeee!#define OLC_PGE_APPLICATIONYEAAAAAAA\n";
$compiler->setCode($code);
$compiler->processCode();
$this->assertTrue($code == $compiler->getCode());
}

public function test_valid_white_space_macros(): void
{
$compiler = new Compiler();

$code = "\t #define OLC_PGE_APPLICATION\n";
$compiler->setCode($code);
$compiler->processCode();
print_r($code);
print_r($compiler->getCode());
$this->assertTrue($code != $compiler->getCode());

$code = "# define OLC_PGE_APPLICATION\n";
$compiler->setCode($code);
$compiler->processCode();
$this->assertTrue($code != $compiler->getCode());

$code = "#define OLC_PGE_APPLICATION\n";
$compiler->setCode($code);
$compiler->processCode();
$this->assertTrue($code != $compiler->getCode());

$code = "#define OLC_PGE_APPLICATION \n";
$compiler->setCode($code);
$compiler->processCode();
$this->assertTrue($code != $compiler->getCode());
}

// public function test_process_code_fails_on_bad_macro(): void
// {
// $compiler = new Compiler();

// $code = "I can write anything wheeeee!#define OLC_PGE_APPLICATIONYEAAAAAAA\n";
// $compiler->setCode($code);
// $compiler->processCode();

// $this->assertTrue($code == $compiler->getCode());
// }

}

0 comments on commit 9ebe4ac

Please sign in to comment.