Skip to content

Commit

Permalink
Generate standalone executable to compile scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
drojf committed Aug 19, 2024
1 parent 54f79a1 commit d74c1e8
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 7 deletions.
10 changes: 7 additions & 3 deletions Assembly-CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<ProjectGuid>{0BA774AA-B2B4-468C-B002-CAA0652C466E}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>Library</OutputType>
<OutputType>Exe</OutputType>
<LangVersion>7.1</LangVersion>
<AssemblyName>Assembly-CSharp</AssemblyName>
<AssemblyName>HigurashiScriptCompiler</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
Expand All @@ -27,7 +27,10 @@
<Optimize>true</Optimize>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<DefineConstants>USE_CONSOLE_LOGGING</DefineConstants>
<DefineConstants>STANDALONE_SCRIPT_COMPILER</DefineConstants>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="System">
Expand Down Expand Up @@ -55,6 +58,7 @@
<Compile Include="BGICompiler.Compiler\BGIParameters.cs" />
<Compile Include="BGICompiler.Compiler\BGItoMG.cs" />
<Compile Include="BGICompiler.Compiler\BGIValue.cs" />
<Compile Include="BGICompiler.Compiler\NoUnityScriptCompiler.cs" />
<Compile Include="BGICompiler.Compiler\OperationHandler.cs" />
<Compile Include="BGICompiler.Compiler\OpType.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
5 changes: 1 addition & 4 deletions BGICompiler.Compiler/LoggerWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// Comment out to use Console.WriteLine(...)
// #define USE_CONSOLE_LOGGING

using System;

namespace BGICompiler.Compiler.Logger
{
class Debug
{
#if USE_CONSOLE_LOGGING
#if STANDALONE_SCRIPT_COMPILER
public static void Log(object message)
{
print($"INFO", message);
Expand Down
90 changes: 90 additions & 0 deletions BGICompiler.Compiler/NoUnityScriptCompiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#if STANDALONE_SCRIPT_COMPILER

using System;
using System.IO;
using BGICompiler.Compiler.Logger;

namespace BGICompiler.Compiler
{
internal class NoUnityScriptCompiler
{
private static bool SaveCompileStatus(int numTotal, int numPass, int numFail)
{
bool allCompiledOK = numPass == numTotal;
bool atLeastOneCompiled = numPass != 0;
bool pass = allCompiledOK && atLeastOneCompiled;

// Also consider compilation a failure if no scripts were compiled
string statusString = pass ? "Compile OK" : "FAIL";
statusString += $" | {numPass}/{numTotal} compiled and {numFail} failed";
File.WriteAllText("higu_script_compile_status.txt", statusString);

return pass;
}

private static bool CompileFolder(string srcDir, string destDir)
{
string[] txtPaths = Directory.GetFiles(srcDir, "*.txt");
int numTotal = txtPaths.Length;

int numPass = 0;
int numFail = 0;
int progress = 0;
foreach (string txtPath in txtPaths)
{
progress++;

try
{
string txtPathNoExt = Path.GetFileNameWithoutExtension(txtPath);
string mgPath = Path.Combine(destDir, txtPathNoExt) + ".mg";
Debug.Log($"Compiling [{progress}/{numTotal}] {txtPath} -> {mgPath}...");
new BGItoMG(txtPath, mgPath);
numPass++;
}
catch (Exception e)
{
Debug.LogWarning($"Failed to compile script {txtPath}!\r\n{e}");
numFail++;
}
}

return SaveCompileStatus(numTotal, numPass, numFail);
}

public static int Main(string[] args)
{
if(args.Length < 2)
{
Debug.LogError($"Got {args.Length} args but need at least two args:\n" +
$" 1. path to the 'Update' (.txt) folder\n" +
$" 2. path to the 'CompiledUpdateScripts' folder (.mg)\n" +
$"Example: [HigurashiScriptCompiler Update CompiledUpdateScripts] when called from inside the StreamingAssets folder.");
return -1;
}

Debug.Log($"Got {args.Length} args");

string txtFolderPath = args[0];
string mgFolderPath = args[1];

Debug.Log($"Compiling Scripts In {txtFolderPath} -> {mgFolderPath}");

if(!Directory.Exists(txtFolderPath))
{
Debug.LogError($"Source .txt script folder does not exist at [{txtFolderPath}]");
return -1;
}

if (!Directory.Exists(mgFolderPath))
{
Debug.LogError($"Destination .mg script folder does not exist at [{mgFolderPath}]");
return -1;
}

return CompileFolder(txtFolderPath, mgFolderPath) ? 0 : -1;
}
}
}

#endif

0 comments on commit d74c1e8

Please sign in to comment.