Skip to content

Commit

Permalink
Merge pull request #71 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 Feb 21, 2024
2 parents 9caf28a + 69005c1 commit 7b4c82c
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 27 deletions.
6 changes: 5 additions & 1 deletion InkpotDemo/Plugins/Inkpot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ https://github.com/The-Chinese-Room/InkpotDemo

### My Ink files will not import.

* Make sure you have **.Net framework 3.1** installed.<br>
Inkpot uses Inklecate to compile the Ink files and it needs .net 3.1.<br>
Without this the Ink files will not import.<br>

* Inkpot has only been tested as an application plugin and not an engine plugin.<br>
It needs to be located in the project's plugins folder and not the engine plugin folder.<br>

Expand All @@ -62,7 +66,7 @@ https://github.com/The-Chinese-Room/InkpotDemo<br>
### My Ink file changes will not update on reimport.
* Inky, the Ink script editor, will silently fail when saving to read only files.<br>
Some version control systems will make the file read only when you check them out, eg perforce.<br>
This can lead to some conusion when running Inky alongside Unreal & Inkpot.<br>
This can lead to some confusion when running Inky alongside Unreal & Inkpot.<br>


## Module Structure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Inkpot/InkpotStories.h"
#include "Utility/InkpotLog.h"
#include "Settings/InkpotCVars.h"
#include "Asset/InkpotStoryAsset.h"

static FDelayedAutoRegisterHelper DelayedAutoRegister(
EDelayedRegisterRunPhase::EndOfEngineInit,
Expand Down Expand Up @@ -47,10 +48,13 @@ void UInkpot::Register()
void UInkpot::BindPostImport()
{
#if WITH_EDITOR
UImportSubsystem *imports = GEditor->GetEditorSubsystem<UImportSubsystem>();
if(!imports)
return;
imports->OnAssetPostImport.AddUObject( this, &UInkpot::OnAssetPostImport );
if (IsValid(GEditor))
{
UImportSubsystem* imports = GEditor->GetEditorSubsystem<UImportSubsystem>();
if (!imports)
return;
imports->OnAssetPostImport.AddUObject(this, &UInkpot::OnAssetPostImport);
}
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ UInkpotChoice::UInkpotChoice()

void UInkpotChoice::Initialise( int32 InIndex, const FString &InString)
{
UInkpotLine::Initialise(InString);
UInkpotLine::Initialise( InString );
Index = InIndex;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include "Ink/Path.h"
#include "Utility/InkpotLog.h"

static const int32 dbgIndent{12};
#define INKPOT_DBG( section, msg, ... ) INKPOT_LOG( "%- *s: " msg, dbgIndent, TEXT(section), ##__VA_ARGS__ )

void UInkpotStory::Initialise( TSharedPtr<FInkpotStoryInternal> InInkpotStory )
{
Expand Down Expand Up @@ -383,7 +381,7 @@ void UInkpotStory::OnChoosePathStringInternal(const FString& InPath, const TArra
void UInkpotStory::DumpDebug()
{
FString currentFlow = GetCurrentFlowName();
INKPOT_DBG("Flow", "%s", *currentFlow);
INKPOT_LOG("Flow : %s", *currentFlow);

if( GetAliveFlowCount() > 0 )
{
Expand All @@ -396,12 +394,12 @@ void UInkpotStory::DumpDebug()
flowsAlive.Append( flow );
flowsAlive.Append( ", " );
}
INKPOT_DBG( "Flows alive", "%s", *flowsAlive );
INKPOT_LOG("Flows alive : %s", *flowsAlive );
}
}

FString currentText = GetCurrentText();
INKPOT_DBG("Text", "%s", *currentText);
INKPOT_LOG("Text : %s", *currentText);

const TArray<FString>& tags = GetCurrentTags();
if( tags.Num() > 0 )
Expand All @@ -413,7 +411,7 @@ void UInkpotStory::DumpDebug()
tagsSet.Append( tag );
tagsSet.Append( "' " );
}
INKPOT_DBG( "CTags", "%s", *tagsSet );
INKPOT_LOG("CTags : %s", *tagsSet);
}

TArray<FString> gtags = GlobalTags();
Expand All @@ -426,7 +424,7 @@ void UInkpotStory::DumpDebug()
tagsSet.Append( tag );
tagsSet.Append( "' " );
}
INKPOT_DBG( "GTags", "%s", *tagsSet );
INKPOT_LOG("GTags : %s", *tagsSet);
}

TArray<FString> keys;
Expand All @@ -439,21 +437,21 @@ void UInkpotStory::DumpDebug()
const FString& value = obj->ToString();
if(i==0)
{
INKPOT_DBG( "Variables", "%s = %s", *key, *value );
INKPOT_LOG("Variables : %s = %s", *key, *value);
}
else
{
INKPOT_DBG( " ", "%s = %s", *key, *value );
INKPOT_LOG(" : %s = %s", *key, *value);
}
}
}

if(Choices.Num() > 0)
{
INKPOT_DBG( "Choice", "%d - %s", Choices[0]->GetIndex(), *Choices[0]->GetString() );
INKPOT_LOG("Choice : %d - %s", Choices[0]->GetIndex(), *Choices[0]->GetString());
for( int32 i=1; i<Choices.Num(); ++i )
{
INKPOT_DBG( " ", "%d - %s", Choices[i]->GetIndex(), *Choices[i]->GetString() );
INKPOT_LOG(" : %d - %s", Choices[i]->GetIndex(), *Choices[i]->GetString());
}
}
}
Expand All @@ -462,7 +460,7 @@ void UInkpotStory::DumpDebug(UInkpotChoice *InChoice)
{
if(!InChoice)
return;
INKPOT_DBG( "Chose", "%d - %s", InChoice->GetIndex(), *InChoice->GetString() );
INKPOT_LOG("Chose : %d - %s", InChoice->GetIndex(), *InChoice->GetString());
}


Expand Down Expand Up @@ -533,6 +531,60 @@ void UInkpotStory::DumpContentAtKnot( const FString& InName )
DumpContainer(InName, knotContainer );
}

void UInkpotStory::GatherAllStrings( TMap<FString, FString> &OutStrings )
{
TSharedPtr<Ink::FContainer> main = StoryInternal->GetMainContentContainer();
GatherAllStrings( "", main, OutStrings);
}

void UInkpotStory::GatherAllStrings( const FString &InRootName, TSharedPtr<Ink::FContainer> InContainer, TMap<FString, FString> &OutStrings )
{
if(!InContainer)
return;

FString rootName = InRootName;
FString containerName = InContainer->GetName();
if (rootName.IsEmpty())
rootName = containerName;
else if (containerName.Len() > 0)
rootName += TEXT(".") + containerName;

TArray<TSharedPtr<Ink::FObject>> *contents = InContainer->GetContent().Get();
int contentIndex = 1;
for( TSharedPtr<Ink::FObject> obj : *contents)
{
TSharedPtr<Ink::FContainer> container = Ink::FObject::DynamicCastTo<Ink::FContainer>(obj);
if( container )
{
GatherAllStrings( rootName, container, OutStrings );
}
else
{
if(obj->CanCastTo(Ink::EInkObjectClass::FValueString))
{
FString entry = obj->ToString();
entry.TrimEndInline();
if ( entry.Len() )
{
FString key = FString::Printf(TEXT("%s.%02d"), *rootName, contentIndex);
OutStrings.Add( key, entry );
INKPOT_LOG( "%s : %s", *key, *entry );
contentIndex++;
}
}
}
}

TSharedPtr<TMap<FString, TSharedPtr<Ink::FObject>>> namedContentPtr = InContainer->GetNamedContent();
for( auto pair : *namedContentPtr )
{
TSharedPtr<Ink::FContainer> container = Ink::FObject::DynamicCastTo<Ink::FContainer>( pair.Value );
if(!container)
continue;
GatherAllStrings( rootName, container, OutStrings );
}
}

TArray<FString> UInkpotStory::GetNamedContent()
{
TSharedPtr<Ink::FContainer> container = StoryInternal->GetMainContentContainer();
Expand Down Expand Up @@ -616,6 +668,11 @@ FOnSwitchFlow& UInkpotStory::OnSwitchFlow()
return EventOnSwitchFlow;
}

FOnStoryLoadJSON& UInkpotStory::OnStoryLoadJSON()
{
return EventOnStoryLoadJSON;
}

UInkpotLine *UInkpotStory::GetCurrentLine()
{
UInkpotLine* line = NewObject<UInkpotLine>( this );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class INKPOT_API UInkpotStory : public UObject
FOnMakeChoice& OnMakeChoice();
FOnChoosePath& OnChoosePath();
FOnSwitchFlow& OnSwitchFlow();
FOnStoryLoadJSON OnStoryLoadJSON();
FOnStoryLoadJSON& OnStoryLoadJSON();

void ResetContent( TSharedPtr<FInkpotStoryInternal> InNewStoryContent );
void ResetState();
Expand All @@ -182,8 +182,11 @@ class INKPOT_API UInkpotStory : public UObject
void DumpMainContent();
void DumpContentAtPath( const FString& InName );
void DumpContentAtKnot( const FString& InName );
void DumpContainer(const FString& InName, TSharedPtr<Ink::FContainer> InContainer, int Indent = 0);

private:
void GatherAllStrings( TMap<FString, FString> &OutStrings );

protected:
void OnContinueInternal();
void OnMakeChoiceInternal(TSharedPtr<Ink::FChoice> InChoice);
void OnEvaluateFunctionInternal(const FString& InFunctionName, const TArray<TSharedPtr<Ink::FValueType>>& InFunctionParms);
Expand All @@ -195,11 +198,11 @@ class INKPOT_API UInkpotStory : public UObject

void DumpDebug();
void DumpDebug(UInkpotChoice *Choice);

void DumpContainer( const FString& InName, TSharedPtr<Ink::FContainer> InContainer, int Indent = 0 );

TArray<FString> GetNamedContent( TSharedPtr<Ink::FContainer> Container );

void GatherAllStrings(const FString &InRootName, TSharedPtr<Ink::FContainer> InContainer, TMap<FString, FString>& OutStrings);

protected:
TSharedPtr<FInkpotStoryInternal> StoryInternal;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "Compiler/InkCompiler.h"
#include "Logging/MessageLog.h"
#include "EditorFramework/AssetImportData.h"
#include "Inkpot/Inkpot.h"
#include "Inkpot/InkpotStory.h"

#define LOCTEXT_NAMESPACE "InkpotStoryAssetFactory"

Expand All @@ -21,20 +23,22 @@ bool UInkpotStoryAssetFactory::FactoryCanImport( const FString& Filename )
return Filename.EndsWith("ink");
}

UObject* UInkpotStoryAssetFactory::FactoryCreateFile( UClass* InClass, UObject* InParent, FName InName, EObjectFlags InFlags, const FString& InFilename, const TCHAR* InParms, FFeedbackContext* InWarn, bool& bOutOperationCanceled )
UObject* UInkpotStoryAssetFactory::FactoryCreateFile( UClass* InClass, UObject* InParent, FName InName, EObjectFlags InFlags, const FString& InFullFilePath, const TCHAR* InParms, FFeedbackContext* InWarn, bool& bOutOperationCanceled )
{
const FString fileExtension = FPaths::GetExtension(InFilename);
const FString fileExtension = FPaths::GetExtension(InFullFilePath);
GEditor->GetEditorSubsystem<UImportSubsystem>()->BroadcastAssetPreImport(this, InClass, InParent, InName, *fileExtension);

UInkpotStoryAsset *newObject = nullptr;
FString inkStory;
FString inkJSON;
if( LoadAndCompileStory( InFilename, inkStory, inkJSON ) )
if( LoadAndCompileStory(InFullFilePath, inkStory, inkJSON ) )
{
newObject = NewObject<UInkpotStoryAsset>( InParent, InClass, InName, InFlags );
newObject->SetSource( inkStory );
newObject->SetCompiledJSON( inkJSON );
newObject->UpdateAssetInfo( InFilename );
newObject->UpdateAssetInfo(InFullFilePath);

//DumpStrings(newObject);
}

if(!newObject)
Expand All @@ -45,6 +49,32 @@ UObject* UInkpotStoryAssetFactory::FactoryCreateFile( UClass* InClass, UObject*
return newObject;
}

void UInkpotStoryAssetFactory::DumpStrings(UInkpotStoryAsset* InStoryAsset) const
{
UInkpot* inkpot = GEngine->GetEngineSubsystem<UInkpot>();
UInkpotStory *story = inkpot->BeginStory(InStoryAsset);
TMap<FString, FString> strings;
story->GatherAllStrings(strings);

FString filecontent = FString::Printf(TEXT("\"Name\",\"InkText\",\"LocText\"\n"));

for (auto entry : strings)
{
const FString& key = entry.Key;
const FString& val = entry.Value;
FString line = FString::Printf(TEXT("%s,\"%s\",\"%s\"\n"), *key, *val, *val );
filecontent += line;
}

FString fullPathAndName = InStoryAsset->GetAssetImportData()->GetFirstFilename();
FString path = FPaths::GetPath(fullPathAndName);
FString inkname = FPaths::GetBaseFilename(fullPathAndName);
inkname += TEXT("_");
FString newPathAndName = FPaths::CreateTempFilename(*path, *inkname, TEXT(".csv"));

FFileHelper::SaveStringToFile( filecontent, *newPathAndName);
}

bool UInkpotStoryAssetFactory::LoadAndCompileStory( const FString& InFilename, FString &OutStory, FString &OutCompiledStory) const
{
if ( !FFileHelper::LoadFileToString( OutStory, *InFilename ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ class INKPOTEDITOR_API UInkpotStoryAssetFactory : public UFactory, public FReimp
//~ End FReimportHandler Interface

private:
bool LoadAndCompileStory( const FString& InFilename, FString &OutStory, FString &OutCompiledStory) const;
bool LoadAndCompileStory(const FString& InFilename, FString& OutStory, FString& OutCompiledStory) const;
void DumpStrings(UInkpotStoryAsset *InStoryAsset) const;
};

0 comments on commit 7b4c82c

Please sign in to comment.