Skip to content

Commit

Permalink
Merge pull request #73 from The-Chinese-Room/master
Browse files Browse the repository at this point in the history
updated Inkpot plugin
  • Loading branch information
TCR-Nick authored Mar 17, 2024
2 parents 46c782d + 248ab33 commit 9997e67
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,25 @@ namespace InkCompiler
FPaths::CollapseRelativeDirectories(InkFilePathStripped);
if (!FPaths::FileExists(inkExePath))
{
INKPOT_WARN("CompileInkString - %s could not locate inklecate.exe.", *InkFilePathStripped);
INKPOT_ERROR("CompileInkFile_Internal - %s could not locate inklecate.exe.", *InkFilePathStripped);
return false;
}

FString compiledJsonPath = InScratchFilePath + TEXT(".json");;
FPaths::CollapseRelativeDirectories(compiledJsonPath);

FString args = FString::Printf(TEXT("%s -j -o \"%s\" \"%s\""), shouldCountVisits?TEXT(" -c"):TEXT(""), *compiledJsonPath, *InkFilePathStripped );
FString args = FString::Printf(TEXT("%s -j -o \"%s\" \"%s\""), shouldCountVisits?TEXT(" -c"):TEXT(" "), *compiledJsonPath, *InkFilePathStripped );

// Execute compile using inklecate and output compiled JSON to Intermediate/InkCommandline.
FString stdOut;
FString stdErr;
int32 returnCode;
bool bSuccess = FPlatformProcess::ExecProcess(*inkExePath, *args, &returnCode, &stdOut, &stdErr);
FString wkDir = GetScratchDirectory();

bool bSuccess = FPlatformProcess::ExecProcess(*inkExePath, *args, &returnCode, &stdOut, &stdErr, *wkDir, true );
if (!bSuccess)
{
INKPOT_WARN("CompileInkString - %s failed ExecProcess. Error %s: ", *InkFilePathStripped, *(stdOut + stdErr));
INKPOT_ERROR("CompileInkFile_Internal - %s failed ExecProcess. Error %s: ", *InkFilePathStripped, *(stdOut + stdErr));
return false;
}

Expand All @@ -64,17 +67,17 @@ namespace InkCompiler
// supress warnings for tests that we expect to fail compilation
if(!bIsCompilationFailExpected)
{
INKPOT_WARN( "CompileInkString - %s failed to compile ink string. Error %s: ", *InkFilePathStripped, *(stdOut + stdErr) );
INKPOT_ERROR( "CompileInkFile_Internal - %s failed to compile ink. Error %s: ", *InkFilePathStripped, *(stdOut + stdErr) );
}
return false;
}
if (!bExported)
{
INKPOT_WARN("CompileInkFile - %s failed to export ink.json string. Error : %s", *InkFilePathStripped, *(stdOut + stdErr));
INKPOT_ERROR("CompileInkFile_Internal - %s failed to export ink.json . Error : %s", *InkFilePathStripped, *(stdOut + stdErr));
return false;
}

INKPOT_LOG("CompileInkString - Output: %s", *(stdOut + stdErr));
INKPOT_LOG("CompileInkFile_Internal - Output: %s", *(stdOut + stdErr));

return FFileHelper::LoadFileToString(OutCompiledJSON, *compiledJsonPath);
}
Expand All @@ -99,32 +102,18 @@ namespace InkCompiler
void FlushScratchDirectory()
{
IPlatformFile::GetPlatformPhysical().DeleteDirectoryRecursively( *GetScratchDirectory() );
}

void CopyFilesMatchingFilter(const FString& SourceFolder, const FString& DestinationFolder, const FString& FileFilter)
{
const FString fileFilter = SourceFolder + FileFilter;

TArray<FString> IncludeFiles;
FFileManagerGeneric::Get().FindFiles(IncludeFiles, *fileFilter, true, false);
for (const FString& file : IncludeFiles)
{
const FString sourcePath = SourceFolder + file;
const FString destinationPath = DestinationFolder + file;
IFileManager::Get().Copy(*destinationPath, *sourcePath, true, false, false, nullptr);
}
IPlatformFile::GetPlatformPhysical().CreateDirectory( *GetScratchDirectory() );
}

bool CompileInkFile(const FString& InSourceFilePath, const FString& ScratchFilePath, FString& OutCompiledJSON, TArray<FString>& Errors, TArray<FString>& Warnings, bool shouldCountVisits, bool bIsCompilationFailExpected )
{
// Copy the ink source file to the scratch directory for compiling
IFileManager::Get().Copy(*ScratchFilePath, *InSourceFilePath, true, false, false, nullptr);
return CompileInkFile_Internal(InSourceFilePath, ScratchFilePath, OutCompiledJSON, Errors, Warnings, shouldCountVisits, bIsCompilationFailExpected);
}

bool CompileInkString(const FString& SourceString, const FString& ScratchFilePath, FString& OutCompiledJSON, TArray<FString>& Errors, TArray<FString>& Warnings, bool shouldCountVisits, bool bIsCompilationFailExpected )
{
// Save the ink source string to an ink file in the scratch directory for compiling
FlushScratchDirectory();
FFileHelper::SaveStringToFile(SourceString, *ScratchFilePath);
return CompileInkFile_Internal(ScratchFilePath, ScratchFilePath, OutCompiledJSON, Errors, Warnings, shouldCountVisits, bIsCompilationFailExpected );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@
#define TEST_CURRENT_STORY_TEXT TEXT("TEST_CURRENT_STORY_TEXT")
#define TEST_CHOICE_COUNT TEXT("TEST_CHOICE_COUNT")
#define TEST_CHOICE_TEXT TEXT("TEST_CHOICE_TEXT")

#define EXECUTE_STORY_CHOICE TEXT("EXECUTE_STORY_CHOICE")
#define EXECUTE_SAVE_JSON_STATE TEXT("EXECUTE_SAVE_JSON_STATE")
#define EXECUTE_LOAD_JSON_STATE TEXT("EXECUTE_LOAD_JSON_STATE")
#define EXECUTE_CHOOSE_STRING_PATH TEXT("EXECUTE_CHOOSE_STRING_PATH")
#define EXECUTE_INK_FUNCTION TEXT("EXECUTE_INK_FUNCTION")

#define TEST_CURRENT_TAGS TEXT("TEST_CURRENT_TAGS")
#define TEST_CURRENT_TAG_COUNT TEXT("TEST_CURRENT_TAG_COUNT")
#define TEST_STORY_GLOBAL_TAGS TEXT("TEST_STORY_GLOBAL_TAGS")
#define TEST_STORY_EVALUATION_STACK_COUNT TEXT("TEST_STORY_EVALUATION_STACK_COUNT")
#define TEST_TAG_FOR_PATH TEXT("TEST_TAG_FOR_PATH")

#define TEST_STORY_EVALUATION_STACK_COUNT TEXT("TEST_STORY_EVALUATION_STACK_COUNT")
#define TEST_RANDOM_LIST_CONTINUE TEXT("TEST_RANDOM_LIST_CONTINUE")
#define TEST_PATH_VISIT_COUNT TEXT("TEST_PATH_VISIT_COUNT")
#define TEST_CONTINUE_CONTAINS TEXT("TEST_CONTINUE_CONTAINS")
Expand Down Expand Up @@ -94,7 +98,12 @@ static bool ExecuteContinueMaximally(UInkpotStory* Story, FString& Output)
{
if (Story)
{
Output = Story->ContinueMaximally();
while(Story->CanContinue())
{
Output += Story->Continue();
for( const FString &tag : Story->GetCurrentTags() )
Output += TEXT("# ") + tag;
}
return true;
}
return false;
Expand Down Expand Up @@ -167,9 +176,11 @@ static bool IsStoryInstruction(const FString& Instructions)
EXECUTE_CHOOSE_STRING_PATH,
EXECUTE_INK_FUNCTION,
TEST_CURRENT_TAGS,
TEST_CURRENT_TAG_COUNT,
TEST_STORY_GLOBAL_TAGS,
TEST_STORY_EVALUATION_STACK_COUNT,
TEST_TAG_FOR_PATH,

TEST_STORY_EVALUATION_STACK_COUNT,
TEST_RANDOM_LIST_CONTINUE,
TEST_PATH_VISIT_COUNT,
TEST_CONTINUE_CONTAINS,
Expand Down Expand Up @@ -203,10 +214,7 @@ void FInkTests::GetTests(TArray<FString>& OutBeautifiedNames, TArray <FString>&

bool FInkTests::RunTest(const FString& InkTestName)
{
const FString includeSourceFolder = FPaths::ProjectPluginsDir() + TEXT("Inkpot/TestInkSource/Includes/");
const FString includeDestinationFolder = InkCompiler::GetScratchDirectory() + "Includes/";
InkCompiler::FlushScratchDirectory();
InkCompiler::CopyFilesMatchingFilter(includeSourceFolder, includeDestinationFolder, "*.ink");

const FString testScriptStartPhrase = TEXT("INK_TEST_START");
const FString testStoryScriptStartPhrase = TEXT("INK_TEST_STORY_START");
Expand Down Expand Up @@ -247,7 +255,7 @@ bool FInkTests::RunTest(const FString& InkTestName)

TArray<FString> compileErrors, compileWarnings;
FString compiledJSON;
bool bCompileSuccess = InkCompiler::CompileInkString(fileContents, InkScratchFilePath, compiledJSON, compileErrors, compileWarnings, shouldCountVisits, expectedErrors>0 );
bool bCompileSuccess = InkCompiler::CompileInkFile(testFilePath, InkScratchFilePath, compiledJSON, compileErrors, compileWarnings, shouldCountVisits, expectedErrors>0 );

if (bCompileAsStory && bCompileSuccess)
{
Expand Down Expand Up @@ -677,7 +685,7 @@ bool FInkTests::RunTest(const FString& InkTestName)
const TArray<FString> &actualTags = story->GetCurrentTags();
if (expectedTags->Num() != actualTags.Num())
{
INKPOT_ERROR("%s : TEST_CURRENT_TAGS Tag count not equal: %s, \nExpected: %d\nActual__: %d\n", *InkTestName, expectedTags->Num(), actualTags.Num());
INKPOT_ERROR("%s : TEST_CURRENT_TAGS Tag count not equal: , \nExpected: %d\nActual__: %d\n", *InkTestName, expectedTags->Num(), actualTags.Num());
return false;
}

Expand All @@ -699,6 +707,24 @@ bool FInkTests::RunTest(const FString& InkTestName)
}
}
}
else if (testInstruction->HasField(TEST_CURRENT_TAG_COUNT))
{
if ( testInstruction->TryGetNumberField( TEST_CURRENT_TAG_COUNT, jsonParsedInt ) )
{
int32 numTagsExpected = jsonParsedInt;
int32 numTagsActual = story->GetCurrentTags().Num();
if ( numTagsExpected != numTagsActual )
{
INKPOT_ERROR( "%s : TEST_CURRENT_TAG_COUNT was not as expected. \nExpected: %d\nActual__: %d\n", *InkTestName, numTagsExpected, numTagsActual );
return false;
}
}
else
{
INKPOT_ERROR( "%s : TEST_CURRENT_TAG_COUNT could not get tag count\n", *InkTestName );
return false;
}
}
else if (testInstruction->HasField(TEST_STORY_GLOBAL_TAGS))
{
const TArray< TSharedPtr<FJsonValue> >* expectedTags;
Expand Down Expand Up @@ -743,13 +769,13 @@ bool FInkTests::RunTest(const FString& InkTestName)
{
if (expectedTags.Num() != actualTags.Num())
{
INKPOT_ERROR("%s : TEST_TAG_FOR_PATH Tag count not equal: %s, \nExpected: %d\nActual__: %d\n", *InkTestName, expectedTags.Num(), actualTags.Num());
INKPOT_ERROR("%s : TEST_TAG_FOR_PATH Tag count not equal: \nExpected: %d\nActual__: %d\n", *InkTestName, expectedTags.Num(), actualTags.Num());
return false;
}

for (int32 i = 0; i < expectedTags.Num(); i++)
{
const bool success = expectedTags[i].Equals(expectedTags[i]);
const bool success = actualTags[i].Equals(expectedTags[i]);
if (!success)
{
INKPOT_ERROR("%s : TEST_TAG_FOR_PATH tag string did not match. \nExpected: %s\nActual__: %s\n", *InkTestName, *expectedTags[i], *actualTags[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace InkCompiler
{
INKPOTEDITOR_API FString GetScratchDirectory();
INKPOTEDITOR_API void FlushScratchDirectory();
INKPOTEDITOR_API void CopyFilesMatchingFilter(const FString& SourceFolder, const FString& DestinationFolder, const FString& FileFilter);
INKPOTEDITOR_API bool CompileInkFile(const FString& SourceFilePath, const FString& ScratchFilePath, FString& OutCompiledJSON, TArray<FString>& Errors, TArray<FString>& Warnings, bool shouldCountVisits = false, bool bIsCompilationFailExpected = false);
INKPOTEDITOR_API bool CompileInkString(const FString& SourceString, const FString& ScratchFilePath, FString& OutCompiledJSON, TArray<FString>& Errors, TArray<FString>& Warnings, bool shouldCountVisits = false, bool bIsCompilationFailExpected = false);
INKPOTEDITOR_API void ParseInklecateOutput(const FString& InklecateOutput, TArray<FString>& Errors, TArray<FString>& Warnings, bool& bCompileSuccess, bool& bExportSuccess);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TODO: b
INK_TEST_STORY_START
[
{"TEST_ERROR_EQUAL": 0},
{"TEST_WARNING_EQUAL": 0},
{"TEST_WARNING_EQUAL": 1},
]
INK_TEST_END
*/

0 comments on commit 9997e67

Please sign in to comment.