Skip to content

Commit 35015ca

Browse files
authored
Merge pull request #19 from Legacy-LuaSTG-Engine/feature/stdout-redirect
适配 LuaSTG Sub v0.21.118 stdout 重定向
2 parents f5c27b4 + eff5452 commit 35015ca

6 files changed

Lines changed: 61 additions & 107 deletions

File tree

LuaSTGEditorSharp.Core/Execution/Execution.cs

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,46 +46,65 @@ public virtual void Run(Logger logger, Action end)
4646
CreateNoWindow = CreateNoWindow,
4747
WorkingDirectory = WorkingDirectory,
4848
RedirectStandardError = RedirectStandardError,
49-
RedirectStandardOutput = RedirectStandardOutput
49+
RedirectStandardOutput = RedirectStandardOutput,
50+
StandardErrorEncoding = RedirectStandardError ? Encoding.UTF8 : null,
51+
StandardOutputEncoding = RedirectStandardOutput ? Encoding.UTF8 : null,
5052
}
5153
};
5254
LSTGInstance.EnableRaisingEvents = true;
53-
LSTGInstance.Start();
54-
55-
logger("LuaSTG is Running.\n\n");
56-
57-
LSTGInstance.Exited += (s, e) => {
58-
FileStream fs = null;
59-
StreamReader sr = null;
60-
StringBuilder sb = new StringBuilder();
61-
try
55+
56+
if (RedirectStandardOutput)
57+
{
58+
LSTGInstance.OutputDataReceived += (s, e) => logger(e.Data);
59+
LSTGInstance.Exited += (s, e) =>
6260
{
63-
fs = new FileStream(Path.GetFullPath(Path.Combine(
64-
Path.GetDirectoryName(LuaSTGPath), LogFileName)), FileMode.Open);
65-
sr = new StreamReader(fs);
66-
int i = 0;
67-
while (!sr.EndOfStream && i < 8192)
61+
end();
62+
logger("\nExited with code " + LSTGInstance.ExitCode + ".");
63+
};
64+
}
65+
else
66+
{
67+
LSTGInstance.Exited += (s, e) => {
68+
FileStream fs = null;
69+
StreamReader sr = null;
70+
StringBuilder sb = new StringBuilder();
71+
try
72+
{
73+
fs = new FileStream(Path.GetFullPath(Path.Combine(
74+
Path.GetDirectoryName(LuaSTGPath), LogFileName)), FileMode.Open);
75+
sr = new StreamReader(fs);
76+
int i = 0;
77+
while (!sr.EndOfStream && i < 8192)
78+
{
79+
sb.Append(sr.ReadLine());
80+
sb.Append("\n");
81+
i++;
82+
}
83+
logger(sb.ToString());
84+
end();
85+
}
86+
catch (System.Exception exc)
87+
{
88+
System.Windows.MessageBox.Show(exc.ToString());
89+
}
90+
finally
6891
{
69-
sb.Append(sr.ReadLine());
70-
sb.Append("\n");
71-
i++;
92+
if (fs != null) fs.Close();
93+
if (sr != null) sr.Close();
7294
}
95+
sb.Append("\nExited with code " + LSTGInstance.ExitCode + ".");
7396
logger(sb.ToString());
74-
end();
75-
}
76-
catch (System.Exception exc)
77-
{
78-
System.Windows.MessageBox.Show(exc.ToString());
79-
}
80-
finally
81-
{
82-
if (fs != null) fs.Close();
83-
if (sr != null) sr.Close();
84-
}
85-
sb.Append("\nExited with code " + LSTGInstance.ExitCode + ".");
86-
logger(sb.ToString());
87-
};
97+
};
98+
}
8899

100+
LSTGInstance.Start();
101+
102+
logger("LuaSTG is Running.\n\n");
103+
104+
if (RedirectStandardOutput)
105+
{
106+
LSTGInstance.BeginOutputReadLine();
107+
}
89108
}
90109
else
91110
{

LuaSTGEditorSharp/MainWindow.xaml.cs

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -720,81 +720,14 @@ private void FinishPackaging(object sender, RunWorkerCompletedEventArgs args)
720720

721721
private void RunLuaSTG(App currentApp, CompileProcess process)
722722
{
723-
/*
724-
string LuaSTGparam = "\"" +
725-
"start_game=true is_debug=true setting.nosplash=true setting.windowed="
726-
+ currentApp.DebugWindowed.ToString().ToLower() + " setting.resx=" + currentApp.DebugResolutionX +
727-
" setting.resy=" + currentApp.DebugResolutionY + " cheat=" + currentApp.DebugCheat.ToString().ToLower() +
728-
" updatelib=" + currentApp.DebugUpdateLib.ToString().ToLower() + " setting.mod=\'"
729-
+ process.projName + "\'\"";
730-
try
731-
{
732-
if (lstgInstance == null || lstgInstance.HasExited)
733-
{
734-
lstgInstance = new Process
735-
{
736-
StartInfo = new ProcessStartInfo(process.luaSTGExePath, LuaSTGparam)
737-
{
738-
UseShellExecute = false,
739-
CreateNoWindow = true,
740-
WorkingDirectory = process.luaSTGFolder,
741-
RedirectStandardError = true,
742-
RedirectStandardOutput = true
743-
}
744-
};
745-
lstgInstance.Start();
746-
DebugString += "LuaSTG is Running.\n\n";
747-
*/
748-
/*
749-
* what it should be like:
750-
*
751-
lstg.OutputDataReceived += (s, e) => DebugString += e.Data;
752-
lstg.ErrorDataReceived += (s, e) => DebugString += e.Data;
753-
*
754-
* what it actually is:
755-
*/
756-
/*
757-
lstgInstance.Exited += (s, e) => {
758-
FileStream fs = null;
759-
StreamReader sr = null;
760-
try
761-
{
762-
fs = new FileStream(Path.GetFullPath(Path.Combine(
763-
Path.GetDirectoryName(process.luaSTGExePath), "log.txt")), FileMode.Open);
764-
sr = new StreamReader(fs);
765-
DebugString += sr.ReadToEnd();
766-
//debugOutput.ScrollToEnd();
767-
}
768-
finally
769-
{
770-
if (fs != null) fs.Close();
771-
if (sr != null) sr.Close();
772-
}
773-
DebugString += "\nExited with code " + lstgInstance.ExitCode + ".";
774-
};
775-
lstgInstance.EnableRaisingEvents = true;
776-
lstgInstance.BeginOutputReadLine();
777-
lstgInstance.BeginErrorReadLine();
778-
//lstg.WaitForExit();
779-
}
780-
else
781-
{
782-
MessageBox.Show("LuaSTG is already running, please exit first."
783-
, "LuaSTG Editor Sharp", MessageBoxButton.OK, MessageBoxImage.Error);
784-
}
785-
}
786-
catch (Win32Exception)
787-
{
788-
throw new EXEPathNotSetException();
789-
}
790-
*/
791723
PluginHandler.Plugin.Execution.BeforeRun(new ExecutionConfig()
792724
{
793725
ModName = process.projName
794726
});
727+
DebugString = "";
795728
PluginHandler.Plugin.Execution.Run((s) =>
796729
{
797-
DebugString = s;
730+
DebugString += s + "\n";
798731
}
799732
, () => App.Current.Dispatcher.Invoke(() => debugOutput.ScrollToEnd()));
800733
}

LuaSTGEditorSharp/Windows/SettingsWindow.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ private void WriteSettings()
470470
PluginPathSettings = PluginPath;
471471
TempPathSettings = TempPath;
472472
ZipExecutablePathSettings = ZipExecutablePath;
473+
DynamicDebugReportingSettings = DynamicDebugReporting;
473474
SpaceIndentationSettings = SpaceIndentation;
474475
IndentationSpaceLengthSettings = IndentationSpaceLength;
475476
TabDisplayWidthSettings = TabDisplayWidth;
@@ -503,6 +504,7 @@ private void ReadSettings()
503504
PluginPath = PluginPathSettings;
504505
TempPath = TempPathSettings;
505506
ZipExecutablePath = ZipExecutablePathSettings;
507+
DynamicDebugReporting = DynamicDebugReportingSettings;
506508
SpaceIndentation = SpaceIndentationSettings;
507509
IndentationSpaceLength = IndentationSpaceLengthSettings;
508510
TabDisplayWidth = TabDisplayWidthSettings;

LuaSTGPlusLib/LSTGEXPlusExecution.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public override void BeforeRun(ExecutionConfig config)
2020
+ config.ModName + "\'\"";
2121
UseShellExecute = false;
2222
CreateNoWindow = true;
23-
RedirectStandardError = true;
24-
RedirectStandardOutput = true;
23+
RedirectStandardError = false;
24+
RedirectStandardOutput = false;
2525
}
2626

2727
protected override string LogFileName => "log.txt";

LuaSTGSubLib/LSTGSubExecution.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public override void BeforeRun(ExecutionConfig config)
2020
+ config.ModName + "\'\"";
2121
UseShellExecute = false;
2222
CreateNoWindow = true;
23-
RedirectStandardError = true;
24-
RedirectStandardOutput = true;
23+
RedirectStandardError = false;
24+
RedirectStandardOutput = currentApp.DynamicDebugReporting; // LuaSTG Sub v0.21.118+ supports stdout redirect
2525
}
2626

2727
protected override string LogFileName => "engine.log";

LuaSTGXLib.Legacy/LSTGXExecution.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public override void BeforeRun(ExecutionConfig config)
2020
+ config.ModName + "\'\"";
2121
UseShellExecute = false;
2222
CreateNoWindow = true;
23-
RedirectStandardError = true;
24-
RedirectStandardOutput = true;
23+
RedirectStandardError = false;
24+
RedirectStandardOutput = false;
2525
}
2626

2727
protected override string LogFileName => "lstg_log.txt";

0 commit comments

Comments
 (0)