Skip to content

Commit

Permalink
Add Default values for configuration items
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Jan 26, 2021
1 parent 0e3399e commit 2b458b9
Show file tree
Hide file tree
Showing 23 changed files with 284 additions and 194 deletions.
16 changes: 13 additions & 3 deletions BlocklyAtsGui/BaseBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ public async Task BkyResetWorkspace() {
}

public async Task<XElement> BkySaveWorkspace() {
var element = XElement.Parse((await InvokeScript("batsWkspSave();")).ToString());
var workspaceString = (await InvokeScript("batsWkspSave();"))?.ToString();
if (workspaceString == null) return null;
var element = XElement.Parse(workspaceString);
element.RemoveAttributes();
return element;
}
Expand All @@ -112,12 +114,20 @@ public async Task BkyLoadWorkspace(XElement bkyxml) {
await InvokeScript(string.Format("batsWkspLoad({0});", arg));
}

public async Task BkyLoadInitWorkspace(XElement bkyxml) {
var arg = EscapeJsString(bkyxml.ToString(SaveOptions.DisableFormatting));
await InvokeScript(string.Format(
"if (workspace == null) window.onBlocklyLoad = function() {{batsWkspLoad({0});}}; else batsWkspLoad({0});",
arg
));
}

public async Task<string> BkyExportLua() {
return (await InvokeScript("batsWkspExportLua();")).ToString();
return (await InvokeScript("batsWkspExportLua();"))?.ToString();
}

public async Task<string> BkyExportCSharp() {
return (await InvokeScript("batsWkspExportCSharp();")).ToString();
return (await InvokeScript("batsWkspExportCSharp();"))?.ToString();
}
}
}
19 changes: 19 additions & 0 deletions BlocklyAtsGui/CompilerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
Expand Down Expand Up @@ -32,8 +33,15 @@ public CompilerConfig Clone() {
return (CompilerConfig)this.MemberwiseClone();
}

#if !MONO
[DllImport("msi.dll", CharSet = CharSet.Unicode)]
private static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len);
#endif

public Process GetGameProcess() {
var path = GamePath;

// OpenBVE
if (string.IsNullOrEmpty(path)) {
if (PlatformFunction.IsWindows()) {
const string InnoAppID = "{D617A45D-C2F6-44D1-A85C-CA7FFA91F7FC}_is1";
Expand All @@ -53,6 +61,17 @@ public Process GetGameProcess() {
// TODO: Support them
}
}

#if !MONO
// BVE5
/*if (string.IsNullOrEmpty(path) && PlatformFunction.IsWindows()) {
Int32 len = 512;
StringBuilder builder = new StringBuilder(len);
MsiGetProductInfo("{D38EB8AB-0772-473D-9443-9B2149E4F13D}", "LocalPackage", builder, ref len);
string str = builder.ToString();
}*/
#endif

if (string.IsNullOrEmpty(path) || !File.Exists(path)) {
return null;
}
Expand Down
7 changes: 5 additions & 2 deletions BlocklyAtsGui/CompilerFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ static class CompilerFunction {
return process.HasExited ? Task.CompletedTask : tcs.Task;
}

public static string BoilerplateLua = File.ReadAllText(Path.Combine(appDir, "lib", "boilerplate.lua"));
public static string BoilerplateCSharp = File.ReadAllText(Path.Combine(appDir, "lib", "boilerplate.cs"));

public static async Task CompileLua(string script, string outputPath, string arch) {
/*var proc = new Process {
StartInfo = new ProcessStartInfo {
Expand All @@ -51,7 +54,7 @@ public static async Task CompileLua(string script, string outputPath, string arc
}
var luaByteStream = new MemoryStream();*/

var sourceCode = File.ReadAllText(Path.Combine(appDir, "lib", "boilerplate.lua")) + script;
var sourceCode = BoilerplateLua + script;

var boilerplateStream = new FileStream(
Path.Combine(appDir, "lib", "batswinapi_" + arch + ".dll"),
Expand Down Expand Up @@ -80,7 +83,7 @@ public static async Task CompileLua(string script, string outputPath, string arc
}

public static void CompileCSharp(string script, string outputPath) {
var sourceCode = File.ReadAllText(Path.Combine(appDir, "lib", "boilerplate.cs")) + script;
var sourceCode = BoilerplateCSharp + script;
var settings = new Dictionary<string, string>() {
{ "CompilerVersion", "v4.0" }
};
Expand Down
4 changes: 2 additions & 2 deletions BlocklyAtsGui/FormDebug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public FormDebug() {

private void rbLua_CheckedChanged(object sender, EventArgs e) {
if (sender == rbLua) {
tbCode.Text = codeLua;
tbCode.Text = CompilerFunction.BoilerplateLua + codeLua;
} else {
tbCode.Text = codeCSharp;
tbCode.Text = CompilerFunction.BoilerplateCSharp + codeCSharp;
}
}

Expand Down
27 changes: 21 additions & 6 deletions BlocklyAtsGui/FormMain.Designer.cs

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

83 changes: 53 additions & 30 deletions BlocklyAtsGui/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,18 @@ public FormMain() {

private Workspace currentWorkspace = new Workspace();

private void FormMain_Load(object sender, EventArgs e) {
#if DEBUG
string webDirectory = Path.Combine(Path.GetDirectoryName(CompilerFunction.appDir), "www");
if (!Directory.Exists(webDirectory)) webDirectory = Path.Combine(CompilerFunction.appDir, "www");
#else
string webDirectory = Path.Combine(CompilerFunction.appDir, "www");
#endif
string pageURL = Path.Combine(webDirectory, "index.html") + string.Format("?ver={0}&lang={1}",
Assembly.GetExecutingAssembly().GetName().Version.ToString(),
I18n.Translate("BlocklyName")
);
mainWebBrowser = BaseBrowser.AcquireInstance(pageURL);
mainWebBrowser.KeyDown += new PreviewKeyDownEventHandler(mainWebBrowser_PreviewKeyDown);
this.PreviewKeyDown += new PreviewKeyDownEventHandler(mainWebBrowser_PreviewKeyDown);
mainWebBrowser.BindTo(this);
updateSaveFileState();
private async void FormMain_Load(object sender, EventArgs e) {

foreach (ToolStripItem item in mainToolStrip.Items) {
if (I18n.CanTranslate("FormMain." + item.Name)) {
item.Text = I18n.Translate("FormMain." + item.Name);
}
}
for (int i = 0; i < I18n.LanguageDisplayList.Count; i++) {
tscbLanguage.Items.Add(I18n.LanguageDisplayList[i].Value);
if (I18n.LanguageDisplayList[i].Key == I18n.SelectedLanguage) {
tscbLanguage.SelectedIndex = i;
tscbLanguage.Text = I18n.LanguageDisplayList[i].Value;
}
}
await ApplyLanguage();

Task.Run(async () => {
await Task.Run(async () => {
var info = await UpgradeInfo.FetchOnline(
"https://www.zbx1425.cn/nautilus/projectmeta.xml",
"BlocklyAts"
Expand All @@ -66,14 +47,47 @@ private void FormMain_Load(object sender, EventArgs e) {
});
}

private async Task ApplyLanguage() {
foreach (ToolStripItem item in mainToolStrip.Items) {
if (I18n.CanTranslate("FormMain." + item.Name)) {
item.Text = I18n.Translate("FormMain." + item.Name);
}
}
updateSaveFileState();
#if DEBUG
string webDirectory = Path.Combine(Path.GetDirectoryName(CompilerFunction.appDir), "www");
if (!Directory.Exists(webDirectory)) webDirectory = Path.Combine(CompilerFunction.appDir, "www");
#else
string webDirectory = Path.Combine(CompilerFunction.appDir, "www");
#endif
string pageURL = Path.Combine(webDirectory, "index.html") + string.Format("?ver={0}&lang={1}",
Assembly.GetExecutingAssembly().GetName().Version.ToString(),
I18n.Translate("BlocklyName")
);
if (mainWebBrowser == null) {
mainWebBrowser = BaseBrowser.AcquireInstance(pageURL);
mainWebBrowser.KeyDown += new PreviewKeyDownEventHandler(mainWebBrowser_PreviewKeyDown);
this.PreviewKeyDown += new PreviewKeyDownEventHandler(mainWebBrowser_PreviewKeyDown);
mainWebBrowser.BindTo(this);
} else {
var workspaceState = await mainWebBrowser.BkySaveWorkspace();
if (workspaceState == null) return;
currentWorkspace.BlocklyXml = new FPXElement(workspaceState);
mainWebBrowser.Navigate(pageURL);
EventHandler loadHandler = null;
loadHandler = (EventHandler)(async (sender, e) => {
await mainWebBrowser.BkyLoadInitWorkspace(currentWorkspace.BlocklyXml);
mainWebBrowser.PageFinished -= loadHandler;
});
mainWebBrowser.PageFinished += loadHandler;
}
}

private async void mainWebBrowser_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
if (e.KeyCode == Keys.F5) {
if (ModifierKeys.HasFlag(Keys.Control) && ModifierKeys.HasFlag(Keys.Shift)) {
// Debug function.
currentWorkspace.BlocklyXml = new FPXElement(await mainWebBrowser.BkySaveWorkspace());
mainWebBrowser.Reload();
await Task.Delay(2000);
await mainWebBrowser.BkyLoadWorkspace(currentWorkspace.BlocklyXml);
await ApplyLanguage();
} else {
tsbtnCompileRun_Click(null, null);
}
Expand Down Expand Up @@ -128,7 +142,9 @@ private async void tsbtnSaveAs_Click(object sender, EventArgs e) {
};
if (sfd.ShowDialog() != DialogResult.OK) return;

currentWorkspace.BlocklyXml = new FPXElement(await mainWebBrowser.BkySaveWorkspace());
var workspaceState = await mainWebBrowser.BkySaveWorkspace();
if (workspaceState == null) return;
currentWorkspace.BlocklyXml = new FPXElement(workspaceState);
currentWorkspace.SaveToFile(sfd.FileName);
updateSaveFileState();
}
Expand Down Expand Up @@ -199,7 +215,9 @@ private async void tsbtnCompile_Click(object sender, EventArgs e) {

private async void tsbtnSave_Click(object sender, EventArgs e) {
if (string.IsNullOrEmpty(currentWorkspace.SaveFilePath)) return;
currentWorkspace.BlocklyXml = new FPXElement(await mainWebBrowser.BkySaveWorkspace());
var workspaceState = await mainWebBrowser.BkySaveWorkspace();
if (workspaceState == null) return;
currentWorkspace.BlocklyXml = new FPXElement(workspaceState);
currentWorkspace.SaveToFile();
}

Expand Down Expand Up @@ -248,12 +266,17 @@ private void tsbtnAbout_Click(object sender, EventArgs e) {
new FormAbout().ShowDialog();
}

private void tscbLanguage_SelectedIndexChanged(object sender, EventArgs e) {
private async void tscbLanguage_SelectedIndexChanged(object sender, EventArgs e) {
var newlang = I18n.LanguageDisplayList[tscbLanguage.SelectedIndex].Key;
if (newlang != I18n.SelectedLanguage) {
I18n.SelectedLanguage = newlang;
MessageBox.Show(I18n.Translate("Msg.LanguageChange"));
await ApplyLanguage();
//MessageBox.Show(I18n.Translate("Msg.LanguageChange"));
}
}

private void tsbtnHelp_Click(object sender, EventArgs e) {
PlatformFunction.CallBrowser("https://github.com/zbx1425/BlocklyAts/wiki");
}
}
}
Loading

0 comments on commit 2b458b9

Please sign in to comment.