Skip to content

Commit

Permalink
Fix relative output path
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Jul 26, 2021
1 parent f741c68 commit d2d37d9
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 95 deletions.
5 changes: 2 additions & 3 deletions BlocklyAtsGui/Host/PlatformFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ namespace BlocklyAts.Host {
static class PlatformFunction {

public static readonly string AppDir = Path.GetDirectoryName(Application.ExecutablePath);

public static readonly string AppName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);

static readonly string featureControlRegKey = @"Software\Microsoft\Internet Explorer\Main\FeatureControl\";

public static bool IsMono { get; } = Type.GetType("Mono.Runtime") != null;
Expand Down Expand Up @@ -66,6 +63,7 @@ public static void SetWebBrowserFeatures() {
if (IsMono) return;
// don't change the registry if running in-proc inside Visual Studio
if (LicenseManager.UsageMode != LicenseUsageMode.Runtime) return;
var AppName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);

Registry.CurrentUser.CreateSubKey(featureControlRegKey + "FEATURE_BROWSER_EMULATION", true)
.SetValue(AppName, GetBrowserEmulationMode(), RegistryValueKind.DWord);
Expand All @@ -88,6 +86,7 @@ public static void UnsetWebBrowserFeatures() {
// don't change the registry if running in-proc inside Visual Studio
if (LicenseManager.UsageMode != LicenseUsageMode.Runtime) return;

var AppName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
Registry.CurrentUser.OpenSubKey(featureControlRegKey + "FEATURE_BROWSER_EMULATION", true)
?.DeleteValue(AppName);

Expand Down
2 changes: 1 addition & 1 deletion BlocklyAtsGui/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.0.1")]
[assembly: AssemblyVersion("1.1.0.2")]
32 changes: 23 additions & 9 deletions BlocklyAtsGui/UserInterface/FormDebug.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions BlocklyAtsGui/UserInterface/FormDebug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ private void ResetTextbox(string text) {
tbCode.Zoom = 100;
tbCode.Language = FastColoredTextBoxNS.Language.CSharp;
tbCode.Text = text;
tbCode.SelectionChanged += (object sender, EventArgs e) => {
var selection = tbCode.Selection.Start;
if (selection == null) {
selection = new FastColoredTextBoxNS.Place(0, 0);
}
lblCursorPos.Text = string.Format("Row: {0}; Col: {1}",
selection.iLine + 1, selection.iChar + 1);
};
lblCursorPos.Text = "Row: 0; Col: 0";
pnlMain.Controls.Add(tbCode);
((ISupportInitialize)(tbCode)).EndInit();
} else {
Expand All @@ -75,6 +84,7 @@ private void ResetTextbox(string text) {
fallbackTbCode.Dock = DockStyle.Fill;
fallbackTbCode.TabIndex = 2;
fallbackTbCode.Text = text;
lblCursorPos.Text = "";
pnlMain.Controls.Add(fallbackTbCode);
}
ResumeLayout(false);
Expand Down
144 changes: 73 additions & 71 deletions BlocklyAtsGui/Workspace/CompilerFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,94 +99,96 @@ public static void Compile(string script, string outputPath, Platform platform,
const string defaultModuleName = "AtsCallConverter00000000000000000000000000000000";
var randomModuleName = "AtsCallConverter" + Guid.NewGuid().ToString("N");
// Prevent module name conflicts
var tempOutputPath = Path.Combine(Path.GetDirectoryName(outputPath),
var tempOutputPath = Path.Combine(Path.GetDirectoryName(outputPath),
"BlocklyAtsGenerated-" + Guid.NewGuid() + ".dll");
var modifiedProxyDllPath = Path.Combine(Path.GetDirectoryName(outputPath), randomModuleName + ".dll");
var sourceCode = CombineCode(script, platform);

var settings = new Dictionary<string, string>() {
{ "CompilerVersion", "v4.0" }
};
CSharpCodeProvider codeProvider = new CSharpCodeProvider(settings);
CompilerParameters parameters = new CompilerParameters() {
IncludeDebugInformation = includePDB,
GenerateExecutable = false,
OutputAssembly = tempOutputPath,
};
if (!includePDB) parameters.CompilerOptions += "/optimize";

string[] commonReferences = new string[] {
"mscorlib.dll",
"System.Core.dll",
"System.dll",
"Microsoft.CSharp.dll",
"System.Windows.Forms.dll"
};
foreach (string a in commonReferences) parameters.ReferencedAssemblies.Add(a);
string modifiedProxyDllPath = null;
if (platform == Platform.OpenBve) {
parameters.ReferencedAssemblies.Add(Path.Combine(PlatformFunction.AppDir, "lib", "OpenBveApi.dll"));
} else if (platform == Platform.WinDll32 || platform == Platform.WinDll64) {
var boilerplateFile = Path.Combine(
PlatformFunction.AppDir,
"lib",
platform == Platform.WinDll32 ? "AtsCallConverter_x86.dll" : "AtsCallConverter_x64.dll"
);
modifiedProxyDllPath = Path.Combine(Path.GetDirectoryName(outputPath), randomModuleName + ".dll");
byte[] srcBytes = File.ReadAllBytes(boilerplateFile);
byte[] classNameBytes = Encoding.ASCII.GetBytes(randomModuleName);

// You need to use different module names for each plugin.
// Otherwise, the type references will mess up when multiple plugins are loaded by DetailManager.
ReplaceBytes(ref srcBytes, Encoding.ASCII.GetBytes(defaultModuleName),
Encoding.ASCII.GetBytes(randomModuleName));
ReplaceBytes(ref srcBytes, Encoding.Unicode.GetBytes(defaultModuleName),
Encoding.Unicode.GetBytes(randomModuleName));
File.WriteAllBytes(modifiedProxyDllPath, srcBytes);
parameters.ReferencedAssemblies.Add(modifiedProxyDllPath);
}

CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, sourceCode);
if (results.Errors.HasErrors) {
if (modifiedProxyDllPath != null) File.Delete(modifiedProxyDllPath);
if (tempOutputPath != null) File.Delete(tempOutputPath);
throw new CompileException(results.Errors);
}
try {
CSharpCodeProvider codeProvider = new CSharpCodeProvider(settings);
CompilerParameters parameters = new CompilerParameters() {
IncludeDebugInformation = includePDB,
GenerateExecutable = false,
OutputAssembly = tempOutputPath,
};
if (!includePDB) parameters.CompilerOptions += "/optimize";

foreach (string a in commonReferences) parameters.ReferencedAssemblies.Add(a);
if (platform == Platform.OpenBve) {
parameters.ReferencedAssemblies.Add(Path.Combine(PlatformFunction.AppDir, "lib", "OpenBveApi.dll"));
} else if (platform == Platform.WinDll32 || platform == Platform.WinDll64) {
var boilerplateFile = Path.Combine(
PlatformFunction.AppDir,
"lib",
platform == Platform.WinDll32 ? "AtsCallConverter_x86.dll" : "AtsCallConverter_x64.dll"
);
byte[] srcBytes = File.ReadAllBytes(boilerplateFile);
byte[] classNameBytes = Encoding.ASCII.GetBytes(randomModuleName);

// You need to use different module names for each plugin.
// Otherwise, the type references will mess up when multiple plugins are loaded by DetailManager.
ReplaceBytes(ref srcBytes, Encoding.ASCII.GetBytes(defaultModuleName),
Encoding.ASCII.GetBytes(randomModuleName));
ReplaceBytes(ref srcBytes, Encoding.Unicode.GetBytes(defaultModuleName),
Encoding.Unicode.GetBytes(randomModuleName));
File.WriteAllBytes(modifiedProxyDllPath, srcBytes);
parameters.ReferencedAssemblies.Add(modifiedProxyDllPath);
}

if (File.Exists(outputPath)) File.Delete(outputPath);
if (platform == Platform.OpenBve) {
File.Copy(tempOutputPath, outputPath);
var targetPdbFile = Path.ChangeExtension(outputPath, ".pdb");
if (File.Exists(targetPdbFile)) File.Delete(targetPdbFile);
File.Move(Path.ChangeExtension(tempOutputPath, ".pdb"), targetPdbFile);
} else if (platform == Platform.WinDll32 || platform == Platform.WinDll64) {
var programData = new FileStream(tempOutputPath, FileMode.Open, FileAccess.Read);
var proxyStream = new FileStream(modifiedProxyDllPath, FileMode.Open, FileAccess.Read);
var outStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write);

// Write Identifier and PE length to DOS stub
byte[] identifier = Encoding.UTF8.GetBytes("BATSNET1");
proxyStream.CopySectionTo(outStream, 0x6C - identifier.Length);
outStream.Write(identifier, 0, identifier.Length);
outStream.Write(BitConverter.GetBytes(proxyStream.Length), 0, 4);
outStream.Write(BitConverter.GetBytes(programData.Length), 0, 4);
proxyStream.Seek(8 + identifier.Length, SeekOrigin.Current);
proxyStream.CopyTo(outStream);
proxyStream.Close();

programData.CopyTo(outStream);
programData.Close();
if (includePDB) {
var pdbFilePath = Path.ChangeExtension(tempOutputPath, ".pdb");
var pdbStream = new FileStream(pdbFilePath, FileMode.Open, FileAccess.Read);
pdbStream.CopyTo(outStream);
pdbStream.Close();
File.Delete(pdbFilePath);
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, sourceCode);
if (results.Errors.HasErrors) {
throw new CompileException(results.Errors);
}
outStream.Close();
}

if (modifiedProxyDllPath != null) File.Delete(modifiedProxyDllPath);
if (tempOutputPath != null) File.Delete(tempOutputPath);
if (File.Exists(outputPath)) File.Delete(outputPath);
if (platform == Platform.OpenBve) {
File.Copy(tempOutputPath, outputPath);
if (includePDB) {
var targetPdbFile = Path.ChangeExtension(outputPath, ".pdb");
if (File.Exists(targetPdbFile)) File.Delete(targetPdbFile);
File.Move(Path.ChangeExtension(tempOutputPath, ".pdb"), targetPdbFile);
}
} else if (platform == Platform.WinDll32 || platform == Platform.WinDll64) {
var programData = new FileStream(tempOutputPath, FileMode.Open, FileAccess.Read);
var proxyStream = new FileStream(modifiedProxyDllPath, FileMode.Open, FileAccess.Read);
var outStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write);

// Write Identifier and PE length to DOS stub
byte[] identifier = Encoding.UTF8.GetBytes("BATSNET1");
proxyStream.CopySectionTo(outStream, 0x6C - identifier.Length);
outStream.Write(identifier, 0, identifier.Length);
outStream.Write(BitConverter.GetBytes(proxyStream.Length), 0, 4);
outStream.Write(BitConverter.GetBytes(programData.Length), 0, 4);
proxyStream.Seek(8 + identifier.Length, SeekOrigin.Current);
proxyStream.CopyTo(outStream);
proxyStream.Close();

programData.CopyTo(outStream);
programData.Close();
if (includePDB) {
var pdbFilePath = Path.ChangeExtension(tempOutputPath, ".pdb");
var pdbStream = new FileStream(pdbFilePath, FileMode.Open, FileAccess.Read);
pdbStream.CopyTo(outStream);
pdbStream.Close();
File.Delete(pdbFilePath);
}
outStream.Close();
}
} finally {
if (modifiedProxyDllPath != null) File.Delete(modifiedProxyDllPath);
if (tempOutputPath != null) File.Delete(tempOutputPath);
}
}
}
}
6 changes: 5 additions & 1 deletion BlocklyAtsGui/Workspace/SaveState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ private string SelectDefaultCompilePath(string compilePath, string suffix) {
Path.GetFileNameWithoutExtension(SaveFilePath) + suffix
);
} else {
return compilePath;
if (Path.IsPathRooted(compilePath)) {
return compilePath;
} else {
return Path.GetFullPath(Path.Combine(Path.GetDirectoryName(SaveFilePath), compilePath));
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@
<block type="obve_sound_play"></block>
<block type="obve_preceding_vehicle"></block>
<block type="obve_next_station"></block>
<block type="obve_destination"></block>
<block type="obve_vehicle_state"></block>
<block type="obve_langcode"></block>
<block type="obve_show_message"></block>
<block type="obve_set_debug_message"></block>
Expand Down
2 changes: 2 additions & 0 deletions www/js/bats_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ function batsInit(toolboxNode) {
bve_hat_set_beacon: 1,
bve_hat_load: 1,
bve_hat_dispose: 1,
bve_hat_door_change_any: 1,
bve_hat_perform_ai: 1
},
});
workspace.setTheme(themeWithHat);
Expand Down
19 changes: 17 additions & 2 deletions www/js/blocks_bve.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,24 @@ Blockly.defineBlocksWithJsonArray([
output: "Number",
},
{
type: "obve_destination",
type: "obve_vehicle_state",
style: "openbve_blocks",
message0: "%{BKY_OBVE_DESTINATION}",
message0: "%{BKY_BVE_VEHICLE_STATE}",
args0: [
{
type: "field_dropdown",
name: "FIELD_SEL",
options: [
["%{BKY_OBVE_VSTATE_RADIUS}", "Radius"],
["%{BKY_OBVE_VSTATE_CANT}", "Cant"],
["%{BKY_OBVE_VSTATE_PITCH}", "Pitch"],
/* ["%{BKY_OBVE_VSTATE_ADHESION}", "Adhesion"],
["%{BKY_OBVE_VSTATE_RAININTENSITY}", "RainIntensity"], */
["%{BKY_OBVE_STATE_CAMERAVIEWMODE}", "CameraViewMode"],
["%{BKY_OBVE_STATE_DESTINATION}", "Destination"],
],
},
],
output: "Number",
},
{
Expand Down
17 changes: 13 additions & 4 deletions www/js/csharp_generator_bve.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function batsExportCSharp(workspace) {
code += indentString(Blockly.CSharp.blockToCode(block, true).trim(), 4, true);

if (block.type == "bve_hat_load" && !allHats.includes("bve_hat_perform_ai")) {
code += "_c.LProp.AISupport = AISupport.Basic;\n";
code += "\n _c.LProp.AISupport = AISupport.Basic;\n";
}

code += "\n }\n\n";
Expand All @@ -159,7 +159,11 @@ function batsExportCSharp(workspace) {
}
code += "\n";
for (var i = 0, hatName; hatName = allHats[i]; i++) {
code += " " + Blockly.CSharp[hatName]().trim() + " }\n";
code += " " + Blockly.CSharp[hatName]().trim();
if (hatName == "bve_hat_load" && !allHats.includes("bve_hat_perform_ai")) {
code += " _c.LProp.AISupport = AISupport.Basic;";
}
code += " }\n";
}

code = Blockly.CSharp.finish(code);
Expand Down Expand Up @@ -367,8 +371,13 @@ Blockly.CSharp.obve_next_station=function(block){
return [func + "." + block.getFieldValue("FIELD"), Blockly.CSharp.ORDER_MEMBER];
}
}
Blockly.CSharp.obve_destination=function(block){
return ["_c.EData.Destination", Blockly.CSharp.ORDER_MEMBER];
Blockly.CSharp.obve_vehicle_state=function(block){
var field = block.getFieldValue("FIELD_SEL");
if (field == "CameraViewMode" || field == "Destination") {
return ["_c.EData." + field, Blockly.CSharp.ORDER_MEMBER];
} else {
return ["_c.EData.Vehicle." + field, Blockly.CSharp.ORDER_MEMBER];
}
}
Blockly.CSharp.obve_langcode=function(block){
return ["_c.EData.CurrentLanguageCode", Blockly.CSharp.ORDER_MEMBER];
Expand Down
Loading

0 comments on commit d2d37d9

Please sign in to comment.